device-mapper.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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/math64.h>
  12. #include <linux/ratelimit.h>
  13. struct dm_dev;
  14. struct dm_target;
  15. struct dm_table;
  16. struct mapped_device;
  17. struct bio_vec;
  18. /*
  19. * Type of table, mapped_device's mempool and request_queue
  20. */
  21. #define DM_TYPE_NONE 0
  22. #define DM_TYPE_BIO_BASED 1
  23. #define DM_TYPE_REQUEST_BASED 2
  24. #define DM_TYPE_MQ_REQUEST_BASED 3
  25. #define DM_TYPE_DAX_BIO_BASED 4
  26. typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
  27. union map_info {
  28. void *ptr;
  29. };
  30. /*
  31. * In the constructor the target parameter will already have the
  32. * table, type, begin and len fields filled in.
  33. */
  34. typedef int (*dm_ctr_fn) (struct dm_target *target,
  35. unsigned int argc, char **argv);
  36. /*
  37. * The destructor doesn't need to free the dm_target, just
  38. * anything hidden ti->private.
  39. */
  40. typedef void (*dm_dtr_fn) (struct dm_target *ti);
  41. /*
  42. * The map function must return:
  43. * < 0: error
  44. * = 0: The target will handle the io by resubmitting it later
  45. * = 1: simple remap complete
  46. * = 2: The target wants to push back the io
  47. */
  48. typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
  49. typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone,
  50. union map_info *map_context);
  51. typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
  52. struct request *rq,
  53. union map_info *map_context,
  54. struct request **clone);
  55. typedef void (*dm_release_clone_request_fn) (struct request *clone);
  56. /*
  57. * Returns:
  58. * < 0 : error (currently ignored)
  59. * 0 : ended successfully
  60. * 1 : for some reason the io has still not completed (eg,
  61. * multipath target might want to requeue a failed io).
  62. * 2 : The target wants to push back the io
  63. */
  64. typedef int (*dm_endio_fn) (struct dm_target *ti,
  65. struct bio *bio, int error);
  66. typedef int (*dm_request_endio_fn) (struct dm_target *ti,
  67. struct request *clone, int error,
  68. union map_info *map_context);
  69. typedef void (*dm_presuspend_fn) (struct dm_target *ti);
  70. typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
  71. typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
  72. typedef int (*dm_preresume_fn) (struct dm_target *ti);
  73. typedef void (*dm_resume_fn) (struct dm_target *ti);
  74. typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
  75. unsigned status_flags, char *result, unsigned maxlen);
  76. typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
  77. typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti,
  78. struct block_device **bdev, fmode_t *mode);
  79. /*
  80. * These iteration functions are typically used to check (and combine)
  81. * properties of underlying devices.
  82. * E.g. Does at least one underlying device support flush?
  83. * Does any underlying device not support WRITE_SAME?
  84. *
  85. * The callout function is called once for each contiguous section of
  86. * an underlying device. State can be maintained in *data.
  87. * Return non-zero to stop iterating through any further devices.
  88. */
  89. typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
  90. struct dm_dev *dev,
  91. sector_t start, sector_t len,
  92. void *data);
  93. /*
  94. * This function must iterate through each section of device used by the
  95. * target until it encounters a non-zero return code, which it then returns.
  96. * Returns zero if no callout returned non-zero.
  97. */
  98. typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
  99. iterate_devices_callout_fn fn,
  100. void *data);
  101. typedef void (*dm_io_hints_fn) (struct dm_target *ti,
  102. struct queue_limits *limits);
  103. /*
  104. * Returns:
  105. * 0: The target can handle the next I/O immediately.
  106. * 1: The target can't handle the next I/O immediately.
  107. */
  108. typedef int (*dm_busy_fn) (struct dm_target *ti);
  109. /*
  110. * Returns:
  111. * < 0 : error
  112. * >= 0 : the number of bytes accessible at the address
  113. */
  114. typedef long (*dm_direct_access_fn) (struct dm_target *ti, sector_t sector,
  115. void **kaddr, pfn_t *pfn, long size);
  116. void dm_error(const char *message);
  117. struct dm_dev {
  118. struct block_device *bdev;
  119. fmode_t mode;
  120. char name[16];
  121. };
  122. dev_t dm_get_dev_t(const char *path);
  123. /*
  124. * Constructors should call these functions to ensure destination devices
  125. * are opened/closed correctly.
  126. */
  127. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  128. struct dm_dev **result);
  129. void dm_put_device(struct dm_target *ti, struct dm_dev *d);
  130. /*
  131. * Information about a target type
  132. */
  133. struct target_type {
  134. uint64_t features;
  135. const char *name;
  136. struct module *module;
  137. unsigned version[3];
  138. dm_ctr_fn ctr;
  139. dm_dtr_fn dtr;
  140. dm_map_fn map;
  141. dm_map_request_fn map_rq;
  142. dm_clone_and_map_request_fn clone_and_map_rq;
  143. dm_release_clone_request_fn release_clone_rq;
  144. dm_endio_fn end_io;
  145. dm_request_endio_fn rq_end_io;
  146. dm_presuspend_fn presuspend;
  147. dm_presuspend_undo_fn presuspend_undo;
  148. dm_postsuspend_fn postsuspend;
  149. dm_preresume_fn preresume;
  150. dm_resume_fn resume;
  151. dm_status_fn status;
  152. dm_message_fn message;
  153. dm_prepare_ioctl_fn prepare_ioctl;
  154. dm_busy_fn busy;
  155. dm_iterate_devices_fn iterate_devices;
  156. dm_io_hints_fn io_hints;
  157. dm_direct_access_fn direct_access;
  158. /* For internal device-mapper use. */
  159. struct list_head list;
  160. };
  161. /*
  162. * Target features
  163. */
  164. /*
  165. * Any table that contains an instance of this target must have only one.
  166. */
  167. #define DM_TARGET_SINGLETON 0x00000001
  168. #define dm_target_needs_singleton(type) ((type)->features & DM_TARGET_SINGLETON)
  169. /*
  170. * Indicates that a target does not support read-only devices.
  171. */
  172. #define DM_TARGET_ALWAYS_WRITEABLE 0x00000002
  173. #define dm_target_always_writeable(type) \
  174. ((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
  175. /*
  176. * Any device that contains a table with an instance of this target may never
  177. * have tables containing any different target type.
  178. */
  179. #define DM_TARGET_IMMUTABLE 0x00000004
  180. #define dm_target_is_immutable(type) ((type)->features & DM_TARGET_IMMUTABLE)
  181. /*
  182. * Indicates that a target may replace any target; even immutable targets.
  183. * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
  184. */
  185. #define DM_TARGET_WILDCARD 0x00000008
  186. #define dm_target_is_wildcard(type) ((type)->features & DM_TARGET_WILDCARD)
  187. /*
  188. * Some targets need to be sent the same WRITE bio severals times so
  189. * that they can send copies of it to different devices. This function
  190. * examines any supplied bio and returns the number of copies of it the
  191. * target requires.
  192. */
  193. typedef unsigned (*dm_num_write_bios_fn) (struct dm_target *ti, struct bio *bio);
  194. struct dm_target {
  195. struct dm_table *table;
  196. struct target_type *type;
  197. /* target limits */
  198. sector_t begin;
  199. sector_t len;
  200. /* If non-zero, maximum size of I/O submitted to a target. */
  201. uint32_t max_io_len;
  202. /*
  203. * A number of zero-length barrier bios that will be submitted
  204. * to the target for the purpose of flushing cache.
  205. *
  206. * The bio number can be accessed with dm_bio_get_target_bio_nr.
  207. * It is a responsibility of the target driver to remap these bios
  208. * to the real underlying devices.
  209. */
  210. unsigned num_flush_bios;
  211. /*
  212. * The number of discard bios that will be submitted to the target.
  213. * The bio number can be accessed with dm_bio_get_target_bio_nr.
  214. */
  215. unsigned num_discard_bios;
  216. /*
  217. * The number of WRITE SAME bios that will be submitted to the target.
  218. * The bio number can be accessed with dm_bio_get_target_bio_nr.
  219. */
  220. unsigned num_write_same_bios;
  221. /*
  222. * The minimum number of extra bytes allocated in each io for the
  223. * target to use.
  224. */
  225. unsigned per_io_data_size;
  226. /*
  227. * If defined, this function is called to find out how many
  228. * duplicate bios should be sent to the target when writing
  229. * data.
  230. */
  231. dm_num_write_bios_fn num_write_bios;
  232. /* target specific data */
  233. void *private;
  234. /* Used to provide an error string from the ctr */
  235. char *error;
  236. /*
  237. * Set if this target needs to receive flushes regardless of
  238. * whether or not its underlying devices have support.
  239. */
  240. bool flush_supported:1;
  241. /*
  242. * Set if this target needs to receive discards regardless of
  243. * whether or not its underlying devices have support.
  244. */
  245. bool discards_supported:1;
  246. /*
  247. * Set if the target required discard bios to be split
  248. * on max_io_len boundary.
  249. */
  250. bool split_discard_bios:1;
  251. /*
  252. * Set if this target does not return zeroes on discarded blocks.
  253. */
  254. bool discard_zeroes_data_unsupported:1;
  255. };
  256. /* Each target can link one of these into the table */
  257. struct dm_target_callbacks {
  258. struct list_head list;
  259. int (*congested_fn) (struct dm_target_callbacks *, int);
  260. };
  261. /*
  262. * For bio-based dm.
  263. * One of these is allocated for each bio.
  264. * This structure shouldn't be touched directly by target drivers.
  265. * It is here so that we can inline dm_per_bio_data and
  266. * dm_bio_from_per_bio_data
  267. */
  268. struct dm_target_io {
  269. struct dm_io *io;
  270. struct dm_target *ti;
  271. unsigned target_bio_nr;
  272. unsigned *len_ptr;
  273. struct bio clone;
  274. };
  275. static inline void *dm_per_bio_data(struct bio *bio, size_t data_size)
  276. {
  277. return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
  278. }
  279. static inline struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
  280. {
  281. return (struct bio *)((char *)data + data_size + offsetof(struct dm_target_io, clone));
  282. }
  283. static inline unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
  284. {
  285. return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
  286. }
  287. int dm_register_target(struct target_type *t);
  288. void dm_unregister_target(struct target_type *t);
  289. /*
  290. * Target argument parsing.
  291. */
  292. struct dm_arg_set {
  293. unsigned argc;
  294. char **argv;
  295. };
  296. /*
  297. * The minimum and maximum value of a numeric argument, together with
  298. * the error message to use if the number is found to be outside that range.
  299. */
  300. struct dm_arg {
  301. unsigned min;
  302. unsigned max;
  303. char *error;
  304. };
  305. /*
  306. * Validate the next argument, either returning it as *value or, if invalid,
  307. * returning -EINVAL and setting *error.
  308. */
  309. int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  310. unsigned *value, char **error);
  311. /*
  312. * Process the next argument as the start of a group containing between
  313. * arg->min and arg->max further arguments. Either return the size as
  314. * *num_args or, if invalid, return -EINVAL and set *error.
  315. */
  316. int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
  317. unsigned *num_args, char **error);
  318. /*
  319. * Return the current argument and shift to the next.
  320. */
  321. const char *dm_shift_arg(struct dm_arg_set *as);
  322. /*
  323. * Move through num_args arguments.
  324. */
  325. void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
  326. /*-----------------------------------------------------------------
  327. * Functions for creating and manipulating mapped devices.
  328. * Drop the reference with dm_put when you finish with the object.
  329. *---------------------------------------------------------------*/
  330. /*
  331. * DM_ANY_MINOR chooses the next available minor number.
  332. */
  333. #define DM_ANY_MINOR (-1)
  334. int dm_create(int minor, struct mapped_device **md);
  335. /*
  336. * Reference counting for md.
  337. */
  338. struct mapped_device *dm_get_md(dev_t dev);
  339. void dm_get(struct mapped_device *md);
  340. int dm_hold(struct mapped_device *md);
  341. void dm_put(struct mapped_device *md);
  342. /*
  343. * An arbitrary pointer may be stored alongside a mapped device.
  344. */
  345. void dm_set_mdptr(struct mapped_device *md, void *ptr);
  346. void *dm_get_mdptr(struct mapped_device *md);
  347. /*
  348. * A device can still be used while suspended, but I/O is deferred.
  349. */
  350. int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
  351. int dm_resume(struct mapped_device *md);
  352. /*
  353. * Event functions.
  354. */
  355. uint32_t dm_get_event_nr(struct mapped_device *md);
  356. int dm_wait_event(struct mapped_device *md, int event_nr);
  357. uint32_t dm_next_uevent_seq(struct mapped_device *md);
  358. void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
  359. /*
  360. * Info functions.
  361. */
  362. const char *dm_device_name(struct mapped_device *md);
  363. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
  364. struct gendisk *dm_disk(struct mapped_device *md);
  365. int dm_suspended(struct dm_target *ti);
  366. int dm_noflush_suspending(struct dm_target *ti);
  367. void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
  368. union map_info *dm_get_rq_mapinfo(struct request *rq);
  369. struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
  370. /*
  371. * Geometry functions.
  372. */
  373. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
  374. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
  375. /*-----------------------------------------------------------------
  376. * Functions for manipulating device-mapper tables.
  377. *---------------------------------------------------------------*/
  378. /*
  379. * First create an empty table.
  380. */
  381. int dm_table_create(struct dm_table **result, fmode_t mode,
  382. unsigned num_targets, struct mapped_device *md);
  383. /*
  384. * Then call this once for each target.
  385. */
  386. int dm_table_add_target(struct dm_table *t, const char *type,
  387. sector_t start, sector_t len, char *params);
  388. /*
  389. * Target_ctr should call this if it needs to add any callbacks.
  390. */
  391. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
  392. /*
  393. * Target can use this to set the table's type.
  394. * Can only ever be called from a target's ctr.
  395. * Useful for "hybrid" target (supports both bio-based
  396. * and request-based).
  397. */
  398. void dm_table_set_type(struct dm_table *t, unsigned type);
  399. /*
  400. * Finally call this to make the table ready for use.
  401. */
  402. int dm_table_complete(struct dm_table *t);
  403. /*
  404. * Target may require that it is never sent I/O larger than len.
  405. */
  406. int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
  407. /*
  408. * Table reference counting.
  409. */
  410. struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
  411. void dm_put_live_table(struct mapped_device *md, int srcu_idx);
  412. void dm_sync_table(struct mapped_device *md);
  413. /*
  414. * Queries
  415. */
  416. sector_t dm_table_get_size(struct dm_table *t);
  417. unsigned int dm_table_get_num_targets(struct dm_table *t);
  418. fmode_t dm_table_get_mode(struct dm_table *t);
  419. struct mapped_device *dm_table_get_md(struct dm_table *t);
  420. /*
  421. * Trigger an event.
  422. */
  423. void dm_table_event(struct dm_table *t);
  424. /*
  425. * Run the queue for request-based targets.
  426. */
  427. void dm_table_run_md_queue_async(struct dm_table *t);
  428. /*
  429. * The device must be suspended before calling this method.
  430. * Returns the previous table, which the caller must destroy.
  431. */
  432. struct dm_table *dm_swap_table(struct mapped_device *md,
  433. struct dm_table *t);
  434. /*
  435. * A wrapper around vmalloc.
  436. */
  437. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
  438. /*-----------------------------------------------------------------
  439. * Macros.
  440. *---------------------------------------------------------------*/
  441. #define DM_NAME "device-mapper"
  442. #ifdef CONFIG_PRINTK
  443. extern struct ratelimit_state dm_ratelimit_state;
  444. #define dm_ratelimit() __ratelimit(&dm_ratelimit_state)
  445. #else
  446. #define dm_ratelimit() 0
  447. #endif
  448. #define DMCRIT(f, arg...) \
  449. printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  450. #define DMERR(f, arg...) \
  451. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  452. #define DMERR_LIMIT(f, arg...) \
  453. do { \
  454. if (dm_ratelimit()) \
  455. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
  456. f "\n", ## arg); \
  457. } while (0)
  458. #define DMWARN(f, arg...) \
  459. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  460. #define DMWARN_LIMIT(f, arg...) \
  461. do { \
  462. if (dm_ratelimit()) \
  463. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
  464. f "\n", ## arg); \
  465. } while (0)
  466. #define DMINFO(f, arg...) \
  467. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  468. #define DMINFO_LIMIT(f, arg...) \
  469. do { \
  470. if (dm_ratelimit()) \
  471. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
  472. "\n", ## arg); \
  473. } while (0)
  474. #ifdef CONFIG_DM_DEBUG
  475. # define DMDEBUG(f, arg...) \
  476. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
  477. # define DMDEBUG_LIMIT(f, arg...) \
  478. do { \
  479. if (dm_ratelimit()) \
  480. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
  481. "\n", ## arg); \
  482. } while (0)
  483. #else
  484. # define DMDEBUG(f, arg...) do {} while (0)
  485. # define DMDEBUG_LIMIT(f, arg...) do {} while (0)
  486. #endif
  487. #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
  488. 0 : scnprintf(result + sz, maxlen - sz, x))
  489. #define SECTOR_SHIFT 9
  490. /*
  491. * Definitions of return values from target end_io function.
  492. */
  493. #define DM_ENDIO_INCOMPLETE 1
  494. #define DM_ENDIO_REQUEUE 2
  495. /*
  496. * Definitions of return values from target map function.
  497. */
  498. #define DM_MAPIO_SUBMITTED 0
  499. #define DM_MAPIO_REMAPPED 1
  500. #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
  501. #define DM_MAPIO_DELAY_REQUEUE 3
  502. #define dm_sector_div64(x, y)( \
  503. { \
  504. u64 _res; \
  505. (x) = div64_u64_rem(x, y, &_res); \
  506. _res; \
  507. } \
  508. )
  509. /*
  510. * Ceiling(n / sz)
  511. */
  512. #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
  513. #define dm_sector_div_up(n, sz) ( \
  514. { \
  515. sector_t _r = ((n) + (sz) - 1); \
  516. sector_div(_r, (sz)); \
  517. _r; \
  518. } \
  519. )
  520. /*
  521. * ceiling(n / size) * size
  522. */
  523. #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
  524. #define dm_array_too_big(fixed, obj, num) \
  525. ((num) > (UINT_MAX - (fixed)) / (obj))
  526. /*
  527. * Sector offset taken relative to the start of the target instead of
  528. * relative to the start of the device.
  529. */
  530. #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
  531. static inline sector_t to_sector(unsigned long n)
  532. {
  533. return (n >> SECTOR_SHIFT);
  534. }
  535. static inline unsigned long to_bytes(sector_t n)
  536. {
  537. return (n << SECTOR_SHIFT);
  538. }
  539. #endif /* _LINUX_DEVICE_MAPPER_H */