~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/blkdev.h

Version: ~ [ linux-5.13-rc5 ] ~ [ linux-5.12.9 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.42 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.124 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.193 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.235 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.271 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.271 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp