at v4.10-rc6 57 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_sectors(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 1003/* 1004 * Some commands like WRITE SAME have a payload or data transfer size which 1005 * is different from the size of the request. Any driver that supports such 1006 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to 1007 * calculate the data transfer size. 1008 */ 1009static inline unsigned int blk_rq_payload_bytes(struct request *rq) 1010{ 1011 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 1012 return rq->special_vec.bv_len; 1013 return blk_rq_bytes(rq); 1014} 1015 1016static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q, 1017 int op) 1018{ 1019 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)) 1020 return min(q->limits.max_discard_sectors, UINT_MAX >> 9); 1021 1022 if (unlikely(op == REQ_OP_WRITE_SAME)) 1023 return q->limits.max_write_same_sectors; 1024 1025 if (unlikely(op == REQ_OP_WRITE_ZEROES)) 1026 return q->limits.max_write_zeroes_sectors; 1027 1028 return q->limits.max_sectors; 1029} 1030 1031/* 1032 * Return maximum size of a request at given offset. Only valid for 1033 * file system requests. 1034 */ 1035static inline unsigned int blk_max_size_offset(struct request_queue *q, 1036 sector_t offset) 1037{ 1038 if (!q->limits.chunk_sectors) 1039 return q->limits.max_sectors; 1040 1041 return q->limits.chunk_sectors - 1042 (offset & (q->limits.chunk_sectors - 1)); 1043} 1044 1045static inline unsigned int blk_rq_get_max_sectors(struct request *rq, 1046 sector_t offset) 1047{ 1048 struct request_queue *q = rq->q; 1049 1050 if (unlikely(rq->cmd_type != REQ_TYPE_FS)) 1051 return q->limits.max_hw_sectors; 1052 1053 if (!q->limits.chunk_sectors || 1054 req_op(rq) == REQ_OP_DISCARD || 1055 req_op(rq) == REQ_OP_SECURE_ERASE) 1056 return blk_queue_get_max_sectors(q, req_op(rq)); 1057 1058 return min(blk_max_size_offset(q, offset), 1059 blk_queue_get_max_sectors(q, req_op(rq))); 1060} 1061 1062static inline unsigned int blk_rq_count_bios(struct request *rq) 1063{ 1064 unsigned int nr_bios = 0; 1065 struct bio *bio; 1066 1067 __rq_for_each_bio(bio, rq) 1068 nr_bios++; 1069 1070 return nr_bios; 1071} 1072 1073/* 1074 * blk_rq_set_prio - associate a request with prio from ioc 1075 * @rq: request of interest 1076 * @ioc: target iocontext 1077 * 1078 * Assocate request prio with ioc prio so request based drivers 1079 * can leverage priority information. 1080 */ 1081static inline void blk_rq_set_prio(struct request *rq, struct io_context *ioc) 1082{ 1083 if (ioc) 1084 rq->ioprio = ioc->ioprio; 1085} 1086 1087/* 1088 * Request issue related functions. 1089 */ 1090extern struct request *blk_peek_request(struct request_queue *q); 1091extern void blk_start_request(struct request *rq); 1092extern struct request *blk_fetch_request(struct request_queue *q); 1093 1094/* 1095 * Request completion related functions. 1096 * 1097 * blk_update_request() completes given number of bytes and updates 1098 * the request without completing it. 1099 * 1100 * blk_end_request() and friends. __blk_end_request() must be called 1101 * with the request queue spinlock acquired. 1102 * 1103 * Several drivers define their own end_request and call 1104 * blk_end_request() for parts of the original function. 1105 * This prevents code duplication in drivers. 1106 */ 1107extern bool blk_update_request(struct request *rq, int error, 1108 unsigned int nr_bytes); 1109extern void blk_finish_request(struct request *rq, int error); 1110extern bool blk_end_request(struct request *rq, int error, 1111 unsigned int nr_bytes); 1112extern void blk_end_request_all(struct request *rq, int error); 1113extern bool blk_end_request_cur(struct request *rq, int error); 1114extern bool blk_end_request_err(struct request *rq, int error); 1115extern bool __blk_end_request(struct request *rq, int error, 1116 unsigned int nr_bytes); 1117extern void __blk_end_request_all(struct request *rq, int error); 1118extern bool __blk_end_request_cur(struct request *rq, int error); 1119extern bool __blk_end_request_err(struct request *rq, int error); 1120 1121extern void blk_complete_request(struct request *); 1122extern void __blk_complete_request(struct request *); 1123extern void blk_abort_request(struct request *); 1124extern void blk_unprep_request(struct request *); 1125 1126/* 1127 * Access functions for manipulating queue properties 1128 */ 1129extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn, 1130 spinlock_t *lock, int node_id); 1131extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *); 1132extern struct request_queue *blk_init_allocated_queue(struct request_queue *, 1133 request_fn_proc *, spinlock_t *); 1134extern void blk_cleanup_queue(struct request_queue *); 1135extern void blk_queue_make_request(struct request_queue *, make_request_fn *); 1136extern void blk_queue_bounce_limit(struct request_queue *, u64); 1137extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int); 1138extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int); 1139extern void blk_queue_max_segments(struct request_queue *, unsigned short); 1140extern void blk_queue_max_segment_size(struct request_queue *, unsigned int); 1141extern void blk_queue_max_discard_sectors(struct request_queue *q, 1142 unsigned int max_discard_sectors); 1143extern void blk_queue_max_write_same_sectors(struct request_queue *q, 1144 unsigned int max_write_same_sectors); 1145extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q, 1146 unsigned int max_write_same_sectors); 1147extern void blk_queue_logical_block_size(struct request_queue *, unsigned short); 1148extern void blk_queue_physical_block_size(struct request_queue *, unsigned int); 1149extern void blk_queue_alignment_offset(struct request_queue *q, 1150 unsigned int alignment); 1151extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min); 1152extern void blk_queue_io_min(struct request_queue *q, unsigned int min); 1153extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt); 1154extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt); 1155extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth); 1156extern void blk_set_default_limits(struct queue_limits *lim); 1157extern void blk_set_stacking_limits(struct queue_limits *lim); 1158extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 1159 sector_t offset); 1160extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev, 1161 sector_t offset); 1162extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev, 1163 sector_t offset); 1164extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b); 1165extern void blk_queue_dma_pad(struct request_queue *, unsigned int); 1166extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int); 1167extern int blk_queue_dma_drain(struct request_queue *q, 1168 dma_drain_needed_fn *dma_drain_needed, 1169 void *buf, unsigned int size); 1170extern void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn); 1171extern void blk_queue_segment_boundary(struct request_queue *, unsigned long); 1172extern void blk_queue_virt_boundary(struct request_queue *, unsigned long); 1173extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn); 1174extern void blk_queue_unprep_rq(struct request_queue *, unprep_rq_fn *ufn); 1175extern void blk_queue_dma_alignment(struct request_queue *, int); 1176extern void blk_queue_update_dma_alignment(struct request_queue *, int); 1177extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *); 1178extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *); 1179extern void blk_queue_rq_timeout(struct request_queue *, unsigned int); 1180extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable); 1181extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua); 1182extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev); 1183 1184static inline unsigned short blk_rq_nr_phys_segments(struct request *rq) 1185{ 1186 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 1187 return 1; 1188 return rq->nr_phys_segments; 1189} 1190 1191extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *); 1192extern void blk_dump_rq_flags(struct request *, char *); 1193extern long nr_blockdev_pages(void); 1194 1195bool __must_check blk_get_queue(struct request_queue *); 1196struct request_queue *blk_alloc_queue(gfp_t); 1197struct request_queue *blk_alloc_queue_node(gfp_t, int); 1198extern void blk_put_queue(struct request_queue *); 1199extern void blk_set_queue_dying(struct request_queue *); 1200 1201/* 1202 * block layer runtime pm functions 1203 */ 1204#ifdef CONFIG_PM 1205extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev); 1206extern int blk_pre_runtime_suspend(struct request_queue *q); 1207extern void blk_post_runtime_suspend(struct request_queue *q, int err); 1208extern void blk_pre_runtime_resume(struct request_queue *q); 1209extern void blk_post_runtime_resume(struct request_queue *q, int err); 1210extern void blk_set_runtime_active(struct request_queue *q); 1211#else 1212static inline void blk_pm_runtime_init(struct request_queue *q, 1213 struct device *dev) {} 1214static inline int blk_pre_runtime_suspend(struct request_queue *q) 1215{ 1216 return -ENOSYS; 1217} 1218static inline void blk_post_runtime_suspend(struct request_queue *q, int err) {} 1219static inline void blk_pre_runtime_resume(struct request_queue *q) {} 1220static inline void blk_post_runtime_resume(struct request_queue *q, int err) {} 1221static inline void blk_set_runtime_active(struct request_queue *q) {} 1222#endif 1223 1224/* 1225 * blk_plug permits building a queue of related requests by holding the I/O 1226 * fragments for a short period. This allows merging of sequential requests 1227 * into single larger request. As the requests are moved from a per-task list to 1228 * the device's request_queue in a batch, this results in improved scalability 1229 * as the lock contention for request_queue lock is reduced. 1230 * 1231 * It is ok not to disable preemption when adding the request to the plug list 1232 * or when attempting a merge, because blk_schedule_flush_list() will only flush 1233 * the plug list when the task sleeps by itself. For details, please see 1234 * schedule() where blk_schedule_flush_plug() is called. 1235 */ 1236struct blk_plug { 1237 struct list_head list; /* requests */ 1238 struct list_head mq_list; /* blk-mq requests */ 1239 struct list_head cb_list; /* md requires an unplug callback */ 1240}; 1241#define BLK_MAX_REQUEST_COUNT 16 1242#define BLK_PLUG_FLUSH_SIZE (128 * 1024) 1243 1244struct blk_plug_cb; 1245typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool); 1246struct blk_plug_cb { 1247 struct list_head list; 1248 blk_plug_cb_fn callback; 1249 void *data; 1250}; 1251extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, 1252 void *data, int size); 1253extern void blk_start_plug(struct blk_plug *); 1254extern void blk_finish_plug(struct blk_plug *); 1255extern void blk_flush_plug_list(struct blk_plug *, bool); 1256 1257static inline void blk_flush_plug(struct task_struct *tsk) 1258{ 1259 struct blk_plug *plug = tsk->plug; 1260 1261 if (plug) 1262 blk_flush_plug_list(plug, false); 1263} 1264 1265static inline void blk_schedule_flush_plug(struct task_struct *tsk) 1266{ 1267 struct blk_plug *plug = tsk->plug; 1268 1269 if (plug) 1270 blk_flush_plug_list(plug, true); 1271} 1272 1273static inline bool blk_needs_flush_plug(struct task_struct *tsk) 1274{ 1275 struct blk_plug *plug = tsk->plug; 1276 1277 return plug && 1278 (!list_empty(&plug->list) || 1279 !list_empty(&plug->mq_list) || 1280 !list_empty(&plug->cb_list)); 1281} 1282 1283/* 1284 * tag stuff 1285 */ 1286extern int blk_queue_start_tag(struct request_queue *, struct request *); 1287extern struct request *blk_queue_find_tag(struct request_queue *, int); 1288extern void blk_queue_end_tag(struct request_queue *, struct request *); 1289extern int blk_queue_init_tags(struct request_queue *, int, struct blk_queue_tag *, int); 1290extern void blk_queue_free_tags(struct request_queue *); 1291extern int blk_queue_resize_tags(struct request_queue *, int); 1292extern void blk_queue_invalidate_tags(struct request_queue *); 1293extern struct blk_queue_tag *blk_init_tags(int, int); 1294extern void blk_free_tags(struct blk_queue_tag *); 1295 1296static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt, 1297 int tag) 1298{ 1299 if (unlikely(bqt == NULL || tag >= bqt->real_max_depth)) 1300 return NULL; 1301 return bqt->tag_index[tag]; 1302} 1303 1304 1305#define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */ 1306#define BLKDEV_DISCARD_ZERO (1 << 1) /* must reliably zero data */ 1307 1308extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *); 1309extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector, 1310 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags); 1311extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, 1312 sector_t nr_sects, gfp_t gfp_mask, int flags, 1313 struct bio **biop); 1314extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, 1315 sector_t nr_sects, gfp_t gfp_mask, struct page *page); 1316extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 1317 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, 1318 bool discard); 1319extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 1320 sector_t nr_sects, gfp_t gfp_mask, bool discard); 1321static inline int sb_issue_discard(struct super_block *sb, sector_t block, 1322 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags) 1323{ 1324 return blkdev_issue_discard(sb->s_bdev, block << (sb->s_blocksize_bits - 9), 1325 nr_blocks << (sb->s_blocksize_bits - 9), 1326 gfp_mask, flags); 1327} 1328static inline int sb_issue_zeroout(struct super_block *sb, sector_t block, 1329 sector_t nr_blocks, gfp_t gfp_mask) 1330{ 1331 return blkdev_issue_zeroout(sb->s_bdev, 1332 block << (sb->s_blocksize_bits - 9), 1333 nr_blocks << (sb->s_blocksize_bits - 9), 1334 gfp_mask, true); 1335} 1336 1337extern int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm); 1338 1339enum blk_default_limits { 1340 BLK_MAX_SEGMENTS = 128, 1341 BLK_SAFE_MAX_SECTORS = 255, 1342 BLK_DEF_MAX_SECTORS = 2560, 1343 BLK_MAX_SEGMENT_SIZE = 65536, 1344 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL, 1345}; 1346 1347#define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist) 1348 1349static inline unsigned long queue_bounce_pfn(struct request_queue *q) 1350{ 1351 return q->limits.bounce_pfn; 1352} 1353 1354static inline unsigned long queue_segment_boundary(struct request_queue *q) 1355{ 1356 return q->limits.seg_boundary_mask; 1357} 1358 1359static inline unsigned long queue_virt_boundary(struct request_queue *q) 1360{ 1361 return q->limits.virt_boundary_mask; 1362} 1363 1364static inline unsigned int queue_max_sectors(struct request_queue *q) 1365{ 1366 return q->limits.max_sectors; 1367} 1368 1369static inline unsigned int queue_max_hw_sectors(struct request_queue *q) 1370{ 1371 return q->limits.max_hw_sectors; 1372} 1373 1374static inline unsigned short queue_max_segments(struct request_queue *q) 1375{ 1376 return q->limits.max_segments; 1377} 1378 1379static inline unsigned int queue_max_segment_size(struct request_queue *q) 1380{ 1381 return q->limits.max_segment_size; 1382} 1383 1384static inline unsigned short queue_logical_block_size(struct request_queue *q) 1385{ 1386 int retval = 512; 1387 1388 if (q && q->limits.logical_block_size) 1389 retval = q->limits.logical_block_size; 1390 1391 return retval; 1392} 1393 1394static inline unsigned short bdev_logical_block_size(struct block_device *bdev) 1395{ 1396 return queue_logical_block_size(bdev_get_queue(bdev)); 1397} 1398 1399static inline unsigned int queue_physical_block_size(struct request_queue *q) 1400{ 1401 return q->limits.physical_block_size; 1402} 1403 1404static inline unsigned int bdev_physical_block_size(struct block_device *bdev) 1405{ 1406 return queue_physical_block_size(bdev_get_queue(bdev)); 1407} 1408 1409static inline unsigned int queue_io_min(struct request_queue *q) 1410{ 1411 return q->limits.io_min; 1412} 1413 1414static inline int bdev_io_min(struct block_device *bdev) 1415{ 1416 return queue_io_min(bdev_get_queue(bdev)); 1417} 1418 1419static inline unsigned int queue_io_opt(struct request_queue *q) 1420{ 1421 return q->limits.io_opt; 1422} 1423 1424static inline int bdev_io_opt(struct block_device *bdev) 1425{ 1426 return queue_io_opt(bdev_get_queue(bdev)); 1427} 1428 1429static inline int queue_alignment_offset(struct request_queue *q) 1430{ 1431 if (q->limits.misaligned) 1432 return -1; 1433 1434 return q->limits.alignment_offset; 1435} 1436 1437static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector) 1438{ 1439 unsigned int granularity = max(lim->physical_block_size, lim->io_min); 1440 unsigned int alignment = sector_div(sector, granularity >> 9) << 9; 1441 1442 return (granularity + lim->alignment_offset - alignment) % granularity; 1443} 1444 1445static inline int bdev_alignment_offset(struct block_device *bdev) 1446{ 1447 struct request_queue *q = bdev_get_queue(bdev); 1448 1449 if (q->limits.misaligned) 1450 return -1; 1451 1452 if (bdev != bdev->bd_contains) 1453 return bdev->bd_part->alignment_offset; 1454 1455 return q->limits.alignment_offset; 1456} 1457 1458static inline int queue_discard_alignment(struct request_queue *q) 1459{ 1460 if (q->limits.discard_misaligned) 1461 return -1; 1462 1463 return q->limits.discard_alignment; 1464} 1465 1466static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector) 1467{ 1468 unsigned int alignment, granularity, offset; 1469 1470 if (!lim->max_discard_sectors) 1471 return 0; 1472 1473 /* Why are these in bytes, not sectors? */ 1474 alignment = lim->discard_alignment >> 9; 1475 granularity = lim->discard_granularity >> 9; 1476 if (!granularity) 1477 return 0; 1478 1479 /* Offset of the partition start in 'granularity' sectors */ 1480 offset = sector_div(sector, granularity); 1481 1482 /* And why do we do this modulus *again* in blkdev_issue_discard()? */ 1483 offset = (granularity + alignment - offset) % granularity; 1484 1485 /* Turn it back into bytes, gaah */ 1486 return offset << 9; 1487} 1488 1489static inline int bdev_discard_alignment(struct block_device *bdev) 1490{ 1491 struct request_queue *q = bdev_get_queue(bdev); 1492 1493 if (bdev != bdev->bd_contains) 1494 return bdev->bd_part->discard_alignment; 1495 1496 return q->limits.discard_alignment; 1497} 1498 1499static inline unsigned int queue_discard_zeroes_data(struct request_queue *q) 1500{ 1501 if (q->limits.max_discard_sectors && q->limits.discard_zeroes_data == 1) 1502 return 1; 1503 1504 return 0; 1505} 1506 1507static inline unsigned int bdev_discard_zeroes_data(struct block_device *bdev) 1508{ 1509 return queue_discard_zeroes_data(bdev_get_queue(bdev)); 1510} 1511 1512static inline unsigned int bdev_write_same(struct block_device *bdev) 1513{ 1514 struct request_queue *q = bdev_get_queue(bdev); 1515 1516 if (q) 1517 return q->limits.max_write_same_sectors; 1518 1519 return 0; 1520} 1521 1522static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev) 1523{ 1524 struct request_queue *q = bdev_get_queue(bdev); 1525 1526 if (q) 1527 return q->limits.max_write_zeroes_sectors; 1528 1529 return 0; 1530} 1531 1532static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev) 1533{ 1534 struct request_queue *q = bdev_get_queue(bdev); 1535 1536 if (q) 1537 return blk_queue_zoned_model(q); 1538 1539 return BLK_ZONED_NONE; 1540} 1541 1542static inline bool bdev_is_zoned(struct block_device *bdev) 1543{ 1544 struct request_queue *q = bdev_get_queue(bdev); 1545 1546 if (q) 1547 return blk_queue_is_zoned(q); 1548 1549 return false; 1550} 1551 1552static inline unsigned int bdev_zone_sectors(struct block_device *bdev) 1553{ 1554 struct request_queue *q = bdev_get_queue(bdev); 1555 1556 if (q) 1557 return blk_queue_zone_sectors(q); 1558 1559 return 0; 1560} 1561 1562static inline int queue_dma_alignment(struct request_queue *q) 1563{ 1564 return q ? q->dma_alignment : 511; 1565} 1566 1567static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr, 1568 unsigned int len) 1569{ 1570 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask; 1571 return !(addr & alignment) && !(len & alignment); 1572} 1573 1574/* assumes size > 256 */ 1575static inline unsigned int blksize_bits(unsigned int size) 1576{ 1577 unsigned int bits = 8; 1578 do { 1579 bits++; 1580 size >>= 1; 1581 } while (size > 256); 1582 return bits; 1583} 1584 1585static inline unsigned int block_size(struct block_device *bdev) 1586{ 1587 return bdev->bd_block_size; 1588} 1589 1590static inline bool queue_flush_queueable(struct request_queue *q) 1591{ 1592 return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags); 1593} 1594 1595typedef struct {struct page *v;} Sector; 1596 1597unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *); 1598 1599static inline void put_dev_sector(Sector p) 1600{ 1601 put_page(p.v); 1602} 1603 1604static inline bool __bvec_gap_to_prev(struct request_queue *q, 1605 struct bio_vec *bprv, unsigned int offset) 1606{ 1607 return offset || 1608 ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q)); 1609} 1610 1611/* 1612 * Check if adding a bio_vec after bprv with offset would create a gap in 1613 * the SG list. Most drivers don't care about this, but some do. 1614 */ 1615static inline bool bvec_gap_to_prev(struct request_queue *q, 1616 struct bio_vec *bprv, unsigned int offset) 1617{ 1618 if (!queue_virt_boundary(q)) 1619 return false; 1620 return __bvec_gap_to_prev(q, bprv, offset); 1621} 1622 1623static inline bool bio_will_gap(struct request_queue *q, struct bio *prev, 1624 struct bio *next) 1625{ 1626 if (bio_has_data(prev) && queue_virt_boundary(q)) { 1627 struct bio_vec pb, nb; 1628 1629 bio_get_last_bvec(prev, &pb); 1630 bio_get_first_bvec(next, &nb); 1631 1632 return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 1633 } 1634 1635 return false; 1636} 1637 1638static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 1639{ 1640 return bio_will_gap(req->q, req->biotail, bio); 1641} 1642 1643static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 1644{ 1645 return bio_will_gap(req->q, bio, req->bio); 1646} 1647 1648int kblockd_schedule_work(struct work_struct *work); 1649int kblockd_schedule_work_on(int cpu, struct work_struct *work); 1650int kblockd_schedule_delayed_work(struct delayed_work *dwork, unsigned long delay); 1651int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay); 1652 1653#ifdef CONFIG_BLK_CGROUP 1654/* 1655 * This should not be using sched_clock(). A real patch is in progress 1656 * to fix this up, until that is in place we need to disable preemption 1657 * around sched_clock() in this function and set_io_start_time_ns(). 1658 */ 1659static inline void set_start_time_ns(struct request *req) 1660{ 1661 preempt_disable(); 1662 req->start_time_ns = sched_clock(); 1663 preempt_enable(); 1664} 1665 1666static inline void set_io_start_time_ns(struct request *req) 1667{ 1668 preempt_disable(); 1669 req->io_start_time_ns = sched_clock(); 1670 preempt_enable(); 1671} 1672 1673static inline uint64_t rq_start_time_ns(struct request *req) 1674{ 1675 return req->start_time_ns; 1676} 1677 1678static inline uint64_t rq_io_start_time_ns(struct request *req) 1679{ 1680 return req->io_start_time_ns; 1681} 1682#else 1683static inline void set_start_time_ns(struct request *req) {} 1684static inline void set_io_start_time_ns(struct request *req) {} 1685static inline uint64_t rq_start_time_ns(struct request *req) 1686{ 1687 return 0; 1688} 1689static inline uint64_t rq_io_start_time_ns(struct request *req) 1690{ 1691 return 0; 1692} 1693#endif 1694 1695#define MODULE_ALIAS_BLOCKDEV(major,minor) \ 1696 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor)) 1697#define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \ 1698 MODULE_ALIAS("block-major-" __stringify(major) "-*") 1699 1700#if defined(CONFIG_BLK_DEV_INTEGRITY) 1701 1702enum blk_integrity_flags { 1703 BLK_INTEGRITY_VERIFY = 1 << 0, 1704 BLK_INTEGRITY_GENERATE = 1 << 1, 1705 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2, 1706 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3, 1707}; 1708 1709struct blk_integrity_iter { 1710 void *prot_buf; 1711 void *data_buf; 1712 sector_t seed; 1713 unsigned int data_size; 1714 unsigned short interval; 1715 const char *disk_name; 1716}; 1717 1718typedef int (integrity_processing_fn) (struct blk_integrity_iter *); 1719 1720struct blk_integrity_profile { 1721 integrity_processing_fn *generate_fn; 1722 integrity_processing_fn *verify_fn; 1723 const char *name; 1724}; 1725 1726extern void blk_integrity_register(struct gendisk *, struct blk_integrity *); 1727extern void blk_integrity_unregister(struct gendisk *); 1728extern int blk_integrity_compare(struct gendisk *, struct gendisk *); 1729extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *, 1730 struct scatterlist *); 1731extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *); 1732extern bool blk_integrity_merge_rq(struct request_queue *, struct request *, 1733 struct request *); 1734extern bool blk_integrity_merge_bio(struct request_queue *, struct request *, 1735 struct bio *); 1736 1737static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk) 1738{ 1739 struct blk_integrity *bi = &disk->queue->integrity; 1740 1741 if (!bi->profile) 1742 return NULL; 1743 1744 return bi; 1745} 1746 1747static inline 1748struct blk_integrity *bdev_get_integrity(struct block_device *bdev) 1749{ 1750 return blk_get_integrity(bdev->bd_disk); 1751} 1752 1753static inline bool blk_integrity_rq(struct request *rq) 1754{ 1755 return rq->cmd_flags & REQ_INTEGRITY; 1756} 1757 1758static inline void blk_queue_max_integrity_segments(struct request_queue *q, 1759 unsigned int segs) 1760{ 1761 q->limits.max_integrity_segments = segs; 1762} 1763 1764static inline unsigned short 1765queue_max_integrity_segments(struct request_queue *q) 1766{ 1767 return q->limits.max_integrity_segments; 1768} 1769 1770static inline bool integrity_req_gap_back_merge(struct request *req, 1771 struct bio *next) 1772{ 1773 struct bio_integrity_payload *bip = bio_integrity(req->bio); 1774 struct bio_integrity_payload *bip_next = bio_integrity(next); 1775 1776 return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1], 1777 bip_next->bip_vec[0].bv_offset); 1778} 1779 1780static inline bool integrity_req_gap_front_merge(struct request *req, 1781 struct bio *bio) 1782{ 1783 struct bio_integrity_payload *bip = bio_integrity(bio); 1784 struct bio_integrity_payload *bip_next = bio_integrity(req->bio); 1785 1786 return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1], 1787 bip_next->bip_vec[0].bv_offset); 1788} 1789 1790#else /* CONFIG_BLK_DEV_INTEGRITY */ 1791 1792struct bio; 1793struct block_device; 1794struct gendisk; 1795struct blk_integrity; 1796 1797static inline int blk_integrity_rq(struct request *rq) 1798{ 1799 return 0; 1800} 1801static inline int blk_rq_count_integrity_sg(struct request_queue *q, 1802 struct bio *b) 1803{ 1804 return 0; 1805} 1806static inline int blk_rq_map_integrity_sg(struct request_queue *q, 1807 struct bio *b, 1808 struct scatterlist *s) 1809{ 1810 return 0; 1811} 1812static inline struct blk_integrity *bdev_get_integrity(struct block_device *b) 1813{ 1814 return NULL; 1815} 1816static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk) 1817{ 1818 return NULL; 1819} 1820static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b) 1821{ 1822 return 0; 1823} 1824static inline void blk_integrity_register(struct gendisk *d, 1825 struct blk_integrity *b) 1826{ 1827} 1828static inline void blk_integrity_unregister(struct gendisk *d) 1829{ 1830} 1831static inline void blk_queue_max_integrity_segments(struct request_queue *q, 1832 unsigned int segs) 1833{ 1834} 1835static inline unsigned short queue_max_integrity_segments(struct request_queue *q) 1836{ 1837 return 0; 1838} 1839static inline bool blk_integrity_merge_rq(struct request_queue *rq, 1840 struct request *r1, 1841 struct request *r2) 1842{ 1843 return true; 1844} 1845static inline bool blk_integrity_merge_bio(struct request_queue *rq, 1846 struct request *r, 1847 struct bio *b) 1848{ 1849 return true; 1850} 1851 1852static inline bool integrity_req_gap_back_merge(struct request *req, 1853 struct bio *next) 1854{ 1855 return false; 1856} 1857static inline bool integrity_req_gap_front_merge(struct request *req, 1858 struct bio *bio) 1859{ 1860 return false; 1861} 1862 1863#endif /* CONFIG_BLK_DEV_INTEGRITY */ 1864 1865/** 1866 * struct blk_dax_ctl - control and output parameters for ->direct_access 1867 * @sector: (input) offset relative to a block_device 1868 * @addr: (output) kernel virtual address for @sector populated by driver 1869 * @pfn: (output) page frame number for @addr populated by driver 1870 * @size: (input) number of bytes requested 1871 */ 1872struct blk_dax_ctl { 1873 sector_t sector; 1874 void *addr; 1875 long size; 1876 pfn_t pfn; 1877}; 1878 1879struct block_device_operations { 1880 int (*open) (struct block_device *, fmode_t); 1881 void (*release) (struct gendisk *, fmode_t); 1882 int (*rw_page)(struct block_device *, sector_t, struct page *, bool); 1883 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long); 1884 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long); 1885 long (*direct_access)(struct block_device *, sector_t, void **, pfn_t *, 1886 long); 1887 unsigned int (*check_events) (struct gendisk *disk, 1888 unsigned int clearing); 1889 /* ->media_changed() is DEPRECATED, use ->check_events() instead */ 1890 int (*media_changed) (struct gendisk *); 1891 void (*unlock_native_capacity) (struct gendisk *); 1892 int (*revalidate_disk) (struct gendisk *); 1893 int (*getgeo)(struct block_device *, struct hd_geometry *); 1894 /* this callback is with swap_lock and sometimes page table lock held */ 1895 void (*swap_slot_free_notify) (struct block_device *, unsigned long); 1896 struct module *owner; 1897 const struct pr_ops *pr_ops; 1898}; 1899 1900extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int, 1901 unsigned long); 1902extern int bdev_read_page(struct block_device *, sector_t, struct page *); 1903extern int bdev_write_page(struct block_device *, sector_t, struct page *, 1904 struct writeback_control *); 1905extern long bdev_direct_access(struct block_device *, struct blk_dax_ctl *); 1906extern int bdev_dax_supported(struct super_block *, int); 1907extern bool bdev_dax_capable(struct block_device *); 1908#else /* CONFIG_BLOCK */ 1909 1910struct block_device; 1911 1912/* 1913 * stubs for when the block layer is configured out 1914 */ 1915#define buffer_heads_over_limit 0 1916 1917static inline long nr_blockdev_pages(void) 1918{ 1919 return 0; 1920} 1921 1922struct blk_plug { 1923}; 1924 1925static inline void blk_start_plug(struct blk_plug *plug) 1926{ 1927} 1928 1929static inline void blk_finish_plug(struct blk_plug *plug) 1930{ 1931} 1932 1933static inline void blk_flush_plug(struct task_struct *task) 1934{ 1935} 1936 1937static inline void blk_schedule_flush_plug(struct task_struct *task) 1938{ 1939} 1940 1941 1942static inline bool blk_needs_flush_plug(struct task_struct *tsk) 1943{ 1944 return false; 1945} 1946 1947static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask, 1948 sector_t *error_sector) 1949{ 1950 return 0; 1951} 1952 1953#endif /* CONFIG_BLOCK */ 1954 1955#endif