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