device-mapper.h 17 KB

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