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