device-mapper.h 17 KB

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