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