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 int blk_validate_block_size(unsigned int bsize)
240 if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
246 static inline bool blk_op_is_passthrough(unsigned int op)
249 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
252 static inline bool blk_rq_is_passthrough(struct request *rq)
254 return blk_op_is_passthrough(req_op(rq));
257 static inline unsigned short req_get_ioprio(struct request *req)
262 #include <linux/elevator.h>
264 struct blk_queue_ctx;
268 enum blk_eh_timer_return {
269 BLK_EH_DONE, /* drivers has completed the command */
270 BLK_EH_RESET_TIMER, /* reset timer and try again */
273 enum blk_queue_state {
278 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
279 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
282 * Zoned block device models (zoned limit).
284 * Note: This needs to be ordered from the least to the most severe
285 * restrictions for the inheritance in blk_stack_limits() to work.
287 enum blk_zoned_model {
288 BLK_ZONED_NONE = 0, /* Regular block device */
289 BLK_ZONED_HA, /* Host-aware zoned block device */
290 BLK_ZONED_HM, /* Host-managed zoned block device */
294 * BLK_BOUNCE_NONE: never bounce (default)
295 * BLK_BOUNCE_HIGH: bounce all highmem pages
302 struct queue_limits {
303 enum blk_bounce bounce;
304 unsigned long seg_boundary_mask;
305 unsigned long virt_boundary_mask;
307 unsigned int max_hw_sectors;
308 unsigned int max_dev_sectors;
309 unsigned int chunk_sectors;
310 unsigned int max_sectors;
311 unsigned int max_segment_size;
312 unsigned int physical_block_size;
313 unsigned int logical_block_size;
314 unsigned int alignment_offset;
317 unsigned int max_discard_sectors;
318 unsigned int max_hw_discard_sectors;
319 unsigned int max_write_same_sectors;
320 unsigned int max_write_zeroes_sectors;
321 unsigned int max_zone_append_sectors;
322 unsigned int discard_granularity;
323 unsigned int discard_alignment;
324 unsigned int zone_write_granularity;
326 unsigned short max_segments;
327 unsigned short max_integrity_segments;
328 unsigned short max_discard_segments;
330 unsigned char misaligned;
331 unsigned char discard_misaligned;
332 unsigned char raid_partial_stripes_expensive;
333 enum blk_zoned_model zoned;
336 typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
339 void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model);
341 #ifdef CONFIG_BLK_DEV_ZONED
343 #define BLK_ALL_ZONES ((unsigned int)-1)
344 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
345 unsigned int nr_zones, report_zones_cb cb, void *data);
346 unsigned int blkdev_nr_zones(struct gendisk *disk);
347 extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
348 sector_t sectors, sector_t nr_sectors,
350 int blk_revalidate_disk_zones(struct gendisk *disk,
351 void (*update_driver_data)(struct gendisk *disk));
353 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
354 unsigned int cmd, unsigned long arg);
355 extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
356 unsigned int cmd, unsigned long arg);
358 #else /* CONFIG_BLK_DEV_ZONED */
360 static inline unsigned int blkdev_nr_zones(struct gendisk *disk)
365 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
366 fmode_t mode, unsigned int cmd,
372 static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
373 fmode_t mode, unsigned int cmd,
379 #endif /* CONFIG_BLK_DEV_ZONED */
381 struct request_queue {
382 struct request *last_merge;
383 struct elevator_queue *elevator;
385 struct percpu_ref q_usage_counter;
387 struct blk_queue_stats *stats;
388 struct rq_qos *rq_qos;
390 const struct blk_mq_ops *mq_ops;
393 struct blk_mq_ctx __percpu *queue_ctx;
395 unsigned int queue_depth;
397 /* hw dispatch queues */
398 struct blk_mq_hw_ctx **queue_hw_ctx;
399 unsigned int nr_hw_queues;
402 * The queue owner gets to use this for whatever they like.
403 * ll_rw_blk doesn't touch it.
408 * various queue flags, see QUEUE_* below
410 unsigned long queue_flags;
412 * Number of contexts that have called blk_set_pm_only(). If this
413 * counter is above zero then only RQF_PM requests are processed.
418 * ida allocated id for this queue. Used to index queues from
423 spinlock_t queue_lock;
425 struct gendisk *disk;
435 struct kobject *mq_kobj;
437 #ifdef CONFIG_BLK_DEV_INTEGRITY
438 struct blk_integrity integrity;
439 #endif /* CONFIG_BLK_DEV_INTEGRITY */
443 enum rpm_status rpm_status;
449 unsigned long nr_requests; /* Max # of requests */
451 unsigned int dma_pad_mask;
452 unsigned int dma_alignment;
454 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
455 /* Inline crypto capabilities */
456 struct blk_keyslot_manager *ksm;
459 unsigned int rq_timeout;
462 struct blk_stat_callback *poll_cb;
463 struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
465 struct timer_list timeout;
466 struct work_struct timeout_work;
468 atomic_t nr_active_requests_shared_sbitmap;
470 struct sbitmap_queue sched_bitmap_tags;
471 struct sbitmap_queue sched_breserved_tags;
473 struct list_head icq_list;
474 #ifdef CONFIG_BLK_CGROUP
475 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
476 struct blkcg_gq *root_blkg;
477 struct list_head blkg_list;
480 struct queue_limits limits;
482 unsigned int required_elevator_features;
484 #ifdef CONFIG_BLK_DEV_ZONED
486 * Zoned block device information for request dispatch control.
487 * nr_zones is the total number of zones of the device. This is always
488 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
489 * bits which indicates if a zone is conventional (bit set) or
490 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
491 * bits which indicates if a zone is write locked, that is, if a write
492 * request targeting the zone was dispatched. All three fields are
493 * initialized by the low level device driver (e.g. scsi/sd.c).
494 * Stacking drivers (device mappers) may or may not initialize
497 * Reads of this information must be protected with blk_queue_enter() /
498 * blk_queue_exit(). Modifying this information is only allowed while
499 * no requests are being processed. See also blk_mq_freeze_queue() and
500 * blk_mq_unfreeze_queue().
502 unsigned int nr_zones;
503 unsigned long *conv_zones_bitmap;
504 unsigned long *seq_zones_wlock;
505 unsigned int max_open_zones;
506 unsigned int max_active_zones;
507 #endif /* CONFIG_BLK_DEV_ZONED */
510 struct mutex debugfs_mutex;
511 #ifdef CONFIG_BLK_DEV_IO_TRACE
512 struct blk_trace __rcu *blk_trace;
515 * for flush operations
517 struct blk_flush_queue *fq;
519 struct list_head requeue_list;
520 spinlock_t requeue_lock;
521 struct delayed_work requeue_work;
523 struct mutex sysfs_lock;
524 struct mutex sysfs_dir_lock;
527 * for reusing dead hctx instance in case of updating
530 struct list_head unused_hctx_list;
531 spinlock_t unused_hctx_lock;
535 #ifdef CONFIG_BLK_DEV_THROTTLING
537 struct throtl_data *td;
539 struct rcu_head rcu_head;
540 wait_queue_head_t mq_freeze_wq;
542 * Protect concurrent access to q_usage_counter by
543 * percpu_ref_kill() and percpu_ref_reinit().
545 struct mutex mq_freeze_lock;
547 struct blk_mq_tag_set *tag_set;
548 struct list_head tag_set_list;
549 struct bio_set bio_split;
551 struct dentry *debugfs_dir;
553 #ifdef CONFIG_BLK_DEBUG_FS
554 struct dentry *sched_debugfs_dir;
555 struct dentry *rqos_debugfs_dir;
558 bool mq_sysfs_init_done;
562 #define BLK_MAX_WRITE_HINTS 5
563 u64 write_hints[BLK_MAX_WRITE_HINTS];
566 /* Keep blk_queue_flag_name[] in sync with the definitions below */
567 #define QUEUE_FLAG_STOPPED 0 /* queue is stopped */
568 #define QUEUE_FLAG_DYING 1 /* queue being torn down */
569 #define QUEUE_FLAG_NOMERGES 3 /* disable merge attempts */
570 #define QUEUE_FLAG_SAME_COMP 4 /* complete on same CPU-group */
571 #define QUEUE_FLAG_FAIL_IO 5 /* fake timeout */
572 #define QUEUE_FLAG_NONROT 6 /* non-rotational device (SSD) */
573 #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
574 #define QUEUE_FLAG_IO_STAT 7 /* do disk/partitions IO accounting */
575 #define QUEUE_FLAG_DISCARD 8 /* supports DISCARD */
576 #define QUEUE_FLAG_NOXMERGES 9 /* No extended merges */
577 #define QUEUE_FLAG_ADD_RANDOM 10 /* Contributes to random pool */
578 #define QUEUE_FLAG_SECERASE 11 /* supports secure erase */
579 #define QUEUE_FLAG_SAME_FORCE 12 /* force complete on same CPU */
580 #define QUEUE_FLAG_DEAD 13 /* queue tear-down finished */
581 #define QUEUE_FLAG_INIT_DONE 14 /* queue is initialized */
582 #define QUEUE_FLAG_STABLE_WRITES 15 /* don't modify blks until WB is done */
583 #define QUEUE_FLAG_POLL 16 /* IO polling enabled if set */
584 #define QUEUE_FLAG_WC 17 /* Write back caching */
585 #define QUEUE_FLAG_FUA 18 /* device supports FUA writes */
586 #define QUEUE_FLAG_DAX 19 /* device supports DAX */
587 #define QUEUE_FLAG_STATS 20 /* track IO start and completion times */
588 #define QUEUE_FLAG_POLL_STATS 21 /* collecting stats for hybrid polling */
589 #define QUEUE_FLAG_REGISTERED 22 /* queue has been registered to a disk */
590 #define QUEUE_FLAG_SCSI_PASSTHROUGH 23 /* queue supports SCSI commands */
591 #define QUEUE_FLAG_QUIESCED 24 /* queue has been quiesced */
592 #define QUEUE_FLAG_PCI_P2PDMA 25 /* device supports PCI p2p requests */
593 #define QUEUE_FLAG_ZONE_RESETALL 26 /* supports Zone Reset All */
594 #define QUEUE_FLAG_RQ_ALLOC_TIME 27 /* record rq->alloc_time_ns */
595 #define QUEUE_FLAG_HCTX_ACTIVE 28 /* at least one blk-mq hctx is active */
596 #define QUEUE_FLAG_NOWAIT 29 /* device supports NOWAIT */
598 #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
599 (1 << QUEUE_FLAG_SAME_COMP) | \
600 (1 << QUEUE_FLAG_NOWAIT))
602 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
603 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
604 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
606 #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
607 #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
608 #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
609 #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
610 #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
611 #define blk_queue_noxmerges(q) \
612 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
613 #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
614 #define blk_queue_stable_writes(q) \
615 test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
616 #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
617 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
618 #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
619 #define blk_queue_zone_resetall(q) \
620 test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
621 #define blk_queue_secure_erase(q) \
622 (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
623 #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
624 #define blk_queue_scsi_passthrough(q) \
625 test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
626 #define blk_queue_pci_p2pdma(q) \
627 test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
628 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
629 #define blk_queue_rq_alloc_time(q) \
630 test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
632 #define blk_queue_rq_alloc_time(q) false
635 #define blk_noretry_request(rq) \
636 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
637 REQ_FAILFAST_DRIVER))
638 #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
639 #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
640 #define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
641 #define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
642 #define blk_queue_nowait(q) test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
644 extern void blk_set_pm_only(struct request_queue *q);
645 extern void blk_clear_pm_only(struct request_queue *q);
647 #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
649 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
651 #define rq_dma_dir(rq) \
652 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
654 #define dma_map_bvec(dev, bv, dir, attrs) \
655 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
658 static inline bool queue_is_mq(struct request_queue *q)
664 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
666 return q->rpm_status;
669 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
675 static inline enum blk_zoned_model
676 blk_queue_zoned_model(struct request_queue *q)
678 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
679 return q->limits.zoned;
680 return BLK_ZONED_NONE;
683 static inline bool blk_queue_is_zoned(struct request_queue *q)
685 switch (blk_queue_zoned_model(q)) {
694 static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
696 return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
699 #ifdef CONFIG_BLK_DEV_ZONED
700 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
702 return blk_queue_is_zoned(q) ? q->nr_zones : 0;
705 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
708 if (!blk_queue_is_zoned(q))
710 return sector >> ilog2(q->limits.chunk_sectors);
713 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
716 if (!blk_queue_is_zoned(q))
718 if (!q->conv_zones_bitmap)
720 return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
723 static inline void blk_queue_max_open_zones(struct request_queue *q,
724 unsigned int max_open_zones)
726 q->max_open_zones = max_open_zones;
729 static inline unsigned int queue_max_open_zones(const struct request_queue *q)
731 return q->max_open_zones;
734 static inline void blk_queue_max_active_zones(struct request_queue *q,
735 unsigned int max_active_zones)
737 q->max_active_zones = max_active_zones;
740 static inline unsigned int queue_max_active_zones(const struct request_queue *q)
742 return q->max_active_zones;
744 #else /* CONFIG_BLK_DEV_ZONED */
745 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
749 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
754 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
759 static inline unsigned int queue_max_open_zones(const struct request_queue *q)
763 static inline unsigned int queue_max_active_zones(const struct request_queue *q)
767 #endif /* CONFIG_BLK_DEV_ZONED */
769 static inline bool rq_is_sync(struct request *rq)
771 return op_is_sync(rq->cmd_flags);
774 static inline bool rq_mergeable(struct request *rq)
776 if (blk_rq_is_passthrough(rq))
779 if (req_op(rq) == REQ_OP_FLUSH)
782 if (req_op(rq) == REQ_OP_WRITE_ZEROES)
785 if (req_op(rq) == REQ_OP_ZONE_APPEND)
788 if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
790 if (rq->rq_flags & RQF_NOMERGE_FLAGS)
796 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
798 if (bio_page(a) == bio_page(b) &&
799 bio_offset(a) == bio_offset(b))
805 static inline unsigned int blk_queue_depth(struct request_queue *q)
808 return q->queue_depth;
810 return q->nr_requests;
814 * default timeout for SG_IO if none specified
816 #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
817 #define BLK_MIN_SG_TIMEOUT (7 * HZ)
823 unsigned long offset;
828 struct req_iterator {
829 struct bvec_iter iter;
833 /* This should not be used directly - use rq_for_each_segment */
834 #define for_each_bio(_bio) \
835 for (; _bio; _bio = _bio->bi_next)
836 #define __rq_for_each_bio(_bio, rq) \
838 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
840 #define rq_for_each_segment(bvl, _rq, _iter) \
841 __rq_for_each_bio(_iter.bio, _rq) \
842 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
844 #define rq_for_each_bvec(bvl, _rq, _iter) \
845 __rq_for_each_bio(_iter.bio, _rq) \
846 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
848 #define rq_iter_last(bvec, _iter) \
849 (_iter.bio->bi_next == NULL && \
850 bio_iter_last(bvec, _iter.iter))
852 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
853 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
855 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
856 extern void rq_flush_dcache_pages(struct request *rq);
858 static inline void rq_flush_dcache_pages(struct request *rq)
863 extern int blk_register_queue(struct gendisk *disk);
864 extern void blk_unregister_queue(struct gendisk *disk);
865 blk_qc_t submit_bio_noacct(struct bio *bio);
866 extern void blk_rq_init(struct request_queue *q, struct request *rq);
867 extern void blk_put_request(struct request *);
868 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
869 blk_mq_req_flags_t flags);
870 extern int blk_lld_busy(struct request_queue *q);
871 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
872 struct bio_set *bs, gfp_t gfp_mask,
873 int (*bio_ctr)(struct bio *, struct bio *, void *),
875 extern void blk_rq_unprep_clone(struct request *rq);
876 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
878 int blk_rq_append_bio(struct request *rq, struct bio *bio);
879 extern void blk_queue_split(struct bio **);
880 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
881 extern void blk_queue_exit(struct request_queue *q);
882 extern void blk_sync_queue(struct request_queue *q);
883 extern int blk_rq_map_user(struct request_queue *, struct request *,
884 struct rq_map_data *, void __user *, unsigned long,
886 extern int blk_rq_unmap_user(struct bio *);
887 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
888 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
889 struct rq_map_data *, const struct iov_iter *,
891 extern void blk_execute_rq_nowait(struct gendisk *,
892 struct request *, int, rq_end_io_fn *);
894 blk_status_t blk_execute_rq(struct gendisk *bd_disk, struct request *rq,
897 /* Helper to convert REQ_OP_XXX to its string format XXX */
898 extern const char *blk_op_str(unsigned int op);
900 int blk_status_to_errno(blk_status_t status);
901 blk_status_t errno_to_blk_status(int errno);
903 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
905 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
907 return bdev->bd_disk->queue; /* this is never NULL */
911 * The basic unit of block I/O is a sector. It is used in a number of contexts
912 * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
913 * bytes. Variables of type sector_t represent an offset or size that is a
914 * multiple of 512 bytes. Hence these two constants.
917 #define SECTOR_SHIFT 9
920 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
923 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
924 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
925 #define SECTOR_MASK (PAGE_SECTORS - 1)
928 * blk_rq_pos() : the current sector
929 * blk_rq_bytes() : bytes left in the entire request
930 * blk_rq_cur_bytes() : bytes left in the current segment
931 * blk_rq_err_bytes() : bytes left till the next error boundary
932 * blk_rq_sectors() : sectors left in the entire request
933 * blk_rq_cur_sectors() : sectors left in the current segment
934 * blk_rq_stats_sectors() : sectors of the entire request used for stats
936 static inline sector_t blk_rq_pos(const struct request *rq)
941 static inline unsigned int blk_rq_bytes(const struct request *rq)
943 return rq->__data_len;
946 static inline int blk_rq_cur_bytes(const struct request *rq)
948 return rq->bio ? bio_cur_bytes(rq->bio) : 0;
951 extern unsigned int blk_rq_err_bytes(const struct request *rq);
953 static inline unsigned int blk_rq_sectors(const struct request *rq)
955 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
958 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
960 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
963 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
965 return rq->stats_sectors;
968 #ifdef CONFIG_BLK_DEV_ZONED
970 /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
971 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
973 static inline unsigned int bio_zone_no(struct bio *bio)
975 return blk_queue_zone_no(bdev_get_queue(bio->bi_bdev),
976 bio->bi_iter.bi_sector);
979 static inline unsigned int bio_zone_is_seq(struct bio *bio)
981 return blk_queue_zone_is_seq(bdev_get_queue(bio->bi_bdev),
982 bio->bi_iter.bi_sector);
985 static inline unsigned int blk_rq_zone_no(struct request *rq)
987 return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
990 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
992 return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
994 #endif /* CONFIG_BLK_DEV_ZONED */
997 * Some commands like WRITE SAME have a payload or data transfer size which
998 * is different from the size of the request. Any driver that supports such
999 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1000 * calculate the data transfer size.
1002 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1004 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1005 return rq->special_vec.bv_len;
1006 return blk_rq_bytes(rq);
1010 * Return the first full biovec in the request. The caller needs to check that
1011 * there are any bvecs before calling this helper.
1013 static inline struct bio_vec req_bvec(struct request *rq)
1015 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1016 return rq->special_vec;
1017 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1020 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1023 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1024 return min(q->limits.max_discard_sectors,
1025 UINT_MAX >> SECTOR_SHIFT);
1027 if (unlikely(op == REQ_OP_WRITE_SAME))
1028 return q->limits.max_write_same_sectors;
1030 if (unlikely(op == REQ_OP_WRITE_ZEROES))
1031 return q->limits.max_write_zeroes_sectors;
1033 return q->limits.max_sectors;
1037 * Return maximum size of a request at given offset. Only valid for
1038 * file system requests.
1040 static inline unsigned int blk_max_size_offset(struct request_queue *q,
1042 unsigned int chunk_sectors)
1044 if (!chunk_sectors) {
1045 if (q->limits.chunk_sectors)
1046 chunk_sectors = q->limits.chunk_sectors;
1048 return q->limits.max_sectors;
1051 if (likely(is_power_of_2(chunk_sectors)))
1052 chunk_sectors -= offset & (chunk_sectors - 1);
1054 chunk_sectors -= sector_div(offset, chunk_sectors);
1056 return min(q->limits.max_sectors, chunk_sectors);
1059 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1062 struct request_queue *q = rq->q;
1064 if (blk_rq_is_passthrough(rq))
1065 return q->limits.max_hw_sectors;
1067 if (!q->limits.chunk_sectors ||
1068 req_op(rq) == REQ_OP_DISCARD ||
1069 req_op(rq) == REQ_OP_SECURE_ERASE)
1070 return blk_queue_get_max_sectors(q, req_op(rq));
1072 return min(blk_max_size_offset(q, offset, 0),
1073 blk_queue_get_max_sectors(q, req_op(rq)));
1076 static inline unsigned int blk_rq_count_bios(struct request *rq)
1078 unsigned int nr_bios = 0;
1081 __rq_for_each_bio(bio, rq)
1087 void blk_steal_bios(struct bio_list *list, struct request *rq);
1090 * Request completion related functions.
1092 * blk_update_request() completes given number of bytes and updates
1093 * the request without completing it.
1095 extern bool blk_update_request(struct request *rq, blk_status_t error,
1096 unsigned int nr_bytes);
1098 extern void blk_abort_request(struct request *);
1101 * Access functions for manipulating queue properties
1103 extern void blk_cleanup_queue(struct request_queue *);
1104 void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
1105 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1106 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1107 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1108 extern void blk_queue_max_discard_segments(struct request_queue *,
1110 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1111 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1112 unsigned int max_discard_sectors);
1113 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1114 unsigned int max_write_same_sectors);
1115 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1116 unsigned int max_write_same_sectors);
1117 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
1118 extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
1119 unsigned int max_zone_append_sectors);
1120 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1121 void blk_queue_zone_write_granularity(struct request_queue *q,
1123 extern void blk_queue_alignment_offset(struct request_queue *q,
1124 unsigned int alignment);
1125 void disk_update_readahead(struct gendisk *disk);
1126 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1127 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1128 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1129 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1130 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1131 extern void blk_set_default_limits(struct queue_limits *lim);
1132 extern void blk_set_stacking_limits(struct queue_limits *lim);
1133 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1135 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1137 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1138 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1139 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1140 extern void blk_queue_dma_alignment(struct request_queue *, int);
1141 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1142 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1143 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1144 extern void blk_queue_required_elevator_features(struct request_queue *q,
1145 unsigned int features);
1146 extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
1147 struct device *dev);
1150 * Number of physical segments as sent to the device.
1152 * Normally this is the number of discontiguous data segments sent by the
1153 * submitter. But for data-less command like discard we might have no
1154 * actual data segments submitted, but the driver might have to add it's
1155 * own special payload. In that case we still return 1 here so that this
1156 * special payload will be mapped.
1158 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1160 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1162 return rq->nr_phys_segments;
1166 * Number of discard segments (or ranges) the driver needs to fill in.
1167 * Each discard bio merged into a request is counted as one segment.
1169 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1171 return max_t(unsigned short, rq->nr_phys_segments, 1);
1174 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1175 struct scatterlist *sglist, struct scatterlist **last_sg);
1176 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1177 struct scatterlist *sglist)
1179 struct scatterlist *last_sg = NULL;
1181 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1183 extern void blk_dump_rq_flags(struct request *, char *);
1185 bool __must_check blk_get_queue(struct request_queue *);
1186 extern void blk_put_queue(struct request_queue *);
1187 extern void blk_set_queue_dying(struct request_queue *);
1191 * blk_plug permits building a queue of related requests by holding the I/O
1192 * fragments for a short period. This allows merging of sequential requests
1193 * into single larger request. As the requests are moved from a per-task list to
1194 * the device's request_queue in a batch, this results in improved scalability
1195 * as the lock contention for request_queue lock is reduced.
1197 * It is ok not to disable preemption when adding the request to the plug list
1198 * or when attempting a merge, because blk_schedule_flush_list() will only flush
1199 * the plug list when the task sleeps by itself. For details, please see
1200 * schedule() where blk_schedule_flush_plug() is called.
1203 struct list_head mq_list; /* blk-mq requests */
1204 struct list_head cb_list; /* md requires an unplug callback */
1205 unsigned short rq_count;
1206 bool multiple_queues;
1211 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1212 struct blk_plug_cb {
1213 struct list_head list;
1214 blk_plug_cb_fn callback;
1217 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1218 void *data, int size);
1219 extern void blk_start_plug(struct blk_plug *);
1220 extern void blk_finish_plug(struct blk_plug *);
1221 extern void blk_flush_plug_list(struct blk_plug *, bool);
1223 static inline void blk_flush_plug(struct task_struct *tsk)
1225 struct blk_plug *plug = tsk->plug;
1228 blk_flush_plug_list(plug, false);
1231 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1233 struct blk_plug *plug = tsk->plug;
1236 blk_flush_plug_list(plug, true);
1239 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1241 struct blk_plug *plug = tsk->plug;
1244 (!list_empty(&plug->mq_list) ||
1245 !list_empty(&plug->cb_list));
1248 int blkdev_issue_flush(struct block_device *bdev);
1249 long nr_blockdev_pages(void);
1250 #else /* CONFIG_BLOCK */
1254 static inline void blk_start_plug(struct blk_plug *plug)
1258 static inline void blk_finish_plug(struct blk_plug *plug)
1262 static inline void blk_flush_plug(struct task_struct *task)
1266 static inline void blk_schedule_flush_plug(struct task_struct *task)
1271 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1276 static inline int blkdev_issue_flush(struct block_device *bdev)
1281 static inline long nr_blockdev_pages(void)
1285 #endif /* CONFIG_BLOCK */
1287 extern void blk_io_schedule(void);
1289 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1290 sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1292 #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
1294 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1295 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1296 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1297 sector_t nr_sects, gfp_t gfp_mask, int flags,
1300 #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1301 #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1303 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1304 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1306 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1307 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1309 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1310 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1312 return blkdev_issue_discard(sb->s_bdev,
1313 block << (sb->s_blocksize_bits -
1315 nr_blocks << (sb->s_blocksize_bits -
1319 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1320 sector_t nr_blocks, gfp_t gfp_mask)
1322 return blkdev_issue_zeroout(sb->s_bdev,
1323 block << (sb->s_blocksize_bits -
1325 nr_blocks << (sb->s_blocksize_bits -
1330 static inline bool bdev_is_partition(struct block_device *bdev)
1332 return bdev->bd_partno;
1335 enum blk_default_limits {
1336 BLK_MAX_SEGMENTS = 128,
1337 BLK_SAFE_MAX_SECTORS = 255,
1338 BLK_DEF_MAX_SECTORS = 2560,
1339 BLK_MAX_SEGMENT_SIZE = 65536,
1340 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1343 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1345 return q->limits.seg_boundary_mask;
1348 static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1350 return q->limits.virt_boundary_mask;
1353 static inline unsigned int queue_max_sectors(const struct request_queue *q)
1355 return q->limits.max_sectors;
1358 static inline unsigned int queue_max_bytes(struct request_queue *q)
1360 return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1363 static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1365 return q->limits.max_hw_sectors;
1368 static inline unsigned short queue_max_segments(const struct request_queue *q)
1370 return q->limits.max_segments;
1373 static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1375 return q->limits.max_discard_segments;
1378 static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1380 return q->limits.max_segment_size;
1383 static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
1386 const struct queue_limits *l = &q->limits;
1388 return min(l->max_zone_append_sectors, l->max_sectors);
1391 static inline unsigned queue_logical_block_size(const struct request_queue *q)
1395 if (q && q->limits.logical_block_size)
1396 retval = q->limits.logical_block_size;
1401 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1403 return queue_logical_block_size(bdev_get_queue(bdev));
1406 static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1408 return q->limits.physical_block_size;
1411 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1413 return queue_physical_block_size(bdev_get_queue(bdev));
1416 static inline unsigned int queue_io_min(const struct request_queue *q)
1418 return q->limits.io_min;
1421 static inline int bdev_io_min(struct block_device *bdev)
1423 return queue_io_min(bdev_get_queue(bdev));
1426 static inline unsigned int queue_io_opt(const struct request_queue *q)
1428 return q->limits.io_opt;
1431 static inline int bdev_io_opt(struct block_device *bdev)
1433 return queue_io_opt(bdev_get_queue(bdev));
1436 static inline unsigned int
1437 queue_zone_write_granularity(const struct request_queue *q)
1439 return q->limits.zone_write_granularity;
1442 static inline unsigned int
1443 bdev_zone_write_granularity(struct block_device *bdev)
1445 return queue_zone_write_granularity(bdev_get_queue(bdev));
1448 static inline int queue_alignment_offset(const struct request_queue *q)
1450 if (q->limits.misaligned)
1453 return q->limits.alignment_offset;
1456 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1458 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1459 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1462 return (granularity + lim->alignment_offset - alignment) % granularity;
1465 static inline int bdev_alignment_offset(struct block_device *bdev)
1467 struct request_queue *q = bdev_get_queue(bdev);
1469 if (q->limits.misaligned)
1471 if (bdev_is_partition(bdev))
1472 return queue_limit_alignment_offset(&q->limits,
1473 bdev->bd_start_sect);
1474 return q->limits.alignment_offset;
1477 static inline int queue_discard_alignment(const struct request_queue *q)
1479 if (q->limits.discard_misaligned)
1482 return q->limits.discard_alignment;
1485 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1487 unsigned int alignment, granularity, offset;
1489 if (!lim->max_discard_sectors)
1492 /* Why are these in bytes, not sectors? */
1493 alignment = lim->discard_alignment >> SECTOR_SHIFT;
1494 granularity = lim->discard_granularity >> SECTOR_SHIFT;
1498 /* Offset of the partition start in 'granularity' sectors */
1499 offset = sector_div(sector, granularity);
1501 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
1502 offset = (granularity + alignment - offset) % granularity;
1504 /* Turn it back into bytes, gaah */
1505 return offset << SECTOR_SHIFT;
1509 * Two cases of handling DISCARD merge:
1510 * If max_discard_segments > 1, the driver takes every bio
1511 * as a range and send them to controller together. The ranges
1512 * needn't to be contiguous.
1513 * Otherwise, the bios/requests will be handled as same as
1514 * others which should be contiguous.
1516 static inline bool blk_discard_mergable(struct request *req)
1518 if (req_op(req) == REQ_OP_DISCARD &&
1519 queue_max_discard_segments(req->q) > 1)
1524 static inline int bdev_discard_alignment(struct block_device *bdev)
1526 struct request_queue *q = bdev_get_queue(bdev);
1528 if (bdev_is_partition(bdev))
1529 return queue_limit_discard_alignment(&q->limits,
1530 bdev->bd_start_sect);
1531 return q->limits.discard_alignment;
1534 static inline unsigned int bdev_write_same(struct block_device *bdev)
1536 struct request_queue *q = bdev_get_queue(bdev);
1539 return q->limits.max_write_same_sectors;
1544 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1546 struct request_queue *q = bdev_get_queue(bdev);
1549 return q->limits.max_write_zeroes_sectors;
1554 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1556 struct request_queue *q = bdev_get_queue(bdev);
1559 return blk_queue_zoned_model(q);
1561 return BLK_ZONED_NONE;
1564 static inline bool bdev_is_zoned(struct block_device *bdev)
1566 struct request_queue *q = bdev_get_queue(bdev);
1569 return blk_queue_is_zoned(q);
1574 static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1576 struct request_queue *q = bdev_get_queue(bdev);
1579 return blk_queue_zone_sectors(q);
1583 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
1585 struct request_queue *q = bdev_get_queue(bdev);
1588 return queue_max_open_zones(q);
1592 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
1594 struct request_queue *q = bdev_get_queue(bdev);
1597 return queue_max_active_zones(q);
1601 static inline int queue_dma_alignment(const struct request_queue *q)
1603 return q ? q->dma_alignment : 511;
1606 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1609 unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1610 return !(addr & alignment) && !(len & alignment);
1613 /* assumes size > 256 */
1614 static inline unsigned int blksize_bits(unsigned int size)
1616 unsigned int bits = 8;
1620 } while (size > 256);
1624 static inline unsigned int block_size(struct block_device *bdev)
1626 return 1 << bdev->bd_inode->i_blkbits;
1629 int kblockd_schedule_work(struct work_struct *work);
1630 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1632 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1633 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1634 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1635 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1637 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1639 enum blk_integrity_flags {
1640 BLK_INTEGRITY_VERIFY = 1 << 0,
1641 BLK_INTEGRITY_GENERATE = 1 << 1,
1642 BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
1643 BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
1646 struct blk_integrity_iter {
1650 unsigned int data_size;
1651 unsigned short interval;
1652 const char *disk_name;
1655 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1656 typedef void (integrity_prepare_fn) (struct request *);
1657 typedef void (integrity_complete_fn) (struct request *, unsigned int);
1659 struct blk_integrity_profile {
1660 integrity_processing_fn *generate_fn;
1661 integrity_processing_fn *verify_fn;
1662 integrity_prepare_fn *prepare_fn;
1663 integrity_complete_fn *complete_fn;
1667 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1668 extern void blk_integrity_unregister(struct gendisk *);
1669 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1670 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1671 struct scatterlist *);
1672 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1674 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1676 struct blk_integrity *bi = &disk->queue->integrity;
1685 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1687 return blk_get_integrity(bdev->bd_disk);
1691 blk_integrity_queue_supports_integrity(struct request_queue *q)
1693 return q->integrity.profile;
1696 static inline bool blk_integrity_rq(struct request *rq)
1698 return rq->cmd_flags & REQ_INTEGRITY;
1701 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1704 q->limits.max_integrity_segments = segs;
1707 static inline unsigned short
1708 queue_max_integrity_segments(const struct request_queue *q)
1710 return q->limits.max_integrity_segments;
1714 * bio_integrity_intervals - Return number of integrity intervals for a bio
1715 * @bi: blk_integrity profile for device
1716 * @sectors: Size of the bio in 512-byte sectors
1718 * Description: The block layer calculates everything in 512 byte
1719 * sectors but integrity metadata is done in terms of the data integrity
1720 * interval size of the storage device. Convert the block layer sectors
1721 * to the appropriate number of integrity intervals.
1723 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1724 unsigned int sectors)
1726 return sectors >> (bi->interval_exp - 9);
1729 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1730 unsigned int sectors)
1732 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1736 * Return the first bvec that contains integrity data. Only drivers that are
1737 * limited to a single integrity segment should use this helper.
1739 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1741 if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
1743 return rq->bio->bi_integrity->bip_vec;
1746 #else /* CONFIG_BLK_DEV_INTEGRITY */
1749 struct block_device;
1751 struct blk_integrity;
1753 static inline int blk_integrity_rq(struct request *rq)
1757 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1762 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1764 struct scatterlist *s)
1768 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1772 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1777 blk_integrity_queue_supports_integrity(struct request_queue *q)
1781 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1785 static inline void blk_integrity_register(struct gendisk *d,
1786 struct blk_integrity *b)
1789 static inline void blk_integrity_unregister(struct gendisk *d)
1792 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1796 static inline unsigned short queue_max_integrity_segments(const struct request_queue *q)
1801 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1802 unsigned int sectors)
1807 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1808 unsigned int sectors)
1813 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1818 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1820 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1822 bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
1824 void blk_ksm_unregister(struct request_queue *q);
1826 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1828 static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
1829 struct request_queue *q)
1834 static inline void blk_ksm_unregister(struct request_queue *q) { }
1836 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1839 struct block_device_operations {
1840 blk_qc_t (*submit_bio) (struct bio *bio);
1841 int (*open) (struct block_device *, fmode_t);
1842 void (*release) (struct gendisk *, fmode_t);
1843 int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1844 int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1845 int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1846 unsigned int (*check_events) (struct gendisk *disk,
1847 unsigned int clearing);
1848 void (*unlock_native_capacity) (struct gendisk *);
1849 int (*getgeo)(struct block_device *, struct hd_geometry *);
1850 int (*set_read_only)(struct block_device *bdev, bool ro);
1851 /* this callback is with swap_lock and sometimes page table lock held */
1852 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1853 int (*report_zones)(struct gendisk *, sector_t sector,
1854 unsigned int nr_zones, report_zones_cb cb, void *data);
1855 char *(*devnode)(struct gendisk *disk, umode_t *mode);
1856 struct module *owner;
1857 const struct pr_ops *pr_ops;
1860 * Special callback for probing GPT entry at a given sector.
1861 * Needed by Android devices, used by GPT scanner and MMC blk
1864 int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
1867 #ifdef CONFIG_COMPAT
1868 extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1869 unsigned int, unsigned long);
1871 #define blkdev_compat_ptr_ioctl NULL
1874 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1875 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1876 struct writeback_control *);
1878 #ifdef CONFIG_BLK_DEV_ZONED
1879 bool blk_req_needs_zone_write_lock(struct request *rq);
1880 bool blk_req_zone_write_trylock(struct request *rq);
1881 void __blk_req_zone_write_lock(struct request *rq);
1882 void __blk_req_zone_write_unlock(struct request *rq);
1884 static inline void blk_req_zone_write_lock(struct request *rq)
1886 if (blk_req_needs_zone_write_lock(rq))
1887 __blk_req_zone_write_lock(rq);
1890 static inline void blk_req_zone_write_unlock(struct request *rq)
1892 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1893 __blk_req_zone_write_unlock(rq);
1896 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1898 return rq->q->seq_zones_wlock &&
1899 test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1902 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1904 if (!blk_req_needs_zone_write_lock(rq))
1906 return !blk_req_zone_is_write_locked(rq);
1909 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1914 static inline void blk_req_zone_write_lock(struct request *rq)
1918 static inline void blk_req_zone_write_unlock(struct request *rq)
1921 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1926 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1930 #endif /* CONFIG_BLK_DEV_ZONED */
1932 static inline void blk_wake_io_task(struct task_struct *waiter)
1935 * If we're polling, the task itself is doing the completions. For
1936 * that case, we don't need to signal a wakeup, it's enough to just
1937 * mark us as RUNNING.
1939 if (waiter == current)
1940 __set_current_state(TASK_RUNNING);
1942 wake_up_process(waiter);
1945 unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1947 void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1948 unsigned long start_time);
1950 void bio_start_io_acct_time(struct bio *bio, unsigned long start_time);
1951 unsigned long bio_start_io_acct(struct bio *bio);
1952 void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1953 struct block_device *orig_bdev);
1956 * bio_end_io_acct - end I/O accounting for bio based drivers
1957 * @bio: bio to end account for
1958 * @start: start time returned by bio_start_io_acct()
1960 static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1962 return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1965 int bdev_read_only(struct block_device *bdev);
1966 int set_blocksize(struct block_device *bdev, int size);
1968 const char *bdevname(struct block_device *bdev, char *buffer);
1969 int lookup_bdev(const char *pathname, dev_t *dev);
1971 void blkdev_show(struct seq_file *seqf, off_t offset);
1973 #define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */
1974 #define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */
1976 #define BLKDEV_MAJOR_MAX 512
1978 #define BLKDEV_MAJOR_MAX 0
1981 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1983 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
1984 int bd_prepare_to_claim(struct block_device *bdev, void *holder);
1985 void bd_abort_claiming(struct block_device *bdev, void *holder);
1986 void blkdev_put(struct block_device *bdev, fmode_t mode);
1988 /* just for blk-cgroup, don't use elsewhere */
1989 struct block_device *blkdev_get_no_open(dev_t dev);
1990 void blkdev_put_no_open(struct block_device *bdev);
1992 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
1993 void bdev_add(struct block_device *bdev, dev_t dev);
1994 struct block_device *I_BDEV(struct inode *inode);
1995 int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
1999 void invalidate_bdev(struct block_device *bdev);
2000 int sync_blockdev(struct block_device *bdev);
2002 static inline void invalidate_bdev(struct block_device *bdev)
2005 static inline int sync_blockdev(struct block_device *bdev)
2010 int fsync_bdev(struct block_device *bdev);
2012 int freeze_bdev(struct block_device *bdev);
2013 int thaw_bdev(struct block_device *bdev);
2015 #endif /* _LINUX_BLKDEV_H */