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 typedef void (rq_end_io_fn
)(struct request
*, blk_status_t
);
61 #define BLK_RL_SYNCFULL (1U << 0)
62 #define BLK_RL_ASYNCFULL (1U << 1)
65 struct request_queue
*q
; /* the queue this rl belongs to */
66 #ifdef CONFIG_BLK_CGROUP
67 struct blkcg_gq
*blkg
; /* blkg this request pool belongs to */
70 * count[], starved[], and wait[] are indexed by
71 * BLK_RW_SYNC/BLK_RW_ASYNC
76 wait_queue_head_t wait
[2];
82 typedef __u32 __bitwise req_flags_t
;
84 /* elevator knows about this request */
85 #define RQF_SORTED ((__force req_flags_t)(1 << 0))
86 /* drive already may have started this one */
87 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
88 /* uses tagged queueing */
89 #define RQF_QUEUED ((__force req_flags_t)(1 << 2))
90 /* may not be passed by ioscheduler */
91 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
92 /* request for flush sequence */
93 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
94 /* merge of different types, fail separately */
95 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
96 /* track inflight for MQ */
97 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
98 /* don't call prep for this one */
99 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
100 /* set for "ide_preempt" requests and also for requests for which the SCSI
101 "quiesce" state must be ignored. */
102 #define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
103 /* contains copies of user pages */
104 #define RQF_COPY_USER ((__force req_flags_t)(1 << 9))
105 /* vaguely specified driver internal error. Ignored by the block layer */
106 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
107 /* don't warn about errors */
108 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
109 /* elevator private data attached */
110 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
111 /* account I/O stat */
112 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
113 /* request came from our alloc pool */
114 #define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
115 /* runtime pm request */
116 #define RQF_PM ((__force req_flags_t)(1 << 15))
117 /* on IO scheduler merge hash */
118 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
119 /* IO stats tracking on */
120 #define RQF_STATS ((__force req_flags_t)(1 << 17))
121 /* Look at ->special_vec for the actual data payload instead of the
123 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
125 /* flags that prevent us from merging requests: */
126 #define RQF_NOMERGE_FLAGS \
127 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
130 * Try to put the fields that are referenced together in the same cacheline.
132 * If you modify this structure, make sure to update blk_rq_init() and
133 * especially blk_mq_rq_ctx_init() to take care of the added fields.
136 struct list_head queuelist
;
138 struct __call_single_data csd
;
142 struct request_queue
*q
;
143 struct blk_mq_ctx
*mq_ctx
;
146 unsigned int cmd_flags
; /* op and common flags */
147 req_flags_t rq_flags
;
151 unsigned long atomic_flags
;
153 /* the following two fields are internal, NEVER access directly */
154 unsigned int __data_len
; /* total data len */
156 sector_t __sector
; /* sector cursor */
157 #ifdef CONFIG_CRYPTO_DISKCIPHER_DUN
158 u64 __dun
; /* dun for UFS */
164 * The hash is used inside the scheduler, and killed once the
165 * request reaches the dispatch list. The ipi_list is only used
166 * to queue the request for softirq completion, which is long
167 * after the request has been unhashed (and even removed from
168 * the dispatch list).
171 struct hlist_node hash
; /* merge hash */
172 struct list_head ipi_list
;
176 * The rb_node is only used inside the io scheduler, requests
177 * are pruned when moved to the dispatch queue. So let the
178 * completion_data share space with the rb_node.
181 struct rb_node rb_node
; /* sort/lookup */
182 struct bio_vec special_vec
;
183 void *completion_data
;
184 int error_count
; /* for legacy drivers, don't use */
188 * Three pointers are available for the IO schedulers, if they need
189 * more they have to dynamically allocate it. Flush requests are
190 * never put on the IO scheduler. So let the flush fields share
191 * space with the elevator data.
201 struct list_head list
;
202 rq_end_io_fn
*saved_end_io
;
206 struct gendisk
*rq_disk
;
207 struct hd_struct
*part
;
208 unsigned long start_time
;
209 struct blk_issue_stat issue_stat
;
210 #ifdef CONFIG_BLK_CGROUP
211 struct request_list
*rl
; /* rl this rq is alloced from */
212 unsigned long long start_time_ns
;
213 unsigned long long io_start_time_ns
; /* when passed to hardware */
215 /* Number of scatter-gather DMA addr+len pairs after
216 * physical address coalescing is performed.
218 unsigned short nr_phys_segments
;
219 #if defined(CONFIG_BLK_DEV_INTEGRITY)
220 unsigned short nr_integrity_segments
;
223 unsigned short ioprio
;
225 unsigned int timeout
;
227 void *special
; /* opaque pointer available for LLD use */
229 unsigned int extra_len
; /* length of alignment and padding */
231 unsigned short write_hint
;
233 unsigned long deadline
;
234 struct list_head timeout_list
;
237 * completion callback.
239 rq_end_io_fn
*end_io
;
243 struct request
*next_rq
;
246 static inline bool blk_op_is_scsi(unsigned int op
)
248 return op
== REQ_OP_SCSI_IN
|| op
== REQ_OP_SCSI_OUT
;
251 static inline bool blk_op_is_private(unsigned int op
)
253 return op
== REQ_OP_DRV_IN
|| op
== REQ_OP_DRV_OUT
;
256 static inline bool blk_rq_is_scsi(struct request
*rq
)
258 return blk_op_is_scsi(req_op(rq
));
261 static inline bool blk_rq_is_private(struct request
*rq
)
263 return blk_op_is_private(req_op(rq
));
266 static inline bool blk_rq_is_passthrough(struct request
*rq
)
268 return blk_rq_is_scsi(rq
) || blk_rq_is_private(rq
);
271 static inline bool bio_is_passthrough(struct bio
*bio
)
273 unsigned op
= bio_op(bio
);
275 return blk_op_is_scsi(op
) || blk_op_is_private(op
);
278 static inline unsigned short req_get_ioprio(struct request
*req
)
283 #include <linux/elevator.h>
285 struct blk_queue_ctx
;
287 typedef void (request_fn_proc
) (struct request_queue
*q
);
288 typedef blk_qc_t (make_request_fn
) (struct request_queue
*q
, struct bio
*bio
);
289 typedef int (prep_rq_fn
) (struct request_queue
*, struct request
*);
290 typedef void (unprep_rq_fn
) (struct request_queue
*, struct request
*);
293 typedef void (softirq_done_fn
)(struct request
*);
294 typedef int (dma_drain_needed_fn
)(struct request
*);
295 typedef int (lld_busy_fn
) (struct request_queue
*q
);
296 typedef int (bsg_job_fn
) (struct bsg_job
*);
297 typedef int (init_rq_fn
)(struct request_queue
*, struct request
*, gfp_t
);
298 typedef void (exit_rq_fn
)(struct request_queue
*, struct request
*);
300 enum blk_eh_timer_return
{
306 typedef enum blk_eh_timer_return (rq_timed_out_fn
)(struct request
*);
308 enum blk_queue_state
{
313 struct blk_queue_tag
{
314 struct request
**tag_index
; /* map of busy tags */
315 unsigned long *tag_map
; /* bit map of free/busy tags */
316 int max_depth
; /* what we will send to device */
317 int real_max_depth
; /* what the array can hold */
318 atomic_t refcnt
; /* map can be shared */
319 int alloc_policy
; /* tag allocation policy */
320 int next_tag
; /* next tag */
322 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
323 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
325 #define BLK_SCSI_MAX_CMDS (256)
326 #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
329 * Zoned block device models (zoned limit).
331 enum blk_zoned_model
{
332 BLK_ZONED_NONE
, /* Regular block device */
333 BLK_ZONED_HA
, /* Host-aware zoned block device */
334 BLK_ZONED_HM
, /* Host-managed zoned block device */
337 struct queue_limits
{
338 unsigned long bounce_pfn
;
339 unsigned long seg_boundary_mask
;
340 unsigned long virt_boundary_mask
;
342 unsigned int max_hw_sectors
;
343 unsigned int max_dev_sectors
;
344 unsigned int chunk_sectors
;
345 unsigned int max_sectors
;
346 unsigned int max_segment_size
;
347 unsigned int physical_block_size
;
348 unsigned int alignment_offset
;
351 unsigned int max_discard_sectors
;
352 unsigned int max_hw_discard_sectors
;
353 unsigned int max_write_same_sectors
;
354 unsigned int max_write_zeroes_sectors
;
355 unsigned int discard_granularity
;
356 unsigned int discard_alignment
;
358 unsigned short logical_block_size
;
359 unsigned short max_segments
;
360 unsigned short max_integrity_segments
;
361 unsigned short max_discard_segments
;
363 unsigned char misaligned
;
364 unsigned char discard_misaligned
;
365 unsigned char cluster
;
366 unsigned char raid_partial_stripes_expensive
;
367 enum blk_zoned_model zoned
;
370 #ifdef CONFIG_BLK_DEV_ZONED
372 struct blk_zone_report_hdr
{
373 unsigned int nr_zones
;
377 extern int blkdev_report_zones(struct block_device
*bdev
,
378 sector_t sector
, struct blk_zone
*zones
,
379 unsigned int *nr_zones
, gfp_t gfp_mask
);
380 extern int blkdev_reset_zones(struct block_device
*bdev
, sector_t sectors
,
381 sector_t nr_sectors
, gfp_t gfp_mask
);
383 extern int blkdev_report_zones_ioctl(struct block_device
*bdev
, fmode_t mode
,
384 unsigned int cmd
, unsigned long arg
);
385 extern int blkdev_reset_zones_ioctl(struct block_device
*bdev
, fmode_t mode
,
386 unsigned int cmd
, unsigned long arg
);
388 #else /* CONFIG_BLK_DEV_ZONED */
390 static inline int blkdev_report_zones_ioctl(struct block_device
*bdev
,
391 fmode_t mode
, unsigned int cmd
,
397 static inline int blkdev_reset_zones_ioctl(struct block_device
*bdev
,
398 fmode_t mode
, unsigned int cmd
,
404 #endif /* CONFIG_BLK_DEV_ZONED */
406 struct request_queue
{
408 * Together with queue_head for cacheline sharing
410 struct list_head queue_head
;
411 struct request
*last_merge
;
412 struct elevator_queue
*elevator
;
413 int nr_rqs
[2]; /* # allocated [a]sync rqs */
414 int nr_rqs_elvpriv
; /* # allocated rqs w/ elvpriv */
416 atomic_t shared_hctx_restart
;
418 struct blk_queue_stats
*stats
;
422 * If blkcg is not used, @q->root_rl serves all requests. If blkcg
423 * is used, root blkg allocates from @q->root_rl and all other
424 * blkgs from their own blkg->rl. Which one to use should be
425 * determined using bio_request_list().
427 struct request_list root_rl
;
429 request_fn_proc
*request_fn
;
430 make_request_fn
*make_request_fn
;
431 prep_rq_fn
*prep_rq_fn
;
432 unprep_rq_fn
*unprep_rq_fn
;
433 softirq_done_fn
*softirq_done_fn
;
434 rq_timed_out_fn
*rq_timed_out_fn
;
435 dma_drain_needed_fn
*dma_drain_needed
;
436 lld_busy_fn
*lld_busy_fn
;
437 /* Called just after a request is allocated */
438 init_rq_fn
*init_rq_fn
;
439 /* Called just before a request is freed */
440 exit_rq_fn
*exit_rq_fn
;
441 /* Called from inside blk_get_request() */
442 void (*initialize_rq_fn
)(struct request
*rq
);
444 const struct blk_mq_ops
*mq_ops
;
446 unsigned int *mq_map
;
449 struct blk_mq_ctx __percpu
*queue_ctx
;
450 unsigned int nr_queues
;
452 unsigned int queue_depth
;
454 /* hw dispatch queues */
455 struct blk_mq_hw_ctx
**queue_hw_ctx
;
456 unsigned int nr_hw_queues
;
459 * Dispatch queue sorting
462 struct request
*boundary_rq
;
465 * Delayed queue handling
467 struct delayed_work delay_work
;
469 struct backing_dev_info
*backing_dev_info
;
472 * The queue owner gets to use this for whatever they like.
473 * ll_rw_blk doesn't touch it.
478 * various queue flags, see QUEUE_* below
480 unsigned long queue_flags
;
483 * ida allocated id for this queue. Used to index queues from
489 * queue needs bounce pages for pages above this limit
494 * protects queue structures from reentrancy. ->__queue_lock should
495 * _never_ be used directly, it is queue private. always use
498 spinlock_t __queue_lock
;
499 spinlock_t
*queue_lock
;
509 struct kobject mq_kobj
;
511 #ifdef CONFIG_BLK_DEV_INTEGRITY
512 struct blk_integrity integrity
;
513 #endif /* CONFIG_BLK_DEV_INTEGRITY */
518 unsigned int nr_pending
;
524 unsigned long nr_requests
; /* Max # of requests */
525 unsigned int nr_congestion_on
;
526 unsigned int nr_congestion_off
;
527 unsigned int nr_batching
;
529 unsigned int dma_drain_size
;
530 void *dma_drain_buffer
;
531 unsigned int dma_pad_mask
;
532 unsigned int dma_alignment
;
534 struct blk_queue_tag
*queue_tags
;
535 struct list_head tag_busy_list
;
537 unsigned int nr_sorted
;
538 unsigned int in_flight
[2];
541 * Number of active block driver functions for which blk_drain_queue()
542 * must wait. Must be incremented around functions that unlock the
543 * queue_lock internally, e.g. scsi_request_fn().
545 unsigned int request_fn_active
;
547 unsigned int rq_timeout
;
550 struct blk_stat_callback
*poll_cb
;
551 struct blk_rq_stat poll_stat
[BLK_MQ_POLL_STATS_BKTS
];
553 struct timer_list timeout
;
554 struct work_struct timeout_work
;
555 struct list_head timeout_list
;
557 struct list_head icq_list
;
558 #ifdef CONFIG_BLK_CGROUP
559 DECLARE_BITMAP (blkcg_pols
, BLKCG_MAX_POLS
);
560 struct blkcg_gq
*root_blkg
;
561 struct list_head blkg_list
;
564 struct queue_limits limits
;
569 unsigned int sg_timeout
;
570 unsigned int sg_reserved_size
;
572 #ifdef CONFIG_BLK_DEV_IO_TRACE
573 struct blk_trace
*blk_trace
;
574 struct mutex blk_trace_mutex
;
577 * for flush operations
579 struct blk_flush_queue
*fq
;
581 struct list_head requeue_list
;
582 spinlock_t requeue_lock
;
583 struct delayed_work requeue_work
;
585 struct mutex sysfs_lock
;
588 atomic_t mq_freeze_depth
;
590 #if defined(CONFIG_BLK_DEV_BSG)
591 bsg_job_fn
*bsg_job_fn
;
592 struct bsg_class_device bsg_dev
;
595 #ifdef CONFIG_BLK_DEV_THROTTLING
597 struct throtl_data
*td
;
599 struct rcu_head rcu_head
;
600 wait_queue_head_t mq_freeze_wq
;
601 struct percpu_ref q_usage_counter
;
602 struct list_head all_q_node
;
604 struct blk_mq_tag_set
*tag_set
;
605 struct list_head tag_set_list
;
606 struct bio_set
*bio_split
;
608 #ifdef CONFIG_BLK_DEBUG_FS
609 struct dentry
*debugfs_dir
;
610 struct dentry
*sched_debugfs_dir
;
613 bool mq_sysfs_init_done
;
618 struct work_struct release_work
;
620 #define BLK_MAX_WRITE_HINTS 5
621 u64 write_hints
[BLK_MAX_WRITE_HINTS
];
624 #define QUEUE_FLAG_QUEUED 0 /* uses generic tag queueing */
625 #define QUEUE_FLAG_STOPPED 1 /* queue is stopped */
626 #define QUEUE_FLAG_DYING 2 /* queue being torn down */
627 #define QUEUE_FLAG_BYPASS 3 /* act as dumb FIFO queue */
628 #define QUEUE_FLAG_BIDI 4 /* queue supports bidi requests */
629 #define QUEUE_FLAG_NOMERGES 5 /* disable merge attempts */
630 #define QUEUE_FLAG_SAME_COMP 6 /* complete on same CPU-group */
631 #define QUEUE_FLAG_FAIL_IO 7 /* fake timeout */
632 #define QUEUE_FLAG_STACKABLE 8 /* supports request stacking */
633 #define QUEUE_FLAG_NONROT 9 /* non-rotational device (SSD) */
634 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
635 #define QUEUE_FLAG_IO_STAT 10 /* do IO stats */
636 #define QUEUE_FLAG_DISCARD 11 /* supports DISCARD */
637 #define QUEUE_FLAG_NOXMERGES 12 /* No extended merges */
638 #define QUEUE_FLAG_ADD_RANDOM 13 /* Contributes to random pool */
639 #define QUEUE_FLAG_SECERASE 14 /* supports secure erase */
640 #define QUEUE_FLAG_SAME_FORCE 15 /* force complete on same CPU */
641 #define QUEUE_FLAG_DEAD 16 /* queue tear-down finished */
642 #define QUEUE_FLAG_INIT_DONE 17 /* queue is initialized */
643 #define QUEUE_FLAG_NO_SG_MERGE 18 /* don't attempt to merge SG segments*/
644 #define QUEUE_FLAG_POLL 19 /* IO polling enabled if set */
645 #define QUEUE_FLAG_WC 20 /* Write back caching */
646 #define QUEUE_FLAG_FUA 21 /* device supports FUA writes */
647 #define QUEUE_FLAG_FLUSH_NQ 22 /* flush not queueuable */
648 #define QUEUE_FLAG_DAX 23 /* device supports DAX */
649 #define QUEUE_FLAG_STATS 24 /* track rq completion times */
650 #define QUEUE_FLAG_POLL_STATS 25 /* collecting stats for hybrid polling */
651 #define QUEUE_FLAG_REGISTERED 26 /* queue has been registered to a disk */
652 #define QUEUE_FLAG_SCSI_PASSTHROUGH 27 /* queue supports SCSI commands */
653 #define QUEUE_FLAG_QUIESCED 28 /* queue has been quiesced */
655 #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
656 (1 << QUEUE_FLAG_STACKABLE) | \
657 (1 << QUEUE_FLAG_SAME_COMP) | \
658 (1 << QUEUE_FLAG_ADD_RANDOM))
660 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
661 (1 << QUEUE_FLAG_STACKABLE) | \
662 (1 << QUEUE_FLAG_SAME_COMP) | \
663 (1 << QUEUE_FLAG_POLL))
666 * @q->queue_lock is set while a queue is being initialized. Since we know
667 * that no other threads access the queue object before @q->queue_lock has
668 * been set, it is safe to manipulate queue flags without holding the
669 * queue_lock if @q->queue_lock == NULL. See also blk_alloc_queue_node() and
670 * blk_init_allocated_queue().
672 static inline void queue_lockdep_assert_held(struct request_queue
*q
)
675 lockdep_assert_held(q
->queue_lock
);
678 static inline void queue_flag_set_unlocked(unsigned int flag
,
679 struct request_queue
*q
)
681 __set_bit(flag
, &q
->queue_flags
);
684 static inline int queue_flag_test_and_clear(unsigned int flag
,
685 struct request_queue
*q
)
687 queue_lockdep_assert_held(q
);
689 if (test_bit(flag
, &q
->queue_flags
)) {
690 __clear_bit(flag
, &q
->queue_flags
);
697 static inline int queue_flag_test_and_set(unsigned int flag
,
698 struct request_queue
*q
)
700 queue_lockdep_assert_held(q
);
702 if (!test_bit(flag
, &q
->queue_flags
)) {
703 __set_bit(flag
, &q
->queue_flags
);
710 static inline void queue_flag_set(unsigned int flag
, struct request_queue
*q
)
712 queue_lockdep_assert_held(q
);
713 __set_bit(flag
, &q
->queue_flags
);
716 static inline void queue_flag_clear_unlocked(unsigned int flag
,
717 struct request_queue
*q
)
719 __clear_bit(flag
, &q
->queue_flags
);
722 static inline int queue_in_flight(struct request_queue
*q
)
724 return q
->in_flight
[0] + q
->in_flight
[1];
727 static inline void queue_flag_clear(unsigned int flag
, struct request_queue
*q
)
729 queue_lockdep_assert_held(q
);
730 __clear_bit(flag
, &q
->queue_flags
);
733 #define blk_queue_tagged(q) test_bit(QUEUE_FLAG_QUEUED, &(q)->queue_flags)
734 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
735 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
736 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
737 #define blk_queue_bypass(q) test_bit(QUEUE_FLAG_BYPASS, &(q)->queue_flags)
738 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
739 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
740 #define blk_queue_noxmerges(q) \
741 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
742 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
743 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
744 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
745 #define blk_queue_stackable(q) \
746 test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
747 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
748 #define blk_queue_secure_erase(q) \
749 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
750 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
751 #define blk_queue_scsi_passthrough(q) \
752 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
754 #define blk_noretry_request(rq) \
755 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
756 REQ_FAILFAST_DRIVER))
757 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
759 static inline bool blk_account_rq(struct request
*rq
)
761 return (rq
->rq_flags
& RQF_STARTED
) && !blk_rq_is_passthrough(rq
);
764 #define blk_rq_cpu_valid(rq) ((rq)->cpu != -1)
765 #define blk_bidi_rq(rq) ((rq)->next_rq != NULL)
766 /* rq->queuelist of dequeued request must be list_empty() */
767 #define blk_queued_rq(rq) (!list_empty(&(rq)->queuelist))
769 #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
771 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
774 * Driver can handle struct request, if it either has an old style
775 * request_fn defined, or is blk-mq based.
777 static inline bool queue_is_rq_based(struct request_queue
*q
)
779 return q
->request_fn
|| q
->mq_ops
;
782 static inline unsigned int blk_queue_cluster(struct request_queue
*q
)
784 return q
->limits
.cluster
;
787 static inline enum blk_zoned_model
788 blk_queue_zoned_model(struct request_queue
*q
)
790 return q
->limits
.zoned
;
793 static inline bool blk_queue_is_zoned(struct request_queue
*q
)
795 switch (blk_queue_zoned_model(q
)) {
804 static inline unsigned int blk_queue_zone_sectors(struct request_queue
*q
)
806 return blk_queue_is_zoned(q
) ? q
->limits
.chunk_sectors
: 0;
809 static inline bool rq_is_sync(struct request
*rq
)
811 return op_is_sync(rq
->cmd_flags
);
814 static inline bool blk_rl_full(struct request_list
*rl
, bool sync
)
816 unsigned int flag
= sync
? BLK_RL_SYNCFULL
: BLK_RL_ASYNCFULL
;
818 return rl
->flags
& flag
;
821 static inline void blk_set_rl_full(struct request_list
*rl
, bool sync
)
823 unsigned int flag
= sync
? BLK_RL_SYNCFULL
: BLK_RL_ASYNCFULL
;
828 static inline void blk_clear_rl_full(struct request_list
*rl
, bool sync
)
830 unsigned int flag
= sync
? BLK_RL_SYNCFULL
: BLK_RL_ASYNCFULL
;
835 static inline bool rq_mergeable(struct request
*rq
)
837 if (blk_rq_is_passthrough(rq
))
840 if (req_op(rq
) == REQ_OP_FLUSH
)
843 if (req_op(rq
) == REQ_OP_WRITE_ZEROES
)
846 if (rq
->cmd_flags
& REQ_NOMERGE_FLAGS
)
848 if (rq
->rq_flags
& RQF_NOMERGE_FLAGS
)
854 static inline bool blk_write_same_mergeable(struct bio
*a
, struct bio
*b
)
856 if (bio_page(a
) == bio_page(b
) &&
857 bio_offset(a
) == bio_offset(b
))
863 static inline unsigned int blk_queue_depth(struct request_queue
*q
)
866 return q
->queue_depth
;
868 return q
->nr_requests
;
872 * q->prep_rq_fn return values
875 BLKPREP_OK
, /* serve it */
876 BLKPREP_KILL
, /* fatal error, kill, return -EIO */
877 BLKPREP_DEFER
, /* leave on queue */
878 BLKPREP_INVALID
, /* invalid command, kill, return -EREMOTEIO */
881 extern unsigned long blk_max_low_pfn
, blk_max_pfn
;
884 * standard bounce addresses:
886 * BLK_BOUNCE_HIGH : bounce all highmem pages
887 * BLK_BOUNCE_ANY : don't bounce anything
888 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
891 #if BITS_PER_LONG == 32
892 #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
894 #define BLK_BOUNCE_HIGH -1ULL
896 #define BLK_BOUNCE_ANY (-1ULL)
897 #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
900 * default timeout for SG_IO if none specified
902 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
903 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
909 unsigned long offset
;
914 struct req_iterator
{
915 struct bvec_iter iter
;
919 /* This should not be used directly - use rq_for_each_segment */
920 #define for_each_bio(_bio) \
921 for (; _bio; _bio = _bio->bi_next)
922 #define __rq_for_each_bio(_bio, rq) \
924 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
926 #define rq_for_each_segment(bvl, _rq, _iter) \
927 __rq_for_each_bio(_iter.bio, _rq) \
928 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
930 #define rq_iter_last(bvec, _iter) \
931 (_iter.bio->bi_next == NULL && \
932 bio_iter_last(bvec, _iter.iter))
934 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
935 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
937 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
938 extern void rq_flush_dcache_pages(struct request
*rq
);
940 static inline void rq_flush_dcache_pages(struct request
*rq
)
946 #define vfs_msg(sb, level, fmt, ...) \
947 __vfs_msg(sb, level, fmt, ##__VA_ARGS__)
949 #define vfs_msg(sb, level, fmt, ...) \
951 no_printk(fmt, ##__VA_ARGS__); \
952 __vfs_msg(sb, "", " "); \
956 extern int blk_register_queue(struct gendisk
*disk
);
957 extern void blk_unregister_queue(struct gendisk
*disk
);
958 extern blk_qc_t
generic_make_request(struct bio
*bio
);
959 extern void blk_rq_init(struct request_queue
*q
, struct request
*rq
);
960 extern void blk_init_request_from_bio(struct request
*req
, struct bio
*bio
);
961 extern void blk_put_request(struct request
*);
962 extern void __blk_put_request(struct request_queue
*, struct request
*);
963 extern struct request
*blk_get_request(struct request_queue
*, unsigned int op
,
965 extern void blk_requeue_request(struct request_queue
*, struct request
*);
966 extern int blk_lld_busy(struct request_queue
*q
);
967 extern int blk_rq_prep_clone(struct request
*rq
, struct request
*rq_src
,
968 struct bio_set
*bs
, gfp_t gfp_mask
,
969 int (*bio_ctr
)(struct bio
*, struct bio
*, void *),
971 extern void blk_rq_unprep_clone(struct request
*rq
);
972 extern blk_status_t
blk_insert_cloned_request(struct request_queue
*q
,
974 extern int blk_rq_append_bio(struct request
*rq
, struct bio
**bio
);
975 extern void blk_delay_queue(struct request_queue
*, unsigned long);
976 extern void blk_queue_split(struct request_queue
*, struct bio
**);
977 extern void blk_recount_segments(struct request_queue
*, struct bio
*);
978 extern int scsi_verify_blk_ioctl(struct block_device
*, unsigned int);
979 extern int scsi_cmd_blk_ioctl(struct block_device
*, fmode_t
,
980 unsigned int, void __user
*);
981 extern int scsi_cmd_ioctl(struct request_queue
*, struct gendisk
*, fmode_t
,
982 unsigned int, void __user
*);
983 extern int sg_scsi_ioctl(struct request_queue
*, struct gendisk
*, fmode_t
,
984 struct scsi_ioctl_command __user
*);
986 extern int blk_queue_enter(struct request_queue
*q
, bool nowait
);
987 extern void blk_queue_exit(struct request_queue
*q
);
988 extern void blk_start_queue(struct request_queue
*q
);
989 extern void blk_start_queue_async(struct request_queue
*q
);
990 extern void blk_stop_queue(struct request_queue
*q
);
991 extern void blk_sync_queue(struct request_queue
*q
);
992 extern void __blk_stop_queue(struct request_queue
*q
);
993 extern void __blk_run_queue(struct request_queue
*q
);
994 extern void __blk_run_queue_uncond(struct request_queue
*q
);
995 extern void blk_run_queue(struct request_queue
*);
996 extern void blk_run_queue_async(struct request_queue
*q
);
997 extern int blk_rq_map_user(struct request_queue
*, struct request
*,
998 struct rq_map_data
*, void __user
*, unsigned long,
1000 extern int blk_rq_unmap_user(struct bio
*);
1001 extern int blk_rq_map_kern(struct request_queue
*, struct request
*, void *, unsigned int, gfp_t
);
1002 extern int blk_rq_map_user_iov(struct request_queue
*, struct request
*,
1003 struct rq_map_data
*, const struct iov_iter
*,
1005 extern void blk_execute_rq(struct request_queue
*, struct gendisk
*,
1006 struct request
*, int);
1007 extern void blk_execute_rq_nowait(struct request_queue
*, struct gendisk
*,
1008 struct request
*, int, rq_end_io_fn
*);
1010 int blk_status_to_errno(blk_status_t status
);
1011 blk_status_t
errno_to_blk_status(int errno
);
1013 bool blk_mq_poll(struct request_queue
*q
, blk_qc_t cookie
);
1015 static inline struct request_queue
*bdev_get_queue(struct block_device
*bdev
)
1017 return bdev
->bd_disk
->queue
; /* this is never NULL */
1021 * blk_rq_pos() : the current sector
1022 * blk_rq_bytes() : bytes left in the entire request
1023 * blk_rq_cur_bytes() : bytes left in the current segment
1024 * blk_rq_err_bytes() : bytes left till the next error boundary
1025 * blk_rq_sectors() : sectors left in the entire request
1026 * blk_rq_cur_sectors() : sectors left in the current segment
1028 static inline sector_t
blk_rq_pos(const struct request
*rq
)
1030 return rq
->__sector
;
1033 #ifdef CONFIG_CRYPTO_DISKCIPHER_DUN
1034 static inline sector_t
blk_rq_dun(const struct request
*rq
)
1040 static inline unsigned int blk_rq_bytes(const struct request
*rq
)
1042 return rq
->__data_len
;
1045 static inline int blk_rq_cur_bytes(const struct request
*rq
)
1047 return rq
->bio
? bio_cur_bytes(rq
->bio
) : 0;
1050 extern unsigned int blk_rq_err_bytes(const struct request
*rq
);
1052 static inline unsigned int blk_rq_sectors(const struct request
*rq
)
1054 return blk_rq_bytes(rq
) >> 9;
1057 static inline unsigned int blk_rq_cur_sectors(const struct request
*rq
)
1059 return blk_rq_cur_bytes(rq
) >> 9;
1063 * Some commands like WRITE SAME have a payload or data transfer size which
1064 * is different from the size of the request. Any driver that supports such
1065 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1066 * calculate the data transfer size.
1068 static inline unsigned int blk_rq_payload_bytes(struct request
*rq
)
1070 if (rq
->rq_flags
& RQF_SPECIAL_PAYLOAD
)
1071 return rq
->special_vec
.bv_len
;
1072 return blk_rq_bytes(rq
);
1075 static inline unsigned int blk_queue_get_max_sectors(struct request_queue
*q
,
1078 if (unlikely(op
== REQ_OP_DISCARD
|| op
== REQ_OP_SECURE_ERASE
))
1079 return min(q
->limits
.max_discard_sectors
, UINT_MAX
>> 9);
1081 if (unlikely(op
== REQ_OP_WRITE_SAME
))
1082 return q
->limits
.max_write_same_sectors
;
1084 if (unlikely(op
== REQ_OP_WRITE_ZEROES
))
1085 return q
->limits
.max_write_zeroes_sectors
;
1087 return q
->limits
.max_sectors
;
1091 * Return maximum size of a request at given offset. Only valid for
1092 * file system requests.
1094 static inline unsigned int blk_max_size_offset(struct request_queue
*q
,
1097 if (!q
->limits
.chunk_sectors
)
1098 return q
->limits
.max_sectors
;
1100 return min(q
->limits
.max_sectors
, (unsigned int)(q
->limits
.chunk_sectors
-
1101 (offset
& (q
->limits
.chunk_sectors
- 1))));
1104 static inline unsigned int blk_rq_get_max_sectors(struct request
*rq
,
1107 struct request_queue
*q
= rq
->q
;
1109 if (blk_rq_is_passthrough(rq
))
1110 return q
->limits
.max_hw_sectors
;
1112 if (!q
->limits
.chunk_sectors
||
1113 req_op(rq
) == REQ_OP_DISCARD
||
1114 req_op(rq
) == REQ_OP_SECURE_ERASE
)
1115 return blk_queue_get_max_sectors(q
, req_op(rq
));
1117 return min(blk_max_size_offset(q
, offset
),
1118 blk_queue_get_max_sectors(q
, req_op(rq
)));
1121 static inline unsigned int blk_rq_count_bios(struct request
*rq
)
1123 unsigned int nr_bios
= 0;
1126 __rq_for_each_bio(bio
, rq
)
1133 * Request issue related functions.
1135 extern struct request
*blk_peek_request(struct request_queue
*q
);
1136 extern void blk_start_request(struct request
*rq
);
1137 extern struct request
*blk_fetch_request(struct request_queue
*q
);
1140 * Request completion related functions.
1142 * blk_update_request() completes given number of bytes and updates
1143 * the request without completing it.
1145 * blk_end_request() and friends. __blk_end_request() must be called
1146 * with the request queue spinlock acquired.
1148 * Several drivers define their own end_request and call
1149 * blk_end_request() for parts of the original function.
1150 * This prevents code duplication in drivers.
1152 extern bool blk_update_request(struct request
*rq
, blk_status_t error
,
1153 unsigned int nr_bytes
);
1154 extern void blk_finish_request(struct request
*rq
, blk_status_t error
);
1155 extern bool blk_end_request(struct request
*rq
, blk_status_t error
,
1156 unsigned int nr_bytes
);
1157 extern void blk_end_request_all(struct request
*rq
, blk_status_t error
);
1158 extern bool __blk_end_request(struct request
*rq
, blk_status_t error
,
1159 unsigned int nr_bytes
);
1160 extern void __blk_end_request_all(struct request
*rq
, blk_status_t error
);
1161 extern bool __blk_end_request_cur(struct request
*rq
, blk_status_t error
);
1163 extern void blk_complete_request(struct request
*);
1164 extern void __blk_complete_request(struct request
*);
1165 extern void blk_abort_request(struct request
*);
1166 extern void blk_unprep_request(struct request
*);
1169 * Access functions for manipulating queue properties
1171 extern struct request_queue
*blk_init_queue_node(request_fn_proc
*rfn
,
1172 spinlock_t
*lock
, int node_id
);
1173 extern struct request_queue
*blk_init_queue(request_fn_proc
*, spinlock_t
*);
1174 extern int blk_init_allocated_queue(struct request_queue
*);
1175 extern void blk_cleanup_queue(struct request_queue
*);
1176 extern void blk_queue_make_request(struct request_queue
*, make_request_fn
*);
1177 extern void blk_queue_bounce_limit(struct request_queue
*, u64
);
1178 extern void blk_queue_max_hw_sectors(struct request_queue
*, unsigned int);
1179 extern void blk_queue_chunk_sectors(struct request_queue
*, unsigned int);
1180 extern void blk_queue_max_segments(struct request_queue
*, unsigned short);
1181 extern void blk_queue_max_discard_segments(struct request_queue
*,
1183 extern void blk_queue_max_segment_size(struct request_queue
*, unsigned int);
1184 extern void blk_queue_max_discard_sectors(struct request_queue
*q
,
1185 unsigned int max_discard_sectors
);
1186 extern void blk_queue_max_write_same_sectors(struct request_queue
*q
,
1187 unsigned int max_write_same_sectors
);
1188 extern void blk_queue_max_write_zeroes_sectors(struct request_queue
*q
,
1189 unsigned int max_write_same_sectors
);
1190 extern void blk_queue_logical_block_size(struct request_queue
*, unsigned short);
1191 extern void blk_queue_physical_block_size(struct request_queue
*, unsigned int);
1192 extern void blk_queue_alignment_offset(struct request_queue
*q
,
1193 unsigned int alignment
);
1194 extern void blk_limits_io_min(struct queue_limits
*limits
, unsigned int min
);
1195 extern void blk_queue_io_min(struct request_queue
*q
, unsigned int min
);
1196 extern void blk_limits_io_opt(struct queue_limits
*limits
, unsigned int opt
);
1197 extern void blk_queue_io_opt(struct request_queue
*q
, unsigned int opt
);
1198 extern void blk_set_queue_depth(struct request_queue
*q
, unsigned int depth
);
1199 extern void blk_set_default_limits(struct queue_limits
*lim
);
1200 extern void blk_set_stacking_limits(struct queue_limits
*lim
);
1201 extern int blk_stack_limits(struct queue_limits
*t
, struct queue_limits
*b
,
1203 extern int bdev_stack_limits(struct queue_limits
*t
, struct block_device
*bdev
,
1205 extern void disk_stack_limits(struct gendisk
*disk
, struct block_device
*bdev
,
1207 extern void blk_queue_stack_limits(struct request_queue
*t
, struct request_queue
*b
);
1208 extern void blk_queue_dma_pad(struct request_queue
*, unsigned int);
1209 extern void blk_queue_update_dma_pad(struct request_queue
*, unsigned int);
1210 extern int blk_queue_dma_drain(struct request_queue
*q
,
1211 dma_drain_needed_fn
*dma_drain_needed
,
1212 void *buf
, unsigned int size
);
1213 extern void blk_queue_lld_busy(struct request_queue
*q
, lld_busy_fn
*fn
);
1214 extern void blk_queue_segment_boundary(struct request_queue
*, unsigned long);
1215 extern void blk_queue_virt_boundary(struct request_queue
*, unsigned long);
1216 extern void blk_queue_prep_rq(struct request_queue
*, prep_rq_fn
*pfn
);
1217 extern void blk_queue_unprep_rq(struct request_queue
*, unprep_rq_fn
*ufn
);
1218 extern void blk_queue_dma_alignment(struct request_queue
*, int);
1219 extern void blk_queue_update_dma_alignment(struct request_queue
*, int);
1220 extern void blk_queue_softirq_done(struct request_queue
*, softirq_done_fn
*);
1221 extern void blk_queue_rq_timed_out(struct request_queue
*, rq_timed_out_fn
*);
1222 extern void blk_queue_rq_timeout(struct request_queue
*, unsigned int);
1223 extern void blk_queue_flush_queueable(struct request_queue
*q
, bool queueable
);
1224 extern void blk_queue_write_cache(struct request_queue
*q
, bool enabled
, bool fua
);
1227 * Number of physical segments as sent to the device.
1229 * Normally this is the number of discontiguous data segments sent by the
1230 * submitter. But for data-less command like discard we might have no
1231 * actual data segments submitted, but the driver might have to add it's
1232 * own special payload. In that case we still return 1 here so that this
1233 * special payload will be mapped.
1235 static inline unsigned short blk_rq_nr_phys_segments(struct request
*rq
)
1237 if (rq
->rq_flags
& RQF_SPECIAL_PAYLOAD
)
1239 return rq
->nr_phys_segments
;
1243 * Number of discard segments (or ranges) the driver needs to fill in.
1244 * Each discard bio merged into a request is counted as one segment.
1246 static inline unsigned short blk_rq_nr_discard_segments(struct request
*rq
)
1248 return max_t(unsigned short, rq
->nr_phys_segments
, 1);
1251 extern int blk_rq_map_sg(struct request_queue
*, struct request
*, struct scatterlist
*);
1252 extern void blk_dump_rq_flags(struct request
*, char *);
1253 extern long nr_blockdev_pages(void);
1255 bool __must_check
blk_get_queue(struct request_queue
*);
1256 struct request_queue
*blk_alloc_queue(gfp_t
);
1257 struct request_queue
*blk_alloc_queue_node(gfp_t
, int);
1258 extern void blk_put_queue(struct request_queue
*);
1259 extern void blk_set_queue_dying(struct request_queue
*);
1262 * block layer runtime pm functions
1265 extern void blk_pm_runtime_init(struct request_queue
*q
, struct device
*dev
);
1266 extern int blk_pre_runtime_suspend(struct request_queue
*q
);
1267 extern void blk_post_runtime_suspend(struct request_queue
*q
, int err
);
1268 extern void blk_pre_runtime_resume(struct request_queue
*q
);
1269 extern void blk_post_runtime_resume(struct request_queue
*q
, int err
);
1270 extern void blk_set_runtime_active(struct request_queue
*q
);
1272 static inline void blk_pm_runtime_init(struct request_queue
*q
,
1273 struct device
*dev
) {}
1274 static inline int blk_pre_runtime_suspend(struct request_queue
*q
)
1278 static inline void blk_post_runtime_suspend(struct request_queue
*q
, int err
) {}
1279 static inline void blk_pre_runtime_resume(struct request_queue
*q
) {}
1280 static inline void blk_post_runtime_resume(struct request_queue
*q
, int err
) {}
1281 static inline void blk_set_runtime_active(struct request_queue
*q
) {}
1285 * blk_plug permits building a queue of related requests by holding the I/O
1286 * fragments for a short period. This allows merging of sequential requests
1287 * into single larger request. As the requests are moved from a per-task list to
1288 * the device's request_queue in a batch, this results in improved scalability
1289 * as the lock contention for request_queue lock is reduced.
1291 * It is ok not to disable preemption when adding the request to the plug list
1292 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1293 * the plug list when the task sleeps by itself. For details, please see
1294 * schedule() where blk_schedule_flush_plug() is called.
1297 struct list_head list
; /* requests */
1298 struct list_head mq_list
; /* blk-mq requests */
1299 struct list_head cb_list
; /* md requires an unplug callback */
1301 #define BLK_MAX_REQUEST_COUNT 16
1302 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1305 typedef void (*blk_plug_cb_fn
)(struct blk_plug_cb
*, bool);
1306 struct blk_plug_cb
{
1307 struct list_head list
;
1308 blk_plug_cb_fn callback
;
1311 extern struct blk_plug_cb
*blk_check_plugged(blk_plug_cb_fn unplug
,
1312 void *data
, int size
);
1313 extern void blk_start_plug(struct blk_plug
*);
1314 extern void blk_finish_plug(struct blk_plug
*);
1315 extern void blk_flush_plug_list(struct blk_plug
*, bool);
1317 static inline void blk_flush_plug(struct task_struct
*tsk
)
1319 struct blk_plug
*plug
= tsk
->plug
;
1322 blk_flush_plug_list(plug
, false);
1325 static inline void blk_schedule_flush_plug(struct task_struct
*tsk
)
1327 struct blk_plug
*plug
= tsk
->plug
;
1330 blk_flush_plug_list(plug
, true);
1333 static inline bool blk_needs_flush_plug(struct task_struct
*tsk
)
1335 struct blk_plug
*plug
= tsk
->plug
;
1338 (!list_empty(&plug
->list
) ||
1339 !list_empty(&plug
->mq_list
) ||
1340 !list_empty(&plug
->cb_list
));
1346 extern int blk_queue_start_tag(struct request_queue
*, struct request
*);
1347 extern struct request
*blk_queue_find_tag(struct request_queue
*, int);
1348 extern void blk_queue_end_tag(struct request_queue
*, struct request
*);
1349 extern int blk_queue_init_tags(struct request_queue
*, int, struct blk_queue_tag
*, int);
1350 extern void blk_queue_free_tags(struct request_queue
*);
1351 extern int blk_queue_resize_tags(struct request_queue
*, int);
1352 extern void blk_queue_invalidate_tags(struct request_queue
*);
1353 extern struct blk_queue_tag
*blk_init_tags(int, int);
1354 extern void blk_free_tags(struct blk_queue_tag
*);
1356 static inline struct request
*blk_map_queue_find_tag(struct blk_queue_tag
*bqt
,
1359 if (unlikely(bqt
== NULL
|| tag
>= bqt
->real_max_depth
))
1361 return bqt
->tag_index
[tag
];
1364 extern int blkdev_issue_flush(struct block_device
*, gfp_t
, sector_t
*);
1365 extern int blkdev_issue_write_same(struct block_device
*bdev
, sector_t sector
,
1366 sector_t nr_sects
, gfp_t gfp_mask
, struct page
*page
);
1368 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1370 extern int blkdev_issue_discard(struct block_device
*bdev
, sector_t sector
,
1371 sector_t nr_sects
, gfp_t gfp_mask
, unsigned long flags
);
1372 extern int __blkdev_issue_discard(struct block_device
*bdev
, sector_t sector
,
1373 sector_t nr_sects
, gfp_t gfp_mask
, int flags
,
1376 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1377 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1379 extern int __blkdev_issue_zeroout(struct block_device
*bdev
, sector_t sector
,
1380 sector_t nr_sects
, gfp_t gfp_mask
, struct bio
**biop
,
1382 extern int blkdev_issue_zeroout(struct block_device
*bdev
, sector_t sector
,
1383 sector_t nr_sects
, gfp_t gfp_mask
, unsigned flags
);
1385 static inline int sb_issue_discard(struct super_block
*sb
, sector_t block
,
1386 sector_t nr_blocks
, gfp_t gfp_mask
, unsigned long flags
)
1388 return blkdev_issue_discard(sb
->s_bdev
, block
<< (sb
->s_blocksize_bits
- 9),
1389 nr_blocks
<< (sb
->s_blocksize_bits
- 9),
1392 static inline int sb_issue_zeroout(struct super_block
*sb
, sector_t block
,
1393 sector_t nr_blocks
, gfp_t gfp_mask
)
1395 return blkdev_issue_zeroout(sb
->s_bdev
,
1396 block
<< (sb
->s_blocksize_bits
- 9),
1397 nr_blocks
<< (sb
->s_blocksize_bits
- 9),
1401 extern int blk_verify_command(unsigned char *cmd
, fmode_t has_write_perm
);
1403 enum blk_default_limits
{
1404 BLK_MAX_SEGMENTS
= 128,
1405 BLK_SAFE_MAX_SECTORS
= 255,
1406 BLK_DEF_MAX_SECTORS
= 2560,
1407 BLK_MAX_SEGMENT_SIZE
= 65536,
1408 BLK_SEG_BOUNDARY_MASK
= 0xFFFFFFFFUL
,
1411 #define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist)
1413 static inline unsigned long queue_segment_boundary(struct request_queue
*q
)
1415 return q
->limits
.seg_boundary_mask
;
1418 static inline unsigned long queue_virt_boundary(struct request_queue
*q
)
1420 return q
->limits
.virt_boundary_mask
;
1423 static inline unsigned int queue_max_sectors(struct request_queue
*q
)
1425 return q
->limits
.max_sectors
;
1428 static inline unsigned int queue_max_hw_sectors(struct request_queue
*q
)
1430 return q
->limits
.max_hw_sectors
;
1433 static inline unsigned short queue_max_segments(struct request_queue
*q
)
1435 return q
->limits
.max_segments
;
1438 static inline unsigned short queue_max_discard_segments(struct request_queue
*q
)
1440 return q
->limits
.max_discard_segments
;
1443 static inline unsigned int queue_max_segment_size(struct request_queue
*q
)
1445 return q
->limits
.max_segment_size
;
1448 static inline unsigned short queue_logical_block_size(struct request_queue
*q
)
1452 if (q
&& q
->limits
.logical_block_size
)
1453 retval
= q
->limits
.logical_block_size
;
1458 static inline unsigned short bdev_logical_block_size(struct block_device
*bdev
)
1460 return queue_logical_block_size(bdev_get_queue(bdev
));
1463 static inline unsigned int queue_physical_block_size(struct request_queue
*q
)
1465 return q
->limits
.physical_block_size
;
1468 static inline unsigned int bdev_physical_block_size(struct block_device
*bdev
)
1470 return queue_physical_block_size(bdev_get_queue(bdev
));
1473 static inline unsigned int queue_io_min(struct request_queue
*q
)
1475 return q
->limits
.io_min
;
1478 static inline int bdev_io_min(struct block_device
*bdev
)
1480 return queue_io_min(bdev_get_queue(bdev
));
1483 static inline unsigned int queue_io_opt(struct request_queue
*q
)
1485 return q
->limits
.io_opt
;
1488 static inline int bdev_io_opt(struct block_device
*bdev
)
1490 return queue_io_opt(bdev_get_queue(bdev
));
1493 static inline int queue_alignment_offset(struct request_queue
*q
)
1495 if (q
->limits
.misaligned
)
1498 return q
->limits
.alignment_offset
;
1501 static inline int queue_limit_alignment_offset(struct queue_limits
*lim
, sector_t sector
)
1503 unsigned int granularity
= max(lim
->physical_block_size
, lim
->io_min
);
1504 unsigned int alignment
= sector_div(sector
, granularity
>> 9) << 9;
1506 return (granularity
+ lim
->alignment_offset
- alignment
) % granularity
;
1509 static inline int bdev_alignment_offset(struct block_device
*bdev
)
1511 struct request_queue
*q
= bdev_get_queue(bdev
);
1513 if (q
->limits
.misaligned
)
1516 if (bdev
!= bdev
->bd_contains
)
1517 return bdev
->bd_part
->alignment_offset
;
1519 return q
->limits
.alignment_offset
;
1522 static inline int queue_discard_alignment(struct request_queue
*q
)
1524 if (q
->limits
.discard_misaligned
)
1527 return q
->limits
.discard_alignment
;
1530 static inline int queue_limit_discard_alignment(struct queue_limits
*lim
, sector_t sector
)
1532 unsigned int alignment
, granularity
, offset
;
1534 if (!lim
->max_discard_sectors
)
1537 /* Why are these in bytes, not sectors? */
1538 alignment
= lim
->discard_alignment
>> 9;
1539 granularity
= lim
->discard_granularity
>> 9;
1543 /* Offset of the partition start in 'granularity' sectors */
1544 offset
= sector_div(sector
, granularity
);
1546 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1547 offset
= (granularity
+ alignment
- offset
) % granularity
;
1549 /* Turn it back into bytes, gaah */
1553 static inline int bdev_discard_alignment(struct block_device
*bdev
)
1555 struct request_queue
*q
= bdev_get_queue(bdev
);
1557 if (bdev
!= bdev
->bd_contains
)
1558 return bdev
->bd_part
->discard_alignment
;
1560 return q
->limits
.discard_alignment
;
1563 static inline unsigned int bdev_write_same(struct block_device
*bdev
)
1565 struct request_queue
*q
= bdev_get_queue(bdev
);
1568 return q
->limits
.max_write_same_sectors
;
1573 static inline unsigned int bdev_write_zeroes_sectors(struct block_device
*bdev
)
1575 struct request_queue
*q
= bdev_get_queue(bdev
);
1578 return q
->limits
.max_write_zeroes_sectors
;
1583 static inline enum blk_zoned_model
bdev_zoned_model(struct block_device
*bdev
)
1585 struct request_queue
*q
= bdev_get_queue(bdev
);
1588 return blk_queue_zoned_model(q
);
1590 return BLK_ZONED_NONE
;
1593 static inline bool bdev_is_zoned(struct block_device
*bdev
)
1595 struct request_queue
*q
= bdev_get_queue(bdev
);
1598 return blk_queue_is_zoned(q
);
1603 static inline unsigned int bdev_zone_sectors(struct block_device
*bdev
)
1605 struct request_queue
*q
= bdev_get_queue(bdev
);
1608 return blk_queue_zone_sectors(q
);
1613 static inline int queue_dma_alignment(struct request_queue
*q
)
1615 return q
? q
->dma_alignment
: 511;
1618 static inline int blk_rq_aligned(struct request_queue
*q
, unsigned long addr
,
1621 unsigned int alignment
= queue_dma_alignment(q
) | q
->dma_pad_mask
;
1622 return !(addr
& alignment
) && !(len
& alignment
);
1625 /* assumes size > 256 */
1626 static inline unsigned int blksize_bits(unsigned int size
)
1628 unsigned int bits
= 8;
1632 } while (size
> 256);
1636 static inline unsigned int block_size(struct block_device
*bdev
)
1638 return bdev
->bd_block_size
;
1641 static inline bool queue_flush_queueable(struct request_queue
*q
)
1643 return !test_bit(QUEUE_FLAG_FLUSH_NQ
, &q
->queue_flags
);
1646 typedef struct {struct page
*v
;} Sector
;
1648 unsigned char *read_dev_sector(struct block_device
*, sector_t
, Sector
*);
1650 static inline void put_dev_sector(Sector p
)
1655 static inline bool __bvec_gap_to_prev(struct request_queue
*q
,
1656 struct bio_vec
*bprv
, unsigned int offset
)
1659 ((bprv
->bv_offset
+ bprv
->bv_len
) & queue_virt_boundary(q
));
1663 * Check if adding a bio_vec after bprv with offset would create a gap in
1664 * the SG list. Most drivers don't care about this, but some do.
1666 static inline bool bvec_gap_to_prev(struct request_queue
*q
,
1667 struct bio_vec
*bprv
, unsigned int offset
)
1669 if (!queue_virt_boundary(q
))
1671 return __bvec_gap_to_prev(q
, bprv
, offset
);
1675 * Check if the two bvecs from two bios can be merged to one segment.
1676 * If yes, no need to check gap between the two bios since the 1st bio
1677 * and the 1st bvec in the 2nd bio can be handled in one segment.
1679 static inline bool bios_segs_mergeable(struct request_queue
*q
,
1680 struct bio
*prev
, struct bio_vec
*prev_last_bv
,
1681 struct bio_vec
*next_first_bv
)
1683 if (!BIOVEC_PHYS_MERGEABLE(prev_last_bv
, next_first_bv
))
1685 if (!BIOVEC_SEG_BOUNDARY(q
, prev_last_bv
, next_first_bv
))
1687 if (prev
->bi_seg_back_size
+ next_first_bv
->bv_len
>
1688 queue_max_segment_size(q
))
1693 static inline bool bio_will_gap(struct request_queue
*q
,
1694 struct request
*prev_rq
,
1698 if (bio_has_data(prev
) && queue_virt_boundary(q
)) {
1699 struct bio_vec pb
, nb
;
1702 * don't merge if the 1st bio starts with non-zero
1703 * offset, otherwise it is quite difficult to respect
1704 * sg gap limit. We work hard to merge a huge number of small
1705 * single bios in case of mkfs.
1708 bio_get_first_bvec(prev_rq
->bio
, &pb
);
1710 bio_get_first_bvec(prev
, &pb
);
1715 * We don't need to worry about the situation that the
1716 * merged segment ends in unaligned virt boundary:
1718 * - if 'pb' ends aligned, the merged segment ends aligned
1719 * - if 'pb' ends unaligned, the next bio must include
1720 * one single bvec of 'nb', otherwise the 'nb' can't
1723 bio_get_last_bvec(prev
, &pb
);
1724 bio_get_first_bvec(next
, &nb
);
1726 if (!bios_segs_mergeable(q
, prev
, &pb
, &nb
))
1727 return __bvec_gap_to_prev(q
, &pb
, nb
.bv_offset
);
1733 static inline bool req_gap_back_merge(struct request
*req
, struct bio
*bio
)
1735 return bio_will_gap(req
->q
, req
, req
->biotail
, bio
);
1738 static inline bool req_gap_front_merge(struct request
*req
, struct bio
*bio
)
1740 return bio_will_gap(req
->q
, NULL
, bio
, req
->bio
);
1743 int kblockd_schedule_work(struct work_struct
*work
);
1744 int kblockd_schedule_work_on(int cpu
, struct work_struct
*work
);
1745 int kblockd_schedule_delayed_work(struct delayed_work
*dwork
, unsigned long delay
);
1746 int kblockd_schedule_delayed_work_on(int cpu
, struct delayed_work
*dwork
, unsigned long delay
);
1747 int kblockd_mod_delayed_work_on(int cpu
, struct delayed_work
*dwork
, unsigned long delay
);
1749 #ifdef CONFIG_BLK_CGROUP
1751 * This should not be using sched_clock(). A real patch is in progress
1752 * to fix this up, until that is in place we need to disable preemption
1753 * around sched_clock() in this function and set_io_start_time_ns().
1755 static inline void set_start_time_ns(struct request
*req
)
1758 req
->start_time_ns
= sched_clock();
1762 static inline void set_io_start_time_ns(struct request
*req
)
1765 req
->io_start_time_ns
= sched_clock();
1769 static inline uint64_t rq_start_time_ns(struct request
*req
)
1771 return req
->start_time_ns
;
1774 static inline uint64_t rq_io_start_time_ns(struct request
*req
)
1776 return req
->io_start_time_ns
;
1779 static inline void set_start_time_ns(struct request
*req
) {}
1780 static inline void set_io_start_time_ns(struct request
*req
) {}
1781 static inline uint64_t rq_start_time_ns(struct request
*req
)
1785 static inline uint64_t rq_io_start_time_ns(struct request
*req
)
1791 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1792 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1793 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1794 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1796 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1798 enum blk_integrity_flags
{
1799 BLK_INTEGRITY_VERIFY
= 1 << 0,
1800 BLK_INTEGRITY_GENERATE
= 1 << 1,
1801 BLK_INTEGRITY_DEVICE_CAPABLE
= 1 << 2,
1802 BLK_INTEGRITY_IP_CHECKSUM
= 1 << 3,
1805 struct blk_integrity_iter
{
1809 unsigned int data_size
;
1810 unsigned short interval
;
1811 const char *disk_name
;
1814 typedef blk_status_t (integrity_processing_fn
) (struct blk_integrity_iter
*);
1816 struct blk_integrity_profile
{
1817 integrity_processing_fn
*generate_fn
;
1818 integrity_processing_fn
*verify_fn
;
1822 extern void blk_integrity_register(struct gendisk
*, struct blk_integrity
*);
1823 extern void blk_integrity_unregister(struct gendisk
*);
1824 extern int blk_integrity_compare(struct gendisk
*, struct gendisk
*);
1825 extern int blk_rq_map_integrity_sg(struct request_queue
*, struct bio
*,
1826 struct scatterlist
*);
1827 extern int blk_rq_count_integrity_sg(struct request_queue
*, struct bio
*);
1828 extern bool blk_integrity_merge_rq(struct request_queue
*, struct request
*,
1830 extern bool blk_integrity_merge_bio(struct request_queue
*, struct request
*,
1833 static inline struct blk_integrity
*blk_get_integrity(struct gendisk
*disk
)
1835 struct blk_integrity
*bi
= &disk
->queue
->integrity
;
1844 struct blk_integrity
*bdev_get_integrity(struct block_device
*bdev
)
1846 return blk_get_integrity(bdev
->bd_disk
);
1849 static inline bool blk_integrity_rq(struct request
*rq
)
1851 return rq
->cmd_flags
& REQ_INTEGRITY
;
1854 static inline void blk_queue_max_integrity_segments(struct request_queue
*q
,
1857 q
->limits
.max_integrity_segments
= segs
;
1860 static inline unsigned short
1861 queue_max_integrity_segments(struct request_queue
*q
)
1863 return q
->limits
.max_integrity_segments
;
1866 static inline bool integrity_req_gap_back_merge(struct request
*req
,
1869 struct bio_integrity_payload
*bip
= bio_integrity(req
->bio
);
1870 struct bio_integrity_payload
*bip_next
= bio_integrity(next
);
1872 return bvec_gap_to_prev(req
->q
, &bip
->bip_vec
[bip
->bip_vcnt
- 1],
1873 bip_next
->bip_vec
[0].bv_offset
);
1876 static inline bool integrity_req_gap_front_merge(struct request
*req
,
1879 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
1880 struct bio_integrity_payload
*bip_next
= bio_integrity(req
->bio
);
1882 return bvec_gap_to_prev(req
->q
, &bip
->bip_vec
[bip
->bip_vcnt
- 1],
1883 bip_next
->bip_vec
[0].bv_offset
);
1886 #else /* CONFIG_BLK_DEV_INTEGRITY */
1889 struct block_device
;
1891 struct blk_integrity
;
1893 static inline int blk_integrity_rq(struct request
*rq
)
1897 static inline int blk_rq_count_integrity_sg(struct request_queue
*q
,
1902 static inline int blk_rq_map_integrity_sg(struct request_queue
*q
,
1904 struct scatterlist
*s
)
1908 static inline struct blk_integrity
*bdev_get_integrity(struct block_device
*b
)
1912 static inline struct blk_integrity
*blk_get_integrity(struct gendisk
*disk
)
1916 static inline int blk_integrity_compare(struct gendisk
*a
, struct gendisk
*b
)
1920 static inline void blk_integrity_register(struct gendisk
*d
,
1921 struct blk_integrity
*b
)
1924 static inline void blk_integrity_unregister(struct gendisk
*d
)
1927 static inline void blk_queue_max_integrity_segments(struct request_queue
*q
,
1931 static inline unsigned short queue_max_integrity_segments(struct request_queue
*q
)
1935 static inline bool blk_integrity_merge_rq(struct request_queue
*rq
,
1941 static inline bool blk_integrity_merge_bio(struct request_queue
*rq
,
1948 static inline bool integrity_req_gap_back_merge(struct request
*req
,
1953 static inline bool integrity_req_gap_front_merge(struct request
*req
,
1959 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1961 struct block_device_operations
{
1962 int (*open
) (struct block_device
*, fmode_t
);
1963 void (*release
) (struct gendisk
*, fmode_t
);
1964 int (*rw_page
)(struct block_device
*, sector_t
, struct page
*, bool);
1965 int (*ioctl
) (struct block_device
*, fmode_t
, unsigned, unsigned long);
1966 int (*compat_ioctl
) (struct block_device
*, fmode_t
, unsigned, unsigned long);
1967 unsigned int (*check_events
) (struct gendisk
*disk
,
1968 unsigned int clearing
);
1969 /* ->media_changed() is DEPRECATED, use ->check_events() instead */
1970 int (*media_changed
) (struct gendisk
*);
1971 void (*unlock_native_capacity
) (struct gendisk
*);
1972 int (*revalidate_disk
) (struct gendisk
*);
1973 int (*getgeo
)(struct block_device
*, struct hd_geometry
*);
1974 /* this callback is with swap_lock and sometimes page table lock held */
1975 void (*swap_slot_free_notify
) (struct block_device
*, unsigned long);
1976 struct module
*owner
;
1977 const struct pr_ops
*pr_ops
;
1980 extern int __blkdev_driver_ioctl(struct block_device
*, fmode_t
, unsigned int,
1982 extern int bdev_read_page(struct block_device
*, sector_t
, struct page
*);
1983 extern int bdev_write_page(struct block_device
*, sector_t
, struct page
*,
1984 struct writeback_control
*);
1985 #else /* CONFIG_BLOCK */
1987 struct block_device
;
1990 * stubs for when the block layer is configured out
1992 #define buffer_heads_over_limit 0
1994 static inline long nr_blockdev_pages(void)
2002 static inline void blk_start_plug(struct blk_plug
*plug
)
2006 static inline void blk_finish_plug(struct blk_plug
*plug
)
2010 static inline void blk_flush_plug(struct task_struct
*task
)
2014 static inline void blk_schedule_flush_plug(struct task_struct
*task
)
2019 static inline bool blk_needs_flush_plug(struct task_struct
*tsk
)
2024 static inline int blkdev_issue_flush(struct block_device
*bdev
, gfp_t gfp_mask
,
2025 sector_t
*error_sector
)
2030 #endif /* CONFIG_BLOCK */