at v6.17-rc2 54 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * Portions Copyright (C) 1992 Drew Eckhardt 4 */ 5#ifndef _LINUX_BLKDEV_H 6#define _LINUX_BLKDEV_H 7 8#include <linux/types.h> 9#include <linux/blk_types.h> 10#include <linux/device.h> 11#include <linux/list.h> 12#include <linux/llist.h> 13#include <linux/minmax.h> 14#include <linux/timer.h> 15#include <linux/workqueue.h> 16#include <linux/wait.h> 17#include <linux/bio.h> 18#include <linux/gfp.h> 19#include <linux/kdev_t.h> 20#include <linux/rcupdate.h> 21#include <linux/percpu-refcount.h> 22#include <linux/blkzoned.h> 23#include <linux/sched.h> 24#include <linux/sbitmap.h> 25#include <linux/uuid.h> 26#include <linux/xarray.h> 27#include <linux/file.h> 28#include <linux/lockdep.h> 29 30struct module; 31struct request_queue; 32struct elevator_queue; 33struct blk_trace; 34struct request; 35struct sg_io_hdr; 36struct blkcg_gq; 37struct blk_flush_queue; 38struct kiocb; 39struct pr_ops; 40struct rq_qos; 41struct blk_queue_stats; 42struct blk_stat_callback; 43struct blk_crypto_profile; 44 45extern const struct device_type disk_type; 46extern const struct device_type part_type; 47extern const struct class block_class; 48 49/* 50 * Maximum number of blkcg policies allowed to be registered concurrently. 51 * Defined here to simplify include dependency. 52 */ 53#define BLKCG_MAX_POLS 6 54 55#define DISK_MAX_PARTS 256 56#define DISK_NAME_LEN 32 57 58#define PARTITION_META_INFO_VOLNAMELTH 64 59/* 60 * Enough for the string representation of any kind of UUID plus NULL. 61 * EFI UUID is 36 characters. MSDOS UUID is 11 characters. 62 */ 63#define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1) 64 65struct partition_meta_info { 66 char uuid[PARTITION_META_INFO_UUIDLTH]; 67 u8 volname[PARTITION_META_INFO_VOLNAMELTH]; 68}; 69 70/** 71 * DOC: genhd capability flags 72 * 73 * ``GENHD_FL_REMOVABLE``: indicates that the block device gives access to 74 * removable media. When set, the device remains present even when media is not 75 * inserted. Shall not be set for devices which are removed entirely when the 76 * media is removed. 77 * 78 * ``GENHD_FL_HIDDEN``: the block device is hidden; it doesn't produce events, 79 * doesn't appear in sysfs, and can't be opened from userspace or using 80 * blkdev_get*. Used for the underlying components of multipath devices. 81 * 82 * ``GENHD_FL_NO_PART``: partition support is disabled. The kernel will not 83 * scan for partitions from add_disk, and users can't add partitions manually. 84 * 85 */ 86enum { 87 GENHD_FL_REMOVABLE = 1 << 0, 88 GENHD_FL_HIDDEN = 1 << 1, 89 GENHD_FL_NO_PART = 1 << 2, 90}; 91 92enum { 93 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */ 94 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */ 95}; 96 97enum { 98 /* Poll even if events_poll_msecs is unset */ 99 DISK_EVENT_FLAG_POLL = 1 << 0, 100 /* Forward events to udev */ 101 DISK_EVENT_FLAG_UEVENT = 1 << 1, 102 /* Block event polling when open for exclusive write */ 103 DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE = 1 << 2, 104}; 105 106struct disk_events; 107struct badblocks; 108 109enum blk_integrity_checksum { 110 BLK_INTEGRITY_CSUM_NONE = 0, 111 BLK_INTEGRITY_CSUM_IP = 1, 112 BLK_INTEGRITY_CSUM_CRC = 2, 113 BLK_INTEGRITY_CSUM_CRC64 = 3, 114} __packed ; 115 116struct blk_integrity { 117 unsigned char flags; 118 enum blk_integrity_checksum csum_type; 119 unsigned char metadata_size; 120 unsigned char pi_offset; 121 unsigned char interval_exp; 122 unsigned char tag_size; 123 unsigned char pi_tuple_size; 124}; 125 126typedef unsigned int __bitwise blk_mode_t; 127 128/* open for reading */ 129#define BLK_OPEN_READ ((__force blk_mode_t)(1 << 0)) 130/* open for writing */ 131#define BLK_OPEN_WRITE ((__force blk_mode_t)(1 << 1)) 132/* open exclusively (vs other exclusive openers */ 133#define BLK_OPEN_EXCL ((__force blk_mode_t)(1 << 2)) 134/* opened with O_NDELAY */ 135#define BLK_OPEN_NDELAY ((__force blk_mode_t)(1 << 3)) 136/* open for "writes" only for ioctls (specialy hack for floppy.c) */ 137#define BLK_OPEN_WRITE_IOCTL ((__force blk_mode_t)(1 << 4)) 138/* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */ 139#define BLK_OPEN_RESTRICT_WRITES ((__force blk_mode_t)(1 << 5)) 140/* return partition scanning errors */ 141#define BLK_OPEN_STRICT_SCAN ((__force blk_mode_t)(1 << 6)) 142 143struct gendisk { 144 /* 145 * major/first_minor/minors should not be set by any new driver, the 146 * block core will take care of allocating them automatically. 147 */ 148 int major; 149 int first_minor; 150 int minors; 151 152 char disk_name[DISK_NAME_LEN]; /* name of major driver */ 153 154 unsigned short events; /* supported events */ 155 unsigned short event_flags; /* flags related to event processing */ 156 157 struct xarray part_tbl; 158 struct block_device *part0; 159 160 const struct block_device_operations *fops; 161 struct request_queue *queue; 162 void *private_data; 163 164 struct bio_set bio_split; 165 166 int flags; 167 unsigned long state; 168#define GD_NEED_PART_SCAN 0 169#define GD_READ_ONLY 1 170#define GD_DEAD 2 171#define GD_NATIVE_CAPACITY 3 172#define GD_ADDED 4 173#define GD_SUPPRESS_PART_SCAN 5 174#define GD_OWNS_QUEUE 6 175 176 struct mutex open_mutex; /* open/close mutex */ 177 unsigned open_partitions; /* number of open partitions */ 178 179 struct backing_dev_info *bdi; 180 struct kobject queue_kobj; /* the queue/ directory */ 181 struct kobject *slave_dir; 182#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED 183 struct list_head slave_bdevs; 184#endif 185 struct timer_rand_state *random; 186 struct disk_events *ev; 187 188#ifdef CONFIG_BLK_DEV_ZONED 189 /* 190 * Zoned block device information. Reads of this information must be 191 * protected with blk_queue_enter() / blk_queue_exit(). Modifying this 192 * information is only allowed while no requests are being processed. 193 * See also blk_mq_freeze_queue() and blk_mq_unfreeze_queue(). 194 */ 195 unsigned int nr_zones; 196 unsigned int zone_capacity; 197 unsigned int last_zone_capacity; 198 unsigned long __rcu *conv_zones_bitmap; 199 unsigned int zone_wplugs_hash_bits; 200 atomic_t nr_zone_wplugs; 201 spinlock_t zone_wplugs_lock; 202 struct mempool_s *zone_wplugs_pool; 203 struct hlist_head *zone_wplugs_hash; 204 struct workqueue_struct *zone_wplugs_wq; 205#endif /* CONFIG_BLK_DEV_ZONED */ 206 207#if IS_ENABLED(CONFIG_CDROM) 208 struct cdrom_device_info *cdi; 209#endif 210 int node_id; 211 struct badblocks *bb; 212 struct lockdep_map lockdep_map; 213 u64 diskseq; 214 blk_mode_t open_mode; 215 216 /* 217 * Independent sector access ranges. This is always NULL for 218 * devices that do not have multiple independent access ranges. 219 */ 220 struct blk_independent_access_ranges *ia_ranges; 221 222 struct mutex rqos_state_mutex; /* rqos state change mutex */ 223}; 224 225/** 226 * disk_openers - returns how many openers are there for a disk 227 * @disk: disk to check 228 * 229 * This returns the number of openers for a disk. Note that this value is only 230 * stable if disk->open_mutex is held. 231 * 232 * Note: Due to a quirk in the block layer open code, each open partition is 233 * only counted once even if there are multiple openers. 234 */ 235static inline unsigned int disk_openers(struct gendisk *disk) 236{ 237 return atomic_read(&disk->part0->bd_openers); 238} 239 240/** 241 * disk_has_partscan - return %true if partition scanning is enabled on a disk 242 * @disk: disk to check 243 * 244 * Returns %true if partitions scanning is enabled for @disk, or %false if 245 * partition scanning is disabled either permanently or temporarily. 246 */ 247static inline bool disk_has_partscan(struct gendisk *disk) 248{ 249 return !(disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN)) && 250 !test_bit(GD_SUPPRESS_PART_SCAN, &disk->state); 251} 252 253/* 254 * The gendisk is refcounted by the part0 block_device, and the bd_device 255 * therein is also used for device model presentation in sysfs. 256 */ 257#define dev_to_disk(device) \ 258 (dev_to_bdev(device)->bd_disk) 259#define disk_to_dev(disk) \ 260 (&((disk)->part0->bd_device)) 261 262#if IS_REACHABLE(CONFIG_CDROM) 263#define disk_to_cdi(disk) ((disk)->cdi) 264#else 265#define disk_to_cdi(disk) NULL 266#endif 267 268static inline dev_t disk_devt(struct gendisk *disk) 269{ 270 return MKDEV(disk->major, disk->first_minor); 271} 272 273#ifdef CONFIG_TRANSPARENT_HUGEPAGE 274/* 275 * We should strive for 1 << (PAGE_SHIFT + MAX_PAGECACHE_ORDER) 276 * however we constrain this to what we can validate and test. 277 */ 278#define BLK_MAX_BLOCK_SIZE SZ_64K 279#else 280#define BLK_MAX_BLOCK_SIZE PAGE_SIZE 281#endif 282 283 284/* blk_validate_limits() validates bsize, so drivers don't usually need to */ 285static inline int blk_validate_block_size(unsigned long bsize) 286{ 287 if (bsize < 512 || bsize > BLK_MAX_BLOCK_SIZE || !is_power_of_2(bsize)) 288 return -EINVAL; 289 290 return 0; 291} 292 293static inline bool blk_op_is_passthrough(blk_opf_t op) 294{ 295 op &= REQ_OP_MASK; 296 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT; 297} 298 299/* flags set by the driver in queue_limits.features */ 300typedef unsigned int __bitwise blk_features_t; 301 302/* supports a volatile write cache */ 303#define BLK_FEAT_WRITE_CACHE ((__force blk_features_t)(1u << 0)) 304 305/* supports passing on the FUA bit */ 306#define BLK_FEAT_FUA ((__force blk_features_t)(1u << 1)) 307 308/* rotational device (hard drive or floppy) */ 309#define BLK_FEAT_ROTATIONAL ((__force blk_features_t)(1u << 2)) 310 311/* contributes to the random number pool */ 312#define BLK_FEAT_ADD_RANDOM ((__force blk_features_t)(1u << 3)) 313 314/* do disk/partitions IO accounting */ 315#define BLK_FEAT_IO_STAT ((__force blk_features_t)(1u << 4)) 316 317/* don't modify data until writeback is done */ 318#define BLK_FEAT_STABLE_WRITES ((__force blk_features_t)(1u << 5)) 319 320/* always completes in submit context */ 321#define BLK_FEAT_SYNCHRONOUS ((__force blk_features_t)(1u << 6)) 322 323/* supports REQ_NOWAIT */ 324#define BLK_FEAT_NOWAIT ((__force blk_features_t)(1u << 7)) 325 326/* supports DAX */ 327#define BLK_FEAT_DAX ((__force blk_features_t)(1u << 8)) 328 329/* supports I/O polling */ 330#define BLK_FEAT_POLL ((__force blk_features_t)(1u << 9)) 331 332/* is a zoned device */ 333#define BLK_FEAT_ZONED ((__force blk_features_t)(1u << 10)) 334 335/* supports PCI(e) p2p requests */ 336#define BLK_FEAT_PCI_P2PDMA ((__force blk_features_t)(1u << 12)) 337 338/* skip this queue in blk_mq_(un)quiesce_tagset */ 339#define BLK_FEAT_SKIP_TAGSET_QUIESCE ((__force blk_features_t)(1u << 13)) 340 341/* undocumented magic for bcache */ 342#define BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE \ 343 ((__force blk_features_t)(1u << 15)) 344 345/* atomic writes enabled */ 346#define BLK_FEAT_ATOMIC_WRITES \ 347 ((__force blk_features_t)(1u << 16)) 348 349/* 350 * Flags automatically inherited when stacking limits. 351 */ 352#define BLK_FEAT_INHERIT_MASK \ 353 (BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA | BLK_FEAT_ROTATIONAL | \ 354 BLK_FEAT_STABLE_WRITES | BLK_FEAT_ZONED | \ 355 BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE) 356 357/* internal flags in queue_limits.flags */ 358typedef unsigned int __bitwise blk_flags_t; 359 360/* do not send FLUSH/FUA commands despite advertising a write cache */ 361#define BLK_FLAG_WRITE_CACHE_DISABLED ((__force blk_flags_t)(1u << 0)) 362 363/* I/O topology is misaligned */ 364#define BLK_FLAG_MISALIGNED ((__force blk_flags_t)(1u << 1)) 365 366/* passthrough command IO accounting */ 367#define BLK_FLAG_IOSTATS_PASSTHROUGH ((__force blk_flags_t)(1u << 2)) 368 369struct queue_limits { 370 blk_features_t features; 371 blk_flags_t flags; 372 unsigned long seg_boundary_mask; 373 unsigned long virt_boundary_mask; 374 375 unsigned int max_hw_sectors; 376 unsigned int max_dev_sectors; 377 unsigned int chunk_sectors; 378 unsigned int max_sectors; 379 unsigned int max_user_sectors; 380 unsigned int max_segment_size; 381 unsigned int min_segment_size; 382 unsigned int physical_block_size; 383 unsigned int logical_block_size; 384 unsigned int alignment_offset; 385 unsigned int io_min; 386 unsigned int io_opt; 387 unsigned int max_discard_sectors; 388 unsigned int max_hw_discard_sectors; 389 unsigned int max_user_discard_sectors; 390 unsigned int max_secure_erase_sectors; 391 unsigned int max_write_zeroes_sectors; 392 unsigned int max_wzeroes_unmap_sectors; 393 unsigned int max_hw_wzeroes_unmap_sectors; 394 unsigned int max_user_wzeroes_unmap_sectors; 395 unsigned int max_hw_zone_append_sectors; 396 unsigned int max_zone_append_sectors; 397 unsigned int discard_granularity; 398 unsigned int discard_alignment; 399 unsigned int zone_write_granularity; 400 401 /* atomic write limits */ 402 unsigned int atomic_write_hw_max; 403 unsigned int atomic_write_max_sectors; 404 unsigned int atomic_write_hw_boundary; 405 unsigned int atomic_write_boundary_sectors; 406 unsigned int atomic_write_hw_unit_min; 407 unsigned int atomic_write_unit_min; 408 unsigned int atomic_write_hw_unit_max; 409 unsigned int atomic_write_unit_max; 410 411 unsigned short max_segments; 412 unsigned short max_integrity_segments; 413 unsigned short max_discard_segments; 414 415 unsigned short max_write_streams; 416 unsigned int write_stream_granularity; 417 418 unsigned int max_open_zones; 419 unsigned int max_active_zones; 420 421 /* 422 * Drivers that set dma_alignment to less than 511 must be prepared to 423 * handle individual bvec's that are not a multiple of a SECTOR_SIZE 424 * due to possible offsets. 425 */ 426 unsigned int dma_alignment; 427 unsigned int dma_pad_mask; 428 429 struct blk_integrity integrity; 430}; 431 432typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx, 433 void *data); 434 435#define BLK_ALL_ZONES ((unsigned int)-1) 436int blkdev_report_zones(struct block_device *bdev, sector_t sector, 437 unsigned int nr_zones, report_zones_cb cb, void *data); 438int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op, 439 sector_t sectors, sector_t nr_sectors); 440int blk_revalidate_disk_zones(struct gendisk *disk); 441 442/* 443 * Independent access ranges: struct blk_independent_access_range describes 444 * a range of contiguous sectors that can be accessed using device command 445 * execution resources that are independent from the resources used for 446 * other access ranges. This is typically found with single-LUN multi-actuator 447 * HDDs where each access range is served by a different set of heads. 448 * The set of independent ranges supported by the device is defined using 449 * struct blk_independent_access_ranges. The independent ranges must not overlap 450 * and must include all sectors within the disk capacity (no sector holes 451 * allowed). 452 * For a device with multiple ranges, requests targeting sectors in different 453 * ranges can be executed in parallel. A request can straddle an access range 454 * boundary. 455 */ 456struct blk_independent_access_range { 457 struct kobject kobj; 458 sector_t sector; 459 sector_t nr_sectors; 460}; 461 462struct blk_independent_access_ranges { 463 struct kobject kobj; 464 bool sysfs_registered; 465 unsigned int nr_ia_ranges; 466 struct blk_independent_access_range ia_range[]; 467}; 468 469struct request_queue { 470 /* 471 * The queue owner gets to use this for whatever they like. 472 * ll_rw_blk doesn't touch it. 473 */ 474 void *queuedata; 475 476 struct elevator_queue *elevator; 477 478 const struct blk_mq_ops *mq_ops; 479 480 /* sw queues */ 481 struct blk_mq_ctx __percpu *queue_ctx; 482 483 /* 484 * various queue flags, see QUEUE_* below 485 */ 486 unsigned long queue_flags; 487 488 unsigned int rq_timeout; 489 490 unsigned int queue_depth; 491 492 refcount_t refs; 493 494 /* hw dispatch queues */ 495 unsigned int nr_hw_queues; 496 struct xarray hctx_table; 497 498 struct percpu_ref q_usage_counter; 499 struct lock_class_key io_lock_cls_key; 500 struct lockdep_map io_lockdep_map; 501 502 struct lock_class_key q_lock_cls_key; 503 struct lockdep_map q_lockdep_map; 504 505 struct request *last_merge; 506 507 spinlock_t queue_lock; 508 509 int quiesce_depth; 510 511 struct gendisk *disk; 512 513 /* 514 * mq queue kobject 515 */ 516 struct kobject *mq_kobj; 517 518 struct queue_limits limits; 519 520#ifdef CONFIG_PM 521 struct device *dev; 522 enum rpm_status rpm_status; 523#endif 524 525 /* 526 * Number of contexts that have called blk_set_pm_only(). If this 527 * counter is above zero then only RQF_PM requests are processed. 528 */ 529 atomic_t pm_only; 530 531 struct blk_queue_stats *stats; 532 struct rq_qos *rq_qos; 533 struct mutex rq_qos_mutex; 534 535 /* 536 * ida allocated id for this queue. Used to index queues from 537 * ioctx. 538 */ 539 int id; 540 541 /* 542 * queue settings 543 */ 544 unsigned long nr_requests; /* Max # of requests */ 545 546#ifdef CONFIG_BLK_INLINE_ENCRYPTION 547 struct blk_crypto_profile *crypto_profile; 548 struct kobject *crypto_kobject; 549#endif 550 551 struct timer_list timeout; 552 struct work_struct timeout_work; 553 554 atomic_t nr_active_requests_shared_tags; 555 556 struct blk_mq_tags *sched_shared_tags; 557 558 struct list_head icq_list; 559#ifdef CONFIG_BLK_CGROUP 560 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS); 561 struct blkcg_gq *root_blkg; 562 struct list_head blkg_list; 563 struct mutex blkcg_mutex; 564#endif 565 566 int node; 567 568 spinlock_t requeue_lock; 569 struct list_head requeue_list; 570 struct delayed_work requeue_work; 571 572#ifdef CONFIG_BLK_DEV_IO_TRACE 573 struct blk_trace __rcu *blk_trace; 574#endif 575 /* 576 * for flush operations 577 */ 578 struct blk_flush_queue *fq; 579 struct list_head flush_list; 580 581 /* 582 * Protects against I/O scheduler switching, particularly when updating 583 * q->elevator. Since the elevator update code path may also modify q-> 584 * nr_requests and wbt latency, this lock also protects the sysfs attrs 585 * nr_requests and wbt_lat_usec. Additionally the nr_hw_queues update 586 * may modify hctx tags, reserved-tags and cpumask, so this lock also 587 * helps protect the hctx sysfs/debugfs attrs. To ensure proper locking 588 * order during an elevator or nr_hw_queue update, first freeze the 589 * queue, then acquire ->elevator_lock. 590 */ 591 struct mutex elevator_lock; 592 593 struct mutex sysfs_lock; 594 /* 595 * Protects queue limits and also sysfs attribute read_ahead_kb. 596 */ 597 struct mutex limits_lock; 598 599 /* 600 * for reusing dead hctx instance in case of updating 601 * nr_hw_queues 602 */ 603 struct list_head unused_hctx_list; 604 spinlock_t unused_hctx_lock; 605 606 int mq_freeze_depth; 607 608#ifdef CONFIG_BLK_DEV_THROTTLING 609 /* Throttle data */ 610 struct throtl_data *td; 611#endif 612 struct rcu_head rcu_head; 613#ifdef CONFIG_LOCKDEP 614 struct task_struct *mq_freeze_owner; 615 int mq_freeze_owner_depth; 616 /* 617 * Records disk & queue state in current context, used in unfreeze 618 * queue 619 */ 620 bool mq_freeze_disk_dead; 621 bool mq_freeze_queue_dying; 622#endif 623 wait_queue_head_t mq_freeze_wq; 624 /* 625 * Protect concurrent access to q_usage_counter by 626 * percpu_ref_kill() and percpu_ref_reinit(). 627 */ 628 struct mutex mq_freeze_lock; 629 630 struct blk_mq_tag_set *tag_set; 631 struct list_head tag_set_list; 632 633 struct dentry *debugfs_dir; 634 struct dentry *sched_debugfs_dir; 635 struct dentry *rqos_debugfs_dir; 636 /* 637 * Serializes all debugfs metadata operations using the above dentries. 638 */ 639 struct mutex debugfs_mutex; 640}; 641 642/* Keep blk_queue_flag_name[] in sync with the definitions below */ 643enum { 644 QUEUE_FLAG_DYING, /* queue being torn down */ 645 QUEUE_FLAG_NOMERGES, /* disable merge attempts */ 646 QUEUE_FLAG_SAME_COMP, /* complete on same CPU-group */ 647 QUEUE_FLAG_FAIL_IO, /* fake timeout */ 648 QUEUE_FLAG_NOXMERGES, /* No extended merges */ 649 QUEUE_FLAG_SAME_FORCE, /* force complete on same CPU */ 650 QUEUE_FLAG_INIT_DONE, /* queue is initialized */ 651 QUEUE_FLAG_STATS, /* track IO start and completion times */ 652 QUEUE_FLAG_REGISTERED, /* queue has been registered to a disk */ 653 QUEUE_FLAG_QUIESCED, /* queue has been quiesced */ 654 QUEUE_FLAG_RQ_ALLOC_TIME, /* record rq->alloc_time_ns */ 655 QUEUE_FLAG_HCTX_ACTIVE, /* at least one blk-mq hctx is active */ 656 QUEUE_FLAG_SQ_SCHED, /* single queue style io dispatch */ 657 QUEUE_FLAG_DISABLE_WBT_DEF, /* for sched to disable/enable wbt */ 658 QUEUE_FLAG_NO_ELV_SWITCH, /* can't switch elevator any more */ 659 QUEUE_FLAG_MAX 660}; 661 662#define QUEUE_FLAG_MQ_DEFAULT (1UL << QUEUE_FLAG_SAME_COMP) 663 664void blk_queue_flag_set(unsigned int flag, struct request_queue *q); 665void blk_queue_flag_clear(unsigned int flag, struct request_queue *q); 666 667#define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags) 668#define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags) 669#define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags) 670#define blk_queue_noxmerges(q) \ 671 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags) 672#define blk_queue_nonrot(q) (!((q)->limits.features & BLK_FEAT_ROTATIONAL)) 673#define blk_queue_io_stat(q) ((q)->limits.features & BLK_FEAT_IO_STAT) 674#define blk_queue_passthrough_stat(q) \ 675 ((q)->limits.flags & BLK_FLAG_IOSTATS_PASSTHROUGH) 676#define blk_queue_dax(q) ((q)->limits.features & BLK_FEAT_DAX) 677#define blk_queue_pci_p2pdma(q) ((q)->limits.features & BLK_FEAT_PCI_P2PDMA) 678#ifdef CONFIG_BLK_RQ_ALLOC_TIME 679#define blk_queue_rq_alloc_time(q) \ 680 test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags) 681#else 682#define blk_queue_rq_alloc_time(q) false 683#endif 684 685#define blk_noretry_request(rq) \ 686 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \ 687 REQ_FAILFAST_DRIVER)) 688#define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags) 689#define blk_queue_pm_only(q) atomic_read(&(q)->pm_only) 690#define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags) 691#define blk_queue_sq_sched(q) test_bit(QUEUE_FLAG_SQ_SCHED, &(q)->queue_flags) 692#define blk_queue_skip_tagset_quiesce(q) \ 693 ((q)->limits.features & BLK_FEAT_SKIP_TAGSET_QUIESCE) 694#define blk_queue_disable_wbt(q) \ 695 test_bit(QUEUE_FLAG_DISABLE_WBT_DEF, &(q)->queue_flags) 696#define blk_queue_no_elv_switch(q) \ 697 test_bit(QUEUE_FLAG_NO_ELV_SWITCH, &(q)->queue_flags) 698 699extern void blk_set_pm_only(struct request_queue *q); 700extern void blk_clear_pm_only(struct request_queue *q); 701 702#define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist) 703 704#define dma_map_bvec(dev, bv, dir, attrs) \ 705 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \ 706 (dir), (attrs)) 707 708static inline bool queue_is_mq(struct request_queue *q) 709{ 710 return q->mq_ops; 711} 712 713#ifdef CONFIG_PM 714static inline enum rpm_status queue_rpm_status(struct request_queue *q) 715{ 716 return q->rpm_status; 717} 718#else 719static inline enum rpm_status queue_rpm_status(struct request_queue *q) 720{ 721 return RPM_ACTIVE; 722} 723#endif 724 725static inline bool blk_queue_is_zoned(struct request_queue *q) 726{ 727 return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && 728 (q->limits.features & BLK_FEAT_ZONED); 729} 730 731static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector) 732{ 733 if (!blk_queue_is_zoned(disk->queue)) 734 return 0; 735 return sector >> ilog2(disk->queue->limits.chunk_sectors); 736} 737 738static inline unsigned int bdev_max_open_zones(struct block_device *bdev) 739{ 740 return bdev->bd_disk->queue->limits.max_open_zones; 741} 742 743static inline unsigned int bdev_max_active_zones(struct block_device *bdev) 744{ 745 return bdev->bd_disk->queue->limits.max_active_zones; 746} 747 748static inline unsigned int blk_queue_depth(struct request_queue *q) 749{ 750 if (q->queue_depth) 751 return q->queue_depth; 752 753 return q->nr_requests; 754} 755 756/* 757 * default timeout for SG_IO if none specified 758 */ 759#define BLK_DEFAULT_SG_TIMEOUT (60 * HZ) 760#define BLK_MIN_SG_TIMEOUT (7 * HZ) 761 762/* This should not be used directly - use rq_for_each_segment */ 763#define for_each_bio(_bio) \ 764 for (; _bio; _bio = _bio->bi_next) 765 766int __must_check add_disk_fwnode(struct device *parent, struct gendisk *disk, 767 const struct attribute_group **groups, 768 struct fwnode_handle *fwnode); 769int __must_check device_add_disk(struct device *parent, struct gendisk *disk, 770 const struct attribute_group **groups); 771static inline int __must_check add_disk(struct gendisk *disk) 772{ 773 return device_add_disk(NULL, disk, NULL); 774} 775void del_gendisk(struct gendisk *gp); 776void invalidate_disk(struct gendisk *disk); 777void set_disk_ro(struct gendisk *disk, bool read_only); 778void disk_uevent(struct gendisk *disk, enum kobject_action action); 779 780static inline u8 bdev_partno(const struct block_device *bdev) 781{ 782 return atomic_read(&bdev->__bd_flags) & BD_PARTNO; 783} 784 785static inline bool bdev_test_flag(const struct block_device *bdev, unsigned flag) 786{ 787 return atomic_read(&bdev->__bd_flags) & flag; 788} 789 790static inline void bdev_set_flag(struct block_device *bdev, unsigned flag) 791{ 792 atomic_or(flag, &bdev->__bd_flags); 793} 794 795static inline void bdev_clear_flag(struct block_device *bdev, unsigned flag) 796{ 797 atomic_andnot(flag, &bdev->__bd_flags); 798} 799 800static inline bool get_disk_ro(struct gendisk *disk) 801{ 802 return bdev_test_flag(disk->part0, BD_READ_ONLY) || 803 test_bit(GD_READ_ONLY, &disk->state); 804} 805 806static inline bool bdev_read_only(struct block_device *bdev) 807{ 808 return bdev_test_flag(bdev, BD_READ_ONLY) || get_disk_ro(bdev->bd_disk); 809} 810 811bool set_capacity_and_notify(struct gendisk *disk, sector_t size); 812void disk_force_media_change(struct gendisk *disk); 813void bdev_mark_dead(struct block_device *bdev, bool surprise); 814 815void add_disk_randomness(struct gendisk *disk) __latent_entropy; 816void rand_initialize_disk(struct gendisk *disk); 817 818static inline sector_t get_start_sect(struct block_device *bdev) 819{ 820 return bdev->bd_start_sect; 821} 822 823static inline sector_t bdev_nr_sectors(struct block_device *bdev) 824{ 825 return bdev->bd_nr_sectors; 826} 827 828static inline loff_t bdev_nr_bytes(struct block_device *bdev) 829{ 830 return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT; 831} 832 833static inline sector_t get_capacity(struct gendisk *disk) 834{ 835 return bdev_nr_sectors(disk->part0); 836} 837 838static inline u64 sb_bdev_nr_blocks(struct super_block *sb) 839{ 840 return bdev_nr_sectors(sb->s_bdev) >> 841 (sb->s_blocksize_bits - SECTOR_SHIFT); 842} 843 844#ifdef CONFIG_BLK_DEV_ZONED 845static inline unsigned int disk_nr_zones(struct gendisk *disk) 846{ 847 return disk->nr_zones; 848} 849 850/** 851 * bio_needs_zone_write_plugging - Check if a BIO needs to be handled with zone 852 * write plugging 853 * @bio: The BIO being submitted 854 * 855 * Return true whenever @bio execution needs to be handled through zone 856 * write plugging (using blk_zone_plug_bio()). Return false otherwise. 857 */ 858static inline bool bio_needs_zone_write_plugging(struct bio *bio) 859{ 860 enum req_op op = bio_op(bio); 861 862 /* 863 * Only zoned block devices have a zone write plug hash table. But not 864 * all of them have one (e.g. DM devices may not need one). 865 */ 866 if (!bio->bi_bdev->bd_disk->zone_wplugs_hash) 867 return false; 868 869 /* Only write operations need zone write plugging. */ 870 if (!op_is_write(op)) 871 return false; 872 873 /* Ignore empty flush */ 874 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio)) 875 return false; 876 877 /* Ignore BIOs that already have been handled by zone write plugging. */ 878 if (bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING)) 879 return false; 880 881 /* 882 * All zone write operations must be handled through zone write plugging 883 * using blk_zone_plug_bio(). 884 */ 885 switch (op) { 886 case REQ_OP_ZONE_APPEND: 887 case REQ_OP_WRITE: 888 case REQ_OP_WRITE_ZEROES: 889 case REQ_OP_ZONE_FINISH: 890 case REQ_OP_ZONE_RESET: 891 case REQ_OP_ZONE_RESET_ALL: 892 return true; 893 default: 894 return false; 895 } 896} 897 898bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs); 899 900/** 901 * disk_zone_capacity - returns the zone capacity of zone containing @sector 902 * @disk: disk to work with 903 * @sector: sector number within the querying zone 904 * 905 * Returns the zone capacity of a zone containing @sector. @sector can be any 906 * sector in the zone. 907 */ 908static inline unsigned int disk_zone_capacity(struct gendisk *disk, 909 sector_t sector) 910{ 911 sector_t zone_sectors = disk->queue->limits.chunk_sectors; 912 913 if (sector + zone_sectors >= get_capacity(disk)) 914 return disk->last_zone_capacity; 915 return disk->zone_capacity; 916} 917static inline unsigned int bdev_zone_capacity(struct block_device *bdev, 918 sector_t pos) 919{ 920 return disk_zone_capacity(bdev->bd_disk, pos); 921} 922#else /* CONFIG_BLK_DEV_ZONED */ 923static inline unsigned int disk_nr_zones(struct gendisk *disk) 924{ 925 return 0; 926} 927 928static inline bool bio_needs_zone_write_plugging(struct bio *bio) 929{ 930 return false; 931} 932 933static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs) 934{ 935 return false; 936} 937#endif /* CONFIG_BLK_DEV_ZONED */ 938 939static inline unsigned int bdev_nr_zones(struct block_device *bdev) 940{ 941 return disk_nr_zones(bdev->bd_disk); 942} 943 944int bdev_disk_changed(struct gendisk *disk, bool invalidate); 945 946void put_disk(struct gendisk *disk); 947struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node, 948 struct lock_class_key *lkclass); 949 950/** 951 * blk_alloc_disk - allocate a gendisk structure 952 * @lim: queue limits to be used for this disk. 953 * @node_id: numa node to allocate on 954 * 955 * Allocate and pre-initialize a gendisk structure for use with BIO based 956 * drivers. 957 * 958 * Returns an ERR_PTR on error, else the allocated disk. 959 * 960 * Context: can sleep 961 */ 962#define blk_alloc_disk(lim, node_id) \ 963({ \ 964 static struct lock_class_key __key; \ 965 \ 966 __blk_alloc_disk(lim, node_id, &__key); \ 967}) 968 969int __register_blkdev(unsigned int major, const char *name, 970 void (*probe)(dev_t devt)); 971#define register_blkdev(major, name) \ 972 __register_blkdev(major, name, NULL) 973void unregister_blkdev(unsigned int major, const char *name); 974 975bool disk_check_media_change(struct gendisk *disk); 976void set_capacity(struct gendisk *disk, sector_t size); 977 978#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED 979int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk); 980void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk); 981#else 982static inline int bd_link_disk_holder(struct block_device *bdev, 983 struct gendisk *disk) 984{ 985 return 0; 986} 987static inline void bd_unlink_disk_holder(struct block_device *bdev, 988 struct gendisk *disk) 989{ 990} 991#endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */ 992 993dev_t part_devt(struct gendisk *disk, u8 partno); 994void inc_diskseq(struct gendisk *disk); 995void blk_request_module(dev_t devt); 996 997extern int blk_register_queue(struct gendisk *disk); 998extern void blk_unregister_queue(struct gendisk *disk); 999void submit_bio_noacct(struct bio *bio); 1000struct bio *bio_split_to_limits(struct bio *bio); 1001 1002extern int blk_lld_busy(struct request_queue *q); 1003extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags); 1004extern void blk_queue_exit(struct request_queue *q); 1005extern void blk_sync_queue(struct request_queue *q); 1006 1007/* Helper to convert REQ_OP_XXX to its string format XXX */ 1008extern const char *blk_op_str(enum req_op op); 1009 1010int blk_status_to_errno(blk_status_t status); 1011blk_status_t errno_to_blk_status(int errno); 1012const char *blk_status_to_str(blk_status_t status); 1013 1014/* only poll the hardware once, don't continue until a completion was found */ 1015#define BLK_POLL_ONESHOT (1 << 0) 1016int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags); 1017int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob, 1018 unsigned int flags); 1019 1020static inline struct request_queue *bdev_get_queue(struct block_device *bdev) 1021{ 1022 return bdev->bd_queue; /* this is never NULL */ 1023} 1024 1025/* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */ 1026const char *blk_zone_cond_str(enum blk_zone_cond zone_cond); 1027 1028static inline unsigned int bio_zone_no(struct bio *bio) 1029{ 1030 return disk_zone_no(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector); 1031} 1032 1033static inline bool bio_straddles_zones(struct bio *bio) 1034{ 1035 return bio_sectors(bio) && 1036 bio_zone_no(bio) != 1037 disk_zone_no(bio->bi_bdev->bd_disk, bio_end_sector(bio) - 1); 1038} 1039 1040/* 1041 * Return how much within the boundary is left to be used for I/O at a given 1042 * offset. 1043 */ 1044static inline unsigned int blk_boundary_sectors_left(sector_t offset, 1045 unsigned int boundary_sectors) 1046{ 1047 if (unlikely(!is_power_of_2(boundary_sectors))) 1048 return boundary_sectors - sector_div(offset, boundary_sectors); 1049 return boundary_sectors - (offset & (boundary_sectors - 1)); 1050} 1051 1052/** 1053 * queue_limits_start_update - start an atomic update of queue limits 1054 * @q: queue to update 1055 * 1056 * This functions starts an atomic update of the queue limits. It takes a lock 1057 * to prevent other updates and returns a snapshot of the current limits that 1058 * the caller can modify. The caller must call queue_limits_commit_update() 1059 * to finish the update. 1060 * 1061 * Context: process context. 1062 */ 1063static inline struct queue_limits 1064queue_limits_start_update(struct request_queue *q) 1065{ 1066 mutex_lock(&q->limits_lock); 1067 return q->limits; 1068} 1069int queue_limits_commit_update_frozen(struct request_queue *q, 1070 struct queue_limits *lim); 1071int queue_limits_commit_update(struct request_queue *q, 1072 struct queue_limits *lim); 1073int queue_limits_set(struct request_queue *q, struct queue_limits *lim); 1074int blk_validate_limits(struct queue_limits *lim); 1075 1076/** 1077 * queue_limits_cancel_update - cancel an atomic update of queue limits 1078 * @q: queue to update 1079 * 1080 * This functions cancels an atomic update of the queue limits started by 1081 * queue_limits_start_update() and should be used when an error occurs after 1082 * starting update. 1083 */ 1084static inline void queue_limits_cancel_update(struct request_queue *q) 1085{ 1086 mutex_unlock(&q->limits_lock); 1087} 1088 1089/* 1090 * These helpers are for drivers that have sloppy feature negotiation and might 1091 * have to disable DISCARD, WRITE_ZEROES or SECURE_DISCARD from the I/O 1092 * completion handler when the device returned an indicator that the respective 1093 * feature is not actually supported. They are racy and the driver needs to 1094 * cope with that. Try to avoid this scheme if you can. 1095 */ 1096static inline void blk_queue_disable_discard(struct request_queue *q) 1097{ 1098 q->limits.max_discard_sectors = 0; 1099} 1100 1101static inline void blk_queue_disable_secure_erase(struct request_queue *q) 1102{ 1103 q->limits.max_secure_erase_sectors = 0; 1104} 1105 1106static inline void blk_queue_disable_write_zeroes(struct request_queue *q) 1107{ 1108 q->limits.max_write_zeroes_sectors = 0; 1109 q->limits.max_wzeroes_unmap_sectors = 0; 1110} 1111 1112/* 1113 * Access functions for manipulating queue properties 1114 */ 1115extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth); 1116extern void blk_set_stacking_limits(struct queue_limits *lim); 1117extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 1118 sector_t offset); 1119void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev, 1120 sector_t offset, const char *pfx); 1121extern void blk_queue_rq_timeout(struct request_queue *, unsigned int); 1122 1123struct blk_independent_access_ranges * 1124disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges); 1125void disk_set_independent_access_ranges(struct gendisk *disk, 1126 struct blk_independent_access_ranges *iars); 1127 1128bool __must_check blk_get_queue(struct request_queue *); 1129extern void blk_put_queue(struct request_queue *); 1130 1131void blk_mark_disk_dead(struct gendisk *disk); 1132 1133struct rq_list { 1134 struct request *head; 1135 struct request *tail; 1136}; 1137 1138#ifdef CONFIG_BLOCK 1139/* 1140 * blk_plug permits building a queue of related requests by holding the I/O 1141 * fragments for a short period. This allows merging of sequential requests 1142 * into single larger request. As the requests are moved from a per-task list to 1143 * the device's request_queue in a batch, this results in improved scalability 1144 * as the lock contention for request_queue lock is reduced. 1145 * 1146 * It is ok not to disable preemption when adding the request to the plug list 1147 * or when attempting a merge. For details, please see schedule() where 1148 * blk_flush_plug() is called. 1149 */ 1150struct blk_plug { 1151 struct rq_list mq_list; /* blk-mq requests */ 1152 1153 /* if ios_left is > 1, we can batch tag/rq allocations */ 1154 struct rq_list cached_rqs; 1155 u64 cur_ktime; 1156 unsigned short nr_ios; 1157 1158 unsigned short rq_count; 1159 1160 bool multiple_queues; 1161 bool has_elevator; 1162 1163 struct list_head cb_list; /* md requires an unplug callback */ 1164}; 1165 1166struct blk_plug_cb; 1167typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool); 1168struct blk_plug_cb { 1169 struct list_head list; 1170 blk_plug_cb_fn callback; 1171 void *data; 1172}; 1173extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, 1174 void *data, int size); 1175extern void blk_start_plug(struct blk_plug *); 1176extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short); 1177extern void blk_finish_plug(struct blk_plug *); 1178 1179void __blk_flush_plug(struct blk_plug *plug, bool from_schedule); 1180static inline void blk_flush_plug(struct blk_plug *plug, bool async) 1181{ 1182 if (plug) 1183 __blk_flush_plug(plug, async); 1184} 1185 1186/* 1187 * tsk == current here 1188 */ 1189static inline void blk_plug_invalidate_ts(struct task_struct *tsk) 1190{ 1191 struct blk_plug *plug = tsk->plug; 1192 1193 if (plug) 1194 plug->cur_ktime = 0; 1195 current->flags &= ~PF_BLOCK_TS; 1196} 1197 1198int blkdev_issue_flush(struct block_device *bdev); 1199long nr_blockdev_pages(void); 1200#else /* CONFIG_BLOCK */ 1201struct blk_plug { 1202}; 1203 1204static inline void blk_start_plug_nr_ios(struct blk_plug *plug, 1205 unsigned short nr_ios) 1206{ 1207} 1208 1209static inline void blk_start_plug(struct blk_plug *plug) 1210{ 1211} 1212 1213static inline void blk_finish_plug(struct blk_plug *plug) 1214{ 1215} 1216 1217static inline void blk_flush_plug(struct blk_plug *plug, bool async) 1218{ 1219} 1220 1221static inline void blk_plug_invalidate_ts(struct task_struct *tsk) 1222{ 1223} 1224 1225static inline int blkdev_issue_flush(struct block_device *bdev) 1226{ 1227 return 0; 1228} 1229 1230static inline long nr_blockdev_pages(void) 1231{ 1232 return 0; 1233} 1234#endif /* CONFIG_BLOCK */ 1235 1236extern void blk_io_schedule(void); 1237 1238int blkdev_issue_discard(struct block_device *bdev, sector_t sector, 1239 sector_t nr_sects, gfp_t gfp_mask); 1240int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, 1241 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop); 1242int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, 1243 sector_t nr_sects, gfp_t gfp); 1244 1245#define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */ 1246#define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */ 1247#define BLKDEV_ZERO_KILLABLE (1 << 2) /* interruptible by fatal signals */ 1248 1249extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 1250 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, 1251 unsigned flags); 1252extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 1253 sector_t nr_sects, gfp_t gfp_mask, unsigned flags); 1254 1255static inline int sb_issue_discard(struct super_block *sb, sector_t block, 1256 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags) 1257{ 1258 return blkdev_issue_discard(sb->s_bdev, 1259 block << (sb->s_blocksize_bits - 1260 SECTOR_SHIFT), 1261 nr_blocks << (sb->s_blocksize_bits - 1262 SECTOR_SHIFT), 1263 gfp_mask); 1264} 1265static inline int sb_issue_zeroout(struct super_block *sb, sector_t block, 1266 sector_t nr_blocks, gfp_t gfp_mask) 1267{ 1268 return blkdev_issue_zeroout(sb->s_bdev, 1269 block << (sb->s_blocksize_bits - 1270 SECTOR_SHIFT), 1271 nr_blocks << (sb->s_blocksize_bits - 1272 SECTOR_SHIFT), 1273 gfp_mask, 0); 1274} 1275 1276static inline bool bdev_is_partition(struct block_device *bdev) 1277{ 1278 return bdev_partno(bdev) != 0; 1279} 1280 1281enum blk_default_limits { 1282 BLK_MAX_SEGMENTS = 128, 1283 BLK_SAFE_MAX_SECTORS = 255, 1284 BLK_MAX_SEGMENT_SIZE = 65536, 1285 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL, 1286}; 1287 1288static inline struct queue_limits *bdev_limits(struct block_device *bdev) 1289{ 1290 return &bdev_get_queue(bdev)->limits; 1291} 1292 1293static inline unsigned long queue_segment_boundary(const struct request_queue *q) 1294{ 1295 return q->limits.seg_boundary_mask; 1296} 1297 1298static inline unsigned long queue_virt_boundary(const struct request_queue *q) 1299{ 1300 return q->limits.virt_boundary_mask; 1301} 1302 1303static inline unsigned int queue_max_sectors(const struct request_queue *q) 1304{ 1305 return q->limits.max_sectors; 1306} 1307 1308static inline unsigned int queue_max_bytes(struct request_queue *q) 1309{ 1310 return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9; 1311} 1312 1313static inline unsigned int queue_max_hw_sectors(const struct request_queue *q) 1314{ 1315 return q->limits.max_hw_sectors; 1316} 1317 1318static inline unsigned short queue_max_segments(const struct request_queue *q) 1319{ 1320 return q->limits.max_segments; 1321} 1322 1323static inline unsigned short queue_max_discard_segments(const struct request_queue *q) 1324{ 1325 return q->limits.max_discard_segments; 1326} 1327 1328static inline unsigned int queue_max_segment_size(const struct request_queue *q) 1329{ 1330 return q->limits.max_segment_size; 1331} 1332 1333static inline bool queue_emulates_zone_append(struct request_queue *q) 1334{ 1335 return blk_queue_is_zoned(q) && !q->limits.max_hw_zone_append_sectors; 1336} 1337 1338static inline bool bdev_emulates_zone_append(struct block_device *bdev) 1339{ 1340 return queue_emulates_zone_append(bdev_get_queue(bdev)); 1341} 1342 1343static inline unsigned int 1344bdev_max_zone_append_sectors(struct block_device *bdev) 1345{ 1346 return bdev_limits(bdev)->max_zone_append_sectors; 1347} 1348 1349static inline unsigned int bdev_max_segments(struct block_device *bdev) 1350{ 1351 return queue_max_segments(bdev_get_queue(bdev)); 1352} 1353 1354static inline unsigned short bdev_max_write_streams(struct block_device *bdev) 1355{ 1356 if (bdev_is_partition(bdev)) 1357 return 0; 1358 return bdev_limits(bdev)->max_write_streams; 1359} 1360 1361static inline unsigned queue_logical_block_size(const struct request_queue *q) 1362{ 1363 return q->limits.logical_block_size; 1364} 1365 1366static inline unsigned int bdev_logical_block_size(struct block_device *bdev) 1367{ 1368 return queue_logical_block_size(bdev_get_queue(bdev)); 1369} 1370 1371static inline unsigned int queue_physical_block_size(const struct request_queue *q) 1372{ 1373 return q->limits.physical_block_size; 1374} 1375 1376static inline unsigned int bdev_physical_block_size(struct block_device *bdev) 1377{ 1378 return queue_physical_block_size(bdev_get_queue(bdev)); 1379} 1380 1381static inline unsigned int queue_io_min(const struct request_queue *q) 1382{ 1383 return q->limits.io_min; 1384} 1385 1386static inline unsigned int bdev_io_min(struct block_device *bdev) 1387{ 1388 return queue_io_min(bdev_get_queue(bdev)); 1389} 1390 1391static inline unsigned int queue_io_opt(const struct request_queue *q) 1392{ 1393 return q->limits.io_opt; 1394} 1395 1396static inline unsigned int bdev_io_opt(struct block_device *bdev) 1397{ 1398 return queue_io_opt(bdev_get_queue(bdev)); 1399} 1400 1401static inline unsigned int 1402queue_zone_write_granularity(const struct request_queue *q) 1403{ 1404 return q->limits.zone_write_granularity; 1405} 1406 1407static inline unsigned int 1408bdev_zone_write_granularity(struct block_device *bdev) 1409{ 1410 return queue_zone_write_granularity(bdev_get_queue(bdev)); 1411} 1412 1413int bdev_alignment_offset(struct block_device *bdev); 1414unsigned int bdev_discard_alignment(struct block_device *bdev); 1415 1416static inline unsigned int bdev_max_discard_sectors(struct block_device *bdev) 1417{ 1418 return bdev_limits(bdev)->max_discard_sectors; 1419} 1420 1421static inline unsigned int bdev_discard_granularity(struct block_device *bdev) 1422{ 1423 return bdev_limits(bdev)->discard_granularity; 1424} 1425 1426static inline unsigned int 1427bdev_max_secure_erase_sectors(struct block_device *bdev) 1428{ 1429 return bdev_limits(bdev)->max_secure_erase_sectors; 1430} 1431 1432static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev) 1433{ 1434 return bdev_limits(bdev)->max_write_zeroes_sectors; 1435} 1436 1437static inline unsigned int 1438bdev_write_zeroes_unmap_sectors(struct block_device *bdev) 1439{ 1440 return bdev_limits(bdev)->max_wzeroes_unmap_sectors; 1441} 1442 1443static inline bool bdev_nonrot(struct block_device *bdev) 1444{ 1445 return blk_queue_nonrot(bdev_get_queue(bdev)); 1446} 1447 1448static inline bool bdev_synchronous(struct block_device *bdev) 1449{ 1450 return bdev->bd_disk->queue->limits.features & BLK_FEAT_SYNCHRONOUS; 1451} 1452 1453static inline bool bdev_stable_writes(struct block_device *bdev) 1454{ 1455 struct request_queue *q = bdev_get_queue(bdev); 1456 1457 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) && 1458 q->limits.integrity.csum_type != BLK_INTEGRITY_CSUM_NONE) 1459 return true; 1460 return q->limits.features & BLK_FEAT_STABLE_WRITES; 1461} 1462 1463static inline bool blk_queue_write_cache(struct request_queue *q) 1464{ 1465 return (q->limits.features & BLK_FEAT_WRITE_CACHE) && 1466 !(q->limits.flags & BLK_FLAG_WRITE_CACHE_DISABLED); 1467} 1468 1469static inline bool bdev_write_cache(struct block_device *bdev) 1470{ 1471 return blk_queue_write_cache(bdev_get_queue(bdev)); 1472} 1473 1474static inline bool bdev_fua(struct block_device *bdev) 1475{ 1476 return bdev_limits(bdev)->features & BLK_FEAT_FUA; 1477} 1478 1479static inline bool bdev_nowait(struct block_device *bdev) 1480{ 1481 return bdev->bd_disk->queue->limits.features & BLK_FEAT_NOWAIT; 1482} 1483 1484static inline bool bdev_is_zoned(struct block_device *bdev) 1485{ 1486 return blk_queue_is_zoned(bdev_get_queue(bdev)); 1487} 1488 1489static inline unsigned int bdev_zone_no(struct block_device *bdev, sector_t sec) 1490{ 1491 return disk_zone_no(bdev->bd_disk, sec); 1492} 1493 1494static inline sector_t bdev_zone_sectors(struct block_device *bdev) 1495{ 1496 struct request_queue *q = bdev_get_queue(bdev); 1497 1498 if (!blk_queue_is_zoned(q)) 1499 return 0; 1500 return q->limits.chunk_sectors; 1501} 1502 1503static inline sector_t bdev_offset_from_zone_start(struct block_device *bdev, 1504 sector_t sector) 1505{ 1506 return sector & (bdev_zone_sectors(bdev) - 1); 1507} 1508 1509static inline sector_t bio_offset_from_zone_start(struct bio *bio) 1510{ 1511 return bdev_offset_from_zone_start(bio->bi_bdev, 1512 bio->bi_iter.bi_sector); 1513} 1514 1515static inline bool bdev_is_zone_start(struct block_device *bdev, 1516 sector_t sector) 1517{ 1518 return bdev_offset_from_zone_start(bdev, sector) == 0; 1519} 1520 1521/* Check whether @sector is a multiple of the zone size. */ 1522static inline bool bdev_is_zone_aligned(struct block_device *bdev, 1523 sector_t sector) 1524{ 1525 return bdev_is_zone_start(bdev, sector); 1526} 1527 1528/** 1529 * bdev_zone_is_seq - check if a sector belongs to a sequential write zone 1530 * @bdev: block device to check 1531 * @sector: sector number 1532 * 1533 * Check if @sector on @bdev is contained in a sequential write required zone. 1534 */ 1535static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector) 1536{ 1537 bool is_seq = false; 1538 1539#if IS_ENABLED(CONFIG_BLK_DEV_ZONED) 1540 if (bdev_is_zoned(bdev)) { 1541 struct gendisk *disk = bdev->bd_disk; 1542 unsigned long *bitmap; 1543 1544 rcu_read_lock(); 1545 bitmap = rcu_dereference(disk->conv_zones_bitmap); 1546 is_seq = !bitmap || 1547 !test_bit(disk_zone_no(disk, sector), bitmap); 1548 rcu_read_unlock(); 1549 } 1550#endif 1551 1552 return is_seq; 1553} 1554 1555int blk_zone_issue_zeroout(struct block_device *bdev, sector_t sector, 1556 sector_t nr_sects, gfp_t gfp_mask); 1557 1558static inline unsigned int queue_dma_alignment(const struct request_queue *q) 1559{ 1560 return q->limits.dma_alignment; 1561} 1562 1563static inline unsigned int 1564queue_atomic_write_unit_max_bytes(const struct request_queue *q) 1565{ 1566 return q->limits.atomic_write_unit_max; 1567} 1568 1569static inline unsigned int 1570queue_atomic_write_unit_min_bytes(const struct request_queue *q) 1571{ 1572 return q->limits.atomic_write_unit_min; 1573} 1574 1575static inline unsigned int 1576queue_atomic_write_boundary_bytes(const struct request_queue *q) 1577{ 1578 return q->limits.atomic_write_boundary_sectors << SECTOR_SHIFT; 1579} 1580 1581static inline unsigned int 1582queue_atomic_write_max_bytes(const struct request_queue *q) 1583{ 1584 return q->limits.atomic_write_max_sectors << SECTOR_SHIFT; 1585} 1586 1587static inline unsigned int bdev_dma_alignment(struct block_device *bdev) 1588{ 1589 return queue_dma_alignment(bdev_get_queue(bdev)); 1590} 1591 1592static inline bool bdev_iter_is_aligned(struct block_device *bdev, 1593 struct iov_iter *iter) 1594{ 1595 return iov_iter_is_aligned(iter, bdev_dma_alignment(bdev), 1596 bdev_logical_block_size(bdev) - 1); 1597} 1598 1599static inline unsigned int 1600blk_lim_dma_alignment_and_pad(struct queue_limits *lim) 1601{ 1602 return lim->dma_alignment | lim->dma_pad_mask; 1603} 1604 1605static inline bool blk_rq_aligned(struct request_queue *q, unsigned long addr, 1606 unsigned int len) 1607{ 1608 unsigned int alignment = blk_lim_dma_alignment_and_pad(&q->limits); 1609 1610 return !(addr & alignment) && !(len & alignment); 1611} 1612 1613/* assumes size > 256 */ 1614static inline unsigned int blksize_bits(unsigned int size) 1615{ 1616 return order_base_2(size >> SECTOR_SHIFT) + SECTOR_SHIFT; 1617} 1618 1619int kblockd_schedule_work(struct work_struct *work); 1620int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay); 1621 1622#define MODULE_ALIAS_BLOCKDEV(major,minor) \ 1623 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor)) 1624#define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \ 1625 MODULE_ALIAS("block-major-" __stringify(major) "-*") 1626 1627#ifdef CONFIG_BLK_INLINE_ENCRYPTION 1628 1629bool blk_crypto_register(struct blk_crypto_profile *profile, 1630 struct request_queue *q); 1631 1632#else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1633 1634static inline bool blk_crypto_register(struct blk_crypto_profile *profile, 1635 struct request_queue *q) 1636{ 1637 return true; 1638} 1639 1640#endif /* CONFIG_BLK_INLINE_ENCRYPTION */ 1641 1642enum blk_unique_id { 1643 /* these match the Designator Types specified in SPC */ 1644 BLK_UID_T10 = 1, 1645 BLK_UID_EUI64 = 2, 1646 BLK_UID_NAA = 3, 1647}; 1648 1649struct block_device_operations { 1650 void (*submit_bio)(struct bio *bio); 1651 int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob, 1652 unsigned int flags); 1653 int (*open)(struct gendisk *disk, blk_mode_t mode); 1654 void (*release)(struct gendisk *disk); 1655 int (*ioctl)(struct block_device *bdev, blk_mode_t mode, 1656 unsigned cmd, unsigned long arg); 1657 int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode, 1658 unsigned cmd, unsigned long arg); 1659 unsigned int (*check_events) (struct gendisk *disk, 1660 unsigned int clearing); 1661 void (*unlock_native_capacity) (struct gendisk *); 1662 int (*getgeo)(struct block_device *, struct hd_geometry *); 1663 int (*set_read_only)(struct block_device *bdev, bool ro); 1664 void (*free_disk)(struct gendisk *disk); 1665 /* this callback is with swap_lock and sometimes page table lock held */ 1666 void (*swap_slot_free_notify) (struct block_device *, unsigned long); 1667 int (*report_zones)(struct gendisk *, sector_t sector, 1668 unsigned int nr_zones, report_zones_cb cb, void *data); 1669 char *(*devnode)(struct gendisk *disk, umode_t *mode); 1670 /* returns the length of the identifier or a negative errno: */ 1671 int (*get_unique_id)(struct gendisk *disk, u8 id[16], 1672 enum blk_unique_id id_type); 1673 struct module *owner; 1674 const struct pr_ops *pr_ops; 1675 1676 /* 1677 * Special callback for probing GPT entry at a given sector. 1678 * Needed by Android devices, used by GPT scanner and MMC blk 1679 * driver. 1680 */ 1681 int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector); 1682}; 1683 1684#ifdef CONFIG_COMPAT 1685extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t, 1686 unsigned int, unsigned long); 1687#else 1688#define blkdev_compat_ptr_ioctl NULL 1689#endif 1690 1691static inline void blk_wake_io_task(struct task_struct *waiter) 1692{ 1693 /* 1694 * If we're polling, the task itself is doing the completions. For 1695 * that case, we don't need to signal a wakeup, it's enough to just 1696 * mark us as RUNNING. 1697 */ 1698 if (waiter == current) 1699 __set_current_state(TASK_RUNNING); 1700 else 1701 wake_up_process(waiter); 1702} 1703 1704unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op, 1705 unsigned long start_time); 1706void bdev_end_io_acct(struct block_device *bdev, enum req_op op, 1707 unsigned int sectors, unsigned long start_time); 1708 1709unsigned long bio_start_io_acct(struct bio *bio); 1710void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time, 1711 struct block_device *orig_bdev); 1712 1713/** 1714 * bio_end_io_acct - end I/O accounting for bio based drivers 1715 * @bio: bio to end account for 1716 * @start_time: start time returned by bio_start_io_acct() 1717 */ 1718static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time) 1719{ 1720 return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev); 1721} 1722 1723int bdev_validate_blocksize(struct block_device *bdev, int block_size); 1724int set_blocksize(struct file *file, int size); 1725 1726int lookup_bdev(const char *pathname, dev_t *dev); 1727 1728void blkdev_show(struct seq_file *seqf, off_t offset); 1729 1730#define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */ 1731#define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */ 1732#ifdef CONFIG_BLOCK 1733#define BLKDEV_MAJOR_MAX 512 1734#else 1735#define BLKDEV_MAJOR_MAX 0 1736#endif 1737 1738struct blk_holder_ops { 1739 void (*mark_dead)(struct block_device *bdev, bool surprise); 1740 1741 /* 1742 * Sync the file system mounted on the block device. 1743 */ 1744 void (*sync)(struct block_device *bdev); 1745 1746 /* 1747 * Freeze the file system mounted on the block device. 1748 */ 1749 int (*freeze)(struct block_device *bdev); 1750 1751 /* 1752 * Thaw the file system mounted on the block device. 1753 */ 1754 int (*thaw)(struct block_device *bdev); 1755}; 1756 1757/* 1758 * For filesystems using @fs_holder_ops, the @holder argument passed to 1759 * helpers used to open and claim block devices via 1760 * bd_prepare_to_claim() must point to a superblock. 1761 */ 1762extern const struct blk_holder_ops fs_holder_ops; 1763 1764/* 1765 * Return the correct open flags for blkdev_get_by_* for super block flags 1766 * as stored in sb->s_flags. 1767 */ 1768#define sb_open_mode(flags) \ 1769 (BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \ 1770 (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE)) 1771 1772struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, 1773 const struct blk_holder_ops *hops); 1774struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode, 1775 void *holder, const struct blk_holder_ops *hops); 1776int bd_prepare_to_claim(struct block_device *bdev, void *holder, 1777 const struct blk_holder_ops *hops); 1778void bd_abort_claiming(struct block_device *bdev, void *holder); 1779 1780struct block_device *I_BDEV(struct inode *inode); 1781struct block_device *file_bdev(struct file *bdev_file); 1782bool disk_live(struct gendisk *disk); 1783unsigned int block_size(struct block_device *bdev); 1784 1785#ifdef CONFIG_BLOCK 1786void invalidate_bdev(struct block_device *bdev); 1787int sync_blockdev(struct block_device *bdev); 1788int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend); 1789int sync_blockdev_nowait(struct block_device *bdev); 1790void sync_bdevs(bool wait); 1791void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask); 1792void printk_all_partitions(void); 1793int __init early_lookup_bdev(const char *pathname, dev_t *dev); 1794#else 1795static inline void invalidate_bdev(struct block_device *bdev) 1796{ 1797} 1798static inline int sync_blockdev(struct block_device *bdev) 1799{ 1800 return 0; 1801} 1802static inline int sync_blockdev_nowait(struct block_device *bdev) 1803{ 1804 return 0; 1805} 1806static inline void sync_bdevs(bool wait) 1807{ 1808} 1809static inline void bdev_statx(const struct path *path, struct kstat *stat, 1810 u32 request_mask) 1811{ 1812} 1813static inline void printk_all_partitions(void) 1814{ 1815} 1816static inline int early_lookup_bdev(const char *pathname, dev_t *dev) 1817{ 1818 return -EINVAL; 1819} 1820#endif /* CONFIG_BLOCK */ 1821 1822int bdev_freeze(struct block_device *bdev); 1823int bdev_thaw(struct block_device *bdev); 1824void bdev_fput(struct file *bdev_file); 1825 1826struct io_comp_batch { 1827 struct rq_list req_list; 1828 bool need_ts; 1829 void (*complete)(struct io_comp_batch *); 1830}; 1831 1832static inline bool blk_atomic_write_start_sect_aligned(sector_t sector, 1833 struct queue_limits *limits) 1834{ 1835 unsigned int alignment = max(limits->atomic_write_hw_unit_min, 1836 limits->atomic_write_hw_boundary); 1837 1838 return IS_ALIGNED(sector, alignment >> SECTOR_SHIFT); 1839} 1840 1841static inline bool bdev_can_atomic_write(struct block_device *bdev) 1842{ 1843 struct request_queue *bd_queue = bdev->bd_queue; 1844 struct queue_limits *limits = &bd_queue->limits; 1845 1846 if (!limits->atomic_write_unit_min) 1847 return false; 1848 1849 if (bdev_is_partition(bdev)) 1850 return blk_atomic_write_start_sect_aligned(bdev->bd_start_sect, 1851 limits); 1852 1853 return true; 1854} 1855 1856static inline unsigned int 1857bdev_atomic_write_unit_min_bytes(struct block_device *bdev) 1858{ 1859 if (!bdev_can_atomic_write(bdev)) 1860 return 0; 1861 return queue_atomic_write_unit_min_bytes(bdev_get_queue(bdev)); 1862} 1863 1864static inline unsigned int 1865bdev_atomic_write_unit_max_bytes(struct block_device *bdev) 1866{ 1867 if (!bdev_can_atomic_write(bdev)) 1868 return 0; 1869 return queue_atomic_write_unit_max_bytes(bdev_get_queue(bdev)); 1870} 1871 1872#define DEFINE_IO_COMP_BATCH(name) struct io_comp_batch name = { } 1873 1874#endif /* _LINUX_BLKDEV_H */