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