]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
8324aa91 JA |
2 | #ifndef BLK_INTERNAL_H |
3 | #define BLK_INTERNAL_H | |
4 | ||
a73f730d | 5 | #include <linux/idr.h> |
f70ced09 | 6 | #include <linux/blk-mq.h> |
c6a564ff | 7 | #include <linux/part_stat.h> |
a892c8d5 | 8 | #include <linux/blk-crypto.h> |
c39ae60d | 9 | #include <xen/xen.h> |
a892c8d5 | 10 | #include "blk-crypto-internal.h" |
f70ced09 | 11 | #include "blk-mq.h" |
c3e22192 | 12 | #include "blk-mq-sched.h" |
a73f730d | 13 | |
0d2602ca JA |
14 | /* Max future timer expiry for timeouts */ |
15 | #define BLK_MAX_TIMEOUT (5 * HZ) | |
16 | ||
18fbda91 | 17 | extern struct dentry *blk_debugfs_root; |
18fbda91 | 18 | |
7c94e1c1 | 19 | struct blk_flush_queue { |
7c94e1c1 ML |
20 | unsigned int flush_pending_idx:1; |
21 | unsigned int flush_running_idx:1; | |
8d699663 | 22 | blk_status_t rq_status; |
7c94e1c1 ML |
23 | unsigned long flush_pending_since; |
24 | struct list_head flush_queue[2]; | |
25 | struct list_head flush_data_in_flight; | |
26 | struct request *flush_rq; | |
0048b483 ML |
27 | |
28 | /* | |
29 | * flush_rq shares tag with this rq, both can't be active | |
30 | * at the same time | |
31 | */ | |
32 | struct request *orig_rq; | |
b3c6a599 | 33 | struct lock_class_key key; |
7c94e1c1 ML |
34 | spinlock_t mq_flush_lock; |
35 | }; | |
36 | ||
8324aa91 JA |
37 | extern struct kmem_cache *blk_requestq_cachep; |
38 | extern struct kobj_type blk_queue_ktype; | |
a73f730d | 39 | extern struct ida blk_queue_ida; |
8324aa91 | 40 | |
f9afca4d JA |
41 | static inline struct blk_flush_queue * |
42 | blk_get_flush_queue(struct request_queue *q, struct blk_mq_ctx *ctx) | |
7c94e1c1 | 43 | { |
8ccdf4a3 | 44 | return blk_mq_map_queue(q, REQ_OP_FLUSH, ctx)->fq; |
7c94e1c1 ML |
45 | } |
46 | ||
09ac46c4 TH |
47 | static inline void __blk_get_queue(struct request_queue *q) |
48 | { | |
49 | kobject_get(&q->kobj); | |
50 | } | |
51 | ||
8d699663 YY |
52 | static inline bool |
53 | is_flush_rq(struct request *req, struct blk_mq_hw_ctx *hctx) | |
54 | { | |
55 | return hctx->fq->flush_rq == req; | |
56 | } | |
57 | ||
754a1572 GJ |
58 | struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size, |
59 | gfp_t flags); | |
f70ced09 | 60 | void blk_free_flush_queue(struct blk_flush_queue *q); |
f3552655 | 61 | |
3ef28e83 DW |
62 | void blk_freeze_queue(struct request_queue *q); |
63 | ||
3dccdae5 CH |
64 | static inline bool biovec_phys_mergeable(struct request_queue *q, |
65 | struct bio_vec *vec1, struct bio_vec *vec2) | |
6a9f5f24 | 66 | { |
3dccdae5 | 67 | unsigned long mask = queue_segment_boundary(q); |
6e768461 CH |
68 | phys_addr_t addr1 = page_to_phys(vec1->bv_page) + vec1->bv_offset; |
69 | phys_addr_t addr2 = page_to_phys(vec2->bv_page) + vec2->bv_offset; | |
3dccdae5 CH |
70 | |
71 | if (addr1 + vec1->bv_len != addr2) | |
6a9f5f24 | 72 | return false; |
0383ad43 | 73 | if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page)) |
6a9f5f24 | 74 | return false; |
3dccdae5 CH |
75 | if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask)) |
76 | return false; | |
6a9f5f24 CH |
77 | return true; |
78 | } | |
79 | ||
27ca1d4e CH |
80 | static inline bool __bvec_gap_to_prev(struct request_queue *q, |
81 | struct bio_vec *bprv, unsigned int offset) | |
82 | { | |
df376b2e | 83 | return (offset & queue_virt_boundary(q)) || |
27ca1d4e CH |
84 | ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q)); |
85 | } | |
86 | ||
87 | /* | |
88 | * Check if adding a bio_vec after bprv with offset would create a gap in | |
89 | * the SG list. Most drivers don't care about this, but some do. | |
90 | */ | |
91 | static inline bool bvec_gap_to_prev(struct request_queue *q, | |
92 | struct bio_vec *bprv, unsigned int offset) | |
93 | { | |
94 | if (!queue_virt_boundary(q)) | |
95 | return false; | |
96 | return __bvec_gap_to_prev(q, bprv, offset); | |
97 | } | |
98 | ||
1aa0a133 CH |
99 | static inline void blk_rq_bio_prep(struct request *rq, struct bio *bio, |
100 | unsigned int nr_segs) | |
101 | { | |
102 | rq->nr_phys_segments = nr_segs; | |
103 | rq->__data_len = bio->bi_iter.bi_size; | |
104 | rq->bio = rq->biotail = bio; | |
105 | rq->ioprio = bio_prio(bio); | |
106 | ||
107 | if (bio->bi_disk) | |
108 | rq->rq_disk = bio->bi_disk; | |
109 | } | |
110 | ||
5a48fc14 DW |
111 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
112 | void blk_flush_integrity(void); | |
7c20f116 | 113 | bool __bio_integrity_endio(struct bio *); |
ece841ab | 114 | void bio_integrity_free(struct bio *bio); |
7c20f116 CH |
115 | static inline bool bio_integrity_endio(struct bio *bio) |
116 | { | |
117 | if (bio_integrity(bio)) | |
118 | return __bio_integrity_endio(bio); | |
119 | return true; | |
120 | } | |
43b729bf CH |
121 | |
122 | static inline bool integrity_req_gap_back_merge(struct request *req, | |
123 | struct bio *next) | |
124 | { | |
125 | struct bio_integrity_payload *bip = bio_integrity(req->bio); | |
126 | struct bio_integrity_payload *bip_next = bio_integrity(next); | |
127 | ||
128 | return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1], | |
129 | bip_next->bip_vec[0].bv_offset); | |
130 | } | |
131 | ||
132 | static inline bool integrity_req_gap_front_merge(struct request *req, | |
133 | struct bio *bio) | |
134 | { | |
135 | struct bio_integrity_payload *bip = bio_integrity(bio); | |
136 | struct bio_integrity_payload *bip_next = bio_integrity(req->bio); | |
137 | ||
138 | return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1], | |
139 | bip_next->bip_vec[0].bv_offset); | |
140 | } | |
581e2600 CH |
141 | |
142 | void blk_integrity_add(struct gendisk *); | |
143 | void blk_integrity_del(struct gendisk *); | |
43b729bf CH |
144 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
145 | static inline bool integrity_req_gap_back_merge(struct request *req, | |
146 | struct bio *next) | |
147 | { | |
148 | return false; | |
149 | } | |
150 | static inline bool integrity_req_gap_front_merge(struct request *req, | |
151 | struct bio *bio) | |
152 | { | |
153 | return false; | |
154 | } | |
155 | ||
5a48fc14 DW |
156 | static inline void blk_flush_integrity(void) |
157 | { | |
158 | } | |
7c20f116 CH |
159 | static inline bool bio_integrity_endio(struct bio *bio) |
160 | { | |
161 | return true; | |
162 | } | |
ece841ab JT |
163 | static inline void bio_integrity_free(struct bio *bio) |
164 | { | |
165 | } | |
581e2600 CH |
166 | static inline void blk_integrity_add(struct gendisk *disk) |
167 | { | |
168 | } | |
169 | static inline void blk_integrity_del(struct gendisk *disk) | |
170 | { | |
171 | } | |
43b729bf | 172 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
8324aa91 | 173 | |
0d2602ca | 174 | unsigned long blk_rq_timeout(unsigned long timeout); |
87ee7b11 | 175 | void blk_add_timer(struct request *req); |
320ae51f | 176 | |
14ccb66b CH |
177 | bool bio_attempt_front_merge(struct request *req, struct bio *bio, |
178 | unsigned int nr_segs); | |
179 | bool bio_attempt_back_merge(struct request *req, struct bio *bio, | |
180 | unsigned int nr_segs); | |
1e739730 CH |
181 | bool bio_attempt_discard_merge(struct request_queue *q, struct request *req, |
182 | struct bio *bio); | |
320ae51f | 183 | bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, |
14ccb66b | 184 | unsigned int nr_segs, struct request **same_queue_rq); |
320ae51f | 185 | |
b5af37ab | 186 | void blk_account_io_start(struct request *req); |
522a7775 | 187 | void blk_account_io_done(struct request *req, u64 now); |
320ae51f | 188 | |
158dbda0 TH |
189 | /* |
190 | * Internal elevator interface | |
191 | */ | |
e8064021 | 192 | #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED) |
158dbda0 | 193 | |
ae1b1539 | 194 | void blk_insert_flush(struct request *rq); |
dd831006 | 195 | |
954b4a5c | 196 | void elevator_init_mq(struct request_queue *q); |
d48ece20 JW |
197 | int elevator_switch_mq(struct request_queue *q, |
198 | struct elevator_type *new_e); | |
c3e22192 | 199 | void __elevator_exit(struct request_queue *, struct elevator_queue *); |
cecf5d87 | 200 | int elv_register_queue(struct request_queue *q, bool uevent); |
83d016ac BVA |
201 | void elv_unregister_queue(struct request_queue *q); |
202 | ||
c3e22192 ML |
203 | static inline void elevator_exit(struct request_queue *q, |
204 | struct elevator_queue *e) | |
205 | { | |
284b94be ML |
206 | lockdep_assert_held(&q->sysfs_lock); |
207 | ||
c3e22192 ML |
208 | blk_mq_sched_free_requests(q); |
209 | __elevator_exit(q, e); | |
210 | } | |
211 | ||
807d4af2 CH |
212 | struct hd_struct *__disk_get_part(struct gendisk *disk, int partno); |
213 | ||
3ad5cee5 CH |
214 | ssize_t part_size_show(struct device *dev, struct device_attribute *attr, |
215 | char *buf); | |
216 | ssize_t part_stat_show(struct device *dev, struct device_attribute *attr, | |
217 | char *buf); | |
218 | ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr, | |
219 | char *buf); | |
220 | ssize_t part_fail_show(struct device *dev, struct device_attribute *attr, | |
221 | char *buf); | |
222 | ssize_t part_fail_store(struct device *dev, struct device_attribute *attr, | |
223 | const char *buf, size_t count); | |
581d4e28 JA |
224 | ssize_t part_timeout_show(struct device *, struct device_attribute *, char *); |
225 | ssize_t part_timeout_store(struct device *, struct device_attribute *, | |
226 | const char *, size_t); | |
581d4e28 | 227 | |
14ccb66b CH |
228 | void __blk_queue_split(struct request_queue *q, struct bio **bio, |
229 | unsigned int *nr_segs); | |
230 | int ll_back_merge_fn(struct request *req, struct bio *bio, | |
231 | unsigned int nr_segs); | |
232 | int ll_front_merge_fn(struct request *req, struct bio *bio, | |
233 | unsigned int nr_segs); | |
b973cb7e JA |
234 | struct request *attempt_back_merge(struct request_queue *q, struct request *rq); |
235 | struct request *attempt_front_merge(struct request_queue *q, struct request *rq); | |
5e84ea3a JA |
236 | int blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
237 | struct request *next); | |
e9cd19c0 | 238 | unsigned int blk_recalc_rq_segments(struct request *rq); |
80a761fd | 239 | void blk_rq_set_mixed_merge(struct request *rq); |
050c8ea8 | 240 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio); |
34fe7c05 | 241 | enum elv_merge blk_try_merge(struct request *rq, struct bio *bio); |
d6d48196 | 242 | |
ff88972c AB |
243 | int blk_dev_init(void); |
244 | ||
c2553b58 JA |
245 | /* |
246 | * Contribute to IO statistics IFF: | |
247 | * | |
248 | * a) it's attached to a gendisk, and | |
48d9b0d4 | 249 | * b) the queue had IO stats enabled when this request was started |
c2553b58 | 250 | */ |
599d067d | 251 | static inline bool blk_do_io_stat(struct request *rq) |
fb8ec18c | 252 | { |
48d9b0d4 | 253 | return rq->rq_disk && (rq->rq_flags & RQF_IO_STAT); |
fb8ec18c JA |
254 | } |
255 | ||
6cf7677f CH |
256 | static inline void req_set_nomerge(struct request_queue *q, struct request *req) |
257 | { | |
258 | req->cmd_flags |= REQ_NOMERGE; | |
259 | if (req == q->last_merge) | |
260 | q->last_merge = NULL; | |
261 | } | |
262 | ||
1adfc5e4 ML |
263 | /* |
264 | * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size | |
265 | * is defined as 'unsigned int', meantime it has to aligned to with logical | |
266 | * block size which is the minimum accepted unit by hardware. | |
267 | */ | |
268 | static inline unsigned int bio_allowed_max_sectors(struct request_queue *q) | |
269 | { | |
270 | return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9; | |
271 | } | |
272 | ||
f2dbd76a TH |
273 | /* |
274 | * Internal io_context interface | |
275 | */ | |
276 | void get_io_context(struct io_context *ioc); | |
47fdd4ca | 277 | struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q); |
24acfc34 TH |
278 | struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q, |
279 | gfp_t gfp_mask); | |
7e5a8794 | 280 | void ioc_clear_queue(struct request_queue *q); |
f2dbd76a | 281 | |
24acfc34 | 282 | int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node); |
f2dbd76a | 283 | |
f2dbd76a TH |
284 | /* |
285 | * Internal throttling interface | |
286 | */ | |
bc9fcbf9 | 287 | #ifdef CONFIG_BLK_DEV_THROTTLING |
bc9fcbf9 TH |
288 | extern int blk_throtl_init(struct request_queue *q); |
289 | extern void blk_throtl_exit(struct request_queue *q); | |
d61fcfa4 | 290 | extern void blk_throtl_register_queue(struct request_queue *q); |
bc9fcbf9 | 291 | #else /* CONFIG_BLK_DEV_THROTTLING */ |
bc9fcbf9 TH |
292 | static inline int blk_throtl_init(struct request_queue *q) { return 0; } |
293 | static inline void blk_throtl_exit(struct request_queue *q) { } | |
d61fcfa4 | 294 | static inline void blk_throtl_register_queue(struct request_queue *q) { } |
bc9fcbf9 | 295 | #endif /* CONFIG_BLK_DEV_THROTTLING */ |
297e3d85 SL |
296 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
297 | extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page); | |
298 | extern ssize_t blk_throtl_sample_time_store(struct request_queue *q, | |
299 | const char *page, size_t count); | |
9e234eea | 300 | extern void blk_throtl_bio_endio(struct bio *bio); |
b9147dd1 | 301 | extern void blk_throtl_stat_add(struct request *rq, u64 time); |
9e234eea SL |
302 | #else |
303 | static inline void blk_throtl_bio_endio(struct bio *bio) { } | |
b9147dd1 | 304 | static inline void blk_throtl_stat_add(struct request *rq, u64 time) { } |
297e3d85 | 305 | #endif |
bc9fcbf9 | 306 | |
3bce016a CH |
307 | #ifdef CONFIG_BOUNCE |
308 | extern int init_emergency_isa_pool(void); | |
309 | extern void blk_queue_bounce(struct request_queue *q, struct bio **bio); | |
310 | #else | |
311 | static inline int init_emergency_isa_pool(void) | |
312 | { | |
313 | return 0; | |
314 | } | |
315 | static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio) | |
316 | { | |
317 | } | |
318 | #endif /* CONFIG_BOUNCE */ | |
319 | ||
d7067512 JB |
320 | #ifdef CONFIG_BLK_CGROUP_IOLATENCY |
321 | extern int blk_iolatency_init(struct request_queue *q); | |
322 | #else | |
323 | static inline int blk_iolatency_init(struct request_queue *q) { return 0; } | |
324 | #endif | |
325 | ||
a2d6b3a2 DLM |
326 | struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp); |
327 | ||
bf505456 DLM |
328 | #ifdef CONFIG_BLK_DEV_ZONED |
329 | void blk_queue_free_zone_bitmaps(struct request_queue *q); | |
330 | #else | |
331 | static inline void blk_queue_free_zone_bitmaps(struct request_queue *q) {} | |
332 | #endif | |
333 | ||
581e2600 CH |
334 | struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector); |
335 | ||
336 | int blk_alloc_devt(struct hd_struct *part, dev_t *devt); | |
337 | void blk_free_devt(dev_t devt); | |
338 | void blk_invalidate_devt(dev_t devt); | |
339 | char *disk_name(struct gendisk *hd, int partno, char *buf); | |
340 | #define ADDPART_FLAG_NONE 0 | |
341 | #define ADDPART_FLAG_RAID 1 | |
342 | #define ADDPART_FLAG_WHOLEDISK 2 | |
cddae808 | 343 | void delete_partition(struct gendisk *disk, struct hd_struct *part); |
fa9156ae CH |
344 | int bdev_add_partition(struct block_device *bdev, int partno, |
345 | sector_t start, sector_t length); | |
346 | int bdev_del_partition(struct block_device *bdev, int partno); | |
347 | int bdev_resize_partition(struct block_device *bdev, int partno, | |
348 | sector_t start, sector_t length); | |
581e2600 | 349 | int disk_expand_part_tbl(struct gendisk *disk, int target); |
8da2892e | 350 | int hd_ref_init(struct hd_struct *part); |
581e2600 | 351 | |
27eb3af9 | 352 | /* no need to get/put refcount of part0 */ |
581e2600 CH |
353 | static inline int hd_struct_try_get(struct hd_struct *part) |
354 | { | |
27eb3af9 ML |
355 | if (part->partno) |
356 | return percpu_ref_tryget_live(&part->ref); | |
357 | return 1; | |
581e2600 CH |
358 | } |
359 | ||
360 | static inline void hd_struct_put(struct hd_struct *part) | |
361 | { | |
27eb3af9 ML |
362 | if (part->partno) |
363 | percpu_ref_put(&part->ref); | |
581e2600 CH |
364 | } |
365 | ||
581e2600 CH |
366 | static inline void hd_free_part(struct hd_struct *part) |
367 | { | |
58d4f14f | 368 | free_percpu(part->dkstats); |
581e2600 CH |
369 | kfree(part->info); |
370 | percpu_ref_exit(&part->ref); | |
371 | } | |
372 | ||
373 | /* | |
374 | * Any access of part->nr_sects which is not protected by partition | |
375 | * bd_mutex or gendisk bdev bd_mutex, should be done using this | |
376 | * accessor function. | |
377 | * | |
378 | * Code written along the lines of i_size_read() and i_size_write(). | |
379 | * CONFIG_PREEMPTION case optimizes the case of UP kernel with preemption | |
380 | * on. | |
381 | */ | |
382 | static inline sector_t part_nr_sects_read(struct hd_struct *part) | |
383 | { | |
384 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) | |
385 | sector_t nr_sects; | |
386 | unsigned seq; | |
387 | do { | |
388 | seq = read_seqcount_begin(&part->nr_sects_seq); | |
389 | nr_sects = part->nr_sects; | |
390 | } while (read_seqcount_retry(&part->nr_sects_seq, seq)); | |
391 | return nr_sects; | |
392 | #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION) | |
393 | sector_t nr_sects; | |
394 | ||
395 | preempt_disable(); | |
396 | nr_sects = part->nr_sects; | |
397 | preempt_enable(); | |
398 | return nr_sects; | |
399 | #else | |
400 | return part->nr_sects; | |
401 | #endif | |
402 | } | |
403 | ||
404 | /* | |
405 | * Should be called with mutex lock held (typically bd_mutex) of partition | |
406 | * to provide mutual exlusion among writers otherwise seqcount might be | |
407 | * left in wrong state leaving the readers spinning infinitely. | |
408 | */ | |
409 | static inline void part_nr_sects_write(struct hd_struct *part, sector_t size) | |
410 | { | |
411 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) | |
15b81ce5 | 412 | preempt_disable(); |
581e2600 CH |
413 | write_seqcount_begin(&part->nr_sects_seq); |
414 | part->nr_sects = size; | |
415 | write_seqcount_end(&part->nr_sects_seq); | |
15b81ce5 | 416 | preempt_enable(); |
581e2600 CH |
417 | #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION) |
418 | preempt_disable(); | |
419 | part->nr_sects = size; | |
420 | preempt_enable(); | |
421 | #else | |
422 | part->nr_sects = size; | |
423 | #endif | |
424 | } | |
425 | ||
3d745ea5 CH |
426 | struct request_queue *__blk_alloc_queue(int node_id); |
427 | ||
e4581105 | 428 | int bio_add_hw_page(struct request_queue *q, struct bio *bio, |
130879f1 | 429 | struct page *page, unsigned int len, unsigned int offset, |
e4581105 | 430 | unsigned int max_sectors, bool *same_page); |
130879f1 | 431 | |
bc9fcbf9 | 432 | #endif /* BLK_INTERNAL_H */ |