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>
7 #include <linux/major.h>
8 #include <linux/genhd.h>
9 #include <linux/list.h>
10 #include <linux/llist.h>
11 #include <linux/minmax.h>
12 #include <linux/timer.h>
13 #include <linux/workqueue.h>
14 #include <linux/wait.h>
15 #include <linux/mempool.h>
16 #include <linux/pfn.h>
17 #include <linux/bio.h>
18 #include <linux/stringify.h>
19 #include <linux/gfp.h>
20 #include <linux/smp.h>
21 #include <linux/rcupdate.h>
22 #include <linux/percpu-refcount.h>
23 #include <linux/scatterlist.h>
24 #include <linux/blkzoned.h>
26 #include <linux/sbitmap.h>
30 struct elevator_queue;
35 struct blk_flush_queue;
38 struct blk_queue_stats;
39 struct blk_stat_callback;
40 struct blk_keyslot_manager;
42 #define BLKDEV_MIN_RQ 4
43 #define BLKDEV_MAX_RQ 128 /* Default maximum */
45 /* Must be consistent with blk_mq_poll_stats_bkt() */
46 #define BLK_MQ_POLL_STATS_BKTS 16
48 /* Doing classic polling */
49 #define BLK_MQ_POLL_CLASSIC -1
52 * Maximum number of blkcg policies allowed to be registered concurrently.
53 * Defined here to simplify include dependency.
55 #define BLKCG_MAX_POLS 6
57 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
61 typedef __u32 __bitwise req_flags_t;
63 /* drive already may have started this one */
64 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
65 /* may not be passed by ioscheduler */
66 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
67 /* request for flush sequence */
68 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
69 /* merge of different types, fail separately */
70 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
71 /* track inflight for MQ */
72 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
73 /* don't call prep for this one */
74 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
75 /* vaguely specified driver internal error. Ignored by the block layer */
76 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
77 /* don't warn about errors */
78 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
79 /* elevator private data attached */
80 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
81 /* account into disk and partition IO statistics */
82 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
83 /* runtime pm request */
84 #define RQF_PM ((__force req_flags_t)(1 << 15))
85 /* on IO scheduler merge hash */
86 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
87 /* track IO completion time */
88 #define RQF_STATS ((__force req_flags_t)(1 << 17))
89 /* Look at ->special_vec for the actual data payload instead of the
91 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
92 /* The per-zone write lock is held for this request */
93 #define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
94 /* already slept for hybrid poll */
95 #define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
96 /* ->timeout has been called, don't expire again */
97 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
99 /* flags that prevent us from merging requests: */
100 #define RQF_NOMERGE_FLAGS \
101 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
104 * Request state for blk-mq.
113 * Try to put the fields that are referenced together in the same cacheline.
115 * If you modify this structure, make sure to update blk_rq_init() and
116 * especially blk_mq_rq_ctx_init() to take care of the added fields.
119 struct request_queue *q;
120 struct blk_mq_ctx *mq_ctx;
121 struct blk_mq_hw_ctx *mq_hctx;
123 unsigned int cmd_flags; /* op and common flags */
124 req_flags_t rq_flags;
129 /* the following two fields are internal, NEVER access directly */
130 unsigned int __data_len; /* total data len */
131 sector_t __sector; /* sector cursor */
136 struct list_head queuelist;
139 * The hash is used inside the scheduler, and killed once the
140 * request reaches the dispatch list. The ipi_list is only used
141 * to queue the request for softirq completion, which is long
142 * after the request has been unhashed (and even removed from
143 * the dispatch list).
146 struct hlist_node hash; /* merge hash */
147 struct llist_node ipi_list;
151 * The rb_node is only used inside the io scheduler, requests
152 * are pruned when moved to the dispatch queue. So let the
153 * completion_data share space with the rb_node.
156 struct rb_node rb_node; /* sort/lookup */
157 struct bio_vec special_vec;
158 void *completion_data;
159 int error_count; /* for legacy drivers, don't use */
163 * Three pointers are available for the IO schedulers, if they need
164 * more they have to dynamically allocate it. Flush requests are
165 * never put on the IO scheduler. So let the flush fields share
166 * space with the elevator data.
176 struct list_head list;
177 rq_end_io_fn *saved_end_io;
181 struct gendisk *rq_disk;
182 struct block_device *part;
183 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
184 /* Time that the first bio started allocating this request. */
187 /* Time that this request was allocated for this IO. */
189 /* Time that I/O was submitted to the device. */
190 u64 io_start_time_ns;
192 #ifdef CONFIG_BLK_WBT
193 unsigned short wbt_flags;
196 * rq sectors used for blk stats. It has the same value
197 * with blk_rq_sectors(rq), except that it never be zeroed
200 unsigned short stats_sectors;
203 * Number of scatter-gather DMA addr+len pairs after
204 * physical address coalescing is performed.
206 unsigned short nr_phys_segments;
208 #if defined(CONFIG_BLK_DEV_INTEGRITY)
209 unsigned short nr_integrity_segments;
212 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
213 struct bio_crypt_ctx *crypt_ctx;
214 struct blk_ksm_keyslot *crypt_keyslot;
217 unsigned short write_hint;
218 unsigned short ioprio;
220 enum mq_rq_state state;
223 unsigned int timeout;
224 unsigned long deadline;
227 struct __call_single_data csd;
232 * completion callback.
234 rq_end_io_fn *end_io;
238 static inline bool blk_op_is_passthrough(unsigned int op)
241 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
244 static inline bool blk_rq_is_passthrough(struct request *rq)
246 return blk_op_is_passthrough(req_op(rq));
249 static inline unsigned short req_get_ioprio(struct request *req)
254 #include <linux/elevator.h>
256 struct blk_queue_ctx;
260 enum blk_eh_timer_return {
261 BLK_EH_DONE, /* drivers has completed the command */
262 BLK_EH_RESET_TIMER, /* reset timer and try again */
265 enum blk_queue_state {
270 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
271 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
274 * Zoned block device models (zoned limit).
276 * Note: This needs to be ordered from the least to the most severe
277 * restrictions for the inheritance in blk_stack_limits() to work.
279 enum blk_zoned_model {
280 BLK_ZONED_NONE = 0, /* Regular block device */
281 BLK_ZONED_HA, /* Host-aware zoned block device */
282 BLK_ZONED_HM, /* Host-managed zoned block device */
286 * BLK_BOUNCE_NONE: never bounce (default)
287 * BLK_BOUNCE_HIGH: bounce all highmem pages
294 struct queue_limits {
295 enum blk_bounce bounce;
296 unsigned long seg_boundary_mask;
297 unsigned long virt_boundary_mask;
299 unsigned int max_hw_sectors;
300 unsigned int max_dev_sectors;
301 unsigned int chunk_sectors;
302 unsigned int max_sectors;
303 unsigned int max_segment_size;
304 unsigned int physical_block_size;
305 unsigned int logical_block_size;
306 unsigned int alignment_offset;
309 unsigned int max_discard_sectors;
310 unsigned int max_hw_discard_sectors;
311 unsigned int max_write_same_sectors;
312 unsigned int max_write_zeroes_sectors;
313 unsigned int max_zone_append_sectors;
314 unsigned int discard_granularity;
315 unsigned int discard_alignment;
316 unsigned int zone_write_granularity;
318 unsigned short max_segments;
319 unsigned short max_integrity_segments;
320 unsigned short max_discard_segments;
322 unsigned char misaligned;
323 unsigned char discard_misaligned;
324 unsigned char raid_partial_stripes_expensive;
325 enum blk_zoned_model zoned;
328 typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
331 void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model);
333 #ifdef CONFIG_BLK_DEV_ZONED
335 #define BLK_ALL_ZONES ((unsigned int)-1)
336 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
337 unsigned int nr_zones, report_zones_cb cb, void *data);
338 unsigned int blkdev_nr_zones(struct gendisk *disk);
339 extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
340 sector_t sectors, sector_t nr_sectors,
342 int blk_revalidate_disk_zones(struct gendisk *disk,
343 void (*update_driver_data)(struct gendisk *disk));
345 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
346 unsigned int cmd, unsigned long arg);
347 extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
348 unsigned int cmd, unsigned long arg);
350 #else /* CONFIG_BLK_DEV_ZONED */
352 static inline unsigned int blkdev_nr_zones(struct gendisk *disk)
357 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
358 fmode_t mode, unsigned int cmd,
364 static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
365 fmode_t mode, unsigned int cmd,
371 #endif /* CONFIG_BLK_DEV_ZONED */
373 struct request_queue {
374 struct request *last_merge;
375 struct elevator_queue *elevator;
377 struct percpu_ref q_usage_counter;
379 struct blk_queue_stats *stats;
380 struct rq_qos *rq_qos;
382 const struct blk_mq_ops *mq_ops;
385 struct blk_mq_ctx __percpu *queue_ctx;
387 unsigned int queue_depth;
389 /* hw dispatch queues */
390 struct blk_mq_hw_ctx **queue_hw_ctx;
391 unsigned int nr_hw_queues;
394 * The queue owner gets to use this for whatever they like.
395 * ll_rw_blk doesn't touch it.
400 * various queue flags, see QUEUE_* below
402 unsigned long queue_flags;
404 * Number of contexts that have called blk_set_pm_only(). If this
405 * counter is above zero then only RQF_PM requests are processed.
410 * ida allocated id for this queue. Used to index queues from
415 spinlock_t queue_lock;
417 struct gendisk *disk;
427 struct kobject *mq_kobj;
429 #ifdef CONFIG_BLK_DEV_INTEGRITY
430 struct blk_integrity integrity;
431 #endif /* CONFIG_BLK_DEV_INTEGRITY */
435 enum rpm_status rpm_status;
441 unsigned long nr_requests; /* Max # of requests */
443 unsigned int dma_pad_mask;
444 unsigned int dma_alignment;
446 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
447 /* Inline crypto capabilities */
448 struct blk_keyslot_manager *ksm;
451 unsigned int rq_timeout;
454 struct blk_stat_callback *poll_cb;
455 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
457 struct timer_list timeout;
458 struct work_struct timeout_work;
460 atomic_t nr_active_requests_shared_sbitmap;
462 struct sbitmap_queue sched_bitmap_tags;
463 struct sbitmap_queue sched_breserved_tags;
465 struct list_head icq_list;
466 #ifdef CONFIG_BLK_CGROUP
467 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
468 struct blkcg_gq *root_blkg;
469 struct list_head blkg_list;
472 struct queue_limits limits;
474 unsigned int required_elevator_features;
476 #ifdef CONFIG_BLK_DEV_ZONED
478 * Zoned block device information for request dispatch control.
479 * nr_zones is the total number of zones of the device. This is always
480 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
481 * bits which indicates if a zone is conventional (bit set) or
482 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
483 * bits which indicates if a zone is write locked, that is, if a write
484 * request targeting the zone was dispatched. All three fields are
485 * initialized by the low level device driver (e.g. scsi/sd.c).
486 * Stacking drivers (device mappers) may or may not initialize
489 * Reads of this information must be protected with blk_queue_enter() /
490 * blk_queue_exit(). Modifying this information is only allowed while
491 * no requests are being processed. See also blk_mq_freeze_queue() and
492 * blk_mq_unfreeze_queue().
494 unsigned int nr_zones;
495 unsigned long *conv_zones_bitmap;
496 unsigned long *seq_zones_wlock;
497 unsigned int max_open_zones;
498 unsigned int max_active_zones;
499 #endif /* CONFIG_BLK_DEV_ZONED */
502 struct mutex debugfs_mutex;
503 #ifdef CONFIG_BLK_DEV_IO_TRACE
504 struct blk_trace __rcu *blk_trace;
507 * for flush operations
509 struct blk_flush_queue *fq;
511 struct list_head requeue_list;
512 spinlock_t requeue_lock;
513 struct delayed_work requeue_work;
515 struct mutex sysfs_lock;
516 struct mutex sysfs_dir_lock;
519 * for reusing dead hctx instance in case of updating
522 struct list_head unused_hctx_list;
523 spinlock_t unused_hctx_lock;
527 #ifdef CONFIG_BLK_DEV_THROTTLING
529 struct throtl_data *td;
531 struct rcu_head rcu_head;
532 wait_queue_head_t mq_freeze_wq;
534 * Protect concurrent access to q_usage_counter by
535 * percpu_ref_kill() and percpu_ref_reinit().
537 struct mutex mq_freeze_lock;
539 struct blk_mq_tag_set *tag_set;
540 struct list_head tag_set_list;
541 struct bio_set bio_split;
543 struct dentry *debugfs_dir;
545 #ifdef CONFIG_BLK_DEBUG_FS
546 struct dentry *sched_debugfs_dir;
547 struct dentry *rqos_debugfs_dir;
550 bool mq_sysfs_init_done;
554 #define BLK_MAX_WRITE_HINTS 5
555 u64 write_hints[BLK_MAX_WRITE_HINTS];
558 /* Keep blk_queue_flag_name[] in sync with the definitions below */
559 #define QUEUE_FLAG_STOPPED 0 /* queue is stopped */
560 #define QUEUE_FLAG_DYING 1 /* queue being torn down */
561 #define QUEUE_FLAG_NOMERGES 3 /* disable merge attempts */
562 #define QUEUE_FLAG_SAME_COMP 4 /* complete on same CPU-group */
563 #define QUEUE_FLAG_FAIL_IO 5 /* fake timeout */
564 #define QUEUE_FLAG_NONROT 6 /* non-rotational device (SSD) */
565 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
566 #define QUEUE_FLAG_IO_STAT 7 /* do disk/partitions IO accounting */
567 #define QUEUE_FLAG_DISCARD 8 /* supports DISCARD */
568 #define QUEUE_FLAG_NOXMERGES 9 /* No extended merges */
569 #define QUEUE_FLAG_ADD_RANDOM 10 /* Contributes to random pool */
570 #define QUEUE_FLAG_SECERASE 11 /* supports secure erase */
571 #define QUEUE_FLAG_SAME_FORCE 12 /* force complete on same CPU */
572 #define QUEUE_FLAG_DEAD 13 /* queue tear-down finished */
573 #define QUEUE_FLAG_INIT_DONE 14 /* queue is initialized */
574 #define QUEUE_FLAG_STABLE_WRITES 15 /* don't modify blks until WB is done */
575 #define QUEUE_FLAG_POLL 16 /* IO polling enabled if set */
576 #define QUEUE_FLAG_WC 17 /* Write back caching */
577 #define QUEUE_FLAG_FUA 18 /* device supports FUA writes */
578 #define QUEUE_FLAG_DAX 19 /* device supports DAX */
579 #define QUEUE_FLAG_STATS 20 /* track IO start and completion times */
580 #define QUEUE_FLAG_POLL_STATS 21 /* collecting stats for hybrid polling */
581 #define QUEUE_FLAG_REGISTERED 22 /* queue has been registered to a disk */
582 #define QUEUE_FLAG_SCSI_PASSTHROUGH 23 /* queue supports SCSI commands */
583 #define QUEUE_FLAG_QUIESCED 24 /* queue has been quiesced */
584 #define QUEUE_FLAG_PCI_P2PDMA 25 /* device supports PCI p2p requests */
585 #define QUEUE_FLAG_ZONE_RESETALL 26 /* supports Zone Reset All */
586 #define QUEUE_FLAG_RQ_ALLOC_TIME 27 /* record rq->alloc_time_ns */
587 #define QUEUE_FLAG_HCTX_ACTIVE 28 /* at least one blk-mq hctx is active */
588 #define QUEUE_FLAG_NOWAIT 29 /* device supports NOWAIT */
590 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
591 (1 << QUEUE_FLAG_SAME_COMP) | \
592 (1 << QUEUE_FLAG_NOWAIT))
594 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
595 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
596 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
598 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
599 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
600 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
601 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
602 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
603 #define blk_queue_noxmerges(q) \
604 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
605 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
606 #define blk_queue_stable_writes(q) \
607 test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
608 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
609 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
610 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
611 #define blk_queue_zone_resetall(q) \
612 test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
613 #define blk_queue_secure_erase(q) \
614 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
615 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
616 #define blk_queue_scsi_passthrough(q) \
617 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
618 #define blk_queue_pci_p2pdma(q) \
619 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
620 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
621 #define blk_queue_rq_alloc_time(q) \
622 test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
624 #define blk_queue_rq_alloc_time(q) false
627 #define blk_noretry_request(rq) \
628 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
629 REQ_FAILFAST_DRIVER))
630 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
631 #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
632 #define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
633 #define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
634 #define blk_queue_nowait(q) test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
636 extern void blk_set_pm_only(struct request_queue *q);
637 extern void blk_clear_pm_only(struct request_queue *q);
639 #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
641 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
643 #define rq_dma_dir(rq) \
644 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
646 #define dma_map_bvec(dev, bv, dir, attrs) \
647 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
650 static inline bool queue_is_mq(struct request_queue *q)
656 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
658 return q->rpm_status;
661 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
667 static inline enum blk_zoned_model
668 blk_queue_zoned_model(struct request_queue *q)
670 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
671 return q->limits.zoned;
672 return BLK_ZONED_NONE;
675 static inline bool blk_queue_is_zoned(struct request_queue *q)
677 switch (blk_queue_zoned_model(q)) {
686 static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
688 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
691 #ifdef CONFIG_BLK_DEV_ZONED
692 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
694 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
697 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
700 if (!blk_queue_is_zoned(q))
702 return sector >> ilog2(q->limits.chunk_sectors);
705 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
708 if (!blk_queue_is_zoned(q))
710 if (!q->conv_zones_bitmap)
712 return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
715 static inline void blk_queue_max_open_zones(struct request_queue *q,
716 unsigned int max_open_zones)
718 q->max_open_zones = max_open_zones;
721 static inline unsigned int queue_max_open_zones(const struct request_queue *q)
723 return q->max_open_zones;
726 static inline void blk_queue_max_active_zones(struct request_queue *q,
727 unsigned int max_active_zones)
729 q->max_active_zones = max_active_zones;
732 static inline unsigned int queue_max_active_zones(const struct request_queue *q)
734 return q->max_active_zones;
736 #else /* CONFIG_BLK_DEV_ZONED */
737 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
741 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
746 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
751 static inline unsigned int queue_max_open_zones(const struct request_queue *q)
755 static inline unsigned int queue_max_active_zones(const struct request_queue *q)
759 #endif /* CONFIG_BLK_DEV_ZONED */
761 static inline bool rq_is_sync(struct request *rq)
763 return op_is_sync(rq->cmd_flags);
766 static inline bool rq_mergeable(struct request *rq)
768 if (blk_rq_is_passthrough(rq))
771 if (req_op(rq) == REQ_OP_FLUSH)
774 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
777 if (req_op(rq) == REQ_OP_ZONE_APPEND)
780 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
782 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
788 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
790 if (bio_page(a) == bio_page(b) &&
791 bio_offset(a) == bio_offset(b))
797 static inline unsigned int blk_queue_depth(struct request_queue *q)
800 return q->queue_depth;
802 return q->nr_requests;
806 * default timeout for SG_IO if none specified
808 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
809 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
815 unsigned long offset;
820 struct req_iterator {
821 struct bvec_iter iter;
825 /* This should not be used directly - use rq_for_each_segment */
826 #define for_each_bio(_bio) \
827 for (; _bio; _bio = _bio->bi_next)
828 #define __rq_for_each_bio(_bio, rq) \
830 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
832 #define rq_for_each_segment(bvl, _rq, _iter) \
833 __rq_for_each_bio(_iter.bio, _rq) \
834 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
836 #define rq_for_each_bvec(bvl, _rq, _iter) \
837 __rq_for_each_bio(_iter.bio, _rq) \
838 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
840 #define rq_iter_last(bvec, _iter) \
841 (_iter.bio->bi_next == NULL && \
842 bio_iter_last(bvec, _iter.iter))
844 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
845 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
847 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
848 extern void rq_flush_dcache_pages(struct request *rq);
850 static inline void rq_flush_dcache_pages(struct request *rq)
855 extern int blk_register_queue(struct gendisk *disk);
856 extern void blk_unregister_queue(struct gendisk *disk);
857 blk_qc_t submit_bio_noacct(struct bio *bio);
858 extern void blk_rq_init(struct request_queue *q, struct request *rq);
859 extern void blk_put_request(struct request *);
860 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
861 blk_mq_req_flags_t flags);
862 extern int blk_lld_busy(struct request_queue *q);
863 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
864 struct bio_set *bs, gfp_t gfp_mask,
865 int (*bio_ctr)(struct bio *, struct bio *, void *),
867 extern void blk_rq_unprep_clone(struct request *rq);
868 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
870 int blk_rq_append_bio(struct request *rq, struct bio *bio);
871 extern void blk_queue_split(struct bio **);
872 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
873 extern void blk_queue_exit(struct request_queue *q);
874 extern void blk_sync_queue(struct request_queue *q);
875 extern int blk_rq_map_user(struct request_queue *, struct request *,
876 struct rq_map_data *, void __user *, unsigned long,
878 extern int blk_rq_unmap_user(struct bio *);
879 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
880 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
881 struct rq_map_data *, const struct iov_iter *,
883 extern void blk_execute_rq_nowait(struct gendisk *,
884 struct request *, int, rq_end_io_fn *);
886 blk_status_t blk_execute_rq(struct gendisk *bd_disk, struct request *rq,
889 /* Helper to convert REQ_OP_XXX to its string format XXX */
890 extern const char *blk_op_str(unsigned int op);
892 int blk_status_to_errno(blk_status_t status);
893 blk_status_t errno_to_blk_status(int errno);
895 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
897 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
899 return bdev->bd_disk->queue; /* this is never NULL */
903 * The basic unit of block I/O is a sector. It is used in a number of contexts
904 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
905 * bytes. Variables of type sector_t represent an offset or size that is a
906 * multiple of 512 bytes. Hence these two constants.
909 #define SECTOR_SHIFT 9
912 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
915 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
916 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
917 #define SECTOR_MASK (PAGE_SECTORS - 1)
920 * blk_rq_pos() : the current sector
921 * blk_rq_bytes() : bytes left in the entire request
922 * blk_rq_cur_bytes() : bytes left in the current segment
923 * blk_rq_err_bytes() : bytes left till the next error boundary
924 * blk_rq_sectors() : sectors left in the entire request
925 * blk_rq_cur_sectors() : sectors left in the current segment
926 * blk_rq_stats_sectors() : sectors of the entire request used for stats
928 static inline sector_t blk_rq_pos(const struct request *rq)
933 static inline unsigned int blk_rq_bytes(const struct request *rq)
935 return rq->__data_len;
938 static inline int blk_rq_cur_bytes(const struct request *rq)
940 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
943 extern unsigned int blk_rq_err_bytes(const struct request *rq);
945 static inline unsigned int blk_rq_sectors(const struct request *rq)
947 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
950 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
952 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
955 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
957 return rq->stats_sectors;
960 #ifdef CONFIG_BLK_DEV_ZONED
962 /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
963 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
965 static inline unsigned int bio_zone_no(struct bio *bio)
967 return blk_queue_zone_no(bdev_get_queue(bio->bi_bdev),
968 bio->bi_iter.bi_sector);
971 static inline unsigned int bio_zone_is_seq(struct bio *bio)
973 return blk_queue_zone_is_seq(bdev_get_queue(bio->bi_bdev),
974 bio->bi_iter.bi_sector);
977 static inline unsigned int blk_rq_zone_no(struct request *rq)
979 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
982 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
984 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
986 #endif /* CONFIG_BLK_DEV_ZONED */
989 * Some commands like WRITE SAME have a payload or data transfer size which
990 * is different from the size of the request. Any driver that supports such
991 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
992 * calculate the data transfer size.
994 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
996 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
997 return rq->special_vec.bv_len;
998 return blk_rq_bytes(rq);
1002 * Return the first full biovec in the request. The caller needs to check that
1003 * there are any bvecs before calling this helper.
1005 static inline struct bio_vec req_bvec(struct request *rq)
1007 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1008 return rq->special_vec;
1009 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1012 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1015 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1016 return min(q->limits.max_discard_sectors,
1017 UINT_MAX >> SECTOR_SHIFT);
1019 if (unlikely(op == REQ_OP_WRITE_SAME))
1020 return q->limits.max_write_same_sectors;
1022 if (unlikely(op == REQ_OP_WRITE_ZEROES))
1023 return q->limits.max_write_zeroes_sectors;
1025 return q->limits.max_sectors;
1029 * Return maximum size of a request at given offset. Only valid for
1030 * file system requests.
1032 static inline unsigned int blk_max_size_offset(struct request_queue *q,
1034 unsigned int chunk_sectors)
1036 if (!chunk_sectors) {
1037 if (q->limits.chunk_sectors)
1038 chunk_sectors = q->limits.chunk_sectors;
1040 return q->limits.max_sectors;
1043 if (likely(is_power_of_2(chunk_sectors)))
1044 chunk_sectors -= offset & (chunk_sectors - 1);
1046 chunk_sectors -= sector_div(offset, chunk_sectors);
1048 return min(q->limits.max_sectors, chunk_sectors);
1051 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1054 struct request_queue *q = rq->q;
1056 if (blk_rq_is_passthrough(rq))
1057 return q->limits.max_hw_sectors;
1059 if (!q->limits.chunk_sectors ||
1060 req_op(rq) == REQ_OP_DISCARD ||
1061 req_op(rq) == REQ_OP_SECURE_ERASE)
1062 return blk_queue_get_max_sectors(q, req_op(rq));
1064 return min(blk_max_size_offset(q, offset, 0),
1065 blk_queue_get_max_sectors(q, req_op(rq)));
1068 static inline unsigned int blk_rq_count_bios(struct request *rq)
1070 unsigned int nr_bios = 0;
1073 __rq_for_each_bio(bio, rq)
1079 void blk_steal_bios(struct bio_list *list, struct request *rq);
1082 * Request completion related functions.
1084 * blk_update_request() completes given number of bytes and updates
1085 * the request without completing it.
1087 extern bool blk_update_request(struct request *rq, blk_status_t error,
1088 unsigned int nr_bytes);
1090 extern void blk_abort_request(struct request *);
1093 * Access functions for manipulating queue properties
1095 extern void blk_cleanup_queue(struct request_queue *);
1096 void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
1097 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1098 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1099 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1100 extern void blk_queue_max_discard_segments(struct request_queue *,
1102 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1103 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1104 unsigned int max_discard_sectors);
1105 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1106 unsigned int max_write_same_sectors);
1107 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1108 unsigned int max_write_same_sectors);
1109 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
1110 extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
1111 unsigned int max_zone_append_sectors);
1112 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1113 void blk_queue_zone_write_granularity(struct request_queue *q,
1115 extern void blk_queue_alignment_offset(struct request_queue *q,
1116 unsigned int alignment);
1117 void disk_update_readahead(struct gendisk *disk);
1118 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1119 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1120 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1121 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1122 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1123 extern void blk_set_default_limits(struct queue_limits *lim);
1124 extern void blk_set_stacking_limits(struct queue_limits *lim);
1125 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1127 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1129 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1130 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1131 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1132 extern void blk_queue_dma_alignment(struct request_queue *, int);
1133 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1134 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1135 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1136 extern void blk_queue_required_elevator_features(struct request_queue *q,
1137 unsigned int features);
1138 extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
1139 struct device *dev);
1142 * Number of physical segments as sent to the device.
1144 * Normally this is the number of discontiguous data segments sent by the
1145 * submitter. But for data-less command like discard we might have no
1146 * actual data segments submitted, but the driver might have to add it's
1147 * own special payload. In that case we still return 1 here so that this
1148 * special payload will be mapped.
1150 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1152 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1154 return rq->nr_phys_segments;
1158 * Number of discard segments (or ranges) the driver needs to fill in.
1159 * Each discard bio merged into a request is counted as one segment.
1161 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1163 return max_t(unsigned short, rq->nr_phys_segments, 1);
1166 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1167 struct scatterlist *sglist, struct scatterlist **last_sg);
1168 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1169 struct scatterlist *sglist)
1171 struct scatterlist *last_sg = NULL;
1173 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1175 extern void blk_dump_rq_flags(struct request *, char *);
1177 bool __must_check blk_get_queue(struct request_queue *);
1178 extern void blk_put_queue(struct request_queue *);
1179 extern void blk_set_queue_dying(struct request_queue *);
1183 * blk_plug permits building a queue of related requests by holding the I/O
1184 * fragments for a short period. This allows merging of sequential requests
1185 * into single larger request. As the requests are moved from a per-task list to
1186 * the device's request_queue in a batch, this results in improved scalability
1187 * as the lock contention for request_queue lock is reduced.
1189 * It is ok not to disable preemption when adding the request to the plug list
1190 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1191 * the plug list when the task sleeps by itself. For details, please see
1192 * schedule() where blk_schedule_flush_plug() is called.
1195 struct list_head mq_list; /* blk-mq requests */
1196 struct list_head cb_list; /* md requires an unplug callback */
1197 unsigned short rq_count;
1198 bool multiple_queues;
1201 #define BLK_MAX_REQUEST_COUNT 16
1202 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
1205 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1206 struct blk_plug_cb {
1207 struct list_head list;
1208 blk_plug_cb_fn callback;
1211 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1212 void *data, int size);
1213 extern void blk_start_plug(struct blk_plug *);
1214 extern void blk_finish_plug(struct blk_plug *);
1215 extern void blk_flush_plug_list(struct blk_plug *, bool);
1217 static inline void blk_flush_plug(struct task_struct *tsk)
1219 struct blk_plug *plug = tsk->plug;
1222 blk_flush_plug_list(plug, false);
1225 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1227 struct blk_plug *plug = tsk->plug;
1230 blk_flush_plug_list(plug, true);
1233 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1235 struct blk_plug *plug = tsk->plug;
1238 (!list_empty(&plug->mq_list) ||
1239 !list_empty(&plug->cb_list));
1242 int blkdev_issue_flush(struct block_device *bdev);
1243 long nr_blockdev_pages(void);
1244 #else /* CONFIG_BLOCK */
1248 static inline void blk_start_plug(struct blk_plug *plug)
1252 static inline void blk_finish_plug(struct blk_plug *plug)
1256 static inline void blk_flush_plug(struct task_struct *task)
1260 static inline void blk_schedule_flush_plug(struct task_struct *task)
1265 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1270 static inline int blkdev_issue_flush(struct block_device *bdev)
1275 static inline long nr_blockdev_pages(void)
1279 #endif /* CONFIG_BLOCK */
1281 extern void blk_io_schedule(void);
1283 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1284 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1286 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1288 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1289 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1290 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1291 sector_t nr_sects, gfp_t gfp_mask, int flags,
1294 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1295 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1297 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1298 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1300 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1301 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1303 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1304 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1306 return blkdev_issue_discard(sb->s_bdev,
1307 block << (sb->s_blocksize_bits -
1309 nr_blocks << (sb->s_blocksize_bits -
1313 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1314 sector_t nr_blocks, gfp_t gfp_mask)
1316 return blkdev_issue_zeroout(sb->s_bdev,
1317 block << (sb->s_blocksize_bits -
1319 nr_blocks << (sb->s_blocksize_bits -
1324 static inline bool bdev_is_partition(struct block_device *bdev)
1326 return bdev->bd_partno;
1329 enum blk_default_limits {
1330 BLK_MAX_SEGMENTS = 128,
1331 BLK_SAFE_MAX_SECTORS = 255,
1332 BLK_DEF_MAX_SECTORS = 2560,
1333 BLK_MAX_SEGMENT_SIZE = 65536,
1334 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1337 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1339 return q->limits.seg_boundary_mask;
1342 static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1344 return q->limits.virt_boundary_mask;
1347 static inline unsigned int queue_max_sectors(const struct request_queue *q)
1349 return q->limits.max_sectors;
1352 static inline unsigned int queue_max_bytes(struct request_queue *q)
1354 return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1357 static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1359 return q->limits.max_hw_sectors;
1362 static inline unsigned short queue_max_segments(const struct request_queue *q)
1364 return q->limits.max_segments;
1367 static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1369 return q->limits.max_discard_segments;
1372 static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1374 return q->limits.max_segment_size;
1377 static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
1380 const struct queue_limits *l = &q->limits;
1382 return min(l->max_zone_append_sectors, l->max_sectors);
1385 static inline unsigned queue_logical_block_size(const struct request_queue *q)
1389 if (q && q->limits.logical_block_size)
1390 retval = q->limits.logical_block_size;
1395 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1397 return queue_logical_block_size(bdev_get_queue(bdev));
1400 static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1402 return q->limits.physical_block_size;
1405 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1407 return queue_physical_block_size(bdev_get_queue(bdev));
1410 static inline unsigned int queue_io_min(const struct request_queue *q)
1412 return q->limits.io_min;
1415 static inline int bdev_io_min(struct block_device *bdev)
1417 return queue_io_min(bdev_get_queue(bdev));
1420 static inline unsigned int queue_io_opt(const struct request_queue *q)
1422 return q->limits.io_opt;
1425 static inline int bdev_io_opt(struct block_device *bdev)
1427 return queue_io_opt(bdev_get_queue(bdev));
1430 static inline unsigned int
1431 queue_zone_write_granularity(const struct request_queue *q)
1433 return q->limits.zone_write_granularity;
1436 static inline unsigned int
1437 bdev_zone_write_granularity(struct block_device *bdev)
1439 return queue_zone_write_granularity(bdev_get_queue(bdev));
1442 static inline int queue_alignment_offset(const struct request_queue *q)
1444 if (q->limits.misaligned)
1447 return q->limits.alignment_offset;
1450 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1452 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1453 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1456 return (granularity + lim->alignment_offset - alignment) % granularity;
1459 static inline int bdev_alignment_offset(struct block_device *bdev)
1461 struct request_queue *q = bdev_get_queue(bdev);
1463 if (q->limits.misaligned)
1465 if (bdev_is_partition(bdev))
1466 return queue_limit_alignment_offset(&q->limits,
1467 bdev->bd_start_sect);
1468 return q->limits.alignment_offset;
1471 static inline int queue_discard_alignment(const struct request_queue *q)
1473 if (q->limits.discard_misaligned)
1476 return q->limits.discard_alignment;
1479 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1481 unsigned int alignment, granularity, offset;
1483 if (!lim->max_discard_sectors)
1486 /* Why are these in bytes, not sectors? */
1487 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1488 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1492 /* Offset of the partition start in 'granularity' sectors */
1493 offset = sector_div(sector, granularity);
1495 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1496 offset = (granularity + alignment - offset) % granularity;
1498 /* Turn it back into bytes, gaah */
1499 return offset << SECTOR_SHIFT;
1503 * Two cases of handling DISCARD merge:
1504 * If max_discard_segments > 1, the driver takes every bio
1505 * as a range and send them to controller together. The ranges
1506 * needn't to be contiguous.
1507 * Otherwise, the bios/requests will be handled as same as
1508 * others which should be contiguous.
1510 static inline bool blk_discard_mergable(struct request *req)
1512 if (req_op(req) == REQ_OP_DISCARD &&
1513 queue_max_discard_segments(req->q) > 1)
1518 static inline int bdev_discard_alignment(struct block_device *bdev)
1520 struct request_queue *q = bdev_get_queue(bdev);
1522 if (bdev_is_partition(bdev))
1523 return queue_limit_discard_alignment(&q->limits,
1524 bdev->bd_start_sect);
1525 return q->limits.discard_alignment;
1528 static inline unsigned int bdev_write_same(struct block_device *bdev)
1530 struct request_queue *q = bdev_get_queue(bdev);
1533 return q->limits.max_write_same_sectors;
1538 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1540 struct request_queue *q = bdev_get_queue(bdev);
1543 return q->limits.max_write_zeroes_sectors;
1548 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1550 struct request_queue *q = bdev_get_queue(bdev);
1553 return blk_queue_zoned_model(q);
1555 return BLK_ZONED_NONE;
1558 static inline bool bdev_is_zoned(struct block_device *bdev)
1560 struct request_queue *q = bdev_get_queue(bdev);
1563 return blk_queue_is_zoned(q);
1568 static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1570 struct request_queue *q = bdev_get_queue(bdev);
1573 return blk_queue_zone_sectors(q);
1577 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
1579 struct request_queue *q = bdev_get_queue(bdev);
1582 return queue_max_open_zones(q);
1586 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
1588 struct request_queue *q = bdev_get_queue(bdev);
1591 return queue_max_active_zones(q);
1595 static inline int queue_dma_alignment(const struct request_queue *q)
1597 return q ? q->dma_alignment : 511;
1600 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1603 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1604 return !(addr & alignment) && !(len & alignment);
1607 /* assumes size > 256 */
1608 static inline unsigned int blksize_bits(unsigned int size)
1610 unsigned int bits = 8;
1614 } while (size > 256);
1618 static inline unsigned int block_size(struct block_device *bdev)
1620 return 1 << bdev->bd_inode->i_blkbits;
1623 int kblockd_schedule_work(struct work_struct *work);
1624 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1626 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1627 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1628 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1629 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1631 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1633 enum blk_integrity_flags {
1634 BLK_INTEGRITY_VERIFY = 1 << 0,
1635 BLK_INTEGRITY_GENERATE = 1 << 1,
1636 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1637 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1640 struct blk_integrity_iter {
1644 unsigned int data_size;
1645 unsigned short interval;
1646 const char *disk_name;
1649 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1650 typedef void (integrity_prepare_fn) (struct request *);
1651 typedef void (integrity_complete_fn) (struct request *, unsigned int);
1653 struct blk_integrity_profile {
1654 integrity_processing_fn *generate_fn;
1655 integrity_processing_fn *verify_fn;
1656 integrity_prepare_fn *prepare_fn;
1657 integrity_complete_fn *complete_fn;
1661 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1662 extern void blk_integrity_unregister(struct gendisk *);
1663 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1664 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1665 struct scatterlist *);
1666 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1668 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1670 struct blk_integrity *bi = &disk->queue->integrity;
1679 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1681 return blk_get_integrity(bdev->bd_disk);
1685 blk_integrity_queue_supports_integrity(struct request_queue *q)
1687 return q->integrity.profile;
1690 static inline bool blk_integrity_rq(struct request *rq)
1692 return rq->cmd_flags & REQ_INTEGRITY;
1695 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1698 q->limits.max_integrity_segments = segs;
1701 static inline unsigned short
1702 queue_max_integrity_segments(const struct request_queue *q)
1704 return q->limits.max_integrity_segments;
1708 * bio_integrity_intervals - Return number of integrity intervals for a bio
1709 * @bi: blk_integrity profile for device
1710 * @sectors: Size of the bio in 512-byte sectors
1712 * Description: The block layer calculates everything in 512 byte
1713 * sectors but integrity metadata is done in terms of the data integrity
1714 * interval size of the storage device. Convert the block layer sectors
1715 * to the appropriate number of integrity intervals.
1717 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1718 unsigned int sectors)
1720 return sectors >> (bi->interval_exp - 9);
1723 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1724 unsigned int sectors)
1726 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1730 * Return the first bvec that contains integrity data. Only drivers that are
1731 * limited to a single integrity segment should use this helper.
1733 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1735 if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
1737 return rq->bio->bi_integrity->bip_vec;
1740 #else /* CONFIG_BLK_DEV_INTEGRITY */
1743 struct block_device;
1745 struct blk_integrity;
1747 static inline int blk_integrity_rq(struct request *rq)
1751 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1756 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1758 struct scatterlist *s)
1762 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1766 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1771 blk_integrity_queue_supports_integrity(struct request_queue *q)
1775 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1779 static inline void blk_integrity_register(struct gendisk *d,
1780 struct blk_integrity *b)
1783 static inline void blk_integrity_unregister(struct gendisk *d)
1786 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1790 static inline unsigned short queue_max_integrity_segments(const struct request_queue *q)
1795 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1796 unsigned int sectors)
1801 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1802 unsigned int sectors)
1807 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1812 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1814 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1816 bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
1818 void blk_ksm_unregister(struct request_queue *q);
1820 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1822 static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
1823 struct request_queue *q)
1828 static inline void blk_ksm_unregister(struct request_queue *q) { }
1830 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1833 struct block_device_operations {
1834 blk_qc_t (*submit_bio) (struct bio *bio);
1835 int (*open) (struct block_device *, fmode_t);
1836 void (*release) (struct gendisk *, fmode_t);
1837 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1838 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1839 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1840 unsigned int (*check_events) (struct gendisk *disk,
1841 unsigned int clearing);
1842 void (*unlock_native_capacity) (struct gendisk *);
1843 int (*getgeo)(struct block_device *, struct hd_geometry *);
1844 int (*set_read_only)(struct block_device *bdev, bool ro);
1845 /* this callback is with swap_lock and sometimes page table lock held */
1846 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1847 int (*report_zones)(struct gendisk *, sector_t sector,
1848 unsigned int nr_zones, report_zones_cb cb, void *data);
1849 char *(*devnode)(struct gendisk *disk, umode_t *mode);
1850 struct module *owner;
1851 const struct pr_ops *pr_ops;
1854 * Special callback for probing GPT entry at a given sector.
1855 * Needed by Android devices, used by GPT scanner and MMC blk
1858 int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
1861 #ifdef CONFIG_COMPAT
1862 extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1863 unsigned int, unsigned long);
1865 #define blkdev_compat_ptr_ioctl NULL
1868 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1869 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1870 struct writeback_control *);
1872 #ifdef CONFIG_BLK_DEV_ZONED
1873 bool blk_req_needs_zone_write_lock(struct request *rq);
1874 bool blk_req_zone_write_trylock(struct request *rq);
1875 void __blk_req_zone_write_lock(struct request *rq);
1876 void __blk_req_zone_write_unlock(struct request *rq);
1878 static inline void blk_req_zone_write_lock(struct request *rq)
1880 if (blk_req_needs_zone_write_lock(rq))
1881 __blk_req_zone_write_lock(rq);
1884 static inline void blk_req_zone_write_unlock(struct request *rq)
1886 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1887 __blk_req_zone_write_unlock(rq);
1890 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1892 return rq->q->seq_zones_wlock &&
1893 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1896 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1898 if (!blk_req_needs_zone_write_lock(rq))
1900 return !blk_req_zone_is_write_locked(rq);
1903 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1908 static inline void blk_req_zone_write_lock(struct request *rq)
1912 static inline void blk_req_zone_write_unlock(struct request *rq)
1915 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1920 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1924 #endif /* CONFIG_BLK_DEV_ZONED */
1926 static inline void blk_wake_io_task(struct task_struct *waiter)
1929 * If we're polling, the task itself is doing the completions. For
1930 * that case, we don't need to signal a wakeup, it's enough to just
1931 * mark us as RUNNING.
1933 if (waiter == current)
1934 __set_current_state(TASK_RUNNING);
1936 wake_up_process(waiter);
1939 unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1941 void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1942 unsigned long start_time);
1944 unsigned long bio_start_io_acct(struct bio *bio);
1945 void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1946 struct block_device *orig_bdev);
1949 * bio_end_io_acct - end I/O accounting for bio based drivers
1950 * @bio: bio to end account for
1951 * @start: start time returned by bio_start_io_acct()
1953 static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1955 return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1958 int bdev_read_only(struct block_device *bdev);
1959 int set_blocksize(struct block_device *bdev, int size);
1961 const char *bdevname(struct block_device *bdev, char *buffer);
1962 int lookup_bdev(const char *pathname, dev_t *dev);
1964 void blkdev_show(struct seq_file *seqf, off_t offset);
1966 #define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */
1967 #define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */
1969 #define BLKDEV_MAJOR_MAX 512
1971 #define BLKDEV_MAJOR_MAX 0
1974 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1976 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
1977 int bd_prepare_to_claim(struct block_device *bdev, void *holder);
1978 void bd_abort_claiming(struct block_device *bdev, void *holder);
1979 void blkdev_put(struct block_device *bdev, fmode_t mode);
1981 /* just for blk-cgroup, don't use elsewhere */
1982 struct block_device *blkdev_get_no_open(dev_t dev);
1983 void blkdev_put_no_open(struct block_device *bdev);
1985 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
1986 void bdev_add(struct block_device *bdev, dev_t dev);
1987 struct block_device *I_BDEV(struct inode *inode);
1988 int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
1992 void invalidate_bdev(struct block_device *bdev);
1993 int sync_blockdev(struct block_device *bdev);
1995 static inline void invalidate_bdev(struct block_device *bdev)
1998 static inline int sync_blockdev(struct block_device *bdev)
2003 int fsync_bdev(struct block_device *bdev);
2005 int freeze_bdev(struct block_device *bdev);
2006 int thaw_bdev(struct block_device *bdev);
2008 #endif /* _LINUX_BLKDEV_H */