device-mapper.h 16 KB

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