block: remove legacy IO schedulers
[linux-2.6-block.git] / include / linux / blkdev.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_BLKDEV_H
3#define _LINUX_BLKDEV_H
4
85fd0bc9 5#include <linux/sched.h>
e6017571 6#include <linux/sched/clock.h>
85fd0bc9 7
f5ff8422
JA
8#ifdef CONFIG_BLOCK
9
1da177e4
LT
10#include <linux/major.h>
11#include <linux/genhd.h>
12#include <linux/list.h>
320ae51f 13#include <linux/llist.h>
1da177e4
LT
14#include <linux/timer.h>
15#include <linux/workqueue.h>
16#include <linux/pagemap.h>
66114cad 17#include <linux/backing-dev-defs.h>
1da177e4
LT
18#include <linux/wait.h>
19#include <linux/mempool.h>
34c0fd54 20#include <linux/pfn.h>
1da177e4 21#include <linux/bio.h>
1da177e4 22#include <linux/stringify.h>
3e6053d7 23#include <linux/gfp.h>
d351af01 24#include <linux/bsg.h>
c7c22e4d 25#include <linux/smp.h>
548bc8e1 26#include <linux/rcupdate.h>
add703fd 27#include <linux/percpu-refcount.h>
84be456f 28#include <linux/scatterlist.h>
6a0cb1bc 29#include <linux/blkzoned.h>
1da177e4 30
de477254 31struct module;
21b2f0c8
CH
32struct scsi_ioctl_command;
33
1da177e4 34struct request_queue;
1da177e4 35struct elevator_queue;
2056a782 36struct blk_trace;
3d6392cf
JA
37struct request;
38struct sg_io_hdr;
aa387cc8 39struct bsg_job;
3c798398 40struct blkcg_gq;
7c94e1c1 41struct blk_flush_queue;
bbd3e064 42struct pr_ops;
a7905043 43struct rq_qos;
34dbad5d
OS
44struct blk_queue_stats;
45struct blk_stat_callback;
1da177e4
LT
46
47#define BLKDEV_MIN_RQ 4
48#define BLKDEV_MAX_RQ 128 /* Default maximum */
49
096392e0 50/* Must be consistent with blk_mq_poll_stats_bkt() */
0206319f
SB
51#define BLK_MQ_POLL_STATS_BKTS 16
52
8bd435b3
TH
53/*
54 * Maximum number of blkcg policies allowed to be registered concurrently.
55 * Defined here to simplify include dependency.
56 */
01c5f85a 57#define BLKCG_MAX_POLS 5
8bd435b3 58
2a842aca 59typedef void (rq_end_io_fn)(struct request *, blk_status_t);
1da177e4 60
5b788ce3
TH
61#define BLK_RL_SYNCFULL (1U << 0)
62#define BLK_RL_ASYNCFULL (1U << 1)
63
1da177e4 64struct request_list {
5b788ce3 65 struct request_queue *q; /* the queue this rl belongs to */
a051661c
TH
66#ifdef CONFIG_BLK_CGROUP
67 struct blkcg_gq *blkg; /* blkg this request pool belongs to */
68#endif
1faa16d2
JA
69 /*
70 * count[], starved[], and wait[] are indexed by
71 * BLK_RW_SYNC/BLK_RW_ASYNC
72 */
8a5ecdd4
TH
73 int count[2];
74 int starved[2];
75 mempool_t *rq_pool;
76 wait_queue_head_t wait[2];
5b788ce3 77 unsigned int flags;
1da177e4
LT
78};
79
e8064021
CH
80/*
81 * request flags */
82typedef __u32 __bitwise req_flags_t;
83
84/* elevator knows about this request */
85#define RQF_SORTED ((__force req_flags_t)(1 << 0))
86/* drive already may have started this one */
87#define RQF_STARTED ((__force req_flags_t)(1 << 1))
e8064021
CH
88/* may not be passed by ioscheduler */
89#define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
90/* request for flush sequence */
91#define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
92/* merge of different types, fail separately */
93#define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
94/* track inflight for MQ */
95#define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
96/* don't call prep for this one */
97#define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
98/* set for "ide_preempt" requests and also for requests for which the SCSI
99 "quiesce" state must be ignored. */
100#define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
101/* contains copies of user pages */
102#define RQF_COPY_USER ((__force req_flags_t)(1 << 9))
103/* vaguely specified driver internal error. Ignored by the block layer */
104#define RQF_FAILED ((__force req_flags_t)(1 << 10))
105/* don't warn about errors */
106#define RQF_QUIET ((__force req_flags_t)(1 << 11))
107/* elevator private data attached */
108#define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
4822e902 109/* account into disk and partition IO statistics */
e8064021
CH
110#define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
111/* request came from our alloc pool */
112#define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
113/* runtime pm request */
114#define RQF_PM ((__force req_flags_t)(1 << 15))
115/* on IO scheduler merge hash */
116#define RQF_HASHED ((__force req_flags_t)(1 << 16))
4822e902 117/* track IO completion time */
cf43e6be 118#define RQF_STATS ((__force req_flags_t)(1 << 17))
f9d03f96
CH
119/* Look at ->special_vec for the actual data payload instead of the
120 bio chain. */
121#define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
6cc77e9c
CH
122/* The per-zone write lock is held for this request */
123#define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
76a86f9d 124/* already slept for hybrid poll */
12f5b931 125#define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
da661267
CH
126/* ->timeout has been called, don't expire again */
127#define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
e8064021
CH
128
129/* flags that prevent us from merging requests: */
130#define RQF_NOMERGE_FLAGS \
f9d03f96 131 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
e8064021 132
12f5b931
KB
133/*
134 * Request state for blk-mq.
135 */
136enum mq_rq_state {
137 MQ_RQ_IDLE = 0,
138 MQ_RQ_IN_FLIGHT = 1,
139 MQ_RQ_COMPLETE = 2,
140};
141
1da177e4 142/*
af76e555
CH
143 * Try to put the fields that are referenced together in the same cacheline.
144 *
145 * If you modify this structure, make sure to update blk_rq_init() and
146 * especially blk_mq_rq_ctx_init() to take care of the added fields.
1da177e4
LT
147 */
148struct request {
165125e1 149 struct request_queue *q;
320ae51f 150 struct blk_mq_ctx *mq_ctx;
e6a1c874 151
ca93e453 152 int cpu;
ef295ecf 153 unsigned int cmd_flags; /* op and common flags */
e8064021 154 req_flags_t rq_flags;
d486f1f2
JA
155
156 int internal_tag;
157
a2dec7b3 158 /* the following two fields are internal, NEVER access directly */
a2dec7b3 159 unsigned int __data_len; /* total data len */
bd166ef1 160 int tag;
181fdde3 161 sector_t __sector; /* sector cursor */
1da177e4
LT
162
163 struct bio *bio;
164 struct bio *biotail;
165
7c3fb70f
JA
166 struct list_head queuelist;
167
360f92c2
JA
168 /*
169 * The hash is used inside the scheduler, and killed once the
170 * request reaches the dispatch list. The ipi_list is only used
171 * to queue the request for softirq completion, which is long
172 * after the request has been unhashed (and even removed from
173 * the dispatch list).
174 */
175 union {
176 struct hlist_node hash; /* merge hash */
177 struct list_head ipi_list;
178 };
179
e6a1c874
JA
180 /*
181 * The rb_node is only used inside the io scheduler, requests
182 * are pruned when moved to the dispatch queue. So let the
c186794d 183 * completion_data share space with the rb_node.
e6a1c874
JA
184 */
185 union {
186 struct rb_node rb_node; /* sort/lookup */
f9d03f96 187 struct bio_vec special_vec;
c186794d 188 void *completion_data;
e26738e0 189 int error_count; /* for legacy drivers, don't use */
e6a1c874 190 };
9817064b 191
ff7d145f 192 /*
7f1dc8a2 193 * Three pointers are available for the IO schedulers, if they need
c186794d
MS
194 * more they have to dynamically allocate it. Flush requests are
195 * never put on the IO scheduler. So let the flush fields share
a612fddf 196 * space with the elevator data.
ff7d145f 197 */
c186794d 198 union {
a612fddf
TH
199 struct {
200 struct io_cq *icq;
201 void *priv[2];
202 } elv;
203
c186794d
MS
204 struct {
205 unsigned int seq;
206 struct list_head list;
4853abaa 207 rq_end_io_fn *saved_end_io;
c186794d
MS
208 } flush;
209 };
ff7d145f 210
8f34ee75 211 struct gendisk *rq_disk;
09e099d4 212 struct hd_struct *part;
522a7775
OS
213 /* Time that I/O was submitted to the kernel. */
214 u64 start_time_ns;
544ccc8d
OS
215 /* Time that I/O was submitted to the device. */
216 u64 io_start_time_ns;
217
218#ifdef CONFIG_BLK_WBT
219 unsigned short wbt_flags;
220#endif
221#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
222 unsigned short throtl_size;
223#endif
224
225 /*
226 * Number of scatter-gather DMA addr+len pairs after
1da177e4
LT
227 * physical address coalescing is performed.
228 */
229 unsigned short nr_phys_segments;
7c3fb70f 230
13f05c8d
MP
231#if defined(CONFIG_BLK_DEV_INTEGRITY)
232 unsigned short nr_integrity_segments;
233#endif
1da177e4 234
7c3fb70f 235 unsigned short write_hint;
8f34ee75
JA
236 unsigned short ioprio;
237
731ec497 238 void *special; /* opaque pointer available for LLD use */
cdd60262 239
7a85f889 240 unsigned int extra_len; /* length of alignment and padding */
1da177e4 241
12f5b931
KB
242 enum mq_rq_state state;
243 refcount_t ref;
1d9bd516 244
0b7576d8
JA
245 unsigned int timeout;
246
0a72e7f4
JA
247 /* access through blk_rq_set_deadline, blk_rq_deadline */
248 unsigned long __deadline;
cb6934f8 249
242f9dcb 250 struct list_head timeout_list;
1da177e4 251
7c3fb70f 252 union {
0a4b6e2f 253 struct __call_single_data csd;
7c3fb70f
JA
254 u64 fifo_time;
255 };
256
1da177e4 257 /*
c00895ab 258 * completion callback.
1da177e4
LT
259 */
260 rq_end_io_fn *end_io;
261 void *end_io_data;
abae1fde
FT
262
263 /* for bidi */
264 struct request *next_rq;
7c3fb70f
JA
265
266#ifdef CONFIG_BLK_CGROUP
267 struct request_list *rl; /* rl this rq is alloced from */
7c3fb70f 268#endif
1da177e4
LT
269};
270
14cb0dc6
ML
271static inline bool blk_op_is_scsi(unsigned int op)
272{
273 return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
274}
275
276static inline bool blk_op_is_private(unsigned int op)
277{
278 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
279}
280
aebf526b
CH
281static inline bool blk_rq_is_scsi(struct request *rq)
282{
14cb0dc6 283 return blk_op_is_scsi(req_op(rq));
aebf526b
CH
284}
285
286static inline bool blk_rq_is_private(struct request *rq)
287{
14cb0dc6 288 return blk_op_is_private(req_op(rq));
aebf526b
CH
289}
290
57292b58
CH
291static inline bool blk_rq_is_passthrough(struct request *rq)
292{
aebf526b 293 return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
57292b58
CH
294}
295
14cb0dc6
ML
296static inline bool bio_is_passthrough(struct bio *bio)
297{
298 unsigned op = bio_op(bio);
299
300 return blk_op_is_scsi(op) || blk_op_is_private(op);
301}
302
766ca442
FLVC
303static inline unsigned short req_get_ioprio(struct request *req)
304{
305 return req->ioprio;
306}
307
1da177e4
LT
308#include <linux/elevator.h>
309
320ae51f
JA
310struct blk_queue_ctx;
311
165125e1 312typedef void (request_fn_proc) (struct request_queue *q);
dece1635 313typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
ea435e1b 314typedef bool (poll_q_fn) (struct request_queue *q, blk_qc_t);
165125e1 315typedef int (prep_rq_fn) (struct request_queue *, struct request *);
28018c24 316typedef void (unprep_rq_fn) (struct request_queue *, struct request *);
1da177e4
LT
317
318struct bio_vec;
ff856bad 319typedef void (softirq_done_fn)(struct request *);
2fb98e84 320typedef int (dma_drain_needed_fn)(struct request *);
aa387cc8 321typedef int (bsg_job_fn) (struct bsg_job *);
6d247d7f
CH
322typedef int (init_rq_fn)(struct request_queue *, struct request *, gfp_t);
323typedef void (exit_rq_fn)(struct request_queue *, struct request *);
1da177e4 324
242f9dcb 325enum blk_eh_timer_return {
88b0cfad
CH
326 BLK_EH_DONE, /* drivers has completed the command */
327 BLK_EH_RESET_TIMER, /* reset timer and try again */
242f9dcb
JA
328};
329
330typedef enum blk_eh_timer_return (rq_timed_out_fn)(struct request *);
331
1da177e4
LT
332enum blk_queue_state {
333 Queue_down,
334 Queue_up,
335};
336
ee1b6f7a
SL
337#define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
338#define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
1da177e4 339
abf54393
FT
340#define BLK_SCSI_MAX_CMDS (256)
341#define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
342
797476b8
DLM
343/*
344 * Zoned block device models (zoned limit).
345 */
346enum blk_zoned_model {
347 BLK_ZONED_NONE, /* Regular block device */
348 BLK_ZONED_HA, /* Host-aware zoned block device */
349 BLK_ZONED_HM, /* Host-managed zoned block device */
350};
351
025146e1
MP
352struct queue_limits {
353 unsigned long bounce_pfn;
354 unsigned long seg_boundary_mask;
03100aad 355 unsigned long virt_boundary_mask;
025146e1
MP
356
357 unsigned int max_hw_sectors;
ca369d51 358 unsigned int max_dev_sectors;
762380ad 359 unsigned int chunk_sectors;
025146e1
MP
360 unsigned int max_sectors;
361 unsigned int max_segment_size;
c72758f3
MP
362 unsigned int physical_block_size;
363 unsigned int alignment_offset;
364 unsigned int io_min;
365 unsigned int io_opt;
67efc925 366 unsigned int max_discard_sectors;
0034af03 367 unsigned int max_hw_discard_sectors;
4363ac7c 368 unsigned int max_write_same_sectors;
a6f0788e 369 unsigned int max_write_zeroes_sectors;
86b37281
MP
370 unsigned int discard_granularity;
371 unsigned int discard_alignment;
025146e1
MP
372
373 unsigned short logical_block_size;
8a78362c 374 unsigned short max_segments;
13f05c8d 375 unsigned short max_integrity_segments;
1e739730 376 unsigned short max_discard_segments;
025146e1 377
c72758f3 378 unsigned char misaligned;
86b37281 379 unsigned char discard_misaligned;
e692cb66 380 unsigned char cluster;
c78afc62 381 unsigned char raid_partial_stripes_expensive;
797476b8 382 enum blk_zoned_model zoned;
025146e1
MP
383};
384
6a0cb1bc
HR
385#ifdef CONFIG_BLK_DEV_ZONED
386
a91e1380 387extern unsigned int blkdev_nr_zones(struct block_device *bdev);
6a0cb1bc
HR
388extern int blkdev_report_zones(struct block_device *bdev,
389 sector_t sector, struct blk_zone *zones,
390 unsigned int *nr_zones, gfp_t gfp_mask);
391extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors,
392 sector_t nr_sectors, gfp_t gfp_mask);
bf505456 393extern int blk_revalidate_disk_zones(struct gendisk *disk);
6a0cb1bc 394
3ed05a98
ST
395extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
396 unsigned int cmd, unsigned long arg);
397extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
398 unsigned int cmd, unsigned long arg);
399
400#else /* CONFIG_BLK_DEV_ZONED */
401
a91e1380
DLM
402static inline unsigned int blkdev_nr_zones(struct block_device *bdev)
403{
404 return 0;
405}
bf505456
DLM
406
407static inline int blk_revalidate_disk_zones(struct gendisk *disk)
408{
409 return 0;
410}
411
3ed05a98
ST
412static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
413 fmode_t mode, unsigned int cmd,
414 unsigned long arg)
415{
416 return -ENOTTY;
417}
418
419static inline int blkdev_reset_zones_ioctl(struct block_device *bdev,
420 fmode_t mode, unsigned int cmd,
421 unsigned long arg)
422{
423 return -ENOTTY;
424}
425
6a0cb1bc
HR
426#endif /* CONFIG_BLK_DEV_ZONED */
427
d7b76301 428struct request_queue {
1da177e4
LT
429 /*
430 * Together with queue_head for cacheline sharing
431 */
432 struct list_head queue_head;
433 struct request *last_merge;
b374d18a 434 struct elevator_queue *elevator;
8a5ecdd4
TH
435 int nr_rqs[2]; /* # allocated [a]sync rqs */
436 int nr_rqs_elvpriv; /* # allocated rqs w/ elvpriv */
1da177e4 437
34dbad5d 438 struct blk_queue_stats *stats;
a7905043 439 struct rq_qos *rq_qos;
87760e5e 440
1da177e4 441 /*
a051661c
TH
442 * If blkcg is not used, @q->root_rl serves all requests. If blkcg
443 * is used, root blkg allocates from @q->root_rl and all other
444 * blkgs from their own blkg->rl. Which one to use should be
445 * determined using bio_request_list().
1da177e4 446 */
a051661c 447 struct request_list root_rl;
1da177e4
LT
448
449 request_fn_proc *request_fn;
1da177e4 450 make_request_fn *make_request_fn;
ea435e1b 451 poll_q_fn *poll_fn;
1da177e4 452 prep_rq_fn *prep_rq_fn;
28018c24 453 unprep_rq_fn *unprep_rq_fn;
ff856bad 454 softirq_done_fn *softirq_done_fn;
242f9dcb 455 rq_timed_out_fn *rq_timed_out_fn;
2fb98e84 456 dma_drain_needed_fn *dma_drain_needed;
d280bab3 457 /* Called just after a request is allocated */
6d247d7f 458 init_rq_fn *init_rq_fn;
d280bab3 459 /* Called just before a request is freed */
6d247d7f 460 exit_rq_fn *exit_rq_fn;
d280bab3
BVA
461 /* Called from inside blk_get_request() */
462 void (*initialize_rq_fn)(struct request *rq);
1da177e4 463
f8a5b122 464 const struct blk_mq_ops *mq_ops;
320ae51f
JA
465
466 unsigned int *mq_map;
467
468 /* sw queues */
e6cdb092 469 struct blk_mq_ctx __percpu *queue_ctx;
320ae51f
JA
470 unsigned int nr_queues;
471
d278d4a8
JA
472 unsigned int queue_depth;
473
320ae51f
JA
474 /* hw dispatch queues */
475 struct blk_mq_hw_ctx **queue_hw_ctx;
476 unsigned int nr_hw_queues;
477
8922e16c
TH
478 /*
479 * Dispatch queue sorting
480 */
1b47f531 481 sector_t end_sector;
8922e16c 482 struct request *boundary_rq;
8922e16c 483
1da177e4 484 /*
3cca6dc1 485 * Delayed queue handling
1da177e4 486 */
3cca6dc1 487 struct delayed_work delay_work;
1da177e4 488
dc3b17cc 489 struct backing_dev_info *backing_dev_info;
1da177e4
LT
490
491 /*
492 * The queue owner gets to use this for whatever they like.
493 * ll_rw_blk doesn't touch it.
494 */
495 void *queuedata;
496
1da177e4 497 /*
d7b76301 498 * various queue flags, see QUEUE_* below
1da177e4 499 */
d7b76301 500 unsigned long queue_flags;
cd84a62e
BVA
501 /*
502 * Number of contexts that have called blk_set_pm_only(). If this
503 * counter is above zero then only RQF_PM and RQF_PREEMPT requests are
504 * processed.
505 */
506 atomic_t pm_only;
1da177e4 507
a73f730d
TH
508 /*
509 * ida allocated id for this queue. Used to index queues from
510 * ioctx.
511 */
512 int id;
513
1da177e4 514 /*
d7b76301 515 * queue needs bounce pages for pages above this limit
1da177e4 516 */
d7b76301 517 gfp_t bounce_gfp;
1da177e4
LT
518
519 /*
152587de 520 * protects queue structures from reentrancy. ->__queue_lock should
521 * _never_ be used directly, it is queue private. always use
522 * ->queue_lock.
1da177e4 523 */
152587de 524 spinlock_t __queue_lock;
1da177e4
LT
525 spinlock_t *queue_lock;
526
527 /*
528 * queue kobject
529 */
530 struct kobject kobj;
531
320ae51f
JA
532 /*
533 * mq queue kobject
534 */
535 struct kobject mq_kobj;
536
ac6fc48c
DW
537#ifdef CONFIG_BLK_DEV_INTEGRITY
538 struct blk_integrity integrity;
539#endif /* CONFIG_BLK_DEV_INTEGRITY */
540
47fafbc7 541#ifdef CONFIG_PM
6c954667
LM
542 struct device *dev;
543 int rpm_status;
544 unsigned int nr_pending;
545#endif
546
1da177e4
LT
547 /*
548 * queue settings
549 */
550 unsigned long nr_requests; /* Max # of requests */
551 unsigned int nr_congestion_on;
552 unsigned int nr_congestion_off;
553 unsigned int nr_batching;
554
fa0ccd83 555 unsigned int dma_drain_size;
d7b76301 556 void *dma_drain_buffer;
e3790c7d 557 unsigned int dma_pad_mask;
1da177e4
LT
558 unsigned int dma_alignment;
559
15853af9 560 unsigned int nr_sorted;
0a7ae2ff 561 unsigned int in_flight[2];
cf43e6be 562
24faf6f6
BVA
563 /*
564 * Number of active block driver functions for which blk_drain_queue()
565 * must wait. Must be incremented around functions that unlock the
566 * queue_lock internally, e.g. scsi_request_fn().
567 */
568 unsigned int request_fn_active;
1da177e4 569
242f9dcb 570 unsigned int rq_timeout;
64f1c21e 571 int poll_nsec;
34dbad5d
OS
572
573 struct blk_stat_callback *poll_cb;
0206319f 574 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
34dbad5d 575
242f9dcb 576 struct timer_list timeout;
287922eb 577 struct work_struct timeout_work;
242f9dcb
JA
578 struct list_head timeout_list;
579
a612fddf 580 struct list_head icq_list;
4eef3049 581#ifdef CONFIG_BLK_CGROUP
a2b1693b 582 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
3c798398 583 struct blkcg_gq *root_blkg;
03aa264a 584 struct list_head blkg_list;
4eef3049 585#endif
a612fddf 586
025146e1
MP
587 struct queue_limits limits;
588
6a5ac984 589#ifdef CONFIG_BLK_DEV_ZONED
6cc77e9c
CH
590 /*
591 * Zoned block device information for request dispatch control.
592 * nr_zones is the total number of zones of the device. This is always
593 * 0 for regular block devices. seq_zones_bitmap is a bitmap of nr_zones
594 * bits which indicates if a zone is conventional (bit clear) or
595 * sequential (bit set). seq_zones_wlock is a bitmap of nr_zones
596 * bits which indicates if a zone is write locked, that is, if a write
597 * request targeting the zone was dispatched. All three fields are
598 * initialized by the low level device driver (e.g. scsi/sd.c).
599 * Stacking drivers (device mappers) may or may not initialize
600 * these fields.
ccce20fc
BVA
601 *
602 * Reads of this information must be protected with blk_queue_enter() /
603 * blk_queue_exit(). Modifying this information is only allowed while
604 * no requests are being processed. See also blk_mq_freeze_queue() and
605 * blk_mq_unfreeze_queue().
6cc77e9c
CH
606 */
607 unsigned int nr_zones;
608 unsigned long *seq_zones_bitmap;
609 unsigned long *seq_zones_wlock;
6a5ac984 610#endif /* CONFIG_BLK_DEV_ZONED */
6cc77e9c 611
1da177e4
LT
612 /*
613 * sg stuff
614 */
615 unsigned int sg_timeout;
616 unsigned int sg_reserved_size;
1946089a 617 int node;
6c5c9341 618#ifdef CONFIG_BLK_DEV_IO_TRACE
2056a782 619 struct blk_trace *blk_trace;
5acb3cc2 620 struct mutex blk_trace_mutex;
6c5c9341 621#endif
1da177e4 622 /*
4913efe4 623 * for flush operations
1da177e4 624 */
7c94e1c1 625 struct blk_flush_queue *fq;
483f4afc 626
6fca6a61
CH
627 struct list_head requeue_list;
628 spinlock_t requeue_lock;
2849450a 629 struct delayed_work requeue_work;
6fca6a61 630
483f4afc 631 struct mutex sysfs_lock;
d351af01 632
d732580b 633 int bypass_depth;
4ecd4fef 634 atomic_t mq_freeze_depth;
d732580b 635
d351af01 636#if defined(CONFIG_BLK_DEV_BSG)
aa387cc8 637 bsg_job_fn *bsg_job_fn;
d351af01
FT
638 struct bsg_class_device bsg_dev;
639#endif
e43473b7
VG
640
641#ifdef CONFIG_BLK_DEV_THROTTLING
642 /* Throttle data */
643 struct throtl_data *td;
644#endif
548bc8e1 645 struct rcu_head rcu_head;
320ae51f 646 wait_queue_head_t mq_freeze_wq;
3ef28e83 647 struct percpu_ref q_usage_counter;
320ae51f 648 struct list_head all_q_node;
0d2602ca
JA
649
650 struct blk_mq_tag_set *tag_set;
651 struct list_head tag_set_list;
338aa96d 652 struct bio_set bio_split;
4593fdbe 653
03796c14 654#ifdef CONFIG_BLK_DEBUG_FS
07e4fead 655 struct dentry *debugfs_dir;
d332ce09 656 struct dentry *sched_debugfs_dir;
07e4fead
OS
657#endif
658
4593fdbe 659 bool mq_sysfs_init_done;
6d247d7f
CH
660
661 size_t cmd_size;
662 void *rq_alloc_data;
dc9edc44
BVA
663
664 struct work_struct release_work;
f793dfd3
JA
665
666#define BLK_MAX_WRITE_HINTS 5
667 u64 write_hints[BLK_MAX_WRITE_HINTS];
1da177e4
LT
668};
669
e743eb1e
JA
670#define QUEUE_FLAG_STOPPED 1 /* queue is stopped */
671#define QUEUE_FLAG_DYING 2 /* queue being torn down */
672#define QUEUE_FLAG_BYPASS 3 /* act as dumb FIFO queue */
673#define QUEUE_FLAG_BIDI 4 /* queue supports bidi requests */
674#define QUEUE_FLAG_NOMERGES 5 /* disable merge attempts */
675#define QUEUE_FLAG_SAME_COMP 6 /* complete on same CPU-group */
676#define QUEUE_FLAG_FAIL_IO 7 /* fake timeout */
e743eb1e 677#define QUEUE_FLAG_NONROT 9 /* non-rotational device (SSD) */
88e740f1 678#define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
4822e902 679#define QUEUE_FLAG_IO_STAT 10 /* do disk/partitions IO accounting */
e743eb1e
JA
680#define QUEUE_FLAG_DISCARD 11 /* supports DISCARD */
681#define QUEUE_FLAG_NOXMERGES 12 /* No extended merges */
682#define QUEUE_FLAG_ADD_RANDOM 13 /* Contributes to random pool */
683#define QUEUE_FLAG_SECERASE 14 /* supports secure erase */
684#define QUEUE_FLAG_SAME_FORCE 15 /* force complete on same CPU */
685#define QUEUE_FLAG_DEAD 16 /* queue tear-down finished */
686#define QUEUE_FLAG_INIT_DONE 17 /* queue is initialized */
687#define QUEUE_FLAG_NO_SG_MERGE 18 /* don't attempt to merge SG segments*/
688#define QUEUE_FLAG_POLL 19 /* IO polling enabled if set */
689#define QUEUE_FLAG_WC 20 /* Write back caching */
690#define QUEUE_FLAG_FUA 21 /* device supports FUA writes */
691#define QUEUE_FLAG_FLUSH_NQ 22 /* flush not queueuable */
692#define QUEUE_FLAG_DAX 23 /* device supports DAX */
4822e902 693#define QUEUE_FLAG_STATS 24 /* track IO start and completion times */
e743eb1e
JA
694#define QUEUE_FLAG_POLL_STATS 25 /* collecting stats for hybrid polling */
695#define QUEUE_FLAG_REGISTERED 26 /* queue has been registered to a disk */
696#define QUEUE_FLAG_SCSI_PASSTHROUGH 27 /* queue supports SCSI commands */
697#define QUEUE_FLAG_QUIESCED 28 /* queue has been quiesced */
bd6bf7c1 698#define QUEUE_FLAG_PCI_P2PDMA 29 /* device supports PCI p2p requests */
bc58ba94
JA
699
700#define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
e2e1a148
JA
701 (1 << QUEUE_FLAG_SAME_COMP) | \
702 (1 << QUEUE_FLAG_ADD_RANDOM))
797e7dbb 703
94eddfbe 704#define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
8e0b60b9
CH
705 (1 << QUEUE_FLAG_SAME_COMP) | \
706 (1 << QUEUE_FLAG_POLL))
94eddfbe 707
8814ce8a
BVA
708void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
709void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
710bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
711bool blk_queue_flag_test_and_clear(unsigned int flag, struct request_queue *q);
712
1da177e4 713#define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
3f3299d5 714#define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
c246e80d 715#define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
d732580b 716#define blk_queue_bypass(q) test_bit(QUEUE_FLAG_BYPASS, &(q)->queue_flags)
320ae51f 717#define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
ac9fafa1 718#define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
488991e2
AB
719#define blk_queue_noxmerges(q) \
720 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
a68bbddb 721#define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
bc58ba94 722#define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
e2e1a148 723#define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
c15227de 724#define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
288dab8a
CH
725#define blk_queue_secure_erase(q) \
726 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
163d4baa 727#define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
9efc160f
BVA
728#define blk_queue_scsi_passthrough(q) \
729 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
49d92c0d
LG
730#define blk_queue_pci_p2pdma(q) \
731 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
1da177e4 732
33659ebb
CH
733#define blk_noretry_request(rq) \
734 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
735 REQ_FAILFAST_DRIVER))
f4560ffe 736#define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
cd84a62e 737#define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
0ce91444 738#define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
c9254f2d 739
cd84a62e
BVA
740extern void blk_set_pm_only(struct request_queue *q);
741extern void blk_clear_pm_only(struct request_queue *q);
33659ebb 742
66f91322
BVA
743static inline int queue_in_flight(struct request_queue *q)
744{
745 return q->in_flight[0] + q->in_flight[1];
746}
747
57292b58
CH
748static inline bool blk_account_rq(struct request *rq)
749{
750 return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
751}
33659ebb 752
ab780f1e 753#define blk_rq_cpu_valid(rq) ((rq)->cpu != -1)
abae1fde 754#define blk_bidi_rq(rq) ((rq)->next_rq != NULL)
336cdb40
KU
755/* rq->queuelist of dequeued request must be list_empty() */
756#define blk_queued_rq(rq) (!list_empty(&(rq)->queuelist))
1da177e4
LT
757
758#define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
759
4e1b2d52 760#define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
1da177e4 761
49fd524f
JA
762/*
763 * Driver can handle struct request, if it either has an old style
764 * request_fn defined, or is blk-mq based.
765 */
766static inline bool queue_is_rq_based(struct request_queue *q)
767{
768 return q->request_fn || q->mq_ops;
769}
770
e692cb66
MP
771static inline unsigned int blk_queue_cluster(struct request_queue *q)
772{
773 return q->limits.cluster;
774}
775
797476b8
DLM
776static inline enum blk_zoned_model
777blk_queue_zoned_model(struct request_queue *q)
778{
779 return q->limits.zoned;
780}
781
782static inline bool blk_queue_is_zoned(struct request_queue *q)
783{
784 switch (blk_queue_zoned_model(q)) {
785 case BLK_ZONED_HA:
786 case BLK_ZONED_HM:
787 return true;
788 default:
789 return false;
790 }
791}
792
f99e8648 793static inline unsigned int blk_queue_zone_sectors(struct request_queue *q)
6a0cb1bc
HR
794{
795 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
796}
797
6a5ac984 798#ifdef CONFIG_BLK_DEV_ZONED
965b652e
DLM
799static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
800{
801 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
802}
803
6cc77e9c
CH
804static inline unsigned int blk_queue_zone_no(struct request_queue *q,
805 sector_t sector)
806{
807 if (!blk_queue_is_zoned(q))
808 return 0;
809 return sector >> ilog2(q->limits.chunk_sectors);
810}
811
812static inline bool blk_queue_zone_is_seq(struct request_queue *q,
813 sector_t sector)
814{
815 if (!blk_queue_is_zoned(q) || !q->seq_zones_bitmap)
816 return false;
817 return test_bit(blk_queue_zone_no(q, sector), q->seq_zones_bitmap);
818}
965b652e
DLM
819#else /* CONFIG_BLK_DEV_ZONED */
820static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
821{
822 return 0;
823}
6a5ac984 824#endif /* CONFIG_BLK_DEV_ZONED */
6cc77e9c 825
1faa16d2
JA
826static inline bool rq_is_sync(struct request *rq)
827{
ef295ecf 828 return op_is_sync(rq->cmd_flags);
1faa16d2
JA
829}
830
5b788ce3 831static inline bool blk_rl_full(struct request_list *rl, bool sync)
1da177e4 832{
5b788ce3
TH
833 unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
834
835 return rl->flags & flag;
1da177e4
LT
836}
837
5b788ce3 838static inline void blk_set_rl_full(struct request_list *rl, bool sync)
1da177e4 839{
5b788ce3
TH
840 unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
841
842 rl->flags |= flag;
1da177e4
LT
843}
844
5b788ce3 845static inline void blk_clear_rl_full(struct request_list *rl, bool sync)
1da177e4 846{
5b788ce3
TH
847 unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
848
849 rl->flags &= ~flag;
1da177e4
LT
850}
851
e2a60da7
MP
852static inline bool rq_mergeable(struct request *rq)
853{
57292b58 854 if (blk_rq_is_passthrough(rq))
e2a60da7 855 return false;
1da177e4 856
3a5e02ce
MC
857 if (req_op(rq) == REQ_OP_FLUSH)
858 return false;
859
a6f0788e
CK
860 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
861 return false;
862
e2a60da7 863 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
e8064021
CH
864 return false;
865 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
e2a60da7
MP
866 return false;
867
868 return true;
869}
1da177e4 870
4363ac7c
MP
871static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
872{
efbeccdb
CH
873 if (bio_page(a) == bio_page(b) &&
874 bio_offset(a) == bio_offset(b))
4363ac7c
MP
875 return true;
876
877 return false;
878}
879
d278d4a8
JA
880static inline unsigned int blk_queue_depth(struct request_queue *q)
881{
882 if (q->queue_depth)
883 return q->queue_depth;
884
885 return q->nr_requests;
886}
887
1da177e4
LT
888/*
889 * q->prep_rq_fn return values
890 */
0fb5b1fb
MP
891enum {
892 BLKPREP_OK, /* serve it */
893 BLKPREP_KILL, /* fatal error, kill, return -EIO */
894 BLKPREP_DEFER, /* leave on queue */
895 BLKPREP_INVALID, /* invalid command, kill, return -EREMOTEIO */
896};
1da177e4
LT
897
898extern unsigned long blk_max_low_pfn, blk_max_pfn;
899
900/*
901 * standard bounce addresses:
902 *
903 * BLK_BOUNCE_HIGH : bounce all highmem pages
904 * BLK_BOUNCE_ANY : don't bounce anything
905 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
906 */
2472892a
AK
907
908#if BITS_PER_LONG == 32
1da177e4 909#define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
2472892a
AK
910#else
911#define BLK_BOUNCE_HIGH -1ULL
912#endif
913#define BLK_BOUNCE_ANY (-1ULL)
bfe17231 914#define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
1da177e4 915
3d6392cf
JA
916/*
917 * default timeout for SG_IO if none specified
918 */
919#define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
f2f1fa78 920#define BLK_MIN_SG_TIMEOUT (7 * HZ)
3d6392cf 921
152e283f
FT
922struct rq_map_data {
923 struct page **pages;
924 int page_order;
925 int nr_entries;
56c451f4 926 unsigned long offset;
97ae77a1 927 int null_mapped;
ecb554a8 928 int from_user;
152e283f
FT
929};
930
5705f702 931struct req_iterator {
7988613b 932 struct bvec_iter iter;
5705f702
N
933 struct bio *bio;
934};
935
936/* This should not be used directly - use rq_for_each_segment */
1e428079
JA
937#define for_each_bio(_bio) \
938 for (; _bio; _bio = _bio->bi_next)
5705f702 939#define __rq_for_each_bio(_bio, rq) \
1da177e4
LT
940 if ((rq->bio)) \
941 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
942
5705f702
N
943#define rq_for_each_segment(bvl, _rq, _iter) \
944 __rq_for_each_bio(_iter.bio, _rq) \
7988613b 945 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
5705f702 946
4550dd6c 947#define rq_iter_last(bvec, _iter) \
7988613b 948 (_iter.bio->bi_next == NULL && \
4550dd6c 949 bio_iter_last(bvec, _iter.iter))
5705f702 950
2d4dc890
IL
951#ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
952# error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
953#endif
954#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
955extern void rq_flush_dcache_pages(struct request *rq);
956#else
957static inline void rq_flush_dcache_pages(struct request *rq)
958{
959}
960#endif
961
1da177e4
LT
962extern int blk_register_queue(struct gendisk *disk);
963extern void blk_unregister_queue(struct gendisk *disk);
dece1635 964extern blk_qc_t generic_make_request(struct bio *bio);
f421e1d9 965extern blk_qc_t direct_make_request(struct bio *bio);
2a4aa30c 966extern void blk_rq_init(struct request_queue *q, struct request *rq);
da8d7f07 967extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
1da177e4 968extern void blk_put_request(struct request *);
165125e1 969extern void __blk_put_request(struct request_queue *, struct request *);
cd6ce148 970extern struct request *blk_get_request(struct request_queue *, unsigned int op,
ff005a06 971 blk_mq_req_flags_t flags);
165125e1 972extern void blk_requeue_request(struct request_queue *, struct request *);
ef9e3fac 973extern int blk_lld_busy(struct request_queue *q);
78d8e58a
MS
974extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
975 struct bio_set *bs, gfp_t gfp_mask,
976 int (*bio_ctr)(struct bio *, struct bio *, void *),
977 void *data);
978extern void blk_rq_unprep_clone(struct request *rq);
2a842aca 979extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
82124d60 980 struct request *rq);
0abc2a10 981extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
3cca6dc1 982extern void blk_delay_queue(struct request_queue *, unsigned long);
af67c31f 983extern void blk_queue_split(struct request_queue *, struct bio **);
165125e1 984extern void blk_recount_segments(struct request_queue *, struct bio *);
0bfc96cb 985extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
577ebb37
PB
986extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
987 unsigned int, void __user *);
74f3c8af
AV
988extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
989 unsigned int, void __user *);
e915e872
AV
990extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
991 struct scsi_ioctl_command __user *);
3fcfab16 992
9a95e4ef 993extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
2e6edc95 994extern void blk_queue_exit(struct request_queue *q);
165125e1 995extern void blk_start_queue(struct request_queue *q);
21491412 996extern void blk_start_queue_async(struct request_queue *q);
165125e1 997extern void blk_stop_queue(struct request_queue *q);
1da177e4 998extern void blk_sync_queue(struct request_queue *q);
165125e1 999extern void __blk_stop_queue(struct request_queue *q);
24ecfbe2 1000extern void __blk_run_queue(struct request_queue *q);
a7928c15 1001extern void __blk_run_queue_uncond(struct request_queue *q);
165125e1 1002extern void blk_run_queue(struct request_queue *);
c21e6beb 1003extern void blk_run_queue_async(struct request_queue *q);
a3bce90e 1004extern int blk_rq_map_user(struct request_queue *, struct request *,
152e283f
FT
1005 struct rq_map_data *, void __user *, unsigned long,
1006 gfp_t);
8e5cfc45 1007extern int blk_rq_unmap_user(struct bio *);
165125e1
JA
1008extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
1009extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
26e49cfc
KO
1010 struct rq_map_data *, const struct iov_iter *,
1011 gfp_t);
b7819b92 1012extern void blk_execute_rq(struct request_queue *, struct gendisk *,
994ca9a1 1013 struct request *, int);
165125e1 1014extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
15fc858a 1015 struct request *, int, rq_end_io_fn *);
6e39b69e 1016
2a842aca
CH
1017int blk_status_to_errno(blk_status_t status);
1018blk_status_t errno_to_blk_status(int errno);
1019
ea435e1b 1020bool blk_poll(struct request_queue *q, blk_qc_t cookie);
05229bee 1021
165125e1 1022static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
1da177e4 1023{
ff9ea323 1024 return bdev->bd_disk->queue; /* this is never NULL */
1da177e4
LT
1025}
1026
233bde21
BVA
1027/*
1028 * The basic unit of block I/O is a sector. It is used in a number of contexts
1029 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
1030 * bytes. Variables of type sector_t represent an offset or size that is a
1031 * multiple of 512 bytes. Hence these two constants.
1032 */
1033#ifndef SECTOR_SHIFT
1034#define SECTOR_SHIFT 9
1035#endif
1036#ifndef SECTOR_SIZE
1037#define SECTOR_SIZE (1 << SECTOR_SHIFT)
1038#endif
1039
5efccd17 1040/*
80a761fd
TH
1041 * blk_rq_pos() : the current sector
1042 * blk_rq_bytes() : bytes left in the entire request
1043 * blk_rq_cur_bytes() : bytes left in the current segment
1044 * blk_rq_err_bytes() : bytes left till the next error boundary
1045 * blk_rq_sectors() : sectors left in the entire request
1046 * blk_rq_cur_sectors() : sectors left in the current segment
5efccd17 1047 */
5b93629b
TH
1048static inline sector_t blk_rq_pos(const struct request *rq)
1049{
a2dec7b3 1050 return rq->__sector;
2e46e8b2
TH
1051}
1052
1053static inline unsigned int blk_rq_bytes(const struct request *rq)
1054{
a2dec7b3 1055 return rq->__data_len;
5b93629b
TH
1056}
1057
2e46e8b2
TH
1058static inline int blk_rq_cur_bytes(const struct request *rq)
1059{
1060 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
1061}
5efccd17 1062
80a761fd
TH
1063extern unsigned int blk_rq_err_bytes(const struct request *rq);
1064
5b93629b
TH
1065static inline unsigned int blk_rq_sectors(const struct request *rq)
1066{
233bde21 1067 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
5b93629b
TH
1068}
1069
1070static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1071{
233bde21 1072 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
5b93629b
TH
1073}
1074
6a5ac984 1075#ifdef CONFIG_BLK_DEV_ZONED
6cc77e9c
CH
1076static inline unsigned int blk_rq_zone_no(struct request *rq)
1077{
1078 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
1079}
1080
1081static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
1082{
1083 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
1084}
6a5ac984 1085#endif /* CONFIG_BLK_DEV_ZONED */
6cc77e9c 1086
2e3258ec
CH
1087/*
1088 * Some commands like WRITE SAME have a payload or data transfer size which
1089 * is different from the size of the request. Any driver that supports such
1090 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1091 * calculate the data transfer size.
1092 */
1093static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1094{
1095 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1096 return rq->special_vec.bv_len;
1097 return blk_rq_bytes(rq);
1098}
1099
f31dc1cd 1100static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
8fe0d473 1101 int op)
f31dc1cd 1102{
7afafc8a 1103 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
233bde21
BVA
1104 return min(q->limits.max_discard_sectors,
1105 UINT_MAX >> SECTOR_SHIFT);
f31dc1cd 1106
8fe0d473 1107 if (unlikely(op == REQ_OP_WRITE_SAME))
4363ac7c
MP
1108 return q->limits.max_write_same_sectors;
1109
a6f0788e
CK
1110 if (unlikely(op == REQ_OP_WRITE_ZEROES))
1111 return q->limits.max_write_zeroes_sectors;
1112
f31dc1cd
MP
1113 return q->limits.max_sectors;
1114}
1115
762380ad
JA
1116/*
1117 * Return maximum size of a request at given offset. Only valid for
1118 * file system requests.
1119 */
1120static inline unsigned int blk_max_size_offset(struct request_queue *q,
1121 sector_t offset)
1122{
1123 if (!q->limits.chunk_sectors)
736ed4de 1124 return q->limits.max_sectors;
762380ad 1125
15bfd21f
KB
1126 return min(q->limits.max_sectors, (unsigned int)(q->limits.chunk_sectors -
1127 (offset & (q->limits.chunk_sectors - 1))));
762380ad
JA
1128}
1129
17007f39
DLM
1130static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1131 sector_t offset)
f31dc1cd
MP
1132{
1133 struct request_queue *q = rq->q;
1134
57292b58 1135 if (blk_rq_is_passthrough(rq))
f31dc1cd
MP
1136 return q->limits.max_hw_sectors;
1137
7afafc8a
AH
1138 if (!q->limits.chunk_sectors ||
1139 req_op(rq) == REQ_OP_DISCARD ||
1140 req_op(rq) == REQ_OP_SECURE_ERASE)
8fe0d473 1141 return blk_queue_get_max_sectors(q, req_op(rq));
762380ad 1142
17007f39 1143 return min(blk_max_size_offset(q, offset),
8fe0d473 1144 blk_queue_get_max_sectors(q, req_op(rq)));
f31dc1cd
MP
1145}
1146
75afb352
JN
1147static inline unsigned int blk_rq_count_bios(struct request *rq)
1148{
1149 unsigned int nr_bios = 0;
1150 struct bio *bio;
1151
1152 __rq_for_each_bio(bio, rq)
1153 nr_bios++;
1154
1155 return nr_bios;
1156}
1157
9934c8c0
TH
1158/*
1159 * Request issue related functions.
1160 */
1161extern struct request *blk_peek_request(struct request_queue *q);
1162extern void blk_start_request(struct request *rq);
1163extern struct request *blk_fetch_request(struct request_queue *q);
1164
ef71de8b
CH
1165void blk_steal_bios(struct bio_list *list, struct request *rq);
1166
1da177e4 1167/*
2e60e022
TH
1168 * Request completion related functions.
1169 *
1170 * blk_update_request() completes given number of bytes and updates
1171 * the request without completing it.
1172 *
f06d9a2b
TH
1173 * blk_end_request() and friends. __blk_end_request() must be called
1174 * with the request queue spinlock acquired.
1da177e4
LT
1175 *
1176 * Several drivers define their own end_request and call
3bcddeac
KU
1177 * blk_end_request() for parts of the original function.
1178 * This prevents code duplication in drivers.
1da177e4 1179 */
2a842aca 1180extern bool blk_update_request(struct request *rq, blk_status_t error,
2e60e022 1181 unsigned int nr_bytes);
2a842aca
CH
1182extern void blk_finish_request(struct request *rq, blk_status_t error);
1183extern bool blk_end_request(struct request *rq, blk_status_t error,
b1f74493 1184 unsigned int nr_bytes);
2a842aca
CH
1185extern void blk_end_request_all(struct request *rq, blk_status_t error);
1186extern bool __blk_end_request(struct request *rq, blk_status_t error,
b1f74493 1187 unsigned int nr_bytes);
2a842aca
CH
1188extern void __blk_end_request_all(struct request *rq, blk_status_t error);
1189extern bool __blk_end_request_cur(struct request *rq, blk_status_t error);
2e60e022 1190
242f9dcb
JA
1191extern void __blk_complete_request(struct request *);
1192extern void blk_abort_request(struct request *);
28018c24 1193extern void blk_unprep_request(struct request *);
ff856bad 1194
1da177e4
LT
1195/*
1196 * Access functions for manipulating queue properties
1197 */
165125e1 1198extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn,
1946089a 1199 spinlock_t *lock, int node_id);
165125e1 1200extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *);
5ea708d1 1201extern int blk_init_allocated_queue(struct request_queue *);
165125e1
JA
1202extern void blk_cleanup_queue(struct request_queue *);
1203extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
1204extern void blk_queue_bounce_limit(struct request_queue *, u64);
086fa5ff 1205extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
762380ad 1206extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
8a78362c 1207extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1e739730
CH
1208extern void blk_queue_max_discard_segments(struct request_queue *,
1209 unsigned short);
165125e1 1210extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
67efc925
CH
1211extern void blk_queue_max_discard_sectors(struct request_queue *q,
1212 unsigned int max_discard_sectors);
4363ac7c
MP
1213extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1214 unsigned int max_write_same_sectors);
a6f0788e
CK
1215extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1216 unsigned int max_write_same_sectors);
e1defc4f 1217extern void blk_queue_logical_block_size(struct request_queue *, unsigned short);
892b6f90 1218extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
c72758f3
MP
1219extern void blk_queue_alignment_offset(struct request_queue *q,
1220 unsigned int alignment);
7c958e32 1221extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
c72758f3 1222extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
3c5820c7 1223extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
c72758f3 1224extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
d278d4a8 1225extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
e475bba2 1226extern void blk_set_default_limits(struct queue_limits *lim);
b1bd055d 1227extern void blk_set_stacking_limits(struct queue_limits *lim);
c72758f3
MP
1228extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1229 sector_t offset);
17be8c24
MP
1230extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
1231 sector_t offset);
c72758f3
MP
1232extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1233 sector_t offset);
165125e1 1234extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
e3790c7d 1235extern void blk_queue_dma_pad(struct request_queue *, unsigned int);
27f8221a 1236extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
2fb98e84
TH
1237extern int blk_queue_dma_drain(struct request_queue *q,
1238 dma_drain_needed_fn *dma_drain_needed,
1239 void *buf, unsigned int size);
165125e1 1240extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
03100aad 1241extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
165125e1 1242extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn);
28018c24 1243extern void blk_queue_unprep_rq(struct request_queue *, unprep_rq_fn *ufn);
165125e1 1244extern void blk_queue_dma_alignment(struct request_queue *, int);
11c3e689 1245extern void blk_queue_update_dma_alignment(struct request_queue *, int);
165125e1 1246extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *);
242f9dcb
JA
1247extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *);
1248extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
f3876930 1249extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable);
93e9d8e8 1250extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1da177e4 1251
1e739730
CH
1252/*
1253 * Number of physical segments as sent to the device.
1254 *
1255 * Normally this is the number of discontiguous data segments sent by the
1256 * submitter. But for data-less command like discard we might have no
1257 * actual data segments submitted, but the driver might have to add it's
1258 * own special payload. In that case we still return 1 here so that this
1259 * special payload will be mapped.
1260 */
f9d03f96
CH
1261static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1262{
1263 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1264 return 1;
1265 return rq->nr_phys_segments;
1266}
1267
1e739730
CH
1268/*
1269 * Number of discard segments (or ranges) the driver needs to fill in.
1270 * Each discard bio merged into a request is counted as one segment.
1271 */
1272static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1273{
1274 return max_t(unsigned short, rq->nr_phys_segments, 1);
1275}
1276
165125e1 1277extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
1da177e4 1278extern void blk_dump_rq_flags(struct request *, char *);
1da177e4 1279extern long nr_blockdev_pages(void);
1da177e4 1280
09ac46c4 1281bool __must_check blk_get_queue(struct request_queue *);
165125e1 1282struct request_queue *blk_alloc_queue(gfp_t);
5ee0524b
BVA
1283struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id,
1284 spinlock_t *lock);
165125e1 1285extern void blk_put_queue(struct request_queue *);
3f21c265 1286extern void blk_set_queue_dying(struct request_queue *);
1da177e4 1287
316cc67d 1288/*
75df7136
SJ
1289 * blk_plug permits building a queue of related requests by holding the I/O
1290 * fragments for a short period. This allows merging of sequential requests
1291 * into single larger request. As the requests are moved from a per-task list to
1292 * the device's request_queue in a batch, this results in improved scalability
1293 * as the lock contention for request_queue lock is reduced.
1294 *
1295 * It is ok not to disable preemption when adding the request to the plug list
1296 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1297 * the plug list when the task sleeps by itself. For details, please see
1298 * schedule() where blk_schedule_flush_plug() is called.
316cc67d 1299 */
73c10101 1300struct blk_plug {
75df7136 1301 struct list_head list; /* requests */
320ae51f 1302 struct list_head mq_list; /* blk-mq requests */
75df7136 1303 struct list_head cb_list; /* md requires an unplug callback */
73c10101 1304};
55c022bb 1305#define BLK_MAX_REQUEST_COUNT 16
50d24c34 1306#define BLK_PLUG_FLUSH_SIZE (128 * 1024)
55c022bb 1307
9cbb1750 1308struct blk_plug_cb;
74018dc3 1309typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
048c9374
N
1310struct blk_plug_cb {
1311 struct list_head list;
9cbb1750
N
1312 blk_plug_cb_fn callback;
1313 void *data;
048c9374 1314};
9cbb1750
N
1315extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1316 void *data, int size);
73c10101
JA
1317extern void blk_start_plug(struct blk_plug *);
1318extern void blk_finish_plug(struct blk_plug *);
f6603783 1319extern void blk_flush_plug_list(struct blk_plug *, bool);
73c10101
JA
1320
1321static inline void blk_flush_plug(struct task_struct *tsk)
1322{
1323 struct blk_plug *plug = tsk->plug;
1324
a237c1c5
JA
1325 if (plug)
1326 blk_flush_plug_list(plug, false);
1327}
1328
1329static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1330{
1331 struct blk_plug *plug = tsk->plug;
1332
88b996cd 1333 if (plug)
f6603783 1334 blk_flush_plug_list(plug, true);
73c10101
JA
1335}
1336
1337static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1338{
1339 struct blk_plug *plug = tsk->plug;
1340
320ae51f
JA
1341 return plug &&
1342 (!list_empty(&plug->list) ||
1343 !list_empty(&plug->mq_list) ||
1344 !list_empty(&plug->cb_list));
73c10101
JA
1345}
1346
ee472d83
CH
1347extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
1348extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1349 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
e950fdf7
CH
1350
1351#define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
dd3932ed 1352
fbd9b09a
DM
1353extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1354 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
38f25255 1355extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
288dab8a 1356 sector_t nr_sects, gfp_t gfp_mask, int flags,
469e3216 1357 struct bio **biop);
ee472d83
CH
1358
1359#define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
cb365b96 1360#define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
ee472d83 1361
e73c23ff
CK
1362extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1363 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
ee472d83 1364 unsigned flags);
3f14d792 1365extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
ee472d83
CH
1366 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1367
2cf6d26a
CH
1368static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1369 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
fb2dce86 1370{
233bde21
BVA
1371 return blkdev_issue_discard(sb->s_bdev,
1372 block << (sb->s_blocksize_bits -
1373 SECTOR_SHIFT),
1374 nr_blocks << (sb->s_blocksize_bits -
1375 SECTOR_SHIFT),
2cf6d26a 1376 gfp_mask, flags);
fb2dce86 1377}
e6fa0be6 1378static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
a107e5a3 1379 sector_t nr_blocks, gfp_t gfp_mask)
e6fa0be6
LC
1380{
1381 return blkdev_issue_zeroout(sb->s_bdev,
233bde21
BVA
1382 block << (sb->s_blocksize_bits -
1383 SECTOR_SHIFT),
1384 nr_blocks << (sb->s_blocksize_bits -
1385 SECTOR_SHIFT),
ee472d83 1386 gfp_mask, 0);
e6fa0be6 1387}
1da177e4 1388
f00c4d80 1389extern int blk_verify_command(unsigned char *cmd, fmode_t mode);
0b07de85 1390
eb28d31b
MP
1391enum blk_default_limits {
1392 BLK_MAX_SEGMENTS = 128,
1393 BLK_SAFE_MAX_SECTORS = 255,
d2be537c 1394 BLK_DEF_MAX_SECTORS = 2560,
eb28d31b
MP
1395 BLK_MAX_SEGMENT_SIZE = 65536,
1396 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1397};
0e435ac2 1398
ae03bf63
MP
1399static inline unsigned long queue_segment_boundary(struct request_queue *q)
1400{
025146e1 1401 return q->limits.seg_boundary_mask;
ae03bf63
MP
1402}
1403
03100aad
KB
1404static inline unsigned long queue_virt_boundary(struct request_queue *q)
1405{
1406 return q->limits.virt_boundary_mask;
1407}
1408
ae03bf63
MP
1409static inline unsigned int queue_max_sectors(struct request_queue *q)
1410{
025146e1 1411 return q->limits.max_sectors;
ae03bf63
MP
1412}
1413
1414static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
1415{
025146e1 1416 return q->limits.max_hw_sectors;
ae03bf63
MP
1417}
1418
8a78362c 1419static inline unsigned short queue_max_segments(struct request_queue *q)
ae03bf63 1420{
8a78362c 1421 return q->limits.max_segments;
ae03bf63
MP
1422}
1423
1e739730
CH
1424static inline unsigned short queue_max_discard_segments(struct request_queue *q)
1425{
1426 return q->limits.max_discard_segments;
1427}
1428
ae03bf63
MP
1429static inline unsigned int queue_max_segment_size(struct request_queue *q)
1430{
025146e1 1431 return q->limits.max_segment_size;
ae03bf63
MP
1432}
1433
e1defc4f 1434static inline unsigned short queue_logical_block_size(struct request_queue *q)
1da177e4
LT
1435{
1436 int retval = 512;
1437
025146e1
MP
1438 if (q && q->limits.logical_block_size)
1439 retval = q->limits.logical_block_size;
1da177e4
LT
1440
1441 return retval;
1442}
1443
e1defc4f 1444static inline unsigned short bdev_logical_block_size(struct block_device *bdev)
1da177e4 1445{
e1defc4f 1446 return queue_logical_block_size(bdev_get_queue(bdev));
1da177e4
LT
1447}
1448
c72758f3
MP
1449static inline unsigned int queue_physical_block_size(struct request_queue *q)
1450{
1451 return q->limits.physical_block_size;
1452}
1453
892b6f90 1454static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
ac481c20
MP
1455{
1456 return queue_physical_block_size(bdev_get_queue(bdev));
1457}
1458
c72758f3
MP
1459static inline unsigned int queue_io_min(struct request_queue *q)
1460{
1461 return q->limits.io_min;
1462}
1463
ac481c20
MP
1464static inline int bdev_io_min(struct block_device *bdev)
1465{
1466 return queue_io_min(bdev_get_queue(bdev));
1467}
1468
c72758f3
MP
1469static inline unsigned int queue_io_opt(struct request_queue *q)
1470{
1471 return q->limits.io_opt;
1472}
1473
ac481c20
MP
1474static inline int bdev_io_opt(struct block_device *bdev)
1475{
1476 return queue_io_opt(bdev_get_queue(bdev));
1477}
1478
c72758f3
MP
1479static inline int queue_alignment_offset(struct request_queue *q)
1480{
ac481c20 1481 if (q->limits.misaligned)
c72758f3
MP
1482 return -1;
1483
ac481c20 1484 return q->limits.alignment_offset;
c72758f3
MP
1485}
1486
e03a72e1 1487static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
81744ee4
MP
1488{
1489 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
233bde21
BVA
1490 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1491 << SECTOR_SHIFT;
81744ee4 1492
b8839b8c 1493 return (granularity + lim->alignment_offset - alignment) % granularity;
c72758f3
MP
1494}
1495
ac481c20
MP
1496static inline int bdev_alignment_offset(struct block_device *bdev)
1497{
1498 struct request_queue *q = bdev_get_queue(bdev);
1499
1500 if (q->limits.misaligned)
1501 return -1;
1502
1503 if (bdev != bdev->bd_contains)
1504 return bdev->bd_part->alignment_offset;
1505
1506 return q->limits.alignment_offset;
1507}
1508
86b37281
MP
1509static inline int queue_discard_alignment(struct request_queue *q)
1510{
1511 if (q->limits.discard_misaligned)
1512 return -1;
1513
1514 return q->limits.discard_alignment;
1515}
1516
e03a72e1 1517static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
86b37281 1518{
59771079 1519 unsigned int alignment, granularity, offset;
dd3d145d 1520
a934a00a
MP
1521 if (!lim->max_discard_sectors)
1522 return 0;
1523
59771079 1524 /* Why are these in bytes, not sectors? */
233bde21
BVA
1525 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1526 granularity = lim->discard_granularity >> SECTOR_SHIFT;
59771079
LT
1527 if (!granularity)
1528 return 0;
1529
1530 /* Offset of the partition start in 'granularity' sectors */
1531 offset = sector_div(sector, granularity);
1532
1533 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1534 offset = (granularity + alignment - offset) % granularity;
1535
1536 /* Turn it back into bytes, gaah */
233bde21 1537 return offset << SECTOR_SHIFT;
86b37281
MP
1538}
1539
c6e66634
PB
1540static inline int bdev_discard_alignment(struct block_device *bdev)
1541{
1542 struct request_queue *q = bdev_get_queue(bdev);
1543
1544 if (bdev != bdev->bd_contains)
1545 return bdev->bd_part->discard_alignment;
1546
1547 return q->limits.discard_alignment;
1548}
1549
4363ac7c
MP
1550static inline unsigned int bdev_write_same(struct block_device *bdev)
1551{
1552 struct request_queue *q = bdev_get_queue(bdev);
1553
1554 if (q)
1555 return q->limits.max_write_same_sectors;
1556
1557 return 0;
1558}
1559
a6f0788e
CK
1560static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1561{
1562 struct request_queue *q = bdev_get_queue(bdev);
1563
1564 if (q)
1565 return q->limits.max_write_zeroes_sectors;
1566
1567 return 0;
1568}
1569
797476b8
DLM
1570static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1571{
1572 struct request_queue *q = bdev_get_queue(bdev);
1573
1574 if (q)
1575 return blk_queue_zoned_model(q);
1576
1577 return BLK_ZONED_NONE;
1578}
1579
1580static inline bool bdev_is_zoned(struct block_device *bdev)
1581{
1582 struct request_queue *q = bdev_get_queue(bdev);
1583
1584 if (q)
1585 return blk_queue_is_zoned(q);
1586
1587 return false;
1588}
1589
f99e8648 1590static inline unsigned int bdev_zone_sectors(struct block_device *bdev)
6a0cb1bc
HR
1591{
1592 struct request_queue *q = bdev_get_queue(bdev);
1593
1594 if (q)
f99e8648 1595 return blk_queue_zone_sectors(q);
6cc77e9c
CH
1596 return 0;
1597}
6a0cb1bc 1598
165125e1 1599static inline int queue_dma_alignment(struct request_queue *q)
1da177e4 1600{
482eb689 1601 return q ? q->dma_alignment : 511;
1da177e4
LT
1602}
1603
14417799 1604static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
87904074
FT
1605 unsigned int len)
1606{
1607 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
14417799 1608 return !(addr & alignment) && !(len & alignment);
87904074
FT
1609}
1610
1da177e4
LT
1611/* assumes size > 256 */
1612static inline unsigned int blksize_bits(unsigned int size)
1613{
1614 unsigned int bits = 8;
1615 do {
1616 bits++;
1617 size >>= 1;
1618 } while (size > 256);
1619 return bits;
1620}
1621
2befb9e3 1622static inline unsigned int block_size(struct block_device *bdev)
1da177e4
LT
1623{
1624 return bdev->bd_block_size;
1625}
1626
f3876930 1627static inline bool queue_flush_queueable(struct request_queue *q)
1628{
c888a8f9 1629 return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
f3876930 1630}
1631
1da177e4
LT
1632typedef struct {struct page *v;} Sector;
1633
1634unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
1635
1636static inline void put_dev_sector(Sector p)
1637{
09cbfeaf 1638 put_page(p.v);
1da177e4
LT
1639}
1640
59c3d45e 1641int kblockd_schedule_work(struct work_struct *work);
ee63cfa7 1642int kblockd_schedule_work_on(int cpu, struct work_struct *work);
818cd1cb 1643int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1da177e4 1644
1da177e4
LT
1645#define MODULE_ALIAS_BLOCKDEV(major,minor) \
1646 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1647#define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1648 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1649
7ba1ba12
MP
1650#if defined(CONFIG_BLK_DEV_INTEGRITY)
1651
8288f496
MP
1652enum blk_integrity_flags {
1653 BLK_INTEGRITY_VERIFY = 1 << 0,
1654 BLK_INTEGRITY_GENERATE = 1 << 1,
3aec2f41 1655 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
aae7df50 1656 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
8288f496 1657};
7ba1ba12 1658
18593088 1659struct blk_integrity_iter {
7ba1ba12
MP
1660 void *prot_buf;
1661 void *data_buf;
3be91c4a 1662 sector_t seed;
7ba1ba12 1663 unsigned int data_size;
3be91c4a 1664 unsigned short interval;
7ba1ba12
MP
1665 const char *disk_name;
1666};
1667
4e4cbee9 1668typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
7ba1ba12 1669
0f8087ec
MP
1670struct blk_integrity_profile {
1671 integrity_processing_fn *generate_fn;
1672 integrity_processing_fn *verify_fn;
1673 const char *name;
1674};
7ba1ba12 1675
25520d55 1676extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
7ba1ba12 1677extern void blk_integrity_unregister(struct gendisk *);
ad7fce93 1678extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
13f05c8d
MP
1679extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1680 struct scatterlist *);
1681extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
4eaf99be
MP
1682extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
1683 struct request *);
1684extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
1685 struct bio *);
7ba1ba12 1686
25520d55 1687static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
b04accc4 1688{
ac6fc48c 1689 struct blk_integrity *bi = &disk->queue->integrity;
25520d55
MP
1690
1691 if (!bi->profile)
1692 return NULL;
1693
1694 return bi;
b04accc4
JA
1695}
1696
25520d55
MP
1697static inline
1698struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
b02739b0 1699{
25520d55 1700 return blk_get_integrity(bdev->bd_disk);
b02739b0
MP
1701}
1702
180b2f95 1703static inline bool blk_integrity_rq(struct request *rq)
7ba1ba12 1704{
180b2f95 1705 return rq->cmd_flags & REQ_INTEGRITY;
7ba1ba12
MP
1706}
1707
13f05c8d
MP
1708static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1709 unsigned int segs)
1710{
1711 q->limits.max_integrity_segments = segs;
1712}
1713
1714static inline unsigned short
1715queue_max_integrity_segments(struct request_queue *q)
1716{
1717 return q->limits.max_integrity_segments;
1718}
1719
359f6427
GE
1720/**
1721 * bio_integrity_intervals - Return number of integrity intervals for a bio
1722 * @bi: blk_integrity profile for device
1723 * @sectors: Size of the bio in 512-byte sectors
1724 *
1725 * Description: The block layer calculates everything in 512 byte
1726 * sectors but integrity metadata is done in terms of the data integrity
1727 * interval size of the storage device. Convert the block layer sectors
1728 * to the appropriate number of integrity intervals.
1729 */
1730static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1731 unsigned int sectors)
1732{
1733 return sectors >> (bi->interval_exp - 9);
1734}
1735
1736static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1737 unsigned int sectors)
1738{
1739 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1740}
1741
7ba1ba12
MP
1742#else /* CONFIG_BLK_DEV_INTEGRITY */
1743
fd83240a
SR
1744struct bio;
1745struct block_device;
1746struct gendisk;
1747struct blk_integrity;
1748
1749static inline int blk_integrity_rq(struct request *rq)
1750{
1751 return 0;
1752}
1753static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1754 struct bio *b)
1755{
1756 return 0;
1757}
1758static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1759 struct bio *b,
1760 struct scatterlist *s)
1761{
1762 return 0;
1763}
1764static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1765{
61a04e5b 1766 return NULL;
fd83240a
SR
1767}
1768static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1769{
1770 return NULL;
1771}
1772static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1773{
1774 return 0;
1775}
25520d55 1776static inline void blk_integrity_register(struct gendisk *d,
fd83240a
SR
1777 struct blk_integrity *b)
1778{
fd83240a
SR
1779}
1780static inline void blk_integrity_unregister(struct gendisk *d)
1781{
1782}
1783static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1784 unsigned int segs)
1785{
1786}
1787static inline unsigned short queue_max_integrity_segments(struct request_queue *q)
1788{
1789 return 0;
1790}
4eaf99be
MP
1791static inline bool blk_integrity_merge_rq(struct request_queue *rq,
1792 struct request *r1,
1793 struct request *r2)
fd83240a 1794{
cb1a5ab6 1795 return true;
fd83240a 1796}
4eaf99be
MP
1797static inline bool blk_integrity_merge_bio(struct request_queue *rq,
1798 struct request *r,
1799 struct bio *b)
fd83240a 1800{
cb1a5ab6 1801 return true;
fd83240a 1802}
25520d55 1803
359f6427
GE
1804static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1805 unsigned int sectors)
1806{
1807 return 0;
1808}
1809
1810static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1811 unsigned int sectors)
1812{
1813 return 0;
1814}
1815
7ba1ba12
MP
1816#endif /* CONFIG_BLK_DEV_INTEGRITY */
1817
08f85851 1818struct block_device_operations {
d4430d62 1819 int (*open) (struct block_device *, fmode_t);
db2a144b 1820 void (*release) (struct gendisk *, fmode_t);
3f289dcb 1821 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
d4430d62
AV
1822 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1823 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
77ea887e
TH
1824 unsigned int (*check_events) (struct gendisk *disk,
1825 unsigned int clearing);
1826 /* ->media_changed() is DEPRECATED, use ->check_events() instead */
08f85851 1827 int (*media_changed) (struct gendisk *);
c3e33e04 1828 void (*unlock_native_capacity) (struct gendisk *);
08f85851
AV
1829 int (*revalidate_disk) (struct gendisk *);
1830 int (*getgeo)(struct block_device *, struct hd_geometry *);
b3a27d05
NG
1831 /* this callback is with swap_lock and sometimes page table lock held */
1832 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
e76239a3
CH
1833 int (*report_zones)(struct gendisk *, sector_t sector,
1834 struct blk_zone *zones, unsigned int *nr_zones,
1835 gfp_t gfp_mask);
08f85851 1836 struct module *owner;
bbd3e064 1837 const struct pr_ops *pr_ops;
08f85851
AV
1838};
1839
633a08b8
AV
1840extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
1841 unsigned long);
47a191fd
MW
1842extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1843extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1844 struct writeback_control *);
6cc77e9c
CH
1845
1846#ifdef CONFIG_BLK_DEV_ZONED
1847bool blk_req_needs_zone_write_lock(struct request *rq);
1848void __blk_req_zone_write_lock(struct request *rq);
1849void __blk_req_zone_write_unlock(struct request *rq);
1850
1851static inline void blk_req_zone_write_lock(struct request *rq)
1852{
1853 if (blk_req_needs_zone_write_lock(rq))
1854 __blk_req_zone_write_lock(rq);
1855}
1856
1857static inline void blk_req_zone_write_unlock(struct request *rq)
1858{
1859 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1860 __blk_req_zone_write_unlock(rq);
1861}
1862
1863static inline bool blk_req_zone_is_write_locked(struct request *rq)
1864{
1865 return rq->q->seq_zones_wlock &&
1866 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1867}
1868
1869static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1870{
1871 if (!blk_req_needs_zone_write_lock(rq))
1872 return true;
1873 return !blk_req_zone_is_write_locked(rq);
1874}
1875#else
1876static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1877{
1878 return false;
1879}
1880
1881static inline void blk_req_zone_write_lock(struct request *rq)
1882{
1883}
1884
1885static inline void blk_req_zone_write_unlock(struct request *rq)
1886{
1887}
1888static inline bool blk_req_zone_is_write_locked(struct request *rq)
1889{
1890 return false;
1891}
1892
1893static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1894{
1895 return true;
1896}
1897#endif /* CONFIG_BLK_DEV_ZONED */
1898
9361401e 1899#else /* CONFIG_BLOCK */
ac13a829
FF
1900
1901struct block_device;
1902
9361401e
DH
1903/*
1904 * stubs for when the block layer is configured out
1905 */
1906#define buffer_heads_over_limit 0
1907
9361401e
DH
1908static inline long nr_blockdev_pages(void)
1909{
1910 return 0;
1911}
1912
1f940bdf
JA
1913struct blk_plug {
1914};
1915
1916static inline void blk_start_plug(struct blk_plug *plug)
73c10101
JA
1917{
1918}
1919
1f940bdf 1920static inline void blk_finish_plug(struct blk_plug *plug)
73c10101
JA
1921{
1922}
1923
1f940bdf 1924static inline void blk_flush_plug(struct task_struct *task)
73c10101
JA
1925{
1926}
1927
a237c1c5
JA
1928static inline void blk_schedule_flush_plug(struct task_struct *task)
1929{
1930}
1931
1932
73c10101
JA
1933static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1934{
1935 return false;
1936}
1937
ac13a829
FF
1938static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
1939 sector_t *error_sector)
1940{
1941 return 0;
1942}
1943
9361401e
DH
1944#endif /* CONFIG_BLOCK */
1945
1da177e4 1946#endif