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