1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef BLK_INTERNAL_H 3 #define BLK_INTERNAL_H 4 5 #include <linux/bio-integrity.h> 6 #include <linux/blk-crypto.h> 7 #include <linux/part_stat.h> 8 #include <linux/lockdep.h> 9 #include <linux/memblock.h> /* for max_pfn/max_low_pfn */ 10 #include <linux/sched/sysctl.h> 11 #include <linux/timekeeping.h> 12 #include <xen/xen.h> 13 #include "blk-crypto-internal.h" 14 15 struct elv_change_ctx; 16 17 /* 18 * Default upper limit for the software max_sectors limit used for regular I/Os. 19 * This can be increased through sysfs. 20 * 21 * This should not be confused with the max_hw_sector limit that is entirely 22 * controlled by the block device driver, usually based on hardware limits. 23 */ 24 #define BLK_DEF_MAX_SECTORS_CAP (SZ_4M >> SECTOR_SHIFT) 25 26 #define BLK_DEV_MAX_SECTORS (LLONG_MAX >> 9) 27 #define BLK_MIN_SEGMENT_SIZE 4096 28 29 /* Max future timer expiry for timeouts */ 30 #define BLK_MAX_TIMEOUT (5 * HZ) 31 32 extern const struct kobj_type blk_queue_ktype; 33 extern struct dentry *blk_debugfs_root; 34 35 struct blk_flush_queue { 36 spinlock_t mq_flush_lock; 37 unsigned int flush_pending_idx:1; 38 unsigned int flush_running_idx:1; 39 blk_status_t rq_status; 40 unsigned long flush_pending_since; 41 struct list_head flush_queue[2]; 42 unsigned long flush_data_in_flight; 43 struct request *flush_rq; 44 struct rcu_head rcu_head; 45 }; 46 47 bool is_flush_rq(struct request *req); 48 49 struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size, 50 gfp_t flags); 51 void blk_free_flush_queue(struct blk_flush_queue *q); 52 53 const char *blk_status_to_str(blk_status_t status); 54 const char *blk_status_to_tag(blk_status_t status); 55 blk_status_t tag_to_blk_status(const char *tag); 56 enum req_op str_to_blk_op(const char *op); 57 58 bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic); 59 bool blk_queue_start_drain(struct request_queue *q); 60 bool __blk_freeze_queue_start(struct request_queue *q, 61 struct task_struct *owner); 62 int __bio_queue_enter(struct request_queue *q, struct bio *bio); 63 void submit_bio_noacct_nocheck(struct bio *bio, bool split); 64 int bio_submit_or_kill(struct bio *bio, unsigned int flags); 65 66 static inline bool blk_try_enter_queue(struct request_queue *q, bool pm) 67 { 68 rcu_read_lock(); 69 if (!percpu_ref_tryget_live_rcu(&q->q_usage_counter)) 70 goto fail; 71 72 /* 73 * The code that increments the pm_only counter must ensure that the 74 * counter is globally visible before the queue is unfrozen. 75 */ 76 if (blk_queue_pm_only(q) && 77 (!pm || queue_rpm_status(q) == RPM_SUSPENDED)) 78 goto fail_put; 79 80 rcu_read_unlock(); 81 return true; 82 83 fail_put: 84 blk_queue_exit(q); 85 fail: 86 rcu_read_unlock(); 87 return false; 88 } 89 90 static inline int bio_queue_enter(struct bio *bio) 91 { 92 struct request_queue *q = bdev_get_queue(bio->bi_bdev); 93 94 if (blk_try_enter_queue(q, false)) { 95 rwsem_acquire_read(&q->io_lockdep_map, 0, 0, _RET_IP_); 96 rwsem_release(&q->io_lockdep_map, _RET_IP_); 97 return 0; 98 } 99 return __bio_queue_enter(q, bio); 100 } 101 102 static inline void blk_wait_io(struct completion *done) 103 { 104 /* Prevent hang_check timer from firing at us during very long I/O */ 105 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2; 106 107 if (timeout) 108 while (!wait_for_completion_io_timeout(done, timeout)) 109 ; 110 else 111 wait_for_completion_io(done); 112 } 113 114 struct block_device *blkdev_get_no_open(dev_t dev, bool autoload); 115 void blkdev_put_no_open(struct block_device *bdev); 116 117 bool bvec_try_merge_hw_page(struct request_queue *q, struct bio_vec *bv, 118 struct page *page, unsigned len, unsigned offset); 119 120 static inline bool biovec_phys_mergeable(struct request_queue *q, 121 struct bio_vec *vec1, struct bio_vec *vec2) 122 { 123 unsigned long mask = queue_segment_boundary(q); 124 phys_addr_t addr1 = bvec_phys(vec1); 125 phys_addr_t addr2 = bvec_phys(vec2); 126 127 /* 128 * Merging adjacent physical pages may not work correctly under KMSAN 129 * if their metadata pages aren't adjacent. Just disable merging. 130 */ 131 if (IS_ENABLED(CONFIG_KMSAN)) 132 return false; 133 134 if (addr1 + vec1->bv_len != addr2) 135 return false; 136 if (!zone_device_pages_have_same_pgmap(vec1->bv_page, vec2->bv_page)) 137 return false; 138 if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page)) 139 return false; 140 if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask)) 141 return false; 142 return true; 143 } 144 145 /* 146 * Check if two pages from potentially different zone device pgmaps can 147 * coexist as separate bvec entries in the same bio. 148 * 149 * The block DMA iterator (blk_dma_map_iter_start) caches the P2PDMA mapping 150 * state from the first segment and applies it to all subsequent segments, so 151 * P2PDMA pages from different pgmaps must not be mixed in the same bio. 152 * 153 * Other zone device types (FS_DAX, GENERIC) use the same dma_map_phys() path 154 * as normal RAM. PRIVATE and COHERENT pages never appear in bios. 155 */ 156 static inline bool zone_device_pages_compatible(const struct page *a, 157 const struct page *b) 158 { 159 if (is_pci_p2pdma_page(a) || is_pci_p2pdma_page(b)) 160 return zone_device_pages_have_same_pgmap(a, b); 161 return true; 162 } 163 164 static inline bool __bvec_gap_to_prev(const struct queue_limits *lim, 165 struct bio_vec *bprv, unsigned int offset) 166 { 167 return (offset & lim->virt_boundary_mask) || 168 ((bprv->bv_offset + bprv->bv_len) & lim->virt_boundary_mask); 169 } 170 171 /* 172 * Check if adding a bio_vec after bprv with offset would create a gap in 173 * the SG list. Most drivers don't care about this, but some do. 174 */ 175 static inline bool bvec_gap_to_prev(const struct queue_limits *lim, 176 struct bio_vec *bprv, unsigned int offset) 177 { 178 if (!lim->virt_boundary_mask) 179 return false; 180 return __bvec_gap_to_prev(lim, bprv, offset); 181 } 182 183 static inline bool rq_mergeable(struct request *rq) 184 { 185 if (blk_rq_is_passthrough(rq)) 186 return false; 187 188 if (req_op(rq) == REQ_OP_FLUSH) 189 return false; 190 191 if (req_op(rq) == REQ_OP_WRITE_ZEROES) 192 return false; 193 194 if (req_op(rq) == REQ_OP_ZONE_APPEND) 195 return false; 196 197 if (rq->cmd_flags & REQ_NOMERGE_FLAGS) 198 return false; 199 if (rq->rq_flags & RQF_NOMERGE_FLAGS) 200 return false; 201 202 return true; 203 } 204 205 /* 206 * There are two different ways to handle DISCARD merges: 207 * 1) If max_discard_segments > 1, the driver treats every bio as a range and 208 * send the bios to controller together. The ranges don't need to be 209 * contiguous. 210 * 2) Otherwise, the request will be normal read/write requests. The ranges 211 * need to be contiguous. 212 */ 213 static inline bool blk_discard_mergable(struct request *req) 214 { 215 if (req_op(req) == REQ_OP_DISCARD && 216 queue_max_discard_segments(req->q) > 1) 217 return true; 218 return false; 219 } 220 221 static inline unsigned int blk_rq_get_max_segments(struct request *rq) 222 { 223 if (req_op(rq) == REQ_OP_DISCARD) 224 return queue_max_discard_segments(rq->q); 225 return queue_max_segments(rq->q); 226 } 227 228 static inline unsigned int blk_queue_get_max_sectors(struct request *rq) 229 { 230 struct request_queue *q = rq->q; 231 enum req_op op = req_op(rq); 232 233 if (unlikely(op == REQ_OP_DISCARD)) 234 return min(q->limits.max_discard_sectors, 235 UINT_MAX >> SECTOR_SHIFT); 236 237 if (unlikely(op == REQ_OP_SECURE_ERASE)) 238 return min(q->limits.max_secure_erase_sectors, 239 UINT_MAX >> SECTOR_SHIFT); 240 241 if (unlikely(op == REQ_OP_WRITE_ZEROES)) 242 return q->limits.max_write_zeroes_sectors; 243 244 if (rq->cmd_flags & REQ_ATOMIC) 245 return q->limits.atomic_write_max_sectors; 246 247 return q->limits.max_sectors; 248 } 249 250 #ifdef CONFIG_BLK_DEV_INTEGRITY 251 void blk_flush_integrity(void); 252 void bio_integrity_free(struct bio *bio); 253 254 /* 255 * Integrity payloads can either be owned by the submitter, in which case 256 * bio_uninit will free them, or owned and generated by the block layer, 257 * in which case we'll verify them here (for reads) and free them before 258 * the bio is handed back to the submitted. 259 */ 260 bool __bio_integrity_endio(struct bio *bio); 261 static inline bool bio_integrity_endio(struct bio *bio) 262 { 263 struct bio_integrity_payload *bip = bio_integrity(bio); 264 265 if (bip && (bip->bip_flags & BIP_BLOCK_INTEGRITY)) 266 return __bio_integrity_endio(bio); 267 return true; 268 } 269 270 bool blk_integrity_merge_rq(struct request_queue *, struct request *, 271 struct request *); 272 bool blk_integrity_merge_bio(struct request_queue *, struct request *, 273 struct bio *); 274 275 static inline bool integrity_req_gap_back_merge(struct request *req, 276 struct bio *next) 277 { 278 struct bio_integrity_payload *bip = bio_integrity(req->bio); 279 struct bio_integrity_payload *bip_next = bio_integrity(next); 280 281 return bvec_gap_to_prev(&req->q->limits, 282 &bip->bip_vec[bip->bip_vcnt - 1], 283 bip_next->bip_vec[0].bv_offset); 284 } 285 286 static inline bool integrity_req_gap_front_merge(struct request *req, 287 struct bio *bio) 288 { 289 struct bio_integrity_payload *bip = bio_integrity(bio); 290 struct bio_integrity_payload *bip_next = bio_integrity(req->bio); 291 292 return bvec_gap_to_prev(&req->q->limits, 293 &bip->bip_vec[bip->bip_vcnt - 1], 294 bip_next->bip_vec[0].bv_offset); 295 } 296 297 extern const struct attribute_group blk_integrity_attr_group; 298 #else /* CONFIG_BLK_DEV_INTEGRITY */ 299 static inline bool blk_integrity_merge_rq(struct request_queue *rq, 300 struct request *r1, struct request *r2) 301 { 302 return true; 303 } 304 static inline bool blk_integrity_merge_bio(struct request_queue *rq, 305 struct request *r, struct bio *b) 306 { 307 return true; 308 } 309 static inline bool integrity_req_gap_back_merge(struct request *req, 310 struct bio *next) 311 { 312 return false; 313 } 314 static inline bool integrity_req_gap_front_merge(struct request *req, 315 struct bio *bio) 316 { 317 return false; 318 } 319 320 static inline void blk_flush_integrity(void) 321 { 322 } 323 static inline bool bio_integrity_endio(struct bio *bio) 324 { 325 return true; 326 } 327 static inline void bio_integrity_free(struct bio *bio) 328 { 329 } 330 #endif /* CONFIG_BLK_DEV_INTEGRITY */ 331 332 unsigned long blk_rq_timeout(unsigned long timeout); 333 void blk_add_timer(struct request *req); 334 335 /* 336 * The timeout handler changes RQF_TIMED_OUT in a request which it does not 337 * own: whoever completes it changes rq_flags at the same time, with plain 338 * loads and stores. Never write back a flag that has changed under us. 339 */ 340 static inline void blk_rq_change_timed_out(struct request *rq, bool set) 341 { 342 req_flags_t old = READ_ONCE(rq->rq_flags), new; 343 344 do { 345 new = set ? old | RQF_TIMED_OUT : old & ~RQF_TIMED_OUT; 346 } while (!try_cmpxchg(&rq->rq_flags, &old, new)); 347 } 348 349 enum bio_merge_status { 350 BIO_MERGE_OK, 351 BIO_MERGE_NONE, 352 BIO_MERGE_FAILED, 353 }; 354 355 enum bio_merge_status bio_attempt_back_merge(struct request *req, 356 struct bio *bio, unsigned int nr_segs); 357 bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, 358 unsigned int nr_segs); 359 bool blk_bio_list_merge(struct request_queue *q, struct list_head *list, 360 struct bio *bio, unsigned int nr_segs); 361 362 /* 363 * Plug flush limits 364 */ 365 #define BLK_MAX_REQUEST_COUNT 32 366 #define BLK_PLUG_FLUSH_SIZE (128 * 1024) 367 368 /* 369 * Internal elevator interface 370 */ 371 #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED) 372 373 bool blk_insert_flush(struct request *rq); 374 375 void elv_update_nr_hw_queues(struct request_queue *q, 376 struct elv_change_ctx *ctx); 377 void elevator_set_default(struct request_queue *q); 378 void elevator_set_none(struct request_queue *q); 379 380 ssize_t part_size_show(struct device *dev, struct device_attribute *attr, 381 char *buf); 382 ssize_t part_stat_show(struct device *dev, struct device_attribute *attr, 383 char *buf); 384 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr, 385 char *buf); 386 ssize_t part_fail_show(struct device *dev, struct device_attribute *attr, 387 char *buf); 388 ssize_t part_fail_store(struct device *dev, struct device_attribute *attr, 389 const char *buf, size_t count); 390 ssize_t part_timeout_show(struct device *, struct device_attribute *, char *); 391 ssize_t part_timeout_store(struct device *, struct device_attribute *, 392 const char *, size_t); 393 394 struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim, 395 unsigned *nsegs); 396 struct bio *bio_split_write_zeroes(struct bio *bio, 397 const struct queue_limits *lim, unsigned *nsegs); 398 struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim, 399 unsigned *nr_segs); 400 struct bio *bio_split_zone_append(struct bio *bio, 401 const struct queue_limits *lim, unsigned *nr_segs); 402 403 /* 404 * All drivers must accept single-segments bios that are smaller than PAGE_SIZE. 405 * 406 * This is a quick and dirty check that relies on the fact that bi_io_vec[0] is 407 * always valid if a bio has data. The check might lead to occasional false 408 * positives when bios are cloned, but compared to the performance impact of 409 * cloned bios themselves the loop below doesn't matter anyway. 410 */ 411 static inline bool bio_may_need_split(struct bio *bio, 412 const struct queue_limits *lim) 413 { 414 const struct bio_vec *bv; 415 416 if (lim->chunk_sectors) 417 return true; 418 419 if (!bio->bi_io_vec) 420 return true; 421 if (op_is_dmabuf(bio->bi_opf)) 422 return true; 423 424 bv = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 425 if (bio->bi_iter.bi_size > bv->bv_len - bio->bi_iter.bi_offset) 426 return true; 427 if ((bv->bv_offset | bv->bv_len) & lim->dma_alignment) 428 return true; 429 return bv->bv_len + bv->bv_offset > lim->max_fast_segment_size; 430 } 431 432 /** 433 * __bio_split_to_limits - split a bio to fit the queue limits 434 * @bio: bio to be split 435 * @lim: queue limits to split based on 436 * @nr_segs: returns the number of segments in the returned bio 437 * 438 * Check if @bio needs splitting based on the queue limits, and if so split off 439 * a bio fitting the limits from the beginning of @bio and return it. @bio is 440 * shortened to the remainder and re-submitted. 441 * 442 * The split bio is allocated from @q->bio_split, which is provided by the 443 * block layer. 444 */ 445 static inline struct bio *__bio_split_to_limits(struct bio *bio, 446 const struct queue_limits *lim, unsigned int *nr_segs) 447 { 448 switch (bio_op(bio)) { 449 case REQ_OP_READ: 450 case REQ_OP_WRITE: 451 if (bio_may_need_split(bio, lim)) 452 return bio_split_rw(bio, lim, nr_segs); 453 *nr_segs = 1; 454 return bio; 455 case REQ_OP_ZONE_APPEND: 456 return bio_split_zone_append(bio, lim, nr_segs); 457 case REQ_OP_DISCARD: 458 case REQ_OP_SECURE_ERASE: 459 return bio_split_discard(bio, lim, nr_segs); 460 case REQ_OP_WRITE_ZEROES: 461 return bio_split_write_zeroes(bio, lim, nr_segs); 462 default: 463 /* other operations can't be split */ 464 *nr_segs = 0; 465 return bio; 466 } 467 } 468 469 /** 470 * get_max_segment_size() - maximum number of bytes to add as a single segment 471 * @lim: Request queue limits. 472 * @paddr: address of the range to add 473 * @len: maximum length available to add at @paddr 474 * 475 * Returns the maximum number of bytes of the range starting at @paddr that can 476 * be added to a single segment. 477 */ 478 static inline unsigned get_max_segment_size(const struct queue_limits *lim, 479 phys_addr_t paddr, unsigned int len) 480 { 481 /* 482 * Prevent an overflow if mask = ULONG_MAX and offset = 0 by adding 1 483 * after having calculated the minimum. 484 */ 485 return min_t(unsigned long, len, 486 min(lim->seg_boundary_mask - (lim->seg_boundary_mask & paddr), 487 (unsigned long)lim->max_segment_size - 1) + 1); 488 } 489 490 int ll_back_merge_fn(struct request *req, struct bio *bio, 491 unsigned int nr_segs); 492 bool blk_attempt_req_merge(struct request_queue *q, struct request *rq, 493 struct request *next); 494 unsigned int blk_recalc_rq_segments(struct request *rq); 495 bool blk_rq_merge_ok(struct request *rq, struct bio *bio); 496 enum elv_merge blk_try_merge(struct request *rq, struct bio *bio); 497 498 int blk_set_default_limits(struct queue_limits *lim); 499 void blk_apply_bdi_limits(struct backing_dev_info *bdi, 500 struct queue_limits *lim); 501 int blk_dev_init(void); 502 503 void update_io_ticks(struct block_device *part, unsigned long now, bool end); 504 505 static inline void req_set_nomerge(struct request_queue *q, struct request *req) 506 { 507 req->cmd_flags |= REQ_NOMERGE; 508 if (req == q->last_merge) 509 q->last_merge = NULL; 510 } 511 512 static inline void bdev_inc_in_flight(struct block_device *bdev, 513 enum req_op op) 514 { 515 bool rw = op_is_write(op); 516 517 part_stat_local_inc(bdev, in_flight[rw]); 518 if (bdev_is_partition(bdev)) 519 part_stat_local_inc(bdev_whole(bdev), in_flight[rw]); 520 } 521 522 static inline void bdev_dec_in_flight(struct block_device *bdev, 523 enum req_op op) 524 { 525 bool rw = op_is_write(op); 526 527 part_stat_local_dec(bdev, in_flight[rw]); 528 if (bdev_is_partition(bdev)) 529 part_stat_local_dec(bdev_whole(bdev), in_flight[rw]); 530 } 531 532 /* 533 * Internal io_context interface 534 */ 535 struct io_cq *ioc_find_get_icq(struct request_queue *q); 536 struct io_cq *ioc_lookup_icq(struct request_queue *q); 537 #ifdef CONFIG_BLK_ICQ 538 void ioc_clear_queue(struct request_queue *q); 539 #else 540 static inline void ioc_clear_queue(struct request_queue *q) 541 { 542 } 543 #endif /* CONFIG_BLK_ICQ */ 544 545 #ifdef CONFIG_BLK_DEV_ZONED 546 void disk_init_zone_resources(struct gendisk *disk); 547 void disk_release_zone_resources(struct gendisk *disk); 548 static inline bool bio_zone_write_plugging(struct bio *bio) 549 { 550 return bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING); 551 } 552 static inline bool blk_req_bio_is_zone_append(struct request *rq, 553 struct bio *bio) 554 { 555 return req_op(rq) == REQ_OP_ZONE_APPEND || 556 bio_flagged(bio, BIO_EMULATES_ZONE_APPEND); 557 } 558 void blk_zone_write_plug_bio_merged(struct bio *bio); 559 void blk_zone_write_plug_init_request(struct request *rq); 560 void blk_zone_append_update_request_bio(struct request *rq, struct bio *bio); 561 void blk_zone_mgmt_bio_endio(struct bio *bio); 562 void blk_zone_write_plug_bio_endio(struct bio *bio); 563 static inline void blk_zone_bio_endio(struct bio *bio) 564 { 565 /* 566 * Zone management BIOs may impact zone write plugs (e.g. a zone reset 567 * changes a zone write plug zone write pointer offset), but these 568 * operation do not go through zone write plugging as they may operate 569 * on zones that do not have a zone write 570 * plug. blk_zone_mgmt_bio_endio() handles the potential changes to zone 571 * write plugs that are present. 572 */ 573 if (op_is_zone_mgmt(bio_op(bio))) { 574 blk_zone_mgmt_bio_endio(bio); 575 return; 576 } 577 578 /* 579 * For write BIOs to zoned devices, signal the completion of the BIO so 580 * that the next write BIO can be submitted by zone write plugging. 581 */ 582 if (bio_zone_write_plugging(bio)) 583 blk_zone_write_plug_bio_endio(bio); 584 } 585 586 void blk_zone_write_plug_finish_request(struct request *rq); 587 static inline void blk_zone_finish_request(struct request *rq) 588 { 589 if (rq->rq_flags & RQF_ZONE_WRITE_PLUGGING) 590 blk_zone_write_plug_finish_request(rq); 591 } 592 int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd, 593 unsigned long arg); 594 int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode, 595 unsigned int cmd, unsigned long arg); 596 bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector); 597 #else /* CONFIG_BLK_DEV_ZONED */ 598 static inline void disk_init_zone_resources(struct gendisk *disk) 599 { 600 } 601 static inline void disk_release_zone_resources(struct gendisk *disk) 602 { 603 } 604 static inline bool bio_zone_write_plugging(struct bio *bio) 605 { 606 return false; 607 } 608 static inline bool blk_req_bio_is_zone_append(struct request *req, 609 struct bio *bio) 610 { 611 return false; 612 } 613 static inline void blk_zone_write_plug_bio_merged(struct bio *bio) 614 { 615 } 616 static inline void blk_zone_write_plug_init_request(struct request *rq) 617 { 618 } 619 static inline void blk_zone_append_update_request_bio(struct request *rq, 620 struct bio *bio) 621 { 622 } 623 static inline void blk_zone_bio_endio(struct bio *bio) 624 { 625 } 626 static inline void blk_zone_finish_request(struct request *rq) 627 { 628 } 629 static inline int blkdev_report_zones_ioctl(struct block_device *bdev, 630 unsigned int cmd, unsigned long arg) 631 { 632 return -ENOTTY; 633 } 634 static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev, 635 blk_mode_t mode, unsigned int cmd, unsigned long arg) 636 { 637 return -ENOTTY; 638 } 639 static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev, 640 sector_t sector) 641 { 642 return false; 643 } 644 #endif /* CONFIG_BLK_DEV_ZONED */ 645 646 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno); 647 void bdev_add(struct block_device *bdev, dev_t dev); 648 void bdev_unhash(struct block_device *bdev); 649 void bdev_drop(struct block_device *bdev); 650 651 int blk_alloc_ext_minor(void); 652 void blk_free_ext_minor(unsigned int minor); 653 #define ADDPART_FLAG_NONE 0 654 #define ADDPART_FLAG_RAID 1 655 #define ADDPART_FLAG_WHOLEDISK 2 656 #define ADDPART_FLAG_READONLY 4 657 int bdev_add_partition(struct gendisk *disk, int partno, sector_t start, 658 sector_t length); 659 int bdev_del_partition(struct gendisk *disk, int partno); 660 int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start, 661 sector_t length); 662 void drop_partition(struct block_device *part); 663 664 void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors); 665 666 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id, 667 struct lock_class_key *lkclass); 668 struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id); 669 670 int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode); 671 672 int disk_alloc_events(struct gendisk *disk); 673 void disk_add_events(struct gendisk *disk); 674 void disk_del_events(struct gendisk *disk); 675 void disk_release_events(struct gendisk *disk); 676 void disk_block_events(struct gendisk *disk); 677 void disk_unblock_events(struct gendisk *disk); 678 void disk_flush_events(struct gendisk *disk, unsigned int mask); 679 extern struct device_attribute dev_attr_events; 680 extern struct device_attribute dev_attr_events_async; 681 extern struct device_attribute dev_attr_events_poll_msecs; 682 683 extern struct attribute_group blk_trace_attr_group; 684 685 blk_mode_t file_to_blk_mode(struct file *file); 686 int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, 687 loff_t lstart, loff_t lend); 688 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg); 689 int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags); 690 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg); 691 692 extern const struct address_space_operations def_blk_aops; 693 694 int disk_register_independent_access_ranges(struct gendisk *disk); 695 void disk_unregister_independent_access_ranges(struct gendisk *disk); 696 697 int should_fail_bio(struct bio *bio); 698 #ifdef CONFIG_FAIL_MAKE_REQUEST 699 bool should_fail_request(struct block_device *part, unsigned int bytes); 700 #else /* CONFIG_FAIL_MAKE_REQUEST */ 701 static inline bool should_fail_request(struct block_device *part, 702 unsigned int bytes) 703 { 704 return false; 705 } 706 #endif /* CONFIG_FAIL_MAKE_REQUEST */ 707 708 /* 709 * Optimized request reference counting. Ideally we'd make timeouts be more 710 * clever, as that's the only reason we need references at all... But until 711 * this happens, this is faster than using refcount_t. Also see: 712 * 713 * abc54d634334 ("io_uring: switch to atomic_t for io_kiocb reference count") 714 */ 715 #define req_ref_zero_or_close_to_overflow(req) \ 716 ((unsigned int) atomic_read(&(req->ref)) + 127u <= 127u) 717 718 static inline bool req_ref_inc_not_zero(struct request *req) 719 { 720 return atomic_inc_not_zero(&req->ref); 721 } 722 723 static inline bool req_ref_put_and_test(struct request *req) 724 { 725 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); 726 return atomic_dec_and_test(&req->ref); 727 } 728 729 static inline void req_ref_set(struct request *req, int value) 730 { 731 atomic_set(&req->ref, value); 732 } 733 734 static inline int req_ref_read(struct request *req) 735 { 736 return atomic_read(&req->ref); 737 } 738 739 static inline u64 blk_time_get_ns(void) 740 { 741 struct blk_plug *plug = current->plug; 742 u64 now; 743 744 if (!plug || !in_task()) 745 return ktime_get_ns(); 746 747 /* 748 * 0 could very well be a valid time, but rather than flag "this is 749 * a valid timestamp" separately, just accept that we'll do an extra 750 * ktime_get_ns() if we just happen to get 0 as the current time. 751 * 752 * cur_ktime can be zeroed by pre-emption the moment PF_BLOCK_TS is set. 753 */ 754 now = READ_ONCE(plug->cur_ktime); 755 if (!now) { 756 now = ktime_get_ns(); 757 WRITE_ONCE(plug->cur_ktime, now); 758 /* Ensure PF_BLOCK_TS is set after cur_ktime. */ 759 barrier(); 760 current->flags |= PF_BLOCK_TS; 761 } 762 return now; 763 } 764 765 static inline ktime_t blk_time_get(void) 766 { 767 return ns_to_ktime(blk_time_get_ns()); 768 } 769 770 void bdev_release(struct file *bdev_file); 771 int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder, 772 const struct blk_holder_ops *hops, struct file *bdev_file); 773 int bdev_permission(dev_t dev, blk_mode_t mode, void *holder); 774 775 void bio_integrity_generate(struct bio *bio); 776 blk_status_t bio_integrity_verify(struct bio *bio, 777 struct bvec_iter *saved_iter); 778 779 void blk_integrity_prepare(struct request *rq); 780 void blk_integrity_complete(struct request *rq, unsigned int nr_bytes); 781 782 #ifdef CONFIG_LOCKDEP 783 static inline void blk_freeze_acquire_lock(struct request_queue *q) 784 { 785 if (!q->mq_freeze_disk_dead) 786 rwsem_acquire(&q->io_lockdep_map, 0, 1, _RET_IP_); 787 if (!q->mq_freeze_queue_dying) 788 rwsem_acquire(&q->q_lockdep_map, 0, 1, _RET_IP_); 789 } 790 791 static inline void blk_unfreeze_release_lock(struct request_queue *q) 792 { 793 if (!q->mq_freeze_queue_dying) 794 rwsem_release(&q->q_lockdep_map, _RET_IP_); 795 if (!q->mq_freeze_disk_dead) 796 rwsem_release(&q->io_lockdep_map, _RET_IP_); 797 } 798 #else 799 static inline void blk_freeze_acquire_lock(struct request_queue *q) 800 { 801 } 802 static inline void blk_unfreeze_release_lock(struct request_queue *q) 803 { 804 } 805 #endif 806 807 /* 808 * debugfs directory and file creation can trigger fs reclaim, which can enter 809 * back into the block layer request_queue. This can cause deadlock if the 810 * queue is frozen. Use NOIO context together with debugfs_mutex to prevent fs 811 * reclaim from triggering block I/O. 812 */ 813 static inline void blk_debugfs_lock_nomemsave(struct request_queue *q) 814 __acquires(&q->debugfs_mutex) 815 { 816 mutex_lock(&q->debugfs_mutex); 817 } 818 819 static inline void blk_debugfs_unlock_nomemrestore(struct request_queue *q) 820 __releases(&q->debugfs_mutex) 821 { 822 mutex_unlock(&q->debugfs_mutex); 823 } 824 825 static inline unsigned int __must_check blk_debugfs_lock(struct request_queue *q) 826 __acquires(&q->debugfs_mutex) 827 { 828 unsigned int memflags = memalloc_noio_save(); 829 830 blk_debugfs_lock_nomemsave(q); 831 return memflags; 832 } 833 834 static inline void blk_debugfs_unlock(struct request_queue *q, 835 unsigned int memflags) 836 __releases(&q->debugfs_mutex) 837 { 838 blk_debugfs_unlock_nomemrestore(q); 839 memalloc_noio_restore(memflags); 840 } 841 842 #endif /* BLK_INTERNAL_H */ 843