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>
30 #include <linux/sched/sysctl.h>
33 struct scsi_ioctl_command;
36 struct elevator_queue;
42 struct blk_flush_queue;
45 struct blk_queue_stats;
46 struct blk_stat_callback;
48 #define BLKDEV_MIN_RQ 4
49 #define BLKDEV_MAX_RQ 128 /* Default maximum */
51 /* Must be consistent with blk_mq_poll_stats_bkt() */
52 #define BLK_MQ_POLL_STATS_BKTS 16
54 /* Doing classic polling */
55 #define BLK_MQ_POLL_CLASSIC -1
58 * Maximum number of blkcg policies allowed to be registered concurrently.
59 * Defined here to simplify include dependency.
61 #define BLKCG_MAX_POLS 5
63 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
67 typedef __u32 __bitwise req_flags_t;
69 /* elevator knows about this request */
70 #define RQF_SORTED ((__force req_flags_t)(1 << 0))
71 /* drive already may have started this one */
72 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
73 /* may not be passed by ioscheduler */
74 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
75 /* request for flush sequence */
76 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
77 /* merge of different types, fail separately */
78 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
79 /* track inflight for MQ */
80 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
81 /* don't call prep for this one */
82 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
83 /* set for "ide_preempt" requests and also for requests for which the SCSI
84 "quiesce" state must be ignored. */
85 #define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
86 /* vaguely specified driver internal error. Ignored by the block layer */
87 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
88 /* don't warn about errors */
89 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
90 /* elevator private data attached */
91 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
92 /* account into disk and partition IO statistics */
93 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
94 /* request came from our alloc pool */
95 #define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
96 /* runtime pm request */
97 #define RQF_PM ((__force req_flags_t)(1 << 15))
98 /* on IO scheduler merge hash */
99 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
100 /* track IO completion time */
101 #define RQF_STATS ((__force req_flags_t)(1 << 17))
102 /* Look at ->special_vec for the actual data payload instead of the
104 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
105 /* The per-zone write lock is held for this request */
106 #define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
107 /* already slept for hybrid poll */
108 #define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
109 /* ->timeout has been called, don't expire again */
110 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
112 /* flags that prevent us from merging requests: */
113 #define RQF_NOMERGE_FLAGS \
114 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
117 * Request state for blk-mq.
126 * Try to put the fields that are referenced together in the same cacheline.
128 * If you modify this structure, make sure to update blk_rq_init() and
129 * especially blk_mq_rq_ctx_init() to take care of the added fields.
132 struct request_queue *q;
133 struct blk_mq_ctx *mq_ctx;
134 struct blk_mq_hw_ctx *mq_hctx;
136 unsigned int cmd_flags; /* op and common flags */
137 req_flags_t rq_flags;
142 /* the following two fields are internal, NEVER access directly */
143 unsigned int __data_len; /* total data len */
144 sector_t __sector; /* sector cursor */
149 struct list_head queuelist;
152 * The hash is used inside the scheduler, and killed once the
153 * request reaches the dispatch list. The ipi_list is only used
154 * to queue the request for softirq completion, which is long
155 * after the request has been unhashed (and even removed from
156 * the dispatch list).
159 struct hlist_node hash; /* merge hash */
160 struct list_head ipi_list;
164 * The rb_node is only used inside the io scheduler, requests
165 * are pruned when moved to the dispatch queue. So let the
166 * completion_data share space with the rb_node.
169 struct rb_node rb_node; /* sort/lookup */
170 struct bio_vec special_vec;
171 void *completion_data;
172 int error_count; /* for legacy drivers, don't use */
176 * Three pointers are available for the IO schedulers, if they need
177 * more they have to dynamically allocate it. Flush requests are
178 * never put on the IO scheduler. So let the flush fields share
179 * space with the elevator data.
189 struct list_head list;
190 rq_end_io_fn *saved_end_io;
194 struct gendisk *rq_disk;
195 struct hd_struct *part;
196 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
197 /* Time that the first bio started allocating this request. */
200 /* Time that this request was allocated for this IO. */
202 /* Time that I/O was submitted to the device. */
203 u64 io_start_time_ns;
205 #ifdef CONFIG_BLK_WBT
206 unsigned short wbt_flags;
209 * rq sectors used for blk stats. It has the same value
210 * with blk_rq_sectors(rq), except that it never be zeroed
213 unsigned short stats_sectors;
216 * Number of scatter-gather DMA addr+len pairs after
217 * physical address coalescing is performed.
219 unsigned short nr_phys_segments;
221 #if defined(CONFIG_BLK_DEV_INTEGRITY)
222 unsigned short nr_integrity_segments;
225 unsigned short write_hint;
226 unsigned short ioprio;
228 enum mq_rq_state state;
231 unsigned int timeout;
232 unsigned long deadline;
235 struct __call_single_data csd;
240 * completion callback.
242 rq_end_io_fn *end_io;
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 blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
291 enum blk_eh_timer_return {
292 BLK_EH_DONE, /* drivers has completed the command */
293 BLK_EH_RESET_TIMER, /* reset timer and try again */
296 enum blk_queue_state {
301 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
302 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
304 #define BLK_SCSI_MAX_CMDS (256)
305 #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
308 * Zoned block device models (zoned limit).
310 enum blk_zoned_model {
311 BLK_ZONED_NONE, /* Regular block device */
312 BLK_ZONED_HA, /* Host-aware zoned block device */
313 BLK_ZONED_HM, /* Host-managed zoned block device */
316 struct queue_limits {
317 unsigned long bounce_pfn;
318 unsigned long seg_boundary_mask;
319 unsigned long virt_boundary_mask;
321 unsigned int max_hw_sectors;
322 unsigned int max_dev_sectors;
323 unsigned int chunk_sectors;
324 unsigned int max_sectors;
325 unsigned int max_segment_size;
326 unsigned int physical_block_size;
327 unsigned int logical_block_size;
328 unsigned int alignment_offset;
331 unsigned int max_discard_sectors;
332 unsigned int max_hw_discard_sectors;
333 unsigned int max_write_same_sectors;
334 unsigned int max_write_zeroes_sectors;
335 unsigned int max_zone_append_sectors;
336 unsigned int discard_granularity;
337 unsigned int discard_alignment;
339 unsigned short max_segments;
340 unsigned short max_integrity_segments;
341 unsigned short max_discard_segments;
343 unsigned char misaligned;
344 unsigned char discard_misaligned;
345 unsigned char raid_partial_stripes_expensive;
346 enum blk_zoned_model zoned;
349 typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
352 #ifdef CONFIG_BLK_DEV_ZONED
354 #define BLK_ALL_ZONES ((unsigned int)-1)
355 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
356 unsigned int nr_zones, report_zones_cb cb, void *data);
357 unsigned int blkdev_nr_zones(struct gendisk *disk);
358 extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
359 sector_t sectors, sector_t nr_sectors,
361 int blk_revalidate_disk_zones(struct gendisk *disk,
362 void (*update_driver_data)(struct gendisk *disk));
364 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
365 unsigned int cmd, unsigned long arg);
366 extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
367 unsigned int cmd, unsigned long arg);
369 #else /* CONFIG_BLK_DEV_ZONED */
371 static inline unsigned int blkdev_nr_zones(struct gendisk *disk)
376 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
377 fmode_t mode, unsigned int cmd,
383 static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
384 fmode_t mode, unsigned int cmd,
390 #endif /* CONFIG_BLK_DEV_ZONED */
392 struct request_queue {
393 struct request *last_merge;
394 struct elevator_queue *elevator;
396 struct blk_queue_stats *stats;
397 struct rq_qos *rq_qos;
399 make_request_fn *make_request_fn;
401 const struct blk_mq_ops *mq_ops;
404 struct blk_mq_ctx __percpu *queue_ctx;
406 unsigned int queue_depth;
408 /* hw dispatch queues */
409 struct blk_mq_hw_ctx **queue_hw_ctx;
410 unsigned int nr_hw_queues;
412 struct backing_dev_info *backing_dev_info;
415 * The queue owner gets to use this for whatever they like.
416 * ll_rw_blk doesn't touch it.
421 * various queue flags, see QUEUE_* below
423 unsigned long queue_flags;
425 * Number of contexts that have called blk_set_pm_only(). If this
426 * counter is above zero then only RQF_PM and RQF_PREEMPT requests are
432 * ida allocated id for this queue. Used to index queues from
438 * queue needs bounce pages for pages above this limit
442 spinlock_t queue_lock;
452 struct kobject *mq_kobj;
454 #ifdef CONFIG_BLK_DEV_INTEGRITY
455 struct blk_integrity integrity;
456 #endif /* CONFIG_BLK_DEV_INTEGRITY */
461 unsigned int nr_pending;
467 unsigned long nr_requests; /* Max # of requests */
469 unsigned int dma_pad_mask;
470 unsigned int dma_alignment;
472 unsigned int rq_timeout;
475 struct blk_stat_callback *poll_cb;
476 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
478 struct timer_list timeout;
479 struct work_struct timeout_work;
481 struct list_head icq_list;
482 #ifdef CONFIG_BLK_CGROUP
483 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
484 struct blkcg_gq *root_blkg;
485 struct list_head blkg_list;
488 struct queue_limits limits;
490 unsigned int required_elevator_features;
492 #ifdef CONFIG_BLK_DEV_ZONED
494 * Zoned block device information for request dispatch control.
495 * nr_zones is the total number of zones of the device. This is always
496 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
497 * bits which indicates if a zone is conventional (bit set) or
498 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
499 * bits which indicates if a zone is write locked, that is, if a write
500 * request targeting the zone was dispatched. All three fields are
501 * initialized by the low level device driver (e.g. scsi/sd.c).
502 * Stacking drivers (device mappers) may or may not initialize
505 * Reads of this information must be protected with blk_queue_enter() /
506 * blk_queue_exit(). Modifying this information is only allowed while
507 * no requests are being processed. See also blk_mq_freeze_queue() and
508 * blk_mq_unfreeze_queue().
510 unsigned int nr_zones;
511 unsigned long *conv_zones_bitmap;
512 unsigned long *seq_zones_wlock;
513 #endif /* CONFIG_BLK_DEV_ZONED */
518 unsigned int sg_timeout;
519 unsigned int sg_reserved_size;
521 #ifdef CONFIG_BLK_DEV_IO_TRACE
522 struct blk_trace __rcu *blk_trace;
523 struct mutex blk_trace_mutex;
526 * for flush operations
528 struct blk_flush_queue *fq;
530 struct list_head requeue_list;
531 spinlock_t requeue_lock;
532 struct delayed_work requeue_work;
534 struct mutex sysfs_lock;
535 struct mutex sysfs_dir_lock;
538 * for reusing dead hctx instance in case of updating
541 struct list_head unused_hctx_list;
542 spinlock_t unused_hctx_lock;
546 #if defined(CONFIG_BLK_DEV_BSG)
547 struct bsg_class_device bsg_dev;
550 #ifdef CONFIG_BLK_DEV_THROTTLING
552 struct throtl_data *td;
554 struct rcu_head rcu_head;
555 wait_queue_head_t mq_freeze_wq;
557 * Protect concurrent access to q_usage_counter by
558 * percpu_ref_kill() and percpu_ref_reinit().
560 struct mutex mq_freeze_lock;
561 struct percpu_ref q_usage_counter;
563 struct blk_mq_tag_set *tag_set;
564 struct list_head tag_set_list;
565 struct bio_set bio_split;
567 #ifdef CONFIG_BLK_DEBUG_FS
568 struct dentry *debugfs_dir;
569 struct dentry *sched_debugfs_dir;
570 struct dentry *rqos_debugfs_dir;
573 bool mq_sysfs_init_done;
577 struct work_struct release_work;
579 #define BLK_MAX_WRITE_HINTS 5
580 u64 write_hints[BLK_MAX_WRITE_HINTS];
583 #define QUEUE_FLAG_STOPPED 0 /* queue is stopped */
584 #define QUEUE_FLAG_DYING 1 /* queue being torn down */
585 #define QUEUE_FLAG_NOMERGES 3 /* disable merge attempts */
586 #define QUEUE_FLAG_SAME_COMP 4 /* complete on same CPU-group */
587 #define QUEUE_FLAG_FAIL_IO 5 /* fake timeout */
588 #define QUEUE_FLAG_NONROT 6 /* non-rotational device (SSD) */
589 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
590 #define QUEUE_FLAG_IO_STAT 7 /* do disk/partitions IO accounting */
591 #define QUEUE_FLAG_DISCARD 8 /* supports DISCARD */
592 #define QUEUE_FLAG_NOXMERGES 9 /* No extended merges */
593 #define QUEUE_FLAG_ADD_RANDOM 10 /* Contributes to random pool */
594 #define QUEUE_FLAG_SECERASE 11 /* supports secure erase */
595 #define QUEUE_FLAG_SAME_FORCE 12 /* force complete on same CPU */
596 #define QUEUE_FLAG_DEAD 13 /* queue tear-down finished */
597 #define QUEUE_FLAG_INIT_DONE 14 /* queue is initialized */
598 #define QUEUE_FLAG_POLL 16 /* IO polling enabled if set */
599 #define QUEUE_FLAG_WC 17 /* Write back caching */
600 #define QUEUE_FLAG_FUA 18 /* device supports FUA writes */
601 #define QUEUE_FLAG_DAX 19 /* device supports DAX */
602 #define QUEUE_FLAG_STATS 20 /* track IO start and completion times */
603 #define QUEUE_FLAG_POLL_STATS 21 /* collecting stats for hybrid polling */
604 #define QUEUE_FLAG_REGISTERED 22 /* queue has been registered to a disk */
605 #define QUEUE_FLAG_SCSI_PASSTHROUGH 23 /* queue supports SCSI commands */
606 #define QUEUE_FLAG_QUIESCED 24 /* queue has been quiesced */
607 #define QUEUE_FLAG_PCI_P2PDMA 25 /* device supports PCI p2p requests */
608 #define QUEUE_FLAG_ZONE_RESETALL 26 /* supports Zone Reset All */
609 #define QUEUE_FLAG_RQ_ALLOC_TIME 27 /* record rq->alloc_time_ns */
611 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
612 (1 << QUEUE_FLAG_SAME_COMP))
614 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
615 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
616 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
618 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
619 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
620 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
621 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
622 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
623 #define blk_queue_noxmerges(q) \
624 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
625 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
626 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
627 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
628 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
629 #define blk_queue_zone_resetall(q) \
630 test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
631 #define blk_queue_secure_erase(q) \
632 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
633 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
634 #define blk_queue_scsi_passthrough(q) \
635 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
636 #define blk_queue_pci_p2pdma(q) \
637 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
638 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
639 #define blk_queue_rq_alloc_time(q) \
640 test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
642 #define blk_queue_rq_alloc_time(q) false
645 #define blk_noretry_request(rq) \
646 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
647 REQ_FAILFAST_DRIVER))
648 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
649 #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
650 #define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
651 #define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
653 extern void blk_set_pm_only(struct request_queue *q);
654 extern void blk_clear_pm_only(struct request_queue *q);
656 static inline bool blk_account_rq(struct request *rq)
658 return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
661 #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
663 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
665 #define rq_dma_dir(rq) \
666 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
668 #define dma_map_bvec(dev, bv, dir, attrs) \
669 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
672 static inline bool queue_is_mq(struct request_queue *q)
677 static inline enum blk_zoned_model
678 blk_queue_zoned_model(struct request_queue *q)
680 return q->limits.zoned;
683 static inline bool blk_queue_is_zoned(struct request_queue *q)
685 switch (blk_queue_zoned_model(q)) {
694 static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
696 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
699 #ifdef CONFIG_BLK_DEV_ZONED
700 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
702 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
705 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
708 if (!blk_queue_is_zoned(q))
710 return sector >> ilog2(q->limits.chunk_sectors);
713 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
716 if (!blk_queue_is_zoned(q))
718 if (!q->conv_zones_bitmap)
720 return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
722 #else /* CONFIG_BLK_DEV_ZONED */
723 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
727 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
732 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
737 #endif /* CONFIG_BLK_DEV_ZONED */
739 static inline bool rq_is_sync(struct request *rq)
741 return op_is_sync(rq->cmd_flags);
744 static inline bool rq_mergeable(struct request *rq)
746 if (blk_rq_is_passthrough(rq))
749 if (req_op(rq) == REQ_OP_FLUSH)
752 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
755 if (req_op(rq) == REQ_OP_ZONE_APPEND)
758 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
760 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
766 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
768 if (bio_page(a) == bio_page(b) &&
769 bio_offset(a) == bio_offset(b))
775 static inline unsigned int blk_queue_depth(struct request_queue *q)
778 return q->queue_depth;
780 return q->nr_requests;
783 extern unsigned long blk_max_low_pfn, blk_max_pfn;
786 * standard bounce addresses:
788 * BLK_BOUNCE_HIGH : bounce all highmem pages
789 * BLK_BOUNCE_ANY : don't bounce anything
790 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
793 #if BITS_PER_LONG == 32
794 #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
796 #define BLK_BOUNCE_HIGH -1ULL
798 #define BLK_BOUNCE_ANY (-1ULL)
799 #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
802 * default timeout for SG_IO if none specified
804 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
805 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
811 unsigned long offset;
816 struct req_iterator {
817 struct bvec_iter iter;
821 /* This should not be used directly - use rq_for_each_segment */
822 #define for_each_bio(_bio) \
823 for (; _bio; _bio = _bio->bi_next)
824 #define __rq_for_each_bio(_bio, rq) \
826 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
828 #define rq_for_each_segment(bvl, _rq, _iter) \
829 __rq_for_each_bio(_iter.bio, _rq) \
830 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
832 #define rq_for_each_bvec(bvl, _rq, _iter) \
833 __rq_for_each_bio(_iter.bio, _rq) \
834 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
836 #define rq_iter_last(bvec, _iter) \
837 (_iter.bio->bi_next == NULL && \
838 bio_iter_last(bvec, _iter.iter))
840 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
841 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
843 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
844 extern void rq_flush_dcache_pages(struct request *rq);
846 static inline void rq_flush_dcache_pages(struct request *rq)
851 extern int blk_register_queue(struct gendisk *disk);
852 extern void blk_unregister_queue(struct gendisk *disk);
853 extern blk_qc_t generic_make_request(struct bio *bio);
854 extern blk_qc_t direct_make_request(struct bio *bio);
855 extern void blk_rq_init(struct request_queue *q, struct request *rq);
856 extern void blk_put_request(struct request *);
857 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
858 blk_mq_req_flags_t flags);
859 extern int blk_lld_busy(struct request_queue *q);
860 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
861 struct bio_set *bs, gfp_t gfp_mask,
862 int (*bio_ctr)(struct bio *, struct bio *, void *),
864 extern void blk_rq_unprep_clone(struct request *rq);
865 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
867 extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
868 extern void blk_queue_split(struct request_queue *, struct bio **);
869 extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
870 extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
871 unsigned int, void __user *);
872 extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
873 unsigned int, void __user *);
874 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
875 struct scsi_ioctl_command __user *);
876 extern int get_sg_io_hdr(struct sg_io_hdr *hdr, const void __user *argp);
877 extern int put_sg_io_hdr(const struct sg_io_hdr *hdr, void __user *argp);
879 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
880 extern void blk_queue_exit(struct request_queue *q);
881 extern void blk_sync_queue(struct request_queue *q);
882 extern int blk_rq_map_user(struct request_queue *, struct request *,
883 struct rq_map_data *, void __user *, unsigned long,
885 extern int blk_rq_unmap_user(struct bio *);
886 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
887 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
888 struct rq_map_data *, const struct iov_iter *,
890 extern void blk_execute_rq(struct request_queue *, struct gendisk *,
891 struct request *, int);
892 extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
893 struct request *, int, rq_end_io_fn *);
895 /* Helper to convert REQ_OP_XXX to its string format XXX */
896 extern const char *blk_op_str(unsigned int op);
898 int blk_status_to_errno(blk_status_t status);
899 blk_status_t errno_to_blk_status(int errno);
901 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
903 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
905 return bdev->bd_disk->queue; /* this is never NULL */
909 * The basic unit of block I/O is a sector. It is used in a number of contexts
910 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
911 * bytes. Variables of type sector_t represent an offset or size that is a
912 * multiple of 512 bytes. Hence these two constants.
915 #define SECTOR_SHIFT 9
918 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
922 * blk_rq_pos() : the current sector
923 * blk_rq_bytes() : bytes left in the entire request
924 * blk_rq_cur_bytes() : bytes left in the current segment
925 * blk_rq_err_bytes() : bytes left till the next error boundary
926 * blk_rq_sectors() : sectors left in the entire request
927 * blk_rq_cur_sectors() : sectors left in the current segment
928 * blk_rq_stats_sectors() : sectors of the entire request used for stats
930 static inline sector_t blk_rq_pos(const struct request *rq)
935 static inline unsigned int blk_rq_bytes(const struct request *rq)
937 return rq->__data_len;
940 static inline int blk_rq_cur_bytes(const struct request *rq)
942 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
945 extern unsigned int blk_rq_err_bytes(const struct request *rq);
947 static inline unsigned int blk_rq_sectors(const struct request *rq)
949 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
952 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
954 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
957 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
959 return rq->stats_sectors;
962 #ifdef CONFIG_BLK_DEV_ZONED
964 /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
965 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
967 static inline unsigned int blk_rq_zone_no(struct request *rq)
969 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
972 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
974 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
976 #endif /* CONFIG_BLK_DEV_ZONED */
979 * Some commands like WRITE SAME have a payload or data transfer size which
980 * is different from the size of the request. Any driver that supports such
981 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
982 * calculate the data transfer size.
984 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
986 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
987 return rq->special_vec.bv_len;
988 return blk_rq_bytes(rq);
992 * Return the first full biovec in the request. The caller needs to check that
993 * there are any bvecs before calling this helper.
995 static inline struct bio_vec req_bvec(struct request *rq)
997 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
998 return rq->special_vec;
999 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1002 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1005 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1006 return min(q->limits.max_discard_sectors,
1007 UINT_MAX >> SECTOR_SHIFT);
1009 if (unlikely(op == REQ_OP_WRITE_SAME))
1010 return q->limits.max_write_same_sectors;
1012 if (unlikely(op == REQ_OP_WRITE_ZEROES))
1013 return q->limits.max_write_zeroes_sectors;
1015 return q->limits.max_sectors;
1019 * Return maximum size of a request at given offset. Only valid for
1020 * file system requests.
1022 static inline unsigned int blk_max_size_offset(struct request_queue *q,
1025 if (!q->limits.chunk_sectors)
1026 return q->limits.max_sectors;
1028 return min(q->limits.max_sectors, (unsigned int)(q->limits.chunk_sectors -
1029 (offset & (q->limits.chunk_sectors - 1))));
1032 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1035 struct request_queue *q = rq->q;
1037 if (blk_rq_is_passthrough(rq))
1038 return q->limits.max_hw_sectors;
1040 if (!q->limits.chunk_sectors ||
1041 req_op(rq) == REQ_OP_DISCARD ||
1042 req_op(rq) == REQ_OP_SECURE_ERASE)
1043 return blk_queue_get_max_sectors(q, req_op(rq));
1045 return min(blk_max_size_offset(q, offset),
1046 blk_queue_get_max_sectors(q, req_op(rq)));
1049 static inline unsigned int blk_rq_count_bios(struct request *rq)
1051 unsigned int nr_bios = 0;
1054 __rq_for_each_bio(bio, rq)
1060 void blk_steal_bios(struct bio_list *list, struct request *rq);
1063 * Request completion related functions.
1065 * blk_update_request() completes given number of bytes and updates
1066 * the request without completing it.
1068 extern bool blk_update_request(struct request *rq, blk_status_t error,
1069 unsigned int nr_bytes);
1071 extern void __blk_complete_request(struct request *);
1072 extern void blk_abort_request(struct request *);
1075 * Access functions for manipulating queue properties
1077 extern void blk_cleanup_queue(struct request_queue *);
1078 extern void blk_queue_bounce_limit(struct request_queue *, u64);
1079 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1080 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1081 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1082 extern void blk_queue_max_discard_segments(struct request_queue *,
1084 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1085 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1086 unsigned int max_discard_sectors);
1087 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1088 unsigned int max_write_same_sectors);
1089 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1090 unsigned int max_write_same_sectors);
1091 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
1092 extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
1093 unsigned int max_zone_append_sectors);
1094 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1095 extern void blk_queue_alignment_offset(struct request_queue *q,
1096 unsigned int alignment);
1097 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1098 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1099 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1100 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1101 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1102 extern void blk_set_default_limits(struct queue_limits *lim);
1103 extern void blk_set_stacking_limits(struct queue_limits *lim);
1104 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1106 extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
1108 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1110 extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
1111 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1112 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1113 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1114 extern void blk_queue_dma_alignment(struct request_queue *, int);
1115 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1116 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1117 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1118 extern void blk_queue_required_elevator_features(struct request_queue *q,
1119 unsigned int features);
1120 extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
1121 struct device *dev);
1124 * Number of physical segments as sent to the device.
1126 * Normally this is the number of discontiguous data segments sent by the
1127 * submitter. But for data-less command like discard we might have no
1128 * actual data segments submitted, but the driver might have to add it's
1129 * own special payload. In that case we still return 1 here so that this
1130 * special payload will be mapped.
1132 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1134 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1136 return rq->nr_phys_segments;
1140 * Number of discard segments (or ranges) the driver needs to fill in.
1141 * Each discard bio merged into a request is counted as one segment.
1143 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1145 return max_t(unsigned short, rq->nr_phys_segments, 1);
1148 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1149 struct scatterlist *sglist, struct scatterlist **last_sg);
1150 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1151 struct scatterlist *sglist)
1153 struct scatterlist *last_sg = NULL;
1155 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1157 extern void blk_dump_rq_flags(struct request *, char *);
1158 extern long nr_blockdev_pages(void);
1160 bool __must_check blk_get_queue(struct request_queue *);
1161 struct request_queue *blk_alloc_queue(make_request_fn make_request, int node_id);
1162 extern void blk_put_queue(struct request_queue *);
1163 extern void blk_set_queue_dying(struct request_queue *);
1166 * blk_plug permits building a queue of related requests by holding the I/O
1167 * fragments for a short period. This allows merging of sequential requests
1168 * into single larger request. As the requests are moved from a per-task list to
1169 * the device's request_queue in a batch, this results in improved scalability
1170 * as the lock contention for request_queue lock is reduced.
1172 * It is ok not to disable preemption when adding the request to the plug list
1173 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1174 * the plug list when the task sleeps by itself. For details, please see
1175 * schedule() where blk_schedule_flush_plug() is called.
1178 struct list_head mq_list; /* blk-mq requests */
1179 struct list_head cb_list; /* md requires an unplug callback */
1180 unsigned short rq_count;
1181 bool multiple_queues;
1183 #define BLK_MAX_REQUEST_COUNT 16
1184 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1187 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1188 struct blk_plug_cb {
1189 struct list_head list;
1190 blk_plug_cb_fn callback;
1193 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1194 void *data, int size);
1195 extern void blk_start_plug(struct blk_plug *);
1196 extern void blk_finish_plug(struct blk_plug *);
1197 extern void blk_flush_plug_list(struct blk_plug *, bool);
1199 static inline void blk_flush_plug(struct task_struct *tsk)
1201 struct blk_plug *plug = tsk->plug;
1204 blk_flush_plug_list(plug, false);
1207 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1209 struct blk_plug *plug = tsk->plug;
1212 blk_flush_plug_list(plug, true);
1215 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1217 struct blk_plug *plug = tsk->plug;
1220 (!list_empty(&plug->mq_list) ||
1221 !list_empty(&plug->cb_list));
1224 extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
1225 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1226 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1228 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1230 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1231 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1232 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1233 sector_t nr_sects, gfp_t gfp_mask, int flags,
1236 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1237 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1239 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1240 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1242 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1243 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1245 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1246 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1248 return blkdev_issue_discard(sb->s_bdev,
1249 block << (sb->s_blocksize_bits -
1251 nr_blocks << (sb->s_blocksize_bits -
1255 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1256 sector_t nr_blocks, gfp_t gfp_mask)
1258 return blkdev_issue_zeroout(sb->s_bdev,
1259 block << (sb->s_blocksize_bits -
1261 nr_blocks << (sb->s_blocksize_bits -
1266 extern int blk_verify_command(unsigned char *cmd, fmode_t mode);
1268 enum blk_default_limits {
1269 BLK_MAX_SEGMENTS = 128,
1270 BLK_SAFE_MAX_SECTORS = 255,
1271 BLK_DEF_MAX_SECTORS = 2560,
1272 BLK_MAX_SEGMENT_SIZE = 65536,
1273 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1276 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1278 return q->limits.seg_boundary_mask;
1281 static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1283 return q->limits.virt_boundary_mask;
1286 static inline unsigned int queue_max_sectors(const struct request_queue *q)
1288 return q->limits.max_sectors;
1291 static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1293 return q->limits.max_hw_sectors;
1296 static inline unsigned short queue_max_segments(const struct request_queue *q)
1298 return q->limits.max_segments;
1301 static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1303 return q->limits.max_discard_segments;
1306 static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1308 return q->limits.max_segment_size;
1311 static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
1313 return q->limits.max_zone_append_sectors;
1316 static inline unsigned queue_logical_block_size(const struct request_queue *q)
1320 if (q && q->limits.logical_block_size)
1321 retval = q->limits.logical_block_size;
1326 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1328 return queue_logical_block_size(bdev_get_queue(bdev));
1331 static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1333 return q->limits.physical_block_size;
1336 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1338 return queue_physical_block_size(bdev_get_queue(bdev));
1341 static inline unsigned int queue_io_min(const struct request_queue *q)
1343 return q->limits.io_min;
1346 static inline int bdev_io_min(struct block_device *bdev)
1348 return queue_io_min(bdev_get_queue(bdev));
1351 static inline unsigned int queue_io_opt(const struct request_queue *q)
1353 return q->limits.io_opt;
1356 static inline int bdev_io_opt(struct block_device *bdev)
1358 return queue_io_opt(bdev_get_queue(bdev));
1361 static inline int queue_alignment_offset(const struct request_queue *q)
1363 if (q->limits.misaligned)
1366 return q->limits.alignment_offset;
1369 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1371 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1372 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1375 return (granularity + lim->alignment_offset - alignment) % granularity;
1378 static inline int bdev_alignment_offset(struct block_device *bdev)
1380 struct request_queue *q = bdev_get_queue(bdev);
1382 if (q->limits.misaligned)
1385 if (bdev != bdev->bd_contains)
1386 return bdev->bd_part->alignment_offset;
1388 return q->limits.alignment_offset;
1391 static inline int queue_discard_alignment(const struct request_queue *q)
1393 if (q->limits.discard_misaligned)
1396 return q->limits.discard_alignment;
1399 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1401 unsigned int alignment, granularity, offset;
1403 if (!lim->max_discard_sectors)
1406 /* Why are these in bytes, not sectors? */
1407 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1408 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1412 /* Offset of the partition start in 'granularity' sectors */
1413 offset = sector_div(sector, granularity);
1415 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1416 offset = (granularity + alignment - offset) % granularity;
1418 /* Turn it back into bytes, gaah */
1419 return offset << SECTOR_SHIFT;
1422 static inline int bdev_discard_alignment(struct block_device *bdev)
1424 struct request_queue *q = bdev_get_queue(bdev);
1426 if (bdev != bdev->bd_contains)
1427 return bdev->bd_part->discard_alignment;
1429 return q->limits.discard_alignment;
1432 static inline unsigned int bdev_write_same(struct block_device *bdev)
1434 struct request_queue *q = bdev_get_queue(bdev);
1437 return q->limits.max_write_same_sectors;
1442 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1444 struct request_queue *q = bdev_get_queue(bdev);
1447 return q->limits.max_write_zeroes_sectors;
1452 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1454 struct request_queue *q = bdev_get_queue(bdev);
1457 return blk_queue_zoned_model(q);
1459 return BLK_ZONED_NONE;
1462 static inline bool bdev_is_zoned(struct block_device *bdev)
1464 struct request_queue *q = bdev_get_queue(bdev);
1467 return blk_queue_is_zoned(q);
1472 static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1474 struct request_queue *q = bdev_get_queue(bdev);
1477 return blk_queue_zone_sectors(q);
1481 static inline int queue_dma_alignment(const struct request_queue *q)
1483 return q ? q->dma_alignment : 511;
1486 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1489 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1490 return !(addr & alignment) && !(len & alignment);
1493 /* assumes size > 256 */
1494 static inline unsigned int blksize_bits(unsigned int size)
1496 unsigned int bits = 8;
1500 } while (size > 256);
1504 static inline unsigned int block_size(struct block_device *bdev)
1506 return bdev->bd_block_size;
1509 int kblockd_schedule_work(struct work_struct *work);
1510 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1512 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1513 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1514 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1515 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1517 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1519 enum blk_integrity_flags {
1520 BLK_INTEGRITY_VERIFY = 1 << 0,
1521 BLK_INTEGRITY_GENERATE = 1 << 1,
1522 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1523 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1526 struct blk_integrity_iter {
1530 unsigned int data_size;
1531 unsigned short interval;
1532 const char *disk_name;
1535 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1536 typedef void (integrity_prepare_fn) (struct request *);
1537 typedef void (integrity_complete_fn) (struct request *, unsigned int);
1539 struct blk_integrity_profile {
1540 integrity_processing_fn *generate_fn;
1541 integrity_processing_fn *verify_fn;
1542 integrity_prepare_fn *prepare_fn;
1543 integrity_complete_fn *complete_fn;
1547 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1548 extern void blk_integrity_unregister(struct gendisk *);
1549 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1550 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1551 struct scatterlist *);
1552 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1553 extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
1555 extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
1558 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1560 struct blk_integrity *bi = &disk->queue->integrity;
1569 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1571 return blk_get_integrity(bdev->bd_disk);
1574 static inline bool blk_integrity_rq(struct request *rq)
1576 return rq->cmd_flags & REQ_INTEGRITY;
1579 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1582 q->limits.max_integrity_segments = segs;
1585 static inline unsigned short
1586 queue_max_integrity_segments(const struct request_queue *q)
1588 return q->limits.max_integrity_segments;
1592 * bio_integrity_intervals - Return number of integrity intervals for a bio
1593 * @bi: blk_integrity profile for device
1594 * @sectors: Size of the bio in 512-byte sectors
1596 * Description: The block layer calculates everything in 512 byte
1597 * sectors but integrity metadata is done in terms of the data integrity
1598 * interval size of the storage device. Convert the block layer sectors
1599 * to the appropriate number of integrity intervals.
1601 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1602 unsigned int sectors)
1604 return sectors >> (bi->interval_exp - 9);
1607 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1608 unsigned int sectors)
1610 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1614 * Return the first bvec that contains integrity data. Only drivers that are
1615 * limited to a single integrity segment should use this helper.
1617 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1619 if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
1621 return rq->bio->bi_integrity->bip_vec;
1624 #else /* CONFIG_BLK_DEV_INTEGRITY */
1627 struct block_device;
1629 struct blk_integrity;
1631 static inline int blk_integrity_rq(struct request *rq)
1635 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1640 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1642 struct scatterlist *s)
1646 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1650 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1654 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1658 static inline void blk_integrity_register(struct gendisk *d,
1659 struct blk_integrity *b)
1662 static inline void blk_integrity_unregister(struct gendisk *d)
1665 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1669 static inline unsigned short queue_max_integrity_segments(const struct request_queue *q)
1673 static inline bool blk_integrity_merge_rq(struct request_queue *rq,
1679 static inline bool blk_integrity_merge_bio(struct request_queue *rq,
1686 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1687 unsigned int sectors)
1692 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1693 unsigned int sectors)
1698 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1703 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1705 struct block_device_operations {
1706 int (*open) (struct block_device *, fmode_t);
1707 void (*release) (struct gendisk *, fmode_t);
1708 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1709 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1710 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1711 unsigned int (*check_events) (struct gendisk *disk,
1712 unsigned int clearing);
1713 /* ->media_changed() is DEPRECATED, use ->check_events() instead */
1714 int (*media_changed) (struct gendisk *);
1715 void (*unlock_native_capacity) (struct gendisk *);
1716 int (*revalidate_disk) (struct gendisk *);
1717 int (*getgeo)(struct block_device *, struct hd_geometry *);
1718 /* this callback is with swap_lock and sometimes page table lock held */
1719 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1720 int (*report_zones)(struct gendisk *, sector_t sector,
1721 unsigned int nr_zones, report_zones_cb cb, void *data);
1722 char *(*devnode)(struct gendisk *disk, umode_t *mode);
1723 struct module *owner;
1724 const struct pr_ops *pr_ops;
1727 #ifdef CONFIG_COMPAT
1728 extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1729 unsigned int, unsigned long);
1731 #define blkdev_compat_ptr_ioctl NULL
1734 extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
1736 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1737 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1738 struct writeback_control *);
1740 #ifdef CONFIG_BLK_DEV_ZONED
1741 bool blk_req_needs_zone_write_lock(struct request *rq);
1742 bool blk_req_zone_write_trylock(struct request *rq);
1743 void __blk_req_zone_write_lock(struct request *rq);
1744 void __blk_req_zone_write_unlock(struct request *rq);
1746 static inline void blk_req_zone_write_lock(struct request *rq)
1748 if (blk_req_needs_zone_write_lock(rq))
1749 __blk_req_zone_write_lock(rq);
1752 static inline void blk_req_zone_write_unlock(struct request *rq)
1754 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1755 __blk_req_zone_write_unlock(rq);
1758 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1760 return rq->q->seq_zones_wlock &&
1761 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1764 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1766 if (!blk_req_needs_zone_write_lock(rq))
1768 return !blk_req_zone_is_write_locked(rq);
1771 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1776 static inline void blk_req_zone_write_lock(struct request *rq)
1780 static inline void blk_req_zone_write_unlock(struct request *rq)
1783 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1788 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1792 #endif /* CONFIG_BLK_DEV_ZONED */
1794 #else /* CONFIG_BLOCK */
1796 struct block_device;
1799 * stubs for when the block layer is configured out
1801 #define buffer_heads_over_limit 0
1803 static inline long nr_blockdev_pages(void)
1811 static inline void blk_start_plug(struct blk_plug *plug)
1815 static inline void blk_finish_plug(struct blk_plug *plug)
1819 static inline void blk_flush_plug(struct task_struct *task)
1823 static inline void blk_schedule_flush_plug(struct task_struct *task)
1828 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1833 static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
1834 sector_t *error_sector)
1839 #endif /* CONFIG_BLOCK */
1841 static inline void blk_wake_io_task(struct task_struct *waiter)
1844 * If we're polling, the task itself is doing the completions. For
1845 * that case, we don't need to signal a wakeup, it's enough to just
1846 * mark us as RUNNING.
1848 if (waiter == current)
1849 __set_current_state(TASK_RUNNING);
1851 wake_up_process(waiter);
1854 static inline void blk_io_schedule(void)
1856 /* Prevent hang_check timer from firing at us during very long I/O */
1857 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
1860 io_schedule_timeout(timeout);