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