GNU Linux-libre 4.14.319-gnu1
[releases.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_wb;
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 consisitent 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          3
58
59 static inline int blk_validate_block_size(unsigned int bsize)
60 {
61         if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
62                 return -EINVAL;
63
64         return 0;
65 }
66
67 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
68
69 #define BLK_RL_SYNCFULL         (1U << 0)
70 #define BLK_RL_ASYNCFULL        (1U << 1)
71
72 struct request_list {
73         struct request_queue    *q;     /* the queue this rl belongs to */
74 #ifdef CONFIG_BLK_CGROUP
75         struct blkcg_gq         *blkg;  /* blkg this request pool belongs to */
76 #endif
77         /*
78          * count[], starved[], and wait[] are indexed by
79          * BLK_RW_SYNC/BLK_RW_ASYNC
80          */
81         int                     count[2];
82         int                     starved[2];
83         mempool_t               *rq_pool;
84         wait_queue_head_t       wait[2];
85         unsigned int            flags;
86 };
87
88 /*
89  * request flags */
90 typedef __u32 __bitwise req_flags_t;
91
92 /* elevator knows about this request */
93 #define RQF_SORTED              ((__force req_flags_t)(1 << 0))
94 /* drive already may have started this one */
95 #define RQF_STARTED             ((__force req_flags_t)(1 << 1))
96 /* uses tagged queueing */
97 #define RQF_QUEUED              ((__force req_flags_t)(1 << 2))
98 /* may not be passed by ioscheduler */
99 #define RQF_SOFTBARRIER         ((__force req_flags_t)(1 << 3))
100 /* request for flush sequence */
101 #define RQF_FLUSH_SEQ           ((__force req_flags_t)(1 << 4))
102 /* merge of different types, fail separately */
103 #define RQF_MIXED_MERGE         ((__force req_flags_t)(1 << 5))
104 /* track inflight for MQ */
105 #define RQF_MQ_INFLIGHT         ((__force req_flags_t)(1 << 6))
106 /* don't call prep for this one */
107 #define RQF_DONTPREP            ((__force req_flags_t)(1 << 7))
108 /* set for "ide_preempt" requests and also for requests for which the SCSI
109    "quiesce" state must be ignored. */
110 #define RQF_PREEMPT             ((__force req_flags_t)(1 << 8))
111 /* contains copies of user pages */
112 #define RQF_COPY_USER           ((__force req_flags_t)(1 << 9))
113 /* vaguely specified driver internal error.  Ignored by the block layer */
114 #define RQF_FAILED              ((__force req_flags_t)(1 << 10))
115 /* don't warn about errors */
116 #define RQF_QUIET               ((__force req_flags_t)(1 << 11))
117 /* elevator private data attached */
118 #define RQF_ELVPRIV             ((__force req_flags_t)(1 << 12))
119 /* account I/O stat */
120 #define RQF_IO_STAT             ((__force req_flags_t)(1 << 13))
121 /* request came from our alloc pool */
122 #define RQF_ALLOCED             ((__force req_flags_t)(1 << 14))
123 /* runtime pm request */
124 #define RQF_PM                  ((__force req_flags_t)(1 << 15))
125 /* on IO scheduler merge hash */
126 #define RQF_HASHED              ((__force req_flags_t)(1 << 16))
127 /* IO stats tracking on */
128 #define RQF_STATS               ((__force req_flags_t)(1 << 17))
129 /* Look at ->special_vec for the actual data payload instead of the
130    bio chain. */
131 #define RQF_SPECIAL_PAYLOAD     ((__force req_flags_t)(1 << 18))
132
133 /* flags that prevent us from merging requests: */
134 #define RQF_NOMERGE_FLAGS \
135         (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
136
137 /*
138  * Try to put the fields that are referenced together in the same cacheline.
139  *
140  * If you modify this structure, make sure to update blk_rq_init() and
141  * especially blk_mq_rq_ctx_init() to take care of the added fields.
142  */
143 struct request {
144         struct list_head queuelist;
145         union {
146                 struct __call_single_data csd;
147                 u64 fifo_time;
148         };
149
150         struct request_queue *q;
151         struct blk_mq_ctx *mq_ctx;
152
153         int cpu;
154         unsigned int cmd_flags;         /* op and common flags */
155         req_flags_t rq_flags;
156
157         int internal_tag;
158
159         unsigned long atomic_flags;
160
161         /* the following two fields are internal, NEVER access directly */
162         unsigned int __data_len;        /* total data len */
163         int tag;
164         sector_t __sector;              /* sector cursor */
165
166         struct bio *bio;
167         struct bio *biotail;
168
169         /*
170          * The hash is used inside the scheduler, and killed once the
171          * request reaches the dispatch list. The ipi_list is only used
172          * to queue the request for softirq completion, which is long
173          * after the request has been unhashed (and even removed from
174          * the dispatch list).
175          */
176         union {
177                 struct hlist_node hash; /* merge hash */
178                 struct list_head ipi_list;
179         };
180
181         /*
182          * The rb_node is only used inside the io scheduler, requests
183          * are pruned when moved to the dispatch queue. So let the
184          * completion_data share space with the rb_node.
185          */
186         union {
187                 struct rb_node rb_node; /* sort/lookup */
188                 struct bio_vec special_vec;
189                 void *completion_data;
190                 int error_count; /* for legacy drivers, don't use */
191         };
192
193         /*
194          * Three pointers are available for the IO schedulers, if they need
195          * more they have to dynamically allocate it.  Flush requests are
196          * never put on the IO scheduler. So let the flush fields share
197          * space with the elevator data.
198          */
199         union {
200                 struct {
201                         struct io_cq            *icq;
202                         void                    *priv[2];
203                 } elv;
204
205                 struct {
206                         unsigned int            seq;
207                         struct list_head        list;
208                         rq_end_io_fn            *saved_end_io;
209                 } flush;
210         };
211
212         struct gendisk *rq_disk;
213         struct hd_struct *part;
214         unsigned long start_time;
215         struct blk_issue_stat issue_stat;
216 #ifdef CONFIG_BLK_CGROUP
217         struct request_list *rl;                /* rl this rq is alloced from */
218         unsigned long long start_time_ns;
219         unsigned long long io_start_time_ns;    /* when passed to hardware */
220 #endif
221         /* Number of scatter-gather DMA addr+len pairs after
222          * physical address coalescing is performed.
223          */
224         unsigned short nr_phys_segments;
225 #if defined(CONFIG_BLK_DEV_INTEGRITY)
226         unsigned short nr_integrity_segments;
227 #endif
228
229         unsigned short ioprio;
230
231         unsigned int timeout;
232
233         void *special;          /* opaque pointer available for LLD use */
234
235         unsigned int extra_len; /* length of alignment and padding */
236
237         unsigned short write_hint;
238
239         unsigned long deadline;
240         struct list_head timeout_list;
241
242         /*
243          * completion callback.
244          */
245         rq_end_io_fn *end_io;
246         void *end_io_data;
247
248         /* for bidi */
249         struct request *next_rq;
250 };
251
252 static inline bool blk_op_is_scsi(unsigned int op)
253 {
254         return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
255 }
256
257 static inline bool blk_op_is_private(unsigned int op)
258 {
259         return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
260 }
261
262 static inline bool blk_rq_is_scsi(struct request *rq)
263 {
264         return blk_op_is_scsi(req_op(rq));
265 }
266
267 static inline bool blk_rq_is_private(struct request *rq)
268 {
269         return blk_op_is_private(req_op(rq));
270 }
271
272 static inline bool blk_rq_is_passthrough(struct request *rq)
273 {
274         return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
275 }
276
277 static inline bool bio_is_passthrough(struct bio *bio)
278 {
279         unsigned op = bio_op(bio);
280
281         return blk_op_is_scsi(op) || blk_op_is_private(op);
282 }
283
284 static inline unsigned short req_get_ioprio(struct request *req)
285 {
286         return req->ioprio;
287 }
288
289 #include <linux/elevator.h>
290
291 struct blk_queue_ctx;
292
293 typedef void (request_fn_proc) (struct request_queue *q);
294 typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
295 typedef int (prep_rq_fn) (struct request_queue *, struct request *);
296 typedef void (unprep_rq_fn) (struct request_queue *, struct request *);
297
298 struct bio_vec;
299 typedef void (softirq_done_fn)(struct request *);
300 typedef int (dma_drain_needed_fn)(struct request *);
301 typedef int (lld_busy_fn) (struct request_queue *q);
302 typedef int (bsg_job_fn) (struct bsg_job *);
303 typedef int (init_rq_fn)(struct request_queue *, struct request *, gfp_t);
304 typedef void (exit_rq_fn)(struct request_queue *, struct request *);
305
306 enum blk_eh_timer_return {
307         BLK_EH_NOT_HANDLED,
308         BLK_EH_HANDLED,
309         BLK_EH_RESET_TIMER,
310 };
311
312 typedef enum blk_eh_timer_return (rq_timed_out_fn)(struct request *);
313
314 enum blk_queue_state {
315         Queue_down,
316         Queue_up,
317 };
318
319 struct blk_queue_tag {
320         struct request **tag_index;     /* map of busy tags */
321         unsigned long *tag_map;         /* bit map of free/busy tags */
322         int max_depth;                  /* what we will send to device */
323         int real_max_depth;             /* what the array can hold */
324         atomic_t refcnt;                /* map can be shared */
325         int alloc_policy;               /* tag allocation policy */
326         int next_tag;                   /* next tag */
327 };
328 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
329 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
330
331 #define BLK_SCSI_MAX_CMDS       (256)
332 #define BLK_SCSI_CMD_PER_LONG   (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
333
334 /*
335  * Zoned block device models (zoned limit).
336  */
337 enum blk_zoned_model {
338         BLK_ZONED_NONE, /* Regular block device */
339         BLK_ZONED_HA,   /* Host-aware zoned block device */
340         BLK_ZONED_HM,   /* Host-managed zoned block device */
341 };
342
343 struct queue_limits {
344         unsigned long           bounce_pfn;
345         unsigned long           seg_boundary_mask;
346         unsigned long           virt_boundary_mask;
347
348         unsigned int            max_hw_sectors;
349         unsigned int            max_dev_sectors;
350         unsigned int            chunk_sectors;
351         unsigned int            max_sectors;
352         unsigned int            max_segment_size;
353         unsigned int            physical_block_size;
354         unsigned int            logical_block_size;
355         unsigned int            alignment_offset;
356         unsigned int            io_min;
357         unsigned int            io_opt;
358         unsigned int            max_discard_sectors;
359         unsigned int            max_hw_discard_sectors;
360         unsigned int            max_write_same_sectors;
361         unsigned int            max_write_zeroes_sectors;
362         unsigned int            discard_granularity;
363         unsigned int            discard_alignment;
364
365         unsigned short          max_segments;
366         unsigned short          max_integrity_segments;
367         unsigned short          max_discard_segments;
368
369         unsigned char           misaligned;
370         unsigned char           discard_misaligned;
371         unsigned char           cluster;
372         unsigned char           raid_partial_stripes_expensive;
373         enum blk_zoned_model    zoned;
374 };
375
376 #ifdef CONFIG_BLK_DEV_ZONED
377
378 struct blk_zone_report_hdr {
379         unsigned int    nr_zones;
380         u8              padding[60];
381 };
382
383 extern int blkdev_report_zones(struct block_device *bdev,
384                                sector_t sector, struct blk_zone *zones,
385                                unsigned int *nr_zones, gfp_t gfp_mask);
386 extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors,
387                               sector_t nr_sectors, gfp_t gfp_mask);
388
389 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
390                                      unsigned int cmd, unsigned long arg);
391 extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
392                                     unsigned int cmd, unsigned long arg);
393
394 #else /* CONFIG_BLK_DEV_ZONED */
395
396 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
397                                             fmode_t mode, unsigned int cmd,
398                                             unsigned long arg)
399 {
400         return -ENOTTY;
401 }
402
403 static inline int blkdev_reset_zones_ioctl(struct block_device *bdev,
404                                            fmode_t mode, unsigned int cmd,
405                                            unsigned long arg)
406 {
407         return -ENOTTY;
408 }
409
410 #endif /* CONFIG_BLK_DEV_ZONED */
411
412 struct request_queue {
413         /*
414          * Together with queue_head for cacheline sharing
415          */
416         struct list_head        queue_head;
417         struct request          *last_merge;
418         struct elevator_queue   *elevator;
419         int                     nr_rqs[2];      /* # allocated [a]sync rqs */
420         int                     nr_rqs_elvpriv; /* # allocated rqs w/ elvpriv */
421
422         atomic_t                shared_hctx_restart;
423
424         struct blk_queue_stats  *stats;
425         struct rq_wb            *rq_wb;
426
427         /*
428          * If blkcg is not used, @q->root_rl serves all requests.  If blkcg
429          * is used, root blkg allocates from @q->root_rl and all other
430          * blkgs from their own blkg->rl.  Which one to use should be
431          * determined using bio_request_list().
432          */
433         struct request_list     root_rl;
434
435         request_fn_proc         *request_fn;
436         make_request_fn         *make_request_fn;
437         prep_rq_fn              *prep_rq_fn;
438         unprep_rq_fn            *unprep_rq_fn;
439         softirq_done_fn         *softirq_done_fn;
440         rq_timed_out_fn         *rq_timed_out_fn;
441         dma_drain_needed_fn     *dma_drain_needed;
442         lld_busy_fn             *lld_busy_fn;
443         /* Called just after a request is allocated */
444         init_rq_fn              *init_rq_fn;
445         /* Called just before a request is freed */
446         exit_rq_fn              *exit_rq_fn;
447         /* Called from inside blk_get_request() */
448         void (*initialize_rq_fn)(struct request *rq);
449
450         const struct blk_mq_ops *mq_ops;
451
452         unsigned int            *mq_map;
453
454         /* sw queues */
455         struct blk_mq_ctx __percpu      *queue_ctx;
456         unsigned int            nr_queues;
457
458         unsigned int            queue_depth;
459
460         /* hw dispatch queues */
461         struct blk_mq_hw_ctx    **queue_hw_ctx;
462         unsigned int            nr_hw_queues;
463
464         /*
465          * Dispatch queue sorting
466          */
467         sector_t                end_sector;
468         struct request          *boundary_rq;
469
470         /*
471          * Delayed queue handling
472          */
473         struct delayed_work     delay_work;
474
475         struct backing_dev_info *backing_dev_info;
476
477         /*
478          * The queue owner gets to use this for whatever they like.
479          * ll_rw_blk doesn't touch it.
480          */
481         void                    *queuedata;
482
483         /*
484          * various queue flags, see QUEUE_* below
485          */
486         unsigned long           queue_flags;
487
488         /*
489          * ida allocated id for this queue.  Used to index queues from
490          * ioctx.
491          */
492         int                     id;
493
494         /*
495          * queue needs bounce pages for pages above this limit
496          */
497         gfp_t                   bounce_gfp;
498
499         /*
500          * protects queue structures from reentrancy. ->__queue_lock should
501          * _never_ be used directly, it is queue private. always use
502          * ->queue_lock.
503          */
504         spinlock_t              __queue_lock;
505         spinlock_t              *queue_lock;
506
507         /*
508          * queue kobject
509          */
510         struct kobject kobj;
511
512         /*
513          * mq queue kobject
514          */
515         struct kobject mq_kobj;
516
517 #ifdef  CONFIG_BLK_DEV_INTEGRITY
518         struct blk_integrity integrity;
519 #endif  /* CONFIG_BLK_DEV_INTEGRITY */
520
521 #ifdef CONFIG_PM
522         struct device           *dev;
523         int                     rpm_status;
524         unsigned int            nr_pending;
525 #endif
526
527         /*
528          * queue settings
529          */
530         unsigned long           nr_requests;    /* Max # of requests */
531         unsigned int            nr_congestion_on;
532         unsigned int            nr_congestion_off;
533         unsigned int            nr_batching;
534
535         unsigned int            dma_drain_size;
536         void                    *dma_drain_buffer;
537         unsigned int            dma_pad_mask;
538         unsigned int            dma_alignment;
539
540         struct blk_queue_tag    *queue_tags;
541         struct list_head        tag_busy_list;
542
543         unsigned int            nr_sorted;
544         unsigned int            in_flight[2];
545
546         /*
547          * Number of active block driver functions for which blk_drain_queue()
548          * must wait. Must be incremented around functions that unlock the
549          * queue_lock internally, e.g. scsi_request_fn().
550          */
551         unsigned int            request_fn_active;
552
553         unsigned int            rq_timeout;
554         int                     poll_nsec;
555
556         struct blk_stat_callback        *poll_cb;
557         struct blk_rq_stat      poll_stat[BLK_MQ_POLL_STATS_BKTS];
558
559         struct timer_list       timeout;
560         struct work_struct      timeout_work;
561         struct list_head        timeout_list;
562
563         struct list_head        icq_list;
564 #ifdef CONFIG_BLK_CGROUP
565         DECLARE_BITMAP          (blkcg_pols, BLKCG_MAX_POLS);
566         struct blkcg_gq         *root_blkg;
567         struct list_head        blkg_list;
568 #endif
569
570         struct queue_limits     limits;
571
572         /*
573          * sg stuff
574          */
575         unsigned int            sg_timeout;
576         unsigned int            sg_reserved_size;
577         int                     node;
578 #ifdef CONFIG_BLK_DEV_IO_TRACE
579         struct blk_trace __rcu  *blk_trace;
580         struct mutex            blk_trace_mutex;
581 #endif
582         /*
583          * for flush operations
584          */
585         struct blk_flush_queue  *fq;
586
587         struct list_head        requeue_list;
588         spinlock_t              requeue_lock;
589         struct delayed_work     requeue_work;
590
591         struct mutex            sysfs_lock;
592
593         int                     bypass_depth;
594         atomic_t                mq_freeze_depth;
595
596 #if defined(CONFIG_BLK_DEV_BSG)
597         bsg_job_fn              *bsg_job_fn;
598         struct bsg_class_device bsg_dev;
599 #endif
600
601 #ifdef CONFIG_BLK_DEV_THROTTLING
602         /* Throttle data */
603         struct throtl_data *td;
604 #endif
605         struct rcu_head         rcu_head;
606         wait_queue_head_t       mq_freeze_wq;
607         struct percpu_ref       q_usage_counter;
608         struct list_head        all_q_node;
609
610         struct blk_mq_tag_set   *tag_set;
611         struct list_head        tag_set_list;
612         struct bio_set          *bio_split;
613
614 #ifdef CONFIG_BLK_DEBUG_FS
615         struct dentry           *debugfs_dir;
616         struct dentry           *sched_debugfs_dir;
617 #endif
618
619         bool                    mq_sysfs_init_done;
620
621         size_t                  cmd_size;
622         void                    *rq_alloc_data;
623
624         struct work_struct      release_work;
625
626 #define BLK_MAX_WRITE_HINTS     5
627         u64                     write_hints[BLK_MAX_WRITE_HINTS];
628 };
629
630 #define QUEUE_FLAG_QUEUED       0       /* uses generic tag queueing */
631 #define QUEUE_FLAG_STOPPED      1       /* queue is stopped */
632 #define QUEUE_FLAG_DYING        2       /* queue being torn down */
633 #define QUEUE_FLAG_BYPASS       3       /* act as dumb FIFO queue */
634 #define QUEUE_FLAG_BIDI         4       /* queue supports bidi requests */
635 #define QUEUE_FLAG_NOMERGES     5       /* disable merge attempts */
636 #define QUEUE_FLAG_SAME_COMP    6       /* complete on same CPU-group */
637 #define QUEUE_FLAG_FAIL_IO      7       /* fake timeout */
638 #define QUEUE_FLAG_STACKABLE    8       /* supports request stacking */
639 #define QUEUE_FLAG_NONROT       9       /* non-rotational device (SSD) */
640 #define QUEUE_FLAG_VIRT        QUEUE_FLAG_NONROT /* paravirt device */
641 #define QUEUE_FLAG_IO_STAT     10       /* do IO stats */
642 #define QUEUE_FLAG_DISCARD     11       /* supports DISCARD */
643 #define QUEUE_FLAG_NOXMERGES   12       /* No extended merges */
644 #define QUEUE_FLAG_ADD_RANDOM  13       /* Contributes to random pool */
645 #define QUEUE_FLAG_SECERASE    14       /* supports secure erase */
646 #define QUEUE_FLAG_SAME_FORCE  15       /* force complete on same CPU */
647 #define QUEUE_FLAG_DEAD        16       /* queue tear-down finished */
648 #define QUEUE_FLAG_INIT_DONE   17       /* queue is initialized */
649 #define QUEUE_FLAG_NO_SG_MERGE 18       /* don't attempt to merge SG segments*/
650 #define QUEUE_FLAG_POLL        19       /* IO polling enabled if set */
651 #define QUEUE_FLAG_WC          20       /* Write back caching */
652 #define QUEUE_FLAG_FUA         21       /* device supports FUA writes */
653 #define QUEUE_FLAG_FLUSH_NQ    22       /* flush not queueuable */
654 #define QUEUE_FLAG_DAX         23       /* device supports DAX */
655 #define QUEUE_FLAG_STATS       24       /* track rq completion times */
656 #define QUEUE_FLAG_POLL_STATS  25       /* collecting stats for hybrid polling */
657 #define QUEUE_FLAG_REGISTERED  26       /* queue has been registered to a disk */
658 #define QUEUE_FLAG_SCSI_PASSTHROUGH 27  /* queue supports SCSI commands */
659 #define QUEUE_FLAG_QUIESCED    28       /* queue has been quiesced */
660
661 #define QUEUE_FLAG_DEFAULT      ((1 << QUEUE_FLAG_IO_STAT) |            \
662                                  (1 << QUEUE_FLAG_STACKABLE)    |       \
663                                  (1 << QUEUE_FLAG_SAME_COMP)    |       \
664                                  (1 << QUEUE_FLAG_ADD_RANDOM))
665
666 #define QUEUE_FLAG_MQ_DEFAULT   ((1 << QUEUE_FLAG_IO_STAT) |            \
667                                  (1 << QUEUE_FLAG_STACKABLE)    |       \
668                                  (1 << QUEUE_FLAG_SAME_COMP)    |       \
669                                  (1 << QUEUE_FLAG_POLL))
670
671 /*
672  * @q->queue_lock is set while a queue is being initialized. Since we know
673  * that no other threads access the queue object before @q->queue_lock has
674  * been set, it is safe to manipulate queue flags without holding the
675  * queue_lock if @q->queue_lock == NULL. See also blk_alloc_queue_node() and
676  * blk_init_allocated_queue().
677  */
678 static inline void queue_lockdep_assert_held(struct request_queue *q)
679 {
680         if (q->queue_lock)
681                 lockdep_assert_held(q->queue_lock);
682 }
683
684 static inline void queue_flag_set_unlocked(unsigned int flag,
685                                            struct request_queue *q)
686 {
687         __set_bit(flag, &q->queue_flags);
688 }
689
690 static inline int queue_flag_test_and_clear(unsigned int flag,
691                                             struct request_queue *q)
692 {
693         queue_lockdep_assert_held(q);
694
695         if (test_bit(flag, &q->queue_flags)) {
696                 __clear_bit(flag, &q->queue_flags);
697                 return 1;
698         }
699
700         return 0;
701 }
702
703 static inline int queue_flag_test_and_set(unsigned int flag,
704                                           struct request_queue *q)
705 {
706         queue_lockdep_assert_held(q);
707
708         if (!test_bit(flag, &q->queue_flags)) {
709                 __set_bit(flag, &q->queue_flags);
710                 return 0;
711         }
712
713         return 1;
714 }
715
716 static inline void queue_flag_set(unsigned int flag, struct request_queue *q)
717 {
718         queue_lockdep_assert_held(q);
719         __set_bit(flag, &q->queue_flags);
720 }
721
722 static inline void queue_flag_clear_unlocked(unsigned int flag,
723                                              struct request_queue *q)
724 {
725         __clear_bit(flag, &q->queue_flags);
726 }
727
728 static inline int queue_in_flight(struct request_queue *q)
729 {
730         return q->in_flight[0] + q->in_flight[1];
731 }
732
733 static inline void queue_flag_clear(unsigned int flag, struct request_queue *q)
734 {
735         queue_lockdep_assert_held(q);
736         __clear_bit(flag, &q->queue_flags);
737 }
738
739 #define blk_queue_tagged(q)     test_bit(QUEUE_FLAG_QUEUED, &(q)->queue_flags)
740 #define blk_queue_stopped(q)    test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
741 #define blk_queue_dying(q)      test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
742 #define blk_queue_dead(q)       test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
743 #define blk_queue_bypass(q)     test_bit(QUEUE_FLAG_BYPASS, &(q)->queue_flags)
744 #define blk_queue_init_done(q)  test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
745 #define blk_queue_nomerges(q)   test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
746 #define blk_queue_noxmerges(q)  \
747         test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
748 #define blk_queue_nonrot(q)     test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
749 #define blk_queue_io_stat(q)    test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
750 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
751 #define blk_queue_stackable(q)  \
752         test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
753 #define blk_queue_discard(q)    test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
754 #define blk_queue_secure_erase(q) \
755         (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
756 #define blk_queue_dax(q)        test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
757 #define blk_queue_scsi_passthrough(q)   \
758         test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
759
760 #define blk_noretry_request(rq) \
761         ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
762                              REQ_FAILFAST_DRIVER))
763 #define blk_queue_quiesced(q)   test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
764
765 static inline bool blk_account_rq(struct request *rq)
766 {
767         return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
768 }
769
770 #define blk_rq_cpu_valid(rq)    ((rq)->cpu != -1)
771 #define blk_bidi_rq(rq)         ((rq)->next_rq != NULL)
772 /* rq->queuelist of dequeued request must be list_empty() */
773 #define blk_queued_rq(rq)       (!list_empty(&(rq)->queuelist))
774
775 #define list_entry_rq(ptr)      list_entry((ptr), struct request, queuelist)
776
777 #define rq_data_dir(rq)         (op_is_write(req_op(rq)) ? WRITE : READ)
778
779 /*
780  * Driver can handle struct request, if it either has an old style
781  * request_fn defined, or is blk-mq based.
782  */
783 static inline bool queue_is_rq_based(struct request_queue *q)
784 {
785         return q->request_fn || q->mq_ops;
786 }
787
788 static inline unsigned int blk_queue_cluster(struct request_queue *q)
789 {
790         return q->limits.cluster;
791 }
792
793 static inline enum blk_zoned_model
794 blk_queue_zoned_model(struct request_queue *q)
795 {
796         return q->limits.zoned;
797 }
798
799 static inline bool blk_queue_is_zoned(struct request_queue *q)
800 {
801         switch (blk_queue_zoned_model(q)) {
802         case BLK_ZONED_HA:
803         case BLK_ZONED_HM:
804                 return true;
805         default:
806                 return false;
807         }
808 }
809
810 static inline unsigned int blk_queue_zone_sectors(struct request_queue *q)
811 {
812         return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
813 }
814
815 static inline bool rq_is_sync(struct request *rq)
816 {
817         return op_is_sync(rq->cmd_flags);
818 }
819
820 static inline bool blk_rl_full(struct request_list *rl, bool sync)
821 {
822         unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
823
824         return rl->flags & flag;
825 }
826
827 static inline void blk_set_rl_full(struct request_list *rl, bool sync)
828 {
829         unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
830
831         rl->flags |= flag;
832 }
833
834 static inline void blk_clear_rl_full(struct request_list *rl, bool sync)
835 {
836         unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
837
838         rl->flags &= ~flag;
839 }
840
841 static inline bool rq_mergeable(struct request *rq)
842 {
843         if (blk_rq_is_passthrough(rq))
844                 return false;
845
846         if (req_op(rq) == REQ_OP_FLUSH)
847                 return false;
848
849         if (req_op(rq) == REQ_OP_WRITE_ZEROES)
850                 return false;
851
852         if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
853                 return false;
854         if (rq->rq_flags & RQF_NOMERGE_FLAGS)
855                 return false;
856
857         return true;
858 }
859
860 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
861 {
862         if (bio_page(a) == bio_page(b) &&
863             bio_offset(a) == bio_offset(b))
864                 return true;
865
866         return false;
867 }
868
869 static inline unsigned int blk_queue_depth(struct request_queue *q)
870 {
871         if (q->queue_depth)
872                 return q->queue_depth;
873
874         return q->nr_requests;
875 }
876
877 /*
878  * q->prep_rq_fn return values
879  */
880 enum {
881         BLKPREP_OK,             /* serve it */
882         BLKPREP_KILL,           /* fatal error, kill, return -EIO */
883         BLKPREP_DEFER,          /* leave on queue */
884         BLKPREP_INVALID,        /* invalid command, kill, return -EREMOTEIO */
885 };
886
887 extern unsigned long blk_max_low_pfn, blk_max_pfn;
888
889 /*
890  * standard bounce addresses:
891  *
892  * BLK_BOUNCE_HIGH      : bounce all highmem pages
893  * BLK_BOUNCE_ANY       : don't bounce anything
894  * BLK_BOUNCE_ISA       : bounce pages above ISA DMA boundary
895  */
896
897 #if BITS_PER_LONG == 32
898 #define BLK_BOUNCE_HIGH         ((u64)blk_max_low_pfn << PAGE_SHIFT)
899 #else
900 #define BLK_BOUNCE_HIGH         -1ULL
901 #endif
902 #define BLK_BOUNCE_ANY          (-1ULL)
903 #define BLK_BOUNCE_ISA          (DMA_BIT_MASK(24))
904
905 /*
906  * default timeout for SG_IO if none specified
907  */
908 #define BLK_DEFAULT_SG_TIMEOUT  (60 * HZ)
909 #define BLK_MIN_SG_TIMEOUT      (7 * HZ)
910
911 struct rq_map_data {
912         struct page **pages;
913         int page_order;
914         int nr_entries;
915         unsigned long offset;
916         int null_mapped;
917         int from_user;
918 };
919
920 struct req_iterator {
921         struct bvec_iter iter;
922         struct bio *bio;
923 };
924
925 /* This should not be used directly - use rq_for_each_segment */
926 #define for_each_bio(_bio)              \
927         for (; _bio; _bio = _bio->bi_next)
928 #define __rq_for_each_bio(_bio, rq)     \
929         if ((rq->bio))                  \
930                 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
931
932 #define rq_for_each_segment(bvl, _rq, _iter)                    \
933         __rq_for_each_bio(_iter.bio, _rq)                       \
934                 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
935
936 #define rq_iter_last(bvec, _iter)                               \
937                 (_iter.bio->bi_next == NULL &&                  \
938                  bio_iter_last(bvec, _iter.iter))
939
940 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
941 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
942 #endif
943 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
944 extern void rq_flush_dcache_pages(struct request *rq);
945 #else
946 static inline void rq_flush_dcache_pages(struct request *rq)
947 {
948 }
949 #endif
950
951 #ifdef CONFIG_PRINTK
952 #define vfs_msg(sb, level, fmt, ...)                            \
953         __vfs_msg(sb, level, fmt, ##__VA_ARGS__)
954 #else
955 #define vfs_msg(sb, level, fmt, ...)                            \
956 do {                                                            \
957         no_printk(fmt, ##__VA_ARGS__);                          \
958         __vfs_msg(sb, "", " ");                                 \
959 } while (0)
960 #endif
961
962 extern int blk_register_queue(struct gendisk *disk);
963 extern void blk_unregister_queue(struct gendisk *disk);
964 extern blk_qc_t generic_make_request(struct bio *bio);
965 extern void blk_rq_init(struct request_queue *q, struct request *rq);
966 extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
967 extern void blk_put_request(struct request *);
968 extern void __blk_put_request(struct request_queue *, struct request *);
969 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
970                                        gfp_t gfp_mask);
971 extern void blk_requeue_request(struct request_queue *, struct request *);
972 extern int blk_lld_busy(struct request_queue *q);
973 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
974                              struct bio_set *bs, gfp_t gfp_mask,
975                              int (*bio_ctr)(struct bio *, struct bio *, void *),
976                              void *data);
977 extern void blk_rq_unprep_clone(struct request *rq);
978 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
979                                      struct request *rq);
980 extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
981 extern void blk_delay_queue(struct request_queue *, unsigned long);
982 extern void blk_queue_split(struct request_queue *, struct bio **);
983 extern void blk_recount_segments(struct request_queue *, struct bio *);
984 extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
985 extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
986                               unsigned int, void __user *);
987 extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
988                           unsigned int, void __user *);
989 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
990                          struct scsi_ioctl_command __user *);
991
992 extern int blk_queue_enter(struct request_queue *q, bool nowait);
993 extern void blk_queue_exit(struct request_queue *q);
994 extern void blk_start_queue(struct request_queue *q);
995 extern void blk_start_queue_async(struct request_queue *q);
996 extern void blk_stop_queue(struct request_queue *q);
997 extern void blk_sync_queue(struct request_queue *q);
998 extern void __blk_stop_queue(struct request_queue *q);
999 extern void __blk_run_queue(struct request_queue *q);
1000 extern void __blk_run_queue_uncond(struct request_queue *q);
1001 extern void blk_run_queue(struct request_queue *);
1002 extern void blk_run_queue_async(struct request_queue *q);
1003 extern int blk_rq_map_user(struct request_queue *, struct request *,
1004                            struct rq_map_data *, void __user *, unsigned long,
1005                            gfp_t);
1006 extern int blk_rq_unmap_user(struct bio *);
1007 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
1008 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
1009                                struct rq_map_data *, const struct iov_iter *,
1010                                gfp_t);
1011 extern void blk_execute_rq(struct request_queue *, struct gendisk *,
1012                           struct request *, int);
1013 extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
1014                                   struct request *, int, rq_end_io_fn *);
1015
1016 int blk_status_to_errno(blk_status_t status);
1017 blk_status_t errno_to_blk_status(int errno);
1018
1019 bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie);
1020
1021 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
1022 {
1023         return bdev->bd_disk->queue;    /* this is never NULL */
1024 }
1025
1026 /*
1027  * The basic unit of block I/O is a sector. It is used in a number of contexts
1028  * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
1029  * bytes. Variables of type sector_t represent an offset or size that is a
1030  * multiple of 512 bytes. Hence these two constants.
1031  */
1032 #ifndef SECTOR_SHIFT
1033 #define SECTOR_SHIFT 9
1034 #endif
1035 #ifndef SECTOR_SIZE
1036 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
1037 #endif
1038
1039 /*
1040  * blk_rq_pos()                 : the current sector
1041  * blk_rq_bytes()               : bytes left in the entire request
1042  * blk_rq_cur_bytes()           : bytes left in the current segment
1043  * blk_rq_err_bytes()           : bytes left till the next error boundary
1044  * blk_rq_sectors()             : sectors left in the entire request
1045  * blk_rq_cur_sectors()         : sectors left in the current segment
1046  */
1047 static inline sector_t blk_rq_pos(const struct request *rq)
1048 {
1049         return rq->__sector;
1050 }
1051
1052 static inline unsigned int blk_rq_bytes(const struct request *rq)
1053 {
1054         return rq->__data_len;
1055 }
1056
1057 static inline int blk_rq_cur_bytes(const struct request *rq)
1058 {
1059         return rq->bio ? bio_cur_bytes(rq->bio) : 0;
1060 }
1061
1062 extern unsigned int blk_rq_err_bytes(const struct request *rq);
1063
1064 static inline unsigned int blk_rq_sectors(const struct request *rq)
1065 {
1066         return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1067 }
1068
1069 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1070 {
1071         return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1072 }
1073
1074 /*
1075  * Some commands like WRITE SAME have a payload or data transfer size which
1076  * is different from the size of the request.  Any driver that supports such
1077  * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1078  * calculate the data transfer size.
1079  */
1080 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1081 {
1082         if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1083                 return rq->special_vec.bv_len;
1084         return blk_rq_bytes(rq);
1085 }
1086
1087 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1088                                                      int op)
1089 {
1090         if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1091                 return min(q->limits.max_discard_sectors,
1092                            UINT_MAX >> SECTOR_SHIFT);
1093
1094         if (unlikely(op == REQ_OP_WRITE_SAME))
1095                 return q->limits.max_write_same_sectors;
1096
1097         if (unlikely(op == REQ_OP_WRITE_ZEROES))
1098                 return q->limits.max_write_zeroes_sectors;
1099
1100         return q->limits.max_sectors;
1101 }
1102
1103 /*
1104  * Return maximum size of a request at given offset. Only valid for
1105  * file system requests.
1106  */
1107 static inline unsigned int blk_max_size_offset(struct request_queue *q,
1108                                                sector_t offset)
1109 {
1110         if (!q->limits.chunk_sectors)
1111                 return q->limits.max_sectors;
1112
1113         return min(q->limits.max_sectors, (unsigned int)(q->limits.chunk_sectors -
1114                         (offset & (q->limits.chunk_sectors - 1))));
1115 }
1116
1117 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1118                                                   sector_t offset)
1119 {
1120         struct request_queue *q = rq->q;
1121
1122         if (blk_rq_is_passthrough(rq))
1123                 return q->limits.max_hw_sectors;
1124
1125         if (!q->limits.chunk_sectors ||
1126             req_op(rq) == REQ_OP_DISCARD ||
1127             req_op(rq) == REQ_OP_SECURE_ERASE)
1128                 return blk_queue_get_max_sectors(q, req_op(rq));
1129
1130         return min(blk_max_size_offset(q, offset),
1131                         blk_queue_get_max_sectors(q, req_op(rq)));
1132 }
1133
1134 static inline unsigned int blk_rq_count_bios(struct request *rq)
1135 {
1136         unsigned int nr_bios = 0;
1137         struct bio *bio;
1138
1139         __rq_for_each_bio(bio, rq)
1140                 nr_bios++;
1141
1142         return nr_bios;
1143 }
1144
1145 /*
1146  * Request issue related functions.
1147  */
1148 extern struct request *blk_peek_request(struct request_queue *q);
1149 extern void blk_start_request(struct request *rq);
1150 extern struct request *blk_fetch_request(struct request_queue *q);
1151
1152 /*
1153  * Request completion related functions.
1154  *
1155  * blk_update_request() completes given number of bytes and updates
1156  * the request without completing it.
1157  *
1158  * blk_end_request() and friends.  __blk_end_request() must be called
1159  * with the request queue spinlock acquired.
1160  *
1161  * Several drivers define their own end_request and call
1162  * blk_end_request() for parts of the original function.
1163  * This prevents code duplication in drivers.
1164  */
1165 extern bool blk_update_request(struct request *rq, blk_status_t error,
1166                                unsigned int nr_bytes);
1167 extern void blk_finish_request(struct request *rq, blk_status_t error);
1168 extern bool blk_end_request(struct request *rq, blk_status_t error,
1169                             unsigned int nr_bytes);
1170 extern void blk_end_request_all(struct request *rq, blk_status_t error);
1171 extern bool __blk_end_request(struct request *rq, blk_status_t error,
1172                               unsigned int nr_bytes);
1173 extern void __blk_end_request_all(struct request *rq, blk_status_t error);
1174 extern bool __blk_end_request_cur(struct request *rq, blk_status_t error);
1175
1176 extern void blk_complete_request(struct request *);
1177 extern void __blk_complete_request(struct request *);
1178 extern void blk_abort_request(struct request *);
1179 extern void blk_unprep_request(struct request *);
1180
1181 /*
1182  * Access functions for manipulating queue properties
1183  */
1184 extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn,
1185                                         spinlock_t *lock, int node_id);
1186 extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *);
1187 extern int blk_init_allocated_queue(struct request_queue *);
1188 extern void blk_cleanup_queue(struct request_queue *);
1189 extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
1190 extern void blk_queue_bounce_limit(struct request_queue *, u64);
1191 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1192 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1193 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1194 extern void blk_queue_max_discard_segments(struct request_queue *,
1195                 unsigned short);
1196 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1197 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1198                 unsigned int max_discard_sectors);
1199 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1200                 unsigned int max_write_same_sectors);
1201 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1202                 unsigned int max_write_same_sectors);
1203 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
1204 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1205 extern void blk_queue_alignment_offset(struct request_queue *q,
1206                                        unsigned int alignment);
1207 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1208 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1209 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1210 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1211 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1212 extern void blk_set_default_limits(struct queue_limits *lim);
1213 extern void blk_set_stacking_limits(struct queue_limits *lim);
1214 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1215                             sector_t offset);
1216 extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
1217                             sector_t offset);
1218 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1219                               sector_t offset);
1220 extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
1221 extern void blk_queue_dma_pad(struct request_queue *, unsigned int);
1222 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1223 extern int blk_queue_dma_drain(struct request_queue *q,
1224                                dma_drain_needed_fn *dma_drain_needed,
1225                                void *buf, unsigned int size);
1226 extern void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn);
1227 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1228 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1229 extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn);
1230 extern void blk_queue_unprep_rq(struct request_queue *, unprep_rq_fn *ufn);
1231 extern void blk_queue_dma_alignment(struct request_queue *, int);
1232 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1233 extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *);
1234 extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *);
1235 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1236 extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable);
1237 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1238
1239 /*
1240  * Number of physical segments as sent to the device.
1241  *
1242  * Normally this is the number of discontiguous data segments sent by the
1243  * submitter.  But for data-less command like discard we might have no
1244  * actual data segments submitted, but the driver might have to add it's
1245  * own special payload.  In that case we still return 1 here so that this
1246  * special payload will be mapped.
1247  */
1248 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1249 {
1250         if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1251                 return 1;
1252         return rq->nr_phys_segments;
1253 }
1254
1255 /*
1256  * Number of discard segments (or ranges) the driver needs to fill in.
1257  * Each discard bio merged into a request is counted as one segment.
1258  */
1259 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1260 {
1261         return max_t(unsigned short, rq->nr_phys_segments, 1);
1262 }
1263
1264 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
1265 extern void blk_dump_rq_flags(struct request *, char *);
1266 extern long nr_blockdev_pages(void);
1267
1268 bool __must_check blk_get_queue(struct request_queue *);
1269 struct request_queue *blk_alloc_queue(gfp_t);
1270 struct request_queue *blk_alloc_queue_node(gfp_t, int);
1271 extern void blk_put_queue(struct request_queue *);
1272 extern void blk_set_queue_dying(struct request_queue *);
1273
1274 /*
1275  * block layer runtime pm functions
1276  */
1277 #ifdef CONFIG_PM
1278 extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev);
1279 extern int blk_pre_runtime_suspend(struct request_queue *q);
1280 extern void blk_post_runtime_suspend(struct request_queue *q, int err);
1281 extern void blk_pre_runtime_resume(struct request_queue *q);
1282 extern void blk_post_runtime_resume(struct request_queue *q, int err);
1283 extern void blk_set_runtime_active(struct request_queue *q);
1284 #else
1285 static inline void blk_pm_runtime_init(struct request_queue *q,
1286         struct device *dev) {}
1287 static inline int blk_pre_runtime_suspend(struct request_queue *q)
1288 {
1289         return -ENOSYS;
1290 }
1291 static inline void blk_post_runtime_suspend(struct request_queue *q, int err) {}
1292 static inline void blk_pre_runtime_resume(struct request_queue *q) {}
1293 static inline void blk_post_runtime_resume(struct request_queue *q, int err) {}
1294 static inline void blk_set_runtime_active(struct request_queue *q) {}
1295 #endif
1296
1297 /*
1298  * blk_plug permits building a queue of related requests by holding the I/O
1299  * fragments for a short period. This allows merging of sequential requests
1300  * into single larger request. As the requests are moved from a per-task list to
1301  * the device's request_queue in a batch, this results in improved scalability
1302  * as the lock contention for request_queue lock is reduced.
1303  *
1304  * It is ok not to disable preemption when adding the request to the plug list
1305  * or when attempting a merge, because blk_schedule_flush_list() will only flush
1306  * the plug list when the task sleeps by itself. For details, please see
1307  * schedule() where blk_schedule_flush_plug() is called.
1308  */
1309 struct blk_plug {
1310         struct list_head list; /* requests */
1311         struct list_head mq_list; /* blk-mq requests */
1312         struct list_head cb_list; /* md requires an unplug callback */
1313 };
1314 #define BLK_MAX_REQUEST_COUNT 16
1315 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1316
1317 struct blk_plug_cb;
1318 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1319 struct blk_plug_cb {
1320         struct list_head list;
1321         blk_plug_cb_fn callback;
1322         void *data;
1323 };
1324 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1325                                              void *data, int size);
1326 extern void blk_start_plug(struct blk_plug *);
1327 extern void blk_finish_plug(struct blk_plug *);
1328 extern void blk_flush_plug_list(struct blk_plug *, bool);
1329
1330 static inline void blk_flush_plug(struct task_struct *tsk)
1331 {
1332         struct blk_plug *plug = tsk->plug;
1333
1334         if (plug)
1335                 blk_flush_plug_list(plug, false);
1336 }
1337
1338 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1339 {
1340         struct blk_plug *plug = tsk->plug;
1341
1342         if (plug)
1343                 blk_flush_plug_list(plug, true);
1344 }
1345
1346 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1347 {
1348         struct blk_plug *plug = tsk->plug;
1349
1350         return plug &&
1351                 (!list_empty(&plug->list) ||
1352                  !list_empty(&plug->mq_list) ||
1353                  !list_empty(&plug->cb_list));
1354 }
1355
1356 /*
1357  * tag stuff
1358  */
1359 extern int blk_queue_start_tag(struct request_queue *, struct request *);
1360 extern struct request *blk_queue_find_tag(struct request_queue *, int);
1361 extern void blk_queue_end_tag(struct request_queue *, struct request *);
1362 extern int blk_queue_init_tags(struct request_queue *, int, struct blk_queue_tag *, int);
1363 extern void blk_queue_free_tags(struct request_queue *);
1364 extern int blk_queue_resize_tags(struct request_queue *, int);
1365 extern void blk_queue_invalidate_tags(struct request_queue *);
1366 extern struct blk_queue_tag *blk_init_tags(int, int);
1367 extern void blk_free_tags(struct blk_queue_tag *);
1368
1369 static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
1370                                                 int tag)
1371 {
1372         if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
1373                 return NULL;
1374         return bqt->tag_index[tag];
1375 }
1376
1377 extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
1378 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1379                 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1380
1381 #define BLKDEV_DISCARD_SECURE   (1 << 0)        /* issue a secure erase */
1382
1383 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1384                 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1385 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1386                 sector_t nr_sects, gfp_t gfp_mask, int flags,
1387                 struct bio **biop);
1388
1389 #define BLKDEV_ZERO_NOUNMAP     (1 << 0)  /* do not free blocks */
1390 #define BLKDEV_ZERO_NOFALLBACK  (1 << 1)  /* don't write explicit zeroes */
1391
1392 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1393                 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1394                 unsigned flags);
1395 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1396                 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1397
1398 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1399                 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1400 {
1401         return blkdev_issue_discard(sb->s_bdev,
1402                                     block << (sb->s_blocksize_bits -
1403                                               SECTOR_SHIFT),
1404                                     nr_blocks << (sb->s_blocksize_bits -
1405                                                   SECTOR_SHIFT),
1406                                     gfp_mask, flags);
1407 }
1408 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1409                 sector_t nr_blocks, gfp_t gfp_mask)
1410 {
1411         return blkdev_issue_zeroout(sb->s_bdev,
1412                                     block << (sb->s_blocksize_bits -
1413                                               SECTOR_SHIFT),
1414                                     nr_blocks << (sb->s_blocksize_bits -
1415                                                   SECTOR_SHIFT),
1416                                     gfp_mask, 0);
1417 }
1418
1419 extern int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm);
1420
1421 enum blk_default_limits {
1422         BLK_MAX_SEGMENTS        = 128,
1423         BLK_SAFE_MAX_SECTORS    = 255,
1424         BLK_DEF_MAX_SECTORS     = 2560,
1425         BLK_MAX_SEGMENT_SIZE    = 65536,
1426         BLK_SEG_BOUNDARY_MASK   = 0xFFFFFFFFUL,
1427 };
1428
1429 #define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist)
1430
1431 static inline unsigned long queue_segment_boundary(struct request_queue *q)
1432 {
1433         return q->limits.seg_boundary_mask;
1434 }
1435
1436 static inline unsigned long queue_virt_boundary(struct request_queue *q)
1437 {
1438         return q->limits.virt_boundary_mask;
1439 }
1440
1441 static inline unsigned int queue_max_sectors(struct request_queue *q)
1442 {
1443         return q->limits.max_sectors;
1444 }
1445
1446 static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
1447 {
1448         return q->limits.max_hw_sectors;
1449 }
1450
1451 static inline unsigned short queue_max_segments(struct request_queue *q)
1452 {
1453         return q->limits.max_segments;
1454 }
1455
1456 static inline unsigned short queue_max_discard_segments(struct request_queue *q)
1457 {
1458         return q->limits.max_discard_segments;
1459 }
1460
1461 static inline unsigned int queue_max_segment_size(struct request_queue *q)
1462 {
1463         return q->limits.max_segment_size;
1464 }
1465
1466 static inline unsigned queue_logical_block_size(struct request_queue *q)
1467 {
1468         int retval = 512;
1469
1470         if (q && q->limits.logical_block_size)
1471                 retval = q->limits.logical_block_size;
1472
1473         return retval;
1474 }
1475
1476 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1477 {
1478         return queue_logical_block_size(bdev_get_queue(bdev));
1479 }
1480
1481 static inline unsigned int queue_physical_block_size(struct request_queue *q)
1482 {
1483         return q->limits.physical_block_size;
1484 }
1485
1486 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1487 {
1488         return queue_physical_block_size(bdev_get_queue(bdev));
1489 }
1490
1491 static inline unsigned int queue_io_min(struct request_queue *q)
1492 {
1493         return q->limits.io_min;
1494 }
1495
1496 static inline int bdev_io_min(struct block_device *bdev)
1497 {
1498         return queue_io_min(bdev_get_queue(bdev));
1499 }
1500
1501 static inline unsigned int queue_io_opt(struct request_queue *q)
1502 {
1503         return q->limits.io_opt;
1504 }
1505
1506 static inline int bdev_io_opt(struct block_device *bdev)
1507 {
1508         return queue_io_opt(bdev_get_queue(bdev));
1509 }
1510
1511 static inline int queue_alignment_offset(struct request_queue *q)
1512 {
1513         if (q->limits.misaligned)
1514                 return -1;
1515
1516         return q->limits.alignment_offset;
1517 }
1518
1519 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1520 {
1521         unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1522         unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1523                 << SECTOR_SHIFT;
1524
1525         return (granularity + lim->alignment_offset - alignment) % granularity;
1526 }
1527
1528 static inline int bdev_alignment_offset(struct block_device *bdev)
1529 {
1530         struct request_queue *q = bdev_get_queue(bdev);
1531
1532         if (q->limits.misaligned)
1533                 return -1;
1534
1535         if (bdev != bdev->bd_contains)
1536                 return bdev->bd_part->alignment_offset;
1537
1538         return q->limits.alignment_offset;
1539 }
1540
1541 static inline int queue_discard_alignment(struct request_queue *q)
1542 {
1543         if (q->limits.discard_misaligned)
1544                 return -1;
1545
1546         return q->limits.discard_alignment;
1547 }
1548
1549 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1550 {
1551         unsigned int alignment, granularity, offset;
1552
1553         if (!lim->max_discard_sectors)
1554                 return 0;
1555
1556         /* Why are these in bytes, not sectors? */
1557         alignment = lim->discard_alignment >> SECTOR_SHIFT;
1558         granularity = lim->discard_granularity >> SECTOR_SHIFT;
1559         if (!granularity)
1560                 return 0;
1561
1562         /* Offset of the partition start in 'granularity' sectors */
1563         offset = sector_div(sector, granularity);
1564
1565         /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1566         offset = (granularity + alignment - offset) % granularity;
1567
1568         /* Turn it back into bytes, gaah */
1569         return offset << SECTOR_SHIFT;
1570 }
1571
1572 static inline int bdev_discard_alignment(struct block_device *bdev)
1573 {
1574         struct request_queue *q = bdev_get_queue(bdev);
1575
1576         if (bdev != bdev->bd_contains)
1577                 return bdev->bd_part->discard_alignment;
1578
1579         return q->limits.discard_alignment;
1580 }
1581
1582 static inline unsigned int bdev_write_same(struct block_device *bdev)
1583 {
1584         struct request_queue *q = bdev_get_queue(bdev);
1585
1586         if (q)
1587                 return q->limits.max_write_same_sectors;
1588
1589         return 0;
1590 }
1591
1592 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1593 {
1594         struct request_queue *q = bdev_get_queue(bdev);
1595
1596         if (q)
1597                 return q->limits.max_write_zeroes_sectors;
1598
1599         return 0;
1600 }
1601
1602 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1603 {
1604         struct request_queue *q = bdev_get_queue(bdev);
1605
1606         if (q)
1607                 return blk_queue_zoned_model(q);
1608
1609         return BLK_ZONED_NONE;
1610 }
1611
1612 static inline bool bdev_is_zoned(struct block_device *bdev)
1613 {
1614         struct request_queue *q = bdev_get_queue(bdev);
1615
1616         if (q)
1617                 return blk_queue_is_zoned(q);
1618
1619         return false;
1620 }
1621
1622 static inline unsigned int bdev_zone_sectors(struct block_device *bdev)
1623 {
1624         struct request_queue *q = bdev_get_queue(bdev);
1625
1626         if (q)
1627                 return blk_queue_zone_sectors(q);
1628
1629         return 0;
1630 }
1631
1632 static inline int queue_dma_alignment(struct request_queue *q)
1633 {
1634         return q ? q->dma_alignment : 511;
1635 }
1636
1637 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1638                                  unsigned int len)
1639 {
1640         unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1641         return !(addr & alignment) && !(len & alignment);
1642 }
1643
1644 /* assumes size > 256 */
1645 static inline unsigned int blksize_bits(unsigned int size)
1646 {
1647         unsigned int bits = 8;
1648         do {
1649                 bits++;
1650                 size >>= 1;
1651         } while (size > 256);
1652         return bits;
1653 }
1654
1655 static inline unsigned int block_size(struct block_device *bdev)
1656 {
1657         return bdev->bd_block_size;
1658 }
1659
1660 static inline bool queue_flush_queueable(struct request_queue *q)
1661 {
1662         return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
1663 }
1664
1665 typedef struct {struct page *v;} Sector;
1666
1667 unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
1668
1669 static inline void put_dev_sector(Sector p)
1670 {
1671         put_page(p.v);
1672 }
1673
1674 static inline bool __bvec_gap_to_prev(struct request_queue *q,
1675                                 struct bio_vec *bprv, unsigned int offset)
1676 {
1677         return offset ||
1678                 ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q));
1679 }
1680
1681 /*
1682  * Check if adding a bio_vec after bprv with offset would create a gap in
1683  * the SG list. Most drivers don't care about this, but some do.
1684  */
1685 static inline bool bvec_gap_to_prev(struct request_queue *q,
1686                                 struct bio_vec *bprv, unsigned int offset)
1687 {
1688         if (!queue_virt_boundary(q))
1689                 return false;
1690         return __bvec_gap_to_prev(q, bprv, offset);
1691 }
1692
1693 /*
1694  * Check if the two bvecs from two bios can be merged to one segment.
1695  * If yes, no need to check gap between the two bios since the 1st bio
1696  * and the 1st bvec in the 2nd bio can be handled in one segment.
1697  */
1698 static inline bool bios_segs_mergeable(struct request_queue *q,
1699                 struct bio *prev, struct bio_vec *prev_last_bv,
1700                 struct bio_vec *next_first_bv)
1701 {
1702         if (!BIOVEC_PHYS_MERGEABLE(prev_last_bv, next_first_bv))
1703                 return false;
1704         if (!BIOVEC_SEG_BOUNDARY(q, prev_last_bv, next_first_bv))
1705                 return false;
1706         if (prev->bi_seg_back_size + next_first_bv->bv_len >
1707                         queue_max_segment_size(q))
1708                 return false;
1709         return true;
1710 }
1711
1712 static inline bool bio_will_gap(struct request_queue *q,
1713                                 struct request *prev_rq,
1714                                 struct bio *prev,
1715                                 struct bio *next)
1716 {
1717         if (bio_has_data(prev) && queue_virt_boundary(q)) {
1718                 struct bio_vec pb, nb;
1719
1720                 /*
1721                  * don't merge if the 1st bio starts with non-zero
1722                  * offset, otherwise it is quite difficult to respect
1723                  * sg gap limit. We work hard to merge a huge number of small
1724                  * single bios in case of mkfs.
1725                  */
1726                 if (prev_rq)
1727                         bio_get_first_bvec(prev_rq->bio, &pb);
1728                 else
1729                         bio_get_first_bvec(prev, &pb);
1730                 if (pb.bv_offset)
1731                         return true;
1732
1733                 /*
1734                  * We don't need to worry about the situation that the
1735                  * merged segment ends in unaligned virt boundary:
1736                  *
1737                  * - if 'pb' ends aligned, the merged segment ends aligned
1738                  * - if 'pb' ends unaligned, the next bio must include
1739                  *   one single bvec of 'nb', otherwise the 'nb' can't
1740                  *   merge with 'pb'
1741                  */
1742                 bio_get_last_bvec(prev, &pb);
1743                 bio_get_first_bvec(next, &nb);
1744
1745                 if (!bios_segs_mergeable(q, prev, &pb, &nb))
1746                         return __bvec_gap_to_prev(q, &pb, nb.bv_offset);
1747         }
1748
1749         return false;
1750 }
1751
1752 static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
1753 {
1754         return bio_will_gap(req->q, req, req->biotail, bio);
1755 }
1756
1757 static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
1758 {
1759         return bio_will_gap(req->q, NULL, bio, req->bio);
1760 }
1761
1762 int kblockd_schedule_work(struct work_struct *work);
1763 int kblockd_schedule_work_on(int cpu, struct work_struct *work);
1764 int kblockd_schedule_delayed_work(struct delayed_work *dwork, unsigned long delay);
1765 int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1766 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1767
1768 #ifdef CONFIG_BLK_CGROUP
1769 /*
1770  * This should not be using sched_clock(). A real patch is in progress
1771  * to fix this up, until that is in place we need to disable preemption
1772  * around sched_clock() in this function and set_io_start_time_ns().
1773  */
1774 static inline void set_start_time_ns(struct request *req)
1775 {
1776         preempt_disable();
1777         req->start_time_ns = sched_clock();
1778         preempt_enable();
1779 }
1780
1781 static inline void set_io_start_time_ns(struct request *req)
1782 {
1783         preempt_disable();
1784         req->io_start_time_ns = sched_clock();
1785         preempt_enable();
1786 }
1787
1788 static inline uint64_t rq_start_time_ns(struct request *req)
1789 {
1790         return req->start_time_ns;
1791 }
1792
1793 static inline uint64_t rq_io_start_time_ns(struct request *req)
1794 {
1795         return req->io_start_time_ns;
1796 }
1797 #else
1798 static inline void set_start_time_ns(struct request *req) {}
1799 static inline void set_io_start_time_ns(struct request *req) {}
1800 static inline uint64_t rq_start_time_ns(struct request *req)
1801 {
1802         return 0;
1803 }
1804 static inline uint64_t rq_io_start_time_ns(struct request *req)
1805 {
1806         return 0;
1807 }
1808 #endif
1809
1810 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1811         MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1812 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1813         MODULE_ALIAS("block-major-" __stringify(major) "-*")
1814
1815 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1816
1817 enum blk_integrity_flags {
1818         BLK_INTEGRITY_VERIFY            = 1 << 0,
1819         BLK_INTEGRITY_GENERATE          = 1 << 1,
1820         BLK_INTEGRITY_DEVICE_CAPABLE    = 1 << 2,
1821         BLK_INTEGRITY_IP_CHECKSUM       = 1 << 3,
1822 };
1823
1824 struct blk_integrity_iter {
1825         void                    *prot_buf;
1826         void                    *data_buf;
1827         sector_t                seed;
1828         unsigned int            data_size;
1829         unsigned short          interval;
1830         const char              *disk_name;
1831 };
1832
1833 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1834
1835 struct blk_integrity_profile {
1836         integrity_processing_fn         *generate_fn;
1837         integrity_processing_fn         *verify_fn;
1838         const char                      *name;
1839 };
1840
1841 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1842 extern void blk_integrity_unregister(struct gendisk *);
1843 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1844 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1845                                    struct scatterlist *);
1846 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1847 extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
1848                                    struct request *);
1849 extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
1850                                     struct bio *);
1851
1852 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1853 {
1854         struct blk_integrity *bi = &disk->queue->integrity;
1855
1856         if (!bi->profile)
1857                 return NULL;
1858
1859         return bi;
1860 }
1861
1862 static inline
1863 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1864 {
1865         return blk_get_integrity(bdev->bd_disk);
1866 }
1867
1868 static inline bool blk_integrity_rq(struct request *rq)
1869 {
1870         return rq->cmd_flags & REQ_INTEGRITY;
1871 }
1872
1873 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1874                                                     unsigned int segs)
1875 {
1876         q->limits.max_integrity_segments = segs;
1877 }
1878
1879 static inline unsigned short
1880 queue_max_integrity_segments(struct request_queue *q)
1881 {
1882         return q->limits.max_integrity_segments;
1883 }
1884
1885 static inline bool integrity_req_gap_back_merge(struct request *req,
1886                                                 struct bio *next)
1887 {
1888         struct bio_integrity_payload *bip = bio_integrity(req->bio);
1889         struct bio_integrity_payload *bip_next = bio_integrity(next);
1890
1891         return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
1892                                 bip_next->bip_vec[0].bv_offset);
1893 }
1894
1895 static inline bool integrity_req_gap_front_merge(struct request *req,
1896                                                  struct bio *bio)
1897 {
1898         struct bio_integrity_payload *bip = bio_integrity(bio);
1899         struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
1900
1901         return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
1902                                 bip_next->bip_vec[0].bv_offset);
1903 }
1904
1905 #else /* CONFIG_BLK_DEV_INTEGRITY */
1906
1907 struct bio;
1908 struct block_device;
1909 struct gendisk;
1910 struct blk_integrity;
1911
1912 static inline int blk_integrity_rq(struct request *rq)
1913 {
1914         return 0;
1915 }
1916 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1917                                             struct bio *b)
1918 {
1919         return 0;
1920 }
1921 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1922                                           struct bio *b,
1923                                           struct scatterlist *s)
1924 {
1925         return 0;
1926 }
1927 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1928 {
1929         return NULL;
1930 }
1931 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1932 {
1933         return NULL;
1934 }
1935 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1936 {
1937         return 0;
1938 }
1939 static inline void blk_integrity_register(struct gendisk *d,
1940                                          struct blk_integrity *b)
1941 {
1942 }
1943 static inline void blk_integrity_unregister(struct gendisk *d)
1944 {
1945 }
1946 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1947                                                     unsigned int segs)
1948 {
1949 }
1950 static inline unsigned short queue_max_integrity_segments(struct request_queue *q)
1951 {
1952         return 0;
1953 }
1954 static inline bool blk_integrity_merge_rq(struct request_queue *rq,
1955                                           struct request *r1,
1956                                           struct request *r2)
1957 {
1958         return true;
1959 }
1960 static inline bool blk_integrity_merge_bio(struct request_queue *rq,
1961                                            struct request *r,
1962                                            struct bio *b)
1963 {
1964         return true;
1965 }
1966
1967 static inline bool integrity_req_gap_back_merge(struct request *req,
1968                                                 struct bio *next)
1969 {
1970         return false;
1971 }
1972 static inline bool integrity_req_gap_front_merge(struct request *req,
1973                                                  struct bio *bio)
1974 {
1975         return false;
1976 }
1977
1978 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1979
1980 struct block_device_operations {
1981         int (*open) (struct block_device *, fmode_t);
1982         void (*release) (struct gendisk *, fmode_t);
1983         int (*rw_page)(struct block_device *, sector_t, struct page *, bool);
1984         int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1985         int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1986         unsigned int (*check_events) (struct gendisk *disk,
1987                                       unsigned int clearing);
1988         /* ->media_changed() is DEPRECATED, use ->check_events() instead */
1989         int (*media_changed) (struct gendisk *);
1990         void (*unlock_native_capacity) (struct gendisk *);
1991         int (*revalidate_disk) (struct gendisk *);
1992         int (*getgeo)(struct block_device *, struct hd_geometry *);
1993         /* this callback is with swap_lock and sometimes page table lock held */
1994         void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1995         struct module *owner;
1996         const struct pr_ops *pr_ops;
1997 };
1998
1999 extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
2000                                  unsigned long);
2001 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
2002 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
2003                                                 struct writeback_control *);
2004 #else /* CONFIG_BLOCK */
2005
2006 struct block_device;
2007
2008 /*
2009  * stubs for when the block layer is configured out
2010  */
2011 #define buffer_heads_over_limit 0
2012
2013 static inline long nr_blockdev_pages(void)
2014 {
2015         return 0;
2016 }
2017
2018 struct blk_plug {
2019 };
2020
2021 static inline void blk_start_plug(struct blk_plug *plug)
2022 {
2023 }
2024
2025 static inline void blk_finish_plug(struct blk_plug *plug)
2026 {
2027 }
2028
2029 static inline void blk_flush_plug(struct task_struct *task)
2030 {
2031 }
2032
2033 static inline void blk_schedule_flush_plug(struct task_struct *task)
2034 {
2035 }
2036
2037
2038 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
2039 {
2040         return false;
2041 }
2042
2043 static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
2044                                      sector_t *error_sector)
2045 {
2046         return 0;
2047 }
2048
2049 #endif /* CONFIG_BLOCK */
2050
2051 #endif