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