blob: 98fdf5a31fd66c80683cba3a5edf42ddcad6cf4d [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_BLKDEV_H
3#define _LINUX_BLKDEV_H
4
5#include <linux/sched.h>
6#include <linux/sched/clock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007#include <linux/major.h>
8#include <linux/genhd.h>
9#include <linux/list.h>
10#include <linux/llist.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020011#include <linux/minmax.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/timer.h>
13#include <linux/workqueue.h>
14#include <linux/pagemap.h>
15#include <linux/backing-dev-defs.h>
16#include <linux/wait.h>
17#include <linux/mempool.h>
18#include <linux/pfn.h>
19#include <linux/bio.h>
20#include <linux/stringify.h>
21#include <linux/gfp.h>
22#include <linux/bsg.h>
23#include <linux/smp.h>
24#include <linux/rcupdate.h>
25#include <linux/percpu-refcount.h>
26#include <linux/scatterlist.h>
27#include <linux/blkzoned.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020028#include <linux/pm.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029
30struct module;
31struct scsi_ioctl_command;
32
33struct request_queue;
34struct elevator_queue;
35struct blk_trace;
36struct request;
37struct sg_io_hdr;
38struct bsg_job;
39struct blkcg_gq;
40struct blk_flush_queue;
41struct pr_ops;
42struct rq_qos;
43struct blk_queue_stats;
44struct blk_stat_callback;
Olivier Deprez157378f2022-04-04 15:47:50 +020045struct blk_keyslot_manager;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046
47#define BLKDEV_MIN_RQ 4
48#define BLKDEV_MAX_RQ 128 /* Default maximum */
49
50/* Must be consistent with blk_mq_poll_stats_bkt() */
51#define BLK_MQ_POLL_STATS_BKTS 16
52
David Brazdil0f672f62019-12-10 10:32:29 +000053/* Doing classic polling */
54#define BLK_MQ_POLL_CLASSIC -1
55
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056/*
57 * Maximum number of blkcg policies allowed to be registered concurrently.
58 * Defined here to simplify include dependency.
59 */
60#define BLKCG_MAX_POLS 5
61
Olivier Deprez157378f2022-04-04 15:47:50 +020062static inline int blk_validate_block_size(unsigned int bsize)
63{
64 if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
65 return -EINVAL;
66
67 return 0;
68}
69
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070typedef void (rq_end_io_fn)(struct request *, blk_status_t);
71
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072/*
73 * request flags */
74typedef __u32 __bitwise req_flags_t;
75
76/* elevator knows about this request */
77#define RQF_SORTED ((__force req_flags_t)(1 << 0))
78/* drive already may have started this one */
79#define RQF_STARTED ((__force req_flags_t)(1 << 1))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080/* may not be passed by ioscheduler */
81#define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
82/* request for flush sequence */
83#define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
84/* merge of different types, fail separately */
85#define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
86/* track inflight for MQ */
87#define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
88/* don't call prep for this one */
89#define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090/* vaguely specified driver internal error. Ignored by the block layer */
91#define RQF_FAILED ((__force req_flags_t)(1 << 10))
92/* don't warn about errors */
93#define RQF_QUIET ((__force req_flags_t)(1 << 11))
94/* elevator private data attached */
95#define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
David Brazdil0f672f62019-12-10 10:32:29 +000096/* account into disk and partition IO statistics */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097#define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
98/* request came from our alloc pool */
99#define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
100/* runtime pm request */
101#define RQF_PM ((__force req_flags_t)(1 << 15))
102/* on IO scheduler merge hash */
103#define RQF_HASHED ((__force req_flags_t)(1 << 16))
David Brazdil0f672f62019-12-10 10:32:29 +0000104/* track IO completion time */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105#define RQF_STATS ((__force req_flags_t)(1 << 17))
106/* Look at ->special_vec for the actual data payload instead of the
107 bio chain. */
108#define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
109/* The per-zone write lock is held for this request */
110#define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
111/* already slept for hybrid poll */
112#define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
113/* ->timeout has been called, don't expire again */
114#define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
115
116/* flags that prevent us from merging requests: */
117#define RQF_NOMERGE_FLAGS \
118 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
119
120/*
121 * Request state for blk-mq.
122 */
123enum mq_rq_state {
124 MQ_RQ_IDLE = 0,
125 MQ_RQ_IN_FLIGHT = 1,
126 MQ_RQ_COMPLETE = 2,
127};
128
129/*
130 * Try to put the fields that are referenced together in the same cacheline.
131 *
132 * If you modify this structure, make sure to update blk_rq_init() and
133 * especially blk_mq_rq_ctx_init() to take care of the added fields.
134 */
135struct request {
136 struct request_queue *q;
137 struct blk_mq_ctx *mq_ctx;
David Brazdil0f672f62019-12-10 10:32:29 +0000138 struct blk_mq_hw_ctx *mq_hctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140 unsigned int cmd_flags; /* op and common flags */
141 req_flags_t rq_flags;
142
David Brazdil0f672f62019-12-10 10:32:29 +0000143 int tag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144 int internal_tag;
145
146 /* the following two fields are internal, NEVER access directly */
147 unsigned int __data_len; /* total data len */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 sector_t __sector; /* sector cursor */
149
150 struct bio *bio;
151 struct bio *biotail;
152
153 struct list_head queuelist;
154
155 /*
156 * The hash is used inside the scheduler, and killed once the
157 * request reaches the dispatch list. The ipi_list is only used
158 * to queue the request for softirq completion, which is long
159 * after the request has been unhashed (and even removed from
160 * the dispatch list).
161 */
162 union {
163 struct hlist_node hash; /* merge hash */
164 struct list_head ipi_list;
165 };
166
167 /*
168 * The rb_node is only used inside the io scheduler, requests
169 * are pruned when moved to the dispatch queue. So let the
170 * completion_data share space with the rb_node.
171 */
172 union {
173 struct rb_node rb_node; /* sort/lookup */
174 struct bio_vec special_vec;
175 void *completion_data;
176 int error_count; /* for legacy drivers, don't use */
177 };
178
179 /*
180 * Three pointers are available for the IO schedulers, if they need
181 * more they have to dynamically allocate it. Flush requests are
182 * never put on the IO scheduler. So let the flush fields share
183 * space with the elevator data.
184 */
185 union {
186 struct {
187 struct io_cq *icq;
188 void *priv[2];
189 } elv;
190
191 struct {
192 unsigned int seq;
193 struct list_head list;
194 rq_end_io_fn *saved_end_io;
195 } flush;
196 };
197
198 struct gendisk *rq_disk;
199 struct hd_struct *part;
David Brazdil0f672f62019-12-10 10:32:29 +0000200#ifdef CONFIG_BLK_RQ_ALLOC_TIME
201 /* Time that the first bio started allocating this request. */
202 u64 alloc_time_ns;
203#endif
204 /* Time that this request was allocated for this IO. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205 u64 start_time_ns;
206 /* Time that I/O was submitted to the device. */
207 u64 io_start_time_ns;
208
209#ifdef CONFIG_BLK_WBT
210 unsigned short wbt_flags;
211#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000212 /*
213 * rq sectors used for blk stats. It has the same value
214 * with blk_rq_sectors(rq), except that it never be zeroed
215 * by completion.
216 */
217 unsigned short stats_sectors;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218
219 /*
220 * Number of scatter-gather DMA addr+len pairs after
221 * physical address coalescing is performed.
222 */
223 unsigned short nr_phys_segments;
224
225#if defined(CONFIG_BLK_DEV_INTEGRITY)
226 unsigned short nr_integrity_segments;
227#endif
228
Olivier Deprez157378f2022-04-04 15:47:50 +0200229#ifdef CONFIG_BLK_INLINE_ENCRYPTION
230 struct bio_crypt_ctx *crypt_ctx;
231 struct blk_ksm_keyslot *crypt_keyslot;
232#endif
233
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 unsigned short write_hint;
235 unsigned short ioprio;
236
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237 enum mq_rq_state state;
238 refcount_t ref;
239
240 unsigned int timeout;
David Brazdil0f672f62019-12-10 10:32:29 +0000241 unsigned long deadline;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000242
243 union {
244 struct __call_single_data csd;
245 u64 fifo_time;
246 };
247
248 /*
249 * completion callback.
250 */
251 rq_end_io_fn *end_io;
252 void *end_io_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000253};
254
255static inline bool blk_op_is_scsi(unsigned int op)
256{
257 return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
258}
259
260static inline bool blk_op_is_private(unsigned int op)
261{
262 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
263}
264
265static inline bool blk_rq_is_scsi(struct request *rq)
266{
267 return blk_op_is_scsi(req_op(rq));
268}
269
270static inline bool blk_rq_is_private(struct request *rq)
271{
272 return blk_op_is_private(req_op(rq));
273}
274
275static inline bool blk_rq_is_passthrough(struct request *rq)
276{
277 return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
278}
279
280static inline bool bio_is_passthrough(struct bio *bio)
281{
282 unsigned op = bio_op(bio);
283
284 return blk_op_is_scsi(op) || blk_op_is_private(op);
285}
286
287static inline unsigned short req_get_ioprio(struct request *req)
288{
289 return req->ioprio;
290}
291
292#include <linux/elevator.h>
293
294struct blk_queue_ctx;
295
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296struct bio_vec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297
298enum blk_eh_timer_return {
299 BLK_EH_DONE, /* drivers has completed the command */
300 BLK_EH_RESET_TIMER, /* reset timer and try again */
301};
302
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303enum blk_queue_state {
304 Queue_down,
305 Queue_up,
306};
307
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308#define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
309#define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
310
311#define BLK_SCSI_MAX_CMDS (256)
312#define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
313
314/*
315 * Zoned block device models (zoned limit).
Olivier Deprez157378f2022-04-04 15:47:50 +0200316 *
317 * Note: This needs to be ordered from the least to the most severe
318 * restrictions for the inheritance in blk_stack_limits() to work.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000319 */
320enum blk_zoned_model {
Olivier Deprez157378f2022-04-04 15:47:50 +0200321 BLK_ZONED_NONE = 0, /* Regular block device */
322 BLK_ZONED_HA, /* Host-aware zoned block device */
323 BLK_ZONED_HM, /* Host-managed zoned block device */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324};
325
326struct queue_limits {
327 unsigned long bounce_pfn;
328 unsigned long seg_boundary_mask;
329 unsigned long virt_boundary_mask;
330
331 unsigned int max_hw_sectors;
332 unsigned int max_dev_sectors;
333 unsigned int chunk_sectors;
334 unsigned int max_sectors;
335 unsigned int max_segment_size;
336 unsigned int physical_block_size;
Olivier Deprez0e641232021-09-23 10:07:05 +0200337 unsigned int logical_block_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000338 unsigned int alignment_offset;
339 unsigned int io_min;
340 unsigned int io_opt;
341 unsigned int max_discard_sectors;
342 unsigned int max_hw_discard_sectors;
343 unsigned int max_write_same_sectors;
344 unsigned int max_write_zeroes_sectors;
Olivier Deprez157378f2022-04-04 15:47:50 +0200345 unsigned int max_zone_append_sectors;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000346 unsigned int discard_granularity;
347 unsigned int discard_alignment;
348
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 unsigned short max_segments;
350 unsigned short max_integrity_segments;
351 unsigned short max_discard_segments;
352
353 unsigned char misaligned;
354 unsigned char discard_misaligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000355 unsigned char raid_partial_stripes_expensive;
356 enum blk_zoned_model zoned;
357};
358
Olivier Deprez157378f2022-04-04 15:47:50 +0200359typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
360 void *data);
361
362void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model);
363
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364#ifdef CONFIG_BLK_DEV_ZONED
365
Olivier Deprez157378f2022-04-04 15:47:50 +0200366#define BLK_ALL_ZONES ((unsigned int)-1)
367int blkdev_report_zones(struct block_device *bdev, sector_t sector,
368 unsigned int nr_zones, report_zones_cb cb, void *data);
369unsigned int blkdev_nr_zones(struct gendisk *disk);
370extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
371 sector_t sectors, sector_t nr_sectors,
372 gfp_t gfp_mask);
373int blk_revalidate_disk_zones(struct gendisk *disk,
374 void (*update_driver_data)(struct gendisk *disk));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375
376extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
377 unsigned int cmd, unsigned long arg);
Olivier Deprez157378f2022-04-04 15:47:50 +0200378extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
379 unsigned int cmd, unsigned long arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380
381#else /* CONFIG_BLK_DEV_ZONED */
382
Olivier Deprez157378f2022-04-04 15:47:50 +0200383static inline unsigned int blkdev_nr_zones(struct gendisk *disk)
David Brazdil0f672f62019-12-10 10:32:29 +0000384{
385 return 0;
386}
387
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
389 fmode_t mode, unsigned int cmd,
390 unsigned long arg)
391{
392 return -ENOTTY;
393}
394
Olivier Deprez157378f2022-04-04 15:47:50 +0200395static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
396 fmode_t mode, unsigned int cmd,
397 unsigned long arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000398{
399 return -ENOTTY;
400}
401
402#endif /* CONFIG_BLK_DEV_ZONED */
403
404struct request_queue {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000405 struct request *last_merge;
406 struct elevator_queue *elevator;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000407
Olivier Deprez157378f2022-04-04 15:47:50 +0200408 struct percpu_ref q_usage_counter;
409
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000410 struct blk_queue_stats *stats;
411 struct rq_qos *rq_qos;
412
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000413 const struct blk_mq_ops *mq_ops;
414
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415 /* sw queues */
416 struct blk_mq_ctx __percpu *queue_ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417
418 unsigned int queue_depth;
419
420 /* hw dispatch queues */
421 struct blk_mq_hw_ctx **queue_hw_ctx;
422 unsigned int nr_hw_queues;
423
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424 struct backing_dev_info *backing_dev_info;
425
426 /*
427 * The queue owner gets to use this for whatever they like.
428 * ll_rw_blk doesn't touch it.
429 */
430 void *queuedata;
431
432 /*
433 * various queue flags, see QUEUE_* below
434 */
435 unsigned long queue_flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000436 /*
437 * Number of contexts that have called blk_set_pm_only(). If this
Olivier Deprez157378f2022-04-04 15:47:50 +0200438 * counter is above zero then only RQF_PM requests are processed.
David Brazdil0f672f62019-12-10 10:32:29 +0000439 */
440 atomic_t pm_only;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441
442 /*
443 * ida allocated id for this queue. Used to index queues from
444 * ioctx.
445 */
446 int id;
447
448 /*
449 * queue needs bounce pages for pages above this limit
450 */
451 gfp_t bounce_gfp;
452
David Brazdil0f672f62019-12-10 10:32:29 +0000453 spinlock_t queue_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454
455 /*
456 * queue kobject
457 */
458 struct kobject kobj;
459
460 /*
461 * mq queue kobject
462 */
David Brazdil0f672f62019-12-10 10:32:29 +0000463 struct kobject *mq_kobj;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464
465#ifdef CONFIG_BLK_DEV_INTEGRITY
466 struct blk_integrity integrity;
467#endif /* CONFIG_BLK_DEV_INTEGRITY */
468
469#ifdef CONFIG_PM
470 struct device *dev;
Olivier Deprez157378f2022-04-04 15:47:50 +0200471 enum rpm_status rpm_status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472 unsigned int nr_pending;
473#endif
474
475 /*
476 * queue settings
477 */
478 unsigned long nr_requests; /* Max # of requests */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000480 unsigned int dma_pad_mask;
481 unsigned int dma_alignment;
482
Olivier Deprez157378f2022-04-04 15:47:50 +0200483#ifdef CONFIG_BLK_INLINE_ENCRYPTION
484 /* Inline crypto capabilities */
485 struct blk_keyslot_manager *ksm;
486#endif
487
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000488 unsigned int rq_timeout;
489 int poll_nsec;
490
491 struct blk_stat_callback *poll_cb;
492 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
493
494 struct timer_list timeout;
495 struct work_struct timeout_work;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000496
Olivier Deprez157378f2022-04-04 15:47:50 +0200497 atomic_t nr_active_requests_shared_sbitmap;
498
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499 struct list_head icq_list;
500#ifdef CONFIG_BLK_CGROUP
501 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
502 struct blkcg_gq *root_blkg;
503 struct list_head blkg_list;
504#endif
505
506 struct queue_limits limits;
507
David Brazdil0f672f62019-12-10 10:32:29 +0000508 unsigned int required_elevator_features;
509
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510#ifdef CONFIG_BLK_DEV_ZONED
511 /*
512 * Zoned block device information for request dispatch control.
513 * nr_zones is the total number of zones of the device. This is always
Olivier Deprez157378f2022-04-04 15:47:50 +0200514 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
515 * bits which indicates if a zone is conventional (bit set) or
516 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 * bits which indicates if a zone is write locked, that is, if a write
518 * request targeting the zone was dispatched. All three fields are
519 * initialized by the low level device driver (e.g. scsi/sd.c).
520 * Stacking drivers (device mappers) may or may not initialize
521 * these fields.
522 *
523 * Reads of this information must be protected with blk_queue_enter() /
524 * blk_queue_exit(). Modifying this information is only allowed while
525 * no requests are being processed. See also blk_mq_freeze_queue() and
526 * blk_mq_unfreeze_queue().
527 */
528 unsigned int nr_zones;
Olivier Deprez157378f2022-04-04 15:47:50 +0200529 unsigned long *conv_zones_bitmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000530 unsigned long *seq_zones_wlock;
Olivier Deprez157378f2022-04-04 15:47:50 +0200531 unsigned int max_open_zones;
532 unsigned int max_active_zones;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533#endif /* CONFIG_BLK_DEV_ZONED */
534
535 /*
536 * sg stuff
537 */
538 unsigned int sg_timeout;
539 unsigned int sg_reserved_size;
540 int node;
Olivier Deprez157378f2022-04-04 15:47:50 +0200541 struct mutex debugfs_mutex;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542#ifdef CONFIG_BLK_DEV_IO_TRACE
Olivier Deprez0e641232021-09-23 10:07:05 +0200543 struct blk_trace __rcu *blk_trace;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544#endif
545 /*
546 * for flush operations
547 */
548 struct blk_flush_queue *fq;
549
550 struct list_head requeue_list;
551 spinlock_t requeue_lock;
552 struct delayed_work requeue_work;
553
554 struct mutex sysfs_lock;
David Brazdil0f672f62019-12-10 10:32:29 +0000555 struct mutex sysfs_dir_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000556
David Brazdil0f672f62019-12-10 10:32:29 +0000557 /*
558 * for reusing dead hctx instance in case of updating
559 * nr_hw_queues
560 */
561 struct list_head unused_hctx_list;
562 spinlock_t unused_hctx_lock;
563
564 int mq_freeze_depth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565
566#if defined(CONFIG_BLK_DEV_BSG)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567 struct bsg_class_device bsg_dev;
568#endif
569
570#ifdef CONFIG_BLK_DEV_THROTTLING
571 /* Throttle data */
572 struct throtl_data *td;
573#endif
574 struct rcu_head rcu_head;
575 wait_queue_head_t mq_freeze_wq;
David Brazdil0f672f62019-12-10 10:32:29 +0000576 /*
577 * Protect concurrent access to q_usage_counter by
578 * percpu_ref_kill() and percpu_ref_reinit().
579 */
580 struct mutex mq_freeze_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581
582 struct blk_mq_tag_set *tag_set;
583 struct list_head tag_set_list;
584 struct bio_set bio_split;
585
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000586 struct dentry *debugfs_dir;
Olivier Deprez157378f2022-04-04 15:47:50 +0200587
588#ifdef CONFIG_BLK_DEBUG_FS
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589 struct dentry *sched_debugfs_dir;
David Brazdil0f672f62019-12-10 10:32:29 +0000590 struct dentry *rqos_debugfs_dir;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591#endif
592
593 bool mq_sysfs_init_done;
594
595 size_t cmd_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000596
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597#define BLK_MAX_WRITE_HINTS 5
598 u64 write_hints[BLK_MAX_WRITE_HINTS];
599};
600
Olivier Deprez0e641232021-09-23 10:07:05 +0200601/* Keep blk_queue_flag_name[] in sync with the definitions below */
David Brazdil0f672f62019-12-10 10:32:29 +0000602#define QUEUE_FLAG_STOPPED 0 /* queue is stopped */
603#define QUEUE_FLAG_DYING 1 /* queue being torn down */
604#define QUEUE_FLAG_NOMERGES 3 /* disable merge attempts */
605#define QUEUE_FLAG_SAME_COMP 4 /* complete on same CPU-group */
606#define QUEUE_FLAG_FAIL_IO 5 /* fake timeout */
607#define QUEUE_FLAG_NONROT 6 /* non-rotational device (SSD) */
608#define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
609#define QUEUE_FLAG_IO_STAT 7 /* do disk/partitions IO accounting */
610#define QUEUE_FLAG_DISCARD 8 /* supports DISCARD */
611#define QUEUE_FLAG_NOXMERGES 9 /* No extended merges */
612#define QUEUE_FLAG_ADD_RANDOM 10 /* Contributes to random pool */
613#define QUEUE_FLAG_SECERASE 11 /* supports secure erase */
614#define QUEUE_FLAG_SAME_FORCE 12 /* force complete on same CPU */
615#define QUEUE_FLAG_DEAD 13 /* queue tear-down finished */
616#define QUEUE_FLAG_INIT_DONE 14 /* queue is initialized */
Olivier Deprez157378f2022-04-04 15:47:50 +0200617#define QUEUE_FLAG_STABLE_WRITES 15 /* don't modify blks until WB is done */
David Brazdil0f672f62019-12-10 10:32:29 +0000618#define QUEUE_FLAG_POLL 16 /* IO polling enabled if set */
619#define QUEUE_FLAG_WC 17 /* Write back caching */
620#define QUEUE_FLAG_FUA 18 /* device supports FUA writes */
621#define QUEUE_FLAG_DAX 19 /* device supports DAX */
622#define QUEUE_FLAG_STATS 20 /* track IO start and completion times */
623#define QUEUE_FLAG_POLL_STATS 21 /* collecting stats for hybrid polling */
624#define QUEUE_FLAG_REGISTERED 22 /* queue has been registered to a disk */
625#define QUEUE_FLAG_SCSI_PASSTHROUGH 23 /* queue supports SCSI commands */
626#define QUEUE_FLAG_QUIESCED 24 /* queue has been quiesced */
627#define QUEUE_FLAG_PCI_P2PDMA 25 /* device supports PCI p2p requests */
628#define QUEUE_FLAG_ZONE_RESETALL 26 /* supports Zone Reset All */
629#define QUEUE_FLAG_RQ_ALLOC_TIME 27 /* record rq->alloc_time_ns */
Olivier Deprez157378f2022-04-04 15:47:50 +0200630#define QUEUE_FLAG_HCTX_ACTIVE 28 /* at least one blk-mq hctx is active */
631#define QUEUE_FLAG_NOWAIT 29 /* device supports NOWAIT */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000632
633#define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
Olivier Deprez157378f2022-04-04 15:47:50 +0200634 (1 << QUEUE_FLAG_SAME_COMP) | \
635 (1 << QUEUE_FLAG_NOWAIT))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000636
637void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
638void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
639bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000640
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000641#define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
642#define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
643#define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644#define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
645#define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
646#define blk_queue_noxmerges(q) \
647 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
648#define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
Olivier Deprez157378f2022-04-04 15:47:50 +0200649#define blk_queue_stable_writes(q) \
650 test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651#define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
652#define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
653#define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
David Brazdil0f672f62019-12-10 10:32:29 +0000654#define blk_queue_zone_resetall(q) \
655 test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656#define blk_queue_secure_erase(q) \
657 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
658#define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
659#define blk_queue_scsi_passthrough(q) \
660 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
David Brazdil0f672f62019-12-10 10:32:29 +0000661#define blk_queue_pci_p2pdma(q) \
662 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
663#ifdef CONFIG_BLK_RQ_ALLOC_TIME
664#define blk_queue_rq_alloc_time(q) \
665 test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
666#else
667#define blk_queue_rq_alloc_time(q) false
668#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000669
670#define blk_noretry_request(rq) \
671 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
672 REQ_FAILFAST_DRIVER))
673#define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
David Brazdil0f672f62019-12-10 10:32:29 +0000674#define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675#define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
David Brazdil0f672f62019-12-10 10:32:29 +0000676#define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
Olivier Deprez157378f2022-04-04 15:47:50 +0200677#define blk_queue_nowait(q) test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678
David Brazdil0f672f62019-12-10 10:32:29 +0000679extern void blk_set_pm_only(struct request_queue *q);
680extern void blk_clear_pm_only(struct request_queue *q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681
682static inline bool blk_account_rq(struct request *rq)
683{
684 return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
685}
686
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000687#define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
688
689#define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
690
David Brazdil0f672f62019-12-10 10:32:29 +0000691#define rq_dma_dir(rq) \
692 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693
David Brazdil0f672f62019-12-10 10:32:29 +0000694#define dma_map_bvec(dev, bv, dir, attrs) \
695 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
696 (dir), (attrs))
697
698static inline bool queue_is_mq(struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000699{
David Brazdil0f672f62019-12-10 10:32:29 +0000700 return q->mq_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701}
702
Olivier Deprez157378f2022-04-04 15:47:50 +0200703#ifdef CONFIG_PM
704static inline enum rpm_status queue_rpm_status(struct request_queue *q)
705{
706 return q->rpm_status;
707}
708#else
709static inline enum rpm_status queue_rpm_status(struct request_queue *q)
710{
711 return RPM_ACTIVE;
712}
713#endif
714
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000715static inline enum blk_zoned_model
716blk_queue_zoned_model(struct request_queue *q)
717{
Olivier Deprez157378f2022-04-04 15:47:50 +0200718 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
719 return q->limits.zoned;
720 return BLK_ZONED_NONE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721}
722
723static inline bool blk_queue_is_zoned(struct request_queue *q)
724{
725 switch (blk_queue_zoned_model(q)) {
726 case BLK_ZONED_HA:
727 case BLK_ZONED_HM:
728 return true;
729 default:
730 return false;
731 }
732}
733
David Brazdil0f672f62019-12-10 10:32:29 +0000734static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000735{
736 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
737}
738
739#ifdef CONFIG_BLK_DEV_ZONED
David Brazdil0f672f62019-12-10 10:32:29 +0000740static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
741{
742 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
743}
744
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745static inline unsigned int blk_queue_zone_no(struct request_queue *q,
746 sector_t sector)
747{
748 if (!blk_queue_is_zoned(q))
749 return 0;
750 return sector >> ilog2(q->limits.chunk_sectors);
751}
752
753static inline bool blk_queue_zone_is_seq(struct request_queue *q,
754 sector_t sector)
755{
Olivier Deprez157378f2022-04-04 15:47:50 +0200756 if (!blk_queue_is_zoned(q))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757 return false;
Olivier Deprez157378f2022-04-04 15:47:50 +0200758 if (!q->conv_zones_bitmap)
759 return true;
760 return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
761}
762
763static inline void blk_queue_max_open_zones(struct request_queue *q,
764 unsigned int max_open_zones)
765{
766 q->max_open_zones = max_open_zones;
767}
768
769static inline unsigned int queue_max_open_zones(const struct request_queue *q)
770{
771 return q->max_open_zones;
772}
773
774static inline void blk_queue_max_active_zones(struct request_queue *q,
775 unsigned int max_active_zones)
776{
777 q->max_active_zones = max_active_zones;
778}
779
780static inline unsigned int queue_max_active_zones(const struct request_queue *q)
781{
782 return q->max_active_zones;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000783}
David Brazdil0f672f62019-12-10 10:32:29 +0000784#else /* CONFIG_BLK_DEV_ZONED */
785static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
786{
787 return 0;
788}
Olivier Deprez157378f2022-04-04 15:47:50 +0200789static inline bool blk_queue_zone_is_seq(struct request_queue *q,
790 sector_t sector)
791{
792 return false;
793}
794static inline unsigned int blk_queue_zone_no(struct request_queue *q,
795 sector_t sector)
796{
797 return 0;
798}
799static inline unsigned int queue_max_open_zones(const struct request_queue *q)
800{
801 return 0;
802}
803static inline unsigned int queue_max_active_zones(const struct request_queue *q)
804{
805 return 0;
806}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000807#endif /* CONFIG_BLK_DEV_ZONED */
808
809static inline bool rq_is_sync(struct request *rq)
810{
811 return op_is_sync(rq->cmd_flags);
812}
813
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000814static inline bool rq_mergeable(struct request *rq)
815{
816 if (blk_rq_is_passthrough(rq))
817 return false;
818
819 if (req_op(rq) == REQ_OP_FLUSH)
820 return false;
821
822 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
823 return false;
824
Olivier Deprez157378f2022-04-04 15:47:50 +0200825 if (req_op(rq) == REQ_OP_ZONE_APPEND)
826 return false;
827
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
829 return false;
830 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
831 return false;
832
833 return true;
834}
835
836static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
837{
838 if (bio_page(a) == bio_page(b) &&
839 bio_offset(a) == bio_offset(b))
840 return true;
841
842 return false;
843}
844
845static inline unsigned int blk_queue_depth(struct request_queue *q)
846{
847 if (q->queue_depth)
848 return q->queue_depth;
849
850 return q->nr_requests;
851}
852
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000853extern unsigned long blk_max_low_pfn, blk_max_pfn;
854
855/*
856 * standard bounce addresses:
857 *
858 * BLK_BOUNCE_HIGH : bounce all highmem pages
859 * BLK_BOUNCE_ANY : don't bounce anything
860 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
861 */
862
863#if BITS_PER_LONG == 32
864#define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
865#else
866#define BLK_BOUNCE_HIGH -1ULL
867#endif
868#define BLK_BOUNCE_ANY (-1ULL)
869#define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
870
871/*
872 * default timeout for SG_IO if none specified
873 */
874#define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
875#define BLK_MIN_SG_TIMEOUT (7 * HZ)
876
877struct rq_map_data {
878 struct page **pages;
879 int page_order;
880 int nr_entries;
881 unsigned long offset;
882 int null_mapped;
883 int from_user;
884};
885
886struct req_iterator {
887 struct bvec_iter iter;
888 struct bio *bio;
889};
890
891/* This should not be used directly - use rq_for_each_segment */
892#define for_each_bio(_bio) \
893 for (; _bio; _bio = _bio->bi_next)
894#define __rq_for_each_bio(_bio, rq) \
895 if ((rq->bio)) \
896 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
897
898#define rq_for_each_segment(bvl, _rq, _iter) \
899 __rq_for_each_bio(_iter.bio, _rq) \
900 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
901
David Brazdil0f672f62019-12-10 10:32:29 +0000902#define rq_for_each_bvec(bvl, _rq, _iter) \
903 __rq_for_each_bio(_iter.bio, _rq) \
904 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
905
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000906#define rq_iter_last(bvec, _iter) \
907 (_iter.bio->bi_next == NULL && \
908 bio_iter_last(bvec, _iter.iter))
909
910#ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
911# error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
912#endif
913#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
914extern void rq_flush_dcache_pages(struct request *rq);
915#else
916static inline void rq_flush_dcache_pages(struct request *rq)
917{
918}
919#endif
920
921extern int blk_register_queue(struct gendisk *disk);
922extern void blk_unregister_queue(struct gendisk *disk);
Olivier Deprez157378f2022-04-04 15:47:50 +0200923blk_qc_t submit_bio_noacct(struct bio *bio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924extern void blk_rq_init(struct request_queue *q, struct request *rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925extern void blk_put_request(struct request *);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926extern struct request *blk_get_request(struct request_queue *, unsigned int op,
927 blk_mq_req_flags_t flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928extern int blk_lld_busy(struct request_queue *q);
929extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
930 struct bio_set *bs, gfp_t gfp_mask,
931 int (*bio_ctr)(struct bio *, struct bio *, void *),
932 void *data);
933extern void blk_rq_unprep_clone(struct request *rq);
934extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
935 struct request *rq);
936extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
Olivier Deprez157378f2022-04-04 15:47:50 +0200937extern void blk_queue_split(struct bio **);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000938extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
939extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
940 unsigned int, void __user *);
941extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
942 unsigned int, void __user *);
943extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
944 struct scsi_ioctl_command __user *);
Olivier Deprez157378f2022-04-04 15:47:50 +0200945extern int get_sg_io_hdr(struct sg_io_hdr *hdr, const void __user *argp);
946extern int put_sg_io_hdr(const struct sg_io_hdr *hdr, void __user *argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000947
948extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
949extern void blk_queue_exit(struct request_queue *q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000950extern void blk_sync_queue(struct request_queue *q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951extern int blk_rq_map_user(struct request_queue *, struct request *,
952 struct rq_map_data *, void __user *, unsigned long,
953 gfp_t);
954extern int blk_rq_unmap_user(struct bio *);
955extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
956extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
957 struct rq_map_data *, const struct iov_iter *,
958 gfp_t);
959extern void blk_execute_rq(struct request_queue *, struct gendisk *,
960 struct request *, int);
961extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
962 struct request *, int, rq_end_io_fn *);
963
David Brazdil0f672f62019-12-10 10:32:29 +0000964/* Helper to convert REQ_OP_XXX to its string format XXX */
965extern const char *blk_op_str(unsigned int op);
966
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000967int blk_status_to_errno(blk_status_t status);
968blk_status_t errno_to_blk_status(int errno);
969
David Brazdil0f672f62019-12-10 10:32:29 +0000970int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000971
972static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
973{
974 return bdev->bd_disk->queue; /* this is never NULL */
975}
976
977/*
978 * The basic unit of block I/O is a sector. It is used in a number of contexts
979 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
980 * bytes. Variables of type sector_t represent an offset or size that is a
981 * multiple of 512 bytes. Hence these two constants.
982 */
983#ifndef SECTOR_SHIFT
984#define SECTOR_SHIFT 9
985#endif
986#ifndef SECTOR_SIZE
987#define SECTOR_SIZE (1 << SECTOR_SHIFT)
988#endif
989
990/*
991 * blk_rq_pos() : the current sector
992 * blk_rq_bytes() : bytes left in the entire request
993 * blk_rq_cur_bytes() : bytes left in the current segment
994 * blk_rq_err_bytes() : bytes left till the next error boundary
995 * blk_rq_sectors() : sectors left in the entire request
996 * blk_rq_cur_sectors() : sectors left in the current segment
David Brazdil0f672f62019-12-10 10:32:29 +0000997 * blk_rq_stats_sectors() : sectors of the entire request used for stats
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998 */
999static inline sector_t blk_rq_pos(const struct request *rq)
1000{
1001 return rq->__sector;
1002}
1003
1004static inline unsigned int blk_rq_bytes(const struct request *rq)
1005{
1006 return rq->__data_len;
1007}
1008
1009static inline int blk_rq_cur_bytes(const struct request *rq)
1010{
1011 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
1012}
1013
1014extern unsigned int blk_rq_err_bytes(const struct request *rq);
1015
1016static inline unsigned int blk_rq_sectors(const struct request *rq)
1017{
1018 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1019}
1020
1021static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1022{
1023 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1024}
1025
David Brazdil0f672f62019-12-10 10:32:29 +00001026static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
1027{
1028 return rq->stats_sectors;
1029}
1030
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031#ifdef CONFIG_BLK_DEV_ZONED
Olivier Deprez157378f2022-04-04 15:47:50 +02001032
1033/* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
1034const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
1035
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036static inline unsigned int blk_rq_zone_no(struct request *rq)
1037{
1038 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
1039}
1040
1041static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
1042{
1043 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
1044}
1045#endif /* CONFIG_BLK_DEV_ZONED */
1046
1047/*
1048 * Some commands like WRITE SAME have a payload or data transfer size which
1049 * is different from the size of the request. Any driver that supports such
1050 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1051 * calculate the data transfer size.
1052 */
1053static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1054{
1055 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1056 return rq->special_vec.bv_len;
1057 return blk_rq_bytes(rq);
1058}
1059
David Brazdil0f672f62019-12-10 10:32:29 +00001060/*
1061 * Return the first full biovec in the request. The caller needs to check that
1062 * there are any bvecs before calling this helper.
1063 */
1064static inline struct bio_vec req_bvec(struct request *rq)
1065{
1066 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1067 return rq->special_vec;
1068 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1069}
1070
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1072 int op)
1073{
1074 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1075 return min(q->limits.max_discard_sectors,
1076 UINT_MAX >> SECTOR_SHIFT);
1077
1078 if (unlikely(op == REQ_OP_WRITE_SAME))
1079 return q->limits.max_write_same_sectors;
1080
1081 if (unlikely(op == REQ_OP_WRITE_ZEROES))
1082 return q->limits.max_write_zeroes_sectors;
1083
1084 return q->limits.max_sectors;
1085}
1086
1087/*
1088 * Return maximum size of a request at given offset. Only valid for
1089 * file system requests.
1090 */
1091static inline unsigned int blk_max_size_offset(struct request_queue *q,
Olivier Deprez157378f2022-04-04 15:47:50 +02001092 sector_t offset,
1093 unsigned int chunk_sectors)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001094{
Olivier Deprez157378f2022-04-04 15:47:50 +02001095 if (!chunk_sectors) {
1096 if (q->limits.chunk_sectors)
1097 chunk_sectors = q->limits.chunk_sectors;
1098 else
1099 return q->limits.max_sectors;
1100 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101
Olivier Deprez157378f2022-04-04 15:47:50 +02001102 if (likely(is_power_of_2(chunk_sectors)))
1103 chunk_sectors -= offset & (chunk_sectors - 1);
1104 else
1105 chunk_sectors -= sector_div(offset, chunk_sectors);
1106
1107 return min(q->limits.max_sectors, chunk_sectors);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108}
1109
1110static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1111 sector_t offset)
1112{
1113 struct request_queue *q = rq->q;
1114
1115 if (blk_rq_is_passthrough(rq))
1116 return q->limits.max_hw_sectors;
1117
1118 if (!q->limits.chunk_sectors ||
1119 req_op(rq) == REQ_OP_DISCARD ||
1120 req_op(rq) == REQ_OP_SECURE_ERASE)
1121 return blk_queue_get_max_sectors(q, req_op(rq));
1122
Olivier Deprez157378f2022-04-04 15:47:50 +02001123 return min(blk_max_size_offset(q, offset, 0),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001124 blk_queue_get_max_sectors(q, req_op(rq)));
1125}
1126
1127static inline unsigned int blk_rq_count_bios(struct request *rq)
1128{
1129 unsigned int nr_bios = 0;
1130 struct bio *bio;
1131
1132 __rq_for_each_bio(bio, rq)
1133 nr_bios++;
1134
1135 return nr_bios;
1136}
1137
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001138void blk_steal_bios(struct bio_list *list, struct request *rq);
1139
1140/*
1141 * Request completion related functions.
1142 *
1143 * blk_update_request() completes given number of bytes and updates
1144 * the request without completing it.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001145 */
1146extern bool blk_update_request(struct request *rq, blk_status_t error,
1147 unsigned int nr_bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001149extern void blk_abort_request(struct request *);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001150
1151/*
1152 * Access functions for manipulating queue properties
1153 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001154extern void blk_cleanup_queue(struct request_queue *);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001155extern void blk_queue_bounce_limit(struct request_queue *, u64);
1156extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1157extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1158extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1159extern void blk_queue_max_discard_segments(struct request_queue *,
1160 unsigned short);
1161extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1162extern void blk_queue_max_discard_sectors(struct request_queue *q,
1163 unsigned int max_discard_sectors);
1164extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1165 unsigned int max_write_same_sectors);
1166extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1167 unsigned int max_write_same_sectors);
Olivier Deprez0e641232021-09-23 10:07:05 +02001168extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
Olivier Deprez157378f2022-04-04 15:47:50 +02001169extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
1170 unsigned int max_zone_append_sectors);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1172extern void blk_queue_alignment_offset(struct request_queue *q,
1173 unsigned int alignment);
Olivier Deprez157378f2022-04-04 15:47:50 +02001174void blk_queue_update_readahead(struct request_queue *q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001175extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1176extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1177extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1178extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1179extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1180extern void blk_set_default_limits(struct queue_limits *lim);
1181extern void blk_set_stacking_limits(struct queue_limits *lim);
1182extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1183 sector_t offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001184extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1185 sector_t offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001186extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001187extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1188extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001189extern void blk_queue_dma_alignment(struct request_queue *, int);
1190extern void blk_queue_update_dma_alignment(struct request_queue *, int);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001192extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
David Brazdil0f672f62019-12-10 10:32:29 +00001193extern void blk_queue_required_elevator_features(struct request_queue *q,
1194 unsigned int features);
1195extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
1196 struct device *dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001197
1198/*
1199 * Number of physical segments as sent to the device.
1200 *
1201 * Normally this is the number of discontiguous data segments sent by the
1202 * submitter. But for data-less command like discard we might have no
1203 * actual data segments submitted, but the driver might have to add it's
1204 * own special payload. In that case we still return 1 here so that this
1205 * special payload will be mapped.
1206 */
1207static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1208{
1209 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1210 return 1;
1211 return rq->nr_phys_segments;
1212}
1213
1214/*
1215 * Number of discard segments (or ranges) the driver needs to fill in.
1216 * Each discard bio merged into a request is counted as one segment.
1217 */
1218static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1219{
1220 return max_t(unsigned short, rq->nr_phys_segments, 1);
1221}
1222
Olivier Deprez157378f2022-04-04 15:47:50 +02001223int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1224 struct scatterlist *sglist, struct scatterlist **last_sg);
1225static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1226 struct scatterlist *sglist)
1227{
1228 struct scatterlist *last_sg = NULL;
1229
1230 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1231}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001232extern void blk_dump_rq_flags(struct request *, char *);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001233
1234bool __must_check blk_get_queue(struct request_queue *);
Olivier Deprez157378f2022-04-04 15:47:50 +02001235struct request_queue *blk_alloc_queue(int node_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001236extern void blk_put_queue(struct request_queue *);
1237extern void blk_set_queue_dying(struct request_queue *);
1238
Olivier Deprez157378f2022-04-04 15:47:50 +02001239#ifdef CONFIG_BLOCK
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001240/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001241 * blk_plug permits building a queue of related requests by holding the I/O
1242 * fragments for a short period. This allows merging of sequential requests
1243 * into single larger request. As the requests are moved from a per-task list to
1244 * the device's request_queue in a batch, this results in improved scalability
1245 * as the lock contention for request_queue lock is reduced.
1246 *
1247 * It is ok not to disable preemption when adding the request to the plug list
1248 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1249 * the plug list when the task sleeps by itself. For details, please see
1250 * schedule() where blk_schedule_flush_plug() is called.
1251 */
1252struct blk_plug {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001253 struct list_head mq_list; /* blk-mq requests */
1254 struct list_head cb_list; /* md requires an unplug callback */
David Brazdil0f672f62019-12-10 10:32:29 +00001255 unsigned short rq_count;
1256 bool multiple_queues;
Olivier Deprez157378f2022-04-04 15:47:50 +02001257 bool nowait;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001258};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001259
1260struct blk_plug_cb;
1261typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1262struct blk_plug_cb {
1263 struct list_head list;
1264 blk_plug_cb_fn callback;
1265 void *data;
1266};
1267extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1268 void *data, int size);
1269extern void blk_start_plug(struct blk_plug *);
1270extern void blk_finish_plug(struct blk_plug *);
1271extern void blk_flush_plug_list(struct blk_plug *, bool);
1272
1273static inline void blk_flush_plug(struct task_struct *tsk)
1274{
1275 struct blk_plug *plug = tsk->plug;
1276
1277 if (plug)
1278 blk_flush_plug_list(plug, false);
1279}
1280
1281static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1282{
1283 struct blk_plug *plug = tsk->plug;
1284
1285 if (plug)
1286 blk_flush_plug_list(plug, true);
1287}
1288
1289static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1290{
1291 struct blk_plug *plug = tsk->plug;
1292
1293 return plug &&
David Brazdil0f672f62019-12-10 10:32:29 +00001294 (!list_empty(&plug->mq_list) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001295 !list_empty(&plug->cb_list));
1296}
1297
Olivier Deprez157378f2022-04-04 15:47:50 +02001298int blkdev_issue_flush(struct block_device *, gfp_t);
1299long nr_blockdev_pages(void);
1300#else /* CONFIG_BLOCK */
1301struct blk_plug {
1302};
1303
1304static inline void blk_start_plug(struct blk_plug *plug)
1305{
1306}
1307
1308static inline void blk_finish_plug(struct blk_plug *plug)
1309{
1310}
1311
1312static inline void blk_flush_plug(struct task_struct *task)
1313{
1314}
1315
1316static inline void blk_schedule_flush_plug(struct task_struct *task)
1317{
1318}
1319
1320
1321static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1322{
1323 return false;
1324}
1325
1326static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask)
1327{
1328 return 0;
1329}
1330
1331static inline long nr_blockdev_pages(void)
1332{
1333 return 0;
1334}
1335#endif /* CONFIG_BLOCK */
1336
1337extern void blk_io_schedule(void);
1338
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001339extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1340 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1341
1342#define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1343
1344extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1345 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1346extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1347 sector_t nr_sects, gfp_t gfp_mask, int flags,
1348 struct bio **biop);
1349
1350#define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1351#define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1352
1353extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1354 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1355 unsigned flags);
1356extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1357 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1358
1359static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1360 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1361{
1362 return blkdev_issue_discard(sb->s_bdev,
1363 block << (sb->s_blocksize_bits -
1364 SECTOR_SHIFT),
1365 nr_blocks << (sb->s_blocksize_bits -
1366 SECTOR_SHIFT),
1367 gfp_mask, flags);
1368}
1369static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1370 sector_t nr_blocks, gfp_t gfp_mask)
1371{
1372 return blkdev_issue_zeroout(sb->s_bdev,
1373 block << (sb->s_blocksize_bits -
1374 SECTOR_SHIFT),
1375 nr_blocks << (sb->s_blocksize_bits -
1376 SECTOR_SHIFT),
1377 gfp_mask, 0);
1378}
1379
1380extern int blk_verify_command(unsigned char *cmd, fmode_t mode);
1381
Olivier Deprez157378f2022-04-04 15:47:50 +02001382static inline bool bdev_is_partition(struct block_device *bdev)
1383{
1384 return bdev->bd_partno;
1385}
1386
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001387enum blk_default_limits {
1388 BLK_MAX_SEGMENTS = 128,
1389 BLK_SAFE_MAX_SECTORS = 255,
1390 BLK_DEF_MAX_SECTORS = 2560,
1391 BLK_MAX_SEGMENT_SIZE = 65536,
1392 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1393};
1394
David Brazdil0f672f62019-12-10 10:32:29 +00001395static inline unsigned long queue_segment_boundary(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001396{
1397 return q->limits.seg_boundary_mask;
1398}
1399
David Brazdil0f672f62019-12-10 10:32:29 +00001400static inline unsigned long queue_virt_boundary(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401{
1402 return q->limits.virt_boundary_mask;
1403}
1404
David Brazdil0f672f62019-12-10 10:32:29 +00001405static inline unsigned int queue_max_sectors(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001406{
1407 return q->limits.max_sectors;
1408}
1409
David Brazdil0f672f62019-12-10 10:32:29 +00001410static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001411{
1412 return q->limits.max_hw_sectors;
1413}
1414
David Brazdil0f672f62019-12-10 10:32:29 +00001415static inline unsigned short queue_max_segments(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001416{
1417 return q->limits.max_segments;
1418}
1419
David Brazdil0f672f62019-12-10 10:32:29 +00001420static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421{
1422 return q->limits.max_discard_segments;
1423}
1424
David Brazdil0f672f62019-12-10 10:32:29 +00001425static inline unsigned int queue_max_segment_size(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426{
1427 return q->limits.max_segment_size;
1428}
1429
Olivier Deprez157378f2022-04-04 15:47:50 +02001430static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
1431{
1432
1433 const struct queue_limits *l = &q->limits;
1434
1435 return min(l->max_zone_append_sectors, l->max_sectors);
1436}
1437
Olivier Deprez0e641232021-09-23 10:07:05 +02001438static inline unsigned queue_logical_block_size(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439{
1440 int retval = 512;
1441
1442 if (q && q->limits.logical_block_size)
1443 retval = q->limits.logical_block_size;
1444
1445 return retval;
1446}
1447
Olivier Deprez0e641232021-09-23 10:07:05 +02001448static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449{
1450 return queue_logical_block_size(bdev_get_queue(bdev));
1451}
1452
David Brazdil0f672f62019-12-10 10:32:29 +00001453static inline unsigned int queue_physical_block_size(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001454{
1455 return q->limits.physical_block_size;
1456}
1457
1458static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1459{
1460 return queue_physical_block_size(bdev_get_queue(bdev));
1461}
1462
David Brazdil0f672f62019-12-10 10:32:29 +00001463static inline unsigned int queue_io_min(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001464{
1465 return q->limits.io_min;
1466}
1467
1468static inline int bdev_io_min(struct block_device *bdev)
1469{
1470 return queue_io_min(bdev_get_queue(bdev));
1471}
1472
David Brazdil0f672f62019-12-10 10:32:29 +00001473static inline unsigned int queue_io_opt(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001474{
1475 return q->limits.io_opt;
1476}
1477
1478static inline int bdev_io_opt(struct block_device *bdev)
1479{
1480 return queue_io_opt(bdev_get_queue(bdev));
1481}
1482
David Brazdil0f672f62019-12-10 10:32:29 +00001483static inline int queue_alignment_offset(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001484{
1485 if (q->limits.misaligned)
1486 return -1;
1487
1488 return q->limits.alignment_offset;
1489}
1490
1491static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1492{
1493 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1494 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1495 << SECTOR_SHIFT;
1496
1497 return (granularity + lim->alignment_offset - alignment) % granularity;
1498}
1499
1500static inline int bdev_alignment_offset(struct block_device *bdev)
1501{
1502 struct request_queue *q = bdev_get_queue(bdev);
1503
1504 if (q->limits.misaligned)
1505 return -1;
Olivier Deprez157378f2022-04-04 15:47:50 +02001506 if (bdev_is_partition(bdev))
1507 return queue_limit_alignment_offset(&q->limits,
1508 bdev->bd_part->start_sect);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001509 return q->limits.alignment_offset;
1510}
1511
David Brazdil0f672f62019-12-10 10:32:29 +00001512static inline int queue_discard_alignment(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001513{
1514 if (q->limits.discard_misaligned)
1515 return -1;
1516
1517 return q->limits.discard_alignment;
1518}
1519
1520static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1521{
1522 unsigned int alignment, granularity, offset;
1523
1524 if (!lim->max_discard_sectors)
1525 return 0;
1526
1527 /* Why are these in bytes, not sectors? */
1528 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1529 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1530 if (!granularity)
1531 return 0;
1532
1533 /* Offset of the partition start in 'granularity' sectors */
1534 offset = sector_div(sector, granularity);
1535
1536 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1537 offset = (granularity + alignment - offset) % granularity;
1538
1539 /* Turn it back into bytes, gaah */
1540 return offset << SECTOR_SHIFT;
1541}
1542
Olivier Deprez157378f2022-04-04 15:47:50 +02001543/*
1544 * Two cases of handling DISCARD merge:
1545 * If max_discard_segments > 1, the driver takes every bio
1546 * as a range and send them to controller together. The ranges
1547 * needn't to be contiguous.
1548 * Otherwise, the bios/requests will be handled as same as
1549 * others which should be contiguous.
1550 */
1551static inline bool blk_discard_mergable(struct request *req)
1552{
1553 if (req_op(req) == REQ_OP_DISCARD &&
1554 queue_max_discard_segments(req->q) > 1)
1555 return true;
1556 return false;
1557}
1558
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001559static inline int bdev_discard_alignment(struct block_device *bdev)
1560{
1561 struct request_queue *q = bdev_get_queue(bdev);
1562
Olivier Deprez157378f2022-04-04 15:47:50 +02001563 if (bdev_is_partition(bdev))
1564 return queue_limit_discard_alignment(&q->limits,
1565 bdev->bd_part->start_sect);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001566 return q->limits.discard_alignment;
1567}
1568
1569static inline unsigned int bdev_write_same(struct block_device *bdev)
1570{
1571 struct request_queue *q = bdev_get_queue(bdev);
1572
1573 if (q)
1574 return q->limits.max_write_same_sectors;
1575
1576 return 0;
1577}
1578
1579static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1580{
1581 struct request_queue *q = bdev_get_queue(bdev);
1582
1583 if (q)
1584 return q->limits.max_write_zeroes_sectors;
1585
1586 return 0;
1587}
1588
1589static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1590{
1591 struct request_queue *q = bdev_get_queue(bdev);
1592
1593 if (q)
1594 return blk_queue_zoned_model(q);
1595
1596 return BLK_ZONED_NONE;
1597}
1598
1599static inline bool bdev_is_zoned(struct block_device *bdev)
1600{
1601 struct request_queue *q = bdev_get_queue(bdev);
1602
1603 if (q)
1604 return blk_queue_is_zoned(q);
1605
1606 return false;
1607}
1608
David Brazdil0f672f62019-12-10 10:32:29 +00001609static inline sector_t bdev_zone_sectors(struct block_device *bdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001610{
1611 struct request_queue *q = bdev_get_queue(bdev);
1612
1613 if (q)
1614 return blk_queue_zone_sectors(q);
1615 return 0;
1616}
1617
Olivier Deprez157378f2022-04-04 15:47:50 +02001618static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
1619{
1620 struct request_queue *q = bdev_get_queue(bdev);
1621
1622 if (q)
1623 return queue_max_open_zones(q);
1624 return 0;
1625}
1626
1627static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
1628{
1629 struct request_queue *q = bdev_get_queue(bdev);
1630
1631 if (q)
1632 return queue_max_active_zones(q);
1633 return 0;
1634}
1635
David Brazdil0f672f62019-12-10 10:32:29 +00001636static inline int queue_dma_alignment(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001637{
1638 return q ? q->dma_alignment : 511;
1639}
1640
1641static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1642 unsigned int len)
1643{
1644 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1645 return !(addr & alignment) && !(len & alignment);
1646}
1647
1648/* assumes size > 256 */
1649static inline unsigned int blksize_bits(unsigned int size)
1650{
1651 unsigned int bits = 8;
1652 do {
1653 bits++;
1654 size >>= 1;
1655 } while (size > 256);
1656 return bits;
1657}
1658
1659static inline unsigned int block_size(struct block_device *bdev)
1660{
Olivier Deprez157378f2022-04-04 15:47:50 +02001661 return 1 << bdev->bd_inode->i_blkbits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001662}
1663
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001664int kblockd_schedule_work(struct work_struct *work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001665int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1666
1667#define MODULE_ALIAS_BLOCKDEV(major,minor) \
1668 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1669#define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1670 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1671
1672#if defined(CONFIG_BLK_DEV_INTEGRITY)
1673
1674enum blk_integrity_flags {
1675 BLK_INTEGRITY_VERIFY = 1 << 0,
1676 BLK_INTEGRITY_GENERATE = 1 << 1,
1677 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1678 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1679};
1680
1681struct blk_integrity_iter {
1682 void *prot_buf;
1683 void *data_buf;
1684 sector_t seed;
1685 unsigned int data_size;
1686 unsigned short interval;
1687 const char *disk_name;
1688};
1689
1690typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
David Brazdil0f672f62019-12-10 10:32:29 +00001691typedef void (integrity_prepare_fn) (struct request *);
1692typedef void (integrity_complete_fn) (struct request *, unsigned int);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001693
1694struct blk_integrity_profile {
1695 integrity_processing_fn *generate_fn;
1696 integrity_processing_fn *verify_fn;
David Brazdil0f672f62019-12-10 10:32:29 +00001697 integrity_prepare_fn *prepare_fn;
1698 integrity_complete_fn *complete_fn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001699 const char *name;
1700};
1701
1702extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1703extern void blk_integrity_unregister(struct gendisk *);
1704extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1705extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1706 struct scatterlist *);
1707extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001708
1709static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1710{
1711 struct blk_integrity *bi = &disk->queue->integrity;
1712
1713 if (!bi->profile)
1714 return NULL;
1715
1716 return bi;
1717}
1718
1719static inline
1720struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1721{
1722 return blk_get_integrity(bdev->bd_disk);
1723}
1724
Olivier Deprez157378f2022-04-04 15:47:50 +02001725static inline bool
1726blk_integrity_queue_supports_integrity(struct request_queue *q)
1727{
1728 return q->integrity.profile;
1729}
1730
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001731static inline bool blk_integrity_rq(struct request *rq)
1732{
1733 return rq->cmd_flags & REQ_INTEGRITY;
1734}
1735
1736static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1737 unsigned int segs)
1738{
1739 q->limits.max_integrity_segments = segs;
1740}
1741
1742static inline unsigned short
David Brazdil0f672f62019-12-10 10:32:29 +00001743queue_max_integrity_segments(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001744{
1745 return q->limits.max_integrity_segments;
1746}
1747
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001748/**
1749 * bio_integrity_intervals - Return number of integrity intervals for a bio
1750 * @bi: blk_integrity profile for device
1751 * @sectors: Size of the bio in 512-byte sectors
1752 *
1753 * Description: The block layer calculates everything in 512 byte
1754 * sectors but integrity metadata is done in terms of the data integrity
1755 * interval size of the storage device. Convert the block layer sectors
1756 * to the appropriate number of integrity intervals.
1757 */
1758static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1759 unsigned int sectors)
1760{
1761 return sectors >> (bi->interval_exp - 9);
1762}
1763
1764static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1765 unsigned int sectors)
1766{
1767 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1768}
1769
David Brazdil0f672f62019-12-10 10:32:29 +00001770/*
1771 * Return the first bvec that contains integrity data. Only drivers that are
1772 * limited to a single integrity segment should use this helper.
1773 */
1774static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1775{
1776 if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
1777 return NULL;
1778 return rq->bio->bi_integrity->bip_vec;
1779}
1780
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001781#else /* CONFIG_BLK_DEV_INTEGRITY */
1782
1783struct bio;
1784struct block_device;
1785struct gendisk;
1786struct blk_integrity;
1787
1788static inline int blk_integrity_rq(struct request *rq)
1789{
1790 return 0;
1791}
1792static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1793 struct bio *b)
1794{
1795 return 0;
1796}
1797static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1798 struct bio *b,
1799 struct scatterlist *s)
1800{
1801 return 0;
1802}
1803static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1804{
1805 return NULL;
1806}
1807static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1808{
1809 return NULL;
1810}
Olivier Deprez157378f2022-04-04 15:47:50 +02001811static inline bool
1812blk_integrity_queue_supports_integrity(struct request_queue *q)
1813{
1814 return false;
1815}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001816static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1817{
1818 return 0;
1819}
1820static inline void blk_integrity_register(struct gendisk *d,
1821 struct blk_integrity *b)
1822{
1823}
1824static inline void blk_integrity_unregister(struct gendisk *d)
1825{
1826}
1827static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1828 unsigned int segs)
1829{
1830}
David Brazdil0f672f62019-12-10 10:32:29 +00001831static inline unsigned short queue_max_integrity_segments(const struct request_queue *q)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001832{
1833 return 0;
1834}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001835
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001836static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1837 unsigned int sectors)
1838{
1839 return 0;
1840}
1841
1842static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1843 unsigned int sectors)
1844{
1845 return 0;
1846}
1847
David Brazdil0f672f62019-12-10 10:32:29 +00001848static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1849{
1850 return NULL;
1851}
1852
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001853#endif /* CONFIG_BLK_DEV_INTEGRITY */
1854
Olivier Deprez157378f2022-04-04 15:47:50 +02001855#ifdef CONFIG_BLK_INLINE_ENCRYPTION
1856
1857bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
1858
1859void blk_ksm_unregister(struct request_queue *q);
1860
1861#else /* CONFIG_BLK_INLINE_ENCRYPTION */
1862
1863static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
1864 struct request_queue *q)
1865{
1866 return true;
1867}
1868
1869static inline void blk_ksm_unregister(struct request_queue *q) { }
1870
1871#endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1872
1873
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001874struct block_device_operations {
Olivier Deprez157378f2022-04-04 15:47:50 +02001875 blk_qc_t (*submit_bio) (struct bio *bio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001876 int (*open) (struct block_device *, fmode_t);
1877 void (*release) (struct gendisk *, fmode_t);
1878 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1879 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1880 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1881 unsigned int (*check_events) (struct gendisk *disk,
1882 unsigned int clearing);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001883 void (*unlock_native_capacity) (struct gendisk *);
1884 int (*revalidate_disk) (struct gendisk *);
1885 int (*getgeo)(struct block_device *, struct hd_geometry *);
1886 /* this callback is with swap_lock and sometimes page table lock held */
1887 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
David Brazdil0f672f62019-12-10 10:32:29 +00001888 int (*report_zones)(struct gendisk *, sector_t sector,
Olivier Deprez157378f2022-04-04 15:47:50 +02001889 unsigned int nr_zones, report_zones_cb cb, void *data);
1890 char *(*devnode)(struct gendisk *disk, umode_t *mode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001891 struct module *owner;
1892 const struct pr_ops *pr_ops;
1893};
1894
Olivier Deprez157378f2022-04-04 15:47:50 +02001895#ifdef CONFIG_COMPAT
1896extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1897 unsigned int, unsigned long);
1898#else
1899#define blkdev_compat_ptr_ioctl NULL
1900#endif
1901
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001902extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
1903 unsigned long);
1904extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1905extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1906 struct writeback_control *);
1907
1908#ifdef CONFIG_BLK_DEV_ZONED
1909bool blk_req_needs_zone_write_lock(struct request *rq);
Olivier Deprez157378f2022-04-04 15:47:50 +02001910bool blk_req_zone_write_trylock(struct request *rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001911void __blk_req_zone_write_lock(struct request *rq);
1912void __blk_req_zone_write_unlock(struct request *rq);
1913
1914static inline void blk_req_zone_write_lock(struct request *rq)
1915{
1916 if (blk_req_needs_zone_write_lock(rq))
1917 __blk_req_zone_write_lock(rq);
1918}
1919
1920static inline void blk_req_zone_write_unlock(struct request *rq)
1921{
1922 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1923 __blk_req_zone_write_unlock(rq);
1924}
1925
1926static inline bool blk_req_zone_is_write_locked(struct request *rq)
1927{
1928 return rq->q->seq_zones_wlock &&
1929 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1930}
1931
1932static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1933{
1934 if (!blk_req_needs_zone_write_lock(rq))
1935 return true;
1936 return !blk_req_zone_is_write_locked(rq);
1937}
1938#else
1939static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1940{
1941 return false;
1942}
1943
1944static inline void blk_req_zone_write_lock(struct request *rq)
1945{
1946}
1947
1948static inline void blk_req_zone_write_unlock(struct request *rq)
1949{
1950}
1951static inline bool blk_req_zone_is_write_locked(struct request *rq)
1952{
1953 return false;
1954}
1955
1956static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1957{
1958 return true;
1959}
1960#endif /* CONFIG_BLK_DEV_ZONED */
1961
David Brazdil0f672f62019-12-10 10:32:29 +00001962static inline void blk_wake_io_task(struct task_struct *waiter)
1963{
1964 /*
1965 * If we're polling, the task itself is doing the completions. For
1966 * that case, we don't need to signal a wakeup, it's enough to just
1967 * mark us as RUNNING.
1968 */
1969 if (waiter == current)
1970 __set_current_state(TASK_RUNNING);
1971 else
1972 wake_up_process(waiter);
1973}
1974
Olivier Deprez157378f2022-04-04 15:47:50 +02001975unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1976 unsigned int op);
1977void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1978 unsigned long start_time);
1979
1980unsigned long part_start_io_acct(struct gendisk *disk, struct hd_struct **part,
1981 struct bio *bio);
1982void part_end_io_acct(struct hd_struct *part, struct bio *bio,
1983 unsigned long start_time);
1984
1985/**
1986 * bio_start_io_acct - start I/O accounting for bio based drivers
1987 * @bio: bio to start account for
1988 *
1989 * Returns the start time that should be passed back to bio_end_io_acct().
1990 */
1991static inline unsigned long bio_start_io_acct(struct bio *bio)
1992{
1993 return disk_start_io_acct(bio->bi_disk, bio_sectors(bio), bio_op(bio));
1994}
1995
1996/**
1997 * bio_end_io_acct - end I/O accounting for bio based drivers
1998 * @bio: bio to end account for
1999 * @start: start time returned by bio_start_io_acct()
2000 */
2001static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
2002{
2003 return disk_end_io_acct(bio->bi_disk, bio_op(bio), start_time);
2004}
2005
2006int bdev_read_only(struct block_device *bdev);
2007int set_blocksize(struct block_device *bdev, int size);
2008
2009const char *bdevname(struct block_device *bdev, char *buffer);
2010struct block_device *lookup_bdev(const char *);
2011
2012void blkdev_show(struct seq_file *seqf, off_t offset);
2013
2014#define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */
2015#define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */
2016#ifdef CONFIG_BLOCK
2017#define BLKDEV_MAJOR_MAX 512
2018#else
2019#define BLKDEV_MAJOR_MAX 0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002020#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02002021
2022struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
2023 void *holder);
2024struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
2025int bd_prepare_to_claim(struct block_device *bdev, struct block_device *whole,
2026 void *holder);
2027void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
2028 void *holder);
2029void blkdev_put(struct block_device *bdev, fmode_t mode);
2030
2031struct block_device *I_BDEV(struct inode *inode);
2032struct block_device *bdget_part(struct hd_struct *part);
2033struct block_device *bdgrab(struct block_device *bdev);
2034void bdput(struct block_device *);
2035
2036#ifdef CONFIG_BLOCK
2037void invalidate_bdev(struct block_device *bdev);
2038int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
2039 loff_t lend);
2040int sync_blockdev(struct block_device *bdev);
2041#else
2042static inline void invalidate_bdev(struct block_device *bdev)
2043{
2044}
2045static inline int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
2046 loff_t lstart, loff_t lend)
2047{
2048 return 0;
2049}
2050static inline int sync_blockdev(struct block_device *bdev)
2051{
2052 return 0;
2053}
2054#endif
2055int fsync_bdev(struct block_device *bdev);
2056
2057struct super_block *freeze_bdev(struct block_device *bdev);
2058int thaw_bdev(struct block_device *bdev, struct super_block *sb);
2059
2060#endif /* _LINUX_BLKDEV_H */