]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - block/blk.h
block, bfq: limit sectors served with interactive weight raising
[mirror_ubuntu-eoan-kernel.git] / block / blk.h
CommitLineData
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
ML
6#include <linux/blk-mq.h>
7#include "blk-mq.h"
a73f730d 8
86db1e29
JA
9/* Amount of time in which a process may batch requests */
10#define BLK_BATCH_TIME (HZ/50UL)
11
12/* Number of requests a "batching" process may submit */
13#define BLK_BATCH_REQ 32
14
0d2602ca
JA
15/* Max future timer expiry for timeouts */
16#define BLK_MAX_TIMEOUT (5 * HZ)
17
18fbda91
OS
18#ifdef CONFIG_DEBUG_FS
19extern struct dentry *blk_debugfs_root;
20#endif
21
7c94e1c1
ML
22struct blk_flush_queue {
23 unsigned int flush_queue_delayed:1;
24 unsigned int flush_pending_idx:1;
25 unsigned int flush_running_idx:1;
26 unsigned long flush_pending_since;
27 struct list_head flush_queue[2];
28 struct list_head flush_data_in_flight;
29 struct request *flush_rq;
0048b483
ML
30
31 /*
32 * flush_rq shares tag with this rq, both can't be active
33 * at the same time
34 */
35 struct request *orig_rq;
7c94e1c1
ML
36 spinlock_t mq_flush_lock;
37};
38
8324aa91 39extern struct kmem_cache *blk_requestq_cachep;
320ae51f 40extern struct kmem_cache *request_cachep;
8324aa91 41extern struct kobj_type blk_queue_ktype;
a73f730d 42extern struct ida blk_queue_ida;
8324aa91 43
7c94e1c1 44static inline struct blk_flush_queue *blk_get_flush_queue(
e97c293c 45 struct request_queue *q, struct blk_mq_ctx *ctx)
7c94e1c1 46{
7d7e0f90
CH
47 if (q->mq_ops)
48 return blk_mq_map_queue(q, ctx->cpu)->fq;
49 return q->fq;
7c94e1c1
ML
50}
51
09ac46c4
TH
52static inline void __blk_get_queue(struct request_queue *q)
53{
54 kobject_get(&q->kobj);
55}
56
f70ced09
ML
57struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
58 int node, int cmd_size);
59void blk_free_flush_queue(struct blk_flush_queue *q);
f3552655 60
5b788ce3
TH
61int blk_init_rl(struct request_list *rl, struct request_queue *q,
62 gfp_t gfp_mask);
b425e504 63void blk_exit_rl(struct request_queue *q, struct request_list *rl);
86db1e29
JA
64void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
65 struct bio *bio);
d732580b
TH
66void blk_queue_bypass_start(struct request_queue *q);
67void blk_queue_bypass_end(struct request_queue *q);
8324aa91 68void __blk_queue_free_tags(struct request_queue *q);
3ef28e83
DW
69void blk_freeze_queue(struct request_queue *q);
70
71static inline void blk_queue_enter_live(struct request_queue *q)
72{
73 /*
74 * Given that running in generic_make_request() context
75 * guarantees that a live reference against q_usage_counter has
76 * been established, further references under that same context
77 * need not check that the queue has been frozen (marked dead).
78 */
79 percpu_ref_get(&q->q_usage_counter);
80}
8324aa91 81
5a48fc14
DW
82#ifdef CONFIG_BLK_DEV_INTEGRITY
83void blk_flush_integrity(void);
7c20f116
CH
84bool __bio_integrity_endio(struct bio *);
85static inline bool bio_integrity_endio(struct bio *bio)
86{
87 if (bio_integrity(bio))
88 return __bio_integrity_endio(bio);
89 return true;
90}
5a48fc14
DW
91#else
92static inline void blk_flush_integrity(void)
93{
94}
7c20f116
CH
95static inline bool bio_integrity_endio(struct bio *bio)
96{
97 return true;
98}
5a48fc14 99#endif
8324aa91 100
287922eb 101void blk_timeout_work(struct work_struct *work);
0d2602ca 102unsigned long blk_rq_timeout(unsigned long timeout);
87ee7b11 103void blk_add_timer(struct request *req);
242f9dcb 104void blk_delete_timer(struct request *);
242f9dcb 105
320ae51f
JA
106
107bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
108 struct bio *bio);
109bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
110 struct bio *bio);
1e739730
CH
111bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
112 struct bio *bio);
320ae51f 113bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
5b3f341f
SL
114 unsigned int *request_count,
115 struct request **same_queue_rq);
0809e3ac 116unsigned int blk_plug_queued_count(struct request_queue *q);
320ae51f
JA
117
118void blk_account_io_start(struct request *req, bool new_io);
119void blk_account_io_completion(struct request *req, unsigned int bytes);
120void blk_account_io_done(struct request *req);
121
242f9dcb
JA
122/*
123 * EH timer and IO completion will both attempt to 'grab' the request, make
e14575b3
JA
124 * sure that only one of them succeeds. Steal the bottom bit of the
125 * __deadline field for this.
242f9dcb
JA
126 */
127static inline int blk_mark_rq_complete(struct request *rq)
128{
e14575b3 129 return test_and_set_bit(0, &rq->__deadline);
242f9dcb
JA
130}
131
132static inline void blk_clear_rq_complete(struct request *rq)
133{
e14575b3
JA
134 clear_bit(0, &rq->__deadline);
135}
136
137static inline bool blk_rq_is_complete(struct request *rq)
138{
139 return test_bit(0, &rq->__deadline);
242f9dcb 140}
86db1e29 141
158dbda0
TH
142/*
143 * Internal elevator interface
144 */
e8064021 145#define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
158dbda0 146
ae1b1539 147void blk_insert_flush(struct request *rq);
dd831006 148
158dbda0
TH
149static inline void elv_activate_rq(struct request_queue *q, struct request *rq)
150{
151 struct elevator_queue *e = q->elevator;
152
c51ca6cf
JA
153 if (e->type->ops.sq.elevator_activate_req_fn)
154 e->type->ops.sq.elevator_activate_req_fn(q, rq);
158dbda0
TH
155}
156
157static inline void elv_deactivate_rq(struct request_queue *q, struct request *rq)
158{
159 struct elevator_queue *e = q->elevator;
160
c51ca6cf
JA
161 if (e->type->ops.sq.elevator_deactivate_req_fn)
162 e->type->ops.sq.elevator_deactivate_req_fn(q, rq);
158dbda0
TH
163}
164
807d4af2
CH
165struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
166
581d4e28
JA
167#ifdef CONFIG_FAIL_IO_TIMEOUT
168int blk_should_fake_timeout(struct request_queue *);
169ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
170ssize_t part_timeout_store(struct device *, struct device_attribute *,
171 const char *, size_t);
172#else
173static inline int blk_should_fake_timeout(struct request_queue *q)
174{
175 return 0;
176}
177#endif
178
d6d48196
JA
179int ll_back_merge_fn(struct request_queue *q, struct request *req,
180 struct bio *bio);
181int ll_front_merge_fn(struct request_queue *q, struct request *req,
182 struct bio *bio);
b973cb7e
JA
183struct request *attempt_back_merge(struct request_queue *q, struct request *rq);
184struct request *attempt_front_merge(struct request_queue *q, struct request *rq);
5e84ea3a
JA
185int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
186 struct request *next);
d6d48196 187void blk_recalc_rq_segments(struct request *rq);
80a761fd 188void blk_rq_set_mixed_merge(struct request *rq);
050c8ea8 189bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
34fe7c05 190enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
d6d48196 191
8324aa91
JA
192void blk_queue_congestion_threshold(struct request_queue *q);
193
ff88972c
AB
194int blk_dev_init(void);
195
f253b86b 196
8324aa91
JA
197/*
198 * Return the threshold (number of used requests) at which the queue is
199 * considered to be congested. It include a little hysteresis to keep the
200 * context switch rate down.
201 */
202static inline int queue_congestion_on_threshold(struct request_queue *q)
203{
204 return q->nr_congestion_on;
205}
206
207/*
208 * The threshold at which a queue is considered to be uncongested
209 */
210static inline int queue_congestion_off_threshold(struct request_queue *q)
211{
212 return q->nr_congestion_off;
213}
214
e3a2b3f9
JA
215extern int blk_update_nr_requests(struct request_queue *, unsigned int);
216
c2553b58
JA
217/*
218 * Contribute to IO statistics IFF:
219 *
220 * a) it's attached to a gendisk, and
221 * b) the queue had IO stats enabled when this request was started, and
e2a60da7 222 * c) it's a file system request
c2553b58 223 */
26308eab 224static inline int blk_do_io_stat(struct request *rq)
fb8ec18c 225{
33659ebb 226 return rq->rq_disk &&
e8064021 227 (rq->rq_flags & RQF_IO_STAT) &&
57292b58 228 !blk_rq_is_passthrough(rq);
fb8ec18c
JA
229}
230
6cf7677f
CH
231static inline void req_set_nomerge(struct request_queue *q, struct request *req)
232{
233 req->cmd_flags |= REQ_NOMERGE;
234 if (req == q->last_merge)
235 q->last_merge = NULL;
236}
237
0a72e7f4
JA
238/*
239 * Steal a bit from this field for legacy IO path atomic IO marking. Note that
240 * setting the deadline clears the bottom bit, potentially clearing the
241 * completed bit. The user has to be OK with this (current ones are fine).
242 */
243static inline void blk_rq_set_deadline(struct request *rq, unsigned long time)
244{
245 rq->__deadline = time & ~0x1UL;
246}
247
248static inline unsigned long blk_rq_deadline(struct request *rq)
249{
250 return rq->__deadline & ~0x1UL;
251}
252
f2dbd76a
TH
253/*
254 * Internal io_context interface
255 */
256void get_io_context(struct io_context *ioc);
47fdd4ca 257struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
24acfc34
TH
258struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
259 gfp_t gfp_mask);
7e5a8794 260void ioc_clear_queue(struct request_queue *q);
f2dbd76a 261
24acfc34 262int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
f2dbd76a 263
c23ecb42
JA
264/**
265 * rq_ioc - determine io_context for request allocation
266 * @bio: request being allocated is for this bio (can be %NULL)
267 *
268 * Determine io_context to use for request allocation for @bio. May return
269 * %NULL if %current->io_context doesn't exist.
270 */
271static inline struct io_context *rq_ioc(struct bio *bio)
272{
273#ifdef CONFIG_BLK_CGROUP
274 if (bio && bio->bi_ioc)
275 return bio->bi_ioc;
276#endif
277 return current->io_context;
278}
279
f2dbd76a
TH
280/**
281 * create_io_context - try to create task->io_context
f2dbd76a
TH
282 * @gfp_mask: allocation mask
283 * @node: allocation node
284 *
24acfc34
TH
285 * If %current->io_context is %NULL, allocate a new io_context and install
286 * it. Returns the current %current->io_context which may be %NULL if
287 * allocation failed.
f2dbd76a
TH
288 *
289 * Note that this function can't be called with IRQ disabled because
24acfc34 290 * task_lock which protects %current->io_context is IRQ-unsafe.
f2dbd76a 291 */
24acfc34 292static inline struct io_context *create_io_context(gfp_t gfp_mask, int node)
f2dbd76a
TH
293{
294 WARN_ON_ONCE(irqs_disabled());
24acfc34
TH
295 if (unlikely(!current->io_context))
296 create_task_io_context(current, gfp_mask, node);
297 return current->io_context;
f2dbd76a
TH
298}
299
300/*
301 * Internal throttling interface
302 */
bc9fcbf9 303#ifdef CONFIG_BLK_DEV_THROTTLING
c9a929dd 304extern void blk_throtl_drain(struct request_queue *q);
bc9fcbf9
TH
305extern int blk_throtl_init(struct request_queue *q);
306extern void blk_throtl_exit(struct request_queue *q);
d61fcfa4 307extern void blk_throtl_register_queue(struct request_queue *q);
bc9fcbf9 308#else /* CONFIG_BLK_DEV_THROTTLING */
c9a929dd 309static inline void blk_throtl_drain(struct request_queue *q) { }
bc9fcbf9
TH
310static inline int blk_throtl_init(struct request_queue *q) { return 0; }
311static inline void blk_throtl_exit(struct request_queue *q) { }
d61fcfa4 312static inline void blk_throtl_register_queue(struct request_queue *q) { }
bc9fcbf9 313#endif /* CONFIG_BLK_DEV_THROTTLING */
297e3d85
SL
314#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
315extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
316extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
317 const char *page, size_t count);
9e234eea 318extern void blk_throtl_bio_endio(struct bio *bio);
b9147dd1 319extern void blk_throtl_stat_add(struct request *rq, u64 time);
9e234eea
SL
320#else
321static inline void blk_throtl_bio_endio(struct bio *bio) { }
b9147dd1 322static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
297e3d85 323#endif
bc9fcbf9 324
3bce016a
CH
325#ifdef CONFIG_BOUNCE
326extern int init_emergency_isa_pool(void);
327extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
328#else
329static inline int init_emergency_isa_pool(void)
330{
331 return 0;
332}
333static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
334{
335}
336#endif /* CONFIG_BOUNCE */
337
bc9fcbf9 338#endif /* BLK_INTERNAL_H */