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
54 * Maximum number of blkcg policies allowed to be registered concurrently.
55 * Defined here to simplify include dependency.
57 #define BLKCG_MAX_POLS 5
59 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
63 typedef __u32 __bitwise req_flags_t;
65 /* elevator knows about this request */
66 #define RQF_SORTED ((__force req_flags_t)(1 << 0))
67 /* drive already may have started this one */
68 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
69 /* may not be passed by ioscheduler */
70 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
71 /* request for flush sequence */
72 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
73 /* merge of different types, fail separately */
74 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
75 /* track inflight for MQ */
76 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
77 /* don't call prep for this one */
78 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
79 /* set for "ide_preempt" requests and also for requests for which the SCSI
80 "quiesce" state must be ignored. */
81 #define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
82 /* contains copies of user pages */
83 #define RQF_COPY_USER ((__force req_flags_t)(1 << 9))
84 /* vaguely specified driver internal error. Ignored by the block layer */
85 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
86 /* don't warn about errors */
87 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
88 /* elevator private data attached */
89 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
90 /* account into disk and partition IO statistics */
91 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
92 /* request came from our alloc pool */
93 #define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
94 /* runtime pm request */
95 #define RQF_PM ((__force req_flags_t)(1 << 15))
96 /* on IO scheduler merge hash */
97 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
98 /* track IO completion time */
99 #define RQF_STATS ((__force req_flags_t)(1 << 17))
100 /* Look at ->special_vec for the actual data payload instead of the
102 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
103 /* The per-zone write lock is held for this request */
104 #define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
105 /* already slept for hybrid poll */
106 #define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
107 /* ->timeout has been called, don't expire again */
108 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
110 /* flags that prevent us from merging requests: */
111 #define RQF_NOMERGE_FLAGS \
112 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
115 * Request state for blk-mq.
124 * Try to put the fields that are referenced together in the same cacheline.
126 * If you modify this structure, make sure to update blk_rq_init() and
127 * especially blk_mq_rq_ctx_init() to take care of the added fields.
130 struct request_queue *q;
131 struct blk_mq_ctx *mq_ctx;
132 struct blk_mq_hw_ctx *mq_hctx;
134 unsigned int cmd_flags; /* op and common flags */
135 req_flags_t rq_flags;
139 /* the following two fields are internal, NEVER access directly */
140 unsigned int __data_len; /* total data len */
142 sector_t __sector; /* sector cursor */
147 struct list_head queuelist;
150 * The hash is used inside the scheduler, and killed once the
151 * request reaches the dispatch list. The ipi_list is only used
152 * to queue the request for softirq completion, which is long
153 * after the request has been unhashed (and even removed from
154 * the dispatch list).
157 struct hlist_node hash; /* merge hash */
158 struct list_head ipi_list;
162 * The rb_node is only used inside the io scheduler, requests
163 * are pruned when moved to the dispatch queue. So let the
164 * completion_data share space with the rb_node.
167 struct rb_node rb_node; /* sort/lookup */
168 struct bio_vec special_vec;
169 void *completion_data;
170 int error_count; /* for legacy drivers, don't use */
174 * Three pointers are available for the IO schedulers, if they need
175 * more they have to dynamically allocate it. Flush requests are
176 * never put on the IO scheduler. So let the flush fields share
177 * space with the elevator data.
187 struct list_head list;
188 rq_end_io_fn *saved_end_io;
192 struct gendisk *rq_disk;
193 struct hd_struct *part;
194 /* Time that I/O was submitted to the kernel. */
196 /* Time that I/O was submitted to the device. */
197 u64 io_start_time_ns;
199 #ifdef CONFIG_BLK_WBT
200 unsigned short wbt_flags;
202 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
203 unsigned short throtl_size;
207 * Number of scatter-gather DMA addr+len pairs after
208 * physical address coalescing is performed.
210 unsigned short nr_phys_segments;
212 #if defined(CONFIG_BLK_DEV_INTEGRITY)
213 unsigned short nr_integrity_segments;
216 unsigned short write_hint;
217 unsigned short ioprio;
219 void *special; /* opaque pointer available for LLD use */
221 unsigned int extra_len; /* length of alignment and padding */
223 enum mq_rq_state state;
226 unsigned int timeout;
227 unsigned long deadline;
230 struct __call_single_data csd;
235 * completion callback.
237 rq_end_io_fn *end_io;
241 struct request *next_rq;
244 static inline bool blk_op_is_scsi(unsigned int op)
246 return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
249 static inline bool blk_op_is_private(unsigned int op)
251 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
254 static inline bool blk_rq_is_scsi(struct request *rq)
256 return blk_op_is_scsi(req_op(rq));
259 static inline bool blk_rq_is_private(struct request *rq)
261 return blk_op_is_private(req_op(rq));
264 static inline bool blk_rq_is_passthrough(struct request *rq)
266 return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
269 static inline bool bio_is_passthrough(struct bio *bio)
271 unsigned op = bio_op(bio);
273 return blk_op_is_scsi(op) || blk_op_is_private(op);
276 static inline unsigned short req_get_ioprio(struct request *req)
281 #include <linux/elevator.h>
283 struct blk_queue_ctx;
285 typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
286 typedef bool (poll_q_fn) (struct request_queue *q, blk_qc_t);
289 typedef int (dma_drain_needed_fn)(struct request *);
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 alignment_offset;
330 unsigned int max_discard_sectors;
331 unsigned int max_hw_discard_sectors;
332 unsigned int max_write_same_sectors;
333 unsigned int max_write_zeroes_sectors;
334 unsigned int discard_granularity;
335 unsigned int discard_alignment;
337 unsigned short logical_block_size;
338 unsigned short max_segments;
339 unsigned short max_integrity_segments;
340 unsigned short max_discard_segments;
342 unsigned char misaligned;
343 unsigned char discard_misaligned;
344 unsigned char cluster;
345 unsigned char raid_partial_stripes_expensive;
346 enum blk_zoned_model zoned;
349 #ifdef CONFIG_BLK_DEV_ZONED
351 extern unsigned int blkdev_nr_zones(struct block_device *bdev);
352 extern int blkdev_report_zones(struct block_device *bdev,
353 sector_t sector, struct blk_zone *zones,
354 unsigned int *nr_zones, gfp_t gfp_mask);
355 extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors,
356 sector_t nr_sectors, gfp_t gfp_mask);
357 extern int blk_revalidate_disk_zones(struct gendisk *disk);
359 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
360 unsigned int cmd, unsigned long arg);
361 extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
362 unsigned int cmd, unsigned long arg);
364 #else /* CONFIG_BLK_DEV_ZONED */
366 static inline unsigned int blkdev_nr_zones(struct block_device *bdev)
371 static inline int blk_revalidate_disk_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_reset_zones_ioctl(struct block_device *bdev,
384 fmode_t mode, unsigned int cmd,
390 #endif /* CONFIG_BLK_DEV_ZONED */
392 struct request_queue {
394 * Together with queue_head for cacheline sharing
396 struct list_head queue_head;
397 struct request *last_merge;
398 struct elevator_queue *elevator;
400 struct blk_queue_stats *stats;
401 struct rq_qos *rq_qos;
403 make_request_fn *make_request_fn;
405 dma_drain_needed_fn *dma_drain_needed;
407 const struct blk_mq_ops *mq_ops;
410 struct blk_mq_ctx __percpu *queue_ctx;
411 unsigned int nr_queues;
413 unsigned int queue_depth;
415 /* hw dispatch queues */
416 struct blk_mq_hw_ctx **queue_hw_ctx;
417 unsigned int nr_hw_queues;
419 struct backing_dev_info *backing_dev_info;
422 * The queue owner gets to use this for whatever they like.
423 * ll_rw_blk doesn't touch it.
428 * various queue flags, see QUEUE_* below
430 unsigned long queue_flags;
432 * Number of contexts that have called blk_set_pm_only(). If this
433 * counter is above zero then only RQF_PM and RQF_PREEMPT requests are
439 * ida allocated id for this queue. Used to index queues from
445 * queue needs bounce pages for pages above this limit
450 * protects queue structures from reentrancy. ->__queue_lock should
451 * _never_ be used directly, it is queue private. always use
454 spinlock_t __queue_lock;
455 spinlock_t *queue_lock;
465 struct kobject mq_kobj;
467 #ifdef CONFIG_BLK_DEV_INTEGRITY
468 struct blk_integrity integrity;
469 #endif /* CONFIG_BLK_DEV_INTEGRITY */
474 unsigned int nr_pending;
480 unsigned long nr_requests; /* Max # of requests */
482 unsigned int dma_drain_size;
483 void *dma_drain_buffer;
484 unsigned int dma_pad_mask;
485 unsigned int dma_alignment;
487 unsigned int rq_timeout;
490 struct blk_stat_callback *poll_cb;
491 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
493 struct timer_list timeout;
494 struct work_struct timeout_work;
496 struct list_head icq_list;
497 #ifdef CONFIG_BLK_CGROUP
498 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
499 struct blkcg_gq *root_blkg;
500 struct list_head blkg_list;
503 struct queue_limits limits;
505 #ifdef CONFIG_BLK_DEV_ZONED
507 * Zoned block device information for request dispatch control.
508 * nr_zones is the total number of zones of the device. This is always
509 * 0 for regular block devices. seq_zones_bitmap is a bitmap of nr_zones
510 * bits which indicates if a zone is conventional (bit clear) or
511 * sequential (bit set). seq_zones_wlock is a bitmap of nr_zones
512 * bits which indicates if a zone is write locked, that is, if a write
513 * request targeting the zone was dispatched. All three fields are
514 * initialized by the low level device driver (e.g. scsi/sd.c).
515 * Stacking drivers (device mappers) may or may not initialize
518 * Reads of this information must be protected with blk_queue_enter() /
519 * blk_queue_exit(). Modifying this information is only allowed while
520 * no requests are being processed. See also blk_mq_freeze_queue() and
521 * blk_mq_unfreeze_queue().
523 unsigned int nr_zones;
524 unsigned long *seq_zones_bitmap;
525 unsigned long *seq_zones_wlock;
526 #endif /* CONFIG_BLK_DEV_ZONED */
531 unsigned int sg_timeout;
532 unsigned int sg_reserved_size;
534 #ifdef CONFIG_BLK_DEV_IO_TRACE
535 struct blk_trace *blk_trace;
536 struct mutex blk_trace_mutex;
539 * for flush operations
541 struct blk_flush_queue *fq;
543 struct list_head requeue_list;
544 spinlock_t requeue_lock;
545 struct delayed_work requeue_work;
547 struct mutex sysfs_lock;
549 atomic_t mq_freeze_depth;
551 #if defined(CONFIG_BLK_DEV_BSG)
552 struct bsg_class_device bsg_dev;
555 #ifdef CONFIG_BLK_DEV_THROTTLING
557 struct throtl_data *td;
559 struct rcu_head rcu_head;
560 wait_queue_head_t mq_freeze_wq;
561 struct percpu_ref q_usage_counter;
562 struct list_head all_q_node;
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;
573 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 1 /* queue is stopped */
585 #define QUEUE_FLAG_DYING 2 /* queue being torn down */
586 #define QUEUE_FLAG_BIDI 4 /* queue supports bidi requests */
587 #define QUEUE_FLAG_NOMERGES 5 /* disable merge attempts */
588 #define QUEUE_FLAG_SAME_COMP 6 /* complete on same CPU-group */
589 #define QUEUE_FLAG_FAIL_IO 7 /* fake timeout */
590 #define QUEUE_FLAG_NONROT 9 /* non-rotational device (SSD) */
591 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
592 #define QUEUE_FLAG_IO_STAT 10 /* do disk/partitions IO accounting */
593 #define QUEUE_FLAG_DISCARD 11 /* supports DISCARD */
594 #define QUEUE_FLAG_NOXMERGES 12 /* No extended merges */
595 #define QUEUE_FLAG_ADD_RANDOM 13 /* Contributes to random pool */
596 #define QUEUE_FLAG_SECERASE 14 /* supports secure erase */
597 #define QUEUE_FLAG_SAME_FORCE 15 /* force complete on same CPU */
598 #define QUEUE_FLAG_DEAD 16 /* queue tear-down finished */
599 #define QUEUE_FLAG_INIT_DONE 17 /* queue is initialized */
600 #define QUEUE_FLAG_NO_SG_MERGE 18 /* don't attempt to merge SG segments*/
601 #define QUEUE_FLAG_POLL 19 /* IO polling enabled if set */
602 #define QUEUE_FLAG_WC 20 /* Write back caching */
603 #define QUEUE_FLAG_FUA 21 /* device supports FUA writes */
604 #define QUEUE_FLAG_FLUSH_NQ 22 /* flush not queueuable */
605 #define QUEUE_FLAG_DAX 23 /* device supports DAX */
606 #define QUEUE_FLAG_STATS 24 /* track IO start and completion times */
607 #define QUEUE_FLAG_POLL_STATS 25 /* collecting stats for hybrid polling */
608 #define QUEUE_FLAG_REGISTERED 26 /* queue has been registered to a disk */
609 #define QUEUE_FLAG_SCSI_PASSTHROUGH 27 /* queue supports SCSI commands */
610 #define QUEUE_FLAG_QUIESCED 28 /* queue has been quiesced */
611 #define QUEUE_FLAG_PCI_P2PDMA 29 /* device supports PCI p2p requests */
613 #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
614 (1 << QUEUE_FLAG_SAME_COMP) | \
615 (1 << QUEUE_FLAG_ADD_RANDOM))
617 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
618 (1 << QUEUE_FLAG_SAME_COMP) | \
619 (1 << QUEUE_FLAG_POLL))
621 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
622 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
623 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
625 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
626 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
627 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
628 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
629 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
630 #define blk_queue_noxmerges(q) \
631 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
632 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
633 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
634 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
635 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
636 #define blk_queue_secure_erase(q) \
637 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
638 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
639 #define blk_queue_scsi_passthrough(q) \
640 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
641 #define blk_queue_pci_p2pdma(q) \
642 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
644 #define blk_noretry_request(rq) \
645 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
646 REQ_FAILFAST_DRIVER))
647 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
648 #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
649 #define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
651 extern void blk_set_pm_only(struct request_queue *q);
652 extern void blk_clear_pm_only(struct request_queue *q);
654 static inline bool blk_account_rq(struct request *rq)
656 return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
659 #define blk_bidi_rq(rq) ((rq)->next_rq != NULL)
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)
666 * Driver can handle struct request, if it either has an old style
667 * request_fn defined, or is blk-mq based.
669 static inline bool queue_is_rq_based(struct request_queue *q)
674 static inline unsigned int blk_queue_cluster(struct request_queue *q)
676 return q->limits.cluster;
679 static inline enum blk_zoned_model
680 blk_queue_zoned_model(struct request_queue *q)
682 return q->limits.zoned;
685 static inline bool blk_queue_is_zoned(struct request_queue *q)
687 switch (blk_queue_zoned_model(q)) {
696 static inline unsigned int blk_queue_zone_sectors(struct request_queue *q)
698 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
701 #ifdef CONFIG_BLK_DEV_ZONED
702 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
704 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
707 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
710 if (!blk_queue_is_zoned(q))
712 return sector >> ilog2(q->limits.chunk_sectors);
715 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
718 if (!blk_queue_is_zoned(q) || !q->seq_zones_bitmap)
720 return test_bit(blk_queue_zone_no(q, sector), q->seq_zones_bitmap);
722 #else /* CONFIG_BLK_DEV_ZONED */
723 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
727 #endif /* CONFIG_BLK_DEV_ZONED */
729 static inline bool rq_is_sync(struct request *rq)
731 return op_is_sync(rq->cmd_flags);
734 static inline bool rq_mergeable(struct request *rq)
736 if (blk_rq_is_passthrough(rq))
739 if (req_op(rq) == REQ_OP_FLUSH)
742 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
745 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
747 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
753 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
755 if (bio_page(a) == bio_page(b) &&
756 bio_offset(a) == bio_offset(b))
762 static inline unsigned int blk_queue_depth(struct request_queue *q)
765 return q->queue_depth;
767 return q->nr_requests;
770 extern unsigned long blk_max_low_pfn, blk_max_pfn;
773 * standard bounce addresses:
775 * BLK_BOUNCE_HIGH : bounce all highmem pages
776 * BLK_BOUNCE_ANY : don't bounce anything
777 * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
780 #if BITS_PER_LONG == 32
781 #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
783 #define BLK_BOUNCE_HIGH -1ULL
785 #define BLK_BOUNCE_ANY (-1ULL)
786 #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
789 * default timeout for SG_IO if none specified
791 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
792 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
798 unsigned long offset;
803 struct req_iterator {
804 struct bvec_iter iter;
808 /* This should not be used directly - use rq_for_each_segment */
809 #define for_each_bio(_bio) \
810 for (; _bio; _bio = _bio->bi_next)
811 #define __rq_for_each_bio(_bio, rq) \
813 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
815 #define rq_for_each_segment(bvl, _rq, _iter) \
816 __rq_for_each_bio(_iter.bio, _rq) \
817 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
819 #define rq_iter_last(bvec, _iter) \
820 (_iter.bio->bi_next == NULL && \
821 bio_iter_last(bvec, _iter.iter))
823 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
824 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
826 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
827 extern void rq_flush_dcache_pages(struct request *rq);
829 static inline void rq_flush_dcache_pages(struct request *rq)
834 extern int blk_register_queue(struct gendisk *disk);
835 extern void blk_unregister_queue(struct gendisk *disk);
836 extern blk_qc_t generic_make_request(struct bio *bio);
837 extern blk_qc_t direct_make_request(struct bio *bio);
838 extern void blk_rq_init(struct request_queue *q, struct request *rq);
839 extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
840 extern void blk_put_request(struct request *);
841 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
842 blk_mq_req_flags_t flags);
843 extern int blk_lld_busy(struct request_queue *q);
844 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
845 struct bio_set *bs, gfp_t gfp_mask,
846 int (*bio_ctr)(struct bio *, struct bio *, void *),
848 extern void blk_rq_unprep_clone(struct request *rq);
849 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
851 extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
852 extern void blk_queue_split(struct request_queue *, struct bio **);
853 extern void blk_recount_segments(struct request_queue *, struct bio *);
854 extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
855 extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
856 unsigned int, void __user *);
857 extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
858 unsigned int, void __user *);
859 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
860 struct scsi_ioctl_command __user *);
862 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
863 extern void blk_queue_exit(struct request_queue *q);
864 extern void blk_sync_queue(struct request_queue *q);
865 extern int blk_rq_map_user(struct request_queue *, struct request *,
866 struct rq_map_data *, void __user *, unsigned long,
868 extern int blk_rq_unmap_user(struct bio *);
869 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
870 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
871 struct rq_map_data *, const struct iov_iter *,
873 extern void blk_execute_rq(struct request_queue *, struct gendisk *,
874 struct request *, int);
875 extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
876 struct request *, int, rq_end_io_fn *);
878 int blk_status_to_errno(blk_status_t status);
879 blk_status_t errno_to_blk_status(int errno);
881 bool blk_poll(struct request_queue *q, blk_qc_t cookie);
883 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
885 return bdev->bd_disk->queue; /* this is never NULL */
889 * The basic unit of block I/O is a sector. It is used in a number of contexts
890 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
891 * bytes. Variables of type sector_t represent an offset or size that is a
892 * multiple of 512 bytes. Hence these two constants.
895 #define SECTOR_SHIFT 9
898 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
902 * blk_rq_pos() : the current sector
903 * blk_rq_bytes() : bytes left in the entire request
904 * blk_rq_cur_bytes() : bytes left in the current segment
905 * blk_rq_err_bytes() : bytes left till the next error boundary
906 * blk_rq_sectors() : sectors left in the entire request
907 * blk_rq_cur_sectors() : sectors left in the current segment
909 static inline sector_t blk_rq_pos(const struct request *rq)
914 static inline unsigned int blk_rq_bytes(const struct request *rq)
916 return rq->__data_len;
919 static inline int blk_rq_cur_bytes(const struct request *rq)
921 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
924 extern unsigned int blk_rq_err_bytes(const struct request *rq);
926 static inline unsigned int blk_rq_sectors(const struct request *rq)
928 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
931 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
933 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
936 #ifdef CONFIG_BLK_DEV_ZONED
937 static inline unsigned int blk_rq_zone_no(struct request *rq)
939 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
942 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
944 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
946 #endif /* CONFIG_BLK_DEV_ZONED */
949 * Some commands like WRITE SAME have a payload or data transfer size which
950 * is different from the size of the request. Any driver that supports such
951 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
952 * calculate the data transfer size.
954 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
956 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
957 return rq->special_vec.bv_len;
958 return blk_rq_bytes(rq);
961 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
964 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
965 return min(q->limits.max_discard_sectors,
966 UINT_MAX >> SECTOR_SHIFT);
968 if (unlikely(op == REQ_OP_WRITE_SAME))
969 return q->limits.max_write_same_sectors;
971 if (unlikely(op == REQ_OP_WRITE_ZEROES))
972 return q->limits.max_write_zeroes_sectors;
974 return q->limits.max_sectors;
978 * Return maximum size of a request at given offset. Only valid for
979 * file system requests.
981 static inline unsigned int blk_max_size_offset(struct request_queue *q,
984 if (!q->limits.chunk_sectors)
985 return q->limits.max_sectors;
987 return min(q->limits.max_sectors, (unsigned int)(q->limits.chunk_sectors -
988 (offset & (q->limits.chunk_sectors - 1))));
991 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
994 struct request_queue *q = rq->q;
996 if (blk_rq_is_passthrough(rq))
997 return q->limits.max_hw_sectors;
999 if (!q->limits.chunk_sectors ||
1000 req_op(rq) == REQ_OP_DISCARD ||
1001 req_op(rq) == REQ_OP_SECURE_ERASE)
1002 return blk_queue_get_max_sectors(q, req_op(rq));
1004 return min(blk_max_size_offset(q, offset),
1005 blk_queue_get_max_sectors(q, req_op(rq)));
1008 static inline unsigned int blk_rq_count_bios(struct request *rq)
1010 unsigned int nr_bios = 0;
1013 __rq_for_each_bio(bio, rq)
1019 void blk_steal_bios(struct bio_list *list, struct request *rq);
1022 * Request completion related functions.
1024 * blk_update_request() completes given number of bytes and updates
1025 * the request without completing it.
1027 * blk_end_request() and friends. __blk_end_request() must be called
1028 * with the request queue spinlock acquired.
1030 * Several drivers define their own end_request and call
1031 * blk_end_request() for parts of the original function.
1032 * This prevents code duplication in drivers.
1034 extern bool blk_update_request(struct request *rq, blk_status_t error,
1035 unsigned int nr_bytes);
1036 extern void blk_end_request_all(struct request *rq, blk_status_t error);
1037 extern bool __blk_end_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_cur(struct request *rq, blk_status_t error);
1042 extern void __blk_complete_request(struct request *);
1043 extern void blk_abort_request(struct request *);
1046 * Access functions for manipulating queue properties
1048 extern void blk_cleanup_queue(struct request_queue *);
1049 extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
1050 extern void blk_queue_bounce_limit(struct request_queue *, u64);
1051 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1052 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1053 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1054 extern void blk_queue_max_discard_segments(struct request_queue *,
1056 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1057 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1058 unsigned int max_discard_sectors);
1059 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1060 unsigned int max_write_same_sectors);
1061 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1062 unsigned int max_write_same_sectors);
1063 extern void blk_queue_logical_block_size(struct request_queue *, unsigned short);
1064 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1065 extern void blk_queue_alignment_offset(struct request_queue *q,
1066 unsigned int alignment);
1067 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1068 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1069 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1070 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1071 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1072 extern void blk_set_default_limits(struct queue_limits *lim);
1073 extern void blk_set_stacking_limits(struct queue_limits *lim);
1074 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1076 extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
1078 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1080 extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
1081 extern void blk_queue_dma_pad(struct request_queue *, unsigned int);
1082 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1083 extern int blk_queue_dma_drain(struct request_queue *q,
1084 dma_drain_needed_fn *dma_drain_needed,
1085 void *buf, unsigned int size);
1086 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1087 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1088 extern void blk_queue_dma_alignment(struct request_queue *, int);
1089 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1090 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1091 extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable);
1092 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1095 * Number of physical segments as sent to the device.
1097 * Normally this is the number of discontiguous data segments sent by the
1098 * submitter. But for data-less command like discard we might have no
1099 * actual data segments submitted, but the driver might have to add it's
1100 * own special payload. In that case we still return 1 here so that this
1101 * special payload will be mapped.
1103 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1105 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1107 return rq->nr_phys_segments;
1111 * Number of discard segments (or ranges) the driver needs to fill in.
1112 * Each discard bio merged into a request is counted as one segment.
1114 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1116 return max_t(unsigned short, rq->nr_phys_segments, 1);
1119 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
1120 extern void blk_dump_rq_flags(struct request *, char *);
1121 extern long nr_blockdev_pages(void);
1123 bool __must_check blk_get_queue(struct request_queue *);
1124 struct request_queue *blk_alloc_queue(gfp_t);
1125 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id);
1126 extern void blk_put_queue(struct request_queue *);
1127 extern void blk_set_queue_dying(struct request_queue *);
1130 * blk_plug permits building a queue of related requests by holding the I/O
1131 * fragments for a short period. This allows merging of sequential requests
1132 * into single larger request. As the requests are moved from a per-task list to
1133 * the device's request_queue in a batch, this results in improved scalability
1134 * as the lock contention for request_queue lock is reduced.
1136 * It is ok not to disable preemption when adding the request to the plug list
1137 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1138 * the plug list when the task sleeps by itself. For details, please see
1139 * schedule() where blk_schedule_flush_plug() is called.
1142 struct list_head mq_list; /* blk-mq requests */
1143 struct list_head cb_list; /* md requires an unplug callback */
1145 #define BLK_MAX_REQUEST_COUNT 16
1146 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1149 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1150 struct blk_plug_cb {
1151 struct list_head list;
1152 blk_plug_cb_fn callback;
1155 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1156 void *data, int size);
1157 extern void blk_start_plug(struct blk_plug *);
1158 extern void blk_finish_plug(struct blk_plug *);
1159 extern void blk_flush_plug_list(struct blk_plug *, bool);
1161 static inline void blk_flush_plug(struct task_struct *tsk)
1163 struct blk_plug *plug = tsk->plug;
1166 blk_flush_plug_list(plug, false);
1169 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1171 struct blk_plug *plug = tsk->plug;
1174 blk_flush_plug_list(plug, true);
1177 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1179 struct blk_plug *plug = tsk->plug;
1182 (!list_empty(&plug->mq_list) ||
1183 !list_empty(&plug->cb_list));
1186 extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
1187 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1188 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1190 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1192 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1193 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1194 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1195 sector_t nr_sects, gfp_t gfp_mask, int flags,
1198 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1199 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1201 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1202 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1204 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1205 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1207 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1208 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1210 return blkdev_issue_discard(sb->s_bdev,
1211 block << (sb->s_blocksize_bits -
1213 nr_blocks << (sb->s_blocksize_bits -
1217 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1218 sector_t nr_blocks, gfp_t gfp_mask)
1220 return blkdev_issue_zeroout(sb->s_bdev,
1221 block << (sb->s_blocksize_bits -
1223 nr_blocks << (sb->s_blocksize_bits -
1228 extern int blk_verify_command(unsigned char *cmd, fmode_t mode);
1230 enum blk_default_limits {
1231 BLK_MAX_SEGMENTS = 128,
1232 BLK_SAFE_MAX_SECTORS = 255,
1233 BLK_DEF_MAX_SECTORS = 2560,
1234 BLK_MAX_SEGMENT_SIZE = 65536,
1235 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1238 static inline unsigned long queue_segment_boundary(struct request_queue *q)
1240 return q->limits.seg_boundary_mask;
1243 static inline unsigned long queue_virt_boundary(struct request_queue *q)
1245 return q->limits.virt_boundary_mask;
1248 static inline unsigned int queue_max_sectors(struct request_queue *q)
1250 return q->limits.max_sectors;
1253 static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
1255 return q->limits.max_hw_sectors;
1258 static inline unsigned short queue_max_segments(struct request_queue *q)
1260 return q->limits.max_segments;
1263 static inline unsigned short queue_max_discard_segments(struct request_queue *q)
1265 return q->limits.max_discard_segments;
1268 static inline unsigned int queue_max_segment_size(struct request_queue *q)
1270 return q->limits.max_segment_size;
1273 static inline unsigned short queue_logical_block_size(struct request_queue *q)
1277 if (q && q->limits.logical_block_size)
1278 retval = q->limits.logical_block_size;
1283 static inline unsigned short bdev_logical_block_size(struct block_device *bdev)
1285 return queue_logical_block_size(bdev_get_queue(bdev));
1288 static inline unsigned int queue_physical_block_size(struct request_queue *q)
1290 return q->limits.physical_block_size;
1293 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1295 return queue_physical_block_size(bdev_get_queue(bdev));
1298 static inline unsigned int queue_io_min(struct request_queue *q)
1300 return q->limits.io_min;
1303 static inline int bdev_io_min(struct block_device *bdev)
1305 return queue_io_min(bdev_get_queue(bdev));
1308 static inline unsigned int queue_io_opt(struct request_queue *q)
1310 return q->limits.io_opt;
1313 static inline int bdev_io_opt(struct block_device *bdev)
1315 return queue_io_opt(bdev_get_queue(bdev));
1318 static inline int queue_alignment_offset(struct request_queue *q)
1320 if (q->limits.misaligned)
1323 return q->limits.alignment_offset;
1326 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1328 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1329 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1332 return (granularity + lim->alignment_offset - alignment) % granularity;
1335 static inline int bdev_alignment_offset(struct block_device *bdev)
1337 struct request_queue *q = bdev_get_queue(bdev);
1339 if (q->limits.misaligned)
1342 if (bdev != bdev->bd_contains)
1343 return bdev->bd_part->alignment_offset;
1345 return q->limits.alignment_offset;
1348 static inline int queue_discard_alignment(struct request_queue *q)
1350 if (q->limits.discard_misaligned)
1353 return q->limits.discard_alignment;
1356 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1358 unsigned int alignment, granularity, offset;
1360 if (!lim->max_discard_sectors)
1363 /* Why are these in bytes, not sectors? */
1364 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1365 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1369 /* Offset of the partition start in 'granularity' sectors */
1370 offset = sector_div(sector, granularity);
1372 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1373 offset = (granularity + alignment - offset) % granularity;
1375 /* Turn it back into bytes, gaah */
1376 return offset << SECTOR_SHIFT;
1379 static inline int bdev_discard_alignment(struct block_device *bdev)
1381 struct request_queue *q = bdev_get_queue(bdev);
1383 if (bdev != bdev->bd_contains)
1384 return bdev->bd_part->discard_alignment;
1386 return q->limits.discard_alignment;
1389 static inline unsigned int bdev_write_same(struct block_device *bdev)
1391 struct request_queue *q = bdev_get_queue(bdev);
1394 return q->limits.max_write_same_sectors;
1399 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1401 struct request_queue *q = bdev_get_queue(bdev);
1404 return q->limits.max_write_zeroes_sectors;
1409 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1411 struct request_queue *q = bdev_get_queue(bdev);
1414 return blk_queue_zoned_model(q);
1416 return BLK_ZONED_NONE;
1419 static inline bool bdev_is_zoned(struct block_device *bdev)
1421 struct request_queue *q = bdev_get_queue(bdev);
1424 return blk_queue_is_zoned(q);
1429 static inline unsigned int bdev_zone_sectors(struct block_device *bdev)
1431 struct request_queue *q = bdev_get_queue(bdev);
1434 return blk_queue_zone_sectors(q);
1438 static inline int queue_dma_alignment(struct request_queue *q)
1440 return q ? q->dma_alignment : 511;
1443 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1446 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1447 return !(addr & alignment) && !(len & alignment);
1450 /* assumes size > 256 */
1451 static inline unsigned int blksize_bits(unsigned int size)
1453 unsigned int bits = 8;
1457 } while (size > 256);
1461 static inline unsigned int block_size(struct block_device *bdev)
1463 return bdev->bd_block_size;
1466 static inline bool queue_flush_queueable(struct request_queue *q)
1468 return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
1471 typedef struct {struct page *v;} Sector;
1473 unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
1475 static inline void put_dev_sector(Sector p)
1480 int kblockd_schedule_work(struct work_struct *work);
1481 int kblockd_schedule_work_on(int cpu, struct work_struct *work);
1482 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1484 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1485 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1486 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1487 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1489 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1491 enum blk_integrity_flags {
1492 BLK_INTEGRITY_VERIFY = 1 << 0,
1493 BLK_INTEGRITY_GENERATE = 1 << 1,
1494 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1495 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1498 struct blk_integrity_iter {
1502 unsigned int data_size;
1503 unsigned short interval;
1504 const char *disk_name;
1507 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1509 struct blk_integrity_profile {
1510 integrity_processing_fn *generate_fn;
1511 integrity_processing_fn *verify_fn;
1515 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1516 extern void blk_integrity_unregister(struct gendisk *);
1517 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1518 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1519 struct scatterlist *);
1520 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1521 extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
1523 extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
1526 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1528 struct blk_integrity *bi = &disk->queue->integrity;
1537 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1539 return blk_get_integrity(bdev->bd_disk);
1542 static inline bool blk_integrity_rq(struct request *rq)
1544 return rq->cmd_flags & REQ_INTEGRITY;
1547 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1550 q->limits.max_integrity_segments = segs;
1553 static inline unsigned short
1554 queue_max_integrity_segments(struct request_queue *q)
1556 return q->limits.max_integrity_segments;
1560 * bio_integrity_intervals - Return number of integrity intervals for a bio
1561 * @bi: blk_integrity profile for device
1562 * @sectors: Size of the bio in 512-byte sectors
1564 * Description: The block layer calculates everything in 512 byte
1565 * sectors but integrity metadata is done in terms of the data integrity
1566 * interval size of the storage device. Convert the block layer sectors
1567 * to the appropriate number of integrity intervals.
1569 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1570 unsigned int sectors)
1572 return sectors >> (bi->interval_exp - 9);
1575 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1576 unsigned int sectors)
1578 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1581 #else /* CONFIG_BLK_DEV_INTEGRITY */
1584 struct block_device;
1586 struct blk_integrity;
1588 static inline int blk_integrity_rq(struct request *rq)
1592 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1597 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1599 struct scatterlist *s)
1603 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1607 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1611 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1615 static inline void blk_integrity_register(struct gendisk *d,
1616 struct blk_integrity *b)
1619 static inline void blk_integrity_unregister(struct gendisk *d)
1622 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1626 static inline unsigned short queue_max_integrity_segments(struct request_queue *q)
1630 static inline bool blk_integrity_merge_rq(struct request_queue *rq,
1636 static inline bool blk_integrity_merge_bio(struct request_queue *rq,
1643 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1644 unsigned int sectors)
1649 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1650 unsigned int sectors)
1655 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1657 struct block_device_operations {
1658 int (*open) (struct block_device *, fmode_t);
1659 void (*release) (struct gendisk *, fmode_t);
1660 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1661 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1662 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1663 unsigned int (*check_events) (struct gendisk *disk,
1664 unsigned int clearing);
1665 /* ->media_changed() is DEPRECATED, use ->check_events() instead */
1666 int (*media_changed) (struct gendisk *);
1667 void (*unlock_native_capacity) (struct gendisk *);
1668 int (*revalidate_disk) (struct gendisk *);
1669 int (*getgeo)(struct block_device *, struct hd_geometry *);
1670 /* this callback is with swap_lock and sometimes page table lock held */
1671 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1672 int (*report_zones)(struct gendisk *, sector_t sector,
1673 struct blk_zone *zones, unsigned int *nr_zones,
1675 struct module *owner;
1676 const struct pr_ops *pr_ops;
1679 extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
1681 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1682 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1683 struct writeback_control *);
1685 #ifdef CONFIG_BLK_DEV_ZONED
1686 bool blk_req_needs_zone_write_lock(struct request *rq);
1687 void __blk_req_zone_write_lock(struct request *rq);
1688 void __blk_req_zone_write_unlock(struct request *rq);
1690 static inline void blk_req_zone_write_lock(struct request *rq)
1692 if (blk_req_needs_zone_write_lock(rq))
1693 __blk_req_zone_write_lock(rq);
1696 static inline void blk_req_zone_write_unlock(struct request *rq)
1698 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1699 __blk_req_zone_write_unlock(rq);
1702 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1704 return rq->q->seq_zones_wlock &&
1705 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1708 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1710 if (!blk_req_needs_zone_write_lock(rq))
1712 return !blk_req_zone_is_write_locked(rq);
1715 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1720 static inline void blk_req_zone_write_lock(struct request *rq)
1724 static inline void blk_req_zone_write_unlock(struct request *rq)
1727 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1732 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1736 #endif /* CONFIG_BLK_DEV_ZONED */
1738 #else /* CONFIG_BLOCK */
1740 struct block_device;
1743 * stubs for when the block layer is configured out
1745 #define buffer_heads_over_limit 0
1747 static inline long nr_blockdev_pages(void)
1755 static inline void blk_start_plug(struct blk_plug *plug)
1759 static inline void blk_finish_plug(struct blk_plug *plug)
1763 static inline void blk_flush_plug(struct task_struct *task)
1767 static inline void blk_schedule_flush_plug(struct task_struct *task)
1772 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1777 static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
1778 sector_t *error_sector)
1783 #endif /* CONFIG_BLOCK */