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