]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
8324aa91 JA |
2 | #ifndef BLK_INTERNAL_H |
3 | #define BLK_INTERNAL_H | |
4 | ||
da042a36 | 5 | #include <linux/bio-integrity.h> |
a892c8d5 | 6 | #include <linux/blk-crypto.h> |
f1be1788 | 7 | #include <linux/lockdep.h> |
9bb33f24 | 8 | #include <linux/memblock.h> /* for max_pfn/max_low_pfn */ |
0eb4db47 | 9 | #include <linux/sched/sysctl.h> |
08420cf7 | 10 | #include <linux/timekeeping.h> |
c39ae60d | 11 | #include <xen/xen.h> |
a892c8d5 | 12 | #include "blk-crypto-internal.h" |
a73f730d | 13 | |
2e9bc346 CH |
14 | struct elevator_type; |
15 | ||
3d9a9e9a ML |
16 | #define BLK_DEV_MAX_SECTORS (LLONG_MAX >> 9) |
17 | ||
0d2602ca JA |
18 | /* Max future timer expiry for timeouts */ |
19 | #define BLK_MAX_TIMEOUT (5 * HZ) | |
20 | ||
18fbda91 | 21 | extern struct dentry *blk_debugfs_root; |
18fbda91 | 22 | |
7c94e1c1 | 23 | struct blk_flush_queue { |
b175c867 | 24 | spinlock_t mq_flush_lock; |
7c94e1c1 ML |
25 | unsigned int flush_pending_idx:1; |
26 | unsigned int flush_running_idx:1; | |
8d699663 | 27 | blk_status_t rq_status; |
7c94e1c1 ML |
28 | unsigned long flush_pending_since; |
29 | struct list_head flush_queue[2]; | |
b175c867 | 30 | unsigned long flush_data_in_flight; |
7c94e1c1 | 31 | struct request *flush_rq; |
7c94e1c1 ML |
32 | }; |
33 | ||
a9ed27a7 | 34 | bool is_flush_rq(struct request *req); |
8d699663 | 35 | |
754a1572 GJ |
36 | struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size, |
37 | gfp_t flags); | |
f70ced09 | 38 | void blk_free_flush_queue(struct blk_flush_queue *q); |
f3552655 | 39 | |
f1be1788 ML |
40 | bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic); |
41 | bool blk_queue_start_drain(struct request_queue *q); | |
6a786998 ML |
42 | bool __blk_freeze_queue_start(struct request_queue *q, |
43 | struct task_struct *owner); | |
c98cb5bb | 44 | int __bio_queue_enter(struct request_queue *q, struct bio *bio); |
3f98c753 | 45 | void submit_bio_noacct_nocheck(struct bio *bio); |
0f8e9ecc | 46 | void bio_await_chain(struct bio *bio); |
c98cb5bb JA |
47 | |
48 | static inline bool blk_try_enter_queue(struct request_queue *q, bool pm) | |
49 | { | |
50 | rcu_read_lock(); | |
51 | if (!percpu_ref_tryget_live_rcu(&q->q_usage_counter)) | |
52 | goto fail; | |
53 | ||
54 | /* | |
55 | * The code that increments the pm_only counter must ensure that the | |
56 | * counter is globally visible before the queue is unfrozen. | |
57 | */ | |
58 | if (blk_queue_pm_only(q) && | |
59 | (!pm || queue_rpm_status(q) == RPM_SUSPENDED)) | |
60 | goto fail_put; | |
61 | ||
62 | rcu_read_unlock(); | |
63 | return true; | |
64 | ||
65 | fail_put: | |
66 | blk_queue_exit(q); | |
67 | fail: | |
68 | rcu_read_unlock(); | |
69 | return false; | |
70 | } | |
71 | ||
72 | static inline int bio_queue_enter(struct bio *bio) | |
73 | { | |
74 | struct request_queue *q = bdev_get_queue(bio->bi_bdev); | |
75 | ||
f1be1788 ML |
76 | if (blk_try_enter_queue(q, false)) { |
77 | rwsem_acquire_read(&q->io_lockdep_map, 0, 0, _RET_IP_); | |
78 | rwsem_release(&q->io_lockdep_map, _RET_IP_); | |
c98cb5bb | 79 | return 0; |
f1be1788 | 80 | } |
c98cb5bb JA |
81 | return __bio_queue_enter(q, bio); |
82 | } | |
3ef28e83 | 83 | |
0eb4db47 KB |
84 | static inline void blk_wait_io(struct completion *done) |
85 | { | |
86 | /* Prevent hang_check timer from firing at us during very long I/O */ | |
87 | unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2; | |
88 | ||
89 | if (timeout) | |
90 | while (!wait_for_completion_io_timeout(done, timeout)) | |
91 | ; | |
92 | else | |
93 | wait_for_completion_io(done); | |
94 | } | |
95 | ||
dc0b8a57 | 96 | #define BIO_INLINE_VECS 4 |
7a800a20 CH |
97 | struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs, |
98 | gfp_t gfp_mask); | |
99 | void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs); | |
eec716a1 | 100 | |
7c8998f7 JC |
101 | bool bvec_try_merge_hw_page(struct request_queue *q, struct bio_vec *bv, |
102 | struct page *page, unsigned len, unsigned offset, | |
103 | bool *same_page); | |
104 | ||
3dccdae5 CH |
105 | static inline bool biovec_phys_mergeable(struct request_queue *q, |
106 | struct bio_vec *vec1, struct bio_vec *vec2) | |
6a9f5f24 | 107 | { |
3dccdae5 | 108 | unsigned long mask = queue_segment_boundary(q); |
25f76c3d CH |
109 | phys_addr_t addr1 = bvec_phys(vec1); |
110 | phys_addr_t addr2 = bvec_phys(vec2); | |
3dccdae5 | 111 | |
f630a5d0 AP |
112 | /* |
113 | * Merging adjacent physical pages may not work correctly under KMSAN | |
114 | * if their metadata pages aren't adjacent. Just disable merging. | |
115 | */ | |
116 | if (IS_ENABLED(CONFIG_KMSAN)) | |
117 | return false; | |
118 | ||
3dccdae5 | 119 | if (addr1 + vec1->bv_len != addr2) |
6a9f5f24 | 120 | return false; |
0383ad43 | 121 | if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page)) |
6a9f5f24 | 122 | return false; |
3dccdae5 CH |
123 | if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask)) |
124 | return false; | |
6a9f5f24 CH |
125 | return true; |
126 | } | |
127 | ||
aa261f20 | 128 | static inline bool __bvec_gap_to_prev(const struct queue_limits *lim, |
27ca1d4e CH |
129 | struct bio_vec *bprv, unsigned int offset) |
130 | { | |
c55ddd90 CH |
131 | return (offset & lim->virt_boundary_mask) || |
132 | ((bprv->bv_offset + bprv->bv_len) & lim->virt_boundary_mask); | |
27ca1d4e CH |
133 | } |
134 | ||
135 | /* | |
136 | * Check if adding a bio_vec after bprv with offset would create a gap in | |
137 | * the SG list. Most drivers don't care about this, but some do. | |
138 | */ | |
aa261f20 | 139 | static inline bool bvec_gap_to_prev(const struct queue_limits *lim, |
27ca1d4e CH |
140 | struct bio_vec *bprv, unsigned int offset) |
141 | { | |
c55ddd90 | 142 | if (!lim->virt_boundary_mask) |
27ca1d4e | 143 | return false; |
c55ddd90 | 144 | return __bvec_gap_to_prev(lim, bprv, offset); |
27ca1d4e CH |
145 | } |
146 | ||
badf7f64 CH |
147 | static inline bool rq_mergeable(struct request *rq) |
148 | { | |
149 | if (blk_rq_is_passthrough(rq)) | |
150 | return false; | |
151 | ||
152 | if (req_op(rq) == REQ_OP_FLUSH) | |
153 | return false; | |
154 | ||
155 | if (req_op(rq) == REQ_OP_WRITE_ZEROES) | |
156 | return false; | |
157 | ||
158 | if (req_op(rq) == REQ_OP_ZONE_APPEND) | |
159 | return false; | |
160 | ||
161 | if (rq->cmd_flags & REQ_NOMERGE_FLAGS) | |
162 | return false; | |
163 | if (rq->rq_flags & RQF_NOMERGE_FLAGS) | |
164 | return false; | |
165 | ||
166 | return true; | |
167 | } | |
168 | ||
169 | /* | |
170 | * There are two different ways to handle DISCARD merges: | |
171 | * 1) If max_discard_segments > 1, the driver treats every bio as a range and | |
172 | * send the bios to controller together. The ranges don't need to be | |
173 | * contiguous. | |
174 | * 2) Otherwise, the request will be normal read/write requests. The ranges | |
175 | * need to be contiguous. | |
176 | */ | |
177 | static inline bool blk_discard_mergable(struct request *req) | |
178 | { | |
179 | if (req_op(req) == REQ_OP_DISCARD && | |
180 | queue_max_discard_segments(req->q) > 1) | |
181 | return true; | |
182 | return false; | |
183 | } | |
184 | ||
49d24398 US |
185 | static inline unsigned int blk_rq_get_max_segments(struct request *rq) |
186 | { | |
187 | if (req_op(rq) == REQ_OP_DISCARD) | |
188 | return queue_max_discard_segments(rq->q); | |
189 | return queue_max_segments(rq->q); | |
190 | } | |
191 | ||
8d1dfd51 | 192 | static inline unsigned int blk_queue_get_max_sectors(struct request *rq) |
2a9336c4 | 193 | { |
8d1dfd51 JG |
194 | struct request_queue *q = rq->q; |
195 | enum req_op op = req_op(rq); | |
196 | ||
2a9336c4 CH |
197 | if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)) |
198 | return min(q->limits.max_discard_sectors, | |
199 | UINT_MAX >> SECTOR_SHIFT); | |
200 | ||
201 | if (unlikely(op == REQ_OP_WRITE_ZEROES)) | |
202 | return q->limits.max_write_zeroes_sectors; | |
203 | ||
9da3d1e9 JG |
204 | if (rq->cmd_flags & REQ_ATOMIC) |
205 | return q->limits.atomic_write_max_sectors; | |
206 | ||
2a9336c4 CH |
207 | return q->limits.max_sectors; |
208 | } | |
209 | ||
5a48fc14 DW |
210 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
211 | void blk_flush_integrity(void); | |
ece841ab | 212 | void bio_integrity_free(struct bio *bio); |
85253bac CH |
213 | |
214 | /* | |
215 | * Integrity payloads can either be owned by the submitter, in which case | |
216 | * bio_uninit will free them, or owned and generated by the block layer, | |
217 | * in which case we'll verify them here (for reads) and free them before | |
218 | * the bio is handed back to the submitted. | |
219 | */ | |
220 | bool __bio_integrity_endio(struct bio *bio); | |
7c20f116 CH |
221 | static inline bool bio_integrity_endio(struct bio *bio) |
222 | { | |
85253bac CH |
223 | struct bio_integrity_payload *bip = bio_integrity(bio); |
224 | ||
225 | if (bip && (bip->bip_flags & BIP_BLOCK_INTEGRITY)) | |
7c20f116 CH |
226 | return __bio_integrity_endio(bio); |
227 | return true; | |
228 | } | |
43b729bf | 229 | |
92cf2fd1 CH |
230 | bool blk_integrity_merge_rq(struct request_queue *, struct request *, |
231 | struct request *); | |
d59da419 CH |
232 | bool blk_integrity_merge_bio(struct request_queue *, struct request *, |
233 | struct bio *); | |
92cf2fd1 | 234 | |
43b729bf CH |
235 | static inline bool integrity_req_gap_back_merge(struct request *req, |
236 | struct bio *next) | |
237 | { | |
238 | struct bio_integrity_payload *bip = bio_integrity(req->bio); | |
239 | struct bio_integrity_payload *bip_next = bio_integrity(next); | |
240 | ||
c55ddd90 CH |
241 | return bvec_gap_to_prev(&req->q->limits, |
242 | &bip->bip_vec[bip->bip_vcnt - 1], | |
43b729bf CH |
243 | bip_next->bip_vec[0].bv_offset); |
244 | } | |
245 | ||
246 | static inline bool integrity_req_gap_front_merge(struct request *req, | |
247 | struct bio *bio) | |
248 | { | |
249 | struct bio_integrity_payload *bip = bio_integrity(bio); | |
250 | struct bio_integrity_payload *bip_next = bio_integrity(req->bio); | |
251 | ||
c55ddd90 CH |
252 | return bvec_gap_to_prev(&req->q->limits, |
253 | &bip->bip_vec[bip->bip_vcnt - 1], | |
43b729bf CH |
254 | bip_next->bip_vec[0].bv_offset); |
255 | } | |
581e2600 | 256 | |
ff53cd52 | 257 | extern const struct attribute_group blk_integrity_attr_group; |
43b729bf | 258 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
92cf2fd1 CH |
259 | static inline bool blk_integrity_merge_rq(struct request_queue *rq, |
260 | struct request *r1, struct request *r2) | |
261 | { | |
262 | return true; | |
263 | } | |
d59da419 CH |
264 | static inline bool blk_integrity_merge_bio(struct request_queue *rq, |
265 | struct request *r, struct bio *b) | |
266 | { | |
267 | return true; | |
268 | } | |
43b729bf CH |
269 | static inline bool integrity_req_gap_back_merge(struct request *req, |
270 | struct bio *next) | |
271 | { | |
272 | return false; | |
273 | } | |
274 | static inline bool integrity_req_gap_front_merge(struct request *req, | |
275 | struct bio *bio) | |
276 | { | |
277 | return false; | |
278 | } | |
279 | ||
5a48fc14 DW |
280 | static inline void blk_flush_integrity(void) |
281 | { | |
282 | } | |
7c20f116 CH |
283 | static inline bool bio_integrity_endio(struct bio *bio) |
284 | { | |
285 | return true; | |
286 | } | |
ece841ab JT |
287 | static inline void bio_integrity_free(struct bio *bio) |
288 | { | |
289 | } | |
43b729bf | 290 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
8324aa91 | 291 | |
0d2602ca | 292 | unsigned long blk_rq_timeout(unsigned long timeout); |
87ee7b11 | 293 | void blk_add_timer(struct request *req); |
320ae51f | 294 | |
dd850ff3 DLM |
295 | enum bio_merge_status { |
296 | BIO_MERGE_OK, | |
297 | BIO_MERGE_NONE, | |
298 | BIO_MERGE_FAILED, | |
299 | }; | |
300 | ||
301 | enum bio_merge_status bio_attempt_back_merge(struct request *req, | |
302 | struct bio *bio, unsigned int nr_segs); | |
320ae51f | 303 | bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, |
0c5bcc92 | 304 | unsigned int nr_segs); |
bdc6a287 BW |
305 | bool blk_bio_list_merge(struct request_queue *q, struct list_head *list, |
306 | struct bio *bio, unsigned int nr_segs); | |
320ae51f | 307 | |
ba0ffdd8 JA |
308 | /* |
309 | * Plug flush limits | |
310 | */ | |
311 | #define BLK_MAX_REQUEST_COUNT 32 | |
312 | #define BLK_PLUG_FLUSH_SIZE (128 * 1024) | |
313 | ||
158dbda0 TH |
314 | /* |
315 | * Internal elevator interface | |
316 | */ | |
e8064021 | 317 | #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED) |
158dbda0 | 318 | |
360f2648 | 319 | bool blk_insert_flush(struct request *rq); |
dd831006 | 320 | |
8237c01f | 321 | int elevator_switch(struct request_queue *q, struct elevator_type *new_e); |
64b36075 | 322 | void elevator_disable(struct request_queue *q); |
0c6cb3a2 | 323 | void elevator_exit(struct request_queue *q); |
cecf5d87 | 324 | int elv_register_queue(struct request_queue *q, bool uevent); |
83d016ac BVA |
325 | void elv_unregister_queue(struct request_queue *q); |
326 | ||
3ad5cee5 CH |
327 | ssize_t part_size_show(struct device *dev, struct device_attribute *attr, |
328 | char *buf); | |
329 | ssize_t part_stat_show(struct device *dev, struct device_attribute *attr, | |
330 | char *buf); | |
331 | ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr, | |
332 | char *buf); | |
333 | ssize_t part_fail_show(struct device *dev, struct device_attribute *attr, | |
334 | char *buf); | |
335 | ssize_t part_fail_store(struct device *dev, struct device_attribute *attr, | |
336 | const char *buf, size_t count); | |
581d4e28 JA |
337 | ssize_t part_timeout_show(struct device *, struct device_attribute *, char *); |
338 | ssize_t part_timeout_store(struct device *, struct device_attribute *, | |
339 | const char *, size_t); | |
581d4e28 | 340 | |
b35243a4 CH |
341 | struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim, |
342 | unsigned *nsegs); | |
343 | struct bio *bio_split_write_zeroes(struct bio *bio, | |
344 | const struct queue_limits *lim, unsigned *nsegs); | |
345 | struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim, | |
346 | unsigned *nr_segs); | |
1e8a7f6a CH |
347 | struct bio *bio_split_zone_append(struct bio *bio, |
348 | const struct queue_limits *lim, unsigned *nr_segs); | |
b35243a4 CH |
349 | |
350 | /* | |
351 | * All drivers must accept single-segments bios that are smaller than PAGE_SIZE. | |
352 | * | |
353 | * This is a quick and dirty check that relies on the fact that bi_io_vec[0] is | |
354 | * always valid if a bio has data. The check might lead to occasional false | |
355 | * positives when bios are cloned, but compared to the performance impact of | |
356 | * cloned bios themselves the loop below doesn't matter anyway. | |
357 | */ | |
358 | static inline bool bio_may_need_split(struct bio *bio, | |
359 | const struct queue_limits *lim) | |
360 | { | |
361 | return lim->chunk_sectors || bio->bi_vcnt != 1 || | |
362 | bio->bi_io_vec->bv_len + bio->bi_io_vec->bv_offset > PAGE_SIZE; | |
363 | } | |
364 | ||
365 | /** | |
366 | * __bio_split_to_limits - split a bio to fit the queue limits | |
367 | * @bio: bio to be split | |
368 | * @lim: queue limits to split based on | |
369 | * @nr_segs: returns the number of segments in the returned bio | |
370 | * | |
371 | * Check if @bio needs splitting based on the queue limits, and if so split off | |
372 | * a bio fitting the limits from the beginning of @bio and return it. @bio is | |
373 | * shortened to the remainder and re-submitted. | |
374 | * | |
375 | * The split bio is allocated from @q->bio_split, which is provided by the | |
376 | * block layer. | |
377 | */ | |
378 | static inline struct bio *__bio_split_to_limits(struct bio *bio, | |
379 | const struct queue_limits *lim, unsigned int *nr_segs) | |
abd45c15 JA |
380 | { |
381 | switch (bio_op(bio)) { | |
12515809 CH |
382 | case REQ_OP_READ: |
383 | case REQ_OP_WRITE: | |
b35243a4 CH |
384 | if (bio_may_need_split(bio, lim)) |
385 | return bio_split_rw(bio, lim, nr_segs); | |
386 | *nr_segs = 1; | |
387 | return bio; | |
1e8a7f6a CH |
388 | case REQ_OP_ZONE_APPEND: |
389 | return bio_split_zone_append(bio, lim, nr_segs); | |
abd45c15 JA |
390 | case REQ_OP_DISCARD: |
391 | case REQ_OP_SECURE_ERASE: | |
b35243a4 | 392 | return bio_split_discard(bio, lim, nr_segs); |
abd45c15 | 393 | case REQ_OP_WRITE_ZEROES: |
b35243a4 | 394 | return bio_split_write_zeroes(bio, lim, nr_segs); |
12515809 CH |
395 | default: |
396 | /* other operations can't be split */ | |
397 | *nr_segs = 0; | |
398 | return bio; | |
abd45c15 | 399 | } |
abd45c15 JA |
400 | } |
401 | ||
14ccb66b CH |
402 | int ll_back_merge_fn(struct request *req, struct bio *bio, |
403 | unsigned int nr_segs); | |
fd2ef39c | 404 | bool blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
5e84ea3a | 405 | struct request *next); |
e9cd19c0 | 406 | unsigned int blk_recalc_rq_segments(struct request *rq); |
050c8ea8 | 407 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio); |
34fe7c05 | 408 | enum elv_merge blk_try_merge(struct request *rq, struct bio *bio); |
d6d48196 | 409 | |
d690cb8a | 410 | int blk_set_default_limits(struct queue_limits *lim); |
73781b3b CH |
411 | void blk_apply_bdi_limits(struct backing_dev_info *bdi, |
412 | struct queue_limits *lim); | |
ff88972c AB |
413 | int blk_dev_init(void); |
414 | ||
450b7879 | 415 | void update_io_ticks(struct block_device *part, unsigned long now, bool end); |
99dc4223 | 416 | unsigned int part_in_flight(struct block_device *part); |
fb8ec18c | 417 | |
6cf7677f CH |
418 | static inline void req_set_nomerge(struct request_queue *q, struct request *req) |
419 | { | |
420 | req->cmd_flags |= REQ_NOMERGE; | |
421 | if (req == q->last_merge) | |
422 | q->last_merge = NULL; | |
423 | } | |
424 | ||
f2dbd76a TH |
425 | /* |
426 | * Internal io_context interface | |
427 | */ | |
87dd1d63 | 428 | struct io_cq *ioc_find_get_icq(struct request_queue *q); |
eca5892a | 429 | struct io_cq *ioc_lookup_icq(struct request_queue *q); |
5ef16305 | 430 | #ifdef CONFIG_BLK_ICQ |
7e5a8794 | 431 | void ioc_clear_queue(struct request_queue *q); |
5ef16305 CH |
432 | #else |
433 | static inline void ioc_clear_queue(struct request_queue *q) | |
434 | { | |
435 | } | |
436 | #endif /* CONFIG_BLK_ICQ */ | |
f2dbd76a | 437 | |
51d798cd | 438 | struct bio *__blk_queue_bounce(struct bio *bio, struct request_queue *q); |
9bb33f24 CH |
439 | |
440 | static inline bool blk_queue_may_bounce(struct request_queue *q) | |
441 | { | |
442 | return IS_ENABLED(CONFIG_BOUNCE) && | |
339d3948 | 443 | (q->limits.features & BLK_FEAT_BOUNCE_HIGH) && |
9bb33f24 CH |
444 | max_low_pfn >= max_pfn; |
445 | } | |
446 | ||
51d798cd CH |
447 | static inline struct bio *blk_queue_bounce(struct bio *bio, |
448 | struct request_queue *q) | |
3bce016a | 449 | { |
51d798cd CH |
450 | if (unlikely(blk_queue_may_bounce(q) && bio_has_data(bio))) |
451 | return __blk_queue_bounce(bio, q); | |
452 | return bio; | |
3bce016a | 453 | } |
3bce016a | 454 | |
bf505456 | 455 | #ifdef CONFIG_BLK_DEV_ZONED |
dd291d77 DLM |
456 | void disk_init_zone_resources(struct gendisk *disk); |
457 | void disk_free_zone_resources(struct gendisk *disk); | |
458 | static inline bool bio_zone_write_plugging(struct bio *bio) | |
459 | { | |
460 | return bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING); | |
461 | } | |
462 | void blk_zone_write_plug_bio_merged(struct bio *bio); | |
096bc7ea | 463 | void blk_zone_write_plug_init_request(struct request *rq); |
a0508c36 DLM |
464 | static inline void blk_zone_update_request_bio(struct request *rq, |
465 | struct bio *bio) | |
466 | { | |
467 | /* | |
468 | * For zone append requests, the request sector indicates the location | |
469 | * at which the BIO data was written. Return this value to the BIO | |
470 | * issuer through the BIO iter sector. | |
9b1ce7f0 DLM |
471 | * For plugged zone writes, which include emulated zone append, we need |
472 | * the original BIO sector so that blk_zone_write_plug_bio_endio() can | |
473 | * lookup the zone write plug. | |
a0508c36 | 474 | */ |
dd291d77 | 475 | if (req_op(rq) == REQ_OP_ZONE_APPEND || bio_zone_write_plugging(bio)) |
a0508c36 DLM |
476 | bio->bi_iter.bi_sector = rq->__sector; |
477 | } | |
dd291d77 DLM |
478 | void blk_zone_write_plug_bio_endio(struct bio *bio); |
479 | static inline void blk_zone_bio_endio(struct bio *bio) | |
480 | { | |
481 | /* | |
482 | * For write BIOs to zoned devices, signal the completion of the BIO so | |
483 | * that the next write BIO can be submitted by zone write plugging. | |
484 | */ | |
485 | if (bio_zone_write_plugging(bio)) | |
486 | blk_zone_write_plug_bio_endio(bio); | |
487 | } | |
488 | ||
347bde9d DLM |
489 | void blk_zone_write_plug_finish_request(struct request *rq); |
490 | static inline void blk_zone_finish_request(struct request *rq) | |
dd291d77 DLM |
491 | { |
492 | if (rq->rq_flags & RQF_ZONE_WRITE_PLUGGING) | |
347bde9d | 493 | blk_zone_write_plug_finish_request(rq); |
dd291d77 | 494 | } |
5e4ea834 CH |
495 | int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd, |
496 | unsigned long arg); | |
05bdb996 | 497 | int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode, |
cfb42576 CH |
498 | unsigned int cmd, unsigned long arg); |
499 | #else /* CONFIG_BLK_DEV_ZONED */ | |
dd291d77 DLM |
500 | static inline void disk_init_zone_resources(struct gendisk *disk) |
501 | { | |
502 | } | |
503 | static inline void disk_free_zone_resources(struct gendisk *disk) | |
504 | { | |
505 | } | |
506 | static inline bool bio_zone_write_plugging(struct bio *bio) | |
507 | { | |
508 | return false; | |
509 | } | |
510 | static inline void blk_zone_write_plug_bio_merged(struct bio *bio) | |
511 | { | |
512 | } | |
096bc7ea | 513 | static inline void blk_zone_write_plug_init_request(struct request *rq) |
a0508c36 DLM |
514 | { |
515 | } | |
516 | static inline void blk_zone_update_request_bio(struct request *rq, | |
517 | struct bio *bio) | |
518 | { | |
519 | } | |
dd291d77 DLM |
520 | static inline void blk_zone_bio_endio(struct bio *bio) |
521 | { | |
522 | } | |
347bde9d | 523 | static inline void blk_zone_finish_request(struct request *rq) |
dd291d77 DLM |
524 | { |
525 | } | |
cfb42576 | 526 | static inline int blkdev_report_zones_ioctl(struct block_device *bdev, |
5e4ea834 | 527 | unsigned int cmd, unsigned long arg) |
cfb42576 CH |
528 | { |
529 | return -ENOTTY; | |
530 | } | |
531 | static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev, | |
05bdb996 | 532 | blk_mode_t mode, unsigned int cmd, unsigned long arg) |
cfb42576 CH |
533 | { |
534 | return -ENOTTY; | |
535 | } | |
536 | #endif /* CONFIG_BLK_DEV_ZONED */ | |
537 | ||
538 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno); | |
539 | void bdev_add(struct block_device *bdev, dev_t dev); | |
2638c208 AV |
540 | void bdev_unhash(struct block_device *bdev); |
541 | void bdev_drop(struct block_device *bdev); | |
bf505456 | 542 | |
7c3f828b CH |
543 | int blk_alloc_ext_minor(void); |
544 | void blk_free_ext_minor(unsigned int minor); | |
581e2600 CH |
545 | #define ADDPART_FLAG_NONE 0 |
546 | #define ADDPART_FLAG_RAID 1 | |
547 | #define ADDPART_FLAG_WHOLEDISK 2 | |
ba40f4c5 | 548 | #define ADDPART_FLAG_READONLY 4 |
7f6be376 CH |
549 | int bdev_add_partition(struct gendisk *disk, int partno, sector_t start, |
550 | sector_t length); | |
926fbb16 | 551 | int bdev_del_partition(struct gendisk *disk, int partno); |
3d2e7989 CH |
552 | int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start, |
553 | sector_t length); | |
eec1be4c | 554 | void drop_partition(struct block_device *part); |
581e2600 | 555 | |
83794367 DLM |
556 | void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors); |
557 | ||
6f8191fd CH |
558 | struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id, |
559 | struct lock_class_key *lkclass); | |
560 | ||
fd363244 DH |
561 | /* |
562 | * Clean up a page appropriately, where the page may be pinned, may have a | |
563 | * ref taken on it or neither. | |
564 | */ | |
565 | static inline void bio_release_page(struct bio *bio, struct page *page) | |
566 | { | |
567 | if (bio_flagged(bio, BIO_PAGE_PINNED)) | |
568 | unpin_user_page(page); | |
fd363244 DH |
569 | } |
570 | ||
ad751ba1 | 571 | struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id); |
704b914f | 572 | |
05bdb996 | 573 | int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode); |
da7ba729 | 574 | |
92e7755e | 575 | int disk_alloc_events(struct gendisk *disk); |
d5870edf CH |
576 | void disk_add_events(struct gendisk *disk); |
577 | void disk_del_events(struct gendisk *disk); | |
578 | void disk_release_events(struct gendisk *disk); | |
926597ff CH |
579 | void disk_block_events(struct gendisk *disk); |
580 | void disk_unblock_events(struct gendisk *disk); | |
581 | void disk_flush_events(struct gendisk *disk, unsigned int mask); | |
2bc8cda5 CH |
582 | extern struct device_attribute dev_attr_events; |
583 | extern struct device_attribute dev_attr_events_async; | |
584 | extern struct device_attribute dev_attr_events_poll_msecs; | |
d5870edf | 585 | |
cc5c516d CH |
586 | extern struct attribute_group blk_trace_attr_group; |
587 | ||
05bdb996 CH |
588 | blk_mode_t file_to_blk_mode(struct file *file); |
589 | int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, | |
590 | loff_t lstart, loff_t lend); | |
8a709512 | 591 | long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg); |
50c52250 | 592 | int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags); |
84b8514b CH |
593 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg); |
594 | ||
cd82cca7 CH |
595 | extern const struct address_space_operations def_blk_aops; |
596 | ||
22d0c408 | 597 | int disk_register_independent_access_ranges(struct gendisk *disk); |
a2247f19 DLM |
598 | void disk_unregister_independent_access_ranges(struct gendisk *disk); |
599 | ||
06c8c691 CH |
600 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
601 | bool should_fail_request(struct block_device *part, unsigned int bytes); | |
602 | #else /* CONFIG_FAIL_MAKE_REQUEST */ | |
603 | static inline bool should_fail_request(struct block_device *part, | |
604 | unsigned int bytes) | |
605 | { | |
606 | return false; | |
607 | } | |
608 | #endif /* CONFIG_FAIL_MAKE_REQUEST */ | |
609 | ||
0a467d0f JA |
610 | /* |
611 | * Optimized request reference counting. Ideally we'd make timeouts be more | |
612 | * clever, as that's the only reason we need references at all... But until | |
613 | * this happens, this is faster than using refcount_t. Also see: | |
614 | * | |
615 | * abc54d634334 ("io_uring: switch to atomic_t for io_kiocb reference count") | |
616 | */ | |
617 | #define req_ref_zero_or_close_to_overflow(req) \ | |
618 | ((unsigned int) atomic_read(&(req->ref)) + 127u <= 127u) | |
619 | ||
620 | static inline bool req_ref_inc_not_zero(struct request *req) | |
621 | { | |
622 | return atomic_inc_not_zero(&req->ref); | |
623 | } | |
624 | ||
625 | static inline bool req_ref_put_and_test(struct request *req) | |
626 | { | |
627 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); | |
628 | return atomic_dec_and_test(&req->ref); | |
629 | } | |
630 | ||
631 | static inline void req_ref_set(struct request *req, int value) | |
632 | { | |
633 | atomic_set(&req->ref, value); | |
634 | } | |
635 | ||
636 | static inline int req_ref_read(struct request *req) | |
637 | { | |
638 | return atomic_read(&req->ref); | |
639 | } | |
640 | ||
08420cf7 JA |
641 | static inline u64 blk_time_get_ns(void) |
642 | { | |
da4c8c3d JA |
643 | struct blk_plug *plug = current->plug; |
644 | ||
b874d4aa | 645 | if (!plug || !in_task()) |
da4c8c3d JA |
646 | return ktime_get_ns(); |
647 | ||
648 | /* | |
649 | * 0 could very well be a valid time, but rather than flag "this is | |
650 | * a valid timestamp" separately, just accept that we'll do an extra | |
651 | * ktime_get_ns() if we just happen to get 0 as the current time. | |
652 | */ | |
06b23f92 | 653 | if (!plug->cur_ktime) { |
da4c8c3d | 654 | plug->cur_ktime = ktime_get_ns(); |
06b23f92 JA |
655 | current->flags |= PF_BLOCK_TS; |
656 | } | |
da4c8c3d | 657 | return plug->cur_ktime; |
08420cf7 JA |
658 | } |
659 | ||
660 | static inline ktime_t blk_time_get(void) | |
661 | { | |
662 | return ns_to_ktime(blk_time_get_ns()); | |
663 | } | |
664 | ||
c4e47bbb JA |
665 | /* |
666 | * From most significant bit: | |
667 | * 1 bit: reserved for other usage, see below | |
668 | * 12 bits: original size of bio | |
669 | * 51 bits: issue time of bio | |
670 | */ | |
671 | #define BIO_ISSUE_RES_BITS 1 | |
672 | #define BIO_ISSUE_SIZE_BITS 12 | |
673 | #define BIO_ISSUE_RES_SHIFT (64 - BIO_ISSUE_RES_BITS) | |
674 | #define BIO_ISSUE_SIZE_SHIFT (BIO_ISSUE_RES_SHIFT - BIO_ISSUE_SIZE_BITS) | |
675 | #define BIO_ISSUE_TIME_MASK ((1ULL << BIO_ISSUE_SIZE_SHIFT) - 1) | |
676 | #define BIO_ISSUE_SIZE_MASK \ | |
677 | (((1ULL << BIO_ISSUE_SIZE_BITS) - 1) << BIO_ISSUE_SIZE_SHIFT) | |
678 | #define BIO_ISSUE_RES_MASK (~((1ULL << BIO_ISSUE_RES_SHIFT) - 1)) | |
679 | ||
680 | /* Reserved bit for blk-throtl */ | |
681 | #define BIO_ISSUE_THROTL_SKIP_LATENCY (1ULL << 63) | |
682 | ||
683 | static inline u64 __bio_issue_time(u64 time) | |
684 | { | |
685 | return time & BIO_ISSUE_TIME_MASK; | |
686 | } | |
687 | ||
688 | static inline u64 bio_issue_time(struct bio_issue *issue) | |
689 | { | |
690 | return __bio_issue_time(issue->value); | |
691 | } | |
692 | ||
693 | static inline sector_t bio_issue_size(struct bio_issue *issue) | |
694 | { | |
695 | return ((issue->value & BIO_ISSUE_SIZE_MASK) >> BIO_ISSUE_SIZE_SHIFT); | |
696 | } | |
697 | ||
698 | static inline void bio_issue_init(struct bio_issue *issue, | |
699 | sector_t size) | |
700 | { | |
701 | size &= (1ULL << BIO_ISSUE_SIZE_BITS) - 1; | |
702 | issue->value = ((issue->value & BIO_ISSUE_RES_MASK) | | |
08420cf7 | 703 | (blk_time_get_ns() & BIO_ISSUE_TIME_MASK) | |
c4e47bbb JA |
704 | ((u64)size << BIO_ISSUE_SIZE_SHIFT)); |
705 | } | |
706 | ||
7c09a4ed | 707 | void bdev_release(struct file *bdev_file); |
a56aefca CB |
708 | int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder, |
709 | const struct blk_holder_ops *hops, struct file *bdev_file); | |
710 | int bdev_permission(dev_t dev, blk_mode_t mode, void *holder); | |
1ddeeb2a | 711 | |
d19b4634 CH |
712 | void blk_integrity_generate(struct bio *bio); |
713 | void blk_integrity_verify(struct bio *bio); | |
e9f5f44a CH |
714 | void blk_integrity_prepare(struct request *rq); |
715 | void blk_integrity_complete(struct request *rq, unsigned int nr_bytes); | |
716 | ||
6f491a8d | 717 | #ifdef CONFIG_LOCKDEP |
f6661b1d | 718 | static inline void blk_freeze_acquire_lock(struct request_queue *q) |
f1be1788 | 719 | { |
6f491a8d | 720 | if (!q->mq_freeze_disk_dead) |
f1be1788 | 721 | rwsem_acquire(&q->io_lockdep_map, 0, 1, _RET_IP_); |
f6661b1d | 722 | if (!q->mq_freeze_queue_dying) |
f1be1788 ML |
723 | rwsem_acquire(&q->q_lockdep_map, 0, 1, _RET_IP_); |
724 | } | |
725 | ||
f6661b1d | 726 | static inline void blk_unfreeze_release_lock(struct request_queue *q) |
f1be1788 | 727 | { |
f6661b1d | 728 | if (!q->mq_freeze_queue_dying) |
f1be1788 | 729 | rwsem_release(&q->q_lockdep_map, _RET_IP_); |
6f491a8d | 730 | if (!q->mq_freeze_disk_dead) |
f1be1788 ML |
731 | rwsem_release(&q->io_lockdep_map, _RET_IP_); |
732 | } | |
6f491a8d | 733 | #else |
f6661b1d | 734 | static inline void blk_freeze_acquire_lock(struct request_queue *q) |
6f491a8d ML |
735 | { |
736 | } | |
f6661b1d | 737 | static inline void blk_unfreeze_release_lock(struct request_queue *q) |
6f491a8d ML |
738 | { |
739 | } | |
740 | #endif | |
f1be1788 | 741 | |
bc9fcbf9 | 742 | #endif /* BLK_INTERNAL_H */ |