xref: /linux-6.15/include/linux/blkdev.h (revision e70feb8b)
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 
318*e70feb8bSMing Lei 	int			quiesce_depth;
319*e70feb8bSMing Lei 
3200d2602caSJens Axboe 	struct blk_mq_tag_set	*tag_set;
3210d2602caSJens Axboe 	struct list_head	tag_set_list;
322338aa96dSKent Overstreet 	struct bio_set		bio_split;
3234593fdbeSAkinobu Mita 
32407e4feadSOmar Sandoval 	struct dentry		*debugfs_dir;
32585e0cbbbSLuis Chamberlain 
32685e0cbbbSLuis Chamberlain #ifdef CONFIG_BLK_DEBUG_FS
327d332ce09SOmar Sandoval 	struct dentry		*sched_debugfs_dir;
328cc56694fSMing Lei 	struct dentry		*rqos_debugfs_dir;
32907e4feadSOmar Sandoval #endif
33007e4feadSOmar Sandoval 
3314593fdbeSAkinobu Mita 	bool			mq_sysfs_init_done;
3326d247d7fSChristoph Hellwig 
333f793dfd3SJens Axboe #define BLK_MAX_WRITE_HINTS	5
334f793dfd3SJens Axboe 	u64			write_hints[BLK_MAX_WRITE_HINTS];
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
337bfe373f6SHou Tao /* Keep blk_queue_flag_name[] in sync with the definitions below */
338eca7abf3SJens Axboe #define QUEUE_FLAG_STOPPED	0	/* queue is stopped */
339eca7abf3SJens Axboe #define QUEUE_FLAG_DYING	1	/* queue being torn down */
340eca7abf3SJens Axboe #define QUEUE_FLAG_NOMERGES     3	/* disable merge attempts */
341eca7abf3SJens Axboe #define QUEUE_FLAG_SAME_COMP	4	/* complete on same CPU-group */
342eca7abf3SJens Axboe #define QUEUE_FLAG_FAIL_IO	5	/* fake timeout */
343eca7abf3SJens Axboe #define QUEUE_FLAG_NONROT	6	/* non-rotational device (SSD) */
34488e740f1SFernando Luis Vázquez Cao #define QUEUE_FLAG_VIRT		QUEUE_FLAG_NONROT /* paravirt device */
345eca7abf3SJens Axboe #define QUEUE_FLAG_IO_STAT	7	/* do disk/partitions IO accounting */
346eca7abf3SJens Axboe #define QUEUE_FLAG_DISCARD	8	/* supports DISCARD */
347eca7abf3SJens Axboe #define QUEUE_FLAG_NOXMERGES	9	/* No extended merges */
348eca7abf3SJens Axboe #define QUEUE_FLAG_ADD_RANDOM	10	/* Contributes to random pool */
349eca7abf3SJens Axboe #define QUEUE_FLAG_SECERASE	11	/* supports secure erase */
350eca7abf3SJens Axboe #define QUEUE_FLAG_SAME_FORCE	12	/* force complete on same CPU */
351eca7abf3SJens Axboe #define QUEUE_FLAG_DEAD		13	/* queue tear-down finished */
352eca7abf3SJens Axboe #define QUEUE_FLAG_INIT_DONE	14	/* queue is initialized */
3531cb039f3SChristoph Hellwig #define QUEUE_FLAG_STABLE_WRITES 15	/* don't modify blks until WB is done */
354eca7abf3SJens Axboe #define QUEUE_FLAG_POLL		16	/* IO polling enabled if set */
355eca7abf3SJens Axboe #define QUEUE_FLAG_WC		17	/* Write back caching */
356eca7abf3SJens Axboe #define QUEUE_FLAG_FUA		18	/* device supports FUA writes */
357eca7abf3SJens Axboe #define QUEUE_FLAG_DAX		19	/* device supports DAX */
358eca7abf3SJens Axboe #define QUEUE_FLAG_STATS	20	/* track IO start and completion times */
359eca7abf3SJens Axboe #define QUEUE_FLAG_POLL_STATS	21	/* collecting stats for hybrid polling */
360eca7abf3SJens Axboe #define QUEUE_FLAG_REGISTERED	22	/* queue has been registered to a disk */
361eca7abf3SJens Axboe #define QUEUE_FLAG_SCSI_PASSTHROUGH 23	/* queue supports SCSI commands */
362eca7abf3SJens Axboe #define QUEUE_FLAG_QUIESCED	24	/* queue has been quiesced */
363eca7abf3SJens Axboe #define QUEUE_FLAG_PCI_P2PDMA	25	/* device supports PCI p2p requests */
364e84e8f06SChaitanya Kulkarni #define QUEUE_FLAG_ZONE_RESETALL 26	/* supports Zone Reset All */
3656f816b4bSTejun Heo #define QUEUE_FLAG_RQ_ALLOC_TIME 27	/* record rq->alloc_time_ns */
366f1b49fdcSJohn Garry #define QUEUE_FLAG_HCTX_ACTIVE	28	/* at least one blk-mq hctx is active */
367021a2446SMike Snitzer #define QUEUE_FLAG_NOWAIT       29	/* device supports NOWAIT */
368797e7dbbSTejun Heo 
36994eddfbeSJens Axboe #define QUEUE_FLAG_MQ_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
370021a2446SMike Snitzer 				 (1 << QUEUE_FLAG_SAME_COMP) |		\
371021a2446SMike Snitzer 				 (1 << QUEUE_FLAG_NOWAIT))
37294eddfbeSJens Axboe 
3738814ce8aSBart Van Assche void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
3748814ce8aSBart Van Assche void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
3758814ce8aSBart Van Assche bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
3768814ce8aSBart Van Assche 
3771da177e4SLinus Torvalds #define blk_queue_stopped(q)	test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
3783f3299d5SBart Van Assche #define blk_queue_dying(q)	test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
379c246e80dSBart Van Assche #define blk_queue_dead(q)	test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
380320ae51fSJens Axboe #define blk_queue_init_done(q)	test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
381ac9fafa1SAlan D. Brunelle #define blk_queue_nomerges(q)	test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
382488991e2SAlan D. Brunelle #define blk_queue_noxmerges(q)	\
383488991e2SAlan D. Brunelle 	test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
384a68bbddbSJens Axboe #define blk_queue_nonrot(q)	test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
3851cb039f3SChristoph Hellwig #define blk_queue_stable_writes(q) \
3861cb039f3SChristoph Hellwig 	test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
387bc58ba94SJens Axboe #define blk_queue_io_stat(q)	test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
388e2e1a148SJens Axboe #define blk_queue_add_random(q)	test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
389c15227deSChristoph Hellwig #define blk_queue_discard(q)	test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
390e84e8f06SChaitanya Kulkarni #define blk_queue_zone_resetall(q)	\
391e84e8f06SChaitanya Kulkarni 	test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
392288dab8aSChristoph Hellwig #define blk_queue_secure_erase(q) \
393288dab8aSChristoph Hellwig 	(test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
394163d4baaSToshi Kani #define blk_queue_dax(q)	test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
3959efc160fSBart Van Assche #define blk_queue_scsi_passthrough(q)	\
3969efc160fSBart Van Assche 	test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
39749d92c0dSLogan Gunthorpe #define blk_queue_pci_p2pdma(q)	\
39849d92c0dSLogan Gunthorpe 	test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
3996f816b4bSTejun Heo #ifdef CONFIG_BLK_RQ_ALLOC_TIME
4006f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q)	\
4016f816b4bSTejun Heo 	test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
4026f816b4bSTejun Heo #else
4036f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q)	false
4046f816b4bSTejun Heo #endif
4051da177e4SLinus Torvalds 
40633659ebbSChristoph Hellwig #define blk_noretry_request(rq) \
40733659ebbSChristoph Hellwig 	((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
40833659ebbSChristoph Hellwig 			     REQ_FAILFAST_DRIVER))
409f4560ffeSMing Lei #define blk_queue_quiesced(q)	test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
410cd84a62eSBart Van Assche #define blk_queue_pm_only(q)	atomic_read(&(q)->pm_only)
4110ce91444SDave Chinner #define blk_queue_fua(q)	test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
41258c898baSMing Lei #define blk_queue_registered(q)	test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
413021a2446SMike Snitzer #define blk_queue_nowait(q)	test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
414c9254f2dSBart Van Assche 
415cd84a62eSBart Van Assche extern void blk_set_pm_only(struct request_queue *q);
416cd84a62eSBart Van Assche extern void blk_clear_pm_only(struct request_queue *q);
4174aff5e23SJens Axboe 
4181da177e4SLinus Torvalds #define list_entry_rq(ptr)	list_entry((ptr), struct request, queuelist)
4191da177e4SLinus Torvalds 
4203ab3a031SChristoph Hellwig #define dma_map_bvec(dev, bv, dir, attrs) \
4213ab3a031SChristoph Hellwig 	dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
4223ab3a031SChristoph Hellwig 	(dir), (attrs))
4233ab3a031SChristoph Hellwig 
424344e9ffcSJens Axboe static inline bool queue_is_mq(struct request_queue *q)
42549fd524fSJens Axboe {
426a1ce35faSJens Axboe 	return q->mq_ops;
42749fd524fSJens Axboe }
42849fd524fSJens Axboe 
42952abca64SAlan Stern #ifdef CONFIG_PM
43052abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
43152abca64SAlan Stern {
43252abca64SAlan Stern 	return q->rpm_status;
43352abca64SAlan Stern }
43452abca64SAlan Stern #else
43552abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
43652abca64SAlan Stern {
43752abca64SAlan Stern 	return RPM_ACTIVE;
43852abca64SAlan Stern }
43952abca64SAlan Stern #endif
44052abca64SAlan Stern 
441797476b8SDamien Le Moal static inline enum blk_zoned_model
442797476b8SDamien Le Moal blk_queue_zoned_model(struct request_queue *q)
443797476b8SDamien Le Moal {
4446fcd6695SChristoph Hellwig 	if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
445797476b8SDamien Le Moal 		return q->limits.zoned;
4466fcd6695SChristoph Hellwig 	return BLK_ZONED_NONE;
447797476b8SDamien Le Moal }
448797476b8SDamien Le Moal 
449797476b8SDamien Le Moal static inline bool blk_queue_is_zoned(struct request_queue *q)
450797476b8SDamien Le Moal {
451797476b8SDamien Le Moal 	switch (blk_queue_zoned_model(q)) {
452797476b8SDamien Le Moal 	case BLK_ZONED_HA:
453797476b8SDamien Le Moal 	case BLK_ZONED_HM:
454797476b8SDamien Le Moal 		return true;
455797476b8SDamien Le Moal 	default:
456797476b8SDamien Le Moal 		return false;
457797476b8SDamien Le Moal 	}
458797476b8SDamien Le Moal }
459797476b8SDamien Le Moal 
460113ab72eSDamien Le Moal static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
4616a0cb1bcSHannes Reinecke {
4626a0cb1bcSHannes Reinecke 	return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
4636a0cb1bcSHannes Reinecke }
4646a0cb1bcSHannes Reinecke 
4656a5ac984SBart Van Assche #ifdef CONFIG_BLK_DEV_ZONED
466965b652eSDamien Le Moal static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
467965b652eSDamien Le Moal {
468965b652eSDamien Le Moal 	return blk_queue_is_zoned(q) ? q->nr_zones : 0;
469965b652eSDamien Le Moal }
470965b652eSDamien Le Moal 
4716cc77e9cSChristoph Hellwig static inline unsigned int blk_queue_zone_no(struct request_queue *q,
4726cc77e9cSChristoph Hellwig 					     sector_t sector)
4736cc77e9cSChristoph Hellwig {
4746cc77e9cSChristoph Hellwig 	if (!blk_queue_is_zoned(q))
4756cc77e9cSChristoph Hellwig 		return 0;
4766cc77e9cSChristoph Hellwig 	return sector >> ilog2(q->limits.chunk_sectors);
4776cc77e9cSChristoph Hellwig }
4786cc77e9cSChristoph Hellwig 
4796cc77e9cSChristoph Hellwig static inline bool blk_queue_zone_is_seq(struct request_queue *q,
4806cc77e9cSChristoph Hellwig 					 sector_t sector)
4816cc77e9cSChristoph Hellwig {
482f216fdd7SChristoph Hellwig 	if (!blk_queue_is_zoned(q))
4836cc77e9cSChristoph Hellwig 		return false;
484f216fdd7SChristoph Hellwig 	if (!q->conv_zones_bitmap)
485f216fdd7SChristoph Hellwig 		return true;
486f216fdd7SChristoph Hellwig 	return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
4876cc77e9cSChristoph Hellwig }
488e15864f8SNiklas Cassel 
489e15864f8SNiklas Cassel static inline void blk_queue_max_open_zones(struct request_queue *q,
490e15864f8SNiklas Cassel 		unsigned int max_open_zones)
491e15864f8SNiklas Cassel {
492e15864f8SNiklas Cassel 	q->max_open_zones = max_open_zones;
493e15864f8SNiklas Cassel }
494e15864f8SNiklas Cassel 
495e15864f8SNiklas Cassel static inline unsigned int queue_max_open_zones(const struct request_queue *q)
496e15864f8SNiklas Cassel {
497e15864f8SNiklas Cassel 	return q->max_open_zones;
498e15864f8SNiklas Cassel }
499659bf827SNiklas Cassel 
500659bf827SNiklas Cassel static inline void blk_queue_max_active_zones(struct request_queue *q,
501659bf827SNiklas Cassel 		unsigned int max_active_zones)
502659bf827SNiklas Cassel {
503659bf827SNiklas Cassel 	q->max_active_zones = max_active_zones;
504659bf827SNiklas Cassel }
505659bf827SNiklas Cassel 
506659bf827SNiklas Cassel static inline unsigned int queue_max_active_zones(const struct request_queue *q)
507659bf827SNiklas Cassel {
508659bf827SNiklas Cassel 	return q->max_active_zones;
509659bf827SNiklas Cassel }
510965b652eSDamien Le Moal #else /* CONFIG_BLK_DEV_ZONED */
511965b652eSDamien Le Moal static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
512965b652eSDamien Le Moal {
513965b652eSDamien Le Moal 	return 0;
514965b652eSDamien Le Moal }
51502992df8SJohannes Thumshirn static inline bool blk_queue_zone_is_seq(struct request_queue *q,
51602992df8SJohannes Thumshirn 					 sector_t sector)
51702992df8SJohannes Thumshirn {
51802992df8SJohannes Thumshirn 	return false;
51902992df8SJohannes Thumshirn }
52002992df8SJohannes Thumshirn static inline unsigned int blk_queue_zone_no(struct request_queue *q,
52102992df8SJohannes Thumshirn 					     sector_t sector)
52202992df8SJohannes Thumshirn {
52302992df8SJohannes Thumshirn 	return 0;
52402992df8SJohannes Thumshirn }
525e15864f8SNiklas Cassel static inline unsigned int queue_max_open_zones(const struct request_queue *q)
526e15864f8SNiklas Cassel {
527e15864f8SNiklas Cassel 	return 0;
528e15864f8SNiklas Cassel }
529659bf827SNiklas Cassel static inline unsigned int queue_max_active_zones(const struct request_queue *q)
530659bf827SNiklas Cassel {
531659bf827SNiklas Cassel 	return 0;
532659bf827SNiklas Cassel }
5336a5ac984SBart Van Assche #endif /* CONFIG_BLK_DEV_ZONED */
5346cc77e9cSChristoph Hellwig 
535d278d4a8SJens Axboe static inline unsigned int blk_queue_depth(struct request_queue *q)
536d278d4a8SJens Axboe {
537d278d4a8SJens Axboe 	if (q->queue_depth)
538d278d4a8SJens Axboe 		return q->queue_depth;
539d278d4a8SJens Axboe 
540d278d4a8SJens Axboe 	return q->nr_requests;
541d278d4a8SJens Axboe }
542d278d4a8SJens Axboe 
5433d6392cfSJens Axboe /*
5443d6392cfSJens Axboe  * default timeout for SG_IO if none specified
5453d6392cfSJens Axboe  */
5463d6392cfSJens Axboe #define BLK_DEFAULT_SG_TIMEOUT	(60 * HZ)
547f2f1fa78SLinus Torvalds #define BLK_MIN_SG_TIMEOUT	(7 * HZ)
5483d6392cfSJens Axboe 
5495705f702SNeilBrown /* This should not be used directly - use rq_for_each_segment */
5501e428079SJens Axboe #define for_each_bio(_bio)		\
5511e428079SJens Axboe 	for (; _bio; _bio = _bio->bi_next)
5521da177e4SLinus Torvalds 
5532d4dc890SIlya Loginov 
5541da177e4SLinus Torvalds extern int blk_register_queue(struct gendisk *disk);
5551da177e4SLinus Torvalds extern void blk_unregister_queue(struct gendisk *disk);
5563e08773cSChristoph Hellwig void submit_bio_noacct(struct bio *bio);
55724b83debSChristoph Hellwig 
558ef9e3facSKiyoshi Ueda extern int blk_lld_busy(struct request_queue *q);
559f695ca38SChristoph Hellwig extern void blk_queue_split(struct bio **);
5609a95e4efSBart Van Assche extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
5612e6edc95SDan Williams extern void blk_queue_exit(struct request_queue *q);
5621da177e4SLinus Torvalds extern void blk_sync_queue(struct request_queue *q);
563fb9b16e1SKeith Busch 
564e47bc4edSChaitanya Kulkarni /* Helper to convert REQ_OP_XXX to its string format XXX */
565e47bc4edSChaitanya Kulkarni extern const char *blk_op_str(unsigned int op);
566e47bc4edSChaitanya Kulkarni 
5672a842acaSChristoph Hellwig int blk_status_to_errno(blk_status_t status);
5682a842acaSChristoph Hellwig blk_status_t errno_to_blk_status(int errno);
5692a842acaSChristoph Hellwig 
570ef99b2d3SChristoph Hellwig /* only poll the hardware once, don't continue until a completion was found */
571ef99b2d3SChristoph Hellwig #define BLK_POLL_ONESHOT		(1 << 0)
572d729cf9aSChristoph Hellwig /* do not sleep to wait for the expected completion time */
573d729cf9aSChristoph Hellwig #define BLK_POLL_NOSLEEP		(1 << 1)
5745a72e899SJens Axboe int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
5755a72e899SJens Axboe int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
5765a72e899SJens Axboe 			unsigned int flags);
57705229beeSJens Axboe 
578165125e1SJens Axboe static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
5791da177e4SLinus Torvalds {
58017220ca5SPavel Begunkov 	return bdev->bd_queue;	/* this is never NULL */
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
584233bde21SBart Van Assche  * The basic unit of block I/O is a sector. It is used in a number of contexts
585233bde21SBart Van Assche  * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
586233bde21SBart Van Assche  * bytes. Variables of type sector_t represent an offset or size that is a
587233bde21SBart Van Assche  * multiple of 512 bytes. Hence these two constants.
588233bde21SBart Van Assche  */
589233bde21SBart Van Assche #ifndef SECTOR_SHIFT
590233bde21SBart Van Assche #define SECTOR_SHIFT 9
591233bde21SBart Van Assche #endif
592233bde21SBart Van Assche #ifndef SECTOR_SIZE
593233bde21SBart Van Assche #define SECTOR_SIZE (1 << SECTOR_SHIFT)
594233bde21SBart Van Assche #endif
595233bde21SBart Van Assche 
596018eca45SGuoqing Jiang #define PAGE_SECTORS_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
597018eca45SGuoqing Jiang #define PAGE_SECTORS		(1 << PAGE_SECTORS_SHIFT)
598018eca45SGuoqing Jiang #define SECTOR_MASK		(PAGE_SECTORS - 1)
599018eca45SGuoqing Jiang 
6006a5ac984SBart Van Assche #ifdef CONFIG_BLK_DEV_ZONED
60102694e86SChaitanya Kulkarni 
60202694e86SChaitanya Kulkarni /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
60302694e86SChaitanya Kulkarni const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
60402694e86SChaitanya Kulkarni 
605d0ea6bdeSDamien Le Moal static inline unsigned int bio_zone_no(struct bio *bio)
606d0ea6bdeSDamien Le Moal {
607d0ea6bdeSDamien Le Moal 	return blk_queue_zone_no(bdev_get_queue(bio->bi_bdev),
608d0ea6bdeSDamien Le Moal 				 bio->bi_iter.bi_sector);
609d0ea6bdeSDamien Le Moal }
610d0ea6bdeSDamien Le Moal 
611d0ea6bdeSDamien Le Moal static inline unsigned int bio_zone_is_seq(struct bio *bio)
612d0ea6bdeSDamien Le Moal {
613d0ea6bdeSDamien Le Moal 	return blk_queue_zone_is_seq(bdev_get_queue(bio->bi_bdev),
614d0ea6bdeSDamien Le Moal 				     bio->bi_iter.bi_sector);
615d0ea6bdeSDamien Le Moal }
6166a5ac984SBart Van Assche #endif /* CONFIG_BLK_DEV_ZONED */
6176cc77e9cSChristoph Hellwig 
618f31dc1cdSMartin K. Petersen static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
6198fe0d473SMike Christie 						     int op)
620f31dc1cdSMartin K. Petersen {
6217afafc8aSAdrian Hunter 	if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
622233bde21SBart Van Assche 		return min(q->limits.max_discard_sectors,
623233bde21SBart Van Assche 			   UINT_MAX >> SECTOR_SHIFT);
624f31dc1cdSMartin K. Petersen 
6258fe0d473SMike Christie 	if (unlikely(op == REQ_OP_WRITE_SAME))
6264363ac7cSMartin K. Petersen 		return q->limits.max_write_same_sectors;
6274363ac7cSMartin K. Petersen 
628a6f0788eSChaitanya Kulkarni 	if (unlikely(op == REQ_OP_WRITE_ZEROES))
629a6f0788eSChaitanya Kulkarni 		return q->limits.max_write_zeroes_sectors;
630a6f0788eSChaitanya Kulkarni 
631f31dc1cdSMartin K. Petersen 	return q->limits.max_sectors;
632f31dc1cdSMartin K. Petersen }
633f31dc1cdSMartin K. Petersen 
634762380adSJens Axboe /*
635762380adSJens Axboe  * Return maximum size of a request at given offset. Only valid for
636762380adSJens Axboe  * file system requests.
637762380adSJens Axboe  */
638762380adSJens Axboe static inline unsigned int blk_max_size_offset(struct request_queue *q,
6393ee16db3SMike Snitzer 					       sector_t offset,
6403ee16db3SMike Snitzer 					       unsigned int chunk_sectors)
641762380adSJens Axboe {
64265f33b35SMike Snitzer 	if (!chunk_sectors) {
64365f33b35SMike Snitzer 		if (q->limits.chunk_sectors)
6443ee16db3SMike Snitzer 			chunk_sectors = q->limits.chunk_sectors;
6453ee16db3SMike Snitzer 		else
646736ed4deSJens Axboe 			return q->limits.max_sectors;
64765f33b35SMike Snitzer 	}
648762380adSJens Axboe 
64907d098e6SMike Snitzer 	if (likely(is_power_of_2(chunk_sectors)))
65007d098e6SMike Snitzer 		chunk_sectors -= offset & (chunk_sectors - 1);
65107d098e6SMike Snitzer 	else
65207d098e6SMike Snitzer 		chunk_sectors -= sector_div(offset, chunk_sectors);
65307d098e6SMike Snitzer 
65407d098e6SMike Snitzer 	return min(q->limits.max_sectors, chunk_sectors);
655762380adSJens Axboe }
656762380adSJens Axboe 
6571da177e4SLinus Torvalds /*
6581da177e4SLinus Torvalds  * Access functions for manipulating queue properties
6591da177e4SLinus Torvalds  */
660165125e1SJens Axboe extern void blk_cleanup_queue(struct request_queue *);
6619bb33f24SChristoph Hellwig void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
662086fa5ffSMartin K. Petersen extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
663762380adSJens Axboe extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
6648a78362cSMartin K. Petersen extern void blk_queue_max_segments(struct request_queue *, unsigned short);
6651e739730SChristoph Hellwig extern void blk_queue_max_discard_segments(struct request_queue *,
6661e739730SChristoph Hellwig 		unsigned short);
667165125e1SJens Axboe extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
66867efc925SChristoph Hellwig extern void blk_queue_max_discard_sectors(struct request_queue *q,
66967efc925SChristoph Hellwig 		unsigned int max_discard_sectors);
6704363ac7cSMartin K. Petersen extern void blk_queue_max_write_same_sectors(struct request_queue *q,
6714363ac7cSMartin K. Petersen 		unsigned int max_write_same_sectors);
672a6f0788eSChaitanya Kulkarni extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
673a6f0788eSChaitanya Kulkarni 		unsigned int max_write_same_sectors);
674ad6bf88aSMikulas Patocka extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
6750512a75bSKeith Busch extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
6760512a75bSKeith Busch 		unsigned int max_zone_append_sectors);
677892b6f90SMartin K. Petersen extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
678a805a4faSDamien Le Moal void blk_queue_zone_write_granularity(struct request_queue *q,
679a805a4faSDamien Le Moal 				      unsigned int size);
680c72758f3SMartin K. Petersen extern void blk_queue_alignment_offset(struct request_queue *q,
681c72758f3SMartin K. Petersen 				       unsigned int alignment);
682471aa704SChristoph Hellwig void disk_update_readahead(struct gendisk *disk);
6837c958e32SMartin K. Petersen extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
684c72758f3SMartin K. Petersen extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
6853c5820c7SMartin K. Petersen extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
686c72758f3SMartin K. Petersen extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
687d278d4a8SJens Axboe extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
688e475bba2SMartin K. Petersen extern void blk_set_default_limits(struct queue_limits *lim);
689b1bd055dSMartin K. Petersen extern void blk_set_stacking_limits(struct queue_limits *lim);
690c72758f3SMartin K. Petersen extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
691c72758f3SMartin K. Petersen 			    sector_t offset);
692c72758f3SMartin K. Petersen extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
693c72758f3SMartin K. Petersen 			      sector_t offset);
69427f8221aSFUJITA Tomonori extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
695165125e1SJens Axboe extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
69603100aadSKeith Busch extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
697165125e1SJens Axboe extern void blk_queue_dma_alignment(struct request_queue *, int);
69811c3e689SJames Bottomley extern void blk_queue_update_dma_alignment(struct request_queue *, int);
699242f9dcbSJens Axboe extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
70093e9d8e8SJens Axboe extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
7012e9bc346SChristoph Hellwig 
7022e9bc346SChristoph Hellwig /*
7032e9bc346SChristoph Hellwig  * Elevator features for blk_queue_required_elevator_features:
7042e9bc346SChristoph Hellwig  */
7052e9bc346SChristoph Hellwig /* Supports zoned block devices sequential write constraint */
7062e9bc346SChristoph Hellwig #define ELEVATOR_F_ZBD_SEQ_WRITE	(1U << 0)
7072e9bc346SChristoph Hellwig /* Supports scheduling on multiple hardware queues */
7082e9bc346SChristoph Hellwig #define ELEVATOR_F_MQ_AWARE		(1U << 1)
7092e9bc346SChristoph Hellwig 
71068c43f13SDamien Le Moal extern void blk_queue_required_elevator_features(struct request_queue *q,
71168c43f13SDamien Le Moal 						 unsigned int features);
71245147fb5SYoshihiro Shimoda extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
71345147fb5SYoshihiro Shimoda 					      struct device *dev);
7141da177e4SLinus Torvalds 
71509ac46c4STejun Heo bool __must_check blk_get_queue(struct request_queue *);
716165125e1SJens Axboe extern void blk_put_queue(struct request_queue *);
7173f21c265SJens Axboe extern void blk_set_queue_dying(struct request_queue *);
7181da177e4SLinus Torvalds 
7191a4dcfa8SChristoph Hellwig #ifdef CONFIG_BLOCK
720316cc67dSShaohua Li /*
72175df7136SSuresh Jayaraman  * blk_plug permits building a queue of related requests by holding the I/O
72275df7136SSuresh Jayaraman  * fragments for a short period. This allows merging of sequential requests
72375df7136SSuresh Jayaraman  * into single larger request. As the requests are moved from a per-task list to
72475df7136SSuresh Jayaraman  * the device's request_queue in a batch, this results in improved scalability
72575df7136SSuresh Jayaraman  * as the lock contention for request_queue lock is reduced.
72675df7136SSuresh Jayaraman  *
72775df7136SSuresh Jayaraman  * It is ok not to disable preemption when adding the request to the plug list
72875df7136SSuresh Jayaraman  * or when attempting a merge, because blk_schedule_flush_list() will only flush
72975df7136SSuresh Jayaraman  * the plug list when the task sleeps by itself. For details, please see
73075df7136SSuresh Jayaraman  * schedule() where blk_schedule_flush_plug() is called.
731316cc67dSShaohua Li  */
73273c10101SJens Axboe struct blk_plug {
733bc490f81SJens Axboe 	struct request *mq_list; /* blk-mq requests */
73447c122e3SJens Axboe 
73547c122e3SJens Axboe 	/* if ios_left is > 1, we can batch tag/rq allocations */
73647c122e3SJens Axboe 	struct request *cached_rq;
73747c122e3SJens Axboe 	unsigned short nr_ios;
73847c122e3SJens Axboe 
7395f0ed774SJens Axboe 	unsigned short rq_count;
74047c122e3SJens Axboe 
741ce5b009cSJens Axboe 	bool multiple_queues;
742dc5fc361SJens Axboe 	bool has_elevator;
7435a473e83SJens Axboe 	bool nowait;
74447c122e3SJens Axboe 
74547c122e3SJens Axboe 	struct list_head cb_list; /* md requires an unplug callback */
74673c10101SJens Axboe };
74755c022bbSShaohua Li 
7489cbb1750SNeilBrown struct blk_plug_cb;
74974018dc3SNeilBrown typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
750048c9374SNeilBrown struct blk_plug_cb {
751048c9374SNeilBrown 	struct list_head list;
7529cbb1750SNeilBrown 	blk_plug_cb_fn callback;
7539cbb1750SNeilBrown 	void *data;
754048c9374SNeilBrown };
7559cbb1750SNeilBrown extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
7569cbb1750SNeilBrown 					     void *data, int size);
75773c10101SJens Axboe extern void blk_start_plug(struct blk_plug *);
75847c122e3SJens Axboe extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
75973c10101SJens Axboe extern void blk_finish_plug(struct blk_plug *);
760f6603783SJens Axboe extern void blk_flush_plug_list(struct blk_plug *, bool);
76173c10101SJens Axboe 
76273c10101SJens Axboe static inline void blk_flush_plug(struct task_struct *tsk)
76373c10101SJens Axboe {
76473c10101SJens Axboe 	struct blk_plug *plug = tsk->plug;
76573c10101SJens Axboe 
76688b996cdSChristoph Hellwig 	if (plug)
767a237c1c5SJens Axboe 		blk_flush_plug_list(plug, false);
768a237c1c5SJens Axboe }
769a237c1c5SJens Axboe 
770a237c1c5SJens Axboe static inline void blk_schedule_flush_plug(struct task_struct *tsk)
771a237c1c5SJens Axboe {
772a237c1c5SJens Axboe 	struct blk_plug *plug = tsk->plug;
773a237c1c5SJens Axboe 
774a237c1c5SJens Axboe 	if (plug)
775f6603783SJens Axboe 		blk_flush_plug_list(plug, true);
77673c10101SJens Axboe }
77773c10101SJens Axboe 
77873c10101SJens Axboe static inline bool blk_needs_flush_plug(struct task_struct *tsk)
77973c10101SJens Axboe {
78073c10101SJens Axboe 	struct blk_plug *plug = tsk->plug;
78173c10101SJens Axboe 
782320ae51fSJens Axboe 	return plug &&
783bc490f81SJens Axboe 		 (plug->mq_list || !list_empty(&plug->cb_list));
78473c10101SJens Axboe }
78573c10101SJens Axboe 
786c6bf3f0eSChristoph Hellwig int blkdev_issue_flush(struct block_device *bdev);
7871a4dcfa8SChristoph Hellwig long nr_blockdev_pages(void);
7881a4dcfa8SChristoph Hellwig #else /* CONFIG_BLOCK */
7891a4dcfa8SChristoph Hellwig struct blk_plug {
7901a4dcfa8SChristoph Hellwig };
7911a4dcfa8SChristoph Hellwig 
79247c122e3SJens Axboe static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
79347c122e3SJens Axboe 					 unsigned short nr_ios)
79447c122e3SJens Axboe {
79547c122e3SJens Axboe }
79647c122e3SJens Axboe 
7971a4dcfa8SChristoph Hellwig static inline void blk_start_plug(struct blk_plug *plug)
7981a4dcfa8SChristoph Hellwig {
7991a4dcfa8SChristoph Hellwig }
8001a4dcfa8SChristoph Hellwig 
8011a4dcfa8SChristoph Hellwig static inline void blk_finish_plug(struct blk_plug *plug)
8021a4dcfa8SChristoph Hellwig {
8031a4dcfa8SChristoph Hellwig }
8041a4dcfa8SChristoph Hellwig 
8051a4dcfa8SChristoph Hellwig static inline void blk_flush_plug(struct task_struct *task)
8061a4dcfa8SChristoph Hellwig {
8071a4dcfa8SChristoph Hellwig }
8081a4dcfa8SChristoph Hellwig 
8091a4dcfa8SChristoph Hellwig static inline void blk_schedule_flush_plug(struct task_struct *task)
8101a4dcfa8SChristoph Hellwig {
8111a4dcfa8SChristoph Hellwig }
8121a4dcfa8SChristoph Hellwig 
8131a4dcfa8SChristoph Hellwig 
8141a4dcfa8SChristoph Hellwig static inline bool blk_needs_flush_plug(struct task_struct *tsk)
8151a4dcfa8SChristoph Hellwig {
8161a4dcfa8SChristoph Hellwig 	return false;
8171a4dcfa8SChristoph Hellwig }
8181a4dcfa8SChristoph Hellwig 
819c6bf3f0eSChristoph Hellwig static inline int blkdev_issue_flush(struct block_device *bdev)
8201a4dcfa8SChristoph Hellwig {
8211a4dcfa8SChristoph Hellwig 	return 0;
8221a4dcfa8SChristoph Hellwig }
8231a4dcfa8SChristoph Hellwig 
8241a4dcfa8SChristoph Hellwig static inline long nr_blockdev_pages(void)
8251a4dcfa8SChristoph Hellwig {
8261a4dcfa8SChristoph Hellwig 	return 0;
8271a4dcfa8SChristoph Hellwig }
8281a4dcfa8SChristoph Hellwig #endif /* CONFIG_BLOCK */
8291a4dcfa8SChristoph Hellwig 
83071ac860aSMing Lei extern void blk_io_schedule(void);
83171ac860aSMing Lei 
832ee472d83SChristoph Hellwig extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
833ee472d83SChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, struct page *page);
834e950fdf7SChristoph Hellwig 
835e950fdf7SChristoph Hellwig #define BLKDEV_DISCARD_SECURE	(1 << 0)	/* issue a secure erase */
836dd3932edSChristoph Hellwig 
837fbd9b09aSDmitry Monakhov extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
838fbd9b09aSDmitry Monakhov 		sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
83938f25255SChristoph Hellwig extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
840288dab8aSChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, int flags,
841469e3216SMike Christie 		struct bio **biop);
842ee472d83SChristoph Hellwig 
843ee472d83SChristoph Hellwig #define BLKDEV_ZERO_NOUNMAP	(1 << 0)  /* do not free blocks */
844cb365b96SChristoph Hellwig #define BLKDEV_ZERO_NOFALLBACK	(1 << 1)  /* don't write explicit zeroes */
845ee472d83SChristoph Hellwig 
846e73c23ffSChaitanya Kulkarni extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
847e73c23ffSChaitanya Kulkarni 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
848ee472d83SChristoph Hellwig 		unsigned flags);
8493f14d792SDmitry Monakhov extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
850ee472d83SChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
851ee472d83SChristoph Hellwig 
8522cf6d26aSChristoph Hellwig static inline int sb_issue_discard(struct super_block *sb, sector_t block,
8532cf6d26aSChristoph Hellwig 		sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
854fb2dce86SDavid Woodhouse {
855233bde21SBart Van Assche 	return blkdev_issue_discard(sb->s_bdev,
856233bde21SBart Van Assche 				    block << (sb->s_blocksize_bits -
857233bde21SBart Van Assche 					      SECTOR_SHIFT),
858233bde21SBart Van Assche 				    nr_blocks << (sb->s_blocksize_bits -
859233bde21SBart Van Assche 						  SECTOR_SHIFT),
8602cf6d26aSChristoph Hellwig 				    gfp_mask, flags);
861fb2dce86SDavid Woodhouse }
862e6fa0be6SLukas Czerner static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
863a107e5a3STheodore Ts'o 		sector_t nr_blocks, gfp_t gfp_mask)
864e6fa0be6SLukas Czerner {
865e6fa0be6SLukas Czerner 	return blkdev_issue_zeroout(sb->s_bdev,
866233bde21SBart Van Assche 				    block << (sb->s_blocksize_bits -
867233bde21SBart Van Assche 					      SECTOR_SHIFT),
868233bde21SBart Van Assche 				    nr_blocks << (sb->s_blocksize_bits -
869233bde21SBart Van Assche 						  SECTOR_SHIFT),
870ee472d83SChristoph Hellwig 				    gfp_mask, 0);
871e6fa0be6SLukas Czerner }
8721da177e4SLinus Torvalds 
873fa01b1e9SChristoph Hellwig static inline bool bdev_is_partition(struct block_device *bdev)
874fa01b1e9SChristoph Hellwig {
875fa01b1e9SChristoph Hellwig 	return bdev->bd_partno;
876fa01b1e9SChristoph Hellwig }
877fa01b1e9SChristoph Hellwig 
878eb28d31bSMartin K. Petersen enum blk_default_limits {
879eb28d31bSMartin K. Petersen 	BLK_MAX_SEGMENTS	= 128,
880eb28d31bSMartin K. Petersen 	BLK_SAFE_MAX_SECTORS	= 255,
881d2be537cSJeff Moyer 	BLK_DEF_MAX_SECTORS	= 2560,
882eb28d31bSMartin K. Petersen 	BLK_MAX_SEGMENT_SIZE	= 65536,
883eb28d31bSMartin K. Petersen 	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
884eb28d31bSMartin K. Petersen };
8850e435ac2SMilan Broz 
886af2c68feSBart Van Assche static inline unsigned long queue_segment_boundary(const struct request_queue *q)
887ae03bf63SMartin K. Petersen {
888025146e1SMartin K. Petersen 	return q->limits.seg_boundary_mask;
889ae03bf63SMartin K. Petersen }
890ae03bf63SMartin K. Petersen 
891af2c68feSBart Van Assche static inline unsigned long queue_virt_boundary(const struct request_queue *q)
89203100aadSKeith Busch {
89303100aadSKeith Busch 	return q->limits.virt_boundary_mask;
89403100aadSKeith Busch }
89503100aadSKeith Busch 
896af2c68feSBart Van Assche static inline unsigned int queue_max_sectors(const struct request_queue *q)
897ae03bf63SMartin K. Petersen {
898025146e1SMartin K. Petersen 	return q->limits.max_sectors;
899ae03bf63SMartin K. Petersen }
900ae03bf63SMartin K. Petersen 
901547e2f70SChristoph Hellwig static inline unsigned int queue_max_bytes(struct request_queue *q)
902547e2f70SChristoph Hellwig {
903547e2f70SChristoph Hellwig 	return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
904547e2f70SChristoph Hellwig }
905547e2f70SChristoph Hellwig 
906af2c68feSBart Van Assche static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
907ae03bf63SMartin K. Petersen {
908025146e1SMartin K. Petersen 	return q->limits.max_hw_sectors;
909ae03bf63SMartin K. Petersen }
910ae03bf63SMartin K. Petersen 
911af2c68feSBart Van Assche static inline unsigned short queue_max_segments(const struct request_queue *q)
912ae03bf63SMartin K. Petersen {
9138a78362cSMartin K. Petersen 	return q->limits.max_segments;
914ae03bf63SMartin K. Petersen }
915ae03bf63SMartin K. Petersen 
916af2c68feSBart Van Assche static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
9171e739730SChristoph Hellwig {
9181e739730SChristoph Hellwig 	return q->limits.max_discard_segments;
9191e739730SChristoph Hellwig }
9201e739730SChristoph Hellwig 
921af2c68feSBart Van Assche static inline unsigned int queue_max_segment_size(const struct request_queue *q)
922ae03bf63SMartin K. Petersen {
923025146e1SMartin K. Petersen 	return q->limits.max_segment_size;
924ae03bf63SMartin K. Petersen }
925ae03bf63SMartin K. Petersen 
9260512a75bSKeith Busch static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
9270512a75bSKeith Busch {
928fe6f0cdcSJohannes Thumshirn 
929fe6f0cdcSJohannes Thumshirn 	const struct queue_limits *l = &q->limits;
930fe6f0cdcSJohannes Thumshirn 
931fe6f0cdcSJohannes Thumshirn 	return min(l->max_zone_append_sectors, l->max_sectors);
9320512a75bSKeith Busch }
9330512a75bSKeith Busch 
934ad6bf88aSMikulas Patocka static inline unsigned queue_logical_block_size(const struct request_queue *q)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	int retval = 512;
9371da177e4SLinus Torvalds 
938025146e1SMartin K. Petersen 	if (q && q->limits.logical_block_size)
939025146e1SMartin K. Petersen 		retval = q->limits.logical_block_size;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	return retval;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
944ad6bf88aSMikulas Patocka static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
9451da177e4SLinus Torvalds {
946e1defc4fSMartin K. Petersen 	return queue_logical_block_size(bdev_get_queue(bdev));
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
949af2c68feSBart Van Assche static inline unsigned int queue_physical_block_size(const struct request_queue *q)
950c72758f3SMartin K. Petersen {
951c72758f3SMartin K. Petersen 	return q->limits.physical_block_size;
952c72758f3SMartin K. Petersen }
953c72758f3SMartin K. Petersen 
954892b6f90SMartin K. Petersen static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
955ac481c20SMartin K. Petersen {
956ac481c20SMartin K. Petersen 	return queue_physical_block_size(bdev_get_queue(bdev));
957ac481c20SMartin K. Petersen }
958ac481c20SMartin K. Petersen 
959af2c68feSBart Van Assche static inline unsigned int queue_io_min(const struct request_queue *q)
960c72758f3SMartin K. Petersen {
961c72758f3SMartin K. Petersen 	return q->limits.io_min;
962c72758f3SMartin K. Petersen }
963c72758f3SMartin K. Petersen 
964ac481c20SMartin K. Petersen static inline int bdev_io_min(struct block_device *bdev)
965ac481c20SMartin K. Petersen {
966ac481c20SMartin K. Petersen 	return queue_io_min(bdev_get_queue(bdev));
967ac481c20SMartin K. Petersen }
968ac481c20SMartin K. Petersen 
969af2c68feSBart Van Assche static inline unsigned int queue_io_opt(const struct request_queue *q)
970c72758f3SMartin K. Petersen {
971c72758f3SMartin K. Petersen 	return q->limits.io_opt;
972c72758f3SMartin K. Petersen }
973c72758f3SMartin K. Petersen 
974ac481c20SMartin K. Petersen static inline int bdev_io_opt(struct block_device *bdev)
975ac481c20SMartin K. Petersen {
976ac481c20SMartin K. Petersen 	return queue_io_opt(bdev_get_queue(bdev));
977ac481c20SMartin K. Petersen }
978ac481c20SMartin K. Petersen 
979a805a4faSDamien Le Moal static inline unsigned int
980a805a4faSDamien Le Moal queue_zone_write_granularity(const struct request_queue *q)
981a805a4faSDamien Le Moal {
982a805a4faSDamien Le Moal 	return q->limits.zone_write_granularity;
983a805a4faSDamien Le Moal }
984a805a4faSDamien Le Moal 
985a805a4faSDamien Le Moal static inline unsigned int
986a805a4faSDamien Le Moal bdev_zone_write_granularity(struct block_device *bdev)
987a805a4faSDamien Le Moal {
988a805a4faSDamien Le Moal 	return queue_zone_write_granularity(bdev_get_queue(bdev));
989a805a4faSDamien Le Moal }
990a805a4faSDamien Le Moal 
991af2c68feSBart Van Assche static inline int queue_alignment_offset(const struct request_queue *q)
992c72758f3SMartin K. Petersen {
993ac481c20SMartin K. Petersen 	if (q->limits.misaligned)
994c72758f3SMartin K. Petersen 		return -1;
995c72758f3SMartin K. Petersen 
996c72758f3SMartin K. Petersen 	return q->limits.alignment_offset;
997c72758f3SMartin K. Petersen }
998c72758f3SMartin K. Petersen 
999e03a72e1SMartin K. Petersen static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
100081744ee4SMartin K. Petersen {
100181744ee4SMartin K. Petersen 	unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1002233bde21SBart Van Assche 	unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1003233bde21SBart Van Assche 		<< SECTOR_SHIFT;
100481744ee4SMartin K. Petersen 
1005b8839b8cSMike Snitzer 	return (granularity + lim->alignment_offset - alignment) % granularity;
1006c72758f3SMartin K. Petersen }
1007c72758f3SMartin K. Petersen 
1008ac481c20SMartin K. Petersen static inline int bdev_alignment_offset(struct block_device *bdev)
1009ac481c20SMartin K. Petersen {
1010ac481c20SMartin K. Petersen 	struct request_queue *q = bdev_get_queue(bdev);
1011ac481c20SMartin K. Petersen 
1012ac481c20SMartin K. Petersen 	if (q->limits.misaligned)
1013ac481c20SMartin K. Petersen 		return -1;
1014fa01b1e9SChristoph Hellwig 	if (bdev_is_partition(bdev))
10157b8917f5SChristoph Hellwig 		return queue_limit_alignment_offset(&q->limits,
101629ff57c6SChristoph Hellwig 				bdev->bd_start_sect);
1017ac481c20SMartin K. Petersen 	return q->limits.alignment_offset;
1018ac481c20SMartin K. Petersen }
1019ac481c20SMartin K. Petersen 
1020af2c68feSBart Van Assche static inline int queue_discard_alignment(const struct request_queue *q)
102186b37281SMartin K. Petersen {
102286b37281SMartin K. Petersen 	if (q->limits.discard_misaligned)
102386b37281SMartin K. Petersen 		return -1;
102486b37281SMartin K. Petersen 
102586b37281SMartin K. Petersen 	return q->limits.discard_alignment;
102686b37281SMartin K. Petersen }
102786b37281SMartin K. Petersen 
1028e03a72e1SMartin K. Petersen static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
102986b37281SMartin K. Petersen {
103059771079SLinus Torvalds 	unsigned int alignment, granularity, offset;
1031dd3d145dSMartin K. Petersen 
1032a934a00aSMartin K. Petersen 	if (!lim->max_discard_sectors)
1033a934a00aSMartin K. Petersen 		return 0;
1034a934a00aSMartin K. Petersen 
103559771079SLinus Torvalds 	/* Why are these in bytes, not sectors? */
1036233bde21SBart Van Assche 	alignment = lim->discard_alignment >> SECTOR_SHIFT;
1037233bde21SBart Van Assche 	granularity = lim->discard_granularity >> SECTOR_SHIFT;
103859771079SLinus Torvalds 	if (!granularity)
103959771079SLinus Torvalds 		return 0;
104059771079SLinus Torvalds 
104159771079SLinus Torvalds 	/* Offset of the partition start in 'granularity' sectors */
104259771079SLinus Torvalds 	offset = sector_div(sector, granularity);
104359771079SLinus Torvalds 
104459771079SLinus Torvalds 	/* And why do we do this modulus *again* in blkdev_issue_discard()? */
104559771079SLinus Torvalds 	offset = (granularity + alignment - offset) % granularity;
104659771079SLinus Torvalds 
104759771079SLinus Torvalds 	/* Turn it back into bytes, gaah */
1048233bde21SBart Van Assche 	return offset << SECTOR_SHIFT;
104986b37281SMartin K. Petersen }
105086b37281SMartin K. Petersen 
1051c6e66634SPaolo Bonzini static inline int bdev_discard_alignment(struct block_device *bdev)
1052c6e66634SPaolo Bonzini {
1053c6e66634SPaolo Bonzini 	struct request_queue *q = bdev_get_queue(bdev);
1054c6e66634SPaolo Bonzini 
1055fa01b1e9SChristoph Hellwig 	if (bdev_is_partition(bdev))
10567cf34d97SChristoph Hellwig 		return queue_limit_discard_alignment(&q->limits,
105729ff57c6SChristoph Hellwig 				bdev->bd_start_sect);
1058c6e66634SPaolo Bonzini 	return q->limits.discard_alignment;
1059c6e66634SPaolo Bonzini }
1060c6e66634SPaolo Bonzini 
10614363ac7cSMartin K. Petersen static inline unsigned int bdev_write_same(struct block_device *bdev)
10624363ac7cSMartin K. Petersen {
10634363ac7cSMartin K. Petersen 	struct request_queue *q = bdev_get_queue(bdev);
10644363ac7cSMartin K. Petersen 
10654363ac7cSMartin K. Petersen 	if (q)
10664363ac7cSMartin K. Petersen 		return q->limits.max_write_same_sectors;
10674363ac7cSMartin K. Petersen 
10684363ac7cSMartin K. Petersen 	return 0;
10694363ac7cSMartin K. Petersen }
10704363ac7cSMartin K. Petersen 
1071a6f0788eSChaitanya Kulkarni static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1072a6f0788eSChaitanya Kulkarni {
1073a6f0788eSChaitanya Kulkarni 	struct request_queue *q = bdev_get_queue(bdev);
1074a6f0788eSChaitanya Kulkarni 
1075a6f0788eSChaitanya Kulkarni 	if (q)
1076a6f0788eSChaitanya Kulkarni 		return q->limits.max_write_zeroes_sectors;
1077a6f0788eSChaitanya Kulkarni 
1078a6f0788eSChaitanya Kulkarni 	return 0;
1079a6f0788eSChaitanya Kulkarni }
1080a6f0788eSChaitanya Kulkarni 
1081797476b8SDamien Le Moal static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1082797476b8SDamien Le Moal {
1083797476b8SDamien Le Moal 	struct request_queue *q = bdev_get_queue(bdev);
1084797476b8SDamien Le Moal 
1085797476b8SDamien Le Moal 	if (q)
1086797476b8SDamien Le Moal 		return blk_queue_zoned_model(q);
1087797476b8SDamien Le Moal 
1088797476b8SDamien Le Moal 	return BLK_ZONED_NONE;
1089797476b8SDamien Le Moal }
1090797476b8SDamien Le Moal 
1091797476b8SDamien Le Moal static inline bool bdev_is_zoned(struct block_device *bdev)
1092797476b8SDamien Le Moal {
1093797476b8SDamien Le Moal 	struct request_queue *q = bdev_get_queue(bdev);
1094797476b8SDamien Le Moal 
1095797476b8SDamien Le Moal 	if (q)
1096797476b8SDamien Le Moal 		return blk_queue_is_zoned(q);
1097797476b8SDamien Le Moal 
1098797476b8SDamien Le Moal 	return false;
1099797476b8SDamien Le Moal }
1100797476b8SDamien Le Moal 
1101113ab72eSDamien Le Moal static inline sector_t bdev_zone_sectors(struct block_device *bdev)
11026a0cb1bcSHannes Reinecke {
11036a0cb1bcSHannes Reinecke 	struct request_queue *q = bdev_get_queue(bdev);
11046a0cb1bcSHannes Reinecke 
11056a0cb1bcSHannes Reinecke 	if (q)
1106f99e8648SDamien Le Moal 		return blk_queue_zone_sectors(q);
11076cc77e9cSChristoph Hellwig 	return 0;
11086cc77e9cSChristoph Hellwig }
11096a0cb1bcSHannes Reinecke 
1110e15864f8SNiklas Cassel static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
1111e15864f8SNiklas Cassel {
1112e15864f8SNiklas Cassel 	struct request_queue *q = bdev_get_queue(bdev);
1113e15864f8SNiklas Cassel 
1114e15864f8SNiklas Cassel 	if (q)
1115e15864f8SNiklas Cassel 		return queue_max_open_zones(q);
1116e15864f8SNiklas Cassel 	return 0;
1117e15864f8SNiklas Cassel }
1118e15864f8SNiklas Cassel 
1119659bf827SNiklas Cassel static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
1120659bf827SNiklas Cassel {
1121659bf827SNiklas Cassel 	struct request_queue *q = bdev_get_queue(bdev);
1122659bf827SNiklas Cassel 
1123659bf827SNiklas Cassel 	if (q)
1124659bf827SNiklas Cassel 		return queue_max_active_zones(q);
1125659bf827SNiklas Cassel 	return 0;
1126659bf827SNiklas Cassel }
1127659bf827SNiklas Cassel 
1128af2c68feSBart Van Assche static inline int queue_dma_alignment(const struct request_queue *q)
11291da177e4SLinus Torvalds {
1130482eb689SPete Wyckoff 	return q ? q->dma_alignment : 511;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
113314417799SNamhyung Kim static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
113487904074SFUJITA Tomonori 				 unsigned int len)
113587904074SFUJITA Tomonori {
113687904074SFUJITA Tomonori 	unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
113714417799SNamhyung Kim 	return !(addr & alignment) && !(len & alignment);
113887904074SFUJITA Tomonori }
113987904074SFUJITA Tomonori 
11401da177e4SLinus Torvalds /* assumes size > 256 */
11411da177e4SLinus Torvalds static inline unsigned int blksize_bits(unsigned int size)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	unsigned int bits = 8;
11441da177e4SLinus Torvalds 	do {
11451da177e4SLinus Torvalds 		bits++;
11461da177e4SLinus Torvalds 		size >>= 1;
11471da177e4SLinus Torvalds 	} while (size > 256);
11481da177e4SLinus Torvalds 	return bits;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11512befb9e3SAdrian Bunk static inline unsigned int block_size(struct block_device *bdev)
11521da177e4SLinus Torvalds {
11536b7b181bSChristoph Hellwig 	return 1 << bdev->bd_inode->i_blkbits;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
115659c3d45eSJens Axboe int kblockd_schedule_work(struct work_struct *work);
1157818cd1cbSJens Axboe int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds #define MODULE_ALIAS_BLOCKDEV(major,minor) \
11601da177e4SLinus Torvalds 	MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
11611da177e4SLinus Torvalds #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
11621da177e4SLinus Torvalds 	MODULE_ALIAS("block-major-" __stringify(major) "-*")
11631da177e4SLinus Torvalds 
1164d145dc23SSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1165d145dc23SSatya Tangirala 
1166d145dc23SSatya Tangirala bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
1167d145dc23SSatya Tangirala 
1168d145dc23SSatya Tangirala void blk_ksm_unregister(struct request_queue *q);
1169d145dc23SSatya Tangirala 
1170d145dc23SSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1171d145dc23SSatya Tangirala 
1172d145dc23SSatya Tangirala static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
1173d145dc23SSatya Tangirala 				    struct request_queue *q)
1174d145dc23SSatya Tangirala {
1175d145dc23SSatya Tangirala 	return true;
1176d145dc23SSatya Tangirala }
1177d145dc23SSatya Tangirala 
1178d145dc23SSatya Tangirala static inline void blk_ksm_unregister(struct request_queue *q) { }
1179d145dc23SSatya Tangirala 
1180d145dc23SSatya Tangirala #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1181d145dc23SSatya Tangirala 
1182d145dc23SSatya Tangirala 
118308f85851SAl Viro struct block_device_operations {
11843e08773cSChristoph Hellwig 	void (*submit_bio)(struct bio *bio);
1185d4430d62SAl Viro 	int (*open) (struct block_device *, fmode_t);
1186db2a144bSAl Viro 	void (*release) (struct gendisk *, fmode_t);
11873f289dcbSTejun Heo 	int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1188d4430d62SAl Viro 	int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1189d4430d62SAl Viro 	int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
119077ea887eSTejun Heo 	unsigned int (*check_events) (struct gendisk *disk,
119177ea887eSTejun Heo 				      unsigned int clearing);
1192c3e33e04STejun Heo 	void (*unlock_native_capacity) (struct gendisk *);
119308f85851SAl Viro 	int (*getgeo)(struct block_device *, struct hd_geometry *);
1194e00adcadSChristoph Hellwig 	int (*set_read_only)(struct block_device *bdev, bool ro);
1195b3a27d05SNitin Gupta 	/* this callback is with swap_lock and sometimes page table lock held */
1196b3a27d05SNitin Gupta 	void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1197e76239a3SChristoph Hellwig 	int (*report_zones)(struct gendisk *, sector_t sector,
1198d4100351SChristoph Hellwig 			unsigned int nr_zones, report_zones_cb cb, void *data);
1199348e114bSChristoph Hellwig 	char *(*devnode)(struct gendisk *disk, umode_t *mode);
120008f85851SAl Viro 	struct module *owner;
1201bbd3e064SChristoph Hellwig 	const struct pr_ops *pr_ops;
12020bdfbca8SDmitry Osipenko 
12030bdfbca8SDmitry Osipenko 	/*
12040bdfbca8SDmitry Osipenko 	 * Special callback for probing GPT entry at a given sector.
12050bdfbca8SDmitry Osipenko 	 * Needed by Android devices, used by GPT scanner and MMC blk
12060bdfbca8SDmitry Osipenko 	 * driver.
12070bdfbca8SDmitry Osipenko 	 */
12080bdfbca8SDmitry Osipenko 	int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
120908f85851SAl Viro };
121008f85851SAl Viro 
1211ee6a129dSArnd Bergmann #ifdef CONFIG_COMPAT
1212ee6a129dSArnd Bergmann extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1213ee6a129dSArnd Bergmann 				      unsigned int, unsigned long);
1214ee6a129dSArnd Bergmann #else
1215ee6a129dSArnd Bergmann #define blkdev_compat_ptr_ioctl NULL
1216ee6a129dSArnd Bergmann #endif
1217ee6a129dSArnd Bergmann 
121847a191fdSMatthew Wilcox extern int bdev_read_page(struct block_device *, sector_t, struct page *);
121947a191fdSMatthew Wilcox extern int bdev_write_page(struct block_device *, sector_t, struct page *,
122047a191fdSMatthew Wilcox 						struct writeback_control *);
12216cc77e9cSChristoph Hellwig 
12220619317fSJens Axboe static inline void blk_wake_io_task(struct task_struct *waiter)
12230619317fSJens Axboe {
12240619317fSJens Axboe 	/*
12250619317fSJens Axboe 	 * If we're polling, the task itself is doing the completions. For
12260619317fSJens Axboe 	 * that case, we don't need to signal a wakeup, it's enough to just
12270619317fSJens Axboe 	 * mark us as RUNNING.
12280619317fSJens Axboe 	 */
12290619317fSJens Axboe 	if (waiter == current)
12300619317fSJens Axboe 		__set_current_state(TASK_RUNNING);
12310619317fSJens Axboe 	else
12320619317fSJens Axboe 		wake_up_process(waiter);
12330619317fSJens Axboe }
12340619317fSJens Axboe 
1235956d510eSChristoph Hellwig unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1236956d510eSChristoph Hellwig 		unsigned int op);
1237956d510eSChristoph Hellwig void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1238956d510eSChristoph Hellwig 		unsigned long start_time);
1239956d510eSChristoph Hellwig 
124099dfc43eSChristoph Hellwig unsigned long bio_start_io_acct(struct bio *bio);
124199dfc43eSChristoph Hellwig void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
124299dfc43eSChristoph Hellwig 		struct block_device *orig_bdev);
1243956d510eSChristoph Hellwig 
1244956d510eSChristoph Hellwig /**
1245956d510eSChristoph Hellwig  * bio_end_io_acct - end I/O accounting for bio based drivers
1246956d510eSChristoph Hellwig  * @bio:	bio to end account for
1247956d510eSChristoph Hellwig  * @start:	start time returned by bio_start_io_acct()
1248956d510eSChristoph Hellwig  */
1249956d510eSChristoph Hellwig static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1250956d510eSChristoph Hellwig {
125199dfc43eSChristoph Hellwig 	return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1252956d510eSChristoph Hellwig }
1253956d510eSChristoph Hellwig 
12543f1266f1SChristoph Hellwig int bdev_read_only(struct block_device *bdev);
12553f1266f1SChristoph Hellwig int set_blocksize(struct block_device *bdev, int size);
12563f1266f1SChristoph Hellwig 
12573f1266f1SChristoph Hellwig const char *bdevname(struct block_device *bdev, char *buffer);
12584e7b5671SChristoph Hellwig int lookup_bdev(const char *pathname, dev_t *dev);
12593f1266f1SChristoph Hellwig 
12603f1266f1SChristoph Hellwig void blkdev_show(struct seq_file *seqf, off_t offset);
12613f1266f1SChristoph Hellwig 
12623f1266f1SChristoph Hellwig #define BDEVNAME_SIZE	32	/* Largest string for a blockdev identifier */
12633f1266f1SChristoph Hellwig #define BDEVT_SIZE	10	/* Largest string for MAJ:MIN for blkdev */
12643f1266f1SChristoph Hellwig #ifdef CONFIG_BLOCK
12653f1266f1SChristoph Hellwig #define BLKDEV_MAJOR_MAX	512
12663f1266f1SChristoph Hellwig #else
12673f1266f1SChristoph Hellwig #define BLKDEV_MAJOR_MAX	0
12681da177e4SLinus Torvalds #endif
12693f1266f1SChristoph Hellwig 
12703f1266f1SChristoph Hellwig struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
12713f1266f1SChristoph Hellwig 		void *holder);
12723f1266f1SChristoph Hellwig struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
127337c3fc9aSChristoph Hellwig int bd_prepare_to_claim(struct block_device *bdev, void *holder);
127437c3fc9aSChristoph Hellwig void bd_abort_claiming(struct block_device *bdev, void *holder);
12753f1266f1SChristoph Hellwig void blkdev_put(struct block_device *bdev, fmode_t mode);
12763f1266f1SChristoph Hellwig 
127722ae8ce8SChristoph Hellwig /* just for blk-cgroup, don't use elsewhere */
127822ae8ce8SChristoph Hellwig struct block_device *blkdev_get_no_open(dev_t dev);
127922ae8ce8SChristoph Hellwig void blkdev_put_no_open(struct block_device *bdev);
128022ae8ce8SChristoph Hellwig 
128122ae8ce8SChristoph Hellwig struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
128222ae8ce8SChristoph Hellwig void bdev_add(struct block_device *bdev, dev_t dev);
1283621c1f42SChristoph Hellwig struct block_device *I_BDEV(struct inode *inode);
12842c2b9fd6SChristoph Hellwig int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
12852c2b9fd6SChristoph Hellwig 		loff_t lend);
12863f1266f1SChristoph Hellwig 
12873f1266f1SChristoph Hellwig #ifdef CONFIG_BLOCK
12883f1266f1SChristoph Hellwig void invalidate_bdev(struct block_device *bdev);
12893f1266f1SChristoph Hellwig int sync_blockdev(struct block_device *bdev);
12903f1266f1SChristoph Hellwig #else
12913f1266f1SChristoph Hellwig static inline void invalidate_bdev(struct block_device *bdev)
12923f1266f1SChristoph Hellwig {
12933f1266f1SChristoph Hellwig }
12943f1266f1SChristoph Hellwig static inline int sync_blockdev(struct block_device *bdev)
12953f1266f1SChristoph Hellwig {
12963f1266f1SChristoph Hellwig 	return 0;
12973f1266f1SChristoph Hellwig }
12983f1266f1SChristoph Hellwig #endif
12993f1266f1SChristoph Hellwig int fsync_bdev(struct block_device *bdev);
13003f1266f1SChristoph Hellwig 
1301040f04bdSChristoph Hellwig int freeze_bdev(struct block_device *bdev);
1302040f04bdSChristoph Hellwig int thaw_bdev(struct block_device *bdev);
13033f1266f1SChristoph Hellwig 
13045a72e899SJens Axboe struct io_comp_batch {
13055a72e899SJens Axboe 	struct request *req_list;
13065a72e899SJens Axboe 	bool need_ts;
13075a72e899SJens Axboe 	void (*complete)(struct io_comp_batch *);
13085a72e899SJens Axboe };
13095a72e899SJens Axboe 
13105a72e899SJens Axboe #define DEFINE_IO_COMP_BATCH(name)	struct io_comp_batch name = { }
13115a72e899SJens Axboe 
1312013a7f95SJens Axboe #define rq_list_add(listptr, rq)	do {		\
1313013a7f95SJens Axboe 	(rq)->rq_next = *(listptr);			\
1314013a7f95SJens Axboe 	*(listptr) = rq;				\
1315013a7f95SJens Axboe } while (0)
1316013a7f95SJens Axboe 
1317013a7f95SJens Axboe #define rq_list_pop(listptr)				\
1318013a7f95SJens Axboe ({							\
1319013a7f95SJens Axboe 	struct request *__req = NULL;			\
1320013a7f95SJens Axboe 	if ((listptr) && *(listptr))	{		\
1321013a7f95SJens Axboe 		__req = *(listptr);			\
1322013a7f95SJens Axboe 		*(listptr) = __req->rq_next;		\
1323013a7f95SJens Axboe 	}						\
1324013a7f95SJens Axboe 	__req;						\
1325013a7f95SJens Axboe })
1326013a7f95SJens Axboe 
1327013a7f95SJens Axboe #define rq_list_peek(listptr)				\
1328013a7f95SJens Axboe ({							\
1329013a7f95SJens Axboe 	struct request *__req = NULL;			\
1330013a7f95SJens Axboe 	if ((listptr) && *(listptr))			\
1331013a7f95SJens Axboe 		__req = *(listptr);			\
1332013a7f95SJens Axboe 	__req;						\
1333013a7f95SJens Axboe })
1334013a7f95SJens Axboe 
1335013a7f95SJens Axboe #define rq_list_for_each(listptr, pos)			\
1336013a7f95SJens Axboe 	for (pos = rq_list_peek((listptr)); pos; pos = rq_list_next(pos)) \
1337013a7f95SJens Axboe 
1338013a7f95SJens Axboe #define rq_list_next(rq)	(rq)->rq_next
1339013a7f95SJens Axboe #define rq_list_empty(list)	((list) == (struct request *) NULL)
1340013a7f95SJens Axboe 
13413f1266f1SChristoph Hellwig #endif /* _LINUX_BLKDEV_H */
1342