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 consistent with blk_mq_poll_stats_bkt() */
51 #define BLK_MQ_POLL_STATS_BKTS 16
53 /* Doing classic polling */
54 #define BLK_MQ_POLL_CLASSIC -1
57 * Maximum number of blkcg policies allowed to be registered concurrently.
58 * Defined here to simplify include dependency.
60 #define BLKCG_MAX_POLS 5
62 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
66 typedef __u32 __bitwise req_flags_t;
68 /* elevator knows about this request */
69 #define RQF_SORTED ((__force req_flags_t)(1 << 0))
70 /* drive already may have started this one */
71 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
72 /* may not be passed by ioscheduler */
73 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
74 /* request for flush sequence */
75 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
76 /* merge of different types, fail separately */
77 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
78 /* track inflight for MQ */
79 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
80 /* don't call prep for this one */
81 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
82 /* set for "ide_preempt" requests and also for requests for which the SCSI
83 "quiesce" state must be ignored. */
84 #define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
85 /* contains copies of user pages */
86 #define RQF_COPY_USER ((__force req_flags_t)(1 << 9))
87 /* vaguely specified driver internal error. Ignored by the block layer */
88 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
89 /* don't warn about errors */
90 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
91 /* elevator private data attached */
92 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
93 /* account into disk and partition IO statistics */
94 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
95 /* request came from our alloc pool */
96 #define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
97 /* runtime pm request */
98 #define RQF_PM ((__force req_flags_t)(1 << 15))
99 /* on IO scheduler merge hash */
100 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
101 /* track IO completion time */
102 #define RQF_STATS ((__force req_flags_t)(1 << 17))
103 /* Look at ->special_vec for the actual data payload instead of the
105 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
106 /* The per-zone write lock is held for this request */
107 #define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
108 /* already slept for hybrid poll */
109 #define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
110 /* ->timeout has been called, don't expire again */
111 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
113 /* flags that prevent us from merging requests: */
114 #define RQF_NOMERGE_FLAGS \
115 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
118 * Request state for blk-mq.
127 * Try to put the fields that are referenced together in the same cacheline.
129 * If you modify this structure, make sure to update blk_rq_init() and
130 * especially blk_mq_rq_ctx_init() to take care of the added fields.
133 struct request_queue *q;
134 struct blk_mq_ctx *mq_ctx;
135 struct blk_mq_hw_ctx *mq_hctx;
137 unsigned int cmd_flags; /* op and common flags */
138 req_flags_t rq_flags;
142 /* the following two fields are internal, NEVER access directly */
143 unsigned int __data_len; /* total data len */
145 sector_t __sector; /* sector cursor */
150 struct list_head queuelist;
153 * The hash is used inside the scheduler, and killed once the
154 * request reaches the dispatch list. The ipi_list is only used
155 * to queue the request for softirq completion, which is long
156 * after the request has been unhashed (and even removed from
157 * the dispatch list).
160 struct hlist_node hash; /* merge hash */
161 struct list_head ipi_list;
165 * The rb_node is only used inside the io scheduler, requests
166 * are pruned when moved to the dispatch queue. So let the
167 * completion_data share space with the rb_node.
170 struct rb_node rb_node; /* sort/lookup */
171 struct bio_vec special_vec;
172 void *completion_data;
173 int error_count; /* for legacy drivers, don't use */
177 * Three pointers are available for the IO schedulers, if they need
178 * more they have to dynamically allocate it. Flush requests are
179 * never put on the IO scheduler. So let the flush fields share
180 * space with the elevator data.
190 struct list_head list;
191 rq_end_io_fn *saved_end_io;
195 struct gendisk *rq_disk;
196 struct hd_struct *part;
197 /* Time that I/O was submitted to the kernel. */
199 /* Time that I/O was submitted to the device. */
200 u64 io_start_time_ns;
202 #ifdef CONFIG_BLK_WBT
203 unsigned short wbt_flags;
205 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
206 unsigned short throtl_size;
210 * Number of scatter-gather DMA addr+len pairs after
211 * physical address coalescing is performed.
213 unsigned short nr_phys_segments;
215 #if defined(CONFIG_BLK_DEV_INTEGRITY)
216 unsigned short nr_integrity_segments;
219 unsigned short write_hint;
220 unsigned short ioprio;
222 unsigned int extra_len; /* length of alignment and padding */
224 enum mq_rq_state state;
227 unsigned int timeout;
228 unsigned long deadline;
231 struct __call_single_data csd;
236 * completion callback.
238 rq_end_io_fn *end_io;
242 static inline bool blk_op_is_scsi(unsigned int op)
244 return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
247 static inline bool blk_op_is_private(unsigned int op)
249 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
252 static inline bool blk_rq_is_scsi(struct request *rq)
254 return blk_op_is_scsi(req_op(rq));
257 static inline bool blk_rq_is_private(struct request *rq)
259 return blk_op_is_private(req_op(rq));
262 static inline bool blk_rq_is_passthrough(struct request *rq)
264 return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
267 static inline bool bio_is_passthrough(struct bio *bio)
269 unsigned op = bio_op(bio);
271 return blk_op_is_scsi(op) || blk_op_is_private(op);
274 static inline unsigned short req_get_ioprio(struct request *req)
279 #include <linux/elevator.h>
281 struct blk_queue_ctx;
283 typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
286 typedef int (dma_drain_needed_fn)(struct request *);
288 enum blk_eh_timer_return {
289 BLK_EH_DONE, /* drivers has completed the command */
290 BLK_EH_RESET_TIMER, /* reset timer and try again */
293 enum blk_queue_state {
298 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
299 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
301 #define BLK_SCSI_MAX_CMDS (256)
302 #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
305 * Zoned block device models (zoned limit).
307 enum blk_zoned_model {
308 BLK_ZONED_NONE, /* Regular block device */
309 BLK_ZONED_HA, /* Host-aware zoned block device */
310 BLK_ZONED_HM, /* Host-managed zoned block device */
313 struct queue_limits {
314 unsigned long bounce_pfn;
315 unsigned long seg_boundary_mask;
316 unsigned long virt_boundary_mask;
318 unsigned int max_hw_sectors;
319 unsigned int max_dev_sectors;
320 unsigned int chunk_sectors;
321 unsigned int max_sectors;
322 unsigned int max_segment_size;
323 unsigned int physical_block_size;
324 unsigned int alignment_offset;
327 unsigned int max_discard_sectors;
328 unsigned int max_hw_discard_sectors;
329 unsigned int max_write_same_sectors;
330 unsigned int max_write_zeroes_sectors;
331 unsigned int discard_granularity;
332 unsigned int discard_alignment;
334 unsigned short logical_block_size;
335 unsigned short max_segments;
336 unsigned short max_integrity_segments;
337 unsigned short max_discard_segments;
339 unsigned char misaligned;
340 unsigned char discard_misaligned;
341 unsigned char raid_partial_stripes_expensive;
342 enum blk_zoned_model zoned;
345 #ifdef CONFIG_BLK_DEV_ZONED
347 extern unsigned int blkdev_nr_zones(struct block_device *bdev);
348 extern int blkdev_report_zones(struct block_device *bdev,
349 sector_t sector, struct blk_zone *zones,
350 unsigned int *nr_zones, gfp_t gfp_mask);
351 extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors,
352 sector_t nr_sectors, gfp_t gfp_mask);
353 extern int blk_revalidate_disk_zones(struct gendisk *disk);
355 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
356 unsigned int cmd, unsigned long arg);
357 extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
358 unsigned int cmd, unsigned long arg);
360 #else /* CONFIG_BLK_DEV_ZONED */
362 static inline unsigned int blkdev_nr_zones(struct block_device *bdev)
367 static inline int blk_revalidate_disk_zones(struct gendisk *disk)
372 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
373 fmode_t mode, unsigned int cmd,
379 static inline int blkdev_reset_zones_ioctl(struct block_device *bdev,
380 fmode_t mode, unsigned int cmd,
386 #endif /* CONFIG_BLK_DEV_ZONED */
388 struct request_queue {
390 * Together with queue_head for cacheline sharing
392 struct list_head queue_head;
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;
400 dma_drain_needed_fn *dma_drain_needed;
402 const struct blk_mq_ops *mq_ops;
405 struct blk_mq_ctx __percpu *queue_ctx;
406 unsigned int nr_queues;
408 unsigned int queue_depth;
410 /* hw dispatch queues */
411 struct blk_mq_hw_ctx **queue_hw_ctx;
412 unsigned int nr_hw_queues;
414 struct backing_dev_info *backing_dev_info;
417 * The queue owner gets to use this for whatever they like.
418 * ll_rw_blk doesn't touch it.
423 * various queue flags, see QUEUE_* below
425 unsigned long queue_flags;
427 * Number of contexts that have called blk_set_pm_only(). If this
428 * counter is above zero then only RQF_PM and RQF_PREEMPT requests are
434 * ida allocated id for this queue. Used to index queues from
440 * queue needs bounce pages for pages above this limit
444 spinlock_t queue_lock;
454 struct kobject *mq_kobj;
456 #ifdef CONFIG_BLK_DEV_INTEGRITY
457 struct blk_integrity integrity;
458 #endif /* CONFIG_BLK_DEV_INTEGRITY */
463 unsigned int nr_pending;
469 unsigned long nr_requests; /* Max # of requests */
471 unsigned int dma_drain_size;
472 void *dma_drain_buffer;
473 unsigned int dma_pad_mask;
474 unsigned int dma_alignment;
476 unsigned int rq_timeout;
479 struct blk_stat_callback *poll_cb;
480 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
482 struct timer_list timeout;
483 struct work_struct timeout_work;
485 struct list_head icq_list;
486 #ifdef CONFIG_BLK_CGROUP
487 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
488 struct blkcg_gq *root_blkg;
489 struct list_head blkg_list;
492 struct queue_limits limits;
494 #ifdef CONFIG_BLK_DEV_ZONED
496 * Zoned block device information for request dispatch control.
497 * nr_zones is the total number of zones of the device. This is always
498 * 0 for regular block devices. seq_zones_bitmap is a bitmap of nr_zones
499 * bits which indicates if a zone is conventional (bit clear) or
500 * sequential (bit set). seq_zones_wlock is a bitmap of nr_zones
501 * bits which indicates if a zone is write locked, that is, if a write
502 * request targeting the zone was dispatched. All three fields are
503 * initialized by the low level device driver (e.g. scsi/sd.c).
504 * Stacking drivers (device mappers) may or may not initialize
507 * Reads of this information must be protected with blk_queue_enter() /
508 * blk_queue_exit(). Modifying this information is only allowed while
509 * no requests are being processed. See also blk_mq_freeze_queue() and
510 * blk_mq_unfreeze_queue().
512 unsigned int nr_zones;
513 unsigned long *seq_zones_bitmap;
514 unsigned long *seq_zones_wlock;
515 #endif /* CONFIG_BLK_DEV_ZONED */
520 unsigned int sg_timeout;
521 unsigned int sg_reserved_size;
523 #ifdef CONFIG_BLK_DEV_IO_TRACE
524 struct blk_trace *blk_trace;
525 struct mutex blk_trace_mutex;
528 * for flush operations
530 struct blk_flush_queue *fq;
532 struct list_head requeue_list;
533 spinlock_t requeue_lock;
534 struct delayed_work requeue_work;
536 struct mutex sysfs_lock;
539 * for reusing dead hctx instance in case of updating
542 struct list_head unused_hctx_list;
543 spinlock_t unused_hctx_lock;
547 #if defined(CONFIG_BLK_DEV_BSG)
548 struct bsg_class_device bsg_dev;
551 #ifdef CONFIG_BLK_DEV_THROTTLING
553 struct throtl_data *td;
555 struct rcu_head rcu_head;
556 wait_queue_head_t mq_freeze_wq;
558 * Protect concurrent access to q_usage_counter by
559 * percpu_ref_kill() and percpu_ref_reinit().
561 struct mutex mq_freeze_lock;
562 struct percpu_ref q_usage_counter;
564 struct blk_mq_tag_set *tag_set;
565 struct list_head tag_set_list;
566 struct bio_set bio_split;
568 #ifdef CONFIG_BLK_DEBUG_FS
569 struct dentry *debugfs_dir;
570 struct dentry *sched_debugfs_dir;
571 struct dentry *rqos_debugfs_dir;
574 bool mq_sysfs_init_done;
578 struct work_struct release_work;
580 #define BLK_MAX_WRITE_HINTS 5
581 u64 write_hints[BLK_MAX_WRITE_HINTS];
584 #define QUEUE_FLAG_STOPPED 0 /* queue is stopped */
585 #define QUEUE_FLAG_DYING 1 /* queue being torn down */
586 #define QUEUE_FLAG_NOMERGES 3 /* disable merge attempts */
587 #define QUEUE_FLAG_SAME_COMP 4 /* complete on same CPU-group */
588 #define QUEUE_FLAG_FAIL_IO 5 /* fake timeout */
589 #define QUEUE_FLAG_NONROT 6 /* non-rotational device (SSD) */
590 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
591 #define QUEUE_FLAG_IO_STAT 7 /* do disk/partitions IO accounting */
592 #define QUEUE_FLAG_DISCARD 8 /* supports DISCARD */
593 #define QUEUE_FLAG_NOXMERGES 9 /* No extended merges */
594 #define QUEUE_FLAG_ADD_RANDOM 10 /* Contributes to random pool */
595 #define QUEUE_FLAG_SECERASE 11 /* supports secure erase */
596 #define QUEUE_FLAG_SAME_FORCE 12 /* force complete on same CPU */
597 #define QUEUE_FLAG_DEAD 13 /* queue tear-down finished */
598 #define QUEUE_FLAG_INIT_DONE 14 /* queue is initialized */
599 #define QUEUE_FLAG_POLL 16 /* IO polling enabled if set */
600 #define QUEUE_FLAG_WC 17 /* Write back caching */
601 #define QUEUE_FLAG_FUA 18 /* device supports FUA writes */
602 #define QUEUE_FLAG_DAX 19 /* device supports DAX */
603 #define QUEUE_FLAG_STATS 20 /* track IO start and completion times */
604 #define QUEUE_FLAG_POLL_STATS 21 /* collecting stats for hybrid polling */
605 #define QUEUE_FLAG_REGISTERED 22 /* queue has been registered to a disk */
606 #define QUEUE_FLAG_SCSI_PASSTHROUGH 23 /* queue supports SCSI commands */
607 #define QUEUE_FLAG_QUIESCED 24 /* queue has been quiesced */
608 #define QUEUE_FLAG_PCI_P2PDMA 25 /* device supports PCI p2p requests */
610 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
611 (1 << QUEUE_FLAG_SAME_COMP))
613 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
614 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
615 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
617 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
618 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
619 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
620 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
621 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
622 #define blk_queue_noxmerges(q) \
623 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
624 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
625 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
626 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
627 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
628 #define blk_queue_secure_erase(q) \
629 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
630 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
631 #define blk_queue_scsi_passthrough(q) \
632 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
633 #define blk_queue_pci_p2pdma(q) \
634 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
636 #define blk_noretry_request(rq) \
637 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
638 REQ_FAILFAST_DRIVER))
639 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
640 #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
641 #define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
643 extern void blk_set_pm_only(struct request_queue *q);
644 extern void blk_clear_pm_only(struct request_queue *q);
646 static inline bool blk_account_rq(struct request *rq)
648 return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
651 #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
653 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
655 #define rq_dma_dir(rq) \
656 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
658 #define dma_map_bvec(dev, bv, dir, attrs) \
659 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
662 static inline bool queue_is_mq(struct request_queue *q)
667 static inline enum blk_zoned_model
668 blk_queue_zoned_model(struct request_queue *q)
670 return q->limits.zoned;
673 static inline bool blk_queue_is_zoned(struct request_queue *q)
675 switch (blk_queue_zoned_model(q)) {
684 static inline unsigned int blk_queue_zone_sectors(struct request_queue *q)
686 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
689 #ifdef CONFIG_BLK_DEV_ZONED
690 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
692 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
695 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
698 if (!blk_queue_is_zoned(q))
700 return sector >> ilog2(q->limits.chunk_sectors);
703 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
706 if (!blk_queue_is_zoned(q) || !q->seq_zones_bitmap)
708 return test_bit(blk_queue_zone_no(q, sector), q->seq_zones_bitmap);
710 #else /* CONFIG_BLK_DEV_ZONED */
711 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
715 #endif /* CONFIG_BLK_DEV_ZONED */
717 static inline bool rq_is_sync(struct request *rq)
719 return op_is_sync(rq->cmd_flags);
722 static inline bool rq_mergeable(struct request *rq)
724 if (blk_rq_is_passthrough(rq))
727 if (req_op(rq) == REQ_OP_FLUSH)
730 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
733 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
735 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
741 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
743 if (bio_page(a) == bio_page(b) &&
744 bio_offset(a) == bio_offset(b))
750 static inline unsigned int blk_queue_depth(struct request_queue *q)
753 return q->queue_depth;
755 return q->nr_requests;
758 extern unsigned long blk_max_low_pfn, blk_max_pfn;
761 * standard bounce addresses:
763 * BLK_BOUNCE_HIGH : bounce all highmem pages
764 * BLK_BOUNCE_ANY : don't bounce anything
765 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
768 #if BITS_PER_LONG == 32
769 #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
771 #define BLK_BOUNCE_HIGH -1ULL
773 #define BLK_BOUNCE_ANY (-1ULL)
774 #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
777 * default timeout for SG_IO if none specified
779 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
780 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
786 unsigned long offset;
791 struct req_iterator {
792 struct bvec_iter iter;
796 /* This should not be used directly - use rq_for_each_segment */
797 #define for_each_bio(_bio) \
798 for (; _bio; _bio = _bio->bi_next)
799 #define __rq_for_each_bio(_bio, rq) \
801 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
803 #define rq_for_each_segment(bvl, _rq, _iter) \
804 __rq_for_each_bio(_iter.bio, _rq) \
805 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
807 #define rq_for_each_bvec(bvl, _rq, _iter) \
808 __rq_for_each_bio(_iter.bio, _rq) \
809 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
811 #define rq_iter_last(bvec, _iter) \
812 (_iter.bio->bi_next == NULL && \
813 bio_iter_last(bvec, _iter.iter))
815 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
816 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
818 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
819 extern void rq_flush_dcache_pages(struct request *rq);
821 static inline void rq_flush_dcache_pages(struct request *rq)
826 extern int blk_register_queue(struct gendisk *disk);
827 extern void blk_unregister_queue(struct gendisk *disk);
828 extern blk_qc_t generic_make_request(struct bio *bio);
829 extern blk_qc_t direct_make_request(struct bio *bio);
830 extern void blk_rq_init(struct request_queue *q, struct request *rq);
831 extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
832 extern void blk_put_request(struct request *);
833 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
834 blk_mq_req_flags_t flags);
835 extern int blk_lld_busy(struct request_queue *q);
836 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
837 struct bio_set *bs, gfp_t gfp_mask,
838 int (*bio_ctr)(struct bio *, struct bio *, void *),
840 extern void blk_rq_unprep_clone(struct request *rq);
841 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
843 extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
844 extern void blk_queue_split(struct request_queue *, struct bio **);
845 extern void blk_recount_segments(struct request_queue *, struct bio *);
846 extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
847 extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
848 unsigned int, void __user *);
849 extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
850 unsigned int, void __user *);
851 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
852 struct scsi_ioctl_command __user *);
854 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
855 extern void blk_queue_exit(struct request_queue *q);
856 extern void blk_sync_queue(struct request_queue *q);
857 extern int blk_rq_map_user(struct request_queue *, struct request *,
858 struct rq_map_data *, void __user *, unsigned long,
860 extern int blk_rq_unmap_user(struct bio *);
861 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
862 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
863 struct rq_map_data *, const struct iov_iter *,
865 extern void blk_execute_rq(struct request_queue *, struct gendisk *,
866 struct request *, int);
867 extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
868 struct request *, int, rq_end_io_fn *);
870 int blk_status_to_errno(blk_status_t status);
871 blk_status_t errno_to_blk_status(int errno);
873 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
875 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
877 return bdev->bd_disk->queue; /* this is never NULL */
881 * The basic unit of block I/O is a sector. It is used in a number of contexts
882 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
883 * bytes. Variables of type sector_t represent an offset or size that is a
884 * multiple of 512 bytes. Hence these two constants.
887 #define SECTOR_SHIFT 9
890 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
894 * blk_rq_pos() : the current sector
895 * blk_rq_bytes() : bytes left in the entire request
896 * blk_rq_cur_bytes() : bytes left in the current segment
897 * blk_rq_err_bytes() : bytes left till the next error boundary
898 * blk_rq_sectors() : sectors left in the entire request
899 * blk_rq_cur_sectors() : sectors left in the current segment
901 static inline sector_t blk_rq_pos(const struct request *rq)
906 static inline unsigned int blk_rq_bytes(const struct request *rq)
908 return rq->__data_len;
911 static inline int blk_rq_cur_bytes(const struct request *rq)
913 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
916 extern unsigned int blk_rq_err_bytes(const struct request *rq);
918 static inline unsigned int blk_rq_sectors(const struct request *rq)
920 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
923 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
925 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
928 #ifdef CONFIG_BLK_DEV_ZONED
929 static inline unsigned int blk_rq_zone_no(struct request *rq)
931 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
934 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
936 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
938 #endif /* CONFIG_BLK_DEV_ZONED */
941 * Some commands like WRITE SAME have a payload or data transfer size which
942 * is different from the size of the request. Any driver that supports such
943 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
944 * calculate the data transfer size.
946 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
948 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
949 return rq->special_vec.bv_len;
950 return blk_rq_bytes(rq);
954 * Return the first full biovec in the request. The caller needs to check that
955 * there are any bvecs before calling this helper.
957 static inline struct bio_vec req_bvec(struct request *rq)
959 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
960 return rq->special_vec;
961 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
964 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
967 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
968 return min(q->limits.max_discard_sectors,
969 UINT_MAX >> SECTOR_SHIFT);
971 if (unlikely(op == REQ_OP_WRITE_SAME))
972 return q->limits.max_write_same_sectors;
974 if (unlikely(op == REQ_OP_WRITE_ZEROES))
975 return q->limits.max_write_zeroes_sectors;
977 return q->limits.max_sectors;
981 * Return maximum size of a request at given offset. Only valid for
982 * file system requests.
984 static inline unsigned int blk_max_size_offset(struct request_queue *q,
987 if (!q->limits.chunk_sectors)
988 return q->limits.max_sectors;
990 return min(q->limits.max_sectors, (unsigned int)(q->limits.chunk_sectors -
991 (offset & (q->limits.chunk_sectors - 1))));
994 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
997 struct request_queue *q = rq->q;
999 if (blk_rq_is_passthrough(rq))
1000 return q->limits.max_hw_sectors;
1002 if (!q->limits.chunk_sectors ||
1003 req_op(rq) == REQ_OP_DISCARD ||
1004 req_op(rq) == REQ_OP_SECURE_ERASE)
1005 return blk_queue_get_max_sectors(q, req_op(rq));
1007 return min(blk_max_size_offset(q, offset),
1008 blk_queue_get_max_sectors(q, req_op(rq)));
1011 static inline unsigned int blk_rq_count_bios(struct request *rq)
1013 unsigned int nr_bios = 0;
1016 __rq_for_each_bio(bio, rq)
1022 void blk_steal_bios(struct bio_list *list, struct request *rq);
1025 * Request completion related functions.
1027 * blk_update_request() completes given number of bytes and updates
1028 * the request without completing it.
1030 * blk_end_request() and friends. __blk_end_request() must be called
1031 * with the request queue spinlock acquired.
1033 * Several drivers define their own end_request and call
1034 * blk_end_request() for parts of the original function.
1035 * This prevents code duplication in drivers.
1037 extern bool blk_update_request(struct request *rq, blk_status_t error,
1038 unsigned int nr_bytes);
1039 extern void blk_end_request_all(struct request *rq, blk_status_t error);
1040 extern bool __blk_end_request(struct request *rq, blk_status_t error,
1041 unsigned int nr_bytes);
1042 extern void __blk_end_request_all(struct request *rq, blk_status_t error);
1043 extern bool __blk_end_request_cur(struct request *rq, blk_status_t error);
1045 extern void __blk_complete_request(struct request *);
1046 extern void blk_abort_request(struct request *);
1049 * Access functions for manipulating queue properties
1051 extern void blk_cleanup_queue(struct request_queue *);
1052 extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
1053 extern void blk_queue_bounce_limit(struct request_queue *, u64);
1054 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1055 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1056 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1057 extern void blk_queue_max_discard_segments(struct request_queue *,
1059 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1060 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1061 unsigned int max_discard_sectors);
1062 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1063 unsigned int max_write_same_sectors);
1064 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1065 unsigned int max_write_same_sectors);
1066 extern void blk_queue_logical_block_size(struct request_queue *, unsigned short);
1067 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1068 extern void blk_queue_alignment_offset(struct request_queue *q,
1069 unsigned int alignment);
1070 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1071 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1072 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1073 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1074 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1075 extern void blk_set_default_limits(struct queue_limits *lim);
1076 extern void blk_set_stacking_limits(struct queue_limits *lim);
1077 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1079 extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
1081 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1083 extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
1084 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1085 extern int blk_queue_dma_drain(struct request_queue *q,
1086 dma_drain_needed_fn *dma_drain_needed,
1087 void *buf, unsigned int size);
1088 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1089 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1090 extern void blk_queue_dma_alignment(struct request_queue *, int);
1091 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1092 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1093 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1096 * Number of physical segments as sent to the device.
1098 * Normally this is the number of discontiguous data segments sent by the
1099 * submitter. But for data-less command like discard we might have no
1100 * actual data segments submitted, but the driver might have to add it's
1101 * own special payload. In that case we still return 1 here so that this
1102 * special payload will be mapped.
1104 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1106 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1108 return rq->nr_phys_segments;
1112 * Number of discard segments (or ranges) the driver needs to fill in.
1113 * Each discard bio merged into a request is counted as one segment.
1115 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1117 return max_t(unsigned short, rq->nr_phys_segments, 1);
1120 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
1121 extern void blk_dump_rq_flags(struct request *, char *);
1122 extern long nr_blockdev_pages(void);
1124 bool __must_check blk_get_queue(struct request_queue *);
1125 struct request_queue *blk_alloc_queue(gfp_t);
1126 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id);
1127 extern void blk_put_queue(struct request_queue *);
1128 extern void blk_set_queue_dying(struct request_queue *);
1131 * blk_plug permits building a queue of related requests by holding the I/O
1132 * fragments for a short period. This allows merging of sequential requests
1133 * into single larger request. As the requests are moved from a per-task list to
1134 * the device's request_queue in a batch, this results in improved scalability
1135 * as the lock contention for request_queue lock is reduced.
1137 * It is ok not to disable preemption when adding the request to the plug list
1138 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1139 * the plug list when the task sleeps by itself. For details, please see
1140 * schedule() where blk_schedule_flush_plug() is called.
1143 struct list_head mq_list; /* blk-mq requests */
1144 struct list_head cb_list; /* md requires an unplug callback */
1145 unsigned short rq_count;
1146 bool multiple_queues;
1148 #define BLK_MAX_REQUEST_COUNT 16
1149 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1152 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1153 struct blk_plug_cb {
1154 struct list_head list;
1155 blk_plug_cb_fn callback;
1158 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1159 void *data, int size);
1160 extern void blk_start_plug(struct blk_plug *);
1161 extern void blk_finish_plug(struct blk_plug *);
1162 extern void blk_flush_plug_list(struct blk_plug *, bool);
1164 static inline void blk_flush_plug(struct task_struct *tsk)
1166 struct blk_plug *plug = tsk->plug;
1169 blk_flush_plug_list(plug, false);
1172 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1174 struct blk_plug *plug = tsk->plug;
1177 blk_flush_plug_list(plug, true);
1180 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1182 struct blk_plug *plug = tsk->plug;
1185 (!list_empty(&plug->mq_list) ||
1186 !list_empty(&plug->cb_list));
1189 extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
1190 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1191 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1193 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1195 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1196 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1197 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1198 sector_t nr_sects, gfp_t gfp_mask, int flags,
1201 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1202 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1204 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1205 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1207 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1208 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1210 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1211 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1213 return blkdev_issue_discard(sb->s_bdev,
1214 block << (sb->s_blocksize_bits -
1216 nr_blocks << (sb->s_blocksize_bits -
1220 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1221 sector_t nr_blocks, gfp_t gfp_mask)
1223 return blkdev_issue_zeroout(sb->s_bdev,
1224 block << (sb->s_blocksize_bits -
1226 nr_blocks << (sb->s_blocksize_bits -
1231 extern int blk_verify_command(unsigned char *cmd, fmode_t mode);
1233 enum blk_default_limits {
1234 BLK_MAX_SEGMENTS = 128,
1235 BLK_SAFE_MAX_SECTORS = 255,
1236 BLK_DEF_MAX_SECTORS = 2560,
1237 BLK_MAX_SEGMENT_SIZE = 65536,
1238 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1241 static inline unsigned long queue_segment_boundary(struct request_queue *q)
1243 return q->limits.seg_boundary_mask;
1246 static inline unsigned long queue_virt_boundary(struct request_queue *q)
1248 return q->limits.virt_boundary_mask;
1251 static inline unsigned int queue_max_sectors(struct request_queue *q)
1253 return q->limits.max_sectors;
1256 static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
1258 return q->limits.max_hw_sectors;
1261 static inline unsigned short queue_max_segments(struct request_queue *q)
1263 return q->limits.max_segments;
1266 static inline unsigned short queue_max_discard_segments(struct request_queue *q)
1268 return q->limits.max_discard_segments;
1271 static inline unsigned int queue_max_segment_size(struct request_queue *q)
1273 return q->limits.max_segment_size;
1276 static inline unsigned short queue_logical_block_size(struct request_queue *q)
1280 if (q && q->limits.logical_block_size)
1281 retval = q->limits.logical_block_size;
1286 static inline unsigned short bdev_logical_block_size(struct block_device *bdev)
1288 return queue_logical_block_size(bdev_get_queue(bdev));
1291 static inline unsigned int queue_physical_block_size(struct request_queue *q)
1293 return q->limits.physical_block_size;
1296 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1298 return queue_physical_block_size(bdev_get_queue(bdev));
1301 static inline unsigned int queue_io_min(struct request_queue *q)
1303 return q->limits.io_min;
1306 static inline int bdev_io_min(struct block_device *bdev)
1308 return queue_io_min(bdev_get_queue(bdev));
1311 static inline unsigned int queue_io_opt(struct request_queue *q)
1313 return q->limits.io_opt;
1316 static inline int bdev_io_opt(struct block_device *bdev)
1318 return queue_io_opt(bdev_get_queue(bdev));
1321 static inline int queue_alignment_offset(struct request_queue *q)
1323 if (q->limits.misaligned)
1326 return q->limits.alignment_offset;
1329 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1331 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1332 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1335 return (granularity + lim->alignment_offset - alignment) % granularity;
1338 static inline int bdev_alignment_offset(struct block_device *bdev)
1340 struct request_queue *q = bdev_get_queue(bdev);
1342 if (q->limits.misaligned)
1345 if (bdev != bdev->bd_contains)
1346 return bdev->bd_part->alignment_offset;
1348 return q->limits.alignment_offset;
1351 static inline int queue_discard_alignment(struct request_queue *q)
1353 if (q->limits.discard_misaligned)
1356 return q->limits.discard_alignment;
1359 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1361 unsigned int alignment, granularity, offset;
1363 if (!lim->max_discard_sectors)
1366 /* Why are these in bytes, not sectors? */
1367 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1368 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1372 /* Offset of the partition start in 'granularity' sectors */
1373 offset = sector_div(sector, granularity);
1375 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1376 offset = (granularity + alignment - offset) % granularity;
1378 /* Turn it back into bytes, gaah */
1379 return offset << SECTOR_SHIFT;
1382 static inline int bdev_discard_alignment(struct block_device *bdev)
1384 struct request_queue *q = bdev_get_queue(bdev);
1386 if (bdev != bdev->bd_contains)
1387 return bdev->bd_part->discard_alignment;
1389 return q->limits.discard_alignment;
1392 static inline unsigned int bdev_write_same(struct block_device *bdev)
1394 struct request_queue *q = bdev_get_queue(bdev);
1397 return q->limits.max_write_same_sectors;
1402 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1404 struct request_queue *q = bdev_get_queue(bdev);
1407 return q->limits.max_write_zeroes_sectors;
1412 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1414 struct request_queue *q = bdev_get_queue(bdev);
1417 return blk_queue_zoned_model(q);
1419 return BLK_ZONED_NONE;
1422 static inline bool bdev_is_zoned(struct block_device *bdev)
1424 struct request_queue *q = bdev_get_queue(bdev);
1427 return blk_queue_is_zoned(q);
1432 static inline unsigned int bdev_zone_sectors(struct block_device *bdev)
1434 struct request_queue *q = bdev_get_queue(bdev);
1437 return blk_queue_zone_sectors(q);
1441 static inline int queue_dma_alignment(struct request_queue *q)
1443 return q ? q->dma_alignment : 511;
1446 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1449 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1450 return !(addr & alignment) && !(len & alignment);
1453 /* assumes size > 256 */
1454 static inline unsigned int blksize_bits(unsigned int size)
1456 unsigned int bits = 8;
1460 } while (size > 256);
1464 static inline unsigned int block_size(struct block_device *bdev)
1466 return bdev->bd_block_size;
1469 typedef struct {struct page *v;} Sector;
1471 unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
1473 static inline void put_dev_sector(Sector p)
1478 int kblockd_schedule_work(struct work_struct *work);
1479 int kblockd_schedule_work_on(int cpu, struct work_struct *work);
1480 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1482 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1483 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1484 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1485 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1487 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1489 enum blk_integrity_flags {
1490 BLK_INTEGRITY_VERIFY = 1 << 0,
1491 BLK_INTEGRITY_GENERATE = 1 << 1,
1492 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1493 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1496 struct blk_integrity_iter {
1500 unsigned int data_size;
1501 unsigned short interval;
1502 const char *disk_name;
1505 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1507 struct blk_integrity_profile {
1508 integrity_processing_fn *generate_fn;
1509 integrity_processing_fn *verify_fn;
1513 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1514 extern void blk_integrity_unregister(struct gendisk *);
1515 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1516 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1517 struct scatterlist *);
1518 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1519 extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
1521 extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
1524 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1526 struct blk_integrity *bi = &disk->queue->integrity;
1535 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1537 return blk_get_integrity(bdev->bd_disk);
1540 static inline bool blk_integrity_rq(struct request *rq)
1542 return rq->cmd_flags & REQ_INTEGRITY;
1545 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1548 q->limits.max_integrity_segments = segs;
1551 static inline unsigned short
1552 queue_max_integrity_segments(struct request_queue *q)
1554 return q->limits.max_integrity_segments;
1558 * bio_integrity_intervals - Return number of integrity intervals for a bio
1559 * @bi: blk_integrity profile for device
1560 * @sectors: Size of the bio in 512-byte sectors
1562 * Description: The block layer calculates everything in 512 byte
1563 * sectors but integrity metadata is done in terms of the data integrity
1564 * interval size of the storage device. Convert the block layer sectors
1565 * to the appropriate number of integrity intervals.
1567 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1568 unsigned int sectors)
1570 return sectors >> (bi->interval_exp - 9);
1573 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1574 unsigned int sectors)
1576 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1580 * Return the first bvec that contains integrity data. Only drivers that are
1581 * limited to a single integrity segment should use this helper.
1583 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1585 if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
1587 return rq->bio->bi_integrity->bip_vec;
1590 #else /* CONFIG_BLK_DEV_INTEGRITY */
1593 struct block_device;
1595 struct blk_integrity;
1597 static inline int blk_integrity_rq(struct request *rq)
1601 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1606 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1608 struct scatterlist *s)
1612 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1616 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1620 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1624 static inline void blk_integrity_register(struct gendisk *d,
1625 struct blk_integrity *b)
1628 static inline void blk_integrity_unregister(struct gendisk *d)
1631 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1635 static inline unsigned short queue_max_integrity_segments(struct request_queue *q)
1639 static inline bool blk_integrity_merge_rq(struct request_queue *rq,
1645 static inline bool blk_integrity_merge_bio(struct request_queue *rq,
1652 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1653 unsigned int sectors)
1658 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1659 unsigned int sectors)
1664 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1669 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1671 struct block_device_operations {
1672 int (*open) (struct block_device *, fmode_t);
1673 void (*release) (struct gendisk *, fmode_t);
1674 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1675 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1676 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1677 unsigned int (*check_events) (struct gendisk *disk,
1678 unsigned int clearing);
1679 /* ->media_changed() is DEPRECATED, use ->check_events() instead */
1680 int (*media_changed) (struct gendisk *);
1681 void (*unlock_native_capacity) (struct gendisk *);
1682 int (*revalidate_disk) (struct gendisk *);
1683 int (*getgeo)(struct block_device *, struct hd_geometry *);
1684 /* this callback is with swap_lock and sometimes page table lock held */
1685 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1686 int (*report_zones)(struct gendisk *, sector_t sector,
1687 struct blk_zone *zones, unsigned int *nr_zones,
1689 struct module *owner;
1690 const struct pr_ops *pr_ops;
1693 extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
1695 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1696 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1697 struct writeback_control *);
1699 #ifdef CONFIG_BLK_DEV_ZONED
1700 bool blk_req_needs_zone_write_lock(struct request *rq);
1701 void __blk_req_zone_write_lock(struct request *rq);
1702 void __blk_req_zone_write_unlock(struct request *rq);
1704 static inline void blk_req_zone_write_lock(struct request *rq)
1706 if (blk_req_needs_zone_write_lock(rq))
1707 __blk_req_zone_write_lock(rq);
1710 static inline void blk_req_zone_write_unlock(struct request *rq)
1712 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1713 __blk_req_zone_write_unlock(rq);
1716 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1718 return rq->q->seq_zones_wlock &&
1719 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1722 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1724 if (!blk_req_needs_zone_write_lock(rq))
1726 return !blk_req_zone_is_write_locked(rq);
1729 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1734 static inline void blk_req_zone_write_lock(struct request *rq)
1738 static inline void blk_req_zone_write_unlock(struct request *rq)
1741 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1746 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1750 #endif /* CONFIG_BLK_DEV_ZONED */
1752 #else /* CONFIG_BLOCK */
1754 struct block_device;
1757 * stubs for when the block layer is configured out
1759 #define buffer_heads_over_limit 0
1761 static inline long nr_blockdev_pages(void)
1769 static inline void blk_start_plug(struct blk_plug *plug)
1773 static inline void blk_finish_plug(struct blk_plug *plug)
1777 static inline void blk_flush_plug(struct task_struct *task)
1781 static inline void blk_schedule_flush_plug(struct task_struct *task)
1786 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1791 static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
1792 sector_t *error_sector)
1797 #endif /* CONFIG_BLOCK */
1799 static inline void blk_wake_io_task(struct task_struct *waiter)
1802 * If we're polling, the task itself is doing the completions. For
1803 * that case, we don't need to signal a wakeup, it's enough to just
1804 * mark us as RUNNING.
1806 if (waiter == current)
1807 __set_current_state(TASK_RUNNING);
1809 wake_up_process(waiter);