xref: /linux-6.15/include/linux/blkdev.h (revision bc490f81)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_BLKDEV_H
31da177e4SLinus Torvalds #define _LINUX_BLKDEV_H
41da177e4SLinus Torvalds 
585fd0bc9SRussell King #include <linux/sched.h>
61da177e4SLinus Torvalds #include <linux/genhd.h>
71da177e4SLinus Torvalds #include <linux/list.h>
8320ae51fSJens Axboe #include <linux/llist.h>
9b296a6d5SAndy Shevchenko #include <linux/minmax.h>
101da177e4SLinus Torvalds #include <linux/timer.h>
111da177e4SLinus Torvalds #include <linux/workqueue.h>
121da177e4SLinus Torvalds #include <linux/wait.h>
131da177e4SLinus Torvalds #include <linux/bio.h>
143e6053d7SHugh Dickins #include <linux/gfp.h>
15548bc8e1STejun Heo #include <linux/rcupdate.h>
16add703fdSTejun Heo #include <linux/percpu-refcount.h>
176a0cb1bcSHannes Reinecke #include <linux/blkzoned.h>
18d97e594cSJohn Garry #include <linux/sbitmap.h>
191da177e4SLinus Torvalds 
20de477254SPaul Gortmaker struct module;
211da177e4SLinus Torvalds struct request_queue;
221da177e4SLinus Torvalds struct elevator_queue;
232056a782SJens Axboe struct blk_trace;
243d6392cfSJens Axboe struct request;
253d6392cfSJens Axboe struct sg_io_hdr;
263c798398STejun Heo struct blkcg_gq;
277c94e1c1SMing Lei struct blk_flush_queue;
283e08773cSChristoph Hellwig struct kiocb;
29bbd3e064SChristoph Hellwig struct pr_ops;
30a7905043SJosef Bacik struct rq_qos;
3134dbad5dSOmar Sandoval struct blk_queue_stats;
3234dbad5dSOmar Sandoval struct blk_stat_callback;
331b262839SSatya Tangirala struct blk_keyslot_manager;
341da177e4SLinus Torvalds 
35096392e0SMinwoo Im /* Must be consistent with blk_mq_poll_stats_bkt() */
360206319fSStephen Bates #define BLK_MQ_POLL_STATS_BKTS 16
370206319fSStephen Bates 
3829ece8b4SYufen Yu /* Doing classic polling */
3929ece8b4SYufen Yu #define BLK_MQ_POLL_CLASSIC -1
4029ece8b4SYufen Yu 
418bd435b3STejun Heo /*
428bd435b3STejun Heo  * Maximum number of blkcg policies allowed to be registered concurrently.
438bd435b3STejun Heo  * Defined here to simplify include dependency.
448bd435b3STejun Heo  */
45ec645dc9SOleksandr Natalenko #define BLKCG_MAX_POLS		6
468bd435b3STejun Heo 
47da6269daSChristoph Hellwig static inline bool blk_op_is_passthrough(unsigned int op)
4814cb0dc6SMing Lei {
49da6269daSChristoph Hellwig 	op &= REQ_OP_MASK;
5014cb0dc6SMing Lei 	return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
5114cb0dc6SMing Lei }
5214cb0dc6SMing Lei 
53797476b8SDamien Le Moal /*
54797476b8SDamien Le Moal  * Zoned block device models (zoned limit).
553093a479SChristoph Hellwig  *
563093a479SChristoph Hellwig  * Note: This needs to be ordered from the least to the most severe
573093a479SChristoph Hellwig  * restrictions for the inheritance in blk_stack_limits() to work.
58797476b8SDamien Le Moal  */
59797476b8SDamien Le Moal enum blk_zoned_model {
603093a479SChristoph Hellwig 	BLK_ZONED_NONE = 0,	/* Regular block device */
61797476b8SDamien Le Moal 	BLK_ZONED_HA,		/* Host-aware zoned block device */
62797476b8SDamien Le Moal 	BLK_ZONED_HM,		/* Host-managed zoned block device */
63797476b8SDamien Le Moal };
64797476b8SDamien Le Moal 
659bb33f24SChristoph Hellwig /*
669bb33f24SChristoph Hellwig  * BLK_BOUNCE_NONE:	never bounce (default)
679bb33f24SChristoph Hellwig  * BLK_BOUNCE_HIGH:	bounce all highmem pages
689bb33f24SChristoph Hellwig  */
699bb33f24SChristoph Hellwig enum blk_bounce {
709bb33f24SChristoph Hellwig 	BLK_BOUNCE_NONE,
719bb33f24SChristoph Hellwig 	BLK_BOUNCE_HIGH,
729bb33f24SChristoph Hellwig };
739bb33f24SChristoph Hellwig 
74025146e1SMartin K. Petersen struct queue_limits {
759bb33f24SChristoph Hellwig 	enum blk_bounce		bounce;
76025146e1SMartin K. Petersen 	unsigned long		seg_boundary_mask;
7703100aadSKeith Busch 	unsigned long		virt_boundary_mask;
78025146e1SMartin K. Petersen 
79025146e1SMartin K. Petersen 	unsigned int		max_hw_sectors;
80ca369d51SMartin K. Petersen 	unsigned int		max_dev_sectors;
81762380adSJens Axboe 	unsigned int		chunk_sectors;
82025146e1SMartin K. Petersen 	unsigned int		max_sectors;
83025146e1SMartin K. Petersen 	unsigned int		max_segment_size;
84c72758f3SMartin K. Petersen 	unsigned int		physical_block_size;
85ad6bf88aSMikulas Patocka 	unsigned int		logical_block_size;
86c72758f3SMartin K. Petersen 	unsigned int		alignment_offset;
87c72758f3SMartin K. Petersen 	unsigned int		io_min;
88c72758f3SMartin K. Petersen 	unsigned int		io_opt;
8967efc925SChristoph Hellwig 	unsigned int		max_discard_sectors;
900034af03SJens Axboe 	unsigned int		max_hw_discard_sectors;
914363ac7cSMartin K. Petersen 	unsigned int		max_write_same_sectors;
92a6f0788eSChaitanya Kulkarni 	unsigned int		max_write_zeroes_sectors;
930512a75bSKeith Busch 	unsigned int		max_zone_append_sectors;
9486b37281SMartin K. Petersen 	unsigned int		discard_granularity;
9586b37281SMartin K. Petersen 	unsigned int		discard_alignment;
96a805a4faSDamien Le Moal 	unsigned int		zone_write_granularity;
97025146e1SMartin K. Petersen 
988a78362cSMartin K. Petersen 	unsigned short		max_segments;
9913f05c8dSMartin K. Petersen 	unsigned short		max_integrity_segments;
1001e739730SChristoph Hellwig 	unsigned short		max_discard_segments;
101025146e1SMartin K. Petersen 
102c72758f3SMartin K. Petersen 	unsigned char		misaligned;
10386b37281SMartin K. Petersen 	unsigned char		discard_misaligned;
104c78afc62SKent Overstreet 	unsigned char		raid_partial_stripes_expensive;
105797476b8SDamien Le Moal 	enum blk_zoned_model	zoned;
106025146e1SMartin K. Petersen };
107025146e1SMartin K. Petersen 
108d4100351SChristoph Hellwig typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
109d4100351SChristoph Hellwig 			       void *data);
110d4100351SChristoph Hellwig 
11127ba3e8fSDamien Le Moal void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model);
11227ba3e8fSDamien Le Moal 
1136a0cb1bcSHannes Reinecke #ifdef CONFIG_BLK_DEV_ZONED
1146a0cb1bcSHannes Reinecke 
115d4100351SChristoph Hellwig #define BLK_ALL_ZONES  ((unsigned int)-1)
116d4100351SChristoph Hellwig int blkdev_report_zones(struct block_device *bdev, sector_t sector,
117d4100351SChristoph Hellwig 			unsigned int nr_zones, report_zones_cb cb, void *data);
1189b38bb4bSChristoph Hellwig unsigned int blkdev_nr_zones(struct gendisk *disk);
1196c1b1da5SAjay Joshi extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
1206c1b1da5SAjay Joshi 			    sector_t sectors, sector_t nr_sectors,
1216c1b1da5SAjay Joshi 			    gfp_t gfp_mask);
122e732671aSDamien Le Moal int blk_revalidate_disk_zones(struct gendisk *disk,
123e732671aSDamien Le Moal 			      void (*update_driver_data)(struct gendisk *disk));
1246a0cb1bcSHannes Reinecke 
1253ed05a98SShaun Tancheff extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
1263ed05a98SShaun Tancheff 				     unsigned int cmd, unsigned long arg);
127e876df1fSAjay Joshi extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
1283ed05a98SShaun Tancheff 				  unsigned int cmd, unsigned long arg);
1293ed05a98SShaun Tancheff 
1303ed05a98SShaun Tancheff #else /* CONFIG_BLK_DEV_ZONED */
1313ed05a98SShaun Tancheff 
1329b38bb4bSChristoph Hellwig static inline unsigned int blkdev_nr_zones(struct gendisk *disk)
133a91e1380SDamien Le Moal {
134a91e1380SDamien Le Moal 	return 0;
135a91e1380SDamien Le Moal }
136bf505456SDamien Le Moal 
1373ed05a98SShaun Tancheff static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
1383ed05a98SShaun Tancheff 					    fmode_t mode, unsigned int cmd,
1393ed05a98SShaun Tancheff 					    unsigned long arg)
1403ed05a98SShaun Tancheff {
1413ed05a98SShaun Tancheff 	return -ENOTTY;
1423ed05a98SShaun Tancheff }
1433ed05a98SShaun Tancheff 
144e876df1fSAjay Joshi static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
1453ed05a98SShaun Tancheff 					 fmode_t mode, unsigned int cmd,
1463ed05a98SShaun Tancheff 					 unsigned long arg)
1473ed05a98SShaun Tancheff {
1483ed05a98SShaun Tancheff 	return -ENOTTY;
1493ed05a98SShaun Tancheff }
1503ed05a98SShaun Tancheff 
1516a0cb1bcSHannes Reinecke #endif /* CONFIG_BLK_DEV_ZONED */
1526a0cb1bcSHannes Reinecke 
153d7b76301SRichard Kennedy struct request_queue {
1541da177e4SLinus Torvalds 	struct request		*last_merge;
155b374d18aSJens Axboe 	struct elevator_queue	*elevator;
1561da177e4SLinus Torvalds 
1570549e87cSMing Lei 	struct percpu_ref	q_usage_counter;
1580549e87cSMing Lei 
15934dbad5dSOmar Sandoval 	struct blk_queue_stats	*stats;
160a7905043SJosef Bacik 	struct rq_qos		*rq_qos;
16187760e5eSJens Axboe 
162f8a5b122SJens Axboe 	const struct blk_mq_ops	*mq_ops;
163320ae51fSJens Axboe 
164320ae51fSJens Axboe 	/* sw queues */
165e6cdb092SMing Lei 	struct blk_mq_ctx __percpu	*queue_ctx;
166320ae51fSJens Axboe 
167d278d4a8SJens Axboe 	unsigned int		queue_depth;
168d278d4a8SJens Axboe 
169320ae51fSJens Axboe 	/* hw dispatch queues */
170320ae51fSJens Axboe 	struct blk_mq_hw_ctx	**queue_hw_ctx;
171320ae51fSJens Axboe 	unsigned int		nr_hw_queues;
172320ae51fSJens Axboe 
1731da177e4SLinus Torvalds 	/*
1741da177e4SLinus Torvalds 	 * The queue owner gets to use this for whatever they like.
1751da177e4SLinus Torvalds 	 * ll_rw_blk doesn't touch it.
1761da177e4SLinus Torvalds 	 */
1771da177e4SLinus Torvalds 	void			*queuedata;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/*
1801da177e4SLinus Torvalds 	 * various queue flags, see QUEUE_* below
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	unsigned long		queue_flags;
183cd84a62eSBart Van Assche 	/*
184cd84a62eSBart Van Assche 	 * Number of contexts that have called blk_set_pm_only(). If this
185a4d34da7SBart Van Assche 	 * counter is above zero then only RQF_PM requests are processed.
186cd84a62eSBart Van Assche 	 */
187cd84a62eSBart Van Assche 	atomic_t		pm_only;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/*
190a73f730dSTejun Heo 	 * ida allocated id for this queue.  Used to index queues from
191a73f730dSTejun Heo 	 * ioctx.
192a73f730dSTejun Heo 	 */
193a73f730dSTejun Heo 	int			id;
194a73f730dSTejun Heo 
1950d945c1fSChristoph Hellwig 	spinlock_t		queue_lock;
1961da177e4SLinus Torvalds 
197d152c682SChristoph Hellwig 	struct gendisk		*disk;
198d152c682SChristoph Hellwig 
1991da177e4SLinus Torvalds 	/*
2001da177e4SLinus Torvalds 	 * queue kobject
2011da177e4SLinus Torvalds 	 */
2021da177e4SLinus Torvalds 	struct kobject kobj;
2031da177e4SLinus Torvalds 
204320ae51fSJens Axboe 	/*
205320ae51fSJens Axboe 	 * mq queue kobject
206320ae51fSJens Axboe 	 */
2071db4909eSMing Lei 	struct kobject *mq_kobj;
208320ae51fSJens Axboe 
209ac6fc48cSDan Williams #ifdef  CONFIG_BLK_DEV_INTEGRITY
210ac6fc48cSDan Williams 	struct blk_integrity integrity;
211ac6fc48cSDan Williams #endif	/* CONFIG_BLK_DEV_INTEGRITY */
212ac6fc48cSDan Williams 
21347fafbc7SRafael J. Wysocki #ifdef CONFIG_PM
2146c954667SLin Ming 	struct device		*dev;
215db04e18dSGeert Uytterhoeven 	enum rpm_status		rpm_status;
2166c954667SLin Ming #endif
2176c954667SLin Ming 
2181da177e4SLinus Torvalds 	/*
2191da177e4SLinus Torvalds 	 * queue settings
2201da177e4SLinus Torvalds 	 */
2211da177e4SLinus Torvalds 	unsigned long		nr_requests;	/* Max # of requests */
2221da177e4SLinus Torvalds 
223e3790c7dSTejun Heo 	unsigned int		dma_pad_mask;
2241da177e4SLinus Torvalds 	unsigned int		dma_alignment;
2251da177e4SLinus Torvalds 
2261b262839SSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
2271b262839SSatya Tangirala 	/* Inline crypto capabilities */
2281b262839SSatya Tangirala 	struct blk_keyslot_manager *ksm;
2291b262839SSatya Tangirala #endif
2301b262839SSatya Tangirala 
231242f9dcbSJens Axboe 	unsigned int		rq_timeout;
23264f1c21eSJens Axboe 	int			poll_nsec;
23334dbad5dSOmar Sandoval 
23434dbad5dSOmar Sandoval 	struct blk_stat_callback	*poll_cb;
2350206319fSStephen Bates 	struct blk_rq_stat	poll_stat[BLK_MQ_POLL_STATS_BKTS];
23634dbad5dSOmar Sandoval 
237242f9dcbSJens Axboe 	struct timer_list	timeout;
238287922ebSChristoph Hellwig 	struct work_struct	timeout_work;
239242f9dcbSJens Axboe 
240079a2e3eSJohn Garry 	atomic_t		nr_active_requests_shared_tags;
241bccf5e26SJohn Garry 
242079a2e3eSJohn Garry 	struct blk_mq_tags	*sched_shared_tags;
243d97e594cSJohn Garry 
244a612fddfSTejun Heo 	struct list_head	icq_list;
2454eef3049STejun Heo #ifdef CONFIG_BLK_CGROUP
246a2b1693bSTejun Heo 	DECLARE_BITMAP		(blkcg_pols, BLKCG_MAX_POLS);
2473c798398STejun Heo 	struct blkcg_gq		*root_blkg;
24803aa264aSTejun Heo 	struct list_head	blkg_list;
2494eef3049STejun Heo #endif
250a612fddfSTejun Heo 
251025146e1SMartin K. Petersen 	struct queue_limits	limits;
252025146e1SMartin K. Petersen 
25368c43f13SDamien Le Moal 	unsigned int		required_elevator_features;
25468c43f13SDamien Le Moal 
2556a5ac984SBart Van Assche #ifdef CONFIG_BLK_DEV_ZONED
2561da177e4SLinus Torvalds 	/*
2576cc77e9cSChristoph Hellwig 	 * Zoned block device information for request dispatch control.
2586cc77e9cSChristoph Hellwig 	 * nr_zones is the total number of zones of the device. This is always
259f216fdd7SChristoph Hellwig 	 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
260f216fdd7SChristoph Hellwig 	 * bits which indicates if a zone is conventional (bit set) or
261f216fdd7SChristoph Hellwig 	 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
2626cc77e9cSChristoph Hellwig 	 * bits which indicates if a zone is write locked, that is, if a write
2636cc77e9cSChristoph Hellwig 	 * request targeting the zone was dispatched. All three fields are
2646cc77e9cSChristoph Hellwig 	 * initialized by the low level device driver (e.g. scsi/sd.c).
2656cc77e9cSChristoph Hellwig 	 * Stacking drivers (device mappers) may or may not initialize
2666cc77e9cSChristoph Hellwig 	 * these fields.
267ccce20fcSBart Van Assche 	 *
268ccce20fcSBart Van Assche 	 * Reads of this information must be protected with blk_queue_enter() /
269ccce20fcSBart Van Assche 	 * blk_queue_exit(). Modifying this information is only allowed while
270ccce20fcSBart Van Assche 	 * no requests are being processed. See also blk_mq_freeze_queue() and
271ccce20fcSBart Van Assche 	 * blk_mq_unfreeze_queue().
2726cc77e9cSChristoph Hellwig 	 */
2736cc77e9cSChristoph Hellwig 	unsigned int		nr_zones;
274f216fdd7SChristoph Hellwig 	unsigned long		*conv_zones_bitmap;
2756cc77e9cSChristoph Hellwig 	unsigned long		*seq_zones_wlock;
276e15864f8SNiklas Cassel 	unsigned int		max_open_zones;
277659bf827SNiklas Cassel 	unsigned int		max_active_zones;
2786a5ac984SBart Van Assche #endif /* CONFIG_BLK_DEV_ZONED */
2796cc77e9cSChristoph Hellwig 
2801946089aSChristoph Lameter 	int			node;
28185e0cbbbSLuis Chamberlain 	struct mutex		debugfs_mutex;
2826c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
283c780e86dSJan Kara 	struct blk_trace __rcu	*blk_trace;
2846c5c9341SAlexey Dobriyan #endif
2851da177e4SLinus Torvalds 	/*
2864913efe4STejun Heo 	 * for flush operations
2871da177e4SLinus Torvalds 	 */
2887c94e1c1SMing Lei 	struct blk_flush_queue	*fq;
289483f4afcSAl Viro 
2906fca6a61SChristoph Hellwig 	struct list_head	requeue_list;
2916fca6a61SChristoph Hellwig 	spinlock_t		requeue_lock;
2922849450aSMike Snitzer 	struct delayed_work	requeue_work;
2936fca6a61SChristoph Hellwig 
294483f4afcSAl Viro 	struct mutex		sysfs_lock;
295cecf5d87SMing Lei 	struct mutex		sysfs_dir_lock;
296d351af01SFUJITA Tomonori 
2972f8f1336SMing Lei 	/*
2982f8f1336SMing Lei 	 * for reusing dead hctx instance in case of updating
2992f8f1336SMing Lei 	 * nr_hw_queues
3002f8f1336SMing Lei 	 */
3012f8f1336SMing Lei 	struct list_head	unused_hctx_list;
3022f8f1336SMing Lei 	spinlock_t		unused_hctx_lock;
3032f8f1336SMing Lei 
3047996a8b5SBob Liu 	int			mq_freeze_depth;
305d732580bSTejun Heo 
306e43473b7SVivek Goyal #ifdef CONFIG_BLK_DEV_THROTTLING
307e43473b7SVivek Goyal 	/* Throttle data */
308e43473b7SVivek Goyal 	struct throtl_data *td;
309e43473b7SVivek Goyal #endif
310548bc8e1STejun Heo 	struct rcu_head		rcu_head;
311320ae51fSJens Axboe 	wait_queue_head_t	mq_freeze_wq;
3127996a8b5SBob Liu 	/*
3137996a8b5SBob Liu 	 * Protect concurrent access to q_usage_counter by
3147996a8b5SBob Liu 	 * percpu_ref_kill() and percpu_ref_reinit().
3157996a8b5SBob Liu 	 */
3167996a8b5SBob Liu 	struct mutex		mq_freeze_lock;
3170d2602caSJens Axboe 
3180d2602caSJens Axboe 	struct blk_mq_tag_set	*tag_set;
3190d2602caSJens Axboe 	struct list_head	tag_set_list;
320338aa96dSKent Overstreet 	struct bio_set		bio_split;
3214593fdbeSAkinobu Mita 
32207e4feadSOmar Sandoval 	struct dentry		*debugfs_dir;
32385e0cbbbSLuis Chamberlain 
32485e0cbbbSLuis Chamberlain #ifdef CONFIG_BLK_DEBUG_FS
325d332ce09SOmar Sandoval 	struct dentry		*sched_debugfs_dir;
326cc56694fSMing Lei 	struct dentry		*rqos_debugfs_dir;
32707e4feadSOmar Sandoval #endif
32807e4feadSOmar Sandoval 
3294593fdbeSAkinobu Mita 	bool			mq_sysfs_init_done;
3306d247d7fSChristoph Hellwig 
331f793dfd3SJens Axboe #define BLK_MAX_WRITE_HINTS	5
332f793dfd3SJens Axboe 	u64			write_hints[BLK_MAX_WRITE_HINTS];
3331da177e4SLinus Torvalds };
3341da177e4SLinus Torvalds 
335bfe373f6SHou Tao /* Keep blk_queue_flag_name[] in sync with the definitions below */
336eca7abf3SJens Axboe #define QUEUE_FLAG_STOPPED	0	/* queue is stopped */
337eca7abf3SJens Axboe #define QUEUE_FLAG_DYING	1	/* queue being torn down */
338eca7abf3SJens Axboe #define QUEUE_FLAG_NOMERGES     3	/* disable merge attempts */
339eca7abf3SJens Axboe #define QUEUE_FLAG_SAME_COMP	4	/* complete on same CPU-group */
340eca7abf3SJens Axboe #define QUEUE_FLAG_FAIL_IO	5	/* fake timeout */
341eca7abf3SJens Axboe #define QUEUE_FLAG_NONROT	6	/* non-rotational device (SSD) */
34288e740f1SFernando Luis Vázquez Cao #define QUEUE_FLAG_VIRT		QUEUE_FLAG_NONROT /* paravirt device */
343eca7abf3SJens Axboe #define QUEUE_FLAG_IO_STAT	7	/* do disk/partitions IO accounting */
344eca7abf3SJens Axboe #define QUEUE_FLAG_DISCARD	8	/* supports DISCARD */
345eca7abf3SJens Axboe #define QUEUE_FLAG_NOXMERGES	9	/* No extended merges */
346eca7abf3SJens Axboe #define QUEUE_FLAG_ADD_RANDOM	10	/* Contributes to random pool */
347eca7abf3SJens Axboe #define QUEUE_FLAG_SECERASE	11	/* supports secure erase */
348eca7abf3SJens Axboe #define QUEUE_FLAG_SAME_FORCE	12	/* force complete on same CPU */
349eca7abf3SJens Axboe #define QUEUE_FLAG_DEAD		13	/* queue tear-down finished */
350eca7abf3SJens Axboe #define QUEUE_FLAG_INIT_DONE	14	/* queue is initialized */
3511cb039f3SChristoph Hellwig #define QUEUE_FLAG_STABLE_WRITES 15	/* don't modify blks until WB is done */
352eca7abf3SJens Axboe #define QUEUE_FLAG_POLL		16	/* IO polling enabled if set */
353eca7abf3SJens Axboe #define QUEUE_FLAG_WC		17	/* Write back caching */
354eca7abf3SJens Axboe #define QUEUE_FLAG_FUA		18	/* device supports FUA writes */
355eca7abf3SJens Axboe #define QUEUE_FLAG_DAX		19	/* device supports DAX */
356eca7abf3SJens Axboe #define QUEUE_FLAG_STATS	20	/* track IO start and completion times */
357eca7abf3SJens Axboe #define QUEUE_FLAG_POLL_STATS	21	/* collecting stats for hybrid polling */
358eca7abf3SJens Axboe #define QUEUE_FLAG_REGISTERED	22	/* queue has been registered to a disk */
359eca7abf3SJens Axboe #define QUEUE_FLAG_SCSI_PASSTHROUGH 23	/* queue supports SCSI commands */
360eca7abf3SJens Axboe #define QUEUE_FLAG_QUIESCED	24	/* queue has been quiesced */
361eca7abf3SJens Axboe #define QUEUE_FLAG_PCI_P2PDMA	25	/* device supports PCI p2p requests */
362e84e8f06SChaitanya Kulkarni #define QUEUE_FLAG_ZONE_RESETALL 26	/* supports Zone Reset All */
3636f816b4bSTejun Heo #define QUEUE_FLAG_RQ_ALLOC_TIME 27	/* record rq->alloc_time_ns */
364f1b49fdcSJohn Garry #define QUEUE_FLAG_HCTX_ACTIVE	28	/* at least one blk-mq hctx is active */
365021a2446SMike Snitzer #define QUEUE_FLAG_NOWAIT       29	/* device supports NOWAIT */
366797e7dbbSTejun Heo 
36794eddfbeSJens Axboe #define QUEUE_FLAG_MQ_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
368021a2446SMike Snitzer 				 (1 << QUEUE_FLAG_SAME_COMP) |		\
369021a2446SMike Snitzer 				 (1 << QUEUE_FLAG_NOWAIT))
37094eddfbeSJens Axboe 
3718814ce8aSBart Van Assche void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
3728814ce8aSBart Van Assche void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
3738814ce8aSBart Van Assche bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
3748814ce8aSBart Van Assche 
3751da177e4SLinus Torvalds #define blk_queue_stopped(q)	test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
3763f3299d5SBart Van Assche #define blk_queue_dying(q)	test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
377c246e80dSBart Van Assche #define blk_queue_dead(q)	test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
378320ae51fSJens Axboe #define blk_queue_init_done(q)	test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
379ac9fafa1SAlan D. Brunelle #define blk_queue_nomerges(q)	test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
380488991e2SAlan D. Brunelle #define blk_queue_noxmerges(q)	\
381488991e2SAlan D. Brunelle 	test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
382a68bbddbSJens Axboe #define blk_queue_nonrot(q)	test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
3831cb039f3SChristoph Hellwig #define blk_queue_stable_writes(q) \
3841cb039f3SChristoph Hellwig 	test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
385bc58ba94SJens Axboe #define blk_queue_io_stat(q)	test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
386e2e1a148SJens Axboe #define blk_queue_add_random(q)	test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
387c15227deSChristoph Hellwig #define blk_queue_discard(q)	test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
388e84e8f06SChaitanya Kulkarni #define blk_queue_zone_resetall(q)	\
389e84e8f06SChaitanya Kulkarni 	test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
390288dab8aSChristoph Hellwig #define blk_queue_secure_erase(q) \
391288dab8aSChristoph Hellwig 	(test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
392163d4baaSToshi Kani #define blk_queue_dax(q)	test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
3939efc160fSBart Van Assche #define blk_queue_scsi_passthrough(q)	\
3949efc160fSBart Van Assche 	test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
39549d92c0dSLogan Gunthorpe #define blk_queue_pci_p2pdma(q)	\
39649d92c0dSLogan Gunthorpe 	test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
3976f816b4bSTejun Heo #ifdef CONFIG_BLK_RQ_ALLOC_TIME
3986f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q)	\
3996f816b4bSTejun Heo 	test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
4006f816b4bSTejun Heo #else
4016f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q)	false
4026f816b4bSTejun Heo #endif
4031da177e4SLinus Torvalds 
40433659ebbSChristoph Hellwig #define blk_noretry_request(rq) \
40533659ebbSChristoph Hellwig 	((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
40633659ebbSChristoph Hellwig 			     REQ_FAILFAST_DRIVER))
407f4560ffeSMing Lei #define blk_queue_quiesced(q)	test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
408cd84a62eSBart Van Assche #define blk_queue_pm_only(q)	atomic_read(&(q)->pm_only)
4090ce91444SDave Chinner #define blk_queue_fua(q)	test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
41058c898baSMing Lei #define blk_queue_registered(q)	test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
411021a2446SMike Snitzer #define blk_queue_nowait(q)	test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
412c9254f2dSBart Van Assche 
413cd84a62eSBart Van Assche extern void blk_set_pm_only(struct request_queue *q);
414cd84a62eSBart Van Assche extern void blk_clear_pm_only(struct request_queue *q);
4154aff5e23SJens Axboe 
4161da177e4SLinus Torvalds #define list_entry_rq(ptr)	list_entry((ptr), struct request, queuelist)
4171da177e4SLinus Torvalds 
4183ab3a031SChristoph Hellwig #define dma_map_bvec(dev, bv, dir, attrs) \
4193ab3a031SChristoph Hellwig 	dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
4203ab3a031SChristoph Hellwig 	(dir), (attrs))
4213ab3a031SChristoph Hellwig 
422344e9ffcSJens Axboe static inline bool queue_is_mq(struct request_queue *q)
42349fd524fSJens Axboe {
424a1ce35faSJens Axboe 	return q->mq_ops;
42549fd524fSJens Axboe }
42649fd524fSJens Axboe 
42752abca64SAlan Stern #ifdef CONFIG_PM
42852abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
42952abca64SAlan Stern {
43052abca64SAlan Stern 	return q->rpm_status;
43152abca64SAlan Stern }
43252abca64SAlan Stern #else
43352abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
43452abca64SAlan Stern {
43552abca64SAlan Stern 	return RPM_ACTIVE;
43652abca64SAlan Stern }
43752abca64SAlan Stern #endif
43852abca64SAlan Stern 
439797476b8SDamien Le Moal static inline enum blk_zoned_model
440797476b8SDamien Le Moal blk_queue_zoned_model(struct request_queue *q)
441797476b8SDamien Le Moal {
4426fcd6695SChristoph Hellwig 	if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
443797476b8SDamien Le Moal 		return q->limits.zoned;
4446fcd6695SChristoph Hellwig 	return BLK_ZONED_NONE;
445797476b8SDamien Le Moal }
446797476b8SDamien Le Moal 
447797476b8SDamien Le Moal static inline bool blk_queue_is_zoned(struct request_queue *q)
448797476b8SDamien Le Moal {
449797476b8SDamien Le Moal 	switch (blk_queue_zoned_model(q)) {
450797476b8SDamien Le Moal 	case BLK_ZONED_HA:
451797476b8SDamien Le Moal 	case BLK_ZONED_HM:
452797476b8SDamien Le Moal 		return true;
453797476b8SDamien Le Moal 	default:
454797476b8SDamien Le Moal 		return false;
455797476b8SDamien Le Moal 	}
456797476b8SDamien Le Moal }
457797476b8SDamien Le Moal 
458113ab72eSDamien Le Moal static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
4596a0cb1bcSHannes Reinecke {
4606a0cb1bcSHannes Reinecke 	return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
4616a0cb1bcSHannes Reinecke }
4626a0cb1bcSHannes Reinecke 
4636a5ac984SBart Van Assche #ifdef CONFIG_BLK_DEV_ZONED
464965b652eSDamien Le Moal static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
465965b652eSDamien Le Moal {
466965b652eSDamien Le Moal 	return blk_queue_is_zoned(q) ? q->nr_zones : 0;
467965b652eSDamien Le Moal }
468965b652eSDamien Le Moal 
4696cc77e9cSChristoph Hellwig static inline unsigned int blk_queue_zone_no(struct request_queue *q,
4706cc77e9cSChristoph Hellwig 					     sector_t sector)
4716cc77e9cSChristoph Hellwig {
4726cc77e9cSChristoph Hellwig 	if (!blk_queue_is_zoned(q))
4736cc77e9cSChristoph Hellwig 		return 0;
4746cc77e9cSChristoph Hellwig 	return sector >> ilog2(q->limits.chunk_sectors);
4756cc77e9cSChristoph Hellwig }
4766cc77e9cSChristoph Hellwig 
4776cc77e9cSChristoph Hellwig static inline bool blk_queue_zone_is_seq(struct request_queue *q,
4786cc77e9cSChristoph Hellwig 					 sector_t sector)
4796cc77e9cSChristoph Hellwig {
480f216fdd7SChristoph Hellwig 	if (!blk_queue_is_zoned(q))
4816cc77e9cSChristoph Hellwig 		return false;
482f216fdd7SChristoph Hellwig 	if (!q->conv_zones_bitmap)
483f216fdd7SChristoph Hellwig 		return true;
484f216fdd7SChristoph Hellwig 	return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
4856cc77e9cSChristoph Hellwig }
486e15864f8SNiklas Cassel 
487e15864f8SNiklas Cassel static inline void blk_queue_max_open_zones(struct request_queue *q,
488e15864f8SNiklas Cassel 		unsigned int max_open_zones)
489e15864f8SNiklas Cassel {
490e15864f8SNiklas Cassel 	q->max_open_zones = max_open_zones;
491e15864f8SNiklas Cassel }
492e15864f8SNiklas Cassel 
493e15864f8SNiklas Cassel static inline unsigned int queue_max_open_zones(const struct request_queue *q)
494e15864f8SNiklas Cassel {
495e15864f8SNiklas Cassel 	return q->max_open_zones;
496e15864f8SNiklas Cassel }
497659bf827SNiklas Cassel 
498659bf827SNiklas Cassel static inline void blk_queue_max_active_zones(struct request_queue *q,
499659bf827SNiklas Cassel 		unsigned int max_active_zones)
500659bf827SNiklas Cassel {
501659bf827SNiklas Cassel 	q->max_active_zones = max_active_zones;
502659bf827SNiklas Cassel }
503659bf827SNiklas Cassel 
504659bf827SNiklas Cassel static inline unsigned int queue_max_active_zones(const struct request_queue *q)
505659bf827SNiklas Cassel {
506659bf827SNiklas Cassel 	return q->max_active_zones;
507659bf827SNiklas Cassel }
508965b652eSDamien Le Moal #else /* CONFIG_BLK_DEV_ZONED */
509965b652eSDamien Le Moal static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
510965b652eSDamien Le Moal {
511965b652eSDamien Le Moal 	return 0;
512965b652eSDamien Le Moal }
51302992df8SJohannes Thumshirn static inline bool blk_queue_zone_is_seq(struct request_queue *q,
51402992df8SJohannes Thumshirn 					 sector_t sector)
51502992df8SJohannes Thumshirn {
51602992df8SJohannes Thumshirn 	return false;
51702992df8SJohannes Thumshirn }
51802992df8SJohannes Thumshirn static inline unsigned int blk_queue_zone_no(struct request_queue *q,
51902992df8SJohannes Thumshirn 					     sector_t sector)
52002992df8SJohannes Thumshirn {
52102992df8SJohannes Thumshirn 	return 0;
52202992df8SJohannes Thumshirn }
523e15864f8SNiklas Cassel static inline unsigned int queue_max_open_zones(const struct request_queue *q)
524e15864f8SNiklas Cassel {
525e15864f8SNiklas Cassel 	return 0;
526e15864f8SNiklas Cassel }
527659bf827SNiklas Cassel static inline unsigned int queue_max_active_zones(const struct request_queue *q)
528659bf827SNiklas Cassel {
529659bf827SNiklas Cassel 	return 0;
530659bf827SNiklas Cassel }
5316a5ac984SBart Van Assche #endif /* CONFIG_BLK_DEV_ZONED */
5326cc77e9cSChristoph Hellwig 
533d278d4a8SJens Axboe static inline unsigned int blk_queue_depth(struct request_queue *q)
534d278d4a8SJens Axboe {
535d278d4a8SJens Axboe 	if (q->queue_depth)
536d278d4a8SJens Axboe 		return q->queue_depth;
537d278d4a8SJens Axboe 
538d278d4a8SJens Axboe 	return q->nr_requests;
539d278d4a8SJens Axboe }
540d278d4a8SJens Axboe 
5413d6392cfSJens Axboe /*
5423d6392cfSJens Axboe  * default timeout for SG_IO if none specified
5433d6392cfSJens Axboe  */
5443d6392cfSJens Axboe #define BLK_DEFAULT_SG_TIMEOUT	(60 * HZ)
545f2f1fa78SLinus Torvalds #define BLK_MIN_SG_TIMEOUT	(7 * HZ)
5463d6392cfSJens Axboe 
5475705f702SNeilBrown /* This should not be used directly - use rq_for_each_segment */
5481e428079SJens Axboe #define for_each_bio(_bio)		\
5491e428079SJens Axboe 	for (; _bio; _bio = _bio->bi_next)
5501da177e4SLinus Torvalds 
5512d4dc890SIlya Loginov 
5521da177e4SLinus Torvalds extern int blk_register_queue(struct gendisk *disk);
5531da177e4SLinus Torvalds extern void blk_unregister_queue(struct gendisk *disk);
5543e08773cSChristoph Hellwig void submit_bio_noacct(struct bio *bio);
55524b83debSChristoph Hellwig 
556ef9e3facSKiyoshi Ueda extern int blk_lld_busy(struct request_queue *q);
557f695ca38SChristoph Hellwig extern void blk_queue_split(struct bio **);
5589a95e4efSBart Van Assche extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
5592e6edc95SDan Williams extern void blk_queue_exit(struct request_queue *q);
5601da177e4SLinus Torvalds extern void blk_sync_queue(struct request_queue *q);
561fb9b16e1SKeith Busch 
562e47bc4edSChaitanya Kulkarni /* Helper to convert REQ_OP_XXX to its string format XXX */
563e47bc4edSChaitanya Kulkarni extern const char *blk_op_str(unsigned int op);
564e47bc4edSChaitanya Kulkarni 
5652a842acaSChristoph Hellwig int blk_status_to_errno(blk_status_t status);
5662a842acaSChristoph Hellwig blk_status_t errno_to_blk_status(int errno);
5672a842acaSChristoph Hellwig 
568ef99b2d3SChristoph Hellwig /* only poll the hardware once, don't continue until a completion was found */
569ef99b2d3SChristoph Hellwig #define BLK_POLL_ONESHOT		(1 << 0)
570d729cf9aSChristoph Hellwig /* do not sleep to wait for the expected completion time */
571d729cf9aSChristoph Hellwig #define BLK_POLL_NOSLEEP		(1 << 1)
5725a72e899SJens Axboe int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
5735a72e899SJens Axboe int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
5745a72e899SJens Axboe 			unsigned int flags);
57505229beeSJens Axboe 
576165125e1SJens Axboe static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
5771da177e4SLinus Torvalds {
57817220ca5SPavel Begunkov 	return bdev->bd_queue;	/* this is never NULL */
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /*
582233bde21SBart Van Assche  * The basic unit of block I/O is a sector. It is used in a number of contexts
583233bde21SBart Van Assche  * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
584233bde21SBart Van Assche  * bytes. Variables of type sector_t represent an offset or size that is a
585233bde21SBart Van Assche  * multiple of 512 bytes. Hence these two constants.
586233bde21SBart Van Assche  */
587233bde21SBart Van Assche #ifndef SECTOR_SHIFT
588233bde21SBart Van Assche #define SECTOR_SHIFT 9
589233bde21SBart Van Assche #endif
590233bde21SBart Van Assche #ifndef SECTOR_SIZE
591233bde21SBart Van Assche #define SECTOR_SIZE (1 << SECTOR_SHIFT)
592233bde21SBart Van Assche #endif
593233bde21SBart Van Assche 
594018eca45SGuoqing Jiang #define PAGE_SECTORS_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
595018eca45SGuoqing Jiang #define PAGE_SECTORS		(1 << PAGE_SECTORS_SHIFT)
596018eca45SGuoqing Jiang #define SECTOR_MASK		(PAGE_SECTORS - 1)
597018eca45SGuoqing Jiang 
5986a5ac984SBart Van Assche #ifdef CONFIG_BLK_DEV_ZONED
59902694e86SChaitanya Kulkarni 
60002694e86SChaitanya Kulkarni /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
60102694e86SChaitanya Kulkarni const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
60202694e86SChaitanya Kulkarni 
603d0ea6bdeSDamien Le Moal static inline unsigned int bio_zone_no(struct bio *bio)
604d0ea6bdeSDamien Le Moal {
605d0ea6bdeSDamien Le Moal 	return blk_queue_zone_no(bdev_get_queue(bio->bi_bdev),
606d0ea6bdeSDamien Le Moal 				 bio->bi_iter.bi_sector);
607d0ea6bdeSDamien Le Moal }
608d0ea6bdeSDamien Le Moal 
609d0ea6bdeSDamien Le Moal static inline unsigned int bio_zone_is_seq(struct bio *bio)
610d0ea6bdeSDamien Le Moal {
611d0ea6bdeSDamien Le Moal 	return blk_queue_zone_is_seq(bdev_get_queue(bio->bi_bdev),
612d0ea6bdeSDamien Le Moal 				     bio->bi_iter.bi_sector);
613d0ea6bdeSDamien Le Moal }
6146a5ac984SBart Van Assche #endif /* CONFIG_BLK_DEV_ZONED */
6156cc77e9cSChristoph Hellwig 
616f31dc1cdSMartin K. Petersen static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
6178fe0d473SMike Christie 						     int op)
618f31dc1cdSMartin K. Petersen {
6197afafc8aSAdrian Hunter 	if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
620233bde21SBart Van Assche 		return min(q->limits.max_discard_sectors,
621233bde21SBart Van Assche 			   UINT_MAX >> SECTOR_SHIFT);
622f31dc1cdSMartin K. Petersen 
6238fe0d473SMike Christie 	if (unlikely(op == REQ_OP_WRITE_SAME))
6244363ac7cSMartin K. Petersen 		return q->limits.max_write_same_sectors;
6254363ac7cSMartin K. Petersen 
626a6f0788eSChaitanya Kulkarni 	if (unlikely(op == REQ_OP_WRITE_ZEROES))
627a6f0788eSChaitanya Kulkarni 		return q->limits.max_write_zeroes_sectors;
628a6f0788eSChaitanya Kulkarni 
629f31dc1cdSMartin K. Petersen 	return q->limits.max_sectors;
630f31dc1cdSMartin K. Petersen }
631f31dc1cdSMartin K. Petersen 
632762380adSJens Axboe /*
633762380adSJens Axboe  * Return maximum size of a request at given offset. Only valid for
634762380adSJens Axboe  * file system requests.
635762380adSJens Axboe  */
636762380adSJens Axboe static inline unsigned int blk_max_size_offset(struct request_queue *q,
6373ee16db3SMike Snitzer 					       sector_t offset,
6383ee16db3SMike Snitzer 					       unsigned int chunk_sectors)
639762380adSJens Axboe {
64065f33b35SMike Snitzer 	if (!chunk_sectors) {
64165f33b35SMike Snitzer 		if (q->limits.chunk_sectors)
6423ee16db3SMike Snitzer 			chunk_sectors = q->limits.chunk_sectors;
6433ee16db3SMike Snitzer 		else
644736ed4deSJens Axboe 			return q->limits.max_sectors;
64565f33b35SMike Snitzer 	}
646762380adSJens Axboe 
64707d098e6SMike Snitzer 	if (likely(is_power_of_2(chunk_sectors)))
64807d098e6SMike Snitzer 		chunk_sectors -= offset & (chunk_sectors - 1);
64907d098e6SMike Snitzer 	else
65007d098e6SMike Snitzer 		chunk_sectors -= sector_div(offset, chunk_sectors);
65107d098e6SMike Snitzer 
65207d098e6SMike Snitzer 	return min(q->limits.max_sectors, chunk_sectors);
653762380adSJens Axboe }
654762380adSJens Axboe 
6551da177e4SLinus Torvalds /*
6561da177e4SLinus Torvalds  * Access functions for manipulating queue properties
6571da177e4SLinus Torvalds  */
658165125e1SJens Axboe extern void blk_cleanup_queue(struct request_queue *);
6599bb33f24SChristoph Hellwig void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
660086fa5ffSMartin K. Petersen extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
661762380adSJens Axboe extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
6628a78362cSMartin K. Petersen extern void blk_queue_max_segments(struct request_queue *, unsigned short);
6631e739730SChristoph Hellwig extern void blk_queue_max_discard_segments(struct request_queue *,
6641e739730SChristoph Hellwig 		unsigned short);
665165125e1SJens Axboe extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
66667efc925SChristoph Hellwig extern void blk_queue_max_discard_sectors(struct request_queue *q,
66767efc925SChristoph Hellwig 		unsigned int max_discard_sectors);
6684363ac7cSMartin K. Petersen extern void blk_queue_max_write_same_sectors(struct request_queue *q,
6694363ac7cSMartin K. Petersen 		unsigned int max_write_same_sectors);
670a6f0788eSChaitanya Kulkarni extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
671a6f0788eSChaitanya Kulkarni 		unsigned int max_write_same_sectors);
672ad6bf88aSMikulas Patocka extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
6730512a75bSKeith Busch extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
6740512a75bSKeith Busch 		unsigned int max_zone_append_sectors);
675892b6f90SMartin K. Petersen extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
676a805a4faSDamien Le Moal void blk_queue_zone_write_granularity(struct request_queue *q,
677a805a4faSDamien Le Moal 				      unsigned int size);
678c72758f3SMartin K. Petersen extern void blk_queue_alignment_offset(struct request_queue *q,
679c72758f3SMartin K. Petersen 				       unsigned int alignment);
680471aa704SChristoph Hellwig void disk_update_readahead(struct gendisk *disk);
6817c958e32SMartin K. Petersen extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
682c72758f3SMartin K. Petersen extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
6833c5820c7SMartin K. Petersen extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
684c72758f3SMartin K. Petersen extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
685d278d4a8SJens Axboe extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
686e475bba2SMartin K. Petersen extern void blk_set_default_limits(struct queue_limits *lim);
687b1bd055dSMartin K. Petersen extern void blk_set_stacking_limits(struct queue_limits *lim);
688c72758f3SMartin K. Petersen extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
689c72758f3SMartin K. Petersen 			    sector_t offset);
690c72758f3SMartin K. Petersen extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
691c72758f3SMartin K. Petersen 			      sector_t offset);
69227f8221aSFUJITA Tomonori extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
693165125e1SJens Axboe extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
69403100aadSKeith Busch extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
695165125e1SJens Axboe extern void blk_queue_dma_alignment(struct request_queue *, int);
69611c3e689SJames Bottomley extern void blk_queue_update_dma_alignment(struct request_queue *, int);
697242f9dcbSJens Axboe extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
69893e9d8e8SJens Axboe extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
6992e9bc346SChristoph Hellwig 
7002e9bc346SChristoph Hellwig /*
7012e9bc346SChristoph Hellwig  * Elevator features for blk_queue_required_elevator_features:
7022e9bc346SChristoph Hellwig  */
7032e9bc346SChristoph Hellwig /* Supports zoned block devices sequential write constraint */
7042e9bc346SChristoph Hellwig #define ELEVATOR_F_ZBD_SEQ_WRITE	(1U << 0)
7052e9bc346SChristoph Hellwig /* Supports scheduling on multiple hardware queues */
7062e9bc346SChristoph Hellwig #define ELEVATOR_F_MQ_AWARE		(1U << 1)
7072e9bc346SChristoph Hellwig 
70868c43f13SDamien Le Moal extern void blk_queue_required_elevator_features(struct request_queue *q,
70968c43f13SDamien Le Moal 						 unsigned int features);
71045147fb5SYoshihiro Shimoda extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
71145147fb5SYoshihiro Shimoda 					      struct device *dev);
7121da177e4SLinus Torvalds 
71309ac46c4STejun Heo bool __must_check blk_get_queue(struct request_queue *);
714165125e1SJens Axboe extern void blk_put_queue(struct request_queue *);
7153f21c265SJens Axboe extern void blk_set_queue_dying(struct request_queue *);
7161da177e4SLinus Torvalds 
7171a4dcfa8SChristoph Hellwig #ifdef CONFIG_BLOCK
718316cc67dSShaohua Li /*
71975df7136SSuresh Jayaraman  * blk_plug permits building a queue of related requests by holding the I/O
72075df7136SSuresh Jayaraman  * fragments for a short period. This allows merging of sequential requests
72175df7136SSuresh Jayaraman  * into single larger request. As the requests are moved from a per-task list to
72275df7136SSuresh Jayaraman  * the device's request_queue in a batch, this results in improved scalability
72375df7136SSuresh Jayaraman  * as the lock contention for request_queue lock is reduced.
72475df7136SSuresh Jayaraman  *
72575df7136SSuresh Jayaraman  * It is ok not to disable preemption when adding the request to the plug list
72675df7136SSuresh Jayaraman  * or when attempting a merge, because blk_schedule_flush_list() will only flush
72775df7136SSuresh Jayaraman  * the plug list when the task sleeps by itself. For details, please see
72875df7136SSuresh Jayaraman  * schedule() where blk_schedule_flush_plug() is called.
729316cc67dSShaohua Li  */
73073c10101SJens Axboe struct blk_plug {
731*bc490f81SJens Axboe 	struct request *mq_list; /* blk-mq requests */
73247c122e3SJens Axboe 
73347c122e3SJens Axboe 	/* if ios_left is > 1, we can batch tag/rq allocations */
73447c122e3SJens Axboe 	struct request *cached_rq;
73547c122e3SJens Axboe 	unsigned short nr_ios;
73647c122e3SJens Axboe 
7375f0ed774SJens Axboe 	unsigned short rq_count;
73847c122e3SJens Axboe 
739ce5b009cSJens Axboe 	bool multiple_queues;
7405a473e83SJens Axboe 	bool nowait;
74147c122e3SJens Axboe 
74247c122e3SJens Axboe 	struct list_head cb_list; /* md requires an unplug callback */
74373c10101SJens Axboe };
74455c022bbSShaohua Li 
7459cbb1750SNeilBrown struct blk_plug_cb;
74674018dc3SNeilBrown typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
747048c9374SNeilBrown struct blk_plug_cb {
748048c9374SNeilBrown 	struct list_head list;
7499cbb1750SNeilBrown 	blk_plug_cb_fn callback;
7509cbb1750SNeilBrown 	void *data;
751048c9374SNeilBrown };
7529cbb1750SNeilBrown extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
7539cbb1750SNeilBrown 					     void *data, int size);
75473c10101SJens Axboe extern void blk_start_plug(struct blk_plug *);
75547c122e3SJens Axboe extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
75673c10101SJens Axboe extern void blk_finish_plug(struct blk_plug *);
757f6603783SJens Axboe extern void blk_flush_plug_list(struct blk_plug *, bool);
75873c10101SJens Axboe 
75973c10101SJens Axboe static inline void blk_flush_plug(struct task_struct *tsk)
76073c10101SJens Axboe {
76173c10101SJens Axboe 	struct blk_plug *plug = tsk->plug;
76273c10101SJens Axboe 
76388b996cdSChristoph Hellwig 	if (plug)
764a237c1c5SJens Axboe 		blk_flush_plug_list(plug, false);
765a237c1c5SJens Axboe }
766a237c1c5SJens Axboe 
767a237c1c5SJens Axboe static inline void blk_schedule_flush_plug(struct task_struct *tsk)
768a237c1c5SJens Axboe {
769a237c1c5SJens Axboe 	struct blk_plug *plug = tsk->plug;
770a237c1c5SJens Axboe 
771a237c1c5SJens Axboe 	if (plug)
772f6603783SJens Axboe 		blk_flush_plug_list(plug, true);
77373c10101SJens Axboe }
77473c10101SJens Axboe 
77573c10101SJens Axboe static inline bool blk_needs_flush_plug(struct task_struct *tsk)
77673c10101SJens Axboe {
77773c10101SJens Axboe 	struct blk_plug *plug = tsk->plug;
77873c10101SJens Axboe 
779320ae51fSJens Axboe 	return plug &&
780*bc490f81SJens Axboe 		 (plug->mq_list || !list_empty(&plug->cb_list));
78173c10101SJens Axboe }
78273c10101SJens Axboe 
783c6bf3f0eSChristoph Hellwig int blkdev_issue_flush(struct block_device *bdev);
7841a4dcfa8SChristoph Hellwig long nr_blockdev_pages(void);
7851a4dcfa8SChristoph Hellwig #else /* CONFIG_BLOCK */
7861a4dcfa8SChristoph Hellwig struct blk_plug {
7871a4dcfa8SChristoph Hellwig };
7881a4dcfa8SChristoph Hellwig 
78947c122e3SJens Axboe static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
79047c122e3SJens Axboe 					 unsigned short nr_ios)
79147c122e3SJens Axboe {
79247c122e3SJens Axboe }
79347c122e3SJens Axboe 
7941a4dcfa8SChristoph Hellwig static inline void blk_start_plug(struct blk_plug *plug)
7951a4dcfa8SChristoph Hellwig {
7961a4dcfa8SChristoph Hellwig }
7971a4dcfa8SChristoph Hellwig 
7981a4dcfa8SChristoph Hellwig static inline void blk_finish_plug(struct blk_plug *plug)
7991a4dcfa8SChristoph Hellwig {
8001a4dcfa8SChristoph Hellwig }
8011a4dcfa8SChristoph Hellwig 
8021a4dcfa8SChristoph Hellwig static inline void blk_flush_plug(struct task_struct *task)
8031a4dcfa8SChristoph Hellwig {
8041a4dcfa8SChristoph Hellwig }
8051a4dcfa8SChristoph Hellwig 
8061a4dcfa8SChristoph Hellwig static inline void blk_schedule_flush_plug(struct task_struct *task)
8071a4dcfa8SChristoph Hellwig {
8081a4dcfa8SChristoph Hellwig }
8091a4dcfa8SChristoph Hellwig 
8101a4dcfa8SChristoph Hellwig 
8111a4dcfa8SChristoph Hellwig static inline bool blk_needs_flush_plug(struct task_struct *tsk)
8121a4dcfa8SChristoph Hellwig {
8131a4dcfa8SChristoph Hellwig 	return false;
8141a4dcfa8SChristoph Hellwig }
8151a4dcfa8SChristoph Hellwig 
816c6bf3f0eSChristoph Hellwig static inline int blkdev_issue_flush(struct block_device *bdev)
8171a4dcfa8SChristoph Hellwig {
8181a4dcfa8SChristoph Hellwig 	return 0;
8191a4dcfa8SChristoph Hellwig }
8201a4dcfa8SChristoph Hellwig 
8211a4dcfa8SChristoph Hellwig static inline long nr_blockdev_pages(void)
8221a4dcfa8SChristoph Hellwig {
8231a4dcfa8SChristoph Hellwig 	return 0;
8241a4dcfa8SChristoph Hellwig }
8251a4dcfa8SChristoph Hellwig #endif /* CONFIG_BLOCK */
8261a4dcfa8SChristoph Hellwig 
82771ac860aSMing Lei extern void blk_io_schedule(void);
82871ac860aSMing Lei 
829ee472d83SChristoph Hellwig extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
830ee472d83SChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, struct page *page);
831e950fdf7SChristoph Hellwig 
832e950fdf7SChristoph Hellwig #define BLKDEV_DISCARD_SECURE	(1 << 0)	/* issue a secure erase */
833dd3932edSChristoph Hellwig 
834fbd9b09aSDmitry Monakhov extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
835fbd9b09aSDmitry Monakhov 		sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
83638f25255SChristoph Hellwig extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
837288dab8aSChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, int flags,
838469e3216SMike Christie 		struct bio **biop);
839ee472d83SChristoph Hellwig 
840ee472d83SChristoph Hellwig #define BLKDEV_ZERO_NOUNMAP	(1 << 0)  /* do not free blocks */
841cb365b96SChristoph Hellwig #define BLKDEV_ZERO_NOFALLBACK	(1 << 1)  /* don't write explicit zeroes */
842ee472d83SChristoph Hellwig 
843e73c23ffSChaitanya Kulkarni extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
844e73c23ffSChaitanya Kulkarni 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
845ee472d83SChristoph Hellwig 		unsigned flags);
8463f14d792SDmitry Monakhov extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
847ee472d83SChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
848ee472d83SChristoph Hellwig 
8492cf6d26aSChristoph Hellwig static inline int sb_issue_discard(struct super_block *sb, sector_t block,
8502cf6d26aSChristoph Hellwig 		sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
851fb2dce86SDavid Woodhouse {
852233bde21SBart Van Assche 	return blkdev_issue_discard(sb->s_bdev,
853233bde21SBart Van Assche 				    block << (sb->s_blocksize_bits -
854233bde21SBart Van Assche 					      SECTOR_SHIFT),
855233bde21SBart Van Assche 				    nr_blocks << (sb->s_blocksize_bits -
856233bde21SBart Van Assche 						  SECTOR_SHIFT),
8572cf6d26aSChristoph Hellwig 				    gfp_mask, flags);
858fb2dce86SDavid Woodhouse }
859e6fa0be6SLukas Czerner static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
860a107e5a3STheodore Ts'o 		sector_t nr_blocks, gfp_t gfp_mask)
861e6fa0be6SLukas Czerner {
862e6fa0be6SLukas Czerner 	return blkdev_issue_zeroout(sb->s_bdev,
863233bde21SBart Van Assche 				    block << (sb->s_blocksize_bits -
864233bde21SBart Van Assche 					      SECTOR_SHIFT),
865233bde21SBart Van Assche 				    nr_blocks << (sb->s_blocksize_bits -
866233bde21SBart Van Assche 						  SECTOR_SHIFT),
867ee472d83SChristoph Hellwig 				    gfp_mask, 0);
868e6fa0be6SLukas Czerner }
8691da177e4SLinus Torvalds 
870fa01b1e9SChristoph Hellwig static inline bool bdev_is_partition(struct block_device *bdev)
871fa01b1e9SChristoph Hellwig {
872fa01b1e9SChristoph Hellwig 	return bdev->bd_partno;
873fa01b1e9SChristoph Hellwig }
874fa01b1e9SChristoph Hellwig 
875eb28d31bSMartin K. Petersen enum blk_default_limits {
876eb28d31bSMartin K. Petersen 	BLK_MAX_SEGMENTS	= 128,
877eb28d31bSMartin K. Petersen 	BLK_SAFE_MAX_SECTORS	= 255,
878d2be537cSJeff Moyer 	BLK_DEF_MAX_SECTORS	= 2560,
879eb28d31bSMartin K. Petersen 	BLK_MAX_SEGMENT_SIZE	= 65536,
880eb28d31bSMartin K. Petersen 	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
881eb28d31bSMartin K. Petersen };
8820e435ac2SMilan Broz 
883af2c68feSBart Van Assche static inline unsigned long queue_segment_boundary(const struct request_queue *q)
884ae03bf63SMartin K. Petersen {
885025146e1SMartin K. Petersen 	return q->limits.seg_boundary_mask;
886ae03bf63SMartin K. Petersen }
887ae03bf63SMartin K. Petersen 
888af2c68feSBart Van Assche static inline unsigned long queue_virt_boundary(const struct request_queue *q)
88903100aadSKeith Busch {
89003100aadSKeith Busch 	return q->limits.virt_boundary_mask;
89103100aadSKeith Busch }
89203100aadSKeith Busch 
893af2c68feSBart Van Assche static inline unsigned int queue_max_sectors(const struct request_queue *q)
894ae03bf63SMartin K. Petersen {
895025146e1SMartin K. Petersen 	return q->limits.max_sectors;
896ae03bf63SMartin K. Petersen }
897ae03bf63SMartin K. Petersen 
898547e2f70SChristoph Hellwig static inline unsigned int queue_max_bytes(struct request_queue *q)
899547e2f70SChristoph Hellwig {
900547e2f70SChristoph Hellwig 	return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
901547e2f70SChristoph Hellwig }
902547e2f70SChristoph Hellwig 
903af2c68feSBart Van Assche static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
904ae03bf63SMartin K. Petersen {
905025146e1SMartin K. Petersen 	return q->limits.max_hw_sectors;
906ae03bf63SMartin K. Petersen }
907ae03bf63SMartin K. Petersen 
908af2c68feSBart Van Assche static inline unsigned short queue_max_segments(const struct request_queue *q)
909ae03bf63SMartin K. Petersen {
9108a78362cSMartin K. Petersen 	return q->limits.max_segments;
911ae03bf63SMartin K. Petersen }
912ae03bf63SMartin K. Petersen 
913af2c68feSBart Van Assche static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
9141e739730SChristoph Hellwig {
9151e739730SChristoph Hellwig 	return q->limits.max_discard_segments;
9161e739730SChristoph Hellwig }
9171e739730SChristoph Hellwig 
918af2c68feSBart Van Assche static inline unsigned int queue_max_segment_size(const struct request_queue *q)
919ae03bf63SMartin K. Petersen {
920025146e1SMartin K. Petersen 	return q->limits.max_segment_size;
921ae03bf63SMartin K. Petersen }
922ae03bf63SMartin K. Petersen 
9230512a75bSKeith Busch static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
9240512a75bSKeith Busch {
925fe6f0cdcSJohannes Thumshirn 
926fe6f0cdcSJohannes Thumshirn 	const struct queue_limits *l = &q->limits;
927fe6f0cdcSJohannes Thumshirn 
928fe6f0cdcSJohannes Thumshirn 	return min(l->max_zone_append_sectors, l->max_sectors);
9290512a75bSKeith Busch }
9300512a75bSKeith Busch 
931ad6bf88aSMikulas Patocka static inline unsigned queue_logical_block_size(const struct request_queue *q)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	int retval = 512;
9341da177e4SLinus Torvalds 
935025146e1SMartin K. Petersen 	if (q && q->limits.logical_block_size)
936025146e1SMartin K. Petersen 		retval = q->limits.logical_block_size;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	return retval;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
941ad6bf88aSMikulas Patocka static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
9421da177e4SLinus Torvalds {
943e1defc4fSMartin K. Petersen 	return queue_logical_block_size(bdev_get_queue(bdev));
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
946af2c68feSBart Van Assche static inline unsigned int queue_physical_block_size(const struct request_queue *q)
947c72758f3SMartin K. Petersen {
948c72758f3SMartin K. Petersen 	return q->limits.physical_block_size;
949c72758f3SMartin K. Petersen }
950c72758f3SMartin K. Petersen 
951892b6f90SMartin K. Petersen static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
952ac481c20SMartin K. Petersen {
953ac481c20SMartin K. Petersen 	return queue_physical_block_size(bdev_get_queue(bdev));
954ac481c20SMartin K. Petersen }
955ac481c20SMartin K. Petersen 
956af2c68feSBart Van Assche static inline unsigned int queue_io_min(const struct request_queue *q)
957c72758f3SMartin K. Petersen {
958c72758f3SMartin K. Petersen 	return q->limits.io_min;
959c72758f3SMartin K. Petersen }
960c72758f3SMartin K. Petersen 
961ac481c20SMartin K. Petersen static inline int bdev_io_min(struct block_device *bdev)
962ac481c20SMartin K. Petersen {
963ac481c20SMartin K. Petersen 	return queue_io_min(bdev_get_queue(bdev));
964ac481c20SMartin K. Petersen }
965ac481c20SMartin K. Petersen 
966af2c68feSBart Van Assche static inline unsigned int queue_io_opt(const struct request_queue *q)
967c72758f3SMartin K. Petersen {
968c72758f3SMartin K. Petersen 	return q->limits.io_opt;
969c72758f3SMartin K. Petersen }
970c72758f3SMartin K. Petersen 
971ac481c20SMartin K. Petersen static inline int bdev_io_opt(struct block_device *bdev)
972ac481c20SMartin K. Petersen {
973ac481c20SMartin K. Petersen 	return queue_io_opt(bdev_get_queue(bdev));
974ac481c20SMartin K. Petersen }
975ac481c20SMartin K. Petersen 
976a805a4faSDamien Le Moal static inline unsigned int
977a805a4faSDamien Le Moal queue_zone_write_granularity(const struct request_queue *q)
978a805a4faSDamien Le Moal {
979a805a4faSDamien Le Moal 	return q->limits.zone_write_granularity;
980a805a4faSDamien Le Moal }
981a805a4faSDamien Le Moal 
982a805a4faSDamien Le Moal static inline unsigned int
983a805a4faSDamien Le Moal bdev_zone_write_granularity(struct block_device *bdev)
984a805a4faSDamien Le Moal {
985a805a4faSDamien Le Moal 	return queue_zone_write_granularity(bdev_get_queue(bdev));
986a805a4faSDamien Le Moal }
987a805a4faSDamien Le Moal 
988af2c68feSBart Van Assche static inline int queue_alignment_offset(const struct request_queue *q)
989c72758f3SMartin K. Petersen {
990ac481c20SMartin K. Petersen 	if (q->limits.misaligned)
991c72758f3SMartin K. Petersen 		return -1;
992c72758f3SMartin K. Petersen 
993c72758f3SMartin K. Petersen 	return q->limits.alignment_offset;
994c72758f3SMartin K. Petersen }
995c72758f3SMartin K. Petersen 
996e03a72e1SMartin K. Petersen static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
99781744ee4SMartin K. Petersen {
99881744ee4SMartin K. Petersen 	unsigned int granularity = max(lim->physical_block_size, lim->io_min);
999233bde21SBart Van Assche 	unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1000233bde21SBart Van Assche 		<< SECTOR_SHIFT;
100181744ee4SMartin K. Petersen 
1002b8839b8cSMike Snitzer 	return (granularity + lim->alignment_offset - alignment) % granularity;
1003c72758f3SMartin K. Petersen }
1004c72758f3SMartin K. Petersen 
1005ac481c20SMartin K. Petersen static inline int bdev_alignment_offset(struct block_device *bdev)
1006ac481c20SMartin K. Petersen {
1007ac481c20SMartin K. Petersen 	struct request_queue *q = bdev_get_queue(bdev);
1008ac481c20SMartin K. Petersen 
1009ac481c20SMartin K. Petersen 	if (q->limits.misaligned)
1010ac481c20SMartin K. Petersen 		return -1;
1011fa01b1e9SChristoph Hellwig 	if (bdev_is_partition(bdev))
10127b8917f5SChristoph Hellwig 		return queue_limit_alignment_offset(&q->limits,
101329ff57c6SChristoph Hellwig 				bdev->bd_start_sect);
1014ac481c20SMartin K. Petersen 	return q->limits.alignment_offset;
1015ac481c20SMartin K. Petersen }
1016ac481c20SMartin K. Petersen 
1017af2c68feSBart Van Assche static inline int queue_discard_alignment(const struct request_queue *q)
101886b37281SMartin K. Petersen {
101986b37281SMartin K. Petersen 	if (q->limits.discard_misaligned)
102086b37281SMartin K. Petersen 		return -1;
102186b37281SMartin K. Petersen 
102286b37281SMartin K. Petersen 	return q->limits.discard_alignment;
102386b37281SMartin K. Petersen }
102486b37281SMartin K. Petersen 
1025e03a72e1SMartin K. Petersen static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
102686b37281SMartin K. Petersen {
102759771079SLinus Torvalds 	unsigned int alignment, granularity, offset;
1028dd3d145dSMartin K. Petersen 
1029a934a00aSMartin K. Petersen 	if (!lim->max_discard_sectors)
1030a934a00aSMartin K. Petersen 		return 0;
1031a934a00aSMartin K. Petersen 
103259771079SLinus Torvalds 	/* Why are these in bytes, not sectors? */
1033233bde21SBart Van Assche 	alignment = lim->discard_alignment >> SECTOR_SHIFT;
1034233bde21SBart Van Assche 	granularity = lim->discard_granularity >> SECTOR_SHIFT;
103559771079SLinus Torvalds 	if (!granularity)
103659771079SLinus Torvalds 		return 0;
103759771079SLinus Torvalds 
103859771079SLinus Torvalds 	/* Offset of the partition start in 'granularity' sectors */
103959771079SLinus Torvalds 	offset = sector_div(sector, granularity);
104059771079SLinus Torvalds 
104159771079SLinus Torvalds 	/* And why do we do this modulus *again* in blkdev_issue_discard()? */
104259771079SLinus Torvalds 	offset = (granularity + alignment - offset) % granularity;
104359771079SLinus Torvalds 
104459771079SLinus Torvalds 	/* Turn it back into bytes, gaah */
1045233bde21SBart Van Assche 	return offset << SECTOR_SHIFT;
104686b37281SMartin K. Petersen }
104786b37281SMartin K. Petersen 
1048c6e66634SPaolo Bonzini static inline int bdev_discard_alignment(struct block_device *bdev)
1049c6e66634SPaolo Bonzini {
1050c6e66634SPaolo Bonzini 	struct request_queue *q = bdev_get_queue(bdev);
1051c6e66634SPaolo Bonzini 
1052fa01b1e9SChristoph Hellwig 	if (bdev_is_partition(bdev))
10537cf34d97SChristoph Hellwig 		return queue_limit_discard_alignment(&q->limits,
105429ff57c6SChristoph Hellwig 				bdev->bd_start_sect);
1055c6e66634SPaolo Bonzini 	return q->limits.discard_alignment;
1056c6e66634SPaolo Bonzini }
1057c6e66634SPaolo Bonzini 
10584363ac7cSMartin K. Petersen static inline unsigned int bdev_write_same(struct block_device *bdev)
10594363ac7cSMartin K. Petersen {
10604363ac7cSMartin K. Petersen 	struct request_queue *q = bdev_get_queue(bdev);
10614363ac7cSMartin K. Petersen 
10624363ac7cSMartin K. Petersen 	if (q)
10634363ac7cSMartin K. Petersen 		return q->limits.max_write_same_sectors;
10644363ac7cSMartin K. Petersen 
10654363ac7cSMartin K. Petersen 	return 0;
10664363ac7cSMartin K. Petersen }
10674363ac7cSMartin K. Petersen 
1068a6f0788eSChaitanya Kulkarni static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1069a6f0788eSChaitanya Kulkarni {
1070a6f0788eSChaitanya Kulkarni 	struct request_queue *q = bdev_get_queue(bdev);
1071a6f0788eSChaitanya Kulkarni 
1072a6f0788eSChaitanya Kulkarni 	if (q)
1073a6f0788eSChaitanya Kulkarni 		return q->limits.max_write_zeroes_sectors;
1074a6f0788eSChaitanya Kulkarni 
1075a6f0788eSChaitanya Kulkarni 	return 0;
1076a6f0788eSChaitanya Kulkarni }
1077a6f0788eSChaitanya Kulkarni 
1078797476b8SDamien Le Moal static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1079797476b8SDamien Le Moal {
1080797476b8SDamien Le Moal 	struct request_queue *q = bdev_get_queue(bdev);
1081797476b8SDamien Le Moal 
1082797476b8SDamien Le Moal 	if (q)
1083797476b8SDamien Le Moal 		return blk_queue_zoned_model(q);
1084797476b8SDamien Le Moal 
1085797476b8SDamien Le Moal 	return BLK_ZONED_NONE;
1086797476b8SDamien Le Moal }
1087797476b8SDamien Le Moal 
1088797476b8SDamien Le Moal static inline bool bdev_is_zoned(struct block_device *bdev)
1089797476b8SDamien Le Moal {
1090797476b8SDamien Le Moal 	struct request_queue *q = bdev_get_queue(bdev);
1091797476b8SDamien Le Moal 
1092797476b8SDamien Le Moal 	if (q)
1093797476b8SDamien Le Moal 		return blk_queue_is_zoned(q);
1094797476b8SDamien Le Moal 
1095797476b8SDamien Le Moal 	return false;
1096797476b8SDamien Le Moal }
1097797476b8SDamien Le Moal 
1098113ab72eSDamien Le Moal static inline sector_t bdev_zone_sectors(struct block_device *bdev)
10996a0cb1bcSHannes Reinecke {
11006a0cb1bcSHannes Reinecke 	struct request_queue *q = bdev_get_queue(bdev);
11016a0cb1bcSHannes Reinecke 
11026a0cb1bcSHannes Reinecke 	if (q)
1103f99e8648SDamien Le Moal 		return blk_queue_zone_sectors(q);
11046cc77e9cSChristoph Hellwig 	return 0;
11056cc77e9cSChristoph Hellwig }
11066a0cb1bcSHannes Reinecke 
1107e15864f8SNiklas Cassel static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
1108e15864f8SNiklas Cassel {
1109e15864f8SNiklas Cassel 	struct request_queue *q = bdev_get_queue(bdev);
1110e15864f8SNiklas Cassel 
1111e15864f8SNiklas Cassel 	if (q)
1112e15864f8SNiklas Cassel 		return queue_max_open_zones(q);
1113e15864f8SNiklas Cassel 	return 0;
1114e15864f8SNiklas Cassel }
1115e15864f8SNiklas Cassel 
1116659bf827SNiklas Cassel static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
1117659bf827SNiklas Cassel {
1118659bf827SNiklas Cassel 	struct request_queue *q = bdev_get_queue(bdev);
1119659bf827SNiklas Cassel 
1120659bf827SNiklas Cassel 	if (q)
1121659bf827SNiklas Cassel 		return queue_max_active_zones(q);
1122659bf827SNiklas Cassel 	return 0;
1123659bf827SNiklas Cassel }
1124659bf827SNiklas Cassel 
1125af2c68feSBart Van Assche static inline int queue_dma_alignment(const struct request_queue *q)
11261da177e4SLinus Torvalds {
1127482eb689SPete Wyckoff 	return q ? q->dma_alignment : 511;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
113014417799SNamhyung Kim static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
113187904074SFUJITA Tomonori 				 unsigned int len)
113287904074SFUJITA Tomonori {
113387904074SFUJITA Tomonori 	unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
113414417799SNamhyung Kim 	return !(addr & alignment) && !(len & alignment);
113587904074SFUJITA Tomonori }
113687904074SFUJITA Tomonori 
11371da177e4SLinus Torvalds /* assumes size > 256 */
11381da177e4SLinus Torvalds static inline unsigned int blksize_bits(unsigned int size)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	unsigned int bits = 8;
11411da177e4SLinus Torvalds 	do {
11421da177e4SLinus Torvalds 		bits++;
11431da177e4SLinus Torvalds 		size >>= 1;
11441da177e4SLinus Torvalds 	} while (size > 256);
11451da177e4SLinus Torvalds 	return bits;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11482befb9e3SAdrian Bunk static inline unsigned int block_size(struct block_device *bdev)
11491da177e4SLinus Torvalds {
11506b7b181bSChristoph Hellwig 	return 1 << bdev->bd_inode->i_blkbits;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
115359c3d45eSJens Axboe int kblockd_schedule_work(struct work_struct *work);
1154818cd1cbSJens Axboe int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds #define MODULE_ALIAS_BLOCKDEV(major,minor) \
11571da177e4SLinus Torvalds 	MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
11581da177e4SLinus Torvalds #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
11591da177e4SLinus Torvalds 	MODULE_ALIAS("block-major-" __stringify(major) "-*")
11601da177e4SLinus Torvalds 
1161d145dc23SSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1162d145dc23SSatya Tangirala 
1163d145dc23SSatya Tangirala bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
1164d145dc23SSatya Tangirala 
1165d145dc23SSatya Tangirala void blk_ksm_unregister(struct request_queue *q);
1166d145dc23SSatya Tangirala 
1167d145dc23SSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1168d145dc23SSatya Tangirala 
1169d145dc23SSatya Tangirala static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
1170d145dc23SSatya Tangirala 				    struct request_queue *q)
1171d145dc23SSatya Tangirala {
1172d145dc23SSatya Tangirala 	return true;
1173d145dc23SSatya Tangirala }
1174d145dc23SSatya Tangirala 
1175d145dc23SSatya Tangirala static inline void blk_ksm_unregister(struct request_queue *q) { }
1176d145dc23SSatya Tangirala 
1177d145dc23SSatya Tangirala #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1178d145dc23SSatya Tangirala 
1179d145dc23SSatya Tangirala 
118008f85851SAl Viro struct block_device_operations {
11813e08773cSChristoph Hellwig 	void (*submit_bio)(struct bio *bio);
1182d4430d62SAl Viro 	int (*open) (struct block_device *, fmode_t);
1183db2a144bSAl Viro 	void (*release) (struct gendisk *, fmode_t);
11843f289dcbSTejun Heo 	int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1185d4430d62SAl Viro 	int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1186d4430d62SAl Viro 	int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
118777ea887eSTejun Heo 	unsigned int (*check_events) (struct gendisk *disk,
118877ea887eSTejun Heo 				      unsigned int clearing);
1189c3e33e04STejun Heo 	void (*unlock_native_capacity) (struct gendisk *);
119008f85851SAl Viro 	int (*getgeo)(struct block_device *, struct hd_geometry *);
1191e00adcadSChristoph Hellwig 	int (*set_read_only)(struct block_device *bdev, bool ro);
1192b3a27d05SNitin Gupta 	/* this callback is with swap_lock and sometimes page table lock held */
1193b3a27d05SNitin Gupta 	void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1194e76239a3SChristoph Hellwig 	int (*report_zones)(struct gendisk *, sector_t sector,
1195d4100351SChristoph Hellwig 			unsigned int nr_zones, report_zones_cb cb, void *data);
1196348e114bSChristoph Hellwig 	char *(*devnode)(struct gendisk *disk, umode_t *mode);
119708f85851SAl Viro 	struct module *owner;
1198bbd3e064SChristoph Hellwig 	const struct pr_ops *pr_ops;
11990bdfbca8SDmitry Osipenko 
12000bdfbca8SDmitry Osipenko 	/*
12010bdfbca8SDmitry Osipenko 	 * Special callback for probing GPT entry at a given sector.
12020bdfbca8SDmitry Osipenko 	 * Needed by Android devices, used by GPT scanner and MMC blk
12030bdfbca8SDmitry Osipenko 	 * driver.
12040bdfbca8SDmitry Osipenko 	 */
12050bdfbca8SDmitry Osipenko 	int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
120608f85851SAl Viro };
120708f85851SAl Viro 
1208ee6a129dSArnd Bergmann #ifdef CONFIG_COMPAT
1209ee6a129dSArnd Bergmann extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1210ee6a129dSArnd Bergmann 				      unsigned int, unsigned long);
1211ee6a129dSArnd Bergmann #else
1212ee6a129dSArnd Bergmann #define blkdev_compat_ptr_ioctl NULL
1213ee6a129dSArnd Bergmann #endif
1214ee6a129dSArnd Bergmann 
121547a191fdSMatthew Wilcox extern int bdev_read_page(struct block_device *, sector_t, struct page *);
121647a191fdSMatthew Wilcox extern int bdev_write_page(struct block_device *, sector_t, struct page *,
121747a191fdSMatthew Wilcox 						struct writeback_control *);
12186cc77e9cSChristoph Hellwig 
12190619317fSJens Axboe static inline void blk_wake_io_task(struct task_struct *waiter)
12200619317fSJens Axboe {
12210619317fSJens Axboe 	/*
12220619317fSJens Axboe 	 * If we're polling, the task itself is doing the completions. For
12230619317fSJens Axboe 	 * that case, we don't need to signal a wakeup, it's enough to just
12240619317fSJens Axboe 	 * mark us as RUNNING.
12250619317fSJens Axboe 	 */
12260619317fSJens Axboe 	if (waiter == current)
12270619317fSJens Axboe 		__set_current_state(TASK_RUNNING);
12280619317fSJens Axboe 	else
12290619317fSJens Axboe 		wake_up_process(waiter);
12300619317fSJens Axboe }
12310619317fSJens Axboe 
1232956d510eSChristoph Hellwig unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1233956d510eSChristoph Hellwig 		unsigned int op);
1234956d510eSChristoph Hellwig void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1235956d510eSChristoph Hellwig 		unsigned long start_time);
1236956d510eSChristoph Hellwig 
123799dfc43eSChristoph Hellwig unsigned long bio_start_io_acct(struct bio *bio);
123899dfc43eSChristoph Hellwig void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
123999dfc43eSChristoph Hellwig 		struct block_device *orig_bdev);
1240956d510eSChristoph Hellwig 
1241956d510eSChristoph Hellwig /**
1242956d510eSChristoph Hellwig  * bio_end_io_acct - end I/O accounting for bio based drivers
1243956d510eSChristoph Hellwig  * @bio:	bio to end account for
1244956d510eSChristoph Hellwig  * @start:	start time returned by bio_start_io_acct()
1245956d510eSChristoph Hellwig  */
1246956d510eSChristoph Hellwig static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1247956d510eSChristoph Hellwig {
124899dfc43eSChristoph Hellwig 	return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1249956d510eSChristoph Hellwig }
1250956d510eSChristoph Hellwig 
12513f1266f1SChristoph Hellwig int bdev_read_only(struct block_device *bdev);
12523f1266f1SChristoph Hellwig int set_blocksize(struct block_device *bdev, int size);
12533f1266f1SChristoph Hellwig 
12543f1266f1SChristoph Hellwig const char *bdevname(struct block_device *bdev, char *buffer);
12554e7b5671SChristoph Hellwig int lookup_bdev(const char *pathname, dev_t *dev);
12563f1266f1SChristoph Hellwig 
12573f1266f1SChristoph Hellwig void blkdev_show(struct seq_file *seqf, off_t offset);
12583f1266f1SChristoph Hellwig 
12593f1266f1SChristoph Hellwig #define BDEVNAME_SIZE	32	/* Largest string for a blockdev identifier */
12603f1266f1SChristoph Hellwig #define BDEVT_SIZE	10	/* Largest string for MAJ:MIN for blkdev */
12613f1266f1SChristoph Hellwig #ifdef CONFIG_BLOCK
12623f1266f1SChristoph Hellwig #define BLKDEV_MAJOR_MAX	512
12633f1266f1SChristoph Hellwig #else
12643f1266f1SChristoph Hellwig #define BLKDEV_MAJOR_MAX	0
12651da177e4SLinus Torvalds #endif
12663f1266f1SChristoph Hellwig 
12673f1266f1SChristoph Hellwig struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
12683f1266f1SChristoph Hellwig 		void *holder);
12693f1266f1SChristoph Hellwig struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
127037c3fc9aSChristoph Hellwig int bd_prepare_to_claim(struct block_device *bdev, void *holder);
127137c3fc9aSChristoph Hellwig void bd_abort_claiming(struct block_device *bdev, void *holder);
12723f1266f1SChristoph Hellwig void blkdev_put(struct block_device *bdev, fmode_t mode);
12733f1266f1SChristoph Hellwig 
127422ae8ce8SChristoph Hellwig /* just for blk-cgroup, don't use elsewhere */
127522ae8ce8SChristoph Hellwig struct block_device *blkdev_get_no_open(dev_t dev);
127622ae8ce8SChristoph Hellwig void blkdev_put_no_open(struct block_device *bdev);
127722ae8ce8SChristoph Hellwig 
127822ae8ce8SChristoph Hellwig struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
127922ae8ce8SChristoph Hellwig void bdev_add(struct block_device *bdev, dev_t dev);
1280621c1f42SChristoph Hellwig struct block_device *I_BDEV(struct inode *inode);
12812c2b9fd6SChristoph Hellwig int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
12822c2b9fd6SChristoph Hellwig 		loff_t lend);
12833f1266f1SChristoph Hellwig 
12843f1266f1SChristoph Hellwig #ifdef CONFIG_BLOCK
12853f1266f1SChristoph Hellwig void invalidate_bdev(struct block_device *bdev);
12863f1266f1SChristoph Hellwig int sync_blockdev(struct block_device *bdev);
12873f1266f1SChristoph Hellwig #else
12883f1266f1SChristoph Hellwig static inline void invalidate_bdev(struct block_device *bdev)
12893f1266f1SChristoph Hellwig {
12903f1266f1SChristoph Hellwig }
12913f1266f1SChristoph Hellwig static inline int sync_blockdev(struct block_device *bdev)
12923f1266f1SChristoph Hellwig {
12933f1266f1SChristoph Hellwig 	return 0;
12943f1266f1SChristoph Hellwig }
12953f1266f1SChristoph Hellwig #endif
12963f1266f1SChristoph Hellwig int fsync_bdev(struct block_device *bdev);
12973f1266f1SChristoph Hellwig 
1298040f04bdSChristoph Hellwig int freeze_bdev(struct block_device *bdev);
1299040f04bdSChristoph Hellwig int thaw_bdev(struct block_device *bdev);
13003f1266f1SChristoph Hellwig 
13015a72e899SJens Axboe struct io_comp_batch {
13025a72e899SJens Axboe 	struct request *req_list;
13035a72e899SJens Axboe 	bool need_ts;
13045a72e899SJens Axboe 	void (*complete)(struct io_comp_batch *);
13055a72e899SJens Axboe };
13065a72e899SJens Axboe 
13075a72e899SJens Axboe #define DEFINE_IO_COMP_BATCH(name)	struct io_comp_batch name = { }
13085a72e899SJens Axboe 
1309013a7f95SJens Axboe #define rq_list_add(listptr, rq)	do {		\
1310013a7f95SJens Axboe 	(rq)->rq_next = *(listptr);			\
1311013a7f95SJens Axboe 	*(listptr) = rq;				\
1312013a7f95SJens Axboe } while (0)
1313013a7f95SJens Axboe 
1314013a7f95SJens Axboe #define rq_list_pop(listptr)				\
1315013a7f95SJens Axboe ({							\
1316013a7f95SJens Axboe 	struct request *__req = NULL;			\
1317013a7f95SJens Axboe 	if ((listptr) && *(listptr))	{		\
1318013a7f95SJens Axboe 		__req = *(listptr);			\
1319013a7f95SJens Axboe 		*(listptr) = __req->rq_next;		\
1320013a7f95SJens Axboe 	}						\
1321013a7f95SJens Axboe 	__req;						\
1322013a7f95SJens Axboe })
1323013a7f95SJens Axboe 
1324013a7f95SJens Axboe #define rq_list_peek(listptr)				\
1325013a7f95SJens Axboe ({							\
1326013a7f95SJens Axboe 	struct request *__req = NULL;			\
1327013a7f95SJens Axboe 	if ((listptr) && *(listptr))			\
1328013a7f95SJens Axboe 		__req = *(listptr);			\
1329013a7f95SJens Axboe 	__req;						\
1330013a7f95SJens Axboe })
1331013a7f95SJens Axboe 
1332013a7f95SJens Axboe #define rq_list_for_each(listptr, pos)			\
1333013a7f95SJens Axboe 	for (pos = rq_list_peek((listptr)); pos; pos = rq_list_next(pos)) \
1334013a7f95SJens Axboe 
1335013a7f95SJens Axboe #define rq_list_next(rq)	(rq)->rq_next
1336013a7f95SJens Axboe #define rq_list_empty(list)	((list) == (struct request *) NULL)
1337013a7f95SJens Axboe 
13383f1266f1SChristoph Hellwig #endif /* _LINUX_BLKDEV_H */
1339