xref: /linux/block/blk.h (revision 9b0c3673c88588d613d8f09f5931b2b466c6a83d)
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 enum bio_merge_status {
336 	BIO_MERGE_OK,
337 	BIO_MERGE_NONE,
338 	BIO_MERGE_FAILED,
339 };
340 
341 enum bio_merge_status bio_attempt_back_merge(struct request *req,
342 		struct bio *bio, unsigned int nr_segs);
343 bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
344 		unsigned int nr_segs);
345 bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
346 			struct bio *bio, unsigned int nr_segs);
347 
348 /*
349  * Plug flush limits
350  */
351 #define BLK_MAX_REQUEST_COUNT	32
352 #define BLK_PLUG_FLUSH_SIZE	(128 * 1024)
353 
354 /*
355  * Internal elevator interface
356  */
357 #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
358 
359 bool blk_insert_flush(struct request *rq);
360 
361 void elv_update_nr_hw_queues(struct request_queue *q,
362 		struct elv_change_ctx *ctx);
363 void elevator_set_default(struct request_queue *q);
364 void elevator_set_none(struct request_queue *q);
365 
366 ssize_t part_size_show(struct device *dev, struct device_attribute *attr,
367 		char *buf);
368 ssize_t part_stat_show(struct device *dev, struct device_attribute *attr,
369 		char *buf);
370 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
371 		char *buf);
372 ssize_t part_fail_show(struct device *dev, struct device_attribute *attr,
373 		char *buf);
374 ssize_t part_fail_store(struct device *dev, struct device_attribute *attr,
375 		const char *buf, size_t count);
376 ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
377 ssize_t part_timeout_store(struct device *, struct device_attribute *,
378 				const char *, size_t);
379 
380 struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim,
381 		unsigned *nsegs);
382 struct bio *bio_split_write_zeroes(struct bio *bio,
383 		const struct queue_limits *lim, unsigned *nsegs);
384 struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
385 		unsigned *nr_segs);
386 struct bio *bio_split_zone_append(struct bio *bio,
387 		const struct queue_limits *lim, unsigned *nr_segs);
388 
389 /*
390  * All drivers must accept single-segments bios that are smaller than PAGE_SIZE.
391  *
392  * This is a quick and dirty check that relies on the fact that bi_io_vec[0] is
393  * always valid if a bio has data.  The check might lead to occasional false
394  * positives when bios are cloned, but compared to the performance impact of
395  * cloned bios themselves the loop below doesn't matter anyway.
396  */
397 static inline bool bio_may_need_split(struct bio *bio,
398 		const struct queue_limits *lim)
399 {
400 	const struct bio_vec *bv;
401 
402 	if (lim->chunk_sectors)
403 		return true;
404 
405 	if (!bio->bi_io_vec)
406 		return true;
407 
408 	bv = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
409 	if (bio->bi_iter.bi_size > bv->bv_len - bio->bi_iter.bi_bvec_done)
410 		return true;
411 	if ((bv->bv_offset | bv->bv_len) & lim->dma_alignment)
412 		return true;
413 	return bv->bv_len + bv->bv_offset > lim->max_fast_segment_size;
414 }
415 
416 /**
417  * __bio_split_to_limits - split a bio to fit the queue limits
418  * @bio:     bio to be split
419  * @lim:     queue limits to split based on
420  * @nr_segs: returns the number of segments in the returned bio
421  *
422  * Check if @bio needs splitting based on the queue limits, and if so split off
423  * a bio fitting the limits from the beginning of @bio and return it.  @bio is
424  * shortened to the remainder and re-submitted.
425  *
426  * The split bio is allocated from @q->bio_split, which is provided by the
427  * block layer.
428  */
429 static inline struct bio *__bio_split_to_limits(struct bio *bio,
430 		const struct queue_limits *lim, unsigned int *nr_segs)
431 {
432 	switch (bio_op(bio)) {
433 	case REQ_OP_READ:
434 	case REQ_OP_WRITE:
435 		if (bio_may_need_split(bio, lim))
436 			return bio_split_rw(bio, lim, nr_segs);
437 		*nr_segs = 1;
438 		return bio;
439 	case REQ_OP_ZONE_APPEND:
440 		return bio_split_zone_append(bio, lim, nr_segs);
441 	case REQ_OP_DISCARD:
442 	case REQ_OP_SECURE_ERASE:
443 		return bio_split_discard(bio, lim, nr_segs);
444 	case REQ_OP_WRITE_ZEROES:
445 		return bio_split_write_zeroes(bio, lim, nr_segs);
446 	default:
447 		/* other operations can't be split */
448 		*nr_segs = 0;
449 		return bio;
450 	}
451 }
452 
453 /**
454  * get_max_segment_size() - maximum number of bytes to add as a single segment
455  * @lim: Request queue limits.
456  * @paddr: address of the range to add
457  * @len: maximum length available to add at @paddr
458  *
459  * Returns the maximum number of bytes of the range starting at @paddr that can
460  * be added to a single segment.
461  */
462 static inline unsigned get_max_segment_size(const struct queue_limits *lim,
463 		phys_addr_t paddr, unsigned int len)
464 {
465 	/*
466 	 * Prevent an overflow if mask = ULONG_MAX and offset = 0 by adding 1
467 	 * after having calculated the minimum.
468 	 */
469 	return min_t(unsigned long, len,
470 		min(lim->seg_boundary_mask - (lim->seg_boundary_mask & paddr),
471 		    (unsigned long)lim->max_segment_size - 1) + 1);
472 }
473 
474 int ll_back_merge_fn(struct request *req, struct bio *bio,
475 		unsigned int nr_segs);
476 bool blk_attempt_req_merge(struct request_queue *q, struct request *rq,
477 				struct request *next);
478 unsigned int blk_recalc_rq_segments(struct request *rq);
479 bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
480 enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
481 
482 int blk_set_default_limits(struct queue_limits *lim);
483 void blk_apply_bdi_limits(struct backing_dev_info *bdi,
484 		struct queue_limits *lim);
485 int blk_dev_init(void);
486 
487 void update_io_ticks(struct block_device *part, unsigned long now, bool end);
488 
489 static inline void req_set_nomerge(struct request_queue *q, struct request *req)
490 {
491 	req->cmd_flags |= REQ_NOMERGE;
492 	if (req == q->last_merge)
493 		q->last_merge = NULL;
494 }
495 
496 static inline void bdev_inc_in_flight(struct block_device *bdev,
497 				      enum req_op op)
498 {
499 	bool rw = op_is_write(op);
500 
501 	part_stat_local_inc(bdev, in_flight[rw]);
502 	if (bdev_is_partition(bdev))
503 		part_stat_local_inc(bdev_whole(bdev), in_flight[rw]);
504 }
505 
506 static inline void bdev_dec_in_flight(struct block_device *bdev,
507 				      enum req_op op)
508 {
509 	bool rw = op_is_write(op);
510 
511 	part_stat_local_dec(bdev, in_flight[rw]);
512 	if (bdev_is_partition(bdev))
513 		part_stat_local_dec(bdev_whole(bdev), in_flight[rw]);
514 }
515 
516 /*
517  * Internal io_context interface
518  */
519 struct io_cq *ioc_find_get_icq(struct request_queue *q);
520 struct io_cq *ioc_lookup_icq(struct request_queue *q);
521 #ifdef CONFIG_BLK_ICQ
522 void ioc_clear_queue(struct request_queue *q);
523 #else
524 static inline void ioc_clear_queue(struct request_queue *q)
525 {
526 }
527 #endif /* CONFIG_BLK_ICQ */
528 
529 #ifdef CONFIG_BLK_DEV_ZONED
530 void disk_init_zone_resources(struct gendisk *disk);
531 void disk_free_zone_resources(struct gendisk *disk);
532 static inline bool bio_zone_write_plugging(struct bio *bio)
533 {
534 	return bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING);
535 }
536 static inline bool blk_req_bio_is_zone_append(struct request *rq,
537 					      struct bio *bio)
538 {
539 	return req_op(rq) == REQ_OP_ZONE_APPEND ||
540 	       bio_flagged(bio, BIO_EMULATES_ZONE_APPEND);
541 }
542 void blk_zone_write_plug_bio_merged(struct bio *bio);
543 void blk_zone_write_plug_init_request(struct request *rq);
544 void blk_zone_append_update_request_bio(struct request *rq, struct bio *bio);
545 void blk_zone_mgmt_bio_endio(struct bio *bio);
546 void blk_zone_write_plug_bio_endio(struct bio *bio);
547 static inline void blk_zone_bio_endio(struct bio *bio)
548 {
549 	/*
550 	 * Zone management BIOs may impact zone write plugs (e.g. a zone reset
551 	 * changes a zone write plug zone write pointer offset), but these
552 	 * operation do not go through zone write plugging as they may operate
553 	 * on zones that do not have a zone write
554 	 * plug. blk_zone_mgmt_bio_endio() handles the potential changes to zone
555 	 * write plugs that are present.
556 	 */
557 	if (op_is_zone_mgmt(bio_op(bio))) {
558 		blk_zone_mgmt_bio_endio(bio);
559 		return;
560 	}
561 
562 	/*
563 	 * For write BIOs to zoned devices, signal the completion of the BIO so
564 	 * that the next write BIO can be submitted by zone write plugging.
565 	 */
566 	if (bio_zone_write_plugging(bio))
567 		blk_zone_write_plug_bio_endio(bio);
568 }
569 
570 void blk_zone_write_plug_finish_request(struct request *rq);
571 static inline void blk_zone_finish_request(struct request *rq)
572 {
573 	if (rq->rq_flags & RQF_ZONE_WRITE_PLUGGING)
574 		blk_zone_write_plug_finish_request(rq);
575 }
576 int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
577 		unsigned long arg);
578 int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
579 		unsigned int cmd, unsigned long arg);
580 #else /* CONFIG_BLK_DEV_ZONED */
581 static inline void disk_init_zone_resources(struct gendisk *disk)
582 {
583 }
584 static inline void disk_free_zone_resources(struct gendisk *disk)
585 {
586 }
587 static inline bool bio_zone_write_plugging(struct bio *bio)
588 {
589 	return false;
590 }
591 static inline bool blk_req_bio_is_zone_append(struct request *req,
592 					      struct bio *bio)
593 {
594 	return false;
595 }
596 static inline void blk_zone_write_plug_bio_merged(struct bio *bio)
597 {
598 }
599 static inline void blk_zone_write_plug_init_request(struct request *rq)
600 {
601 }
602 static inline void blk_zone_append_update_request_bio(struct request *rq,
603 						      struct bio *bio)
604 {
605 }
606 static inline void blk_zone_bio_endio(struct bio *bio)
607 {
608 }
609 static inline void blk_zone_finish_request(struct request *rq)
610 {
611 }
612 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
613 		unsigned int cmd, unsigned long arg)
614 {
615 	return -ENOTTY;
616 }
617 static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
618 		blk_mode_t mode, unsigned int cmd, unsigned long arg)
619 {
620 	return -ENOTTY;
621 }
622 #endif /* CONFIG_BLK_DEV_ZONED */
623 
624 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
625 void bdev_add(struct block_device *bdev, dev_t dev);
626 void bdev_unhash(struct block_device *bdev);
627 void bdev_drop(struct block_device *bdev);
628 
629 int blk_alloc_ext_minor(void);
630 void blk_free_ext_minor(unsigned int minor);
631 #define ADDPART_FLAG_NONE	0
632 #define ADDPART_FLAG_RAID	1
633 #define ADDPART_FLAG_WHOLEDISK	2
634 #define ADDPART_FLAG_READONLY	4
635 int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
636 		sector_t length);
637 int bdev_del_partition(struct gendisk *disk, int partno);
638 int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
639 		sector_t length);
640 void drop_partition(struct block_device *part);
641 
642 void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors);
643 
644 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,
645 		struct lock_class_key *lkclass);
646 struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id);
647 
648 int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode);
649 
650 int disk_alloc_events(struct gendisk *disk);
651 void disk_add_events(struct gendisk *disk);
652 void disk_del_events(struct gendisk *disk);
653 void disk_release_events(struct gendisk *disk);
654 void disk_block_events(struct gendisk *disk);
655 void disk_unblock_events(struct gendisk *disk);
656 void disk_flush_events(struct gendisk *disk, unsigned int mask);
657 extern struct device_attribute dev_attr_events;
658 extern struct device_attribute dev_attr_events_async;
659 extern struct device_attribute dev_attr_events_poll_msecs;
660 
661 extern struct attribute_group blk_trace_attr_group;
662 
663 blk_mode_t file_to_blk_mode(struct file *file);
664 int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
665 		loff_t lstart, loff_t lend);
666 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
667 int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags);
668 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
669 
670 extern const struct address_space_operations def_blk_aops;
671 
672 int disk_register_independent_access_ranges(struct gendisk *disk);
673 void disk_unregister_independent_access_ranges(struct gendisk *disk);
674 
675 int should_fail_bio(struct bio *bio);
676 #ifdef CONFIG_FAIL_MAKE_REQUEST
677 bool should_fail_request(struct block_device *part, unsigned int bytes);
678 #else /* CONFIG_FAIL_MAKE_REQUEST */
679 static inline bool should_fail_request(struct block_device *part,
680 					unsigned int bytes)
681 {
682 	return false;
683 }
684 #endif /* CONFIG_FAIL_MAKE_REQUEST */
685 
686 /*
687  * Optimized request reference counting. Ideally we'd make timeouts be more
688  * clever, as that's the only reason we need references at all... But until
689  * this happens, this is faster than using refcount_t. Also see:
690  *
691  * abc54d634334 ("io_uring: switch to atomic_t for io_kiocb reference count")
692  */
693 #define req_ref_zero_or_close_to_overflow(req)	\
694 	((unsigned int) atomic_read(&(req->ref)) + 127u <= 127u)
695 
696 static inline bool req_ref_inc_not_zero(struct request *req)
697 {
698 	return atomic_inc_not_zero(&req->ref);
699 }
700 
701 static inline bool req_ref_put_and_test(struct request *req)
702 {
703 	WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
704 	return atomic_dec_and_test(&req->ref);
705 }
706 
707 static inline void req_ref_set(struct request *req, int value)
708 {
709 	atomic_set(&req->ref, value);
710 }
711 
712 static inline int req_ref_read(struct request *req)
713 {
714 	return atomic_read(&req->ref);
715 }
716 
717 static inline u64 blk_time_get_ns(void)
718 {
719 	struct blk_plug *plug = current->plug;
720 
721 	if (!plug || !in_task())
722 		return ktime_get_ns();
723 
724 	/*
725 	 * 0 could very well be a valid time, but rather than flag "this is
726 	 * a valid timestamp" separately, just accept that we'll do an extra
727 	 * ktime_get_ns() if we just happen to get 0 as the current time.
728 	 */
729 	if (!plug->cur_ktime) {
730 		plug->cur_ktime = ktime_get_ns();
731 		current->flags |= PF_BLOCK_TS;
732 	}
733 	return plug->cur_ktime;
734 }
735 
736 static inline ktime_t blk_time_get(void)
737 {
738 	return ns_to_ktime(blk_time_get_ns());
739 }
740 
741 void bdev_release(struct file *bdev_file);
742 int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,
743 	      const struct blk_holder_ops *hops, struct file *bdev_file);
744 int bdev_permission(dev_t dev, blk_mode_t mode, void *holder);
745 
746 void bio_integrity_generate(struct bio *bio);
747 blk_status_t bio_integrity_verify(struct bio *bio,
748 		struct bvec_iter *saved_iter);
749 
750 void blk_integrity_prepare(struct request *rq);
751 void blk_integrity_complete(struct request *rq, unsigned int nr_bytes);
752 
753 #ifdef CONFIG_LOCKDEP
754 static inline void blk_freeze_acquire_lock(struct request_queue *q)
755 {
756 	if (!q->mq_freeze_disk_dead)
757 		rwsem_acquire(&q->io_lockdep_map, 0, 1, _RET_IP_);
758 	if (!q->mq_freeze_queue_dying)
759 		rwsem_acquire(&q->q_lockdep_map, 0, 1, _RET_IP_);
760 }
761 
762 static inline void blk_unfreeze_release_lock(struct request_queue *q)
763 {
764 	if (!q->mq_freeze_queue_dying)
765 		rwsem_release(&q->q_lockdep_map, _RET_IP_);
766 	if (!q->mq_freeze_disk_dead)
767 		rwsem_release(&q->io_lockdep_map, _RET_IP_);
768 }
769 #else
770 static inline void blk_freeze_acquire_lock(struct request_queue *q)
771 {
772 }
773 static inline void blk_unfreeze_release_lock(struct request_queue *q)
774 {
775 }
776 #endif
777 
778 /*
779  * debugfs directory and file creation can trigger fs reclaim, which can enter
780  * back into the block layer request_queue. This can cause deadlock if the
781  * queue is frozen. Use NOIO context together with debugfs_mutex to prevent fs
782  * reclaim from triggering block I/O.
783  */
784 static inline void blk_debugfs_lock_nomemsave(struct request_queue *q)
785 	__acquires(&q->debugfs_mutex)
786 {
787 	mutex_lock(&q->debugfs_mutex);
788 }
789 
790 static inline void blk_debugfs_unlock_nomemrestore(struct request_queue *q)
791 	__releases(&q->debugfs_mutex)
792 {
793 	mutex_unlock(&q->debugfs_mutex);
794 }
795 
796 static inline unsigned int __must_check blk_debugfs_lock(struct request_queue *q)
797 	__acquires(&q->debugfs_mutex)
798 {
799 	unsigned int memflags = memalloc_noio_save();
800 
801 	blk_debugfs_lock_nomemsave(q);
802 	return memflags;
803 }
804 
805 static inline void blk_debugfs_unlock(struct request_queue *q,
806 				      unsigned int memflags)
807 	__releases(&q->debugfs_mutex)
808 {
809 	blk_debugfs_unlock_nomemrestore(q);
810 	memalloc_noio_restore(memflags);
811 }
812 
813 #endif /* BLK_INTERNAL_H */
814