device-mapper.h 15 KB

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