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