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 8 #ifndef _LINUX_DEVICE_MAPPER_H 9 #define _LINUX_DEVICE_MAPPER_H 10 11 #include <linux/bio.h> 12 #include <linux/blkdev.h> 13 #include <linux/dm-ioctl.h> 14 #include <linux/math64.h> 15 #include <linux/ratelimit.h> 16 17 struct dm_dev; 18 struct dm_target; 19 struct dm_table; 20 struct dm_report_zones_args; 21 struct mapped_device; 22 struct bio_vec; 23 enum dax_access_mode; 24 25 /* 26 * Type of table, mapped_device's mempool and request_queue 27 */ 28 enum dm_queue_mode { 29 DM_TYPE_NONE = 0, 30 DM_TYPE_BIO_BASED = 1, 31 DM_TYPE_REQUEST_BASED = 2, 32 DM_TYPE_DAX_BIO_BASED = 3, 33 }; 34 35 typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE, STATUSTYPE_IMA } status_type_t; 36 37 union map_info { 38 void *ptr; 39 }; 40 41 /* 42 * In the constructor the target parameter will already have the 43 * table, type, begin and len fields filled in. 44 */ 45 typedef int (*dm_ctr_fn) (struct dm_target *target, 46 unsigned int argc, char **argv); 47 48 /* 49 * The destructor doesn't need to free the dm_target, just 50 * anything hidden ti->private. 51 */ 52 typedef void (*dm_dtr_fn) (struct dm_target *ti); 53 54 /* 55 * The map function must return: 56 * < 0: error 57 * = 0: The target will handle the io by resubmitting it later 58 * = 1: simple remap complete 59 * = 2: The target wants to push back the io 60 */ 61 typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio); 62 typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti, 63 struct request *rq, 64 union map_info *map_context, 65 struct request **clone); 66 typedef void (*dm_release_clone_request_fn) (struct request *clone, 67 union map_info *map_context); 68 69 /* 70 * Returns: 71 * < 0 : error (currently ignored) 72 * 0 : ended successfully 73 * 1 : for some reason the io has still not completed (eg, 74 * multipath target might want to requeue a failed io). 75 * 2 : The target wants to push back the io 76 */ 77 typedef int (*dm_endio_fn) (struct dm_target *ti, 78 struct bio *bio, blk_status_t *error); 79 typedef int (*dm_request_endio_fn) (struct dm_target *ti, 80 struct request *clone, blk_status_t error, 81 union map_info *map_context); 82 83 typedef void (*dm_presuspend_fn) (struct dm_target *ti); 84 typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti); 85 typedef void (*dm_postsuspend_fn) (struct dm_target *ti); 86 typedef int (*dm_preresume_fn) (struct dm_target *ti); 87 typedef void (*dm_resume_fn) (struct dm_target *ti); 88 89 typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type, 90 unsigned status_flags, char *result, unsigned maxlen); 91 92 typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv, 93 char *result, unsigned maxlen); 94 95 typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti, struct block_device **bdev); 96 97 #ifdef CONFIG_BLK_DEV_ZONED 98 typedef int (*dm_report_zones_fn) (struct dm_target *ti, 99 struct dm_report_zones_args *args, 100 unsigned int nr_zones); 101 #else 102 /* 103 * Define dm_report_zones_fn so that targets can assign to NULL if 104 * CONFIG_BLK_DEV_ZONED disabled. Otherwise each target needs to do 105 * awkward #ifdefs in their target_type, etc. 106 */ 107 typedef int (*dm_report_zones_fn) (struct dm_target *dummy); 108 #endif 109 110 /* 111 * These iteration functions are typically used to check (and combine) 112 * properties of underlying devices. 113 * E.g. Does at least one underlying device support flush? 114 * Does any underlying device not support WRITE_SAME? 115 * 116 * The callout function is called once for each contiguous section of 117 * an underlying device. State can be maintained in *data. 118 * Return non-zero to stop iterating through any further devices. 119 */ 120 typedef int (*iterate_devices_callout_fn) (struct dm_target *ti, 121 struct dm_dev *dev, 122 sector_t start, sector_t len, 123 void *data); 124 125 /* 126 * This function must iterate through each section of device used by the 127 * target until it encounters a non-zero return code, which it then returns. 128 * Returns zero if no callout returned non-zero. 129 */ 130 typedef int (*dm_iterate_devices_fn) (struct dm_target *ti, 131 iterate_devices_callout_fn fn, 132 void *data); 133 134 typedef void (*dm_io_hints_fn) (struct dm_target *ti, 135 struct queue_limits *limits); 136 137 /* 138 * Returns: 139 * 0: The target can handle the next I/O immediately. 140 * 1: The target can't handle the next I/O immediately. 141 */ 142 typedef int (*dm_busy_fn) (struct dm_target *ti); 143 144 /* 145 * Returns: 146 * < 0 : error 147 * >= 0 : the number of bytes accessible at the address 148 */ 149 typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff, 150 long nr_pages, enum dax_access_mode node, void **kaddr, 151 pfn_t *pfn); 152 typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff, 153 size_t nr_pages); 154 155 void dm_error(const char *message); 156 157 struct dm_dev { 158 struct block_device *bdev; 159 struct dax_device *dax_dev; 160 fmode_t mode; 161 char name[16]; 162 }; 163 164 dev_t dm_get_dev_t(const char *path); 165 166 /* 167 * Constructors should call these functions to ensure destination devices 168 * are opened/closed correctly. 169 */ 170 int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode, 171 struct dm_dev **result); 172 void dm_put_device(struct dm_target *ti, struct dm_dev *d); 173 174 /* 175 * Information about a target type 176 */ 177 178 struct target_type { 179 uint64_t features; 180 const char *name; 181 struct module *module; 182 unsigned version[3]; 183 dm_ctr_fn ctr; 184 dm_dtr_fn dtr; 185 dm_map_fn map; 186 dm_clone_and_map_request_fn clone_and_map_rq; 187 dm_release_clone_request_fn release_clone_rq; 188 dm_endio_fn end_io; 189 dm_request_endio_fn rq_end_io; 190 dm_presuspend_fn presuspend; 191 dm_presuspend_undo_fn presuspend_undo; 192 dm_postsuspend_fn postsuspend; 193 dm_preresume_fn preresume; 194 dm_resume_fn resume; 195 dm_status_fn status; 196 dm_message_fn message; 197 dm_prepare_ioctl_fn prepare_ioctl; 198 dm_report_zones_fn report_zones; 199 dm_busy_fn busy; 200 dm_iterate_devices_fn iterate_devices; 201 dm_io_hints_fn io_hints; 202 dm_dax_direct_access_fn direct_access; 203 dm_dax_zero_page_range_fn dax_zero_page_range; 204 205 /* For internal device-mapper use. */ 206 struct list_head list; 207 }; 208 209 /* 210 * Target features 211 */ 212 213 /* 214 * Any table that contains an instance of this target must have only one. 215 */ 216 #define DM_TARGET_SINGLETON 0x00000001 217 #define dm_target_needs_singleton(type) ((type)->features & DM_TARGET_SINGLETON) 218 219 /* 220 * Indicates that a target does not support read-only devices. 221 */ 222 #define DM_TARGET_ALWAYS_WRITEABLE 0x00000002 223 #define dm_target_always_writeable(type) \ 224 ((type)->features & DM_TARGET_ALWAYS_WRITEABLE) 225 226 /* 227 * Any device that contains a table with an instance of this target may never 228 * have tables containing any different target type. 229 */ 230 #define DM_TARGET_IMMUTABLE 0x00000004 231 #define dm_target_is_immutable(type) ((type)->features & DM_TARGET_IMMUTABLE) 232 233 /* 234 * Indicates that a target may replace any target; even immutable targets. 235 * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined. 236 */ 237 #define DM_TARGET_WILDCARD 0x00000008 238 #define dm_target_is_wildcard(type) ((type)->features & DM_TARGET_WILDCARD) 239 240 /* 241 * A target implements own bio data integrity. 242 */ 243 #define DM_TARGET_INTEGRITY 0x00000010 244 #define dm_target_has_integrity(type) ((type)->features & DM_TARGET_INTEGRITY) 245 246 /* 247 * A target passes integrity data to the lower device. 248 */ 249 #define DM_TARGET_PASSES_INTEGRITY 0x00000020 250 #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY) 251 252 /* 253 * Indicates support for zoned block devices: 254 * - DM_TARGET_ZONED_HM: the target also supports host-managed zoned 255 * block devices but does not support combining different zoned models. 256 * - DM_TARGET_MIXED_ZONED_MODEL: the target supports combining multiple 257 * devices with different zoned models. 258 */ 259 #ifdef CONFIG_BLK_DEV_ZONED 260 #define DM_TARGET_ZONED_HM 0x00000040 261 #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM) 262 #else 263 #define DM_TARGET_ZONED_HM 0x00000000 264 #define dm_target_supports_zoned_hm(type) (false) 265 #endif 266 267 /* 268 * A target handles REQ_NOWAIT 269 */ 270 #define DM_TARGET_NOWAIT 0x00000080 271 #define dm_target_supports_nowait(type) ((type)->features & DM_TARGET_NOWAIT) 272 273 /* 274 * A target supports passing through inline crypto support. 275 */ 276 #define DM_TARGET_PASSES_CRYPTO 0x00000100 277 #define dm_target_passes_crypto(type) ((type)->features & DM_TARGET_PASSES_CRYPTO) 278 279 #ifdef CONFIG_BLK_DEV_ZONED 280 #define DM_TARGET_MIXED_ZONED_MODEL 0x00000200 281 #define dm_target_supports_mixed_zoned_model(type) \ 282 ((type)->features & DM_TARGET_MIXED_ZONED_MODEL) 283 #else 284 #define DM_TARGET_MIXED_ZONED_MODEL 0x00000000 285 #define dm_target_supports_mixed_zoned_model(type) (false) 286 #endif 287 288 struct dm_target { 289 struct dm_table *table; 290 struct target_type *type; 291 292 /* target limits */ 293 sector_t begin; 294 sector_t len; 295 296 /* If non-zero, maximum size of I/O submitted to a target. */ 297 uint32_t max_io_len; 298 299 /* 300 * A number of zero-length barrier bios that will be submitted 301 * to the target for the purpose of flushing cache. 302 * 303 * The bio number can be accessed with dm_bio_get_target_bio_nr. 304 * It is a responsibility of the target driver to remap these bios 305 * to the real underlying devices. 306 */ 307 unsigned num_flush_bios; 308 309 /* 310 * The number of discard bios that will be submitted to the target. 311 * The bio number can be accessed with dm_bio_get_target_bio_nr. 312 */ 313 unsigned num_discard_bios; 314 315 /* 316 * The number of secure erase bios that will be submitted to the target. 317 * The bio number can be accessed with dm_bio_get_target_bio_nr. 318 */ 319 unsigned num_secure_erase_bios; 320 321 /* 322 * The number of WRITE ZEROES bios that will be submitted to the target. 323 * The bio number can be accessed with dm_bio_get_target_bio_nr. 324 */ 325 unsigned num_write_zeroes_bios; 326 327 /* 328 * The minimum number of extra bytes allocated in each io for the 329 * target to use. 330 */ 331 unsigned per_io_data_size; 332 333 /* target specific data */ 334 void *private; 335 336 /* Used to provide an error string from the ctr */ 337 char *error; 338 339 /* 340 * Set if this target needs to receive flushes regardless of 341 * whether or not its underlying devices have support. 342 */ 343 bool flush_supported:1; 344 345 /* 346 * Set if this target needs to receive discards regardless of 347 * whether or not its underlying devices have support. 348 */ 349 bool discards_supported:1; 350 351 /* 352 * Set if we need to limit the number of in-flight bios when swapping. 353 */ 354 bool limit_swap_bios:1; 355 356 /* 357 * Set if this target implements a zoned device and needs emulation of 358 * zone append operations using regular writes. 359 */ 360 bool emulate_zone_append:1; 361 362 /* 363 * Set if the target will submit IO using dm_submit_bio_remap() 364 * after returning DM_MAPIO_SUBMITTED from its map function. 365 */ 366 bool accounts_remapped_io:1; 367 }; 368 369 void *dm_per_bio_data(struct bio *bio, size_t data_size); 370 struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size); 371 unsigned dm_bio_get_target_bio_nr(const struct bio *bio); 372 373 u64 dm_start_time_ns_from_clone(struct bio *bio); 374 375 int dm_register_target(struct target_type *t); 376 void dm_unregister_target(struct target_type *t); 377 378 /* 379 * Target argument parsing. 380 */ 381 struct dm_arg_set { 382 unsigned argc; 383 char **argv; 384 }; 385 386 /* 387 * The minimum and maximum value of a numeric argument, together with 388 * the error message to use if the number is found to be outside that range. 389 */ 390 struct dm_arg { 391 unsigned min; 392 unsigned max; 393 char *error; 394 }; 395 396 /* 397 * Validate the next argument, either returning it as *value or, if invalid, 398 * returning -EINVAL and setting *error. 399 */ 400 int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set, 401 unsigned *value, char **error); 402 403 /* 404 * Process the next argument as the start of a group containing between 405 * arg->min and arg->max further arguments. Either return the size as 406 * *num_args or, if invalid, return -EINVAL and set *error. 407 */ 408 int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set, 409 unsigned *num_args, char **error); 410 411 /* 412 * Return the current argument and shift to the next. 413 */ 414 const char *dm_shift_arg(struct dm_arg_set *as); 415 416 /* 417 * Move through num_args arguments. 418 */ 419 void dm_consume_args(struct dm_arg_set *as, unsigned num_args); 420 421 /*----------------------------------------------------------------- 422 * Functions for creating and manipulating mapped devices. 423 * Drop the reference with dm_put when you finish with the object. 424 *---------------------------------------------------------------*/ 425 426 /* 427 * DM_ANY_MINOR chooses the next available minor number. 428 */ 429 #define DM_ANY_MINOR (-1) 430 int dm_create(int minor, struct mapped_device **md); 431 432 /* 433 * Reference counting for md. 434 */ 435 struct mapped_device *dm_get_md(dev_t dev); 436 void dm_get(struct mapped_device *md); 437 int dm_hold(struct mapped_device *md); 438 void dm_put(struct mapped_device *md); 439 440 /* 441 * An arbitrary pointer may be stored alongside a mapped device. 442 */ 443 void dm_set_mdptr(struct mapped_device *md, void *ptr); 444 void *dm_get_mdptr(struct mapped_device *md); 445 446 /* 447 * A device can still be used while suspended, but I/O is deferred. 448 */ 449 int dm_suspend(struct mapped_device *md, unsigned suspend_flags); 450 int dm_resume(struct mapped_device *md); 451 452 /* 453 * Event functions. 454 */ 455 uint32_t dm_get_event_nr(struct mapped_device *md); 456 int dm_wait_event(struct mapped_device *md, int event_nr); 457 uint32_t dm_next_uevent_seq(struct mapped_device *md); 458 void dm_uevent_add(struct mapped_device *md, struct list_head *elist); 459 460 /* 461 * Info functions. 462 */ 463 const char *dm_device_name(struct mapped_device *md); 464 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid); 465 struct gendisk *dm_disk(struct mapped_device *md); 466 int dm_suspended(struct dm_target *ti); 467 int dm_post_suspending(struct dm_target *ti); 468 int dm_noflush_suspending(struct dm_target *ti); 469 void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors); 470 void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone); 471 union map_info *dm_get_rq_mapinfo(struct request *rq); 472 473 #ifdef CONFIG_BLK_DEV_ZONED 474 struct dm_report_zones_args { 475 struct dm_target *tgt; 476 sector_t next_sector; 477 478 void *orig_data; 479 report_zones_cb orig_cb; 480 unsigned int zone_idx; 481 482 /* must be filled by ->report_zones before calling dm_report_zones_cb */ 483 sector_t start; 484 }; 485 int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector, 486 struct dm_report_zones_args *args, unsigned int nr_zones); 487 #endif /* CONFIG_BLK_DEV_ZONED */ 488 489 /* 490 * Device mapper functions to parse and create devices specified by the 491 * parameter "dm-mod.create=" 492 */ 493 int __init dm_early_create(struct dm_ioctl *dmi, 494 struct dm_target_spec **spec_array, 495 char **target_params_array); 496 497 struct queue_limits *dm_get_queue_limits(struct mapped_device *md); 498 499 /* 500 * Geometry functions. 501 */ 502 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo); 503 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo); 504 505 /*----------------------------------------------------------------- 506 * Functions for manipulating device-mapper tables. 507 *---------------------------------------------------------------*/ 508 509 /* 510 * First create an empty table. 511 */ 512 int dm_table_create(struct dm_table **result, fmode_t mode, 513 unsigned num_targets, struct mapped_device *md); 514 515 /* 516 * Then call this once for each target. 517 */ 518 int dm_table_add_target(struct dm_table *t, const char *type, 519 sector_t start, sector_t len, char *params); 520 521 /* 522 * Target can use this to set the table's type. 523 * Can only ever be called from a target's ctr. 524 * Useful for "hybrid" target (supports both bio-based 525 * and request-based). 526 */ 527 void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type); 528 529 /* 530 * Finally call this to make the table ready for use. 531 */ 532 int dm_table_complete(struct dm_table *t); 533 534 /* 535 * Destroy the table when finished. 536 */ 537 void dm_table_destroy(struct dm_table *t); 538 539 /* 540 * Target may require that it is never sent I/O larger than len. 541 */ 542 int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len); 543 544 /* 545 * Table reference counting. 546 */ 547 struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx); 548 void dm_put_live_table(struct mapped_device *md, int srcu_idx); 549 void dm_sync_table(struct mapped_device *md); 550 551 /* 552 * Queries 553 */ 554 sector_t dm_table_get_size(struct dm_table *t); 555 unsigned int dm_table_get_num_targets(struct dm_table *t); 556 fmode_t dm_table_get_mode(struct dm_table *t); 557 struct mapped_device *dm_table_get_md(struct dm_table *t); 558 const char *dm_table_device_name(struct dm_table *t); 559 560 /* 561 * Trigger an event. 562 */ 563 void dm_table_event(struct dm_table *t); 564 565 /* 566 * Run the queue for request-based targets. 567 */ 568 void dm_table_run_md_queue_async(struct dm_table *t); 569 570 /* 571 * The device must be suspended before calling this method. 572 * Returns the previous table, which the caller must destroy. 573 */ 574 struct dm_table *dm_swap_table(struct mapped_device *md, 575 struct dm_table *t); 576 577 /* 578 * Table blk_crypto_profile functions 579 */ 580 void dm_destroy_crypto_profile(struct blk_crypto_profile *profile); 581 582 /*----------------------------------------------------------------- 583 * Macros. 584 *---------------------------------------------------------------*/ 585 #define DM_NAME "device-mapper" 586 587 #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n" 588 589 #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__) 590 591 #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__) 592 #define DMERR_LIMIT(fmt, ...) pr_err_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 593 #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__) 594 #define DMWARN_LIMIT(fmt, ...) pr_warn_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 595 #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__) 596 #define DMINFO_LIMIT(fmt, ...) pr_info_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 597 598 #define DMDEBUG(fmt, ...) pr_debug(DM_FMT(fmt), ##__VA_ARGS__) 599 #define DMDEBUG_LIMIT(fmt, ...) pr_debug_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 600 601 #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ 602 0 : scnprintf(result + sz, maxlen - sz, x)) 603 604 #define DMEMIT_TARGET_NAME_VERSION(y) \ 605 DMEMIT("target_name=%s,target_version=%u.%u.%u", \ 606 (y)->name, (y)->version[0], (y)->version[1], (y)->version[2]) 607 608 /* 609 * Definitions of return values from target end_io function. 610 */ 611 #define DM_ENDIO_DONE 0 612 #define DM_ENDIO_INCOMPLETE 1 613 #define DM_ENDIO_REQUEUE 2 614 #define DM_ENDIO_DELAY_REQUEUE 3 615 616 /* 617 * Definitions of return values from target map function. 618 */ 619 #define DM_MAPIO_SUBMITTED 0 620 #define DM_MAPIO_REMAPPED 1 621 #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE 622 #define DM_MAPIO_DELAY_REQUEUE DM_ENDIO_DELAY_REQUEUE 623 #define DM_MAPIO_KILL 4 624 625 #define dm_sector_div64(x, y)( \ 626 { \ 627 u64 _res; \ 628 (x) = div64_u64_rem(x, y, &_res); \ 629 _res; \ 630 } \ 631 ) 632 633 /* 634 * Ceiling(n / sz) 635 */ 636 #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz)) 637 638 #define dm_sector_div_up(n, sz) ( \ 639 { \ 640 sector_t _r = ((n) + (sz) - 1); \ 641 sector_div(_r, (sz)); \ 642 _r; \ 643 } \ 644 ) 645 646 /* 647 * ceiling(n / size) * size 648 */ 649 #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) 650 651 /* 652 * Sector offset taken relative to the start of the target instead of 653 * relative to the start of the device. 654 */ 655 #define dm_target_offset(ti, sector) ((sector) - (ti)->begin) 656 657 static inline sector_t to_sector(unsigned long long n) 658 { 659 return (n >> SECTOR_SHIFT); 660 } 661 662 static inline unsigned long to_bytes(sector_t n) 663 { 664 return (n << SECTOR_SHIFT); 665 } 666 667 #endif /* _LINUX_DEVICE_MAPPER_H */ 668