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