xref: /linux-6.15/include/linux/blkdev.h (revision 186ddac2)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2322cbb50SChristoph Hellwig /*
3322cbb50SChristoph Hellwig  * Portions Copyright (C) 1992 Drew Eckhardt
4322cbb50SChristoph Hellwig  */
51da177e4SLinus Torvalds #ifndef _LINUX_BLKDEV_H
61da177e4SLinus Torvalds #define _LINUX_BLKDEV_H
71da177e4SLinus Torvalds 
8322cbb50SChristoph Hellwig #include <linux/types.h>
9322cbb50SChristoph Hellwig #include <linux/blk_types.h>
10322cbb50SChristoph Hellwig #include <linux/device.h>
111da177e4SLinus Torvalds #include <linux/list.h>
12320ae51fSJens Axboe #include <linux/llist.h>
13b296a6d5SAndy Shevchenko #include <linux/minmax.h>
141da177e4SLinus Torvalds #include <linux/timer.h>
151da177e4SLinus Torvalds #include <linux/workqueue.h>
161da177e4SLinus Torvalds #include <linux/wait.h>
171da177e4SLinus Torvalds #include <linux/bio.h>
183e6053d7SHugh Dickins #include <linux/gfp.h>
19322cbb50SChristoph Hellwig #include <linux/kdev_t.h>
20548bc8e1STejun Heo #include <linux/rcupdate.h>
21add703fdSTejun Heo #include <linux/percpu-refcount.h>
226a0cb1bcSHannes Reinecke #include <linux/blkzoned.h>
23322cbb50SChristoph Hellwig #include <linux/sched.h>
24d97e594cSJohn Garry #include <linux/sbitmap.h>
25322cbb50SChristoph Hellwig #include <linux/uuid.h>
26322cbb50SChristoph Hellwig #include <linux/xarray.h>
27f3a60882SChristian Brauner #include <linux/file.h>
281da177e4SLinus Torvalds 
29de477254SPaul Gortmaker struct module;
301da177e4SLinus Torvalds struct request_queue;
311da177e4SLinus Torvalds struct elevator_queue;
322056a782SJens Axboe struct blk_trace;
333d6392cfSJens Axboe struct request;
343d6392cfSJens Axboe struct sg_io_hdr;
353c798398STejun Heo struct blkcg_gq;
367c94e1c1SMing Lei struct blk_flush_queue;
373e08773cSChristoph Hellwig struct kiocb;
38bbd3e064SChristoph Hellwig struct pr_ops;
39a7905043SJosef Bacik struct rq_qos;
4034dbad5dSOmar Sandoval struct blk_queue_stats;
4134dbad5dSOmar Sandoval struct blk_stat_callback;
42cb77cb5aSEric Biggers struct blk_crypto_profile;
431da177e4SLinus Torvalds 
44322cbb50SChristoph Hellwig extern const struct device_type disk_type;
45cdb37f73SThomas Weißschuh extern const struct device_type part_type;
46f8c7511dSRicardo B. Marliere extern const struct class block_class;
47322cbb50SChristoph Hellwig 
488bd435b3STejun Heo /*
498bd435b3STejun Heo  * Maximum number of blkcg policies allowed to be registered concurrently.
508bd435b3STejun Heo  * Defined here to simplify include dependency.
518bd435b3STejun Heo  */
52ec645dc9SOleksandr Natalenko #define BLKCG_MAX_POLS		6
538bd435b3STejun Heo 
54322cbb50SChristoph Hellwig #define DISK_MAX_PARTS			256
55322cbb50SChristoph Hellwig #define DISK_NAME_LEN			32
56322cbb50SChristoph Hellwig 
57322cbb50SChristoph Hellwig #define PARTITION_META_INFO_VOLNAMELTH	64
58322cbb50SChristoph Hellwig /*
59322cbb50SChristoph Hellwig  * Enough for the string representation of any kind of UUID plus NULL.
60322cbb50SChristoph Hellwig  * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
61322cbb50SChristoph Hellwig  */
62322cbb50SChristoph Hellwig #define PARTITION_META_INFO_UUIDLTH	(UUID_STRING_LEN + 1)
63322cbb50SChristoph Hellwig 
64322cbb50SChristoph Hellwig struct partition_meta_info {
65322cbb50SChristoph Hellwig 	char uuid[PARTITION_META_INFO_UUIDLTH];
66322cbb50SChristoph Hellwig 	u8 volname[PARTITION_META_INFO_VOLNAMELTH];
67322cbb50SChristoph Hellwig };
68322cbb50SChristoph Hellwig 
69322cbb50SChristoph Hellwig /**
70322cbb50SChristoph Hellwig  * DOC: genhd capability flags
71322cbb50SChristoph Hellwig  *
72322cbb50SChristoph Hellwig  * ``GENHD_FL_REMOVABLE``: indicates that the block device gives access to
73322cbb50SChristoph Hellwig  * removable media.  When set, the device remains present even when media is not
74322cbb50SChristoph Hellwig  * inserted.  Shall not be set for devices which are removed entirely when the
75322cbb50SChristoph Hellwig  * media is removed.
76322cbb50SChristoph Hellwig  *
77322cbb50SChristoph Hellwig  * ``GENHD_FL_HIDDEN``: the block device is hidden; it doesn't produce events,
78322cbb50SChristoph Hellwig  * doesn't appear in sysfs, and can't be opened from userspace or using
79322cbb50SChristoph Hellwig  * blkdev_get*. Used for the underlying components of multipath devices.
80322cbb50SChristoph Hellwig  *
81322cbb50SChristoph Hellwig  * ``GENHD_FL_NO_PART``: partition support is disabled.  The kernel will not
82322cbb50SChristoph Hellwig  * scan for partitions from add_disk, and users can't add partitions manually.
83322cbb50SChristoph Hellwig  *
84322cbb50SChristoph Hellwig  */
85322cbb50SChristoph Hellwig enum {
86322cbb50SChristoph Hellwig 	GENHD_FL_REMOVABLE			= 1 << 0,
87322cbb50SChristoph Hellwig 	GENHD_FL_HIDDEN				= 1 << 1,
88322cbb50SChristoph Hellwig 	GENHD_FL_NO_PART			= 1 << 2,
89322cbb50SChristoph Hellwig };
90322cbb50SChristoph Hellwig 
91322cbb50SChristoph Hellwig enum {
92322cbb50SChristoph Hellwig 	DISK_EVENT_MEDIA_CHANGE			= 1 << 0, /* media changed */
93322cbb50SChristoph Hellwig 	DISK_EVENT_EJECT_REQUEST		= 1 << 1, /* eject requested */
94322cbb50SChristoph Hellwig };
95322cbb50SChristoph Hellwig 
96322cbb50SChristoph Hellwig enum {
97322cbb50SChristoph Hellwig 	/* Poll even if events_poll_msecs is unset */
98322cbb50SChristoph Hellwig 	DISK_EVENT_FLAG_POLL			= 1 << 0,
99322cbb50SChristoph Hellwig 	/* Forward events to udev */
100322cbb50SChristoph Hellwig 	DISK_EVENT_FLAG_UEVENT			= 1 << 1,
101322cbb50SChristoph Hellwig 	/* Block event polling when open for exclusive write */
102322cbb50SChristoph Hellwig 	DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE	= 1 << 2,
103322cbb50SChristoph Hellwig };
104322cbb50SChristoph Hellwig 
105322cbb50SChristoph Hellwig struct disk_events;
106322cbb50SChristoph Hellwig struct badblocks;
107322cbb50SChristoph Hellwig 
108322cbb50SChristoph Hellwig struct blk_integrity {
109322cbb50SChristoph Hellwig 	const struct blk_integrity_profile	*profile;
110322cbb50SChristoph Hellwig 	unsigned char				flags;
111322cbb50SChristoph Hellwig 	unsigned char				tuple_size;
11260d21aacSKanchan Joshi 	unsigned char				pi_offset;
113322cbb50SChristoph Hellwig 	unsigned char				interval_exp;
114322cbb50SChristoph Hellwig 	unsigned char				tag_size;
115322cbb50SChristoph Hellwig };
116322cbb50SChristoph Hellwig 
11705bdb996SChristoph Hellwig typedef unsigned int __bitwise blk_mode_t;
11805bdb996SChristoph Hellwig 
11905bdb996SChristoph Hellwig /* open for reading */
12005bdb996SChristoph Hellwig #define BLK_OPEN_READ		((__force blk_mode_t)(1 << 0))
12105bdb996SChristoph Hellwig /* open for writing */
12205bdb996SChristoph Hellwig #define BLK_OPEN_WRITE		((__force blk_mode_t)(1 << 1))
12305bdb996SChristoph Hellwig /* open exclusively (vs other exclusive openers */
12405bdb996SChristoph Hellwig #define BLK_OPEN_EXCL		((__force blk_mode_t)(1 << 2))
12505bdb996SChristoph Hellwig /* opened with O_NDELAY */
12605bdb996SChristoph Hellwig #define BLK_OPEN_NDELAY		((__force blk_mode_t)(1 << 3))
12705bdb996SChristoph Hellwig /* open for "writes" only for ioctls (specialy hack for floppy.c) */
12805bdb996SChristoph Hellwig #define BLK_OPEN_WRITE_IOCTL	((__force blk_mode_t)(1 << 4))
129ed5cc702SJan Kara /* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */
130ed5cc702SJan Kara #define BLK_OPEN_RESTRICT_WRITES	((__force blk_mode_t)(1 << 5))
13105bdb996SChristoph Hellwig 
132322cbb50SChristoph Hellwig struct gendisk {
133322cbb50SChristoph Hellwig 	/*
134322cbb50SChristoph Hellwig 	 * major/first_minor/minors should not be set by any new driver, the
135322cbb50SChristoph Hellwig 	 * block core will take care of allocating them automatically.
136322cbb50SChristoph Hellwig 	 */
137322cbb50SChristoph Hellwig 	int major;
138322cbb50SChristoph Hellwig 	int first_minor;
139322cbb50SChristoph Hellwig 	int minors;
140322cbb50SChristoph Hellwig 
141322cbb50SChristoph Hellwig 	char disk_name[DISK_NAME_LEN];	/* name of major driver */
142322cbb50SChristoph Hellwig 
143322cbb50SChristoph Hellwig 	unsigned short events;		/* supported events */
144322cbb50SChristoph Hellwig 	unsigned short event_flags;	/* flags related to event processing */
145322cbb50SChristoph Hellwig 
146322cbb50SChristoph Hellwig 	struct xarray part_tbl;
147322cbb50SChristoph Hellwig 	struct block_device *part0;
148322cbb50SChristoph Hellwig 
149322cbb50SChristoph Hellwig 	const struct block_device_operations *fops;
150322cbb50SChristoph Hellwig 	struct request_queue *queue;
151322cbb50SChristoph Hellwig 	void *private_data;
152322cbb50SChristoph Hellwig 
15346754bd0SChristoph Hellwig 	struct bio_set bio_split;
15446754bd0SChristoph Hellwig 
155322cbb50SChristoph Hellwig 	int flags;
156322cbb50SChristoph Hellwig 	unsigned long state;
157322cbb50SChristoph Hellwig #define GD_NEED_PART_SCAN		0
158322cbb50SChristoph Hellwig #define GD_READ_ONLY			1
159322cbb50SChristoph Hellwig #define GD_DEAD				2
160322cbb50SChristoph Hellwig #define GD_NATIVE_CAPACITY		3
16176792055SChristoph Hellwig #define GD_ADDED			4
162b9684a71SChristoph Hellwig #define GD_SUPPRESS_PART_SCAN		5
1636f8191fdSChristoph Hellwig #define GD_OWNS_QUEUE			6
164322cbb50SChristoph Hellwig 
165322cbb50SChristoph Hellwig 	struct mutex open_mutex;	/* open/close mutex */
166322cbb50SChristoph Hellwig 	unsigned open_partitions;	/* number of open partitions */
167322cbb50SChristoph Hellwig 
168322cbb50SChristoph Hellwig 	struct backing_dev_info	*bdi;
1692bd85221SChristoph Hellwig 	struct kobject queue_kobj;	/* the queue/ directory */
170322cbb50SChristoph Hellwig 	struct kobject *slave_dir;
171322cbb50SChristoph Hellwig #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
172322cbb50SChristoph Hellwig 	struct list_head slave_bdevs;
173322cbb50SChristoph Hellwig #endif
174322cbb50SChristoph Hellwig 	struct timer_rand_state *random;
175322cbb50SChristoph Hellwig 	atomic_t sync_io;		/* RAID */
176322cbb50SChristoph Hellwig 	struct disk_events *ev;
177d86e716aSChristoph Hellwig 
178d86e716aSChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
179d86e716aSChristoph Hellwig 	/*
180d86e716aSChristoph Hellwig 	 * Zoned block device information for request dispatch control.
181d86e716aSChristoph Hellwig 	 * nr_zones is the total number of zones of the device. This is always
182d86e716aSChristoph Hellwig 	 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
183d86e716aSChristoph Hellwig 	 * bits which indicates if a zone is conventional (bit set) or
184d86e716aSChristoph Hellwig 	 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
185d86e716aSChristoph Hellwig 	 * bits which indicates if a zone is write locked, that is, if a write
186d86e716aSChristoph Hellwig 	 * request targeting the zone was dispatched.
187d86e716aSChristoph Hellwig 	 *
188d86e716aSChristoph Hellwig 	 * Reads of this information must be protected with blk_queue_enter() /
189d86e716aSChristoph Hellwig 	 * blk_queue_exit(). Modifying this information is only allowed while
190d86e716aSChristoph Hellwig 	 * no requests are being processed. See also blk_mq_freeze_queue() and
191d86e716aSChristoph Hellwig 	 * blk_mq_unfreeze_queue().
192d86e716aSChristoph Hellwig 	 */
193d86e716aSChristoph Hellwig 	unsigned int		nr_zones;
194d86e716aSChristoph Hellwig 	unsigned long		*conv_zones_bitmap;
195d86e716aSChristoph Hellwig 	unsigned long		*seq_zones_wlock;
196d86e716aSChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */
197d86e716aSChristoph Hellwig 
198322cbb50SChristoph Hellwig #if IS_ENABLED(CONFIG_CDROM)
199322cbb50SChristoph Hellwig 	struct cdrom_device_info *cdi;
200322cbb50SChristoph Hellwig #endif
201322cbb50SChristoph Hellwig 	int node_id;
202322cbb50SChristoph Hellwig 	struct badblocks *bb;
203322cbb50SChristoph Hellwig 	struct lockdep_map lockdep_map;
204322cbb50SChristoph Hellwig 	u64 diskseq;
20505bdb996SChristoph Hellwig 	blk_mode_t open_mode;
2066a27d28cSChristoph Hellwig 
2076a27d28cSChristoph Hellwig 	/*
2086a27d28cSChristoph Hellwig 	 * Independent sector access ranges. This is always NULL for
2096a27d28cSChristoph Hellwig 	 * devices that do not have multiple independent access ranges.
2106a27d28cSChristoph Hellwig 	 */
2116a27d28cSChristoph Hellwig 	struct blk_independent_access_ranges *ia_ranges;
212322cbb50SChristoph Hellwig };
213322cbb50SChristoph Hellwig 
214dbdc1be3SChristoph Hellwig /**
215dbdc1be3SChristoph Hellwig  * disk_openers - returns how many openers are there for a disk
216dbdc1be3SChristoph Hellwig  * @disk: disk to check
217dbdc1be3SChristoph Hellwig  *
218dbdc1be3SChristoph Hellwig  * This returns the number of openers for a disk.  Note that this value is only
219dbdc1be3SChristoph Hellwig  * stable if disk->open_mutex is held.
220dbdc1be3SChristoph Hellwig  *
221dbdc1be3SChristoph Hellwig  * Note: Due to a quirk in the block layer open code, each open partition is
222dbdc1be3SChristoph Hellwig  * only counted once even if there are multiple openers.
223dbdc1be3SChristoph Hellwig  */
224dbdc1be3SChristoph Hellwig static inline unsigned int disk_openers(struct gendisk *disk)
225dbdc1be3SChristoph Hellwig {
2269acf381fSChristoph Hellwig 	return atomic_read(&disk->part0->bd_openers);
227dbdc1be3SChristoph Hellwig }
228dbdc1be3SChristoph Hellwig 
229322cbb50SChristoph Hellwig /*
230322cbb50SChristoph Hellwig  * The gendisk is refcounted by the part0 block_device, and the bd_device
231322cbb50SChristoph Hellwig  * therein is also used for device model presentation in sysfs.
232322cbb50SChristoph Hellwig  */
233322cbb50SChristoph Hellwig #define dev_to_disk(device) \
234322cbb50SChristoph Hellwig 	(dev_to_bdev(device)->bd_disk)
235322cbb50SChristoph Hellwig #define disk_to_dev(disk) \
236322cbb50SChristoph Hellwig 	(&((disk)->part0->bd_device))
237322cbb50SChristoph Hellwig 
238322cbb50SChristoph Hellwig #if IS_REACHABLE(CONFIG_CDROM)
239322cbb50SChristoph Hellwig #define disk_to_cdi(disk)	((disk)->cdi)
240322cbb50SChristoph Hellwig #else
241322cbb50SChristoph Hellwig #define disk_to_cdi(disk)	NULL
242322cbb50SChristoph Hellwig #endif
243322cbb50SChristoph Hellwig 
244322cbb50SChristoph Hellwig static inline dev_t disk_devt(struct gendisk *disk)
245322cbb50SChristoph Hellwig {
246322cbb50SChristoph Hellwig 	return MKDEV(disk->major, disk->first_minor);
247322cbb50SChristoph Hellwig }
248322cbb50SChristoph Hellwig 
24937ae5a0fSTetsuo Handa static inline int blk_validate_block_size(unsigned long bsize)
250570b1cacSXie Yongji {
251570b1cacSXie Yongji 	if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
252570b1cacSXie Yongji 		return -EINVAL;
253570b1cacSXie Yongji 
254570b1cacSXie Yongji 	return 0;
255570b1cacSXie Yongji }
256570b1cacSXie Yongji 
25716458cf3SBart Van Assche static inline bool blk_op_is_passthrough(blk_opf_t op)
25814cb0dc6SMing Lei {
259da6269daSChristoph Hellwig 	op &= REQ_OP_MASK;
26014cb0dc6SMing Lei 	return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
26114cb0dc6SMing Lei }
26214cb0dc6SMing Lei 
263797476b8SDamien Le Moal /*
2649bb33f24SChristoph Hellwig  * BLK_BOUNCE_NONE:	never bounce (default)
2659bb33f24SChristoph Hellwig  * BLK_BOUNCE_HIGH:	bounce all highmem pages
2669bb33f24SChristoph Hellwig  */
2679bb33f24SChristoph Hellwig enum blk_bounce {
2689bb33f24SChristoph Hellwig 	BLK_BOUNCE_NONE,
2699bb33f24SChristoph Hellwig 	BLK_BOUNCE_HIGH,
2709bb33f24SChristoph Hellwig };
2719bb33f24SChristoph Hellwig 
272025146e1SMartin K. Petersen struct queue_limits {
2739bb33f24SChristoph Hellwig 	enum blk_bounce		bounce;
274025146e1SMartin K. Petersen 	unsigned long		seg_boundary_mask;
27503100aadSKeith Busch 	unsigned long		virt_boundary_mask;
276025146e1SMartin K. Petersen 
277025146e1SMartin K. Petersen 	unsigned int		max_hw_sectors;
278ca369d51SMartin K. Petersen 	unsigned int		max_dev_sectors;
279762380adSJens Axboe 	unsigned int		chunk_sectors;
280025146e1SMartin K. Petersen 	unsigned int		max_sectors;
281c9c77418SKeith Busch 	unsigned int		max_user_sectors;
282025146e1SMartin K. Petersen 	unsigned int		max_segment_size;
283c72758f3SMartin K. Petersen 	unsigned int		physical_block_size;
284ad6bf88aSMikulas Patocka 	unsigned int		logical_block_size;
285c72758f3SMartin K. Petersen 	unsigned int		alignment_offset;
286c72758f3SMartin K. Petersen 	unsigned int		io_min;
287c72758f3SMartin K. Petersen 	unsigned int		io_opt;
28867efc925SChristoph Hellwig 	unsigned int		max_discard_sectors;
2890034af03SJens Axboe 	unsigned int		max_hw_discard_sectors;
2904f563a64SChristoph Hellwig 	unsigned int		max_user_discard_sectors;
29144abff2cSChristoph Hellwig 	unsigned int		max_secure_erase_sectors;
292a6f0788eSChaitanya Kulkarni 	unsigned int		max_write_zeroes_sectors;
2930512a75bSKeith Busch 	unsigned int		max_zone_append_sectors;
29486b37281SMartin K. Petersen 	unsigned int		discard_granularity;
29586b37281SMartin K. Petersen 	unsigned int		discard_alignment;
296a805a4faSDamien Le Moal 	unsigned int		zone_write_granularity;
297025146e1SMartin K. Petersen 
2988a78362cSMartin K. Petersen 	unsigned short		max_segments;
29913f05c8dSMartin K. Petersen 	unsigned short		max_integrity_segments;
3001e739730SChristoph Hellwig 	unsigned short		max_discard_segments;
301025146e1SMartin K. Petersen 
302c72758f3SMartin K. Petersen 	unsigned char		misaligned;
30386b37281SMartin K. Petersen 	unsigned char		discard_misaligned;
304c78afc62SKent Overstreet 	unsigned char		raid_partial_stripes_expensive;
3057437bb73SChristoph Hellwig 	bool			zoned;
3068c4955c0SChristoph Hellwig 	unsigned int		max_open_zones;
3078c4955c0SChristoph Hellwig 	unsigned int		max_active_zones;
308c964d62fSKeith Busch 
309c964d62fSKeith Busch 	/*
310c964d62fSKeith Busch 	 * Drivers that set dma_alignment to less than 511 must be prepared to
311c964d62fSKeith Busch 	 * handle individual bvec's that are not a multiple of a SECTOR_SIZE
312c964d62fSKeith Busch 	 * due to possible offsets.
313c964d62fSKeith Busch 	 */
314c964d62fSKeith Busch 	unsigned int		dma_alignment;
315025146e1SMartin K. Petersen };
316025146e1SMartin K. Petersen 
317d4100351SChristoph Hellwig typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
318d4100351SChristoph Hellwig 			       void *data);
319d4100351SChristoph Hellwig 
320d73e93b4SChristoph Hellwig void disk_set_zoned(struct gendisk *disk);
32127ba3e8fSDamien Le Moal 
322d4100351SChristoph Hellwig #define BLK_ALL_ZONES  ((unsigned int)-1)
323d4100351SChristoph Hellwig int blkdev_report_zones(struct block_device *bdev, sector_t sector,
324d4100351SChristoph Hellwig 		unsigned int nr_zones, report_zones_cb cb, void *data);
325668bfeeaSChristoph Hellwig int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
32671f4ecdbSJohannes Thumshirn 		sector_t sectors, sector_t nr_sectors);
327e732671aSDamien Le Moal int blk_revalidate_disk_zones(struct gendisk *disk,
328e732671aSDamien Le Moal 		void (*update_driver_data)(struct gendisk *disk));
3296a0cb1bcSHannes Reinecke 
330a2247f19SDamien Le Moal /*
331a2247f19SDamien Le Moal  * Independent access ranges: struct blk_independent_access_range describes
332a2247f19SDamien Le Moal  * a range of contiguous sectors that can be accessed using device command
333a2247f19SDamien Le Moal  * execution resources that are independent from the resources used for
334a2247f19SDamien Le Moal  * other access ranges. This is typically found with single-LUN multi-actuator
335a2247f19SDamien Le Moal  * HDDs where each access range is served by a different set of heads.
336a2247f19SDamien Le Moal  * The set of independent ranges supported by the device is defined using
337a2247f19SDamien Le Moal  * struct blk_independent_access_ranges. The independent ranges must not overlap
338a2247f19SDamien Le Moal  * and must include all sectors within the disk capacity (no sector holes
339a2247f19SDamien Le Moal  * allowed).
340a2247f19SDamien Le Moal  * For a device with multiple ranges, requests targeting sectors in different
341a2247f19SDamien Le Moal  * ranges can be executed in parallel. A request can straddle an access range
342a2247f19SDamien Le Moal  * boundary.
343a2247f19SDamien Le Moal  */
344a2247f19SDamien Le Moal struct blk_independent_access_range {
345a2247f19SDamien Le Moal 	struct kobject		kobj;
346a2247f19SDamien Le Moal 	sector_t		sector;
347a2247f19SDamien Le Moal 	sector_t		nr_sectors;
348a2247f19SDamien Le Moal };
349a2247f19SDamien Le Moal 
350a2247f19SDamien Le Moal struct blk_independent_access_ranges {
351a2247f19SDamien Le Moal 	struct kobject				kobj;
352a2247f19SDamien Le Moal 	bool					sysfs_registered;
353a2247f19SDamien Le Moal 	unsigned int				nr_ia_ranges;
354a2247f19SDamien Le Moal 	struct blk_independent_access_range	ia_range[];
355a2247f19SDamien Le Moal };
356a2247f19SDamien Le Moal 
357d7b76301SRichard Kennedy struct request_queue {
3581da177e4SLinus Torvalds 	/*
3591da177e4SLinus Torvalds 	 * The queue owner gets to use this for whatever they like.
3601da177e4SLinus Torvalds 	 * ll_rw_blk doesn't touch it.
3611da177e4SLinus Torvalds 	 */
3621da177e4SLinus Torvalds 	void			*queuedata;
3631da177e4SLinus Torvalds 
3640c734c5eSJens Axboe 	struct elevator_queue	*elevator;
3650c734c5eSJens Axboe 
3660c734c5eSJens Axboe 	const struct blk_mq_ops	*mq_ops;
3670c734c5eSJens Axboe 
3680c734c5eSJens Axboe 	/* sw queues */
3690c734c5eSJens Axboe 	struct blk_mq_ctx __percpu	*queue_ctx;
3700c734c5eSJens Axboe 
3711da177e4SLinus Torvalds 	/*
3721da177e4SLinus Torvalds 	 * various queue flags, see QUEUE_* below
3731da177e4SLinus Torvalds 	 */
3741da177e4SLinus Torvalds 	unsigned long		queue_flags;
3751da177e4SLinus Torvalds 
3760c734c5eSJens Axboe 	unsigned int		rq_timeout;
3770c734c5eSJens Axboe 
3780c734c5eSJens Axboe 	unsigned int		queue_depth;
3790c734c5eSJens Axboe 
3800c734c5eSJens Axboe 	refcount_t		refs;
3810c734c5eSJens Axboe 
3820c734c5eSJens Axboe 	/* hw dispatch queues */
3830c734c5eSJens Axboe 	unsigned int		nr_hw_queues;
3840c734c5eSJens Axboe 	struct xarray		hctx_table;
3850c734c5eSJens Axboe 
3860c734c5eSJens Axboe 	struct percpu_ref	q_usage_counter;
3870c734c5eSJens Axboe 
3880c734c5eSJens Axboe 	struct request		*last_merge;
389a73f730dSTejun Heo 
3900d945c1fSChristoph Hellwig 	spinlock_t		queue_lock;
3911da177e4SLinus Torvalds 
3920c734c5eSJens Axboe 	int			quiesce_depth;
393d152c682SChristoph Hellwig 
3940c734c5eSJens Axboe 	struct gendisk		*disk;
3951da177e4SLinus Torvalds 
396320ae51fSJens Axboe 	/*
397320ae51fSJens Axboe 	 * mq queue kobject
398320ae51fSJens Axboe 	 */
3991db4909eSMing Lei 	struct kobject *mq_kobj;
400320ae51fSJens Axboe 
4010c734c5eSJens Axboe 	struct queue_limits	limits;
4020c734c5eSJens Axboe 
403ac6fc48cSDan Williams #ifdef  CONFIG_BLK_DEV_INTEGRITY
404ac6fc48cSDan Williams 	struct blk_integrity integrity;
405ac6fc48cSDan Williams #endif	/* CONFIG_BLK_DEV_INTEGRITY */
406ac6fc48cSDan Williams 
40747fafbc7SRafael J. Wysocki #ifdef CONFIG_PM
4086c954667SLin Ming 	struct device		*dev;
409db04e18dSGeert Uytterhoeven 	enum rpm_status		rpm_status;
4106c954667SLin Ming #endif
4116c954667SLin Ming 
4121da177e4SLinus Torvalds 	/*
4130c734c5eSJens Axboe 	 * Number of contexts that have called blk_set_pm_only(). If this
4140c734c5eSJens Axboe 	 * counter is above zero then only RQF_PM requests are processed.
4150c734c5eSJens Axboe 	 */
4160c734c5eSJens Axboe 	atomic_t		pm_only;
4170c734c5eSJens Axboe 
4180c734c5eSJens Axboe 	struct blk_queue_stats	*stats;
4190c734c5eSJens Axboe 	struct rq_qos		*rq_qos;
4200c734c5eSJens Axboe 	struct mutex		rq_qos_mutex;
4210c734c5eSJens Axboe 
4220c734c5eSJens Axboe 	/*
4230c734c5eSJens Axboe 	 * ida allocated id for this queue.  Used to index queues from
4240c734c5eSJens Axboe 	 * ioctx.
4250c734c5eSJens Axboe 	 */
4260c734c5eSJens Axboe 	int			id;
4270c734c5eSJens Axboe 
4280c734c5eSJens Axboe 	unsigned int		dma_pad_mask;
4290c734c5eSJens Axboe 
4300c734c5eSJens Axboe 	/*
4311da177e4SLinus Torvalds 	 * queue settings
4321da177e4SLinus Torvalds 	 */
4331da177e4SLinus Torvalds 	unsigned long		nr_requests;	/* Max # of requests */
4341da177e4SLinus Torvalds 
4351b262839SSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
436cb77cb5aSEric Biggers 	struct blk_crypto_profile *crypto_profile;
43720f01f16SEric Biggers 	struct kobject *crypto_kobject;
4381b262839SSatya Tangirala #endif
4391b262839SSatya Tangirala 
440242f9dcbSJens Axboe 	struct timer_list	timeout;
441287922ebSChristoph Hellwig 	struct work_struct	timeout_work;
442242f9dcbSJens Axboe 
443079a2e3eSJohn Garry 	atomic_t		nr_active_requests_shared_tags;
444bccf5e26SJohn Garry 
4450c734c5eSJens Axboe 	unsigned int		required_elevator_features;
4460c734c5eSJens Axboe 
447079a2e3eSJohn Garry 	struct blk_mq_tags	*sched_shared_tags;
448d97e594cSJohn Garry 
449a612fddfSTejun Heo 	struct list_head	icq_list;
4501231039dSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP
4511231039dSChristoph Hellwig 	DECLARE_BITMAP		(blkcg_pols, BLKCG_MAX_POLS);
4521231039dSChristoph Hellwig 	struct blkcg_gq		*root_blkg;
4531231039dSChristoph Hellwig 	struct list_head	blkg_list;
4541231039dSChristoph Hellwig 	struct mutex		blkcg_mutex;
4551231039dSChristoph Hellwig #endif
456a612fddfSTejun Heo 
4571946089aSChristoph Lameter 	int			node;
4580c734c5eSJens Axboe 
4590c734c5eSJens Axboe 	spinlock_t		requeue_lock;
4600c734c5eSJens Axboe 	struct list_head	requeue_list;
4610c734c5eSJens Axboe 	struct delayed_work	requeue_work;
4620c734c5eSJens Axboe 
4636c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
464c780e86dSJan Kara 	struct blk_trace __rcu	*blk_trace;
4656c5c9341SAlexey Dobriyan #endif
4661da177e4SLinus Torvalds 	/*
4674913efe4STejun Heo 	 * for flush operations
4681da177e4SLinus Torvalds 	 */
4697c94e1c1SMing Lei 	struct blk_flush_queue	*fq;
4709a67aa52SChristoph Hellwig 	struct list_head	flush_list;
471483f4afcSAl Viro 
472483f4afcSAl Viro 	struct mutex		sysfs_lock;
473cecf5d87SMing Lei 	struct mutex		sysfs_dir_lock;
474d690cb8aSChristoph Hellwig 	struct mutex		limits_lock;
475d351af01SFUJITA Tomonori 
4762f8f1336SMing Lei 	/*
4772f8f1336SMing Lei 	 * for reusing dead hctx instance in case of updating
4782f8f1336SMing Lei 	 * nr_hw_queues
4792f8f1336SMing Lei 	 */
4802f8f1336SMing Lei 	struct list_head	unused_hctx_list;
4812f8f1336SMing Lei 	spinlock_t		unused_hctx_lock;
4822f8f1336SMing Lei 
4837996a8b5SBob Liu 	int			mq_freeze_depth;
484d732580bSTejun Heo 
485e43473b7SVivek Goyal #ifdef CONFIG_BLK_DEV_THROTTLING
486e43473b7SVivek Goyal 	/* Throttle data */
487e43473b7SVivek Goyal 	struct throtl_data *td;
488e43473b7SVivek Goyal #endif
489548bc8e1STejun Heo 	struct rcu_head		rcu_head;
490320ae51fSJens Axboe 	wait_queue_head_t	mq_freeze_wq;
4917996a8b5SBob Liu 	/*
4927996a8b5SBob Liu 	 * Protect concurrent access to q_usage_counter by
4937996a8b5SBob Liu 	 * percpu_ref_kill() and percpu_ref_reinit().
4947996a8b5SBob Liu 	 */
4957996a8b5SBob Liu 	struct mutex		mq_freeze_lock;
4960d2602caSJens Axboe 
4970d2602caSJens Axboe 	struct blk_mq_tag_set	*tag_set;
4980d2602caSJens Axboe 	struct list_head	tag_set_list;
4994593fdbeSAkinobu Mita 
50007e4feadSOmar Sandoval 	struct dentry		*debugfs_dir;
501d332ce09SOmar Sandoval 	struct dentry		*sched_debugfs_dir;
502cc56694fSMing Lei 	struct dentry		*rqos_debugfs_dir;
5035cf9c91bSChristoph Hellwig 	/*
5045cf9c91bSChristoph Hellwig 	 * Serializes all debugfs metadata operations using the above dentries.
5055cf9c91bSChristoph Hellwig 	 */
5065cf9c91bSChristoph Hellwig 	struct mutex		debugfs_mutex;
50707e4feadSOmar Sandoval 
5084593fdbeSAkinobu Mita 	bool			mq_sysfs_init_done;
5091da177e4SLinus Torvalds };
5101da177e4SLinus Torvalds 
511bfe373f6SHou Tao /* Keep blk_queue_flag_name[] in sync with the definitions below */
512eca7abf3SJens Axboe #define QUEUE_FLAG_STOPPED	0	/* queue is stopped */
513eca7abf3SJens Axboe #define QUEUE_FLAG_DYING	1	/* queue being torn down */
514eca7abf3SJens Axboe #define QUEUE_FLAG_NOMERGES     3	/* disable merge attempts */
515eca7abf3SJens Axboe #define QUEUE_FLAG_SAME_COMP	4	/* complete on same CPU-group */
516eca7abf3SJens Axboe #define QUEUE_FLAG_FAIL_IO	5	/* fake timeout */
517eca7abf3SJens Axboe #define QUEUE_FLAG_NONROT	6	/* non-rotational device (SSD) */
51888e740f1SFernando Luis Vázquez Cao #define QUEUE_FLAG_VIRT		QUEUE_FLAG_NONROT /* paravirt device */
519eca7abf3SJens Axboe #define QUEUE_FLAG_IO_STAT	7	/* do disk/partitions IO accounting */
520eca7abf3SJens Axboe #define QUEUE_FLAG_NOXMERGES	9	/* No extended merges */
521eca7abf3SJens Axboe #define QUEUE_FLAG_ADD_RANDOM	10	/* Contributes to random pool */
5223222d8c2SChristoph Hellwig #define QUEUE_FLAG_SYNCHRONOUS	11	/* always completes in submit context */
523eca7abf3SJens Axboe #define QUEUE_FLAG_SAME_FORCE	12	/* force complete on same CPU */
52402d374f3SChristoph Hellwig #define QUEUE_FLAG_HW_WC	13	/* Write back caching supported */
525eca7abf3SJens Axboe #define QUEUE_FLAG_INIT_DONE	14	/* queue is initialized */
5261cb039f3SChristoph Hellwig #define QUEUE_FLAG_STABLE_WRITES 15	/* don't modify blks until WB is done */
527eca7abf3SJens Axboe #define QUEUE_FLAG_POLL		16	/* IO polling enabled if set */
528eca7abf3SJens Axboe #define QUEUE_FLAG_WC		17	/* Write back caching */
529eca7abf3SJens Axboe #define QUEUE_FLAG_FUA		18	/* device supports FUA writes */
530eca7abf3SJens Axboe #define QUEUE_FLAG_DAX		19	/* device supports DAX */
531eca7abf3SJens Axboe #define QUEUE_FLAG_STATS	20	/* track IO start and completion times */
532eca7abf3SJens Axboe #define QUEUE_FLAG_REGISTERED	22	/* queue has been registered to a disk */
533eca7abf3SJens Axboe #define QUEUE_FLAG_QUIESCED	24	/* queue has been quiesced */
534eca7abf3SJens Axboe #define QUEUE_FLAG_PCI_P2PDMA	25	/* device supports PCI p2p requests */
535e84e8f06SChaitanya Kulkarni #define QUEUE_FLAG_ZONE_RESETALL 26	/* supports Zone Reset All */
5366f816b4bSTejun Heo #define QUEUE_FLAG_RQ_ALLOC_TIME 27	/* record rq->alloc_time_ns */
537f1b49fdcSJohn Garry #define QUEUE_FLAG_HCTX_ACTIVE	28	/* at least one blk-mq hctx is active */
538021a2446SMike Snitzer #define QUEUE_FLAG_NOWAIT       29	/* device supports NOWAIT */
5394d337cebSMing Lei #define QUEUE_FLAG_SQ_SCHED     30	/* single queue style io dispatch */
540414dd48eSChao Leng #define QUEUE_FLAG_SKIP_TAGSET_QUIESCE	31 /* quiesce_tagset skip the queue*/
541797e7dbbSTejun Heo 
542ca5eebdaSBrian Foster #define QUEUE_FLAG_MQ_DEFAULT	((1UL << QUEUE_FLAG_IO_STAT) |		\
543ca5eebdaSBrian Foster 				 (1UL << QUEUE_FLAG_SAME_COMP) |	\
544ca5eebdaSBrian Foster 				 (1UL << QUEUE_FLAG_NOWAIT))
54594eddfbeSJens Axboe 
5468814ce8aSBart Van Assche void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
5478814ce8aSBart Van Assche void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
5488814ce8aSBart Van Assche bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
5498814ce8aSBart Van Assche 
5501da177e4SLinus Torvalds #define blk_queue_stopped(q)	test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
5513f3299d5SBart Van Assche #define blk_queue_dying(q)	test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
552320ae51fSJens Axboe #define blk_queue_init_done(q)	test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
553ac9fafa1SAlan D. Brunelle #define blk_queue_nomerges(q)	test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
554488991e2SAlan D. Brunelle #define blk_queue_noxmerges(q)	\
555488991e2SAlan D. Brunelle 	test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
556a68bbddbSJens Axboe #define blk_queue_nonrot(q)	test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
5571cb039f3SChristoph Hellwig #define blk_queue_stable_writes(q) \
5581cb039f3SChristoph Hellwig 	test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
559bc58ba94SJens Axboe #define blk_queue_io_stat(q)	test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
560e2e1a148SJens Axboe #define blk_queue_add_random(q)	test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
561e84e8f06SChaitanya Kulkarni #define blk_queue_zone_resetall(q)	\
562e84e8f06SChaitanya Kulkarni 	test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
563163d4baaSToshi Kani #define blk_queue_dax(q)	test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
56449d92c0dSLogan Gunthorpe #define blk_queue_pci_p2pdma(q)	\
56549d92c0dSLogan Gunthorpe 	test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
5666f816b4bSTejun Heo #ifdef CONFIG_BLK_RQ_ALLOC_TIME
5676f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q)	\
5686f816b4bSTejun Heo 	test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
5696f816b4bSTejun Heo #else
5706f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q)	false
5716f816b4bSTejun Heo #endif
5721da177e4SLinus Torvalds 
57333659ebbSChristoph Hellwig #define blk_noretry_request(rq) \
57433659ebbSChristoph Hellwig 	((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
57533659ebbSChristoph Hellwig 			     REQ_FAILFAST_DRIVER))
576f4560ffeSMing Lei #define blk_queue_quiesced(q)	test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
577cd84a62eSBart Van Assche #define blk_queue_pm_only(q)	atomic_read(&(q)->pm_only)
57858c898baSMing Lei #define blk_queue_registered(q)	test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
5794d337cebSMing Lei #define blk_queue_sq_sched(q)	test_bit(QUEUE_FLAG_SQ_SCHED, &(q)->queue_flags)
580414dd48eSChao Leng #define blk_queue_skip_tagset_quiesce(q) \
581414dd48eSChao Leng 	test_bit(QUEUE_FLAG_SKIP_TAGSET_QUIESCE, &(q)->queue_flags)
582c9254f2dSBart Van Assche 
583cd84a62eSBart Van Assche extern void blk_set_pm_only(struct request_queue *q);
584cd84a62eSBart Van Assche extern void blk_clear_pm_only(struct request_queue *q);
5854aff5e23SJens Axboe 
5861da177e4SLinus Torvalds #define list_entry_rq(ptr)	list_entry((ptr), struct request, queuelist)
5871da177e4SLinus Torvalds 
5883ab3a031SChristoph Hellwig #define dma_map_bvec(dev, bv, dir, attrs) \
5893ab3a031SChristoph Hellwig 	dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
5903ab3a031SChristoph Hellwig 	(dir), (attrs))
5913ab3a031SChristoph Hellwig 
592344e9ffcSJens Axboe static inline bool queue_is_mq(struct request_queue *q)
59349fd524fSJens Axboe {
594a1ce35faSJens Axboe 	return q->mq_ops;
59549fd524fSJens Axboe }
59649fd524fSJens Axboe 
59752abca64SAlan Stern #ifdef CONFIG_PM
59852abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
59952abca64SAlan Stern {
60052abca64SAlan Stern 	return q->rpm_status;
60152abca64SAlan Stern }
60252abca64SAlan Stern #else
60352abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
60452abca64SAlan Stern {
60552abca64SAlan Stern 	return RPM_ACTIVE;
60652abca64SAlan Stern }
60752abca64SAlan Stern #endif
60852abca64SAlan Stern 
609797476b8SDamien Le Moal static inline bool blk_queue_is_zoned(struct request_queue *q)
610797476b8SDamien Le Moal {
6117437bb73SChristoph Hellwig 	return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && q->limits.zoned;
612797476b8SDamien Le Moal }
613797476b8SDamien Le Moal 
6146a5ac984SBart Van Assche #ifdef CONFIG_BLK_DEV_ZONED
615668bfeeaSChristoph Hellwig unsigned int bdev_nr_zones(struct block_device *bdev);
616668bfeeaSChristoph Hellwig 
617d86e716aSChristoph Hellwig static inline unsigned int disk_nr_zones(struct gendisk *disk)
618965b652eSDamien Le Moal {
619d86e716aSChristoph Hellwig 	return blk_queue_is_zoned(disk->queue) ? disk->nr_zones : 0;
620965b652eSDamien Le Moal }
621965b652eSDamien Le Moal 
622d86e716aSChristoph Hellwig static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
6236cc77e9cSChristoph Hellwig {
624d86e716aSChristoph Hellwig 	if (!blk_queue_is_zoned(disk->queue))
6256cc77e9cSChristoph Hellwig 		return 0;
626d86e716aSChristoph Hellwig 	return sector >> ilog2(disk->queue->limits.chunk_sectors);
6276cc77e9cSChristoph Hellwig }
6286cc77e9cSChristoph Hellwig 
629d86e716aSChristoph Hellwig static inline bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
6306cc77e9cSChristoph Hellwig {
631d86e716aSChristoph Hellwig 	if (!blk_queue_is_zoned(disk->queue))
6326cc77e9cSChristoph Hellwig 		return false;
633d86e716aSChristoph Hellwig 	if (!disk->conv_zones_bitmap)
634f216fdd7SChristoph Hellwig 		return true;
635d86e716aSChristoph Hellwig 	return !test_bit(disk_zone_no(disk, sector), disk->conv_zones_bitmap);
6366cc77e9cSChristoph Hellwig }
637e15864f8SNiklas Cassel 
638982977dfSChristoph Hellwig static inline void disk_set_max_open_zones(struct gendisk *disk,
639e15864f8SNiklas Cassel 		unsigned int max_open_zones)
640e15864f8SNiklas Cassel {
6418c4955c0SChristoph Hellwig 	disk->queue->limits.max_open_zones = max_open_zones;
642e15864f8SNiklas Cassel }
643e15864f8SNiklas Cassel 
644982977dfSChristoph Hellwig static inline void disk_set_max_active_zones(struct gendisk *disk,
645659bf827SNiklas Cassel 		unsigned int max_active_zones)
646659bf827SNiklas Cassel {
6478c4955c0SChristoph Hellwig 	disk->queue->limits.max_active_zones = max_active_zones;
648659bf827SNiklas Cassel }
649659bf827SNiklas Cassel 
6501dc01720SChristoph Hellwig static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
651659bf827SNiklas Cassel {
6528c4955c0SChristoph Hellwig 	return bdev->bd_disk->queue->limits.max_open_zones;
653659bf827SNiklas Cassel }
6541dc01720SChristoph Hellwig 
6551dc01720SChristoph Hellwig static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
6561dc01720SChristoph Hellwig {
6578c4955c0SChristoph Hellwig 	return bdev->bd_disk->queue->limits.max_active_zones;
6581dc01720SChristoph Hellwig }
6591dc01720SChristoph Hellwig 
660965b652eSDamien Le Moal #else /* CONFIG_BLK_DEV_ZONED */
661668bfeeaSChristoph Hellwig static inline unsigned int bdev_nr_zones(struct block_device *bdev)
662668bfeeaSChristoph Hellwig {
663668bfeeaSChristoph Hellwig 	return 0;
664668bfeeaSChristoph Hellwig }
665668bfeeaSChristoph Hellwig 
666d86e716aSChristoph Hellwig static inline unsigned int disk_nr_zones(struct gendisk *disk)
667965b652eSDamien Le Moal {
668965b652eSDamien Le Moal 	return 0;
669965b652eSDamien Le Moal }
670d86e716aSChristoph Hellwig static inline bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
67102992df8SJohannes Thumshirn {
67202992df8SJohannes Thumshirn 	return false;
67302992df8SJohannes Thumshirn }
674d86e716aSChristoph Hellwig static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
67502992df8SJohannes Thumshirn {
67602992df8SJohannes Thumshirn 	return 0;
67702992df8SJohannes Thumshirn }
6781dc01720SChristoph Hellwig static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
679e15864f8SNiklas Cassel {
680e15864f8SNiklas Cassel 	return 0;
681e15864f8SNiklas Cassel }
682d86e716aSChristoph Hellwig 
6831dc01720SChristoph Hellwig static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
684659bf827SNiklas Cassel {
685659bf827SNiklas Cassel 	return 0;
686659bf827SNiklas Cassel }
6876a5ac984SBart Van Assche #endif /* CONFIG_BLK_DEV_ZONED */
6886cc77e9cSChristoph Hellwig 
689d278d4a8SJens Axboe static inline unsigned int blk_queue_depth(struct request_queue *q)
690d278d4a8SJens Axboe {
691d278d4a8SJens Axboe 	if (q->queue_depth)
692d278d4a8SJens Axboe 		return q->queue_depth;
693d278d4a8SJens Axboe 
694d278d4a8SJens Axboe 	return q->nr_requests;
695d278d4a8SJens Axboe }
696d278d4a8SJens Axboe 
6973d6392cfSJens Axboe /*
6983d6392cfSJens Axboe  * default timeout for SG_IO if none specified
6993d6392cfSJens Axboe  */
7003d6392cfSJens Axboe #define BLK_DEFAULT_SG_TIMEOUT	(60 * HZ)
701f2f1fa78SLinus Torvalds #define BLK_MIN_SG_TIMEOUT	(7 * HZ)
7023d6392cfSJens Axboe 
7035705f702SNeilBrown /* This should not be used directly - use rq_for_each_segment */
7041e428079SJens Axboe #define for_each_bio(_bio)		\
7051e428079SJens Axboe 	for (; _bio; _bio = _bio->bi_next)
7061da177e4SLinus Torvalds 
707322cbb50SChristoph Hellwig int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
708322cbb50SChristoph Hellwig 				 const struct attribute_group **groups);
709322cbb50SChristoph Hellwig static inline int __must_check add_disk(struct gendisk *disk)
710322cbb50SChristoph Hellwig {
711322cbb50SChristoph Hellwig 	return device_add_disk(NULL, disk, NULL);
712322cbb50SChristoph Hellwig }
713322cbb50SChristoph Hellwig void del_gendisk(struct gendisk *gp);
714322cbb50SChristoph Hellwig void invalidate_disk(struct gendisk *disk);
715322cbb50SChristoph Hellwig void set_disk_ro(struct gendisk *disk, bool read_only);
716322cbb50SChristoph Hellwig void disk_uevent(struct gendisk *disk, enum kobject_action action);
717322cbb50SChristoph Hellwig 
718322cbb50SChristoph Hellwig static inline int get_disk_ro(struct gendisk *disk)
719322cbb50SChristoph Hellwig {
720322cbb50SChristoph Hellwig 	return disk->part0->bd_read_only ||
721322cbb50SChristoph Hellwig 		test_bit(GD_READ_ONLY, &disk->state);
722322cbb50SChristoph Hellwig }
723322cbb50SChristoph Hellwig 
724322cbb50SChristoph Hellwig static inline int bdev_read_only(struct block_device *bdev)
725322cbb50SChristoph Hellwig {
726322cbb50SChristoph Hellwig 	return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
727322cbb50SChristoph Hellwig }
728322cbb50SChristoph Hellwig 
729322cbb50SChristoph Hellwig bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
730ab6860f6SChristoph Hellwig void disk_force_media_change(struct gendisk *disk);
731560e20e4SChristoph Hellwig void bdev_mark_dead(struct block_device *bdev, bool surprise);
732322cbb50SChristoph Hellwig 
733322cbb50SChristoph Hellwig void add_disk_randomness(struct gendisk *disk) __latent_entropy;
734322cbb50SChristoph Hellwig void rand_initialize_disk(struct gendisk *disk);
735322cbb50SChristoph Hellwig 
736322cbb50SChristoph Hellwig static inline sector_t get_start_sect(struct block_device *bdev)
737322cbb50SChristoph Hellwig {
738322cbb50SChristoph Hellwig 	return bdev->bd_start_sect;
739322cbb50SChristoph Hellwig }
740322cbb50SChristoph Hellwig 
741322cbb50SChristoph Hellwig static inline sector_t bdev_nr_sectors(struct block_device *bdev)
742322cbb50SChristoph Hellwig {
743322cbb50SChristoph Hellwig 	return bdev->bd_nr_sectors;
744322cbb50SChristoph Hellwig }
745322cbb50SChristoph Hellwig 
746322cbb50SChristoph Hellwig static inline loff_t bdev_nr_bytes(struct block_device *bdev)
747322cbb50SChristoph Hellwig {
748322cbb50SChristoph Hellwig 	return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT;
749322cbb50SChristoph Hellwig }
750322cbb50SChristoph Hellwig 
751322cbb50SChristoph Hellwig static inline sector_t get_capacity(struct gendisk *disk)
752322cbb50SChristoph Hellwig {
753322cbb50SChristoph Hellwig 	return bdev_nr_sectors(disk->part0);
754322cbb50SChristoph Hellwig }
755322cbb50SChristoph Hellwig 
756322cbb50SChristoph Hellwig static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
757322cbb50SChristoph Hellwig {
758322cbb50SChristoph Hellwig 	return bdev_nr_sectors(sb->s_bdev) >>
759322cbb50SChristoph Hellwig 		(sb->s_blocksize_bits - SECTOR_SHIFT);
760322cbb50SChristoph Hellwig }
761322cbb50SChristoph Hellwig 
762322cbb50SChristoph Hellwig int bdev_disk_changed(struct gendisk *disk, bool invalidate);
763322cbb50SChristoph Hellwig 
764322cbb50SChristoph Hellwig void put_disk(struct gendisk *disk);
76574fa8f9cSChristoph Hellwig struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node,
76674fa8f9cSChristoph Hellwig 		struct lock_class_key *lkclass);
767322cbb50SChristoph Hellwig 
768322cbb50SChristoph Hellwig /**
769322cbb50SChristoph Hellwig  * blk_alloc_disk - allocate a gendisk structure
77074fa8f9cSChristoph Hellwig  * @lim: queue limits to be used for this disk.
771322cbb50SChristoph Hellwig  * @node_id: numa node to allocate on
772322cbb50SChristoph Hellwig  *
773322cbb50SChristoph Hellwig  * Allocate and pre-initialize a gendisk structure for use with BIO based
774322cbb50SChristoph Hellwig  * drivers.
775322cbb50SChristoph Hellwig  *
77674fa8f9cSChristoph Hellwig  * Returns an ERR_PTR on error, else the allocated disk.
77774fa8f9cSChristoph Hellwig  *
778322cbb50SChristoph Hellwig  * Context: can sleep
779322cbb50SChristoph Hellwig  */
78074fa8f9cSChristoph Hellwig #define blk_alloc_disk(lim, node_id)					\
781322cbb50SChristoph Hellwig ({									\
782322cbb50SChristoph Hellwig 	static struct lock_class_key __key;				\
783322cbb50SChristoph Hellwig 									\
78474fa8f9cSChristoph Hellwig 	__blk_alloc_disk(lim, node_id, &__key);				\
785322cbb50SChristoph Hellwig })
786322cbb50SChristoph Hellwig 
787322cbb50SChristoph Hellwig int __register_blkdev(unsigned int major, const char *name,
788322cbb50SChristoph Hellwig 		void (*probe)(dev_t devt));
789322cbb50SChristoph Hellwig #define register_blkdev(major, name) \
790322cbb50SChristoph Hellwig 	__register_blkdev(major, name, NULL)
791322cbb50SChristoph Hellwig void unregister_blkdev(unsigned int major, const char *name);
792322cbb50SChristoph Hellwig 
793444aa2c5SChristoph Hellwig bool disk_check_media_change(struct gendisk *disk);
794322cbb50SChristoph Hellwig void set_capacity(struct gendisk *disk, sector_t size);
795322cbb50SChristoph Hellwig 
796322cbb50SChristoph Hellwig #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
797322cbb50SChristoph Hellwig int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
798322cbb50SChristoph Hellwig void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
799322cbb50SChristoph Hellwig #else
800322cbb50SChristoph Hellwig static inline int bd_link_disk_holder(struct block_device *bdev,
801322cbb50SChristoph Hellwig 				      struct gendisk *disk)
802322cbb50SChristoph Hellwig {
803322cbb50SChristoph Hellwig 	return 0;
804322cbb50SChristoph Hellwig }
805322cbb50SChristoph Hellwig static inline void bd_unlink_disk_holder(struct block_device *bdev,
806322cbb50SChristoph Hellwig 					 struct gendisk *disk)
807322cbb50SChristoph Hellwig {
808322cbb50SChristoph Hellwig }
809322cbb50SChristoph Hellwig #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
810322cbb50SChristoph Hellwig 
811322cbb50SChristoph Hellwig dev_t part_devt(struct gendisk *disk, u8 partno);
812322cbb50SChristoph Hellwig void inc_diskseq(struct gendisk *disk);
813322cbb50SChristoph Hellwig void blk_request_module(dev_t devt);
8142d4dc890SIlya Loginov 
8151da177e4SLinus Torvalds extern int blk_register_queue(struct gendisk *disk);
8161da177e4SLinus Torvalds extern void blk_unregister_queue(struct gendisk *disk);
8173e08773cSChristoph Hellwig void submit_bio_noacct(struct bio *bio);
8185a97806fSChristoph Hellwig struct bio *bio_split_to_limits(struct bio *bio);
81924b83debSChristoph Hellwig 
820ef9e3facSKiyoshi Ueda extern int blk_lld_busy(struct request_queue *q);
8219a95e4efSBart Van Assche extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
8222e6edc95SDan Williams extern void blk_queue_exit(struct request_queue *q);
8231da177e4SLinus Torvalds extern void blk_sync_queue(struct request_queue *q);
824fb9b16e1SKeith Busch 
825e47bc4edSChaitanya Kulkarni /* Helper to convert REQ_OP_XXX to its string format XXX */
82677e7ffd7SBart Van Assche extern const char *blk_op_str(enum req_op op);
827e47bc4edSChaitanya Kulkarni 
8282a842acaSChristoph Hellwig int blk_status_to_errno(blk_status_t status);
8292a842acaSChristoph Hellwig blk_status_t errno_to_blk_status(int errno);
8307ba37927SKent Overstreet const char *blk_status_to_str(blk_status_t status);
8312a842acaSChristoph Hellwig 
832ef99b2d3SChristoph Hellwig /* only poll the hardware once, don't continue until a completion was found */
833ef99b2d3SChristoph Hellwig #define BLK_POLL_ONESHOT		(1 << 0)
8345a72e899SJens Axboe int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
8355a72e899SJens Axboe int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
8365a72e899SJens Axboe 			unsigned int flags);
83705229beeSJens Axboe 
838165125e1SJens Axboe static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
8391da177e4SLinus Torvalds {
84017220ca5SPavel Begunkov 	return bdev->bd_queue;	/* this is never NULL */
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
84302694e86SChaitanya Kulkarni /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
84402694e86SChaitanya Kulkarni const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
84502694e86SChaitanya Kulkarni 
846d0ea6bdeSDamien Le Moal static inline unsigned int bio_zone_no(struct bio *bio)
847d0ea6bdeSDamien Le Moal {
848d86e716aSChristoph Hellwig 	return disk_zone_no(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
849d0ea6bdeSDamien Le Moal }
850d0ea6bdeSDamien Le Moal 
851d0ea6bdeSDamien Le Moal static inline unsigned int bio_zone_is_seq(struct bio *bio)
852d0ea6bdeSDamien Le Moal {
853d86e716aSChristoph Hellwig 	return disk_zone_is_seq(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
854d0ea6bdeSDamien Le Moal }
8556cc77e9cSChristoph Hellwig 
856762380adSJens Axboe /*
8578689461bSChristoph Hellwig  * Return how much of the chunk is left to be used for I/O at a given offset.
8588689461bSChristoph Hellwig  */
8598689461bSChristoph Hellwig static inline unsigned int blk_chunk_sectors_left(sector_t offset,
8608689461bSChristoph Hellwig 		unsigned int chunk_sectors)
8618689461bSChristoph Hellwig {
8628689461bSChristoph Hellwig 	if (unlikely(!is_power_of_2(chunk_sectors)))
8638689461bSChristoph Hellwig 		return chunk_sectors - sector_div(offset, chunk_sectors);
8648689461bSChristoph Hellwig 	return chunk_sectors - (offset & (chunk_sectors - 1));
8658689461bSChristoph Hellwig }
8668689461bSChristoph Hellwig 
867d690cb8aSChristoph Hellwig /**
868d690cb8aSChristoph Hellwig  * queue_limits_start_update - start an atomic update of queue limits
869d690cb8aSChristoph Hellwig  * @q:		queue to update
870d690cb8aSChristoph Hellwig  *
871d690cb8aSChristoph Hellwig  * This functions starts an atomic update of the queue limits.  It takes a lock
872d690cb8aSChristoph Hellwig  * to prevent other updates and returns a snapshot of the current limits that
873d690cb8aSChristoph Hellwig  * the caller can modify.  The caller must call queue_limits_commit_update()
874d690cb8aSChristoph Hellwig  * to finish the update.
875d690cb8aSChristoph Hellwig  *
876d690cb8aSChristoph Hellwig  * Context: process context.  The caller must have frozen the queue or ensured
877d690cb8aSChristoph Hellwig  * that there is outstanding I/O by other means.
878d690cb8aSChristoph Hellwig  */
879d690cb8aSChristoph Hellwig static inline struct queue_limits
880d690cb8aSChristoph Hellwig queue_limits_start_update(struct request_queue *q)
881d690cb8aSChristoph Hellwig 	__acquires(q->limits_lock)
882d690cb8aSChristoph Hellwig {
883d690cb8aSChristoph Hellwig 	mutex_lock(&q->limits_lock);
884d690cb8aSChristoph Hellwig 	return q->limits;
885d690cb8aSChristoph Hellwig }
886d690cb8aSChristoph Hellwig int queue_limits_commit_update(struct request_queue *q,
887d690cb8aSChristoph Hellwig 		struct queue_limits *lim);
888631d4efbSChristoph Hellwig int queue_limits_set(struct request_queue *q, struct queue_limits *lim);
889d690cb8aSChristoph Hellwig 
8908689461bSChristoph Hellwig /*
8911da177e4SLinus Torvalds  * Access functions for manipulating queue properties
8921da177e4SLinus Torvalds  */
8939bb33f24SChristoph Hellwig void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
894086fa5ffSMartin K. Petersen extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
895762380adSJens Axboe extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
8968a78362cSMartin K. Petersen extern void blk_queue_max_segments(struct request_queue *, unsigned short);
8971e739730SChristoph Hellwig extern void blk_queue_max_discard_segments(struct request_queue *,
8981e739730SChristoph Hellwig 		unsigned short);
89944abff2cSChristoph Hellwig void blk_queue_max_secure_erase_sectors(struct request_queue *q,
90044abff2cSChristoph Hellwig 		unsigned int max_sectors);
901165125e1SJens Axboe extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
90267efc925SChristoph Hellwig extern void blk_queue_max_discard_sectors(struct request_queue *q,
90367efc925SChristoph Hellwig 		unsigned int max_discard_sectors);
904a6f0788eSChaitanya Kulkarni extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
905a6f0788eSChaitanya Kulkarni 		unsigned int max_write_same_sectors);
906ad6bf88aSMikulas Patocka extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
9070512a75bSKeith Busch extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
9080512a75bSKeith Busch 		unsigned int max_zone_append_sectors);
909892b6f90SMartin K. Petersen extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
910a805a4faSDamien Le Moal void blk_queue_zone_write_granularity(struct request_queue *q,
911a805a4faSDamien Le Moal 				      unsigned int size);
912c72758f3SMartin K. Petersen extern void blk_queue_alignment_offset(struct request_queue *q,
913c72758f3SMartin K. Petersen 				       unsigned int alignment);
914471aa704SChristoph Hellwig void disk_update_readahead(struct gendisk *disk);
9157c958e32SMartin K. Petersen extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
916c72758f3SMartin K. Petersen extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
9173c5820c7SMartin K. Petersen extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
918c72758f3SMartin K. Petersen extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
919d278d4a8SJens Axboe extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
920b1bd055dSMartin K. Petersen extern void blk_set_stacking_limits(struct queue_limits *lim);
921c72758f3SMartin K. Petersen extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
922c72758f3SMartin K. Petersen 			    sector_t offset);
923c1373f1cSChristoph Hellwig void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
924c1373f1cSChristoph Hellwig 		sector_t offset, const char *pfx);
92527f8221aSFUJITA Tomonori extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
926165125e1SJens Axboe extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
92703100aadSKeith Busch extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
928165125e1SJens Axboe extern void blk_queue_dma_alignment(struct request_queue *, int);
92911c3e689SJames Bottomley extern void blk_queue_update_dma_alignment(struct request_queue *, int);
930242f9dcbSJens Axboe extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
93193e9d8e8SJens Axboe extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
9322e9bc346SChristoph Hellwig 
933a2247f19SDamien Le Moal struct blk_independent_access_ranges *
934a2247f19SDamien Le Moal disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges);
935a2247f19SDamien Le Moal void disk_set_independent_access_ranges(struct gendisk *disk,
936a2247f19SDamien Le Moal 				struct blk_independent_access_ranges *iars);
937a2247f19SDamien Le Moal 
9382e9bc346SChristoph Hellwig /*
9392e9bc346SChristoph Hellwig  * Elevator features for blk_queue_required_elevator_features:
9402e9bc346SChristoph Hellwig  */
9412e9bc346SChristoph Hellwig /* Supports zoned block devices sequential write constraint */
9422e9bc346SChristoph Hellwig #define ELEVATOR_F_ZBD_SEQ_WRITE	(1U << 0)
9432e9bc346SChristoph Hellwig 
94468c43f13SDamien Le Moal extern void blk_queue_required_elevator_features(struct request_queue *q,
94568c43f13SDamien Le Moal 						 unsigned int features);
94645147fb5SYoshihiro Shimoda extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
94745147fb5SYoshihiro Shimoda 					      struct device *dev);
9481da177e4SLinus Torvalds 
94909ac46c4STejun Heo bool __must_check blk_get_queue(struct request_queue *);
950165125e1SJens Axboe extern void blk_put_queue(struct request_queue *);
9517a5428dcSChristoph Hellwig 
9527a5428dcSChristoph Hellwig void blk_mark_disk_dead(struct gendisk *disk);
9531da177e4SLinus Torvalds 
9541a4dcfa8SChristoph Hellwig #ifdef CONFIG_BLOCK
955316cc67dSShaohua Li /*
95675df7136SSuresh Jayaraman  * blk_plug permits building a queue of related requests by holding the I/O
95775df7136SSuresh Jayaraman  * fragments for a short period. This allows merging of sequential requests
95875df7136SSuresh Jayaraman  * into single larger request. As the requests are moved from a per-task list to
95975df7136SSuresh Jayaraman  * the device's request_queue in a batch, this results in improved scalability
96075df7136SSuresh Jayaraman  * as the lock contention for request_queue lock is reduced.
96175df7136SSuresh Jayaraman  *
96275df7136SSuresh Jayaraman  * It is ok not to disable preemption when adding the request to the plug list
963008f75a2SChristoph Hellwig  * or when attempting a merge. For details, please see schedule() where
964008f75a2SChristoph Hellwig  * blk_flush_plug() is called.
965316cc67dSShaohua Li  */
96673c10101SJens Axboe struct blk_plug {
967bc490f81SJens Axboe 	struct request *mq_list; /* blk-mq requests */
96847c122e3SJens Axboe 
96947c122e3SJens Axboe 	/* if ios_left is > 1, we can batch tag/rq allocations */
97047c122e3SJens Axboe 	struct request *cached_rq;
971da4c8c3dSJens Axboe 	u64 cur_ktime;
97247c122e3SJens Axboe 	unsigned short nr_ios;
97347c122e3SJens Axboe 
9745f0ed774SJens Axboe 	unsigned short rq_count;
97547c122e3SJens Axboe 
976ce5b009cSJens Axboe 	bool multiple_queues;
977dc5fc361SJens Axboe 	bool has_elevator;
97847c122e3SJens Axboe 
97947c122e3SJens Axboe 	struct list_head cb_list; /* md requires an unplug callback */
98073c10101SJens Axboe };
98155c022bbSShaohua Li 
9829cbb1750SNeilBrown struct blk_plug_cb;
98374018dc3SNeilBrown typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
984048c9374SNeilBrown struct blk_plug_cb {
985048c9374SNeilBrown 	struct list_head list;
9869cbb1750SNeilBrown 	blk_plug_cb_fn callback;
9879cbb1750SNeilBrown 	void *data;
988048c9374SNeilBrown };
9899cbb1750SNeilBrown extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
9909cbb1750SNeilBrown 					     void *data, int size);
99173c10101SJens Axboe extern void blk_start_plug(struct blk_plug *);
99247c122e3SJens Axboe extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
99373c10101SJens Axboe extern void blk_finish_plug(struct blk_plug *);
99473c10101SJens Axboe 
995aa8dcccaSChristoph Hellwig void __blk_flush_plug(struct blk_plug *plug, bool from_schedule);
996aa8dcccaSChristoph Hellwig static inline void blk_flush_plug(struct blk_plug *plug, bool async)
99773c10101SJens Axboe {
998aa8dcccaSChristoph Hellwig 	if (plug)
999aa8dcccaSChristoph Hellwig 		__blk_flush_plug(plug, async);
100073c10101SJens Axboe }
100173c10101SJens Axboe 
100206b23f92SJens Axboe /*
100306b23f92SJens Axboe  * tsk == current here
100406b23f92SJens Axboe  */
100506b23f92SJens Axboe static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
100606b23f92SJens Axboe {
100706b23f92SJens Axboe 	struct blk_plug *plug = tsk->plug;
100806b23f92SJens Axboe 
100906b23f92SJens Axboe 	if (plug)
101006b23f92SJens Axboe 		plug->cur_ktime = 0;
101106b23f92SJens Axboe 	current->flags &= ~PF_BLOCK_TS;
101206b23f92SJens Axboe }
101306b23f92SJens Axboe 
1014c6bf3f0eSChristoph Hellwig int blkdev_issue_flush(struct block_device *bdev);
10151a4dcfa8SChristoph Hellwig long nr_blockdev_pages(void);
10161a4dcfa8SChristoph Hellwig #else /* CONFIG_BLOCK */
10171a4dcfa8SChristoph Hellwig struct blk_plug {
10181a4dcfa8SChristoph Hellwig };
10191a4dcfa8SChristoph Hellwig 
102047c122e3SJens Axboe static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
102147c122e3SJens Axboe 					 unsigned short nr_ios)
102247c122e3SJens Axboe {
102347c122e3SJens Axboe }
102447c122e3SJens Axboe 
10251a4dcfa8SChristoph Hellwig static inline void blk_start_plug(struct blk_plug *plug)
10261a4dcfa8SChristoph Hellwig {
10271a4dcfa8SChristoph Hellwig }
10281a4dcfa8SChristoph Hellwig 
10291a4dcfa8SChristoph Hellwig static inline void blk_finish_plug(struct blk_plug *plug)
10301a4dcfa8SChristoph Hellwig {
10311a4dcfa8SChristoph Hellwig }
10321a4dcfa8SChristoph Hellwig 
1033008f75a2SChristoph Hellwig static inline void blk_flush_plug(struct blk_plug *plug, bool async)
10341a4dcfa8SChristoph Hellwig {
10351a4dcfa8SChristoph Hellwig }
10361a4dcfa8SChristoph Hellwig 
103706b23f92SJens Axboe static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
103806b23f92SJens Axboe {
103906b23f92SJens Axboe }
104006b23f92SJens Axboe 
1041c6bf3f0eSChristoph Hellwig static inline int blkdev_issue_flush(struct block_device *bdev)
10421a4dcfa8SChristoph Hellwig {
10431a4dcfa8SChristoph Hellwig 	return 0;
10441a4dcfa8SChristoph Hellwig }
10451a4dcfa8SChristoph Hellwig 
10461a4dcfa8SChristoph Hellwig static inline long nr_blockdev_pages(void)
10471a4dcfa8SChristoph Hellwig {
10481a4dcfa8SChristoph Hellwig 	return 0;
10491a4dcfa8SChristoph Hellwig }
10501a4dcfa8SChristoph Hellwig #endif /* CONFIG_BLOCK */
10511a4dcfa8SChristoph Hellwig 
105271ac860aSMing Lei extern void blk_io_schedule(void);
105371ac860aSMing Lei 
105444abff2cSChristoph Hellwig int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
105544abff2cSChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask);
105644abff2cSChristoph Hellwig int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
105744abff2cSChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);
105844abff2cSChristoph Hellwig int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
105944abff2cSChristoph Hellwig 		sector_t nr_sects, gfp_t gfp);
1060ee472d83SChristoph Hellwig 
1061ee472d83SChristoph Hellwig #define BLKDEV_ZERO_NOUNMAP	(1 << 0)  /* do not free blocks */
1062cb365b96SChristoph Hellwig #define BLKDEV_ZERO_NOFALLBACK	(1 << 1)  /* don't write explicit zeroes */
1063ee472d83SChristoph Hellwig 
1064e73c23ffSChaitanya Kulkarni extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1065e73c23ffSChaitanya Kulkarni 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1066ee472d83SChristoph Hellwig 		unsigned flags);
10673f14d792SDmitry Monakhov extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1068ee472d83SChristoph Hellwig 		sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1069ee472d83SChristoph Hellwig 
10702cf6d26aSChristoph Hellwig static inline int sb_issue_discard(struct super_block *sb, sector_t block,
10712cf6d26aSChristoph Hellwig 		sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1072fb2dce86SDavid Woodhouse {
1073233bde21SBart Van Assche 	return blkdev_issue_discard(sb->s_bdev,
1074233bde21SBart Van Assche 				    block << (sb->s_blocksize_bits -
1075233bde21SBart Van Assche 					      SECTOR_SHIFT),
1076233bde21SBart Van Assche 				    nr_blocks << (sb->s_blocksize_bits -
1077233bde21SBart Van Assche 						  SECTOR_SHIFT),
107844abff2cSChristoph Hellwig 				    gfp_mask);
1079fb2dce86SDavid Woodhouse }
1080e6fa0be6SLukas Czerner static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1081a107e5a3STheodore Ts'o 		sector_t nr_blocks, gfp_t gfp_mask)
1082e6fa0be6SLukas Czerner {
1083e6fa0be6SLukas Czerner 	return blkdev_issue_zeroout(sb->s_bdev,
1084233bde21SBart Van Assche 				    block << (sb->s_blocksize_bits -
1085233bde21SBart Van Assche 					      SECTOR_SHIFT),
1086233bde21SBart Van Assche 				    nr_blocks << (sb->s_blocksize_bits -
1087233bde21SBart Van Assche 						  SECTOR_SHIFT),
1088ee472d83SChristoph Hellwig 				    gfp_mask, 0);
1089e6fa0be6SLukas Czerner }
10901da177e4SLinus Torvalds 
1091fa01b1e9SChristoph Hellwig static inline bool bdev_is_partition(struct block_device *bdev)
1092fa01b1e9SChristoph Hellwig {
1093fa01b1e9SChristoph Hellwig 	return bdev->bd_partno;
1094fa01b1e9SChristoph Hellwig }
1095fa01b1e9SChristoph Hellwig 
1096eb28d31bSMartin K. Petersen enum blk_default_limits {
1097eb28d31bSMartin K. Petersen 	BLK_MAX_SEGMENTS	= 128,
1098eb28d31bSMartin K. Petersen 	BLK_SAFE_MAX_SECTORS	= 255,
1099eb28d31bSMartin K. Petersen 	BLK_MAX_SEGMENT_SIZE	= 65536,
1100eb28d31bSMartin K. Petersen 	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
1101eb28d31bSMartin K. Petersen };
11020e435ac2SMilan Broz 
1103d6b9f4e6SChristoph Hellwig /*
1104d6b9f4e6SChristoph Hellwig  * Default upper limit for the software max_sectors limit used for
1105d6b9f4e6SChristoph Hellwig  * regular file system I/O.  This can be increased through sysfs.
1106d6b9f4e6SChristoph Hellwig  *
1107d6b9f4e6SChristoph Hellwig  * Not to be confused with the max_hw_sector limit that is entirely
1108d6b9f4e6SChristoph Hellwig  * controlled by the driver, usually based on hardware limits.
1109d6b9f4e6SChristoph Hellwig  */
1110d6b9f4e6SChristoph Hellwig #define BLK_DEF_MAX_SECTORS_CAP	2560u
11110a26f327SKeith Busch 
1112af2c68feSBart Van Assche static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1113ae03bf63SMartin K. Petersen {
1114025146e1SMartin K. Petersen 	return q->limits.seg_boundary_mask;
1115ae03bf63SMartin K. Petersen }
1116ae03bf63SMartin K. Petersen 
1117af2c68feSBart Van Assche static inline unsigned long queue_virt_boundary(const struct request_queue *q)
111803100aadSKeith Busch {
111903100aadSKeith Busch 	return q->limits.virt_boundary_mask;
112003100aadSKeith Busch }
112103100aadSKeith Busch 
1122af2c68feSBart Van Assche static inline unsigned int queue_max_sectors(const struct request_queue *q)
1123ae03bf63SMartin K. Petersen {
1124025146e1SMartin K. Petersen 	return q->limits.max_sectors;
1125ae03bf63SMartin K. Petersen }
1126ae03bf63SMartin K. Petersen 
1127547e2f70SChristoph Hellwig static inline unsigned int queue_max_bytes(struct request_queue *q)
1128547e2f70SChristoph Hellwig {
1129547e2f70SChristoph Hellwig 	return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1130547e2f70SChristoph Hellwig }
1131547e2f70SChristoph Hellwig 
1132af2c68feSBart Van Assche static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1133ae03bf63SMartin K. Petersen {
1134025146e1SMartin K. Petersen 	return q->limits.max_hw_sectors;
1135ae03bf63SMartin K. Petersen }
1136ae03bf63SMartin K. Petersen 
1137af2c68feSBart Van Assche static inline unsigned short queue_max_segments(const struct request_queue *q)
1138ae03bf63SMartin K. Petersen {
11398a78362cSMartin K. Petersen 	return q->limits.max_segments;
1140ae03bf63SMartin K. Petersen }
1141ae03bf63SMartin K. Petersen 
1142af2c68feSBart Van Assche static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
11431e739730SChristoph Hellwig {
11441e739730SChristoph Hellwig 	return q->limits.max_discard_segments;
11451e739730SChristoph Hellwig }
11461e739730SChristoph Hellwig 
1147af2c68feSBart Van Assche static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1148ae03bf63SMartin K. Petersen {
1149025146e1SMartin K. Petersen 	return q->limits.max_segment_size;
1150ae03bf63SMartin K. Petersen }
1151ae03bf63SMartin K. Petersen 
11520512a75bSKeith Busch static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
11530512a75bSKeith Busch {
1154fe6f0cdcSJohannes Thumshirn 
1155fe6f0cdcSJohannes Thumshirn 	const struct queue_limits *l = &q->limits;
1156fe6f0cdcSJohannes Thumshirn 
1157fe6f0cdcSJohannes Thumshirn 	return min(l->max_zone_append_sectors, l->max_sectors);
11580512a75bSKeith Busch }
11590512a75bSKeith Busch 
11602aba0d19SChristoph Hellwig static inline unsigned int
11612aba0d19SChristoph Hellwig bdev_max_zone_append_sectors(struct block_device *bdev)
11622aba0d19SChristoph Hellwig {
11632aba0d19SChristoph Hellwig 	return queue_max_zone_append_sectors(bdev_get_queue(bdev));
11642aba0d19SChristoph Hellwig }
11652aba0d19SChristoph Hellwig 
116665ea1b66SNaohiro Aota static inline unsigned int bdev_max_segments(struct block_device *bdev)
116765ea1b66SNaohiro Aota {
116865ea1b66SNaohiro Aota 	return queue_max_segments(bdev_get_queue(bdev));
116965ea1b66SNaohiro Aota }
117065ea1b66SNaohiro Aota 
1171ad6bf88aSMikulas Patocka static inline unsigned queue_logical_block_size(const struct request_queue *q)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	int retval = 512;
11741da177e4SLinus Torvalds 
1175025146e1SMartin K. Petersen 	if (q && q->limits.logical_block_size)
1176025146e1SMartin K. Petersen 		retval = q->limits.logical_block_size;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	return retval;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
1181ad6bf88aSMikulas Patocka static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
11821da177e4SLinus Torvalds {
1183e1defc4fSMartin K. Petersen 	return queue_logical_block_size(bdev_get_queue(bdev));
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
1186af2c68feSBart Van Assche static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1187c72758f3SMartin K. Petersen {
1188c72758f3SMartin K. Petersen 	return q->limits.physical_block_size;
1189c72758f3SMartin K. Petersen }
1190c72758f3SMartin K. Petersen 
1191892b6f90SMartin K. Petersen static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1192ac481c20SMartin K. Petersen {
1193ac481c20SMartin K. Petersen 	return queue_physical_block_size(bdev_get_queue(bdev));
1194ac481c20SMartin K. Petersen }
1195ac481c20SMartin K. Petersen 
1196af2c68feSBart Van Assche static inline unsigned int queue_io_min(const struct request_queue *q)
1197c72758f3SMartin K. Petersen {
1198c72758f3SMartin K. Petersen 	return q->limits.io_min;
1199c72758f3SMartin K. Petersen }
1200c72758f3SMartin K. Petersen 
1201ac481c20SMartin K. Petersen static inline int bdev_io_min(struct block_device *bdev)
1202ac481c20SMartin K. Petersen {
1203ac481c20SMartin K. Petersen 	return queue_io_min(bdev_get_queue(bdev));
1204ac481c20SMartin K. Petersen }
1205ac481c20SMartin K. Petersen 
1206af2c68feSBart Van Assche static inline unsigned int queue_io_opt(const struct request_queue *q)
1207c72758f3SMartin K. Petersen {
1208c72758f3SMartin K. Petersen 	return q->limits.io_opt;
1209c72758f3SMartin K. Petersen }
1210c72758f3SMartin K. Petersen 
1211ac481c20SMartin K. Petersen static inline int bdev_io_opt(struct block_device *bdev)
1212ac481c20SMartin K. Petersen {
1213ac481c20SMartin K. Petersen 	return queue_io_opt(bdev_get_queue(bdev));
1214ac481c20SMartin K. Petersen }
1215ac481c20SMartin K. Petersen 
1216a805a4faSDamien Le Moal static inline unsigned int
1217a805a4faSDamien Le Moal queue_zone_write_granularity(const struct request_queue *q)
1218a805a4faSDamien Le Moal {
1219a805a4faSDamien Le Moal 	return q->limits.zone_write_granularity;
1220a805a4faSDamien Le Moal }
1221a805a4faSDamien Le Moal 
1222a805a4faSDamien Le Moal static inline unsigned int
1223a805a4faSDamien Le Moal bdev_zone_write_granularity(struct block_device *bdev)
1224a805a4faSDamien Le Moal {
1225a805a4faSDamien Le Moal 	return queue_zone_write_granularity(bdev_get_queue(bdev));
1226a805a4faSDamien Le Moal }
1227a805a4faSDamien Le Moal 
122889098b07SChristoph Hellwig int bdev_alignment_offset(struct block_device *bdev);
12295c4b4a5cSChristoph Hellwig unsigned int bdev_discard_alignment(struct block_device *bdev);
1230c6e66634SPaolo Bonzini 
1231cf0fbf89SChristoph Hellwig static inline unsigned int bdev_max_discard_sectors(struct block_device *bdev)
1232cf0fbf89SChristoph Hellwig {
1233cf0fbf89SChristoph Hellwig 	return bdev_get_queue(bdev)->limits.max_discard_sectors;
1234cf0fbf89SChristoph Hellwig }
1235cf0fbf89SChristoph Hellwig 
12367b47ef52SChristoph Hellwig static inline unsigned int bdev_discard_granularity(struct block_device *bdev)
12377b47ef52SChristoph Hellwig {
12387b47ef52SChristoph Hellwig 	return bdev_get_queue(bdev)->limits.discard_granularity;
12397b47ef52SChristoph Hellwig }
12407b47ef52SChristoph Hellwig 
124144abff2cSChristoph Hellwig static inline unsigned int
124244abff2cSChristoph Hellwig bdev_max_secure_erase_sectors(struct block_device *bdev)
124344abff2cSChristoph Hellwig {
124444abff2cSChristoph Hellwig 	return bdev_get_queue(bdev)->limits.max_secure_erase_sectors;
124544abff2cSChristoph Hellwig }
124644abff2cSChristoph Hellwig 
1247a6f0788eSChaitanya Kulkarni static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1248a6f0788eSChaitanya Kulkarni {
1249a6f0788eSChaitanya Kulkarni 	struct request_queue *q = bdev_get_queue(bdev);
1250a6f0788eSChaitanya Kulkarni 
1251a6f0788eSChaitanya Kulkarni 	if (q)
1252a6f0788eSChaitanya Kulkarni 		return q->limits.max_write_zeroes_sectors;
1253a6f0788eSChaitanya Kulkarni 
1254a6f0788eSChaitanya Kulkarni 	return 0;
1255a6f0788eSChaitanya Kulkarni }
1256a6f0788eSChaitanya Kulkarni 
125710f0d2a5SChristoph Hellwig static inline bool bdev_nonrot(struct block_device *bdev)
125810f0d2a5SChristoph Hellwig {
125910f0d2a5SChristoph Hellwig 	return blk_queue_nonrot(bdev_get_queue(bdev));
126010f0d2a5SChristoph Hellwig }
126110f0d2a5SChristoph Hellwig 
12623222d8c2SChristoph Hellwig static inline bool bdev_synchronous(struct block_device *bdev)
12633222d8c2SChristoph Hellwig {
12643222d8c2SChristoph Hellwig 	return test_bit(QUEUE_FLAG_SYNCHRONOUS,
12653222d8c2SChristoph Hellwig 			&bdev_get_queue(bdev)->queue_flags);
12663222d8c2SChristoph Hellwig }
12673222d8c2SChristoph Hellwig 
126836d25489SChristoph Hellwig static inline bool bdev_stable_writes(struct block_device *bdev)
126936d25489SChristoph Hellwig {
127036d25489SChristoph Hellwig 	return test_bit(QUEUE_FLAG_STABLE_WRITES,
127136d25489SChristoph Hellwig 			&bdev_get_queue(bdev)->queue_flags);
127236d25489SChristoph Hellwig }
127336d25489SChristoph Hellwig 
127408e688fdSChristoph Hellwig static inline bool bdev_write_cache(struct block_device *bdev)
127508e688fdSChristoph Hellwig {
127608e688fdSChristoph Hellwig 	return test_bit(QUEUE_FLAG_WC, &bdev_get_queue(bdev)->queue_flags);
127708e688fdSChristoph Hellwig }
127808e688fdSChristoph Hellwig 
1279a557e82eSChristoph Hellwig static inline bool bdev_fua(struct block_device *bdev)
1280a557e82eSChristoph Hellwig {
1281a557e82eSChristoph Hellwig 	return test_bit(QUEUE_FLAG_FUA, &bdev_get_queue(bdev)->queue_flags);
1282a557e82eSChristoph Hellwig }
1283a557e82eSChristoph Hellwig 
1284568ec936SChristoph Hellwig static inline bool bdev_nowait(struct block_device *bdev)
1285568ec936SChristoph Hellwig {
1286568ec936SChristoph Hellwig 	return test_bit(QUEUE_FLAG_NOWAIT, &bdev_get_queue(bdev)->queue_flags);
1287568ec936SChristoph Hellwig }
1288568ec936SChristoph Hellwig 
1289797476b8SDamien Le Moal static inline bool bdev_is_zoned(struct block_device *bdev)
1290797476b8SDamien Le Moal {
1291fea127b3SPankaj Raghav 	return blk_queue_is_zoned(bdev_get_queue(bdev));
1292797476b8SDamien Le Moal }
1293797476b8SDamien Le Moal 
1294d67ea690SPankaj Raghav static inline unsigned int bdev_zone_no(struct block_device *bdev, sector_t sec)
1295d67ea690SPankaj Raghav {
1296d67ea690SPankaj Raghav 	return disk_zone_no(bdev->bd_disk, sec);
1297d67ea690SPankaj Raghav }
1298d67ea690SPankaj Raghav 
1299a3707982SBart Van Assche /* Whether write serialization is required for @op on zoned devices. */
1300a3707982SBart Van Assche static inline bool op_needs_zoned_write_locking(enum req_op op)
13018cafdb5aSPankaj Raghav {
13028cafdb5aSPankaj Raghav 	return op == REQ_OP_WRITE || op == REQ_OP_WRITE_ZEROES;
13038cafdb5aSPankaj Raghav }
13048cafdb5aSPankaj Raghav 
1305de71973cSChristoph Hellwig static inline bool bdev_op_is_zoned_write(struct block_device *bdev,
13063ddbe2a7SBart Van Assche 					  enum req_op op)
1307de71973cSChristoph Hellwig {
1308a3707982SBart Van Assche 	return bdev_is_zoned(bdev) && op_needs_zoned_write_locking(op);
1309482eb689SPete Wyckoff }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds static inline sector_t bdev_zone_sectors(struct block_device *bdev)
13124a2dcc35SKeith Busch {
13134a2dcc35SKeith Busch 	struct request_queue *q = bdev_get_queue(bdev);
13144a2dcc35SKeith Busch 
13154a2dcc35SKeith Busch 	if (!blk_queue_is_zoned(q))
13164a2dcc35SKeith Busch 		return 0;
13175debd969SKeith Busch 	return q->limits.chunk_sectors;
13185debd969SKeith Busch }
13195debd969SKeith Busch 
1320e29b2100SPankaj Raghav static inline sector_t bdev_offset_from_zone_start(struct block_device *bdev,
1321e29b2100SPankaj Raghav 						   sector_t sector)
1322e29b2100SPankaj Raghav {
1323e29b2100SPankaj Raghav 	return sector & (bdev_zone_sectors(bdev) - 1);
1324e29b2100SPankaj Raghav }
1325e29b2100SPankaj Raghav 
1326e29b2100SPankaj Raghav static inline bool bdev_is_zone_start(struct block_device *bdev,
1327e29b2100SPankaj Raghav 				      sector_t sector)
1328e29b2100SPankaj Raghav {
1329e29b2100SPankaj Raghav 	return bdev_offset_from_zone_start(bdev, sector) == 0;
1330e29b2100SPankaj Raghav }
1331e29b2100SPankaj Raghav 
13321da177e4SLinus Torvalds static inline int queue_dma_alignment(const struct request_queue *q)
13331da177e4SLinus Torvalds {
1334c964d62fSKeith Busch 	return q ? q->limits.dma_alignment : 511;
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
133714417799SNamhyung Kim static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
133887904074SFUJITA Tomonori {
133987904074SFUJITA Tomonori 	return queue_dma_alignment(bdev_get_queue(bdev));
134087904074SFUJITA Tomonori }
134114417799SNamhyung Kim 
13425debd969SKeith Busch static inline bool bdev_iter_is_aligned(struct block_device *bdev,
13435debd969SKeith Busch 					struct iov_iter *iter)
13445debd969SKeith Busch {
13455debd969SKeith Busch 	return iov_iter_is_aligned(iter, bdev_dma_alignment(bdev),
13465debd969SKeith Busch 				   bdev_logical_block_size(bdev) - 1);
13475debd969SKeith Busch }
13485debd969SKeith Busch 
134987904074SFUJITA Tomonori static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
135087904074SFUJITA Tomonori 				 unsigned int len)
135187904074SFUJITA Tomonori {
135287904074SFUJITA Tomonori 	unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
135387904074SFUJITA Tomonori 	return !(addr & alignment) && !(len & alignment);
135487904074SFUJITA Tomonori }
135587904074SFUJITA Tomonori 
13561da177e4SLinus Torvalds /* assumes size > 256 */
13571da177e4SLinus Torvalds static inline unsigned int blksize_bits(unsigned int size)
13581da177e4SLinus Torvalds {
1359adff2158SDawei Li 	return order_base_2(size >> SECTOR_SHIFT) + SECTOR_SHIFT;
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
136259c3d45eSJens Axboe int kblockd_schedule_work(struct work_struct *work);
1363818cd1cbSJens Axboe int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds #define MODULE_ALIAS_BLOCKDEV(major,minor) \
13661da177e4SLinus Torvalds 	MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
13671da177e4SLinus Torvalds #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
13681da177e4SLinus Torvalds 	MODULE_ALIAS("block-major-" __stringify(major) "-*")
13691da177e4SLinus Torvalds 
1370d145dc23SSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1371d145dc23SSatya Tangirala 
1372cb77cb5aSEric Biggers bool blk_crypto_register(struct blk_crypto_profile *profile,
1373cb77cb5aSEric Biggers 			 struct request_queue *q);
1374d145dc23SSatya Tangirala 
1375d145dc23SSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1376d145dc23SSatya Tangirala 
1377cb77cb5aSEric Biggers static inline bool blk_crypto_register(struct blk_crypto_profile *profile,
1378d145dc23SSatya Tangirala 				       struct request_queue *q)
1379d145dc23SSatya Tangirala {
1380d145dc23SSatya Tangirala 	return true;
1381d145dc23SSatya Tangirala }
1382d145dc23SSatya Tangirala 
1383d145dc23SSatya Tangirala #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1384d145dc23SSatya Tangirala 
13859208d414SChristoph Hellwig enum blk_unique_id {
13869208d414SChristoph Hellwig 	/* these match the Designator Types specified in SPC */
13879208d414SChristoph Hellwig 	BLK_UID_T10	= 1,
13889208d414SChristoph Hellwig 	BLK_UID_EUI64	= 2,
13899208d414SChristoph Hellwig 	BLK_UID_NAA	= 3,
13909208d414SChristoph Hellwig };
13919208d414SChristoph Hellwig 
139208f85851SAl Viro struct block_device_operations {
13933e08773cSChristoph Hellwig 	void (*submit_bio)(struct bio *bio);
139469fe0f29SMing Lei 	int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
139569fe0f29SMing Lei 			unsigned int flags);
139605bdb996SChristoph Hellwig 	int (*open)(struct gendisk *disk, blk_mode_t mode);
1397ae220766SChristoph Hellwig 	void (*release)(struct gendisk *disk);
139805bdb996SChristoph Hellwig 	int (*ioctl)(struct block_device *bdev, blk_mode_t mode,
139905bdb996SChristoph Hellwig 			unsigned cmd, unsigned long arg);
140005bdb996SChristoph Hellwig 	int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode,
140105bdb996SChristoph Hellwig 			unsigned cmd, unsigned long arg);
140277ea887eSTejun Heo 	unsigned int (*check_events) (struct gendisk *disk,
140377ea887eSTejun Heo 				      unsigned int clearing);
1404c3e33e04STejun Heo 	void (*unlock_native_capacity) (struct gendisk *);
140508f85851SAl Viro 	int (*getgeo)(struct block_device *, struct hd_geometry *);
1406e00adcadSChristoph Hellwig 	int (*set_read_only)(struct block_device *bdev, bool ro);
140776792055SChristoph Hellwig 	void (*free_disk)(struct gendisk *disk);
1408b3a27d05SNitin Gupta 	/* this callback is with swap_lock and sometimes page table lock held */
1409b3a27d05SNitin Gupta 	void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1410e76239a3SChristoph Hellwig 	int (*report_zones)(struct gendisk *, sector_t sector,
1411d4100351SChristoph Hellwig 			unsigned int nr_zones, report_zones_cb cb, void *data);
1412050a4f34SJens Axboe 	char *(*devnode)(struct gendisk *disk, umode_t *mode);
14139208d414SChristoph Hellwig 	/* returns the length of the identifier or a negative errno: */
14149208d414SChristoph Hellwig 	int (*get_unique_id)(struct gendisk *disk, u8 id[16],
14159208d414SChristoph Hellwig 			enum blk_unique_id id_type);
141608f85851SAl Viro 	struct module *owner;
1417bbd3e064SChristoph Hellwig 	const struct pr_ops *pr_ops;
14180bdfbca8SDmitry Osipenko 
14190bdfbca8SDmitry Osipenko 	/*
14200bdfbca8SDmitry Osipenko 	 * Special callback for probing GPT entry at a given sector.
14210bdfbca8SDmitry Osipenko 	 * Needed by Android devices, used by GPT scanner and MMC blk
14220bdfbca8SDmitry Osipenko 	 * driver.
14230bdfbca8SDmitry Osipenko 	 */
14240bdfbca8SDmitry Osipenko 	int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
142508f85851SAl Viro };
142608f85851SAl Viro 
1427ee6a129dSArnd Bergmann #ifdef CONFIG_COMPAT
142805bdb996SChristoph Hellwig extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
1429ee6a129dSArnd Bergmann 				      unsigned int, unsigned long);
1430ee6a129dSArnd Bergmann #else
1431ee6a129dSArnd Bergmann #define blkdev_compat_ptr_ioctl NULL
1432ee6a129dSArnd Bergmann #endif
1433ee6a129dSArnd Bergmann 
14340619317fSJens Axboe static inline void blk_wake_io_task(struct task_struct *waiter)
14350619317fSJens Axboe {
14360619317fSJens Axboe 	/*
14370619317fSJens Axboe 	 * If we're polling, the task itself is doing the completions. For
14380619317fSJens Axboe 	 * that case, we don't need to signal a wakeup, it's enough to just
14390619317fSJens Axboe 	 * mark us as RUNNING.
14400619317fSJens Axboe 	 */
14410619317fSJens Axboe 	if (waiter == current)
14420619317fSJens Axboe 		__set_current_state(TASK_RUNNING);
14430619317fSJens Axboe 	else
14440619317fSJens Axboe 		wake_up_process(waiter);
14450619317fSJens Axboe }
14460619317fSJens Axboe 
14475f275713SYu Kuai unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
14485f0614a5SMing Lei 				 unsigned long start_time);
144977e7ffd7SBart Van Assche void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
14505f275713SYu Kuai 		      unsigned int sectors, unsigned long start_time);
1451956d510eSChristoph Hellwig 
145299dfc43eSChristoph Hellwig unsigned long bio_start_io_acct(struct bio *bio);
145399dfc43eSChristoph Hellwig void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
145499dfc43eSChristoph Hellwig 		struct block_device *orig_bdev);
1455956d510eSChristoph Hellwig 
1456956d510eSChristoph Hellwig /**
1457956d510eSChristoph Hellwig  * bio_end_io_acct - end I/O accounting for bio based drivers
1458956d510eSChristoph Hellwig  * @bio:	bio to end account for
1459b42c1fc3SChristoph Hellwig  * @start_time:	start time returned by bio_start_io_acct()
1460956d510eSChristoph Hellwig  */
1461956d510eSChristoph Hellwig static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1462956d510eSChristoph Hellwig {
146399dfc43eSChristoph Hellwig 	return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1464956d510eSChristoph Hellwig }
1465956d510eSChristoph Hellwig 
14663f1266f1SChristoph Hellwig int bdev_read_only(struct block_device *bdev);
1467ead083aeSAl Viro int set_blocksize(struct file *file, int size);
14683f1266f1SChristoph Hellwig 
14694e7b5671SChristoph Hellwig int lookup_bdev(const char *pathname, dev_t *dev);
14703f1266f1SChristoph Hellwig 
14713f1266f1SChristoph Hellwig void blkdev_show(struct seq_file *seqf, off_t offset);
14723f1266f1SChristoph Hellwig 
14733f1266f1SChristoph Hellwig #define BDEVNAME_SIZE	32	/* Largest string for a blockdev identifier */
14743f1266f1SChristoph Hellwig #define BDEVT_SIZE	10	/* Largest string for MAJ:MIN for blkdev */
14753f1266f1SChristoph Hellwig #ifdef CONFIG_BLOCK
14763f1266f1SChristoph Hellwig #define BLKDEV_MAJOR_MAX	512
14773f1266f1SChristoph Hellwig #else
14783f1266f1SChristoph Hellwig #define BLKDEV_MAJOR_MAX	0
14791da177e4SLinus Torvalds #endif
14803f1266f1SChristoph Hellwig 
14810718afd4SChristoph Hellwig struct blk_holder_ops {
1482d8530de5SChristoph Hellwig 	void (*mark_dead)(struct block_device *bdev, bool surprise);
14832142b88cSChristoph Hellwig 
14842142b88cSChristoph Hellwig 	/*
14852142b88cSChristoph Hellwig 	 * Sync the file system mounted on the block device.
14862142b88cSChristoph Hellwig 	 */
14872142b88cSChristoph Hellwig 	void (*sync)(struct block_device *bdev);
1488a30561a9SChristian Brauner 
1489a30561a9SChristian Brauner 	/*
1490a30561a9SChristian Brauner 	 * Freeze the file system mounted on the block device.
1491a30561a9SChristian Brauner 	 */
1492a30561a9SChristian Brauner 	int (*freeze)(struct block_device *bdev);
1493a30561a9SChristian Brauner 
1494a30561a9SChristian Brauner 	/*
1495a30561a9SChristian Brauner 	 * Thaw the file system mounted on the block device.
1496a30561a9SChristian Brauner 	 */
1497a30561a9SChristian Brauner 	int (*thaw)(struct block_device *bdev);
14980718afd4SChristoph Hellwig };
14990718afd4SChristoph Hellwig 
1500e419cf3eSChristian Brauner /*
1501e419cf3eSChristian Brauner  * For filesystems using @fs_holder_ops, the @holder argument passed to
1502e419cf3eSChristian Brauner  * helpers used to open and claim block devices via
1503e419cf3eSChristian Brauner  * bd_prepare_to_claim() must point to a superblock.
1504e419cf3eSChristian Brauner  */
15057ecd0b6fSChristoph Hellwig extern const struct blk_holder_ops fs_holder_ops;
15067ecd0b6fSChristoph Hellwig 
15073f0b3e78SChristoph Hellwig /*
15083f0b3e78SChristoph Hellwig  * Return the correct open flags for blkdev_get_by_* for super block flags
15093f0b3e78SChristoph Hellwig  * as stored in sb->s_flags.
15103f0b3e78SChristoph Hellwig  */
15113f0b3e78SChristoph Hellwig #define sb_open_mode(flags) \
15126f861765SJan Kara 	(BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \
15136f861765SJan Kara 	 (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
15143f0b3e78SChristoph Hellwig 
1515f3a60882SChristian Brauner struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
1516f3a60882SChristian Brauner 		const struct blk_holder_ops *hops);
1517f3a60882SChristian Brauner struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,
1518f3a60882SChristian Brauner 		void *holder, const struct blk_holder_ops *hops);
15190718afd4SChristoph Hellwig int bd_prepare_to_claim(struct block_device *bdev, void *holder,
15200718afd4SChristoph Hellwig 		const struct blk_holder_ops *hops);
152137c3fc9aSChristoph Hellwig void bd_abort_claiming(struct block_device *bdev, void *holder);
15223f1266f1SChristoph Hellwig 
152322ae8ce8SChristoph Hellwig /* just for blk-cgroup, don't use elsewhere */
152422ae8ce8SChristoph Hellwig struct block_device *blkdev_get_no_open(dev_t dev);
152522ae8ce8SChristoph Hellwig void blkdev_put_no_open(struct block_device *bdev);
152622ae8ce8SChristoph Hellwig 
1527621c1f42SChristoph Hellwig struct block_device *I_BDEV(struct inode *inode);
1528f3a60882SChristian Brauner struct block_device *file_bdev(struct file *bdev_file);
1529*186ddac2SYu Kuai bool disk_live(struct gendisk *disk);
1530*186ddac2SYu Kuai unsigned int block_size(struct block_device *bdev);
15313f1266f1SChristoph Hellwig 
15323f1266f1SChristoph Hellwig #ifdef CONFIG_BLOCK
15333f1266f1SChristoph Hellwig void invalidate_bdev(struct block_device *bdev);
15343f1266f1SChristoph Hellwig int sync_blockdev(struct block_device *bdev);
153597d6fb1bSYuezhang Mo int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
153670164eb6SChristoph Hellwig int sync_blockdev_nowait(struct block_device *bdev);
15371e03a36bSChristoph Hellwig void sync_bdevs(bool wait);
15382d985f8cSEric Biggers void bdev_statx_dioalign(struct inode *inode, struct kstat *stat);
1539322cbb50SChristoph Hellwig void printk_all_partitions(void);
15402577f53fSChristoph Hellwig int __init early_lookup_bdev(const char *pathname, dev_t *dev);
15413f1266f1SChristoph Hellwig #else
15423f1266f1SChristoph Hellwig static inline void invalidate_bdev(struct block_device *bdev)
15433f1266f1SChristoph Hellwig {
15443f1266f1SChristoph Hellwig }
15453f1266f1SChristoph Hellwig static inline int sync_blockdev(struct block_device *bdev)
15463f1266f1SChristoph Hellwig {
15473f1266f1SChristoph Hellwig 	return 0;
15483f1266f1SChristoph Hellwig }
154970164eb6SChristoph Hellwig static inline int sync_blockdev_nowait(struct block_device *bdev)
155070164eb6SChristoph Hellwig {
155170164eb6SChristoph Hellwig 	return 0;
155270164eb6SChristoph Hellwig }
15531e03a36bSChristoph Hellwig static inline void sync_bdevs(bool wait)
15541e03a36bSChristoph Hellwig {
15551e03a36bSChristoph Hellwig }
15562d985f8cSEric Biggers static inline void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
15572d985f8cSEric Biggers {
15582d985f8cSEric Biggers }
1559322cbb50SChristoph Hellwig static inline void printk_all_partitions(void)
1560322cbb50SChristoph Hellwig {
1561322cbb50SChristoph Hellwig }
1562cf056a43SChristoph Hellwig static inline int early_lookup_bdev(const char *pathname, dev_t *dev)
1563cf056a43SChristoph Hellwig {
1564cf056a43SChristoph Hellwig 	return -EINVAL;
1565cf056a43SChristoph Hellwig }
1566322cbb50SChristoph Hellwig #endif /* CONFIG_BLOCK */
1567322cbb50SChristoph Hellwig 
1568982c3b30SChristian Brauner int bdev_freeze(struct block_device *bdev);
1569982c3b30SChristian Brauner int bdev_thaw(struct block_device *bdev);
157022650a99SChristian Brauner void bdev_fput(struct file *bdev_file);
15713f1266f1SChristoph Hellwig 
15725a72e899SJens Axboe struct io_comp_batch {
15735a72e899SJens Axboe 	struct request *req_list;
15745a72e899SJens Axboe 	bool need_ts;
15755a72e899SJens Axboe 	void (*complete)(struct io_comp_batch *);
15765a72e899SJens Axboe };
15775a72e899SJens Axboe 
15785a72e899SJens Axboe #define DEFINE_IO_COMP_BATCH(name)	struct io_comp_batch name = { }
15795a72e899SJens Axboe 
15803f1266f1SChristoph Hellwig #endif /* _LINUX_BLKDEV_H */
1581