1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef BLK_INTERNAL_H
3 #define BLK_INTERNAL_H
4
5 #include <linux/idr.h>
6 #include <linux/blk-mq.h>
7 #include <linux/part_stat.h>
8 #include <linux/blk-crypto.h>
9 #include <linux/memblock.h> /* for max_pfn/max_low_pfn */
10 #include <xen/xen.h>
11 #include "blk-crypto-internal.h"
12 #include "blk-mq.h"
13 #include "blk-mq-sched.h"
14
15 /* Max future timer expiry for timeouts */
16 #define BLK_MAX_TIMEOUT (5 * HZ)
17
18 extern struct dentry *blk_debugfs_root;
19 DECLARE_STATIC_KEY_FALSE(blk_sub_page_limits);
20
21 struct internal_request_queue {
22 struct request_queue q;
23 struct delayed_work requeue_work;
24 };
25
26 static inline struct internal_request_queue *
to_internal_q(struct request_queue * q)27 to_internal_q(struct request_queue *q)
28 {
29 return container_of(q, struct internal_request_queue, q);
30 }
31
32 struct blk_flush_queue {
33 unsigned int flush_pending_idx:1;
34 unsigned int flush_running_idx:1;
35 blk_status_t rq_status;
36 unsigned long flush_pending_since;
37 struct list_head flush_queue[2];
38 struct list_head flush_data_in_flight;
39 struct request *flush_rq;
40
41 spinlock_t mq_flush_lock;
42 };
43
44 extern struct kmem_cache *blk_requestq_cachep;
45 extern struct kobj_type blk_queue_ktype;
46 extern struct ida blk_queue_ida;
47
48 static inline struct blk_flush_queue *
blk_get_flush_queue(struct request_queue * q,struct blk_mq_ctx * ctx)49 blk_get_flush_queue(struct request_queue *q, struct blk_mq_ctx *ctx)
50 {
51 return blk_mq_map_queue(q, REQ_OP_FLUSH, ctx)->fq;
52 }
53
__blk_get_queue(struct request_queue * q)54 static inline void __blk_get_queue(struct request_queue *q)
55 {
56 kobject_get(&q->kobj);
57 }
58
59 bool is_flush_rq(struct request *req);
60
61 struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,
62 gfp_t flags);
63 void blk_free_flush_queue(struct blk_flush_queue *q);
64
blk_queue_sub_page_limits(const struct queue_limits * lim)65 static inline bool blk_queue_sub_page_limits(const struct queue_limits *lim)
66 {
67 return static_branch_unlikely(&blk_sub_page_limits) &&
68 lim->sub_page_limits;
69 }
70
71 int blk_sub_page_limit_queues_get(void *data, u64 *val);
72 void blk_disable_sub_page_limits(struct queue_limits *q);
73
74 void blk_freeze_queue(struct request_queue *q);
75 void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic);
76 void blk_queue_start_drain(struct request_queue *q);
77
78 #define BIO_INLINE_VECS 4
79 struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
80 gfp_t gfp_mask);
81 void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs);
82
83 /* Number of DMA segments required to transfer @bytes data. */
blk_segments(const struct queue_limits * limits,unsigned int bytes)84 static inline unsigned int blk_segments(const struct queue_limits *limits,
85 unsigned int bytes)
86 {
87 if (!blk_queue_sub_page_limits(limits))
88 return 1;
89
90 {
91 const unsigned int mss = limits->max_segment_size;
92
93 if (bytes <= mss)
94 return 1;
95 if (is_power_of_2(mss))
96 return round_up(bytes, mss) >> ilog2(mss);
97 return (bytes + mss - 1) / mss;
98 }
99 }
100
biovec_phys_mergeable(struct request_queue * q,struct bio_vec * vec1,struct bio_vec * vec2)101 static inline bool biovec_phys_mergeable(struct request_queue *q,
102 struct bio_vec *vec1, struct bio_vec *vec2)
103 {
104 unsigned long mask = queue_segment_boundary(q);
105 phys_addr_t addr1 = page_to_phys(vec1->bv_page) + vec1->bv_offset;
106 phys_addr_t addr2 = page_to_phys(vec2->bv_page) + vec2->bv_offset;
107
108 if (addr1 + vec1->bv_len != addr2)
109 return false;
110 if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page))
111 return false;
112 if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask))
113 return false;
114 return true;
115 }
116
__bvec_gap_to_prev(struct request_queue * q,struct bio_vec * bprv,unsigned int offset)117 static inline bool __bvec_gap_to_prev(struct request_queue *q,
118 struct bio_vec *bprv, unsigned int offset)
119 {
120 return (offset & queue_virt_boundary(q)) ||
121 ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q));
122 }
123
124 /*
125 * Check if adding a bio_vec after bprv with offset would create a gap in
126 * the SG list. Most drivers don't care about this, but some do.
127 */
bvec_gap_to_prev(struct request_queue * q,struct bio_vec * bprv,unsigned int offset)128 static inline bool bvec_gap_to_prev(struct request_queue *q,
129 struct bio_vec *bprv, unsigned int offset)
130 {
131 if (!queue_virt_boundary(q))
132 return false;
133 return __bvec_gap_to_prev(q, bprv, offset);
134 }
135
136 #ifdef CONFIG_BLK_DEV_INTEGRITY
137 void blk_flush_integrity(void);
138 bool __bio_integrity_endio(struct bio *);
139 void bio_integrity_free(struct bio *bio);
bio_integrity_endio(struct bio * bio)140 static inline bool bio_integrity_endio(struct bio *bio)
141 {
142 if (bio_integrity(bio))
143 return __bio_integrity_endio(bio);
144 return true;
145 }
146
147 bool blk_integrity_merge_rq(struct request_queue *, struct request *,
148 struct request *);
149 bool blk_integrity_merge_bio(struct request_queue *, struct request *,
150 struct bio *);
151
integrity_req_gap_back_merge(struct request * req,struct bio * next)152 static inline bool integrity_req_gap_back_merge(struct request *req,
153 struct bio *next)
154 {
155 struct bio_integrity_payload *bip = bio_integrity(req->bio);
156 struct bio_integrity_payload *bip_next = bio_integrity(next);
157
158 return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
159 bip_next->bip_vec[0].bv_offset);
160 }
161
integrity_req_gap_front_merge(struct request * req,struct bio * bio)162 static inline bool integrity_req_gap_front_merge(struct request *req,
163 struct bio *bio)
164 {
165 struct bio_integrity_payload *bip = bio_integrity(bio);
166 struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
167
168 return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
169 bip_next->bip_vec[0].bv_offset);
170 }
171
172 int blk_integrity_add(struct gendisk *disk);
173 void blk_integrity_del(struct gendisk *);
174 #else /* CONFIG_BLK_DEV_INTEGRITY */
blk_integrity_merge_rq(struct request_queue * rq,struct request * r1,struct request * r2)175 static inline bool blk_integrity_merge_rq(struct request_queue *rq,
176 struct request *r1, struct request *r2)
177 {
178 return true;
179 }
blk_integrity_merge_bio(struct request_queue * rq,struct request * r,struct bio * b)180 static inline bool blk_integrity_merge_bio(struct request_queue *rq,
181 struct request *r, struct bio *b)
182 {
183 return true;
184 }
integrity_req_gap_back_merge(struct request * req,struct bio * next)185 static inline bool integrity_req_gap_back_merge(struct request *req,
186 struct bio *next)
187 {
188 return false;
189 }
integrity_req_gap_front_merge(struct request * req,struct bio * bio)190 static inline bool integrity_req_gap_front_merge(struct request *req,
191 struct bio *bio)
192 {
193 return false;
194 }
195
blk_flush_integrity(void)196 static inline void blk_flush_integrity(void)
197 {
198 }
bio_integrity_endio(struct bio * bio)199 static inline bool bio_integrity_endio(struct bio *bio)
200 {
201 return true;
202 }
bio_integrity_free(struct bio * bio)203 static inline void bio_integrity_free(struct bio *bio)
204 {
205 }
blk_integrity_add(struct gendisk * disk)206 static inline int blk_integrity_add(struct gendisk *disk)
207 {
208 return 0;
209 }
blk_integrity_del(struct gendisk * disk)210 static inline void blk_integrity_del(struct gendisk *disk)
211 {
212 }
213 #endif /* CONFIG_BLK_DEV_INTEGRITY */
214
215 unsigned long blk_rq_timeout(unsigned long timeout);
216 void blk_add_timer(struct request *req);
217
218 bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
219 unsigned int nr_segs, struct request **same_queue_rq);
220 bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
221 struct bio *bio, unsigned int nr_segs);
222
223 void blk_account_io_start(struct request *req);
224 void blk_account_io_done(struct request *req, u64 now);
225
226 /*
227 * Plug flush limits
228 */
229 #define BLK_MAX_REQUEST_COUNT 32
230 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
231
232 /*
233 * Internal elevator interface
234 */
235 #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
236
237 void blk_insert_flush(struct request *rq);
238
239 int elevator_switch_mq(struct request_queue *q,
240 struct elevator_type *new_e);
241 void __elevator_exit(struct request_queue *, struct elevator_queue *);
242 int elv_register_queue(struct request_queue *q, bool uevent);
243 void elv_unregister_queue(struct request_queue *q);
244
elevator_exit(struct request_queue * q,struct elevator_queue * e)245 static inline void elevator_exit(struct request_queue *q,
246 struct elevator_queue *e)
247 {
248 lockdep_assert_held(&q->sysfs_lock);
249
250 blk_mq_sched_free_requests(q);
251 __elevator_exit(q, e);
252 }
253
254 ssize_t part_size_show(struct device *dev, struct device_attribute *attr,
255 char *buf);
256 ssize_t part_stat_show(struct device *dev, struct device_attribute *attr,
257 char *buf);
258 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
259 char *buf);
260 ssize_t part_fail_show(struct device *dev, struct device_attribute *attr,
261 char *buf);
262 ssize_t part_fail_store(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t count);
264 ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
265 ssize_t part_timeout_store(struct device *, struct device_attribute *,
266 const char *, size_t);
267
268 void __blk_queue_split(struct bio **bio, unsigned int *nr_segs);
269 int ll_back_merge_fn(struct request *req, struct bio *bio,
270 unsigned int nr_segs);
271 bool blk_attempt_req_merge(struct request_queue *q, struct request *rq,
272 struct request *next);
273 unsigned int blk_recalc_rq_segments(struct request *rq);
274 void blk_rq_set_mixed_merge(struct request *rq);
275 bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
276 enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
277
278 int blk_dev_init(void);
279
280 /*
281 * Contribute to IO statistics IFF:
282 *
283 * a) it's attached to a gendisk, and
284 * b) the queue had IO stats enabled when this request was started
285 */
blk_do_io_stat(struct request * rq)286 static inline bool blk_do_io_stat(struct request *rq)
287 {
288 return rq->rq_disk && (rq->rq_flags & RQF_IO_STAT);
289 }
290
req_set_nomerge(struct request_queue * q,struct request * req)291 static inline void req_set_nomerge(struct request_queue *q, struct request *req)
292 {
293 req->cmd_flags |= REQ_NOMERGE;
294 if (req == q->last_merge)
295 q->last_merge = NULL;
296 }
297
298 /*
299 * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
300 * is defined as 'unsigned int', meantime it has to aligned to with logical
301 * block size which is the minimum accepted unit by hardware.
302 */
bio_allowed_max_sectors(struct request_queue * q)303 static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
304 {
305 return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
306 }
307
308 /*
309 * The max bio size which is aligned to q->limits.discard_granularity. This
310 * is a hint to split large discard bio in generic block layer, then if device
311 * driver needs to split the discard bio into smaller ones, their bi_size can
312 * be very probably and easily aligned to discard_granularity of the device's
313 * queue.
314 */
bio_aligned_discard_max_sectors(struct request_queue * q)315 static inline unsigned int bio_aligned_discard_max_sectors(
316 struct request_queue *q)
317 {
318 return round_down(UINT_MAX, q->limits.discard_granularity) >>
319 SECTOR_SHIFT;
320 }
321
322 /*
323 * Internal io_context interface
324 */
325 void get_io_context(struct io_context *ioc);
326 struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
327 struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
328 gfp_t gfp_mask);
329 void ioc_clear_queue(struct request_queue *q);
330
331 int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
332
333 /*
334 * Internal throttling interface
335 */
336 #ifdef CONFIG_BLK_DEV_THROTTLING
337 extern int blk_throtl_init(struct request_queue *q);
338 extern void blk_throtl_exit(struct request_queue *q);
339 extern void blk_throtl_register_queue(struct request_queue *q);
340 extern void blk_throtl_charge_bio_split(struct bio *bio);
341 bool blk_throtl_bio(struct bio *bio);
342 #else /* CONFIG_BLK_DEV_THROTTLING */
blk_throtl_init(struct request_queue * q)343 static inline int blk_throtl_init(struct request_queue *q) { return 0; }
blk_throtl_exit(struct request_queue * q)344 static inline void blk_throtl_exit(struct request_queue *q) { }
blk_throtl_register_queue(struct request_queue * q)345 static inline void blk_throtl_register_queue(struct request_queue *q) { }
blk_throtl_charge_bio_split(struct bio * bio)346 static inline void blk_throtl_charge_bio_split(struct bio *bio) { }
blk_throtl_bio(struct bio * bio)347 static inline bool blk_throtl_bio(struct bio *bio) { return false; }
348 #endif /* CONFIG_BLK_DEV_THROTTLING */
349 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
350 extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
351 extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
352 const char *page, size_t count);
353 extern void blk_throtl_bio_endio(struct bio *bio);
354 extern void blk_throtl_stat_add(struct request *rq, u64 time);
355 #else
blk_throtl_bio_endio(struct bio * bio)356 static inline void blk_throtl_bio_endio(struct bio *bio) { }
blk_throtl_stat_add(struct request * rq,u64 time)357 static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
358 #endif
359
360 void __blk_queue_bounce(struct request_queue *q, struct bio **bio);
361
blk_queue_may_bounce(struct request_queue * q)362 static inline bool blk_queue_may_bounce(struct request_queue *q)
363 {
364 return IS_ENABLED(CONFIG_BOUNCE) &&
365 q->limits.bounce == BLK_BOUNCE_HIGH &&
366 max_low_pfn >= max_pfn;
367 }
368
blk_queue_bounce(struct request_queue * q,struct bio ** bio)369 static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
370 {
371 if (unlikely(blk_queue_may_bounce(q) && bio_has_data(*bio)))
372 __blk_queue_bounce(q, bio);
373 }
374
375 #ifdef CONFIG_BLK_CGROUP_IOLATENCY
376 extern int blk_iolatency_init(struct request_queue *q);
377 #else
blk_iolatency_init(struct request_queue * q)378 static inline int blk_iolatency_init(struct request_queue *q) { return 0; }
379 #endif
380
381 struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
382
383 #ifdef CONFIG_BLK_DEV_ZONED
384 void blk_queue_free_zone_bitmaps(struct request_queue *q);
385 void blk_queue_clear_zone_settings(struct request_queue *q);
386 #else
blk_queue_free_zone_bitmaps(struct request_queue * q)387 static inline void blk_queue_free_zone_bitmaps(struct request_queue *q) {}
blk_queue_clear_zone_settings(struct request_queue * q)388 static inline void blk_queue_clear_zone_settings(struct request_queue *q) {}
389 #endif
390
391 int blk_alloc_ext_minor(void);
392 void blk_free_ext_minor(unsigned int minor);
393 #define ADDPART_FLAG_NONE 0
394 #define ADDPART_FLAG_RAID 1
395 #define ADDPART_FLAG_WHOLEDISK 2
396 int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
397 sector_t length);
398 int bdev_del_partition(struct gendisk *disk, int partno);
399 int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
400 sector_t length);
401
402 int bio_add_hw_page(struct request_queue *q, struct bio *bio,
403 struct page *page, unsigned int len, unsigned int offset,
404 unsigned int max_sectors, bool *same_page);
405
406 struct request_queue *blk_alloc_queue(int node_id);
407
408 int disk_alloc_events(struct gendisk *disk);
409 void disk_add_events(struct gendisk *disk);
410 void disk_del_events(struct gendisk *disk);
411 void disk_release_events(struct gendisk *disk);
412 extern struct device_attribute dev_attr_events;
413 extern struct device_attribute dev_attr_events_async;
414 extern struct device_attribute dev_attr_events_poll_msecs;
415
bio_clear_hipri(struct bio * bio)416 static inline void bio_clear_hipri(struct bio *bio)
417 {
418 /* can't support alloc cache if we turn off polling */
419 bio_clear_flag(bio, BIO_PERCPU_CACHE);
420 bio->bi_opf &= ~REQ_HIPRI;
421 }
422
423 extern const struct address_space_operations def_blk_aops;
424
425 #endif /* BLK_INTERNAL_H */
426