device-mapper.h 17 KB

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