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