device-mapper.h 17 KB

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