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