1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BLKDEV_H
3 #define _LINUX_BLKDEV_H
5 #include <linux/sched.h>
6 #include <linux/sched/clock.h>
10 #include <linux/major.h>
11 #include <linux/genhd.h>
12 #include <linux/list.h>
13 #include <linux/llist.h>
14 #include <linux/timer.h>
15 #include <linux/workqueue.h>
16 #include <linux/pagemap.h>
17 #include <linux/backing-dev-defs.h>
18 #include <linux/wait.h>
19 #include <linux/mempool.h>
20 #include <linux/pfn.h>
21 #include <linux/bio.h>
22 #include <linux/stringify.h>
23 #include <linux/gfp.h>
24 #include <linux/bsg.h>
25 #include <linux/smp.h>
26 #include <linux/rcupdate.h>
27 #include <linux/percpu-refcount.h>
28 #include <linux/scatterlist.h>
29 #include <linux/blkzoned.h>
32 struct scsi_ioctl_command;
35 struct elevator_queue;
41 struct blk_flush_queue;
44 struct blk_queue_stats;
45 struct blk_stat_callback;
47 #define BLKDEV_MIN_RQ 4
48 #define BLKDEV_MAX_RQ 128 /* Default maximum */
50 /* Must be consisitent with blk_mq_poll_stats_bkt() */
51 #define BLK_MQ_POLL_STATS_BKTS 16
54 * Maximum number of blkcg policies allowed to be registered concurrently.
55 * Defined here to simplify include dependency.
57 #define BLKCG_MAX_POLS 3
59 static inline int blk_validate_block_size(unsigned int bsize)
61 if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
67 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
69 #define BLK_RL_SYNCFULL (1U << 0)
70 #define BLK_RL_ASYNCFULL (1U << 1)
73 struct request_queue *q; /* the queue this rl belongs to */
74 #ifdef CONFIG_BLK_CGROUP
75 struct blkcg_gq *blkg; /* blkg this request pool belongs to */
78 * count[], starved[], and wait[] are indexed by
79 * BLK_RW_SYNC/BLK_RW_ASYNC
84 wait_queue_head_t wait[2];
90 typedef __u32 __bitwise req_flags_t;
92 /* elevator knows about this request */
93 #define RQF_SORTED ((__force req_flags_t)(1 << 0))
94 /* drive already may have started this one */
95 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
96 /* uses tagged queueing */
97 #define RQF_QUEUED ((__force req_flags_t)(1 << 2))
98 /* may not be passed by ioscheduler */
99 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
100 /* request for flush sequence */
101 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
102 /* merge of different types, fail separately */
103 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
104 /* track inflight for MQ */
105 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
106 /* don't call prep for this one */
107 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
108 /* set for "ide_preempt" requests and also for requests for which the SCSI
109 "quiesce" state must be ignored. */
110 #define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
111 /* contains copies of user pages */
112 #define RQF_COPY_USER ((__force req_flags_t)(1 << 9))
113 /* vaguely specified driver internal error. Ignored by the block layer */
114 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
115 /* don't warn about errors */
116 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
117 /* elevator private data attached */
118 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
119 /* account I/O stat */
120 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
121 /* request came from our alloc pool */
122 #define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
123 /* runtime pm request */
124 #define RQF_PM ((__force req_flags_t)(1 << 15))
125 /* on IO scheduler merge hash */
126 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
127 /* IO stats tracking on */
128 #define RQF_STATS ((__force req_flags_t)(1 << 17))
129 /* Look at ->special_vec for the actual data payload instead of the
131 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
133 /* flags that prevent us from merging requests: */
134 #define RQF_NOMERGE_FLAGS \
135 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
138 * Try to put the fields that are referenced together in the same cacheline.
140 * If you modify this structure, make sure to update blk_rq_init() and
141 * especially blk_mq_rq_ctx_init() to take care of the added fields.
144 struct list_head queuelist;
146 struct __call_single_data csd;
150 struct request_queue *q;
151 struct blk_mq_ctx *mq_ctx;
154 unsigned int cmd_flags; /* op and common flags */
155 req_flags_t rq_flags;
159 unsigned long atomic_flags;
161 /* the following two fields are internal, NEVER access directly */
162 unsigned int __data_len; /* total data len */
164 sector_t __sector; /* sector cursor */
170 * The hash is used inside the scheduler, and killed once the
171 * request reaches the dispatch list. The ipi_list is only used
172 * to queue the request for softirq completion, which is long
173 * after the request has been unhashed (and even removed from
174 * the dispatch list).
177 struct hlist_node hash; /* merge hash */
178 struct list_head ipi_list;
182 * The rb_node is only used inside the io scheduler, requests
183 * are pruned when moved to the dispatch queue. So let the
184 * completion_data share space with the rb_node.
187 struct rb_node rb_node; /* sort/lookup */
188 struct bio_vec special_vec;
189 void *completion_data;
190 int error_count; /* for legacy drivers, don't use */
194 * Three pointers are available for the IO schedulers, if they need
195 * more they have to dynamically allocate it. Flush requests are
196 * never put on the IO scheduler. So let the flush fields share
197 * space with the elevator data.
207 struct list_head list;
208 rq_end_io_fn *saved_end_io;
212 struct gendisk *rq_disk;
213 struct hd_struct *part;
214 unsigned long start_time;
215 struct blk_issue_stat issue_stat;
216 #ifdef CONFIG_BLK_CGROUP
217 struct request_list *rl; /* rl this rq is alloced from */
218 unsigned long long start_time_ns;
219 unsigned long long io_start_time_ns; /* when passed to hardware */
221 /* Number of scatter-gather DMA addr+len pairs after
222 * physical address coalescing is performed.
224 unsigned short nr_phys_segments;
225 #if defined(CONFIG_BLK_DEV_INTEGRITY)
226 unsigned short nr_integrity_segments;
229 unsigned short ioprio;
231 unsigned int timeout;
233 void *special; /* opaque pointer available for LLD use */
235 unsigned int extra_len; /* length of alignment and padding */
237 unsigned short write_hint;
239 unsigned long deadline;
240 struct list_head timeout_list;
243 * completion callback.
245 rq_end_io_fn *end_io;
249 struct request *next_rq;
252 static inline bool blk_op_is_scsi(unsigned int op)
254 return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
257 static inline bool blk_op_is_private(unsigned int op)
259 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
262 static inline bool blk_rq_is_scsi(struct request *rq)
264 return blk_op_is_scsi(req_op(rq));
267 static inline bool blk_rq_is_private(struct request *rq)
269 return blk_op_is_private(req_op(rq));
272 static inline bool blk_rq_is_passthrough(struct request *rq)
274 return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
277 static inline bool bio_is_passthrough(struct bio *bio)
279 unsigned op = bio_op(bio);
281 return blk_op_is_scsi(op) || blk_op_is_private(op);
284 static inline unsigned short req_get_ioprio(struct request *req)
289 #include <linux/elevator.h>
291 struct blk_queue_ctx;
293 typedef void (request_fn_proc) (struct request_queue *q);
294 typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
295 typedef int (prep_rq_fn) (struct request_queue *, struct request *);
296 typedef void (unprep_rq_fn) (struct request_queue *, struct request *);
299 typedef void (softirq_done_fn)(struct request *);
300 typedef int (dma_drain_needed_fn)(struct request *);
301 typedef int (lld_busy_fn) (struct request_queue *q);
302 typedef int (bsg_job_fn) (struct bsg_job *);
303 typedef int (init_rq_fn)(struct request_queue *, struct request *, gfp_t);
304 typedef void (exit_rq_fn)(struct request_queue *, struct request *);
306 enum blk_eh_timer_return {
312 typedef enum blk_eh_timer_return (rq_timed_out_fn)(struct request *);
314 enum blk_queue_state {
319 struct blk_queue_tag {
320 struct request **tag_index; /* map of busy tags */
321 unsigned long *tag_map; /* bit map of free/busy tags */
322 int max_depth; /* what we will send to device */
323 int real_max_depth; /* what the array can hold */
324 atomic_t refcnt; /* map can be shared */
325 int alloc_policy; /* tag allocation policy */
326 int next_tag; /* next tag */
328 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
329 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
331 #define BLK_SCSI_MAX_CMDS (256)
332 #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
335 * Zoned block device models (zoned limit).
337 enum blk_zoned_model {
338 BLK_ZONED_NONE, /* Regular block device */
339 BLK_ZONED_HA, /* Host-aware zoned block device */
340 BLK_ZONED_HM, /* Host-managed zoned block device */
343 struct queue_limits {
344 unsigned long bounce_pfn;
345 unsigned long seg_boundary_mask;
346 unsigned long virt_boundary_mask;
348 unsigned int max_hw_sectors;
349 unsigned int max_dev_sectors;
350 unsigned int chunk_sectors;
351 unsigned int max_sectors;
352 unsigned int max_segment_size;
353 unsigned int physical_block_size;
354 unsigned int logical_block_size;
355 unsigned int alignment_offset;
358 unsigned int max_discard_sectors;
359 unsigned int max_hw_discard_sectors;
360 unsigned int max_write_same_sectors;
361 unsigned int max_write_zeroes_sectors;
362 unsigned int discard_granularity;
363 unsigned int discard_alignment;
365 unsigned short max_segments;
366 unsigned short max_integrity_segments;
367 unsigned short max_discard_segments;
369 unsigned char misaligned;
370 unsigned char discard_misaligned;
371 unsigned char cluster;
372 unsigned char raid_partial_stripes_expensive;
373 enum blk_zoned_model zoned;
376 #ifdef CONFIG_BLK_DEV_ZONED
378 struct blk_zone_report_hdr {
379 unsigned int nr_zones;
383 extern int blkdev_report_zones(struct block_device *bdev,
384 sector_t sector, struct blk_zone *zones,
385 unsigned int *nr_zones, gfp_t gfp_mask);
386 extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors,
387 sector_t nr_sectors, gfp_t gfp_mask);
389 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
390 unsigned int cmd, unsigned long arg);
391 extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
392 unsigned int cmd, unsigned long arg);
394 #else /* CONFIG_BLK_DEV_ZONED */
396 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
397 fmode_t mode, unsigned int cmd,
403 static inline int blkdev_reset_zones_ioctl(struct block_device *bdev,
404 fmode_t mode, unsigned int cmd,
410 #endif /* CONFIG_BLK_DEV_ZONED */
412 struct request_queue {
414 * Together with queue_head for cacheline sharing
416 struct list_head queue_head;
417 struct request *last_merge;
418 struct elevator_queue *elevator;
419 int nr_rqs[2]; /* # allocated [a]sync rqs */
420 int nr_rqs_elvpriv; /* # allocated rqs w/ elvpriv */
422 atomic_t shared_hctx_restart;
424 struct blk_queue_stats *stats;
428 * If blkcg is not used, @q->root_rl serves all requests. If blkcg
429 * is used, root blkg allocates from @q->root_rl and all other
430 * blkgs from their own blkg->rl. Which one to use should be
431 * determined using bio_request_list().
433 struct request_list root_rl;
435 request_fn_proc *request_fn;
436 make_request_fn *make_request_fn;
437 prep_rq_fn *prep_rq_fn;
438 unprep_rq_fn *unprep_rq_fn;
439 softirq_done_fn *softirq_done_fn;
440 rq_timed_out_fn *rq_timed_out_fn;
441 dma_drain_needed_fn *dma_drain_needed;
442 lld_busy_fn *lld_busy_fn;
443 /* Called just after a request is allocated */
444 init_rq_fn *init_rq_fn;
445 /* Called just before a request is freed */
446 exit_rq_fn *exit_rq_fn;
447 /* Called from inside blk_get_request() */
448 void (*initialize_rq_fn)(struct request *rq);
450 const struct blk_mq_ops *mq_ops;
452 unsigned int *mq_map;
455 struct blk_mq_ctx __percpu *queue_ctx;
456 unsigned int nr_queues;
458 unsigned int queue_depth;
460 /* hw dispatch queues */
461 struct blk_mq_hw_ctx **queue_hw_ctx;
462 unsigned int nr_hw_queues;
465 * Dispatch queue sorting
468 struct request *boundary_rq;
471 * Delayed queue handling
473 struct delayed_work delay_work;
475 struct backing_dev_info *backing_dev_info;
478 * The queue owner gets to use this for whatever they like.
479 * ll_rw_blk doesn't touch it.
484 * various queue flags, see QUEUE_* below
486 unsigned long queue_flags;
489 * ida allocated id for this queue. Used to index queues from
495 * queue needs bounce pages for pages above this limit
500 * protects queue structures from reentrancy. ->__queue_lock should
501 * _never_ be used directly, it is queue private. always use
504 spinlock_t __queue_lock;
505 spinlock_t *queue_lock;
515 struct kobject mq_kobj;
517 #ifdef CONFIG_BLK_DEV_INTEGRITY
518 struct blk_integrity integrity;
519 #endif /* CONFIG_BLK_DEV_INTEGRITY */
524 unsigned int nr_pending;
530 unsigned long nr_requests; /* Max # of requests */
531 unsigned int nr_congestion_on;
532 unsigned int nr_congestion_off;
533 unsigned int nr_batching;
535 unsigned int dma_drain_size;
536 void *dma_drain_buffer;
537 unsigned int dma_pad_mask;
538 unsigned int dma_alignment;
540 struct blk_queue_tag *queue_tags;
541 struct list_head tag_busy_list;
543 unsigned int nr_sorted;
544 unsigned int in_flight[2];
547 * Number of active block driver functions for which blk_drain_queue()
548 * must wait. Must be incremented around functions that unlock the
549 * queue_lock internally, e.g. scsi_request_fn().
551 unsigned int request_fn_active;
553 unsigned int rq_timeout;
556 struct blk_stat_callback *poll_cb;
557 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
559 struct timer_list timeout;
560 struct work_struct timeout_work;
561 struct list_head timeout_list;
563 struct list_head icq_list;
564 #ifdef CONFIG_BLK_CGROUP
565 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
566 struct blkcg_gq *root_blkg;
567 struct list_head blkg_list;
570 struct queue_limits limits;
575 unsigned int sg_timeout;
576 unsigned int sg_reserved_size;
578 #ifdef CONFIG_BLK_DEV_IO_TRACE
579 struct blk_trace __rcu *blk_trace;
580 struct mutex blk_trace_mutex;
583 * for flush operations
585 struct blk_flush_queue *fq;
587 struct list_head requeue_list;
588 spinlock_t requeue_lock;
589 struct delayed_work requeue_work;
591 struct mutex sysfs_lock;
594 atomic_t mq_freeze_depth;
596 #if defined(CONFIG_BLK_DEV_BSG)
597 bsg_job_fn *bsg_job_fn;
598 struct bsg_class_device bsg_dev;
601 #ifdef CONFIG_BLK_DEV_THROTTLING
603 struct throtl_data *td;
605 struct rcu_head rcu_head;
606 wait_queue_head_t mq_freeze_wq;
607 struct percpu_ref q_usage_counter;
608 struct list_head all_q_node;
610 struct blk_mq_tag_set *tag_set;
611 struct list_head tag_set_list;
612 struct bio_set *bio_split;
614 #ifdef CONFIG_BLK_DEBUG_FS
615 struct dentry *debugfs_dir;
616 struct dentry *sched_debugfs_dir;
619 bool mq_sysfs_init_done;
624 struct work_struct release_work;
626 #define BLK_MAX_WRITE_HINTS 5
627 u64 write_hints[BLK_MAX_WRITE_HINTS];
630 #define QUEUE_FLAG_QUEUED 0 /* uses generic tag queueing */
631 #define QUEUE_FLAG_STOPPED 1 /* queue is stopped */
632 #define QUEUE_FLAG_DYING 2 /* queue being torn down */
633 #define QUEUE_FLAG_BYPASS 3 /* act as dumb FIFO queue */
634 #define QUEUE_FLAG_BIDI 4 /* queue supports bidi requests */
635 #define QUEUE_FLAG_NOMERGES 5 /* disable merge attempts */
636 #define QUEUE_FLAG_SAME_COMP 6 /* complete on same CPU-group */
637 #define QUEUE_FLAG_FAIL_IO 7 /* fake timeout */
638 #define QUEUE_FLAG_STACKABLE 8 /* supports request stacking */
639 #define QUEUE_FLAG_NONROT 9 /* non-rotational device (SSD) */
640 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
641 #define QUEUE_FLAG_IO_STAT 10 /* do IO stats */
642 #define QUEUE_FLAG_DISCARD 11 /* supports DISCARD */
643 #define QUEUE_FLAG_NOXMERGES 12 /* No extended merges */
644 #define QUEUE_FLAG_ADD_RANDOM 13 /* Contributes to random pool */
645 #define QUEUE_FLAG_SECERASE 14 /* supports secure erase */
646 #define QUEUE_FLAG_SAME_FORCE 15 /* force complete on same CPU */
647 #define QUEUE_FLAG_DEAD 16 /* queue tear-down finished */
648 #define QUEUE_FLAG_INIT_DONE 17 /* queue is initialized */
649 #define QUEUE_FLAG_NO_SG_MERGE 18 /* don't attempt to merge SG segments*/
650 #define QUEUE_FLAG_POLL 19 /* IO polling enabled if set */
651 #define QUEUE_FLAG_WC 20 /* Write back caching */
652 #define QUEUE_FLAG_FUA 21 /* device supports FUA writes */
653 #define QUEUE_FLAG_FLUSH_NQ 22 /* flush not queueuable */
654 #define QUEUE_FLAG_DAX 23 /* device supports DAX */
655 #define QUEUE_FLAG_STATS 24 /* track rq completion times */
656 #define QUEUE_FLAG_POLL_STATS 25 /* collecting stats for hybrid polling */
657 #define QUEUE_FLAG_REGISTERED 26 /* queue has been registered to a disk */
658 #define QUEUE_FLAG_SCSI_PASSTHROUGH 27 /* queue supports SCSI commands */
659 #define QUEUE_FLAG_QUIESCED 28 /* queue has been quiesced */
661 #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
662 (1 << QUEUE_FLAG_STACKABLE) | \
663 (1 << QUEUE_FLAG_SAME_COMP) | \
664 (1 << QUEUE_FLAG_ADD_RANDOM))
666 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
667 (1 << QUEUE_FLAG_STACKABLE) | \
668 (1 << QUEUE_FLAG_SAME_COMP) | \
669 (1 << QUEUE_FLAG_POLL))
672 * @q->queue_lock is set while a queue is being initialized. Since we know
673 * that no other threads access the queue object before @q->queue_lock has
674 * been set, it is safe to manipulate queue flags without holding the
675 * queue_lock if @q->queue_lock == NULL. See also blk_alloc_queue_node() and
676 * blk_init_allocated_queue().
678 static inline void queue_lockdep_assert_held(struct request_queue *q)
681 lockdep_assert_held(q->queue_lock);
684 static inline void queue_flag_set_unlocked(unsigned int flag,
685 struct request_queue *q)
687 __set_bit(flag, &q->queue_flags);
690 static inline int queue_flag_test_and_clear(unsigned int flag,
691 struct request_queue *q)
693 queue_lockdep_assert_held(q);
695 if (test_bit(flag, &q->queue_flags)) {
696 __clear_bit(flag, &q->queue_flags);
703 static inline int queue_flag_test_and_set(unsigned int flag,
704 struct request_queue *q)
706 queue_lockdep_assert_held(q);
708 if (!test_bit(flag, &q->queue_flags)) {
709 __set_bit(flag, &q->queue_flags);
716 static inline void queue_flag_set(unsigned int flag, struct request_queue *q)
718 queue_lockdep_assert_held(q);
719 __set_bit(flag, &q->queue_flags);
722 static inline void queue_flag_clear_unlocked(unsigned int flag,
723 struct request_queue *q)
725 __clear_bit(flag, &q->queue_flags);
728 static inline int queue_in_flight(struct request_queue *q)
730 return q->in_flight[0] + q->in_flight[1];
733 static inline void queue_flag_clear(unsigned int flag, struct request_queue *q)
735 queue_lockdep_assert_held(q);
736 __clear_bit(flag, &q->queue_flags);
739 #define blk_queue_tagged(q) test_bit(QUEUE_FLAG_QUEUED, &(q)->queue_flags)
740 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
741 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
742 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
743 #define blk_queue_bypass(q) test_bit(QUEUE_FLAG_BYPASS, &(q)->queue_flags)
744 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
745 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
746 #define blk_queue_noxmerges(q) \
747 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
748 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
749 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
750 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
751 #define blk_queue_stackable(q) \
752 test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
753 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
754 #define blk_queue_secure_erase(q) \
755 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
756 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
757 #define blk_queue_scsi_passthrough(q) \
758 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
760 #define blk_noretry_request(rq) \
761 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
762 REQ_FAILFAST_DRIVER))
763 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
765 static inline bool blk_account_rq(struct request *rq)
767 return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
770 #define blk_rq_cpu_valid(rq) ((rq)->cpu != -1)
771 #define blk_bidi_rq(rq) ((rq)->next_rq != NULL)
772 /* rq->queuelist of dequeued request must be list_empty() */
773 #define blk_queued_rq(rq) (!list_empty(&(rq)->queuelist))
775 #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
777 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
780 * Driver can handle struct request, if it either has an old style
781 * request_fn defined, or is blk-mq based.
783 static inline bool queue_is_rq_based(struct request_queue *q)
785 return q->request_fn || q->mq_ops;
788 static inline unsigned int blk_queue_cluster(struct request_queue *q)
790 return q->limits.cluster;
793 static inline enum blk_zoned_model
794 blk_queue_zoned_model(struct request_queue *q)
796 return q->limits.zoned;
799 static inline bool blk_queue_is_zoned(struct request_queue *q)
801 switch (blk_queue_zoned_model(q)) {
810 static inline unsigned int blk_queue_zone_sectors(struct request_queue *q)
812 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
815 static inline bool rq_is_sync(struct request *rq)
817 return op_is_sync(rq->cmd_flags);
820 static inline bool blk_rl_full(struct request_list *rl, bool sync)
822 unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
824 return rl->flags & flag;
827 static inline void blk_set_rl_full(struct request_list *rl, bool sync)
829 unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
834 static inline void blk_clear_rl_full(struct request_list *rl, bool sync)
836 unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
841 static inline bool rq_mergeable(struct request *rq)
843 if (blk_rq_is_passthrough(rq))
846 if (req_op(rq) == REQ_OP_FLUSH)
849 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
852 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
854 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
860 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
862 if (bio_page(a) == bio_page(b) &&
863 bio_offset(a) == bio_offset(b))
869 static inline unsigned int blk_queue_depth(struct request_queue *q)
872 return q->queue_depth;
874 return q->nr_requests;
878 * q->prep_rq_fn return values
881 BLKPREP_OK, /* serve it */
882 BLKPREP_KILL, /* fatal error, kill, return -EIO */
883 BLKPREP_DEFER, /* leave on queue */
884 BLKPREP_INVALID, /* invalid command, kill, return -EREMOTEIO */
887 extern unsigned long blk_max_low_pfn, blk_max_pfn;
890 * standard bounce addresses:
892 * BLK_BOUNCE_HIGH : bounce all highmem pages
893 * BLK_BOUNCE_ANY : don't bounce anything
894 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
897 #if BITS_PER_LONG == 32
898 #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
900 #define BLK_BOUNCE_HIGH -1ULL
902 #define BLK_BOUNCE_ANY (-1ULL)
903 #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
906 * default timeout for SG_IO if none specified
908 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
909 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
915 unsigned long offset;
920 struct req_iterator {
921 struct bvec_iter iter;
925 /* This should not be used directly - use rq_for_each_segment */
926 #define for_each_bio(_bio) \
927 for (; _bio; _bio = _bio->bi_next)
928 #define __rq_for_each_bio(_bio, rq) \
930 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
932 #define rq_for_each_segment(bvl, _rq, _iter) \
933 __rq_for_each_bio(_iter.bio, _rq) \
934 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
936 #define rq_iter_last(bvec, _iter) \
937 (_iter.bio->bi_next == NULL && \
938 bio_iter_last(bvec, _iter.iter))
940 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
941 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
943 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
944 extern void rq_flush_dcache_pages(struct request *rq);
946 static inline void rq_flush_dcache_pages(struct request *rq)
952 #define vfs_msg(sb, level, fmt, ...) \
953 __vfs_msg(sb, level, fmt, ##__VA_ARGS__)
955 #define vfs_msg(sb, level, fmt, ...) \
957 no_printk(fmt, ##__VA_ARGS__); \
958 __vfs_msg(sb, "", " "); \
962 extern int blk_register_queue(struct gendisk *disk);
963 extern void blk_unregister_queue(struct gendisk *disk);
964 extern blk_qc_t generic_make_request(struct bio *bio);
965 extern void blk_rq_init(struct request_queue *q, struct request *rq);
966 extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
967 extern void blk_put_request(struct request *);
968 extern void __blk_put_request(struct request_queue *, struct request *);
969 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
971 extern void blk_requeue_request(struct request_queue *, struct request *);
972 extern int blk_lld_busy(struct request_queue *q);
973 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
974 struct bio_set *bs, gfp_t gfp_mask,
975 int (*bio_ctr)(struct bio *, struct bio *, void *),
977 extern void blk_rq_unprep_clone(struct request *rq);
978 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
980 extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
981 extern void blk_delay_queue(struct request_queue *, unsigned long);
982 extern void blk_queue_split(struct request_queue *, struct bio **);
983 extern void blk_recount_segments(struct request_queue *, struct bio *);
984 extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
985 extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
986 unsigned int, void __user *);
987 extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
988 unsigned int, void __user *);
989 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
990 struct scsi_ioctl_command __user *);
992 extern int blk_queue_enter(struct request_queue *q, bool nowait);
993 extern void blk_queue_exit(struct request_queue *q);
994 extern void blk_start_queue(struct request_queue *q);
995 extern void blk_start_queue_async(struct request_queue *q);
996 extern void blk_stop_queue(struct request_queue *q);
997 extern void blk_sync_queue(struct request_queue *q);
998 extern void __blk_stop_queue(struct request_queue *q);
999 extern void __blk_run_queue(struct request_queue *q);
1000 extern void __blk_run_queue_uncond(struct request_queue *q);
1001 extern void blk_run_queue(struct request_queue *);
1002 extern void blk_run_queue_async(struct request_queue *q);
1003 extern int blk_rq_map_user(struct request_queue *, struct request *,
1004 struct rq_map_data *, void __user *, unsigned long,
1006 extern int blk_rq_unmap_user(struct bio *);
1007 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
1008 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
1009 struct rq_map_data *, const struct iov_iter *,
1011 extern void blk_execute_rq(struct request_queue *, struct gendisk *,
1012 struct request *, int);
1013 extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
1014 struct request *, int, rq_end_io_fn *);
1016 int blk_status_to_errno(blk_status_t status);
1017 blk_status_t errno_to_blk_status(int errno);
1019 bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie);
1021 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
1023 return bdev->bd_disk->queue; /* this is never NULL */
1027 * The basic unit of block I/O is a sector. It is used in a number of contexts
1028 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
1029 * bytes. Variables of type sector_t represent an offset or size that is a
1030 * multiple of 512 bytes. Hence these two constants.
1032 #ifndef SECTOR_SHIFT
1033 #define SECTOR_SHIFT 9
1036 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
1040 * blk_rq_pos() : the current sector
1041 * blk_rq_bytes() : bytes left in the entire request
1042 * blk_rq_cur_bytes() : bytes left in the current segment
1043 * blk_rq_err_bytes() : bytes left till the next error boundary
1044 * blk_rq_sectors() : sectors left in the entire request
1045 * blk_rq_cur_sectors() : sectors left in the current segment
1047 static inline sector_t blk_rq_pos(const struct request *rq)
1049 return rq->__sector;
1052 static inline unsigned int blk_rq_bytes(const struct request *rq)
1054 return rq->__data_len;
1057 static inline int blk_rq_cur_bytes(const struct request *rq)
1059 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
1062 extern unsigned int blk_rq_err_bytes(const struct request *rq);
1064 static inline unsigned int blk_rq_sectors(const struct request *rq)
1066 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1069 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1071 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1075 * Some commands like WRITE SAME have a payload or data transfer size which
1076 * is different from the size of the request. Any driver that supports such
1077 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1078 * calculate the data transfer size.
1080 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1082 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1083 return rq->special_vec.bv_len;
1084 return blk_rq_bytes(rq);
1087 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1090 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1091 return min(q->limits.max_discard_sectors,
1092 UINT_MAX >> SECTOR_SHIFT);
1094 if (unlikely(op == REQ_OP_WRITE_SAME))
1095 return q->limits.max_write_same_sectors;
1097 if (unlikely(op == REQ_OP_WRITE_ZEROES))
1098 return q->limits.max_write_zeroes_sectors;
1100 return q->limits.max_sectors;
1104 * Return maximum size of a request at given offset. Only valid for
1105 * file system requests.
1107 static inline unsigned int blk_max_size_offset(struct request_queue *q,
1110 if (!q->limits.chunk_sectors)
1111 return q->limits.max_sectors;
1113 return min(q->limits.max_sectors, (unsigned int)(q->limits.chunk_sectors -
1114 (offset & (q->limits.chunk_sectors - 1))));
1117 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1120 struct request_queue *q = rq->q;
1122 if (blk_rq_is_passthrough(rq))
1123 return q->limits.max_hw_sectors;
1125 if (!q->limits.chunk_sectors ||
1126 req_op(rq) == REQ_OP_DISCARD ||
1127 req_op(rq) == REQ_OP_SECURE_ERASE)
1128 return blk_queue_get_max_sectors(q, req_op(rq));
1130 return min(blk_max_size_offset(q, offset),
1131 blk_queue_get_max_sectors(q, req_op(rq)));
1134 static inline unsigned int blk_rq_count_bios(struct request *rq)
1136 unsigned int nr_bios = 0;
1139 __rq_for_each_bio(bio, rq)
1146 * Request issue related functions.
1148 extern struct request *blk_peek_request(struct request_queue *q);
1149 extern void blk_start_request(struct request *rq);
1150 extern struct request *blk_fetch_request(struct request_queue *q);
1153 * Request completion related functions.
1155 * blk_update_request() completes given number of bytes and updates
1156 * the request without completing it.
1158 * blk_end_request() and friends. __blk_end_request() must be called
1159 * with the request queue spinlock acquired.
1161 * Several drivers define their own end_request and call
1162 * blk_end_request() for parts of the original function.
1163 * This prevents code duplication in drivers.
1165 extern bool blk_update_request(struct request *rq, blk_status_t error,
1166 unsigned int nr_bytes);
1167 extern void blk_finish_request(struct request *rq, blk_status_t error);
1168 extern bool blk_end_request(struct request *rq, blk_status_t error,
1169 unsigned int nr_bytes);
1170 extern void blk_end_request_all(struct request *rq, blk_status_t error);
1171 extern bool __blk_end_request(struct request *rq, blk_status_t error,
1172 unsigned int nr_bytes);
1173 extern void __blk_end_request_all(struct request *rq, blk_status_t error);
1174 extern bool __blk_end_request_cur(struct request *rq, blk_status_t error);
1176 extern void blk_complete_request(struct request *);
1177 extern void __blk_complete_request(struct request *);
1178 extern void blk_abort_request(struct request *);
1179 extern void blk_unprep_request(struct request *);
1182 * Access functions for manipulating queue properties
1184 extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn,
1185 spinlock_t *lock, int node_id);
1186 extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *);
1187 extern int blk_init_allocated_queue(struct request_queue *);
1188 extern void blk_cleanup_queue(struct request_queue *);
1189 extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
1190 extern void blk_queue_bounce_limit(struct request_queue *, u64);
1191 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1192 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1193 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1194 extern void blk_queue_max_discard_segments(struct request_queue *,
1196 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1197 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1198 unsigned int max_discard_sectors);
1199 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1200 unsigned int max_write_same_sectors);
1201 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1202 unsigned int max_write_same_sectors);
1203 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
1204 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1205 extern void blk_queue_alignment_offset(struct request_queue *q,
1206 unsigned int alignment);
1207 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1208 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1209 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1210 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1211 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1212 extern void blk_set_default_limits(struct queue_limits *lim);
1213 extern void blk_set_stacking_limits(struct queue_limits *lim);
1214 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1216 extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
1218 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1220 extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
1221 extern void blk_queue_dma_pad(struct request_queue *, unsigned int);
1222 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1223 extern int blk_queue_dma_drain(struct request_queue *q,
1224 dma_drain_needed_fn *dma_drain_needed,
1225 void *buf, unsigned int size);
1226 extern void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn);
1227 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1228 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1229 extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn);
1230 extern void blk_queue_unprep_rq(struct request_queue *, unprep_rq_fn *ufn);
1231 extern void blk_queue_dma_alignment(struct request_queue *, int);
1232 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1233 extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *);
1234 extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *);
1235 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1236 extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable);
1237 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1240 * Number of physical segments as sent to the device.
1242 * Normally this is the number of discontiguous data segments sent by the
1243 * submitter. But for data-less command like discard we might have no
1244 * actual data segments submitted, but the driver might have to add it's
1245 * own special payload. In that case we still return 1 here so that this
1246 * special payload will be mapped.
1248 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1250 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1252 return rq->nr_phys_segments;
1256 * Number of discard segments (or ranges) the driver needs to fill in.
1257 * Each discard bio merged into a request is counted as one segment.
1259 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1261 return max_t(unsigned short, rq->nr_phys_segments, 1);
1264 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
1265 extern void blk_dump_rq_flags(struct request *, char *);
1266 extern long nr_blockdev_pages(void);
1268 bool __must_check blk_get_queue(struct request_queue *);
1269 struct request_queue *blk_alloc_queue(gfp_t);
1270 struct request_queue *blk_alloc_queue_node(gfp_t, int);
1271 extern void blk_put_queue(struct request_queue *);
1272 extern void blk_set_queue_dying(struct request_queue *);
1275 * block layer runtime pm functions
1278 extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev);
1279 extern int blk_pre_runtime_suspend(struct request_queue *q);
1280 extern void blk_post_runtime_suspend(struct request_queue *q, int err);
1281 extern void blk_pre_runtime_resume(struct request_queue *q);
1282 extern void blk_post_runtime_resume(struct request_queue *q, int err);
1283 extern void blk_set_runtime_active(struct request_queue *q);
1285 static inline void blk_pm_runtime_init(struct request_queue *q,
1286 struct device *dev) {}
1287 static inline int blk_pre_runtime_suspend(struct request_queue *q)
1291 static inline void blk_post_runtime_suspend(struct request_queue *q, int err) {}
1292 static inline void blk_pre_runtime_resume(struct request_queue *q) {}
1293 static inline void blk_post_runtime_resume(struct request_queue *q, int err) {}
1294 static inline void blk_set_runtime_active(struct request_queue *q) {}
1298 * blk_plug permits building a queue of related requests by holding the I/O
1299 * fragments for a short period. This allows merging of sequential requests
1300 * into single larger request. As the requests are moved from a per-task list to
1301 * the device's request_queue in a batch, this results in improved scalability
1302 * as the lock contention for request_queue lock is reduced.
1304 * It is ok not to disable preemption when adding the request to the plug list
1305 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1306 * the plug list when the task sleeps by itself. For details, please see
1307 * schedule() where blk_schedule_flush_plug() is called.
1310 struct list_head list; /* requests */
1311 struct list_head mq_list; /* blk-mq requests */
1312 struct list_head cb_list; /* md requires an unplug callback */
1314 #define BLK_MAX_REQUEST_COUNT 16
1315 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1318 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1319 struct blk_plug_cb {
1320 struct list_head list;
1321 blk_plug_cb_fn callback;
1324 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1325 void *data, int size);
1326 extern void blk_start_plug(struct blk_plug *);
1327 extern void blk_finish_plug(struct blk_plug *);
1328 extern void blk_flush_plug_list(struct blk_plug *, bool);
1330 static inline void blk_flush_plug(struct task_struct *tsk)
1332 struct blk_plug *plug = tsk->plug;
1335 blk_flush_plug_list(plug, false);
1338 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1340 struct blk_plug *plug = tsk->plug;
1343 blk_flush_plug_list(plug, true);
1346 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1348 struct blk_plug *plug = tsk->plug;
1351 (!list_empty(&plug->list) ||
1352 !list_empty(&plug->mq_list) ||
1353 !list_empty(&plug->cb_list));
1359 extern int blk_queue_start_tag(struct request_queue *, struct request *);
1360 extern struct request *blk_queue_find_tag(struct request_queue *, int);
1361 extern void blk_queue_end_tag(struct request_queue *, struct request *);
1362 extern int blk_queue_init_tags(struct request_queue *, int, struct blk_queue_tag *, int);
1363 extern void blk_queue_free_tags(struct request_queue *);
1364 extern int blk_queue_resize_tags(struct request_queue *, int);
1365 extern void blk_queue_invalidate_tags(struct request_queue *);
1366 extern struct blk_queue_tag *blk_init_tags(int, int);
1367 extern void blk_free_tags(struct blk_queue_tag *);
1369 static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
1372 if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
1374 return bqt->tag_index[tag];
1377 extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
1378 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1379 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1381 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1383 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1384 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1385 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1386 sector_t nr_sects, gfp_t gfp_mask, int flags,
1389 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1390 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1392 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1393 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1395 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1396 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1398 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1399 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1401 return blkdev_issue_discard(sb->s_bdev,
1402 block << (sb->s_blocksize_bits -
1404 nr_blocks << (sb->s_blocksize_bits -
1408 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1409 sector_t nr_blocks, gfp_t gfp_mask)
1411 return blkdev_issue_zeroout(sb->s_bdev,
1412 block << (sb->s_blocksize_bits -
1414 nr_blocks << (sb->s_blocksize_bits -
1419 extern int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm);
1421 enum blk_default_limits {
1422 BLK_MAX_SEGMENTS = 128,
1423 BLK_SAFE_MAX_SECTORS = 255,
1424 BLK_DEF_MAX_SECTORS = 2560,
1425 BLK_MAX_SEGMENT_SIZE = 65536,
1426 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1429 #define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist)
1431 static inline unsigned long queue_segment_boundary(struct request_queue *q)
1433 return q->limits.seg_boundary_mask;
1436 static inline unsigned long queue_virt_boundary(struct request_queue *q)
1438 return q->limits.virt_boundary_mask;
1441 static inline unsigned int queue_max_sectors(struct request_queue *q)
1443 return q->limits.max_sectors;
1446 static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
1448 return q->limits.max_hw_sectors;
1451 static inline unsigned short queue_max_segments(struct request_queue *q)
1453 return q->limits.max_segments;
1456 static inline unsigned short queue_max_discard_segments(struct request_queue *q)
1458 return q->limits.max_discard_segments;
1461 static inline unsigned int queue_max_segment_size(struct request_queue *q)
1463 return q->limits.max_segment_size;
1466 static inline unsigned queue_logical_block_size(struct request_queue *q)
1470 if (q && q->limits.logical_block_size)
1471 retval = q->limits.logical_block_size;
1476 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1478 return queue_logical_block_size(bdev_get_queue(bdev));
1481 static inline unsigned int queue_physical_block_size(struct request_queue *q)
1483 return q->limits.physical_block_size;
1486 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1488 return queue_physical_block_size(bdev_get_queue(bdev));
1491 static inline unsigned int queue_io_min(struct request_queue *q)
1493 return q->limits.io_min;
1496 static inline int bdev_io_min(struct block_device *bdev)
1498 return queue_io_min(bdev_get_queue(bdev));
1501 static inline unsigned int queue_io_opt(struct request_queue *q)
1503 return q->limits.io_opt;
1506 static inline int bdev_io_opt(struct block_device *bdev)
1508 return queue_io_opt(bdev_get_queue(bdev));
1511 static inline int queue_alignment_offset(struct request_queue *q)
1513 if (q->limits.misaligned)
1516 return q->limits.alignment_offset;
1519 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1521 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1522 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1525 return (granularity + lim->alignment_offset - alignment) % granularity;
1528 static inline int bdev_alignment_offset(struct block_device *bdev)
1530 struct request_queue *q = bdev_get_queue(bdev);
1532 if (q->limits.misaligned)
1535 if (bdev != bdev->bd_contains)
1536 return bdev->bd_part->alignment_offset;
1538 return q->limits.alignment_offset;
1541 static inline int queue_discard_alignment(struct request_queue *q)
1543 if (q->limits.discard_misaligned)
1546 return q->limits.discard_alignment;
1549 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1551 unsigned int alignment, granularity, offset;
1553 if (!lim->max_discard_sectors)
1556 /* Why are these in bytes, not sectors? */
1557 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1558 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1562 /* Offset of the partition start in 'granularity' sectors */
1563 offset = sector_div(sector, granularity);
1565 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1566 offset = (granularity + alignment - offset) % granularity;
1568 /* Turn it back into bytes, gaah */
1569 return offset << SECTOR_SHIFT;
1572 static inline int bdev_discard_alignment(struct block_device *bdev)
1574 struct request_queue *q = bdev_get_queue(bdev);
1576 if (bdev != bdev->bd_contains)
1577 return bdev->bd_part->discard_alignment;
1579 return q->limits.discard_alignment;
1582 static inline unsigned int bdev_write_same(struct block_device *bdev)
1584 struct request_queue *q = bdev_get_queue(bdev);
1587 return q->limits.max_write_same_sectors;
1592 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1594 struct request_queue *q = bdev_get_queue(bdev);
1597 return q->limits.max_write_zeroes_sectors;
1602 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1604 struct request_queue *q = bdev_get_queue(bdev);
1607 return blk_queue_zoned_model(q);
1609 return BLK_ZONED_NONE;
1612 static inline bool bdev_is_zoned(struct block_device *bdev)
1614 struct request_queue *q = bdev_get_queue(bdev);
1617 return blk_queue_is_zoned(q);
1622 static inline unsigned int bdev_zone_sectors(struct block_device *bdev)
1624 struct request_queue *q = bdev_get_queue(bdev);
1627 return blk_queue_zone_sectors(q);
1632 static inline int queue_dma_alignment(struct request_queue *q)
1634 return q ? q->dma_alignment : 511;
1637 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1640 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1641 return !(addr & alignment) && !(len & alignment);
1644 /* assumes size > 256 */
1645 static inline unsigned int blksize_bits(unsigned int size)
1647 unsigned int bits = 8;
1651 } while (size > 256);
1655 static inline unsigned int block_size(struct block_device *bdev)
1657 return bdev->bd_block_size;
1660 static inline bool queue_flush_queueable(struct request_queue *q)
1662 return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
1665 typedef struct {struct page *v;} Sector;
1667 unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
1669 static inline void put_dev_sector(Sector p)
1674 static inline bool __bvec_gap_to_prev(struct request_queue *q,
1675 struct bio_vec *bprv, unsigned int offset)
1678 ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q));
1682 * Check if adding a bio_vec after bprv with offset would create a gap in
1683 * the SG list. Most drivers don't care about this, but some do.
1685 static inline bool bvec_gap_to_prev(struct request_queue *q,
1686 struct bio_vec *bprv, unsigned int offset)
1688 if (!queue_virt_boundary(q))
1690 return __bvec_gap_to_prev(q, bprv, offset);
1694 * Check if the two bvecs from two bios can be merged to one segment.
1695 * If yes, no need to check gap between the two bios since the 1st bio
1696 * and the 1st bvec in the 2nd bio can be handled in one segment.
1698 static inline bool bios_segs_mergeable(struct request_queue *q,
1699 struct bio *prev, struct bio_vec *prev_last_bv,
1700 struct bio_vec *next_first_bv)
1702 if (!BIOVEC_PHYS_MERGEABLE(prev_last_bv, next_first_bv))
1704 if (!BIOVEC_SEG_BOUNDARY(q, prev_last_bv, next_first_bv))
1706 if (prev->bi_seg_back_size + next_first_bv->bv_len >
1707 queue_max_segment_size(q))
1712 static inline bool bio_will_gap(struct request_queue *q,
1713 struct request *prev_rq,
1717 if (bio_has_data(prev) && queue_virt_boundary(q)) {
1718 struct bio_vec pb, nb;
1721 * don't merge if the 1st bio starts with non-zero
1722 * offset, otherwise it is quite difficult to respect
1723 * sg gap limit. We work hard to merge a huge number of small
1724 * single bios in case of mkfs.
1727 bio_get_first_bvec(prev_rq->bio, &pb);
1729 bio_get_first_bvec(prev, &pb);
1734 * We don't need to worry about the situation that the
1735 * merged segment ends in unaligned virt boundary:
1737 * - if 'pb' ends aligned, the merged segment ends aligned
1738 * - if 'pb' ends unaligned, the next bio must include
1739 * one single bvec of 'nb', otherwise the 'nb' can't
1742 bio_get_last_bvec(prev, &pb);
1743 bio_get_first_bvec(next, &nb);
1745 if (!bios_segs_mergeable(q, prev, &pb, &nb))
1746 return __bvec_gap_to_prev(q, &pb, nb.bv_offset);
1752 static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
1754 return bio_will_gap(req->q, req, req->biotail, bio);
1757 static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
1759 return bio_will_gap(req->q, NULL, bio, req->bio);
1762 int kblockd_schedule_work(struct work_struct *work);
1763 int kblockd_schedule_work_on(int cpu, struct work_struct *work);
1764 int kblockd_schedule_delayed_work(struct delayed_work *dwork, unsigned long delay);
1765 int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1766 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1768 #ifdef CONFIG_BLK_CGROUP
1770 * This should not be using sched_clock(). A real patch is in progress
1771 * to fix this up, until that is in place we need to disable preemption
1772 * around sched_clock() in this function and set_io_start_time_ns().
1774 static inline void set_start_time_ns(struct request *req)
1777 req->start_time_ns = sched_clock();
1781 static inline void set_io_start_time_ns(struct request *req)
1784 req->io_start_time_ns = sched_clock();
1788 static inline uint64_t rq_start_time_ns(struct request *req)
1790 return req->start_time_ns;
1793 static inline uint64_t rq_io_start_time_ns(struct request *req)
1795 return req->io_start_time_ns;
1798 static inline void set_start_time_ns(struct request *req) {}
1799 static inline void set_io_start_time_ns(struct request *req) {}
1800 static inline uint64_t rq_start_time_ns(struct request *req)
1804 static inline uint64_t rq_io_start_time_ns(struct request *req)
1810 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1811 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1812 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1813 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1815 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1817 enum blk_integrity_flags {
1818 BLK_INTEGRITY_VERIFY = 1 << 0,
1819 BLK_INTEGRITY_GENERATE = 1 << 1,
1820 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1821 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1824 struct blk_integrity_iter {
1828 unsigned int data_size;
1829 unsigned short interval;
1830 const char *disk_name;
1833 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1835 struct blk_integrity_profile {
1836 integrity_processing_fn *generate_fn;
1837 integrity_processing_fn *verify_fn;
1841 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1842 extern void blk_integrity_unregister(struct gendisk *);
1843 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1844 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1845 struct scatterlist *);
1846 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1847 extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
1849 extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
1852 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1854 struct blk_integrity *bi = &disk->queue->integrity;
1863 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1865 return blk_get_integrity(bdev->bd_disk);
1868 static inline bool blk_integrity_rq(struct request *rq)
1870 return rq->cmd_flags & REQ_INTEGRITY;
1873 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1876 q->limits.max_integrity_segments = segs;
1879 static inline unsigned short
1880 queue_max_integrity_segments(struct request_queue *q)
1882 return q->limits.max_integrity_segments;
1885 static inline bool integrity_req_gap_back_merge(struct request *req,
1888 struct bio_integrity_payload *bip = bio_integrity(req->bio);
1889 struct bio_integrity_payload *bip_next = bio_integrity(next);
1891 return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
1892 bip_next->bip_vec[0].bv_offset);
1895 static inline bool integrity_req_gap_front_merge(struct request *req,
1898 struct bio_integrity_payload *bip = bio_integrity(bio);
1899 struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
1901 return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
1902 bip_next->bip_vec[0].bv_offset);
1905 #else /* CONFIG_BLK_DEV_INTEGRITY */
1908 struct block_device;
1910 struct blk_integrity;
1912 static inline int blk_integrity_rq(struct request *rq)
1916 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1921 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1923 struct scatterlist *s)
1927 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1931 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1935 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1939 static inline void blk_integrity_register(struct gendisk *d,
1940 struct blk_integrity *b)
1943 static inline void blk_integrity_unregister(struct gendisk *d)
1946 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1950 static inline unsigned short queue_max_integrity_segments(struct request_queue *q)
1954 static inline bool blk_integrity_merge_rq(struct request_queue *rq,
1960 static inline bool blk_integrity_merge_bio(struct request_queue *rq,
1967 static inline bool integrity_req_gap_back_merge(struct request *req,
1972 static inline bool integrity_req_gap_front_merge(struct request *req,
1978 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1980 struct block_device_operations {
1981 int (*open) (struct block_device *, fmode_t);
1982 void (*release) (struct gendisk *, fmode_t);
1983 int (*rw_page)(struct block_device *, sector_t, struct page *, bool);
1984 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1985 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1986 unsigned int (*check_events) (struct gendisk *disk,
1987 unsigned int clearing);
1988 /* ->media_changed() is DEPRECATED, use ->check_events() instead */
1989 int (*media_changed) (struct gendisk *);
1990 void (*unlock_native_capacity) (struct gendisk *);
1991 int (*revalidate_disk) (struct gendisk *);
1992 int (*getgeo)(struct block_device *, struct hd_geometry *);
1993 /* this callback is with swap_lock and sometimes page table lock held */
1994 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1995 struct module *owner;
1996 const struct pr_ops *pr_ops;
1999 extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
2001 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
2002 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
2003 struct writeback_control *);
2004 #else /* CONFIG_BLOCK */
2006 struct block_device;
2009 * stubs for when the block layer is configured out
2011 #define buffer_heads_over_limit 0
2013 static inline long nr_blockdev_pages(void)
2021 static inline void blk_start_plug(struct blk_plug *plug)
2025 static inline void blk_finish_plug(struct blk_plug *plug)
2029 static inline void blk_flush_plug(struct task_struct *task)
2033 static inline void blk_schedule_flush_plug(struct task_struct *task)
2038 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
2043 static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
2044 sector_t *error_sector)
2049 #endif /* CONFIG_BLOCK */