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