device-mapper.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. };
  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. 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. typedef int (*dm_request_endio_fn) (struct dm_target *ti,
  54. struct request *clone, int error,
  55. union map_info *map_context);
  56. typedef void (*dm_presuspend_fn) (struct dm_target *ti);
  57. typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
  58. typedef int (*dm_preresume_fn) (struct dm_target *ti);
  59. typedef void (*dm_resume_fn) (struct dm_target *ti);
  60. typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
  61. unsigned status_flags, char *result, unsigned maxlen);
  62. typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
  63. typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd,
  64. unsigned long arg);
  65. typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
  66. struct bio_vec *biovec, int max_size);
  67. typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
  68. struct dm_dev *dev,
  69. sector_t start, sector_t len,
  70. void *data);
  71. typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
  72. iterate_devices_callout_fn fn,
  73. void *data);
  74. typedef void (*dm_io_hints_fn) (struct dm_target *ti,
  75. struct queue_limits *limits);
  76. /*
  77. * Returns:
  78. * 0: The target can handle the next I/O immediately.
  79. * 1: The target can't handle the next I/O immediately.
  80. */
  81. typedef int (*dm_busy_fn) (struct dm_target *ti);
  82. void dm_error(const char *message);
  83. /*
  84. * Combine device limits.
  85. */
  86. int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  87. sector_t start, sector_t len, void *data);
  88. struct dm_dev {
  89. struct block_device *bdev;
  90. fmode_t mode;
  91. char name[16];
  92. };
  93. /*
  94. * Constructors should call these functions to ensure destination devices
  95. * are opened/closed correctly.
  96. */
  97. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  98. struct dm_dev **result);
  99. void dm_put_device(struct dm_target *ti, struct dm_dev *d);
  100. /*
  101. * Information about a target type
  102. */
  103. struct target_type {
  104. uint64_t features;
  105. const char *name;
  106. struct module *module;
  107. unsigned version[3];
  108. dm_ctr_fn ctr;
  109. dm_dtr_fn dtr;
  110. dm_map_fn map;
  111. dm_map_request_fn map_rq;
  112. dm_endio_fn end_io;
  113. dm_request_endio_fn rq_end_io;
  114. dm_presuspend_fn presuspend;
  115. dm_postsuspend_fn postsuspend;
  116. dm_preresume_fn preresume;
  117. dm_resume_fn resume;
  118. dm_status_fn status;
  119. dm_message_fn message;
  120. dm_ioctl_fn ioctl;
  121. dm_merge_fn merge;
  122. dm_busy_fn busy;
  123. dm_iterate_devices_fn iterate_devices;
  124. dm_io_hints_fn io_hints;
  125. /* For internal device-mapper use. */
  126. struct list_head list;
  127. };
  128. /*
  129. * Target features
  130. */
  131. /*
  132. * Any table that contains an instance of this target must have only one.
  133. */
  134. #define DM_TARGET_SINGLETON 0x00000001
  135. #define dm_target_needs_singleton(type) ((type)->features & DM_TARGET_SINGLETON)
  136. /*
  137. * Indicates that a target does not support read-only devices.
  138. */
  139. #define DM_TARGET_ALWAYS_WRITEABLE 0x00000002
  140. #define dm_target_always_writeable(type) \
  141. ((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
  142. /*
  143. * Any device that contains a table with an instance of this target may never
  144. * have tables containing any different target type.
  145. */
  146. #define DM_TARGET_IMMUTABLE 0x00000004
  147. #define dm_target_is_immutable(type) ((type)->features & DM_TARGET_IMMUTABLE)
  148. struct dm_target {
  149. struct dm_table *table;
  150. struct target_type *type;
  151. /* target limits */
  152. sector_t begin;
  153. sector_t len;
  154. /* If non-zero, maximum size of I/O submitted to a target. */
  155. uint32_t max_io_len;
  156. /*
  157. * A number of zero-length barrier requests that will be submitted
  158. * to the target for the purpose of flushing cache.
  159. *
  160. * The request number can be accessed with dm_bio_get_target_request_nr.
  161. * It is a responsibility of the target driver to remap these requests
  162. * to the real underlying devices.
  163. */
  164. unsigned num_flush_requests;
  165. /*
  166. * The number of discard requests that will be submitted to the target.
  167. * The request number can be accessed with dm_bio_get_target_request_nr.
  168. */
  169. unsigned num_discard_requests;
  170. /*
  171. * The number of WRITE SAME requests that will be submitted to the target.
  172. * The request number can be accessed with dm_bio_get_target_request_nr.
  173. */
  174. unsigned num_write_same_requests;
  175. /*
  176. * The minimum number of extra bytes allocated in each bio for the
  177. * target to use. dm_per_bio_data returns the data location.
  178. */
  179. unsigned per_bio_data_size;
  180. /* target specific data */
  181. void *private;
  182. /* Used to provide an error string from the ctr */
  183. char *error;
  184. /*
  185. * Set if this target needs to receive flushes regardless of
  186. * whether or not its underlying devices have support.
  187. */
  188. bool flush_supported:1;
  189. /*
  190. * Set if this target needs to receive discards regardless of
  191. * whether or not its underlying devices have support.
  192. */
  193. bool discards_supported:1;
  194. /*
  195. * Set if the target required discard request to be split
  196. * on max_io_len boundary.
  197. */
  198. bool split_discard_requests:1;
  199. /*
  200. * Set if this target does not return zeroes on discarded blocks.
  201. */
  202. bool discard_zeroes_data_unsupported:1;
  203. };
  204. /* Each target can link one of these into the table */
  205. struct dm_target_callbacks {
  206. struct list_head list;
  207. int (*congested_fn) (struct dm_target_callbacks *, int);
  208. };
  209. /*
  210. * For bio-based dm.
  211. * One of these is allocated for each bio.
  212. * This structure shouldn't be touched directly by target drivers.
  213. * It is here so that we can inline dm_per_bio_data and
  214. * dm_bio_from_per_bio_data
  215. */
  216. struct dm_target_io {
  217. struct dm_io *io;
  218. struct dm_target *ti;
  219. union map_info info;
  220. unsigned target_request_nr;
  221. struct bio clone;
  222. };
  223. static inline void *dm_per_bio_data(struct bio *bio, size_t data_size)
  224. {
  225. return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
  226. }
  227. static inline struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
  228. {
  229. return (struct bio *)((char *)data + data_size + offsetof(struct dm_target_io, clone));
  230. }
  231. static inline unsigned dm_bio_get_target_request_nr(const struct bio *bio)
  232. {
  233. return container_of(bio, struct dm_target_io, clone)->target_request_nr;
  234. }
  235. int dm_register_target(struct target_type *t);
  236. void dm_unregister_target(struct target_type *t);
  237. /*
  238. * Target argument parsing.
  239. */
  240. struct dm_arg_set {
  241. unsigned argc;
  242. char **argv;
  243. };
  244. /*
  245. * The minimum and maximum value of a numeric argument, together with
  246. * the error message to use if the number is found to be outside that range.
  247. */
  248. struct dm_arg {
  249. unsigned min;
  250. unsigned max;
  251. char *error;
  252. };
  253. /*
  254. * Validate the next argument, either returning it as *value or, if invalid,
  255. * returning -EINVAL and setting *error.
  256. */
  257. int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  258. unsigned *value, char **error);
  259. /*
  260. * Process the next argument as the start of a group containing between
  261. * arg->min and arg->max further arguments. Either return the size as
  262. * *num_args or, if invalid, return -EINVAL and set *error.
  263. */
  264. int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
  265. unsigned *num_args, char **error);
  266. /*
  267. * Return the current argument and shift to the next.
  268. */
  269. const char *dm_shift_arg(struct dm_arg_set *as);
  270. /*
  271. * Move through num_args arguments.
  272. */
  273. void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
  274. /*-----------------------------------------------------------------
  275. * Functions for creating and manipulating mapped devices.
  276. * Drop the reference with dm_put when you finish with the object.
  277. *---------------------------------------------------------------*/
  278. /*
  279. * DM_ANY_MINOR chooses the next available minor number.
  280. */
  281. #define DM_ANY_MINOR (-1)
  282. int dm_create(int minor, struct mapped_device **md);
  283. /*
  284. * Reference counting for md.
  285. */
  286. struct mapped_device *dm_get_md(dev_t dev);
  287. void dm_get(struct mapped_device *md);
  288. void dm_put(struct mapped_device *md);
  289. /*
  290. * An arbitrary pointer may be stored alongside a mapped device.
  291. */
  292. void dm_set_mdptr(struct mapped_device *md, void *ptr);
  293. void *dm_get_mdptr(struct mapped_device *md);
  294. /*
  295. * A device can still be used while suspended, but I/O is deferred.
  296. */
  297. int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
  298. int dm_resume(struct mapped_device *md);
  299. /*
  300. * Event functions.
  301. */
  302. uint32_t dm_get_event_nr(struct mapped_device *md);
  303. int dm_wait_event(struct mapped_device *md, int event_nr);
  304. uint32_t dm_next_uevent_seq(struct mapped_device *md);
  305. void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
  306. /*
  307. * Info functions.
  308. */
  309. const char *dm_device_name(struct mapped_device *md);
  310. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
  311. struct gendisk *dm_disk(struct mapped_device *md);
  312. int dm_suspended(struct dm_target *ti);
  313. int dm_noflush_suspending(struct dm_target *ti);
  314. union map_info *dm_get_mapinfo(struct bio *bio);
  315. union map_info *dm_get_rq_mapinfo(struct request *rq);
  316. /*
  317. * Geometry functions.
  318. */
  319. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
  320. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
  321. /*-----------------------------------------------------------------
  322. * Functions for manipulating device-mapper tables.
  323. *---------------------------------------------------------------*/
  324. /*
  325. * First create an empty table.
  326. */
  327. int dm_table_create(struct dm_table **result, fmode_t mode,
  328. unsigned num_targets, struct mapped_device *md);
  329. /*
  330. * Then call this once for each target.
  331. */
  332. int dm_table_add_target(struct dm_table *t, const char *type,
  333. sector_t start, sector_t len, char *params);
  334. /*
  335. * Target_ctr should call this if it needs to add any callbacks.
  336. */
  337. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
  338. /*
  339. * Finally call this to make the table ready for use.
  340. */
  341. int dm_table_complete(struct dm_table *t);
  342. /*
  343. * Target may require that it is never sent I/O larger than len.
  344. */
  345. int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
  346. /*
  347. * Table reference counting.
  348. */
  349. struct dm_table *dm_get_live_table(struct mapped_device *md);
  350. void dm_table_get(struct dm_table *t);
  351. void dm_table_put(struct dm_table *t);
  352. /*
  353. * Queries
  354. */
  355. sector_t dm_table_get_size(struct dm_table *t);
  356. unsigned int dm_table_get_num_targets(struct dm_table *t);
  357. fmode_t dm_table_get_mode(struct dm_table *t);
  358. struct mapped_device *dm_table_get_md(struct dm_table *t);
  359. /*
  360. * Trigger an event.
  361. */
  362. void dm_table_event(struct dm_table *t);
  363. /*
  364. * The device must be suspended before calling this method.
  365. * Returns the previous table, which the caller must destroy.
  366. */
  367. struct dm_table *dm_swap_table(struct mapped_device *md,
  368. struct dm_table *t);
  369. /*
  370. * A wrapper around vmalloc.
  371. */
  372. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
  373. /*-----------------------------------------------------------------
  374. * Macros.
  375. *---------------------------------------------------------------*/
  376. #define DM_NAME "device-mapper"
  377. #ifdef CONFIG_PRINTK
  378. extern struct ratelimit_state dm_ratelimit_state;
  379. #define dm_ratelimit() __ratelimit(&dm_ratelimit_state)
  380. #else
  381. #define dm_ratelimit() 0
  382. #endif
  383. #define DMCRIT(f, arg...) \
  384. printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  385. #define DMERR(f, arg...) \
  386. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  387. #define DMERR_LIMIT(f, arg...) \
  388. do { \
  389. if (dm_ratelimit()) \
  390. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
  391. f "\n", ## arg); \
  392. } while (0)
  393. #define DMWARN(f, arg...) \
  394. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  395. #define DMWARN_LIMIT(f, arg...) \
  396. do { \
  397. if (dm_ratelimit()) \
  398. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
  399. f "\n", ## arg); \
  400. } while (0)
  401. #define DMINFO(f, arg...) \
  402. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  403. #define DMINFO_LIMIT(f, arg...) \
  404. do { \
  405. if (dm_ratelimit()) \
  406. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
  407. "\n", ## arg); \
  408. } while (0)
  409. #ifdef CONFIG_DM_DEBUG
  410. # define DMDEBUG(f, arg...) \
  411. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
  412. # define DMDEBUG_LIMIT(f, arg...) \
  413. do { \
  414. if (dm_ratelimit()) \
  415. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
  416. "\n", ## arg); \
  417. } while (0)
  418. #else
  419. # define DMDEBUG(f, arg...) do {} while (0)
  420. # define DMDEBUG_LIMIT(f, arg...) do {} while (0)
  421. #endif
  422. #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
  423. 0 : scnprintf(result + sz, maxlen - sz, x))
  424. #define SECTOR_SHIFT 9
  425. /*
  426. * Definitions of return values from target end_io function.
  427. */
  428. #define DM_ENDIO_INCOMPLETE 1
  429. #define DM_ENDIO_REQUEUE 2
  430. /*
  431. * Definitions of return values from target map function.
  432. */
  433. #define DM_MAPIO_SUBMITTED 0
  434. #define DM_MAPIO_REMAPPED 1
  435. #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
  436. /*
  437. * Ceiling(n / sz)
  438. */
  439. #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
  440. #define dm_sector_div_up(n, sz) ( \
  441. { \
  442. sector_t _r = ((n) + (sz) - 1); \
  443. sector_div(_r, (sz)); \
  444. _r; \
  445. } \
  446. )
  447. /*
  448. * ceiling(n / size) * size
  449. */
  450. #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
  451. #define dm_array_too_big(fixed, obj, num) \
  452. ((num) > (UINT_MAX - (fixed)) / (obj))
  453. /*
  454. * Sector offset taken relative to the start of the target instead of
  455. * relative to the start of the device.
  456. */
  457. #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
  458. static inline sector_t to_sector(unsigned long n)
  459. {
  460. return (n >> SECTOR_SHIFT);
  461. }
  462. static inline unsigned long to_bytes(sector_t n)
  463. {
  464. return (n << SECTOR_SHIFT);
  465. }
  466. /*-----------------------------------------------------------------
  467. * Helper for block layer and dm core operations
  468. *---------------------------------------------------------------*/
  469. void dm_dispatch_request(struct request *rq);
  470. void dm_requeue_unmapped_request(struct request *rq);
  471. void dm_kill_unmapped_request(struct request *rq, int error);
  472. int dm_underlying_device_busy(struct request_queue *q);
  473. #endif /* _LINUX_DEVICE_MAPPER_H */