xref: /linux-6.15/include/linux/blkdev.h (revision 2abd2c39)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Portions Copyright (C) 1992 Drew Eckhardt
4  */
5 #ifndef _LINUX_BLKDEV_H
6 #define _LINUX_BLKDEV_H
7 
8 #include <linux/types.h>
9 #include <linux/blk_types.h>
10 #include <linux/device.h>
11 #include <linux/list.h>
12 #include <linux/llist.h>
13 #include <linux/minmax.h>
14 #include <linux/timer.h>
15 #include <linux/workqueue.h>
16 #include <linux/wait.h>
17 #include <linux/bio.h>
18 #include <linux/gfp.h>
19 #include <linux/kdev_t.h>
20 #include <linux/rcupdate.h>
21 #include <linux/percpu-refcount.h>
22 #include <linux/blkzoned.h>
23 #include <linux/sched.h>
24 #include <linux/sbitmap.h>
25 #include <linux/uuid.h>
26 #include <linux/xarray.h>
27 
28 struct module;
29 struct request_queue;
30 struct elevator_queue;
31 struct blk_trace;
32 struct request;
33 struct sg_io_hdr;
34 struct blkcg_gq;
35 struct blk_flush_queue;
36 struct kiocb;
37 struct pr_ops;
38 struct rq_qos;
39 struct blk_queue_stats;
40 struct blk_stat_callback;
41 struct blk_crypto_profile;
42 
43 extern const struct device_type disk_type;
44 extern const struct device_type part_type;
45 extern struct class block_class;
46 
47 /*
48  * Maximum number of blkcg policies allowed to be registered concurrently.
49  * Defined here to simplify include dependency.
50  */
51 #define BLKCG_MAX_POLS		6
52 
53 #define DISK_MAX_PARTS			256
54 #define DISK_NAME_LEN			32
55 
56 #define PARTITION_META_INFO_VOLNAMELTH	64
57 /*
58  * Enough for the string representation of any kind of UUID plus NULL.
59  * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
60  */
61 #define PARTITION_META_INFO_UUIDLTH	(UUID_STRING_LEN + 1)
62 
63 struct partition_meta_info {
64 	char uuid[PARTITION_META_INFO_UUIDLTH];
65 	u8 volname[PARTITION_META_INFO_VOLNAMELTH];
66 };
67 
68 /**
69  * DOC: genhd capability flags
70  *
71  * ``GENHD_FL_REMOVABLE``: indicates that the block device gives access to
72  * removable media.  When set, the device remains present even when media is not
73  * inserted.  Shall not be set for devices which are removed entirely when the
74  * media is removed.
75  *
76  * ``GENHD_FL_HIDDEN``: the block device is hidden; it doesn't produce events,
77  * doesn't appear in sysfs, and can't be opened from userspace or using
78  * blkdev_get*. Used for the underlying components of multipath devices.
79  *
80  * ``GENHD_FL_NO_PART``: partition support is disabled.  The kernel will not
81  * scan for partitions from add_disk, and users can't add partitions manually.
82  *
83  */
84 enum {
85 	GENHD_FL_REMOVABLE			= 1 << 0,
86 	GENHD_FL_HIDDEN				= 1 << 1,
87 	GENHD_FL_NO_PART			= 1 << 2,
88 };
89 
90 enum {
91 	DISK_EVENT_MEDIA_CHANGE			= 1 << 0, /* media changed */
92 	DISK_EVENT_EJECT_REQUEST		= 1 << 1, /* eject requested */
93 };
94 
95 enum {
96 	/* Poll even if events_poll_msecs is unset */
97 	DISK_EVENT_FLAG_POLL			= 1 << 0,
98 	/* Forward events to udev */
99 	DISK_EVENT_FLAG_UEVENT			= 1 << 1,
100 	/* Block event polling when open for exclusive write */
101 	DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE	= 1 << 2,
102 };
103 
104 struct disk_events;
105 struct badblocks;
106 
107 struct blk_integrity {
108 	const struct blk_integrity_profile	*profile;
109 	unsigned char				flags;
110 	unsigned char				tuple_size;
111 	unsigned char				interval_exp;
112 	unsigned char				tag_size;
113 };
114 
115 typedef unsigned int __bitwise blk_mode_t;
116 
117 /* open for reading */
118 #define BLK_OPEN_READ		((__force blk_mode_t)(1 << 0))
119 /* open for writing */
120 #define BLK_OPEN_WRITE		((__force blk_mode_t)(1 << 1))
121 /* open exclusively (vs other exclusive openers */
122 #define BLK_OPEN_EXCL		((__force blk_mode_t)(1 << 2))
123 /* opened with O_NDELAY */
124 #define BLK_OPEN_NDELAY		((__force blk_mode_t)(1 << 3))
125 /* open for "writes" only for ioctls (specialy hack for floppy.c) */
126 #define BLK_OPEN_WRITE_IOCTL	((__force blk_mode_t)(1 << 4))
127 
128 struct gendisk {
129 	/*
130 	 * major/first_minor/minors should not be set by any new driver, the
131 	 * block core will take care of allocating them automatically.
132 	 */
133 	int major;
134 	int first_minor;
135 	int minors;
136 
137 	char disk_name[DISK_NAME_LEN];	/* name of major driver */
138 
139 	unsigned short events;		/* supported events */
140 	unsigned short event_flags;	/* flags related to event processing */
141 
142 	struct xarray part_tbl;
143 	struct block_device *part0;
144 
145 	const struct block_device_operations *fops;
146 	struct request_queue *queue;
147 	void *private_data;
148 
149 	struct bio_set bio_split;
150 
151 	int flags;
152 	unsigned long state;
153 #define GD_NEED_PART_SCAN		0
154 #define GD_READ_ONLY			1
155 #define GD_DEAD				2
156 #define GD_NATIVE_CAPACITY		3
157 #define GD_ADDED			4
158 #define GD_SUPPRESS_PART_SCAN		5
159 #define GD_OWNS_QUEUE			6
160 
161 	struct mutex open_mutex;	/* open/close mutex */
162 	unsigned open_partitions;	/* number of open partitions */
163 
164 	struct backing_dev_info	*bdi;
165 	struct kobject queue_kobj;	/* the queue/ directory */
166 	struct kobject *slave_dir;
167 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
168 	struct list_head slave_bdevs;
169 #endif
170 	struct timer_rand_state *random;
171 	atomic_t sync_io;		/* RAID */
172 	struct disk_events *ev;
173 
174 #ifdef CONFIG_BLK_DEV_ZONED
175 	/*
176 	 * Zoned block device information for request dispatch control.
177 	 * nr_zones is the total number of zones of the device. This is always
178 	 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
179 	 * bits which indicates if a zone is conventional (bit set) or
180 	 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
181 	 * bits which indicates if a zone is write locked, that is, if a write
182 	 * request targeting the zone was dispatched.
183 	 *
184 	 * Reads of this information must be protected with blk_queue_enter() /
185 	 * blk_queue_exit(). Modifying this information is only allowed while
186 	 * no requests are being processed. See also blk_mq_freeze_queue() and
187 	 * blk_mq_unfreeze_queue().
188 	 */
189 	unsigned int		nr_zones;
190 	unsigned int		max_open_zones;
191 	unsigned int		max_active_zones;
192 	unsigned long		*conv_zones_bitmap;
193 	unsigned long		*seq_zones_wlock;
194 #endif /* CONFIG_BLK_DEV_ZONED */
195 
196 #if IS_ENABLED(CONFIG_CDROM)
197 	struct cdrom_device_info *cdi;
198 #endif
199 	int node_id;
200 	struct badblocks *bb;
201 	struct lockdep_map lockdep_map;
202 	u64 diskseq;
203 	blk_mode_t open_mode;
204 
205 	/*
206 	 * Independent sector access ranges. This is always NULL for
207 	 * devices that do not have multiple independent access ranges.
208 	 */
209 	struct blk_independent_access_ranges *ia_ranges;
210 };
211 
212 static inline bool disk_live(struct gendisk *disk)
213 {
214 	return !inode_unhashed(disk->part0->bd_inode);
215 }
216 
217 /**
218  * disk_openers - returns how many openers are there for a disk
219  * @disk: disk to check
220  *
221  * This returns the number of openers for a disk.  Note that this value is only
222  * stable if disk->open_mutex is held.
223  *
224  * Note: Due to a quirk in the block layer open code, each open partition is
225  * only counted once even if there are multiple openers.
226  */
227 static inline unsigned int disk_openers(struct gendisk *disk)
228 {
229 	return atomic_read(&disk->part0->bd_openers);
230 }
231 
232 /*
233  * The gendisk is refcounted by the part0 block_device, and the bd_device
234  * therein is also used for device model presentation in sysfs.
235  */
236 #define dev_to_disk(device) \
237 	(dev_to_bdev(device)->bd_disk)
238 #define disk_to_dev(disk) \
239 	(&((disk)->part0->bd_device))
240 
241 #if IS_REACHABLE(CONFIG_CDROM)
242 #define disk_to_cdi(disk)	((disk)->cdi)
243 #else
244 #define disk_to_cdi(disk)	NULL
245 #endif
246 
247 static inline dev_t disk_devt(struct gendisk *disk)
248 {
249 	return MKDEV(disk->major, disk->first_minor);
250 }
251 
252 static inline int blk_validate_block_size(unsigned long bsize)
253 {
254 	if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
255 		return -EINVAL;
256 
257 	return 0;
258 }
259 
260 static inline bool blk_op_is_passthrough(blk_opf_t op)
261 {
262 	op &= REQ_OP_MASK;
263 	return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
264 }
265 
266 /*
267  * BLK_BOUNCE_NONE:	never bounce (default)
268  * BLK_BOUNCE_HIGH:	bounce all highmem pages
269  */
270 enum blk_bounce {
271 	BLK_BOUNCE_NONE,
272 	BLK_BOUNCE_HIGH,
273 };
274 
275 struct queue_limits {
276 	enum blk_bounce		bounce;
277 	unsigned long		seg_boundary_mask;
278 	unsigned long		virt_boundary_mask;
279 
280 	unsigned int		max_hw_sectors;
281 	unsigned int		max_dev_sectors;
282 	unsigned int		chunk_sectors;
283 	unsigned int		max_sectors;
284 	unsigned int		max_user_sectors;
285 	unsigned int		max_segment_size;
286 	unsigned int		physical_block_size;
287 	unsigned int		logical_block_size;
288 	unsigned int		alignment_offset;
289 	unsigned int		io_min;
290 	unsigned int		io_opt;
291 	unsigned int		max_discard_sectors;
292 	unsigned int		max_hw_discard_sectors;
293 	unsigned int		max_secure_erase_sectors;
294 	unsigned int		max_write_zeroes_sectors;
295 	unsigned int		max_zone_append_sectors;
296 	unsigned int		discard_granularity;
297 	unsigned int		discard_alignment;
298 	unsigned int		zone_write_granularity;
299 
300 	unsigned short		max_segments;
301 	unsigned short		max_integrity_segments;
302 	unsigned short		max_discard_segments;
303 
304 	unsigned char		misaligned;
305 	unsigned char		discard_misaligned;
306 	unsigned char		raid_partial_stripes_expensive;
307 	bool			zoned;
308 
309 	/*
310 	 * Drivers that set dma_alignment to less than 511 must be prepared to
311 	 * handle individual bvec's that are not a multiple of a SECTOR_SIZE
312 	 * due to possible offsets.
313 	 */
314 	unsigned int		dma_alignment;
315 };
316 
317 typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
318 			       void *data);
319 
320 void disk_set_zoned(struct gendisk *disk);
321 void disk_clear_zoned(struct gendisk *disk);
322 
323 #define BLK_ALL_ZONES  ((unsigned int)-1)
324 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
325 		unsigned int nr_zones, report_zones_cb cb, void *data);
326 int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
327 		sector_t sectors, sector_t nr_sectors, gfp_t gfp_mask);
328 int blk_revalidate_disk_zones(struct gendisk *disk,
329 		void (*update_driver_data)(struct gendisk *disk));
330 
331 /*
332  * Independent access ranges: struct blk_independent_access_range describes
333  * a range of contiguous sectors that can be accessed using device command
334  * execution resources that are independent from the resources used for
335  * other access ranges. This is typically found with single-LUN multi-actuator
336  * HDDs where each access range is served by a different set of heads.
337  * The set of independent ranges supported by the device is defined using
338  * struct blk_independent_access_ranges. The independent ranges must not overlap
339  * and must include all sectors within the disk capacity (no sector holes
340  * allowed).
341  * For a device with multiple ranges, requests targeting sectors in different
342  * ranges can be executed in parallel. A request can straddle an access range
343  * boundary.
344  */
345 struct blk_independent_access_range {
346 	struct kobject		kobj;
347 	sector_t		sector;
348 	sector_t		nr_sectors;
349 };
350 
351 struct blk_independent_access_ranges {
352 	struct kobject				kobj;
353 	bool					sysfs_registered;
354 	unsigned int				nr_ia_ranges;
355 	struct blk_independent_access_range	ia_range[];
356 };
357 
358 struct request_queue {
359 	/*
360 	 * The queue owner gets to use this for whatever they like.
361 	 * ll_rw_blk doesn't touch it.
362 	 */
363 	void			*queuedata;
364 
365 	struct elevator_queue	*elevator;
366 
367 	const struct blk_mq_ops	*mq_ops;
368 
369 	/* sw queues */
370 	struct blk_mq_ctx __percpu	*queue_ctx;
371 
372 	/*
373 	 * various queue flags, see QUEUE_* below
374 	 */
375 	unsigned long		queue_flags;
376 
377 	unsigned int		rq_timeout;
378 
379 	unsigned int		queue_depth;
380 
381 	refcount_t		refs;
382 
383 	/* hw dispatch queues */
384 	unsigned int		nr_hw_queues;
385 	struct xarray		hctx_table;
386 
387 	struct percpu_ref	q_usage_counter;
388 
389 	struct request		*last_merge;
390 
391 	spinlock_t		queue_lock;
392 
393 	int			quiesce_depth;
394 
395 	struct gendisk		*disk;
396 
397 	/*
398 	 * mq queue kobject
399 	 */
400 	struct kobject *mq_kobj;
401 
402 	struct queue_limits	limits;
403 
404 #ifdef  CONFIG_BLK_DEV_INTEGRITY
405 	struct blk_integrity integrity;
406 #endif	/* CONFIG_BLK_DEV_INTEGRITY */
407 
408 #ifdef CONFIG_PM
409 	struct device		*dev;
410 	enum rpm_status		rpm_status;
411 #endif
412 
413 	/*
414 	 * Number of contexts that have called blk_set_pm_only(). If this
415 	 * counter is above zero then only RQF_PM requests are processed.
416 	 */
417 	atomic_t		pm_only;
418 
419 	struct blk_queue_stats	*stats;
420 	struct rq_qos		*rq_qos;
421 	struct mutex		rq_qos_mutex;
422 
423 	/*
424 	 * ida allocated id for this queue.  Used to index queues from
425 	 * ioctx.
426 	 */
427 	int			id;
428 
429 	unsigned int		dma_pad_mask;
430 
431 	/*
432 	 * queue settings
433 	 */
434 	unsigned long		nr_requests;	/* Max # of requests */
435 
436 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
437 	struct blk_crypto_profile *crypto_profile;
438 	struct kobject *crypto_kobject;
439 #endif
440 
441 	struct timer_list	timeout;
442 	struct work_struct	timeout_work;
443 
444 	atomic_t		nr_active_requests_shared_tags;
445 
446 	unsigned int		required_elevator_features;
447 
448 	struct blk_mq_tags	*sched_shared_tags;
449 
450 	struct list_head	icq_list;
451 #ifdef CONFIG_BLK_CGROUP
452 	DECLARE_BITMAP		(blkcg_pols, BLKCG_MAX_POLS);
453 	struct blkcg_gq		*root_blkg;
454 	struct list_head	blkg_list;
455 	struct mutex		blkcg_mutex;
456 #endif
457 
458 	int			node;
459 
460 	spinlock_t		requeue_lock;
461 	struct list_head	requeue_list;
462 	struct delayed_work	requeue_work;
463 
464 #ifdef CONFIG_BLK_DEV_IO_TRACE
465 	struct blk_trace __rcu	*blk_trace;
466 #endif
467 	/*
468 	 * for flush operations
469 	 */
470 	struct blk_flush_queue	*fq;
471 	struct list_head	flush_list;
472 
473 	struct mutex		sysfs_lock;
474 	struct mutex		sysfs_dir_lock;
475 
476 	/*
477 	 * for reusing dead hctx instance in case of updating
478 	 * nr_hw_queues
479 	 */
480 	struct list_head	unused_hctx_list;
481 	spinlock_t		unused_hctx_lock;
482 
483 	int			mq_freeze_depth;
484 
485 #ifdef CONFIG_BLK_DEV_THROTTLING
486 	/* Throttle data */
487 	struct throtl_data *td;
488 #endif
489 	struct rcu_head		rcu_head;
490 	wait_queue_head_t	mq_freeze_wq;
491 	/*
492 	 * Protect concurrent access to q_usage_counter by
493 	 * percpu_ref_kill() and percpu_ref_reinit().
494 	 */
495 	struct mutex		mq_freeze_lock;
496 
497 	struct blk_mq_tag_set	*tag_set;
498 	struct list_head	tag_set_list;
499 
500 	struct dentry		*debugfs_dir;
501 	struct dentry		*sched_debugfs_dir;
502 	struct dentry		*rqos_debugfs_dir;
503 	/*
504 	 * Serializes all debugfs metadata operations using the above dentries.
505 	 */
506 	struct mutex		debugfs_mutex;
507 
508 	bool			mq_sysfs_init_done;
509 };
510 
511 /* Keep blk_queue_flag_name[] in sync with the definitions below */
512 #define QUEUE_FLAG_STOPPED	0	/* queue is stopped */
513 #define QUEUE_FLAG_DYING	1	/* queue being torn down */
514 #define QUEUE_FLAG_NOMERGES     3	/* disable merge attempts */
515 #define QUEUE_FLAG_SAME_COMP	4	/* complete on same CPU-group */
516 #define QUEUE_FLAG_FAIL_IO	5	/* fake timeout */
517 #define QUEUE_FLAG_NONROT	6	/* non-rotational device (SSD) */
518 #define QUEUE_FLAG_VIRT		QUEUE_FLAG_NONROT /* paravirt device */
519 #define QUEUE_FLAG_IO_STAT	7	/* do disk/partitions IO accounting */
520 #define QUEUE_FLAG_NOXMERGES	9	/* No extended merges */
521 #define QUEUE_FLAG_ADD_RANDOM	10	/* Contributes to random pool */
522 #define QUEUE_FLAG_SYNCHRONOUS	11	/* always completes in submit context */
523 #define QUEUE_FLAG_SAME_FORCE	12	/* force complete on same CPU */
524 #define QUEUE_FLAG_HW_WC	18	/* Write back caching supported */
525 #define QUEUE_FLAG_INIT_DONE	14	/* queue is initialized */
526 #define QUEUE_FLAG_STABLE_WRITES 15	/* don't modify blks until WB is done */
527 #define QUEUE_FLAG_POLL		16	/* IO polling enabled if set */
528 #define QUEUE_FLAG_WC		17	/* Write back caching */
529 #define QUEUE_FLAG_FUA		18	/* device supports FUA writes */
530 #define QUEUE_FLAG_DAX		19	/* device supports DAX */
531 #define QUEUE_FLAG_STATS	20	/* track IO start and completion times */
532 #define QUEUE_FLAG_REGISTERED	22	/* queue has been registered to a disk */
533 #define QUEUE_FLAG_QUIESCED	24	/* queue has been quiesced */
534 #define QUEUE_FLAG_PCI_P2PDMA	25	/* device supports PCI p2p requests */
535 #define QUEUE_FLAG_ZONE_RESETALL 26	/* supports Zone Reset All */
536 #define QUEUE_FLAG_RQ_ALLOC_TIME 27	/* record rq->alloc_time_ns */
537 #define QUEUE_FLAG_HCTX_ACTIVE	28	/* at least one blk-mq hctx is active */
538 #define QUEUE_FLAG_NOWAIT       29	/* device supports NOWAIT */
539 #define QUEUE_FLAG_SQ_SCHED     30	/* single queue style io dispatch */
540 #define QUEUE_FLAG_SKIP_TAGSET_QUIESCE	31 /* quiesce_tagset skip the queue*/
541 
542 #define QUEUE_FLAG_MQ_DEFAULT	((1UL << QUEUE_FLAG_IO_STAT) |		\
543 				 (1UL << QUEUE_FLAG_SAME_COMP) |	\
544 				 (1UL << QUEUE_FLAG_NOWAIT))
545 
546 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
547 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
548 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
549 
550 #define blk_queue_stopped(q)	test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
551 #define blk_queue_dying(q)	test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
552 #define blk_queue_init_done(q)	test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
553 #define blk_queue_nomerges(q)	test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
554 #define blk_queue_noxmerges(q)	\
555 	test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
556 #define blk_queue_nonrot(q)	test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
557 #define blk_queue_stable_writes(q) \
558 	test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
559 #define blk_queue_io_stat(q)	test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
560 #define blk_queue_add_random(q)	test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
561 #define blk_queue_zone_resetall(q)	\
562 	test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
563 #define blk_queue_dax(q)	test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
564 #define blk_queue_pci_p2pdma(q)	\
565 	test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
566 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
567 #define blk_queue_rq_alloc_time(q)	\
568 	test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
569 #else
570 #define blk_queue_rq_alloc_time(q)	false
571 #endif
572 
573 #define blk_noretry_request(rq) \
574 	((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
575 			     REQ_FAILFAST_DRIVER))
576 #define blk_queue_quiesced(q)	test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
577 #define blk_queue_pm_only(q)	atomic_read(&(q)->pm_only)
578 #define blk_queue_registered(q)	test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
579 #define blk_queue_sq_sched(q)	test_bit(QUEUE_FLAG_SQ_SCHED, &(q)->queue_flags)
580 #define blk_queue_skip_tagset_quiesce(q) \
581 	test_bit(QUEUE_FLAG_SKIP_TAGSET_QUIESCE, &(q)->queue_flags)
582 
583 extern void blk_set_pm_only(struct request_queue *q);
584 extern void blk_clear_pm_only(struct request_queue *q);
585 
586 #define list_entry_rq(ptr)	list_entry((ptr), struct request, queuelist)
587 
588 #define dma_map_bvec(dev, bv, dir, attrs) \
589 	dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
590 	(dir), (attrs))
591 
592 static inline bool queue_is_mq(struct request_queue *q)
593 {
594 	return q->mq_ops;
595 }
596 
597 #ifdef CONFIG_PM
598 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
599 {
600 	return q->rpm_status;
601 }
602 #else
603 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
604 {
605 	return RPM_ACTIVE;
606 }
607 #endif
608 
609 static inline bool blk_queue_is_zoned(struct request_queue *q)
610 {
611 	return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && q->limits.zoned;
612 }
613 
614 #ifdef CONFIG_BLK_DEV_ZONED
615 unsigned int bdev_nr_zones(struct block_device *bdev);
616 
617 static inline unsigned int disk_nr_zones(struct gendisk *disk)
618 {
619 	return blk_queue_is_zoned(disk->queue) ? disk->nr_zones : 0;
620 }
621 
622 static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
623 {
624 	if (!blk_queue_is_zoned(disk->queue))
625 		return 0;
626 	return sector >> ilog2(disk->queue->limits.chunk_sectors);
627 }
628 
629 static inline bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
630 {
631 	if (!blk_queue_is_zoned(disk->queue))
632 		return false;
633 	if (!disk->conv_zones_bitmap)
634 		return true;
635 	return !test_bit(disk_zone_no(disk, sector), disk->conv_zones_bitmap);
636 }
637 
638 static inline void disk_set_max_open_zones(struct gendisk *disk,
639 		unsigned int max_open_zones)
640 {
641 	disk->max_open_zones = max_open_zones;
642 }
643 
644 static inline void disk_set_max_active_zones(struct gendisk *disk,
645 		unsigned int max_active_zones)
646 {
647 	disk->max_active_zones = max_active_zones;
648 }
649 
650 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
651 {
652 	return bdev->bd_disk->max_open_zones;
653 }
654 
655 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
656 {
657 	return bdev->bd_disk->max_active_zones;
658 }
659 
660 #else /* CONFIG_BLK_DEV_ZONED */
661 static inline unsigned int bdev_nr_zones(struct block_device *bdev)
662 {
663 	return 0;
664 }
665 
666 static inline unsigned int disk_nr_zones(struct gendisk *disk)
667 {
668 	return 0;
669 }
670 static inline bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
671 {
672 	return false;
673 }
674 static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
675 {
676 	return 0;
677 }
678 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
679 {
680 	return 0;
681 }
682 
683 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
684 {
685 	return 0;
686 }
687 #endif /* CONFIG_BLK_DEV_ZONED */
688 
689 static inline unsigned int blk_queue_depth(struct request_queue *q)
690 {
691 	if (q->queue_depth)
692 		return q->queue_depth;
693 
694 	return q->nr_requests;
695 }
696 
697 /*
698  * default timeout for SG_IO if none specified
699  */
700 #define BLK_DEFAULT_SG_TIMEOUT	(60 * HZ)
701 #define BLK_MIN_SG_TIMEOUT	(7 * HZ)
702 
703 /* This should not be used directly - use rq_for_each_segment */
704 #define for_each_bio(_bio)		\
705 	for (; _bio; _bio = _bio->bi_next)
706 
707 int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
708 				 const struct attribute_group **groups);
709 static inline int __must_check add_disk(struct gendisk *disk)
710 {
711 	return device_add_disk(NULL, disk, NULL);
712 }
713 void del_gendisk(struct gendisk *gp);
714 void invalidate_disk(struct gendisk *disk);
715 void set_disk_ro(struct gendisk *disk, bool read_only);
716 void disk_uevent(struct gendisk *disk, enum kobject_action action);
717 
718 static inline int get_disk_ro(struct gendisk *disk)
719 {
720 	return disk->part0->bd_read_only ||
721 		test_bit(GD_READ_ONLY, &disk->state);
722 }
723 
724 static inline int bdev_read_only(struct block_device *bdev)
725 {
726 	return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
727 }
728 
729 bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
730 void disk_force_media_change(struct gendisk *disk);
731 void bdev_mark_dead(struct block_device *bdev, bool surprise);
732 
733 void add_disk_randomness(struct gendisk *disk) __latent_entropy;
734 void rand_initialize_disk(struct gendisk *disk);
735 
736 static inline sector_t get_start_sect(struct block_device *bdev)
737 {
738 	return bdev->bd_start_sect;
739 }
740 
741 static inline sector_t bdev_nr_sectors(struct block_device *bdev)
742 {
743 	return bdev->bd_nr_sectors;
744 }
745 
746 static inline loff_t bdev_nr_bytes(struct block_device *bdev)
747 {
748 	return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT;
749 }
750 
751 static inline sector_t get_capacity(struct gendisk *disk)
752 {
753 	return bdev_nr_sectors(disk->part0);
754 }
755 
756 static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
757 {
758 	return bdev_nr_sectors(sb->s_bdev) >>
759 		(sb->s_blocksize_bits - SECTOR_SHIFT);
760 }
761 
762 int bdev_disk_changed(struct gendisk *disk, bool invalidate);
763 
764 void put_disk(struct gendisk *disk);
765 struct gendisk *__blk_alloc_disk(int node, struct lock_class_key *lkclass);
766 
767 /**
768  * blk_alloc_disk - allocate a gendisk structure
769  * @node_id: numa node to allocate on
770  *
771  * Allocate and pre-initialize a gendisk structure for use with BIO based
772  * drivers.
773  *
774  * Context: can sleep
775  */
776 #define blk_alloc_disk(node_id)						\
777 ({									\
778 	static struct lock_class_key __key;				\
779 									\
780 	__blk_alloc_disk(node_id, &__key);				\
781 })
782 
783 int __register_blkdev(unsigned int major, const char *name,
784 		void (*probe)(dev_t devt));
785 #define register_blkdev(major, name) \
786 	__register_blkdev(major, name, NULL)
787 void unregister_blkdev(unsigned int major, const char *name);
788 
789 bool disk_check_media_change(struct gendisk *disk);
790 void set_capacity(struct gendisk *disk, sector_t size);
791 
792 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
793 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
794 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
795 #else
796 static inline int bd_link_disk_holder(struct block_device *bdev,
797 				      struct gendisk *disk)
798 {
799 	return 0;
800 }
801 static inline void bd_unlink_disk_holder(struct block_device *bdev,
802 					 struct gendisk *disk)
803 {
804 }
805 #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
806 
807 dev_t part_devt(struct gendisk *disk, u8 partno);
808 void inc_diskseq(struct gendisk *disk);
809 void blk_request_module(dev_t devt);
810 
811 extern int blk_register_queue(struct gendisk *disk);
812 extern void blk_unregister_queue(struct gendisk *disk);
813 void submit_bio_noacct(struct bio *bio);
814 struct bio *bio_split_to_limits(struct bio *bio);
815 
816 extern int blk_lld_busy(struct request_queue *q);
817 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
818 extern void blk_queue_exit(struct request_queue *q);
819 extern void blk_sync_queue(struct request_queue *q);
820 
821 /* Helper to convert REQ_OP_XXX to its string format XXX */
822 extern const char *blk_op_str(enum req_op op);
823 
824 int blk_status_to_errno(blk_status_t status);
825 blk_status_t errno_to_blk_status(int errno);
826 const char *blk_status_to_str(blk_status_t status);
827 
828 /* only poll the hardware once, don't continue until a completion was found */
829 #define BLK_POLL_ONESHOT		(1 << 0)
830 int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
831 int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
832 			unsigned int flags);
833 
834 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
835 {
836 	return bdev->bd_queue;	/* this is never NULL */
837 }
838 
839 /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
840 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
841 
842 static inline unsigned int bio_zone_no(struct bio *bio)
843 {
844 	return disk_zone_no(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
845 }
846 
847 static inline unsigned int bio_zone_is_seq(struct bio *bio)
848 {
849 	return disk_zone_is_seq(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
850 }
851 
852 /*
853  * Return how much of the chunk is left to be used for I/O at a given offset.
854  */
855 static inline unsigned int blk_chunk_sectors_left(sector_t offset,
856 		unsigned int chunk_sectors)
857 {
858 	if (unlikely(!is_power_of_2(chunk_sectors)))
859 		return chunk_sectors - sector_div(offset, chunk_sectors);
860 	return chunk_sectors - (offset & (chunk_sectors - 1));
861 }
862 
863 /*
864  * Access functions for manipulating queue properties
865  */
866 void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
867 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
868 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
869 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
870 extern void blk_queue_max_discard_segments(struct request_queue *,
871 		unsigned short);
872 void blk_queue_max_secure_erase_sectors(struct request_queue *q,
873 		unsigned int max_sectors);
874 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
875 extern void blk_queue_max_discard_sectors(struct request_queue *q,
876 		unsigned int max_discard_sectors);
877 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
878 		unsigned int max_write_same_sectors);
879 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
880 extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
881 		unsigned int max_zone_append_sectors);
882 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
883 void blk_queue_zone_write_granularity(struct request_queue *q,
884 				      unsigned int size);
885 extern void blk_queue_alignment_offset(struct request_queue *q,
886 				       unsigned int alignment);
887 void disk_update_readahead(struct gendisk *disk);
888 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
889 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
890 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
891 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
892 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
893 extern void blk_set_stacking_limits(struct queue_limits *lim);
894 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
895 			    sector_t offset);
896 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
897 			      sector_t offset);
898 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
899 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
900 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
901 extern void blk_queue_dma_alignment(struct request_queue *, int);
902 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
903 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
904 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
905 
906 struct blk_independent_access_ranges *
907 disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges);
908 void disk_set_independent_access_ranges(struct gendisk *disk,
909 				struct blk_independent_access_ranges *iars);
910 
911 /*
912  * Elevator features for blk_queue_required_elevator_features:
913  */
914 /* Supports zoned block devices sequential write constraint */
915 #define ELEVATOR_F_ZBD_SEQ_WRITE	(1U << 0)
916 
917 extern void blk_queue_required_elevator_features(struct request_queue *q,
918 						 unsigned int features);
919 extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
920 					      struct device *dev);
921 
922 bool __must_check blk_get_queue(struct request_queue *);
923 extern void blk_put_queue(struct request_queue *);
924 
925 void blk_mark_disk_dead(struct gendisk *disk);
926 
927 #ifdef CONFIG_BLOCK
928 /*
929  * blk_plug permits building a queue of related requests by holding the I/O
930  * fragments for a short period. This allows merging of sequential requests
931  * into single larger request. As the requests are moved from a per-task list to
932  * the device's request_queue in a batch, this results in improved scalability
933  * as the lock contention for request_queue lock is reduced.
934  *
935  * It is ok not to disable preemption when adding the request to the plug list
936  * or when attempting a merge. For details, please see schedule() where
937  * blk_flush_plug() is called.
938  */
939 struct blk_plug {
940 	struct request *mq_list; /* blk-mq requests */
941 
942 	/* if ios_left is > 1, we can batch tag/rq allocations */
943 	struct request *cached_rq;
944 	unsigned short nr_ios;
945 
946 	unsigned short rq_count;
947 
948 	bool multiple_queues;
949 	bool has_elevator;
950 
951 	struct list_head cb_list; /* md requires an unplug callback */
952 };
953 
954 struct blk_plug_cb;
955 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
956 struct blk_plug_cb {
957 	struct list_head list;
958 	blk_plug_cb_fn callback;
959 	void *data;
960 };
961 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
962 					     void *data, int size);
963 extern void blk_start_plug(struct blk_plug *);
964 extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
965 extern void blk_finish_plug(struct blk_plug *);
966 
967 void __blk_flush_plug(struct blk_plug *plug, bool from_schedule);
968 static inline void blk_flush_plug(struct blk_plug *plug, bool async)
969 {
970 	if (plug)
971 		__blk_flush_plug(plug, async);
972 }
973 
974 int blkdev_issue_flush(struct block_device *bdev);
975 long nr_blockdev_pages(void);
976 #else /* CONFIG_BLOCK */
977 struct blk_plug {
978 };
979 
980 static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
981 					 unsigned short nr_ios)
982 {
983 }
984 
985 static inline void blk_start_plug(struct blk_plug *plug)
986 {
987 }
988 
989 static inline void blk_finish_plug(struct blk_plug *plug)
990 {
991 }
992 
993 static inline void blk_flush_plug(struct blk_plug *plug, bool async)
994 {
995 }
996 
997 static inline int blkdev_issue_flush(struct block_device *bdev)
998 {
999 	return 0;
1000 }
1001 
1002 static inline long nr_blockdev_pages(void)
1003 {
1004 	return 0;
1005 }
1006 #endif /* CONFIG_BLOCK */
1007 
1008 extern void blk_io_schedule(void);
1009 
1010 int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1011 		sector_t nr_sects, gfp_t gfp_mask);
1012 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1013 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);
1014 int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
1015 		sector_t nr_sects, gfp_t gfp);
1016 
1017 #define BLKDEV_ZERO_NOUNMAP	(1 << 0)  /* do not free blocks */
1018 #define BLKDEV_ZERO_NOFALLBACK	(1 << 1)  /* don't write explicit zeroes */
1019 
1020 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1021 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1022 		unsigned flags);
1023 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1024 		sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1025 
1026 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1027 		sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1028 {
1029 	return blkdev_issue_discard(sb->s_bdev,
1030 				    block << (sb->s_blocksize_bits -
1031 					      SECTOR_SHIFT),
1032 				    nr_blocks << (sb->s_blocksize_bits -
1033 						  SECTOR_SHIFT),
1034 				    gfp_mask);
1035 }
1036 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1037 		sector_t nr_blocks, gfp_t gfp_mask)
1038 {
1039 	return blkdev_issue_zeroout(sb->s_bdev,
1040 				    block << (sb->s_blocksize_bits -
1041 					      SECTOR_SHIFT),
1042 				    nr_blocks << (sb->s_blocksize_bits -
1043 						  SECTOR_SHIFT),
1044 				    gfp_mask, 0);
1045 }
1046 
1047 static inline bool bdev_is_partition(struct block_device *bdev)
1048 {
1049 	return bdev->bd_partno;
1050 }
1051 
1052 enum blk_default_limits {
1053 	BLK_MAX_SEGMENTS	= 128,
1054 	BLK_SAFE_MAX_SECTORS	= 255,
1055 	BLK_MAX_SEGMENT_SIZE	= 65536,
1056 	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
1057 };
1058 
1059 /*
1060  * Default upper limit for the software max_sectors limit used for
1061  * regular file system I/O.  This can be increased through sysfs.
1062  *
1063  * Not to be confused with the max_hw_sector limit that is entirely
1064  * controlled by the driver, usually based on hardware limits.
1065  */
1066 #define BLK_DEF_MAX_SECTORS_CAP	2560u
1067 
1068 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1069 {
1070 	return q->limits.seg_boundary_mask;
1071 }
1072 
1073 static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1074 {
1075 	return q->limits.virt_boundary_mask;
1076 }
1077 
1078 static inline unsigned int queue_max_sectors(const struct request_queue *q)
1079 {
1080 	return q->limits.max_sectors;
1081 }
1082 
1083 static inline unsigned int queue_max_bytes(struct request_queue *q)
1084 {
1085 	return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1086 }
1087 
1088 static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1089 {
1090 	return q->limits.max_hw_sectors;
1091 }
1092 
1093 static inline unsigned short queue_max_segments(const struct request_queue *q)
1094 {
1095 	return q->limits.max_segments;
1096 }
1097 
1098 static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1099 {
1100 	return q->limits.max_discard_segments;
1101 }
1102 
1103 static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1104 {
1105 	return q->limits.max_segment_size;
1106 }
1107 
1108 static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
1109 {
1110 
1111 	const struct queue_limits *l = &q->limits;
1112 
1113 	return min(l->max_zone_append_sectors, l->max_sectors);
1114 }
1115 
1116 static inline unsigned int
1117 bdev_max_zone_append_sectors(struct block_device *bdev)
1118 {
1119 	return queue_max_zone_append_sectors(bdev_get_queue(bdev));
1120 }
1121 
1122 static inline unsigned int bdev_max_segments(struct block_device *bdev)
1123 {
1124 	return queue_max_segments(bdev_get_queue(bdev));
1125 }
1126 
1127 static inline unsigned queue_logical_block_size(const struct request_queue *q)
1128 {
1129 	int retval = 512;
1130 
1131 	if (q && q->limits.logical_block_size)
1132 		retval = q->limits.logical_block_size;
1133 
1134 	return retval;
1135 }
1136 
1137 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1138 {
1139 	return queue_logical_block_size(bdev_get_queue(bdev));
1140 }
1141 
1142 static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1143 {
1144 	return q->limits.physical_block_size;
1145 }
1146 
1147 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1148 {
1149 	return queue_physical_block_size(bdev_get_queue(bdev));
1150 }
1151 
1152 static inline unsigned int queue_io_min(const struct request_queue *q)
1153 {
1154 	return q->limits.io_min;
1155 }
1156 
1157 static inline int bdev_io_min(struct block_device *bdev)
1158 {
1159 	return queue_io_min(bdev_get_queue(bdev));
1160 }
1161 
1162 static inline unsigned int queue_io_opt(const struct request_queue *q)
1163 {
1164 	return q->limits.io_opt;
1165 }
1166 
1167 static inline int bdev_io_opt(struct block_device *bdev)
1168 {
1169 	return queue_io_opt(bdev_get_queue(bdev));
1170 }
1171 
1172 static inline unsigned int
1173 queue_zone_write_granularity(const struct request_queue *q)
1174 {
1175 	return q->limits.zone_write_granularity;
1176 }
1177 
1178 static inline unsigned int
1179 bdev_zone_write_granularity(struct block_device *bdev)
1180 {
1181 	return queue_zone_write_granularity(bdev_get_queue(bdev));
1182 }
1183 
1184 int bdev_alignment_offset(struct block_device *bdev);
1185 unsigned int bdev_discard_alignment(struct block_device *bdev);
1186 
1187 static inline unsigned int bdev_max_discard_sectors(struct block_device *bdev)
1188 {
1189 	return bdev_get_queue(bdev)->limits.max_discard_sectors;
1190 }
1191 
1192 static inline unsigned int bdev_discard_granularity(struct block_device *bdev)
1193 {
1194 	return bdev_get_queue(bdev)->limits.discard_granularity;
1195 }
1196 
1197 static inline unsigned int
1198 bdev_max_secure_erase_sectors(struct block_device *bdev)
1199 {
1200 	return bdev_get_queue(bdev)->limits.max_secure_erase_sectors;
1201 }
1202 
1203 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1204 {
1205 	struct request_queue *q = bdev_get_queue(bdev);
1206 
1207 	if (q)
1208 		return q->limits.max_write_zeroes_sectors;
1209 
1210 	return 0;
1211 }
1212 
1213 static inline bool bdev_nonrot(struct block_device *bdev)
1214 {
1215 	return blk_queue_nonrot(bdev_get_queue(bdev));
1216 }
1217 
1218 static inline bool bdev_synchronous(struct block_device *bdev)
1219 {
1220 	return test_bit(QUEUE_FLAG_SYNCHRONOUS,
1221 			&bdev_get_queue(bdev)->queue_flags);
1222 }
1223 
1224 static inline bool bdev_stable_writes(struct block_device *bdev)
1225 {
1226 	return test_bit(QUEUE_FLAG_STABLE_WRITES,
1227 			&bdev_get_queue(bdev)->queue_flags);
1228 }
1229 
1230 static inline bool bdev_write_cache(struct block_device *bdev)
1231 {
1232 	return test_bit(QUEUE_FLAG_WC, &bdev_get_queue(bdev)->queue_flags);
1233 }
1234 
1235 static inline bool bdev_fua(struct block_device *bdev)
1236 {
1237 	return test_bit(QUEUE_FLAG_FUA, &bdev_get_queue(bdev)->queue_flags);
1238 }
1239 
1240 static inline bool bdev_nowait(struct block_device *bdev)
1241 {
1242 	return test_bit(QUEUE_FLAG_NOWAIT, &bdev_get_queue(bdev)->queue_flags);
1243 }
1244 
1245 static inline bool bdev_is_zoned(struct block_device *bdev)
1246 {
1247 	return blk_queue_is_zoned(bdev_get_queue(bdev));
1248 }
1249 
1250 static inline unsigned int bdev_zone_no(struct block_device *bdev, sector_t sec)
1251 {
1252 	return disk_zone_no(bdev->bd_disk, sec);
1253 }
1254 
1255 /* Whether write serialization is required for @op on zoned devices. */
1256 static inline bool op_needs_zoned_write_locking(enum req_op op)
1257 {
1258 	return op == REQ_OP_WRITE || op == REQ_OP_WRITE_ZEROES;
1259 }
1260 
1261 static inline bool bdev_op_is_zoned_write(struct block_device *bdev,
1262 					  enum req_op op)
1263 {
1264 	return bdev_is_zoned(bdev) && op_needs_zoned_write_locking(op);
1265 }
1266 
1267 static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1268 {
1269 	struct request_queue *q = bdev_get_queue(bdev);
1270 
1271 	if (!blk_queue_is_zoned(q))
1272 		return 0;
1273 	return q->limits.chunk_sectors;
1274 }
1275 
1276 static inline sector_t bdev_offset_from_zone_start(struct block_device *bdev,
1277 						   sector_t sector)
1278 {
1279 	return sector & (bdev_zone_sectors(bdev) - 1);
1280 }
1281 
1282 static inline bool bdev_is_zone_start(struct block_device *bdev,
1283 				      sector_t sector)
1284 {
1285 	return bdev_offset_from_zone_start(bdev, sector) == 0;
1286 }
1287 
1288 static inline int queue_dma_alignment(const struct request_queue *q)
1289 {
1290 	return q ? q->limits.dma_alignment : 511;
1291 }
1292 
1293 static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
1294 {
1295 	return queue_dma_alignment(bdev_get_queue(bdev));
1296 }
1297 
1298 static inline bool bdev_iter_is_aligned(struct block_device *bdev,
1299 					struct iov_iter *iter)
1300 {
1301 	return iov_iter_is_aligned(iter, bdev_dma_alignment(bdev),
1302 				   bdev_logical_block_size(bdev) - 1);
1303 }
1304 
1305 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1306 				 unsigned int len)
1307 {
1308 	unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1309 	return !(addr & alignment) && !(len & alignment);
1310 }
1311 
1312 /* assumes size > 256 */
1313 static inline unsigned int blksize_bits(unsigned int size)
1314 {
1315 	return order_base_2(size >> SECTOR_SHIFT) + SECTOR_SHIFT;
1316 }
1317 
1318 static inline unsigned int block_size(struct block_device *bdev)
1319 {
1320 	return 1 << bdev->bd_inode->i_blkbits;
1321 }
1322 
1323 int kblockd_schedule_work(struct work_struct *work);
1324 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1325 
1326 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1327 	MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1328 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1329 	MODULE_ALIAS("block-major-" __stringify(major) "-*")
1330 
1331 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1332 
1333 bool blk_crypto_register(struct blk_crypto_profile *profile,
1334 			 struct request_queue *q);
1335 
1336 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1337 
1338 static inline bool blk_crypto_register(struct blk_crypto_profile *profile,
1339 				       struct request_queue *q)
1340 {
1341 	return true;
1342 }
1343 
1344 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1345 
1346 enum blk_unique_id {
1347 	/* these match the Designator Types specified in SPC */
1348 	BLK_UID_T10	= 1,
1349 	BLK_UID_EUI64	= 2,
1350 	BLK_UID_NAA	= 3,
1351 };
1352 
1353 struct block_device_operations {
1354 	void (*submit_bio)(struct bio *bio);
1355 	int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
1356 			unsigned int flags);
1357 	int (*open)(struct gendisk *disk, blk_mode_t mode);
1358 	void (*release)(struct gendisk *disk);
1359 	int (*ioctl)(struct block_device *bdev, blk_mode_t mode,
1360 			unsigned cmd, unsigned long arg);
1361 	int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode,
1362 			unsigned cmd, unsigned long arg);
1363 	unsigned int (*check_events) (struct gendisk *disk,
1364 				      unsigned int clearing);
1365 	void (*unlock_native_capacity) (struct gendisk *);
1366 	int (*getgeo)(struct block_device *, struct hd_geometry *);
1367 	int (*set_read_only)(struct block_device *bdev, bool ro);
1368 	void (*free_disk)(struct gendisk *disk);
1369 	/* this callback is with swap_lock and sometimes page table lock held */
1370 	void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1371 	int (*report_zones)(struct gendisk *, sector_t sector,
1372 			unsigned int nr_zones, report_zones_cb cb, void *data);
1373 	char *(*devnode)(struct gendisk *disk, umode_t *mode);
1374 	/* returns the length of the identifier or a negative errno: */
1375 	int (*get_unique_id)(struct gendisk *disk, u8 id[16],
1376 			enum blk_unique_id id_type);
1377 	struct module *owner;
1378 	const struct pr_ops *pr_ops;
1379 
1380 	/*
1381 	 * Special callback for probing GPT entry at a given sector.
1382 	 * Needed by Android devices, used by GPT scanner and MMC blk
1383 	 * driver.
1384 	 */
1385 	int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
1386 };
1387 
1388 #ifdef CONFIG_COMPAT
1389 extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
1390 				      unsigned int, unsigned long);
1391 #else
1392 #define blkdev_compat_ptr_ioctl NULL
1393 #endif
1394 
1395 static inline void blk_wake_io_task(struct task_struct *waiter)
1396 {
1397 	/*
1398 	 * If we're polling, the task itself is doing the completions. For
1399 	 * that case, we don't need to signal a wakeup, it's enough to just
1400 	 * mark us as RUNNING.
1401 	 */
1402 	if (waiter == current)
1403 		__set_current_state(TASK_RUNNING);
1404 	else
1405 		wake_up_process(waiter);
1406 }
1407 
1408 unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
1409 				 unsigned long start_time);
1410 void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
1411 		      unsigned int sectors, unsigned long start_time);
1412 
1413 unsigned long bio_start_io_acct(struct bio *bio);
1414 void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1415 		struct block_device *orig_bdev);
1416 
1417 /**
1418  * bio_end_io_acct - end I/O accounting for bio based drivers
1419  * @bio:	bio to end account for
1420  * @start_time:	start time returned by bio_start_io_acct()
1421  */
1422 static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1423 {
1424 	return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1425 }
1426 
1427 int bdev_read_only(struct block_device *bdev);
1428 int set_blocksize(struct block_device *bdev, int size);
1429 
1430 int lookup_bdev(const char *pathname, dev_t *dev);
1431 
1432 void blkdev_show(struct seq_file *seqf, off_t offset);
1433 
1434 #define BDEVNAME_SIZE	32	/* Largest string for a blockdev identifier */
1435 #define BDEVT_SIZE	10	/* Largest string for MAJ:MIN for blkdev */
1436 #ifdef CONFIG_BLOCK
1437 #define BLKDEV_MAJOR_MAX	512
1438 #else
1439 #define BLKDEV_MAJOR_MAX	0
1440 #endif
1441 
1442 struct blk_holder_ops {
1443 	void (*mark_dead)(struct block_device *bdev, bool surprise);
1444 
1445 	/*
1446 	 * Sync the file system mounted on the block device.
1447 	 */
1448 	void (*sync)(struct block_device *bdev);
1449 };
1450 
1451 extern const struct blk_holder_ops fs_holder_ops;
1452 
1453 /*
1454  * Return the correct open flags for blkdev_get_by_* for super block flags
1455  * as stored in sb->s_flags.
1456  */
1457 #define sb_open_mode(flags) \
1458 	(BLK_OPEN_READ | (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
1459 
1460 struct bdev_handle {
1461 	struct block_device *bdev;
1462 	void *holder;
1463 	blk_mode_t mode;
1464 };
1465 
1466 struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
1467 		const struct blk_holder_ops *hops);
1468 struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
1469 		void *holder, const struct blk_holder_ops *hops);
1470 struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
1471 		const struct blk_holder_ops *hops);
1472 struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode,
1473 		void *holder, const struct blk_holder_ops *hops);
1474 int bd_prepare_to_claim(struct block_device *bdev, void *holder,
1475 		const struct blk_holder_ops *hops);
1476 void bd_abort_claiming(struct block_device *bdev, void *holder);
1477 void blkdev_put(struct block_device *bdev, void *holder);
1478 void bdev_release(struct bdev_handle *handle);
1479 
1480 /* just for blk-cgroup, don't use elsewhere */
1481 struct block_device *blkdev_get_no_open(dev_t dev);
1482 void blkdev_put_no_open(struct block_device *bdev);
1483 
1484 struct block_device *I_BDEV(struct inode *inode);
1485 
1486 #ifdef CONFIG_BLOCK
1487 void invalidate_bdev(struct block_device *bdev);
1488 int sync_blockdev(struct block_device *bdev);
1489 int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
1490 int sync_blockdev_nowait(struct block_device *bdev);
1491 void sync_bdevs(bool wait);
1492 void bdev_statx_dioalign(struct inode *inode, struct kstat *stat);
1493 void printk_all_partitions(void);
1494 int __init early_lookup_bdev(const char *pathname, dev_t *dev);
1495 #else
1496 static inline void invalidate_bdev(struct block_device *bdev)
1497 {
1498 }
1499 static inline int sync_blockdev(struct block_device *bdev)
1500 {
1501 	return 0;
1502 }
1503 static inline int sync_blockdev_nowait(struct block_device *bdev)
1504 {
1505 	return 0;
1506 }
1507 static inline void sync_bdevs(bool wait)
1508 {
1509 }
1510 static inline void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
1511 {
1512 }
1513 static inline void printk_all_partitions(void)
1514 {
1515 }
1516 static inline int early_lookup_bdev(const char *pathname, dev_t *dev)
1517 {
1518 	return -EINVAL;
1519 }
1520 #endif /* CONFIG_BLOCK */
1521 
1522 int freeze_bdev(struct block_device *bdev);
1523 int thaw_bdev(struct block_device *bdev);
1524 
1525 struct io_comp_batch {
1526 	struct request *req_list;
1527 	bool need_ts;
1528 	void (*complete)(struct io_comp_batch *);
1529 };
1530 
1531 #define DEFINE_IO_COMP_BATCH(name)	struct io_comp_batch name = { }
1532 
1533 #endif /* _LINUX_BLKDEV_H */
1534