• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BLKDEV_H
3 #define _LINUX_BLKDEV_H
4 
5 #include <linux/sched.h>
6 #include <linux/sched/clock.h>
7 #include <linux/major.h>
8 #include <linux/genhd.h>
9 #include <linux/list.h>
10 #include <linux/llist.h>
11 #include <linux/minmax.h>
12 #include <linux/timer.h>
13 #include <linux/workqueue.h>
14 #include <linux/wait.h>
15 #include <linux/mempool.h>
16 #include <linux/pfn.h>
17 #include <linux/bio.h>
18 #include <linux/stringify.h>
19 #include <linux/gfp.h>
20 #include <linux/smp.h>
21 #include <linux/rcupdate.h>
22 #include <linux/percpu-refcount.h>
23 #include <linux/scatterlist.h>
24 #include <linux/blkzoned.h>
25 #include <linux/pm.h>
26 #include <linux/sbitmap.h>
27 #include <linux/android_kabi.h>
28 #include <linux/android_vendor.h>
29 
30 struct module;
31 struct request_queue;
32 struct elevator_queue;
33 struct blk_trace;
34 struct request;
35 struct sg_io_hdr;
36 struct blkcg_gq;
37 struct blk_flush_queue;
38 struct pr_ops;
39 struct rq_qos;
40 struct blk_queue_stats;
41 struct blk_stat_callback;
42 struct blk_keyslot_manager;
43 
44 #define BLKDEV_MIN_RQ	4
45 #define BLKDEV_MAX_RQ	128	/* Default maximum */
46 
47 /* Must be consistent with blk_mq_poll_stats_bkt() */
48 #define BLK_MQ_POLL_STATS_BKTS 16
49 
50 /* Doing classic polling */
51 #define BLK_MQ_POLL_CLASSIC -1
52 
53 /*
54  * Maximum number of blkcg policies allowed to be registered concurrently.
55  * Defined here to simplify include dependency.
56  */
57 #define BLKCG_MAX_POLS		6
58 
59 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
60 
61 /*
62  * request flags */
63 typedef __u32 __bitwise req_flags_t;
64 
65 /* drive already may have started this one */
66 #define RQF_STARTED		((__force req_flags_t)(1 << 1))
67 /* may not be passed by ioscheduler */
68 #define RQF_SOFTBARRIER		((__force req_flags_t)(1 << 3))
69 /* request for flush sequence */
70 #define RQF_FLUSH_SEQ		((__force req_flags_t)(1 << 4))
71 /* merge of different types, fail separately */
72 #define RQF_MIXED_MERGE		((__force req_flags_t)(1 << 5))
73 /* track inflight for MQ */
74 #define RQF_MQ_INFLIGHT		((__force req_flags_t)(1 << 6))
75 /* don't call prep for this one */
76 #define RQF_DONTPREP		((__force req_flags_t)(1 << 7))
77 /* vaguely specified driver internal error.  Ignored by the block layer */
78 #define RQF_FAILED		((__force req_flags_t)(1 << 10))
79 /* don't warn about errors */
80 #define RQF_QUIET		((__force req_flags_t)(1 << 11))
81 /* elevator private data attached */
82 #define RQF_ELVPRIV		((__force req_flags_t)(1 << 12))
83 /* account into disk and partition IO statistics */
84 #define RQF_IO_STAT		((__force req_flags_t)(1 << 13))
85 /* runtime pm request */
86 #define RQF_PM			((__force req_flags_t)(1 << 15))
87 /* on IO scheduler merge hash */
88 #define RQF_HASHED		((__force req_flags_t)(1 << 16))
89 /* track IO completion time */
90 #define RQF_STATS		((__force req_flags_t)(1 << 17))
91 /* Look at ->special_vec for the actual data payload instead of the
92    bio chain. */
93 #define RQF_SPECIAL_PAYLOAD	((__force req_flags_t)(1 << 18))
94 /* The per-zone write lock is held for this request */
95 #define RQF_ZONE_WRITE_LOCKED	((__force req_flags_t)(1 << 19))
96 /* already slept for hybrid poll */
97 #define RQF_MQ_POLL_SLEPT	((__force req_flags_t)(1 << 20))
98 /* ->timeout has been called, don't expire again */
99 #define RQF_TIMED_OUT		((__force req_flags_t)(1 << 21))
100 
101 /* flags that prevent us from merging requests: */
102 #define RQF_NOMERGE_FLAGS \
103 	(RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
104 
105 /*
106  * Request state for blk-mq.
107  */
108 enum mq_rq_state {
109 	MQ_RQ_IDLE		= 0,
110 	MQ_RQ_IN_FLIGHT		= 1,
111 	MQ_RQ_COMPLETE		= 2,
112 };
113 
114 /*
115  * Try to put the fields that are referenced together in the same cacheline.
116  *
117  * If you modify this structure, make sure to update blk_rq_init() and
118  * especially blk_mq_rq_ctx_init() to take care of the added fields.
119  */
120 struct request {
121 	struct request_queue *q;
122 	struct blk_mq_ctx *mq_ctx;
123 	struct blk_mq_hw_ctx *mq_hctx;
124 
125 	unsigned int cmd_flags;		/* op and common flags */
126 	req_flags_t rq_flags;
127 
128 	int tag;
129 	int internal_tag;
130 
131 	/* the following two fields are internal, NEVER access directly */
132 	unsigned int __data_len;	/* total data len */
133 	sector_t __sector;		/* sector cursor */
134 
135 	struct bio *bio;
136 	struct bio *biotail;
137 
138 	struct list_head queuelist;
139 
140 	/*
141 	 * The hash is used inside the scheduler, and killed once the
142 	 * request reaches the dispatch list. The ipi_list is only used
143 	 * to queue the request for softirq completion, which is long
144 	 * after the request has been unhashed (and even removed from
145 	 * the dispatch list).
146 	 */
147 	union {
148 		struct hlist_node hash;	/* merge hash */
149 		struct llist_node ipi_list;
150 	};
151 
152 	/*
153 	 * The rb_node is only used inside the io scheduler, requests
154 	 * are pruned when moved to the dispatch queue. So let the
155 	 * completion_data share space with the rb_node.
156 	 */
157 	union {
158 		struct rb_node rb_node;	/* sort/lookup */
159 		struct bio_vec special_vec;
160 		void *completion_data;
161 		int error_count; /* for legacy drivers, don't use */
162 	};
163 
164 	/*
165 	 * Three pointers are available for the IO schedulers, if they need
166 	 * more they have to dynamically allocate it.  Flush requests are
167 	 * never put on the IO scheduler. So let the flush fields share
168 	 * space with the elevator data.
169 	 */
170 	union {
171 		struct {
172 			struct io_cq		*icq;
173 			void			*priv[2];
174 		} elv;
175 
176 		struct {
177 			unsigned int		seq;
178 			struct list_head	list;
179 			rq_end_io_fn		*saved_end_io;
180 		} flush;
181 	};
182 
183 	struct gendisk *rq_disk;
184 	struct block_device *part;
185 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
186 	/* Time that the first bio started allocating this request. */
187 	u64 alloc_time_ns;
188 #endif
189 	/* Time that this request was allocated for this IO. */
190 	u64 start_time_ns;
191 	/* Time that I/O was submitted to the device. */
192 	u64 io_start_time_ns;
193 
194 #ifdef CONFIG_BLK_WBT
195 	unsigned short wbt_flags;
196 #endif
197 	/*
198 	 * rq sectors used for blk stats. It has the same value
199 	 * with blk_rq_sectors(rq), except that it never be zeroed
200 	 * by completion.
201 	 */
202 	unsigned short stats_sectors;
203 
204 	/*
205 	 * Number of scatter-gather DMA addr+len pairs after
206 	 * physical address coalescing is performed.
207 	 */
208 	unsigned short nr_phys_segments;
209 
210 #if defined(CONFIG_BLK_DEV_INTEGRITY)
211 	unsigned short nr_integrity_segments;
212 #endif
213 
214 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
215 	struct bio_crypt_ctx *crypt_ctx;
216 	struct blk_ksm_keyslot *crypt_keyslot;
217 #endif
218 
219 	unsigned short write_hint;
220 	unsigned short ioprio;
221 
222 	enum mq_rq_state state;
223 	refcount_t ref;
224 
225 	unsigned int timeout;
226 	unsigned long deadline;
227 
228 	union {
229 		struct __call_single_data csd;
230 		u64 fifo_time;
231 	};
232 
233 	/*
234 	 * completion callback.
235 	 */
236 	rq_end_io_fn *end_io;
237 	void *end_io_data;
238 
239 	ANDROID_KABI_RESERVE(1);
240 };
241 
blk_validate_block_size(unsigned int bsize)242 static inline int blk_validate_block_size(unsigned int bsize)
243 {
244 	if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
245 		return -EINVAL;
246 
247 	return 0;
248 }
249 
blk_op_is_passthrough(unsigned int op)250 static inline bool blk_op_is_passthrough(unsigned int op)
251 {
252 	op &= REQ_OP_MASK;
253 	return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
254 }
255 
blk_rq_is_passthrough(struct request * rq)256 static inline bool blk_rq_is_passthrough(struct request *rq)
257 {
258 	return blk_op_is_passthrough(req_op(rq));
259 }
260 
req_get_ioprio(struct request * req)261 static inline unsigned short req_get_ioprio(struct request *req)
262 {
263 	return req->ioprio;
264 }
265 
266 #include <linux/elevator.h>
267 
268 struct bio_vec;
269 
270 enum blk_eh_timer_return {
271 	BLK_EH_DONE,		/* drivers has completed the command */
272 	BLK_EH_RESET_TIMER,	/* reset timer and try again */
273 };
274 
275 enum blk_queue_state {
276 	Queue_down,
277 	Queue_up,
278 };
279 
280 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
281 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
282 
283 /*
284  * Zoned block device models (zoned limit).
285  *
286  * Note: This needs to be ordered from the least to the most severe
287  * restrictions for the inheritance in blk_stack_limits() to work.
288  */
289 enum blk_zoned_model {
290 	BLK_ZONED_NONE = 0,	/* Regular block device */
291 	BLK_ZONED_HA,		/* Host-aware zoned block device */
292 	BLK_ZONED_HM,		/* Host-managed zoned block device */
293 };
294 
295 /*
296  * BLK_BOUNCE_NONE:	never bounce (default)
297  * BLK_BOUNCE_HIGH:	bounce all highmem pages
298  */
299 enum blk_bounce {
300 	BLK_BOUNCE_NONE,
301 	BLK_BOUNCE_HIGH,
302 };
303 
304 struct queue_limits {
305 	enum blk_bounce		bounce;
306 	unsigned long		seg_boundary_mask;
307 	unsigned long		virt_boundary_mask;
308 
309 	unsigned int		max_hw_sectors;
310 	unsigned int		max_dev_sectors;
311 	unsigned int		chunk_sectors;
312 	unsigned int		max_sectors;
313 	unsigned int		max_segment_size;
314 	unsigned int		physical_block_size;
315 	unsigned int		logical_block_size;
316 	unsigned int		alignment_offset;
317 	unsigned int		io_min;
318 	unsigned int		io_opt;
319 	unsigned int		max_discard_sectors;
320 	unsigned int		max_hw_discard_sectors;
321 	unsigned int		max_write_same_sectors;
322 	unsigned int		max_write_zeroes_sectors;
323 	unsigned int		max_zone_append_sectors;
324 	unsigned int		discard_granularity;
325 	unsigned int		discard_alignment;
326 	unsigned int		zone_write_granularity;
327 
328 	unsigned short		max_segments;
329 	unsigned short		max_integrity_segments;
330 	unsigned short		max_discard_segments;
331 
332 	unsigned char		misaligned;
333 	unsigned char		discard_misaligned;
334 	unsigned char		raid_partial_stripes_expensive;
335 	enum blk_zoned_model	zoned;
336 
337 	ANDROID_KABI_RESERVE(1);
338 
339 	ANDROID_OEM_DATA(1);
340 };
341 
342 typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
343 			       void *data);
344 
345 void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model);
346 
347 #ifdef CONFIG_BLK_DEV_ZONED
348 
349 #define BLK_ALL_ZONES  ((unsigned int)-1)
350 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
351 			unsigned int nr_zones, report_zones_cb cb, void *data);
352 unsigned int blkdev_nr_zones(struct gendisk *disk);
353 extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
354 			    sector_t sectors, sector_t nr_sectors,
355 			    gfp_t gfp_mask);
356 int blk_revalidate_disk_zones(struct gendisk *disk,
357 			      void (*update_driver_data)(struct gendisk *disk));
358 
359 extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
360 				     unsigned int cmd, unsigned long arg);
361 extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
362 				  unsigned int cmd, unsigned long arg);
363 
364 #else /* CONFIG_BLK_DEV_ZONED */
365 
blkdev_nr_zones(struct gendisk * disk)366 static inline unsigned int blkdev_nr_zones(struct gendisk *disk)
367 {
368 	return 0;
369 }
370 
blkdev_report_zones_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)371 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
372 					    fmode_t mode, unsigned int cmd,
373 					    unsigned long arg)
374 {
375 	return -ENOTTY;
376 }
377 
blkdev_zone_mgmt_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)378 static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
379 					 fmode_t mode, unsigned int cmd,
380 					 unsigned long arg)
381 {
382 	return -ENOTTY;
383 }
384 
385 #endif /* CONFIG_BLK_DEV_ZONED */
386 
387 struct request_queue {
388 	struct request		*last_merge;
389 	struct elevator_queue	*elevator;
390 
391 	struct percpu_ref	q_usage_counter;
392 
393 	struct blk_queue_stats	*stats;
394 	struct rq_qos		*rq_qos;
395 
396 	const struct blk_mq_ops	*mq_ops;
397 
398 	/* sw queues */
399 	struct blk_mq_ctx __percpu	*queue_ctx;
400 
401 	unsigned int		queue_depth;
402 
403 	/* hw dispatch queues */
404 	struct blk_mq_hw_ctx	**queue_hw_ctx;
405 	unsigned int		nr_hw_queues;
406 
407 	/*
408 	 * The queue owner gets to use this for whatever they like.
409 	 * ll_rw_blk doesn't touch it.
410 	 */
411 	void			*queuedata;
412 
413 	/*
414 	 * various queue flags, see QUEUE_* below
415 	 */
416 	unsigned long		queue_flags;
417 	/*
418 	 * Number of contexts that have called blk_set_pm_only(). If this
419 	 * counter is above zero then only RQF_PM requests are processed.
420 	 */
421 	atomic_t		pm_only;
422 
423 	/*
424 	 * ida allocated id for this queue.  Used to index queues from
425 	 * ioctx.
426 	 */
427 	int			id;
428 
429 	spinlock_t		queue_lock;
430 
431 	struct gendisk		*disk;
432 
433 	/*
434 	 * queue kobject
435 	 */
436 	struct kobject kobj;
437 
438 	/*
439 	 * mq queue kobject
440 	 */
441 	struct kobject *mq_kobj;
442 
443 #ifdef  CONFIG_BLK_DEV_INTEGRITY
444 	struct blk_integrity integrity;
445 #endif	/* CONFIG_BLK_DEV_INTEGRITY */
446 
447 #ifdef CONFIG_PM
448 	struct device		*dev;
449 	enum rpm_status		rpm_status;
450 #endif
451 
452 	/*
453 	 * queue settings
454 	 */
455 	unsigned long		nr_requests;	/* Max # of requests */
456 
457 	unsigned int		dma_pad_mask;
458 	unsigned int		dma_alignment;
459 
460 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
461 	/* Inline crypto capabilities */
462 	struct blk_keyslot_manager *ksm;
463 #endif
464 
465 	unsigned int		rq_timeout;
466 	int			poll_nsec;
467 
468 	struct blk_stat_callback	*poll_cb;
469 	struct blk_rq_stat	poll_stat[BLK_MQ_POLL_STATS_BKTS];
470 
471 	struct timer_list	timeout;
472 	struct work_struct	timeout_work;
473 
474 	atomic_t		nr_active_requests_shared_sbitmap;
475 
476 	struct sbitmap_queue	sched_bitmap_tags;
477 	struct sbitmap_queue	sched_breserved_tags;
478 
479 	struct list_head	icq_list;
480 #ifdef CONFIG_BLK_CGROUP
481 	DECLARE_BITMAP		(blkcg_pols, BLKCG_MAX_POLS);
482 	struct blkcg_gq		*root_blkg;
483 	struct list_head	blkg_list;
484 #endif
485 
486 	struct queue_limits	limits;
487 
488 	unsigned int		required_elevator_features;
489 
490 #ifdef CONFIG_BLK_DEV_ZONED
491 	/*
492 	 * Zoned block device information for request dispatch control.
493 	 * nr_zones is the total number of zones of the device. This is always
494 	 * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones
495 	 * bits which indicates if a zone is conventional (bit set) or
496 	 * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones
497 	 * bits which indicates if a zone is write locked, that is, if a write
498 	 * request targeting the zone was dispatched. All three fields are
499 	 * initialized by the low level device driver (e.g. scsi/sd.c).
500 	 * Stacking drivers (device mappers) may or may not initialize
501 	 * these fields.
502 	 *
503 	 * Reads of this information must be protected with blk_queue_enter() /
504 	 * blk_queue_exit(). Modifying this information is only allowed while
505 	 * no requests are being processed. See also blk_mq_freeze_queue() and
506 	 * blk_mq_unfreeze_queue().
507 	 */
508 	unsigned int		nr_zones;
509 	unsigned long		*conv_zones_bitmap;
510 	unsigned long		*seq_zones_wlock;
511 	unsigned int		max_open_zones;
512 	unsigned int		max_active_zones;
513 #endif /* CONFIG_BLK_DEV_ZONED */
514 
515 	int			node;
516 	struct mutex		debugfs_mutex;
517 #ifdef CONFIG_BLK_DEV_IO_TRACE
518 	struct blk_trace __rcu	*blk_trace;
519 #endif
520 	/*
521 	 * for flush operations
522 	 */
523 	struct blk_flush_queue	*fq;
524 
525 	struct list_head	requeue_list;
526 	spinlock_t		requeue_lock;
527 	struct delayed_work	requeue_work;
528 
529 	struct mutex		sysfs_lock;
530 	struct mutex		sysfs_dir_lock;
531 
532 	/*
533 	 * for reusing dead hctx instance in case of updating
534 	 * nr_hw_queues
535 	 */
536 	struct list_head	unused_hctx_list;
537 	spinlock_t		unused_hctx_lock;
538 
539 	int			mq_freeze_depth;
540 
541 #ifdef CONFIG_BLK_DEV_THROTTLING
542 	/* Throttle data */
543 	struct throtl_data *td;
544 #endif
545 	struct rcu_head		rcu_head;
546 	wait_queue_head_t	mq_freeze_wq;
547 	/*
548 	 * Protect concurrent access to q_usage_counter by
549 	 * percpu_ref_kill() and percpu_ref_reinit().
550 	 */
551 	struct mutex		mq_freeze_lock;
552 
553 	struct blk_mq_tag_set	*tag_set;
554 	struct list_head	tag_set_list;
555 	struct bio_set		bio_split;
556 
557 	struct dentry		*debugfs_dir;
558 
559 #ifdef CONFIG_BLK_DEBUG_FS
560 	struct dentry		*sched_debugfs_dir;
561 	struct dentry		*rqos_debugfs_dir;
562 #endif
563 
564 	bool			mq_sysfs_init_done;
565 
566 	size_t			cmd_size;
567 
568 #define BLK_MAX_WRITE_HINTS	5
569 	u64			write_hints[BLK_MAX_WRITE_HINTS];
570 
571 	ANDROID_KABI_RESERVE(1);
572 	ANDROID_KABI_RESERVE(2);
573 	ANDROID_KABI_RESERVE(3);
574 	ANDROID_KABI_RESERVE(4);
575 
576 	ANDROID_OEM_DATA(1);
577 };
578 
579 /* Keep blk_queue_flag_name[] in sync with the definitions below */
580 #define QUEUE_FLAG_STOPPED	0	/* queue is stopped */
581 #define QUEUE_FLAG_DYING	1	/* queue being torn down */
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_DISCARD	8	/* supports DISCARD */
589 #define QUEUE_FLAG_NOXMERGES	9	/* No extended merges */
590 #define QUEUE_FLAG_ADD_RANDOM	10	/* Contributes to random pool */
591 #define QUEUE_FLAG_SECERASE	11	/* supports secure erase */
592 #define QUEUE_FLAG_SAME_FORCE	12	/* force complete on same CPU */
593 #define QUEUE_FLAG_DEAD		13	/* queue tear-down finished */
594 #define QUEUE_FLAG_INIT_DONE	14	/* queue is initialized */
595 #define QUEUE_FLAG_STABLE_WRITES 15	/* don't modify blks until WB is done */
596 #define QUEUE_FLAG_POLL		16	/* IO polling enabled if set */
597 #define QUEUE_FLAG_WC		17	/* Write back caching */
598 #define QUEUE_FLAG_FUA		18	/* device supports FUA writes */
599 #define QUEUE_FLAG_DAX		19	/* device supports DAX */
600 #define QUEUE_FLAG_STATS	20	/* track IO start and completion times */
601 #define QUEUE_FLAG_POLL_STATS	21	/* collecting stats for hybrid polling */
602 #define QUEUE_FLAG_REGISTERED	22	/* queue has been registered to a disk */
603 #define QUEUE_FLAG_SCSI_PASSTHROUGH 23	/* queue supports SCSI commands */
604 #define QUEUE_FLAG_QUIESCED	24	/* queue has been quiesced */
605 #define QUEUE_FLAG_PCI_P2PDMA	25	/* device supports PCI p2p requests */
606 #define QUEUE_FLAG_ZONE_RESETALL 26	/* supports Zone Reset All */
607 #define QUEUE_FLAG_RQ_ALLOC_TIME 27	/* record rq->alloc_time_ns */
608 #define QUEUE_FLAG_HCTX_ACTIVE	28	/* at least one blk-mq hctx is active */
609 #define QUEUE_FLAG_NOWAIT       29	/* device supports NOWAIT */
610 
611 #define QUEUE_FLAG_MQ_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
612 				 (1 << QUEUE_FLAG_SAME_COMP) |		\
613 				 (1 << QUEUE_FLAG_NOWAIT))
614 
615 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
616 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
617 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
618 
619 #define blk_queue_stopped(q)	test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
620 #define blk_queue_dying(q)	test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
621 #define blk_queue_dead(q)	test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
622 #define blk_queue_init_done(q)	test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
623 #define blk_queue_nomerges(q)	test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
624 #define blk_queue_noxmerges(q)	\
625 	test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
626 #define blk_queue_nonrot(q)	test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
627 #define blk_queue_stable_writes(q) \
628 	test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
629 #define blk_queue_io_stat(q)	test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
630 #define blk_queue_add_random(q)	test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
631 #define blk_queue_discard(q)	test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
632 #define blk_queue_zone_resetall(q)	\
633 	test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
634 #define blk_queue_secure_erase(q) \
635 	(test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
636 #define blk_queue_dax(q)	test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
637 #define blk_queue_scsi_passthrough(q)	\
638 	test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
639 #define blk_queue_pci_p2pdma(q)	\
640 	test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
641 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
642 #define blk_queue_rq_alloc_time(q)	\
643 	test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
644 #else
645 #define blk_queue_rq_alloc_time(q)	false
646 #endif
647 
648 #define blk_noretry_request(rq) \
649 	((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
650 			     REQ_FAILFAST_DRIVER))
651 #define blk_queue_quiesced(q)	test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
652 #define blk_queue_pm_only(q)	atomic_read(&(q)->pm_only)
653 #define blk_queue_fua(q)	test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
654 #define blk_queue_registered(q)	test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
655 #define blk_queue_nowait(q)	test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
656 
657 extern void blk_set_pm_only(struct request_queue *q);
658 extern void blk_clear_pm_only(struct request_queue *q);
659 
660 #define list_entry_rq(ptr)	list_entry((ptr), struct request, queuelist)
661 
662 #define rq_data_dir(rq)		(op_is_write(req_op(rq)) ? WRITE : READ)
663 
664 #define rq_dma_dir(rq) \
665 	(op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
666 
667 #define dma_map_bvec(dev, bv, dir, attrs) \
668 	dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
669 	(dir), (attrs))
670 
queue_is_mq(struct request_queue * q)671 static inline bool queue_is_mq(struct request_queue *q)
672 {
673 	return q->mq_ops;
674 }
675 
676 #ifdef CONFIG_PM
queue_rpm_status(struct request_queue * q)677 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
678 {
679 	return q->rpm_status;
680 }
681 #else
queue_rpm_status(struct request_queue * q)682 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
683 {
684 	return RPM_ACTIVE;
685 }
686 #endif
687 
688 static inline enum blk_zoned_model
blk_queue_zoned_model(struct request_queue * q)689 blk_queue_zoned_model(struct request_queue *q)
690 {
691 	if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
692 		return q->limits.zoned;
693 	return BLK_ZONED_NONE;
694 }
695 
blk_queue_is_zoned(struct request_queue * q)696 static inline bool blk_queue_is_zoned(struct request_queue *q)
697 {
698 	switch (blk_queue_zoned_model(q)) {
699 	case BLK_ZONED_HA:
700 	case BLK_ZONED_HM:
701 		return true;
702 	default:
703 		return false;
704 	}
705 }
706 
blk_queue_zone_sectors(struct request_queue * q)707 static inline sector_t blk_queue_zone_sectors(struct request_queue *q)
708 {
709 	return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
710 }
711 
712 #ifdef CONFIG_BLK_DEV_ZONED
blk_queue_nr_zones(struct request_queue * q)713 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
714 {
715 	return blk_queue_is_zoned(q) ? q->nr_zones : 0;
716 }
717 
blk_queue_zone_no(struct request_queue * q,sector_t sector)718 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
719 					     sector_t sector)
720 {
721 	if (!blk_queue_is_zoned(q))
722 		return 0;
723 	return sector >> ilog2(q->limits.chunk_sectors);
724 }
725 
blk_queue_zone_is_seq(struct request_queue * q,sector_t sector)726 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
727 					 sector_t sector)
728 {
729 	if (!blk_queue_is_zoned(q))
730 		return false;
731 	if (!q->conv_zones_bitmap)
732 		return true;
733 	return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap);
734 }
735 
blk_queue_max_open_zones(struct request_queue * q,unsigned int max_open_zones)736 static inline void blk_queue_max_open_zones(struct request_queue *q,
737 		unsigned int max_open_zones)
738 {
739 	q->max_open_zones = max_open_zones;
740 }
741 
queue_max_open_zones(const struct request_queue * q)742 static inline unsigned int queue_max_open_zones(const struct request_queue *q)
743 {
744 	return q->max_open_zones;
745 }
746 
blk_queue_max_active_zones(struct request_queue * q,unsigned int max_active_zones)747 static inline void blk_queue_max_active_zones(struct request_queue *q,
748 		unsigned int max_active_zones)
749 {
750 	q->max_active_zones = max_active_zones;
751 }
752 
queue_max_active_zones(const struct request_queue * q)753 static inline unsigned int queue_max_active_zones(const struct request_queue *q)
754 {
755 	return q->max_active_zones;
756 }
757 #else /* CONFIG_BLK_DEV_ZONED */
blk_queue_nr_zones(struct request_queue * q)758 static inline unsigned int blk_queue_nr_zones(struct request_queue *q)
759 {
760 	return 0;
761 }
blk_queue_zone_is_seq(struct request_queue * q,sector_t sector)762 static inline bool blk_queue_zone_is_seq(struct request_queue *q,
763 					 sector_t sector)
764 {
765 	return false;
766 }
blk_queue_zone_no(struct request_queue * q,sector_t sector)767 static inline unsigned int blk_queue_zone_no(struct request_queue *q,
768 					     sector_t sector)
769 {
770 	return 0;
771 }
queue_max_open_zones(const struct request_queue * q)772 static inline unsigned int queue_max_open_zones(const struct request_queue *q)
773 {
774 	return 0;
775 }
queue_max_active_zones(const struct request_queue * q)776 static inline unsigned int queue_max_active_zones(const struct request_queue *q)
777 {
778 	return 0;
779 }
780 #endif /* CONFIG_BLK_DEV_ZONED */
781 
rq_is_sync(struct request * rq)782 static inline bool rq_is_sync(struct request *rq)
783 {
784 	return op_is_sync(rq->cmd_flags);
785 }
786 
rq_mergeable(struct request * rq)787 static inline bool rq_mergeable(struct request *rq)
788 {
789 	if (blk_rq_is_passthrough(rq))
790 		return false;
791 
792 	if (req_op(rq) == REQ_OP_FLUSH)
793 		return false;
794 
795 	if (req_op(rq) == REQ_OP_WRITE_ZEROES)
796 		return false;
797 
798 	if (req_op(rq) == REQ_OP_ZONE_APPEND)
799 		return false;
800 
801 	if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
802 		return false;
803 	if (rq->rq_flags & RQF_NOMERGE_FLAGS)
804 		return false;
805 
806 	return true;
807 }
808 
blk_write_same_mergeable(struct bio * a,struct bio * b)809 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
810 {
811 	if (bio_page(a) == bio_page(b) &&
812 	    bio_offset(a) == bio_offset(b))
813 		return true;
814 
815 	return false;
816 }
817 
blk_queue_depth(struct request_queue * q)818 static inline unsigned int blk_queue_depth(struct request_queue *q)
819 {
820 	if (q->queue_depth)
821 		return q->queue_depth;
822 
823 	return q->nr_requests;
824 }
825 
826 /*
827  * default timeout for SG_IO if none specified
828  */
829 #define BLK_DEFAULT_SG_TIMEOUT	(60 * HZ)
830 #define BLK_MIN_SG_TIMEOUT	(7 * HZ)
831 
832 struct rq_map_data {
833 	struct page **pages;
834 	int page_order;
835 	int nr_entries;
836 	unsigned long offset;
837 	int null_mapped;
838 	int from_user;
839 };
840 
841 struct req_iterator {
842 	struct bvec_iter iter;
843 	struct bio *bio;
844 };
845 
846 /* This should not be used directly - use rq_for_each_segment */
847 #define for_each_bio(_bio)		\
848 	for (; _bio; _bio = _bio->bi_next)
849 #define __rq_for_each_bio(_bio, rq)	\
850 	if ((rq->bio))			\
851 		for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
852 
853 #define rq_for_each_segment(bvl, _rq, _iter)			\
854 	__rq_for_each_bio(_iter.bio, _rq)			\
855 		bio_for_each_segment(bvl, _iter.bio, _iter.iter)
856 
857 #define rq_for_each_bvec(bvl, _rq, _iter)			\
858 	__rq_for_each_bio(_iter.bio, _rq)			\
859 		bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
860 
861 #define rq_iter_last(bvec, _iter)				\
862 		(_iter.bio->bi_next == NULL &&			\
863 		 bio_iter_last(bvec, _iter.iter))
864 
865 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
866 # error	"You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
867 #endif
868 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
869 extern void rq_flush_dcache_pages(struct request *rq);
870 #else
rq_flush_dcache_pages(struct request * rq)871 static inline void rq_flush_dcache_pages(struct request *rq)
872 {
873 }
874 #endif
875 
876 extern int blk_register_queue(struct gendisk *disk);
877 extern void blk_unregister_queue(struct gendisk *disk);
878 blk_qc_t submit_bio_noacct(struct bio *bio);
879 extern void blk_rq_init(struct request_queue *q, struct request *rq);
880 extern void blk_put_request(struct request *);
881 extern struct request *blk_get_request(struct request_queue *, unsigned int op,
882 				       blk_mq_req_flags_t flags);
883 extern int blk_lld_busy(struct request_queue *q);
884 extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
885 			     struct bio_set *bs, gfp_t gfp_mask,
886 			     int (*bio_ctr)(struct bio *, struct bio *, void *),
887 			     void *data);
888 extern void blk_rq_unprep_clone(struct request *rq);
889 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
890 				     struct request *rq);
891 int blk_rq_append_bio(struct request *rq, struct bio *bio);
892 extern void blk_queue_split(struct bio **);
893 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
894 extern void blk_queue_exit(struct request_queue *q);
895 extern void blk_sync_queue(struct request_queue *q);
896 extern int blk_rq_map_user(struct request_queue *, struct request *,
897 			   struct rq_map_data *, void __user *, unsigned long,
898 			   gfp_t);
899 extern int blk_rq_unmap_user(struct bio *);
900 extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
901 extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
902 			       struct rq_map_data *, const struct iov_iter *,
903 			       gfp_t);
904 extern void blk_execute_rq_nowait(struct gendisk *,
905 				  struct request *, int, rq_end_io_fn *);
906 
907 blk_status_t blk_execute_rq(struct gendisk *bd_disk, struct request *rq,
908 			    int at_head);
909 
910 /* Helper to convert REQ_OP_XXX to its string format XXX */
911 extern const char *blk_op_str(unsigned int op);
912 
913 int blk_status_to_errno(blk_status_t status);
914 blk_status_t errno_to_blk_status(int errno);
915 
916 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
917 
bdev_get_queue(struct block_device * bdev)918 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
919 {
920 	return bdev->bd_disk->queue;	/* this is never NULL */
921 }
922 
923 /*
924  * The basic unit of block I/O is a sector. It is used in a number of contexts
925  * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
926  * bytes. Variables of type sector_t represent an offset or size that is a
927  * multiple of 512 bytes. Hence these two constants.
928  */
929 #ifndef SECTOR_SHIFT
930 #define SECTOR_SHIFT 9
931 #endif
932 #ifndef SECTOR_SIZE
933 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
934 #endif
935 
936 #define PAGE_SECTORS_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
937 #define PAGE_SECTORS		(1 << PAGE_SECTORS_SHIFT)
938 #define SECTOR_MASK		(PAGE_SECTORS - 1)
939 
940 /*
941  * blk_rq_pos()			: the current sector
942  * blk_rq_bytes()		: bytes left in the entire request
943  * blk_rq_cur_bytes()		: bytes left in the current segment
944  * blk_rq_err_bytes()		: bytes left till the next error boundary
945  * blk_rq_sectors()		: sectors left in the entire request
946  * blk_rq_cur_sectors()		: sectors left in the current segment
947  * blk_rq_stats_sectors()	: sectors of the entire request used for stats
948  */
blk_rq_pos(const struct request * rq)949 static inline sector_t blk_rq_pos(const struct request *rq)
950 {
951 	return rq->__sector;
952 }
953 
blk_rq_bytes(const struct request * rq)954 static inline unsigned int blk_rq_bytes(const struct request *rq)
955 {
956 	return rq->__data_len;
957 }
958 
blk_rq_cur_bytes(const struct request * rq)959 static inline int blk_rq_cur_bytes(const struct request *rq)
960 {
961 	return rq->bio ? bio_cur_bytes(rq->bio) : 0;
962 }
963 
964 extern unsigned int blk_rq_err_bytes(const struct request *rq);
965 
blk_rq_sectors(const struct request * rq)966 static inline unsigned int blk_rq_sectors(const struct request *rq)
967 {
968 	return blk_rq_bytes(rq) >> SECTOR_SHIFT;
969 }
970 
blk_rq_cur_sectors(const struct request * rq)971 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
972 {
973 	return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
974 }
975 
blk_rq_stats_sectors(const struct request * rq)976 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
977 {
978 	return rq->stats_sectors;
979 }
980 
981 #ifdef CONFIG_BLK_DEV_ZONED
982 
983 /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
984 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
985 
bio_zone_no(struct bio * bio)986 static inline unsigned int bio_zone_no(struct bio *bio)
987 {
988 	return blk_queue_zone_no(bdev_get_queue(bio->bi_bdev),
989 				 bio->bi_iter.bi_sector);
990 }
991 
bio_zone_is_seq(struct bio * bio)992 static inline unsigned int bio_zone_is_seq(struct bio *bio)
993 {
994 	return blk_queue_zone_is_seq(bdev_get_queue(bio->bi_bdev),
995 				     bio->bi_iter.bi_sector);
996 }
997 
blk_rq_zone_no(struct request * rq)998 static inline unsigned int blk_rq_zone_no(struct request *rq)
999 {
1000 	return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
1001 }
1002 
blk_rq_zone_is_seq(struct request * rq)1003 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
1004 {
1005 	return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
1006 }
1007 #endif /* CONFIG_BLK_DEV_ZONED */
1008 
1009 /*
1010  * Some commands like WRITE SAME have a payload or data transfer size which
1011  * is different from the size of the request.  Any driver that supports such
1012  * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1013  * calculate the data transfer size.
1014  */
blk_rq_payload_bytes(struct request * rq)1015 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1016 {
1017 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1018 		return rq->special_vec.bv_len;
1019 	return blk_rq_bytes(rq);
1020 }
1021 
1022 /*
1023  * Return the first full biovec in the request.  The caller needs to check that
1024  * there are any bvecs before calling this helper.
1025  */
req_bvec(struct request * rq)1026 static inline struct bio_vec req_bvec(struct request *rq)
1027 {
1028 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1029 		return rq->special_vec;
1030 	return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1031 }
1032 
blk_queue_get_max_sectors(struct request_queue * q,int op)1033 static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
1034 						     int op)
1035 {
1036 	if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
1037 		return min(q->limits.max_discard_sectors,
1038 			   UINT_MAX >> SECTOR_SHIFT);
1039 
1040 	if (unlikely(op == REQ_OP_WRITE_SAME))
1041 		return q->limits.max_write_same_sectors;
1042 
1043 	if (unlikely(op == REQ_OP_WRITE_ZEROES))
1044 		return q->limits.max_write_zeroes_sectors;
1045 
1046 	return q->limits.max_sectors;
1047 }
1048 
1049 /*
1050  * Return maximum size of a request at given offset. Only valid for
1051  * file system requests.
1052  */
blk_max_size_offset(struct request_queue * q,sector_t offset,unsigned int chunk_sectors)1053 static inline unsigned int blk_max_size_offset(struct request_queue *q,
1054 					       sector_t offset,
1055 					       unsigned int chunk_sectors)
1056 {
1057 	if (!chunk_sectors) {
1058 		if (q->limits.chunk_sectors)
1059 			chunk_sectors = q->limits.chunk_sectors;
1060 		else
1061 			return q->limits.max_sectors;
1062 	}
1063 
1064 	if (likely(is_power_of_2(chunk_sectors)))
1065 		chunk_sectors -= offset & (chunk_sectors - 1);
1066 	else
1067 		chunk_sectors -= sector_div(offset, chunk_sectors);
1068 
1069 	return min(q->limits.max_sectors, chunk_sectors);
1070 }
1071 
blk_rq_get_max_sectors(struct request * rq,sector_t offset)1072 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
1073 						  sector_t offset)
1074 {
1075 	struct request_queue *q = rq->q;
1076 
1077 	if (blk_rq_is_passthrough(rq))
1078 		return q->limits.max_hw_sectors;
1079 
1080 	if (!q->limits.chunk_sectors ||
1081 	    req_op(rq) == REQ_OP_DISCARD ||
1082 	    req_op(rq) == REQ_OP_SECURE_ERASE)
1083 		return blk_queue_get_max_sectors(q, req_op(rq));
1084 
1085 	return min(blk_max_size_offset(q, offset, 0),
1086 			blk_queue_get_max_sectors(q, req_op(rq)));
1087 }
1088 
blk_rq_count_bios(struct request * rq)1089 static inline unsigned int blk_rq_count_bios(struct request *rq)
1090 {
1091 	unsigned int nr_bios = 0;
1092 	struct bio *bio;
1093 
1094 	__rq_for_each_bio(bio, rq)
1095 		nr_bios++;
1096 
1097 	return nr_bios;
1098 }
1099 
1100 void blk_steal_bios(struct bio_list *list, struct request *rq);
1101 
1102 /*
1103  * Request completion related functions.
1104  *
1105  * blk_update_request() completes given number of bytes and updates
1106  * the request without completing it.
1107  */
1108 extern bool blk_update_request(struct request *rq, blk_status_t error,
1109 			       unsigned int nr_bytes);
1110 
1111 extern void blk_abort_request(struct request *);
1112 
1113 /*
1114  * Access functions for manipulating queue properties
1115  */
1116 extern void blk_cleanup_queue(struct request_queue *);
1117 void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit);
1118 extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
1119 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
1120 extern void blk_queue_max_segments(struct request_queue *, unsigned short);
1121 extern void blk_queue_max_discard_segments(struct request_queue *,
1122 		unsigned short);
1123 extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
1124 extern void blk_queue_max_discard_sectors(struct request_queue *q,
1125 		unsigned int max_discard_sectors);
1126 extern void blk_queue_max_write_same_sectors(struct request_queue *q,
1127 		unsigned int max_write_same_sectors);
1128 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
1129 		unsigned int max_write_same_sectors);
1130 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
1131 extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
1132 		unsigned int max_zone_append_sectors);
1133 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
1134 void blk_queue_zone_write_granularity(struct request_queue *q,
1135 				      unsigned int size);
1136 extern void blk_queue_alignment_offset(struct request_queue *q,
1137 				       unsigned int alignment);
1138 void disk_update_readahead(struct gendisk *disk);
1139 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
1140 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
1141 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
1142 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
1143 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1144 extern void blk_set_default_limits(struct queue_limits *lim);
1145 extern void blk_set_stacking_limits(struct queue_limits *lim);
1146 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1147 			    sector_t offset);
1148 extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
1149 			      sector_t offset);
1150 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
1151 extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
1152 extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
1153 extern void blk_queue_dma_alignment(struct request_queue *, int);
1154 extern void blk_queue_update_dma_alignment(struct request_queue *, int);
1155 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
1156 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
1157 extern void blk_queue_required_elevator_features(struct request_queue *q,
1158 						 unsigned int features);
1159 extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
1160 					      struct device *dev);
1161 
1162 /*
1163  * Number of physical segments as sent to the device.
1164  *
1165  * Normally this is the number of discontiguous data segments sent by the
1166  * submitter.  But for data-less command like discard we might have no
1167  * actual data segments submitted, but the driver might have to add it's
1168  * own special payload.  In that case we still return 1 here so that this
1169  * special payload will be mapped.
1170  */
blk_rq_nr_phys_segments(struct request * rq)1171 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1172 {
1173 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1174 		return 1;
1175 	return rq->nr_phys_segments;
1176 }
1177 
1178 /*
1179  * Number of discard segments (or ranges) the driver needs to fill in.
1180  * Each discard bio merged into a request is counted as one segment.
1181  */
blk_rq_nr_discard_segments(struct request * rq)1182 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1183 {
1184 	return max_t(unsigned short, rq->nr_phys_segments, 1);
1185 }
1186 
1187 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1188 		struct scatterlist *sglist, struct scatterlist **last_sg);
blk_rq_map_sg(struct request_queue * q,struct request * rq,struct scatterlist * sglist)1189 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1190 		struct scatterlist *sglist)
1191 {
1192 	struct scatterlist *last_sg = NULL;
1193 
1194 	return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1195 }
1196 extern void blk_dump_rq_flags(struct request *, char *);
1197 
1198 bool __must_check blk_get_queue(struct request_queue *);
1199 extern void blk_put_queue(struct request_queue *);
1200 
1201 void blk_mark_disk_dead(struct gendisk *disk);
1202 
1203 #ifdef CONFIG_BLOCK
1204 /*
1205  * blk_plug permits building a queue of related requests by holding the I/O
1206  * fragments for a short period. This allows merging of sequential requests
1207  * into single larger request. As the requests are moved from a per-task list to
1208  * the device's request_queue in a batch, this results in improved scalability
1209  * as the lock contention for request_queue lock is reduced.
1210  *
1211  * It is ok not to disable preemption when adding the request to the plug list
1212  * or when attempting a merge, because blk_schedule_flush_list() will only flush
1213  * the plug list when the task sleeps by itself. For details, please see
1214  * schedule() where blk_schedule_flush_plug() is called.
1215  */
1216 struct blk_plug {
1217 	struct list_head mq_list; /* blk-mq requests */
1218 	struct list_head cb_list; /* md requires an unplug callback */
1219 	unsigned short rq_count;
1220 	bool multiple_queues;
1221 	bool nowait;
1222 };
1223 
1224 struct blk_plug_cb;
1225 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
1226 struct blk_plug_cb {
1227 	struct list_head list;
1228 	blk_plug_cb_fn callback;
1229 	void *data;
1230 };
1231 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1232 					     void *data, int size);
1233 extern void blk_start_plug(struct blk_plug *);
1234 extern void blk_finish_plug(struct blk_plug *);
1235 extern void blk_flush_plug_list(struct blk_plug *, bool);
1236 
blk_flush_plug(struct task_struct * tsk)1237 static inline void blk_flush_plug(struct task_struct *tsk)
1238 {
1239 	struct blk_plug *plug = tsk->plug;
1240 
1241 	if (plug)
1242 		blk_flush_plug_list(plug, false);
1243 }
1244 
blk_schedule_flush_plug(struct task_struct * tsk)1245 static inline void blk_schedule_flush_plug(struct task_struct *tsk)
1246 {
1247 	struct blk_plug *plug = tsk->plug;
1248 
1249 	if (plug)
1250 		blk_flush_plug_list(plug, true);
1251 }
1252 
blk_needs_flush_plug(struct task_struct * tsk)1253 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1254 {
1255 	struct blk_plug *plug = tsk->plug;
1256 
1257 	return plug &&
1258 		 (!list_empty(&plug->mq_list) ||
1259 		 !list_empty(&plug->cb_list));
1260 }
1261 
1262 int blkdev_issue_flush(struct block_device *bdev);
1263 long nr_blockdev_pages(void);
1264 #else /* CONFIG_BLOCK */
1265 struct blk_plug {
1266 };
1267 
blk_start_plug(struct blk_plug * plug)1268 static inline void blk_start_plug(struct blk_plug *plug)
1269 {
1270 }
1271 
blk_finish_plug(struct blk_plug * plug)1272 static inline void blk_finish_plug(struct blk_plug *plug)
1273 {
1274 }
1275 
blk_flush_plug(struct task_struct * task)1276 static inline void blk_flush_plug(struct task_struct *task)
1277 {
1278 }
1279 
blk_schedule_flush_plug(struct task_struct * task)1280 static inline void blk_schedule_flush_plug(struct task_struct *task)
1281 {
1282 }
1283 
1284 
blk_needs_flush_plug(struct task_struct * tsk)1285 static inline bool blk_needs_flush_plug(struct task_struct *tsk)
1286 {
1287 	return false;
1288 }
1289 
blkdev_issue_flush(struct block_device * bdev)1290 static inline int blkdev_issue_flush(struct block_device *bdev)
1291 {
1292 	return 0;
1293 }
1294 
nr_blockdev_pages(void)1295 static inline long nr_blockdev_pages(void)
1296 {
1297 	return 0;
1298 }
1299 #endif /* CONFIG_BLOCK */
1300 
1301 extern void blk_io_schedule(void);
1302 
1303 extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
1304 		sector_t nr_sects, gfp_t gfp_mask, struct page *page);
1305 
1306 #define BLKDEV_DISCARD_SECURE	(1 << 0)	/* issue a secure erase */
1307 
1308 extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1309 		sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
1310 extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1311 		sector_t nr_sects, gfp_t gfp_mask, int flags,
1312 		struct bio **biop);
1313 
1314 #define BLKDEV_ZERO_NOUNMAP	(1 << 0)  /* do not free blocks */
1315 #define BLKDEV_ZERO_NOFALLBACK	(1 << 1)  /* don't write explicit zeroes */
1316 
1317 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1318 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1319 		unsigned flags);
1320 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1321 		sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1322 
sb_issue_discard(struct super_block * sb,sector_t block,sector_t nr_blocks,gfp_t gfp_mask,unsigned long flags)1323 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1324 		sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1325 {
1326 	return blkdev_issue_discard(sb->s_bdev,
1327 				    block << (sb->s_blocksize_bits -
1328 					      SECTOR_SHIFT),
1329 				    nr_blocks << (sb->s_blocksize_bits -
1330 						  SECTOR_SHIFT),
1331 				    gfp_mask, flags);
1332 }
sb_issue_zeroout(struct super_block * sb,sector_t block,sector_t nr_blocks,gfp_t gfp_mask)1333 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1334 		sector_t nr_blocks, gfp_t gfp_mask)
1335 {
1336 	return blkdev_issue_zeroout(sb->s_bdev,
1337 				    block << (sb->s_blocksize_bits -
1338 					      SECTOR_SHIFT),
1339 				    nr_blocks << (sb->s_blocksize_bits -
1340 						  SECTOR_SHIFT),
1341 				    gfp_mask, 0);
1342 }
1343 
bdev_is_partition(struct block_device * bdev)1344 static inline bool bdev_is_partition(struct block_device *bdev)
1345 {
1346 	return bdev->bd_partno;
1347 }
1348 
1349 enum blk_default_limits {
1350 	BLK_MAX_SEGMENTS	= 128,
1351 	BLK_SAFE_MAX_SECTORS	= 255,
1352 	BLK_MAX_SEGMENT_SIZE	= 65536,
1353 	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
1354 };
1355 
1356 #define BLK_DEF_MAX_SECTORS 2560u
1357 
queue_segment_boundary(const struct request_queue * q)1358 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1359 {
1360 	return q->limits.seg_boundary_mask;
1361 }
1362 
queue_virt_boundary(const struct request_queue * q)1363 static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1364 {
1365 	return q->limits.virt_boundary_mask;
1366 }
1367 
queue_max_sectors(const struct request_queue * q)1368 static inline unsigned int queue_max_sectors(const struct request_queue *q)
1369 {
1370 	return q->limits.max_sectors;
1371 }
1372 
queue_max_bytes(struct request_queue * q)1373 static inline unsigned int queue_max_bytes(struct request_queue *q)
1374 {
1375 	return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1376 }
1377 
queue_max_hw_sectors(const struct request_queue * q)1378 static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1379 {
1380 	return q->limits.max_hw_sectors;
1381 }
1382 
queue_max_segments(const struct request_queue * q)1383 static inline unsigned short queue_max_segments(const struct request_queue *q)
1384 {
1385 	return q->limits.max_segments;
1386 }
1387 
queue_max_discard_segments(const struct request_queue * q)1388 static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1389 {
1390 	return q->limits.max_discard_segments;
1391 }
1392 
queue_max_segment_size(const struct request_queue * q)1393 static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1394 {
1395 	return q->limits.max_segment_size;
1396 }
1397 
queue_max_zone_append_sectors(const struct request_queue * q)1398 static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q)
1399 {
1400 
1401 	const struct queue_limits *l = &q->limits;
1402 
1403 	return min(l->max_zone_append_sectors, l->max_sectors);
1404 }
1405 
1406 static inline unsigned int
bdev_max_zone_append_sectors(struct block_device * bdev)1407 bdev_max_zone_append_sectors(struct block_device *bdev)
1408 {
1409 	return queue_max_zone_append_sectors(bdev_get_queue(bdev));
1410 }
1411 
bdev_max_segments(struct block_device * bdev)1412 static inline unsigned int bdev_max_segments(struct block_device *bdev)
1413 {
1414 	return queue_max_segments(bdev_get_queue(bdev));
1415 }
1416 
queue_logical_block_size(const struct request_queue * q)1417 static inline unsigned queue_logical_block_size(const struct request_queue *q)
1418 {
1419 	int retval = 512;
1420 
1421 	if (q && q->limits.logical_block_size)
1422 		retval = q->limits.logical_block_size;
1423 
1424 	return retval;
1425 }
1426 
bdev_logical_block_size(struct block_device * bdev)1427 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1428 {
1429 	return queue_logical_block_size(bdev_get_queue(bdev));
1430 }
1431 
queue_physical_block_size(const struct request_queue * q)1432 static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1433 {
1434 	return q->limits.physical_block_size;
1435 }
1436 
bdev_physical_block_size(struct block_device * bdev)1437 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1438 {
1439 	return queue_physical_block_size(bdev_get_queue(bdev));
1440 }
1441 
queue_io_min(const struct request_queue * q)1442 static inline unsigned int queue_io_min(const struct request_queue *q)
1443 {
1444 	return q->limits.io_min;
1445 }
1446 
bdev_io_min(struct block_device * bdev)1447 static inline int bdev_io_min(struct block_device *bdev)
1448 {
1449 	return queue_io_min(bdev_get_queue(bdev));
1450 }
1451 
queue_io_opt(const struct request_queue * q)1452 static inline unsigned int queue_io_opt(const struct request_queue *q)
1453 {
1454 	return q->limits.io_opt;
1455 }
1456 
bdev_io_opt(struct block_device * bdev)1457 static inline int bdev_io_opt(struct block_device *bdev)
1458 {
1459 	return queue_io_opt(bdev_get_queue(bdev));
1460 }
1461 
1462 static inline unsigned int
queue_zone_write_granularity(const struct request_queue * q)1463 queue_zone_write_granularity(const struct request_queue *q)
1464 {
1465 	return q->limits.zone_write_granularity;
1466 }
1467 
1468 static inline unsigned int
bdev_zone_write_granularity(struct block_device * bdev)1469 bdev_zone_write_granularity(struct block_device *bdev)
1470 {
1471 	return queue_zone_write_granularity(bdev_get_queue(bdev));
1472 }
1473 
queue_alignment_offset(const struct request_queue * q)1474 static inline int queue_alignment_offset(const struct request_queue *q)
1475 {
1476 	if (q->limits.misaligned)
1477 		return -1;
1478 
1479 	return q->limits.alignment_offset;
1480 }
1481 
queue_limit_alignment_offset(struct queue_limits * lim,sector_t sector)1482 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
1483 {
1484 	unsigned int granularity = max(lim->physical_block_size, lim->io_min);
1485 	unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
1486 		<< SECTOR_SHIFT;
1487 
1488 	return (granularity + lim->alignment_offset - alignment) % granularity;
1489 }
1490 
bdev_alignment_offset(struct block_device * bdev)1491 static inline int bdev_alignment_offset(struct block_device *bdev)
1492 {
1493 	struct request_queue *q = bdev_get_queue(bdev);
1494 
1495 	if (q->limits.misaligned)
1496 		return -1;
1497 	if (bdev_is_partition(bdev))
1498 		return queue_limit_alignment_offset(&q->limits,
1499 				bdev->bd_start_sect);
1500 	return q->limits.alignment_offset;
1501 }
1502 
queue_discard_alignment(const struct request_queue * q)1503 static inline int queue_discard_alignment(const struct request_queue *q)
1504 {
1505 	if (q->limits.discard_misaligned)
1506 		return -1;
1507 
1508 	return q->limits.discard_alignment;
1509 }
1510 
queue_limit_discard_alignment(struct queue_limits * lim,sector_t sector)1511 static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
1512 {
1513 	unsigned int alignment, granularity, offset;
1514 
1515 	if (!lim->max_discard_sectors)
1516 		return 0;
1517 
1518 	/* Why are these in bytes, not sectors? */
1519 	alignment = lim->discard_alignment >> SECTOR_SHIFT;
1520 	granularity = lim->discard_granularity >> SECTOR_SHIFT;
1521 	if (!granularity)
1522 		return 0;
1523 
1524 	/* Offset of the partition start in 'granularity' sectors */
1525 	offset = sector_div(sector, granularity);
1526 
1527 	/* And why do we do this modulus *again* in blkdev_issue_discard()? */
1528 	offset = (granularity + alignment - offset) % granularity;
1529 
1530 	/* Turn it back into bytes, gaah */
1531 	return offset << SECTOR_SHIFT;
1532 }
1533 
1534 /*
1535  * Two cases of handling DISCARD merge:
1536  * If max_discard_segments > 1, the driver takes every bio
1537  * as a range and send them to controller together. The ranges
1538  * needn't to be contiguous.
1539  * Otherwise, the bios/requests will be handled as same as
1540  * others which should be contiguous.
1541  */
blk_discard_mergable(struct request * req)1542 static inline bool blk_discard_mergable(struct request *req)
1543 {
1544 	if (req_op(req) == REQ_OP_DISCARD &&
1545 	    queue_max_discard_segments(req->q) > 1)
1546 		return true;
1547 	return false;
1548 }
1549 
bdev_discard_alignment(struct block_device * bdev)1550 static inline int bdev_discard_alignment(struct block_device *bdev)
1551 {
1552 	struct request_queue *q = bdev_get_queue(bdev);
1553 
1554 	if (bdev_is_partition(bdev))
1555 		return queue_limit_discard_alignment(&q->limits,
1556 				bdev->bd_start_sect);
1557 	return q->limits.discard_alignment;
1558 }
1559 
bdev_write_same(struct block_device * bdev)1560 static inline unsigned int bdev_write_same(struct block_device *bdev)
1561 {
1562 	struct request_queue *q = bdev_get_queue(bdev);
1563 
1564 	if (q)
1565 		return q->limits.max_write_same_sectors;
1566 
1567 	return 0;
1568 }
1569 
bdev_write_zeroes_sectors(struct block_device * bdev)1570 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1571 {
1572 	struct request_queue *q = bdev_get_queue(bdev);
1573 
1574 	if (q)
1575 		return q->limits.max_write_zeroes_sectors;
1576 
1577 	return 0;
1578 }
1579 
bdev_zoned_model(struct block_device * bdev)1580 static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
1581 {
1582 	struct request_queue *q = bdev_get_queue(bdev);
1583 
1584 	if (q)
1585 		return blk_queue_zoned_model(q);
1586 
1587 	return BLK_ZONED_NONE;
1588 }
1589 
bdev_is_zoned(struct block_device * bdev)1590 static inline bool bdev_is_zoned(struct block_device *bdev)
1591 {
1592 	struct request_queue *q = bdev_get_queue(bdev);
1593 
1594 	if (q)
1595 		return blk_queue_is_zoned(q);
1596 
1597 	return false;
1598 }
1599 
bdev_zone_sectors(struct block_device * bdev)1600 static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1601 {
1602 	struct request_queue *q = bdev_get_queue(bdev);
1603 
1604 	if (q)
1605 		return blk_queue_zone_sectors(q);
1606 	return 0;
1607 }
1608 
bdev_max_open_zones(struct block_device * bdev)1609 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
1610 {
1611 	struct request_queue *q = bdev_get_queue(bdev);
1612 
1613 	if (q)
1614 		return queue_max_open_zones(q);
1615 	return 0;
1616 }
1617 
bdev_max_active_zones(struct block_device * bdev)1618 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
1619 {
1620 	struct request_queue *q = bdev_get_queue(bdev);
1621 
1622 	if (q)
1623 		return queue_max_active_zones(q);
1624 	return 0;
1625 }
1626 
queue_dma_alignment(const struct request_queue * q)1627 static inline int queue_dma_alignment(const struct request_queue *q)
1628 {
1629 	return q ? q->dma_alignment : 511;
1630 }
1631 
blk_rq_aligned(struct request_queue * q,unsigned long addr,unsigned int len)1632 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1633 				 unsigned int len)
1634 {
1635 	unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1636 	return !(addr & alignment) && !(len & alignment);
1637 }
1638 
1639 /* assumes size > 256 */
blksize_bits(unsigned int size)1640 static inline unsigned int blksize_bits(unsigned int size)
1641 {
1642 	unsigned int bits = 8;
1643 	do {
1644 		bits++;
1645 		size >>= 1;
1646 	} while (size > 256);
1647 	return bits;
1648 }
1649 
block_size(struct block_device * bdev)1650 static inline unsigned int block_size(struct block_device *bdev)
1651 {
1652 	return 1 << bdev->bd_inode->i_blkbits;
1653 }
1654 
1655 int kblockd_schedule_work(struct work_struct *work);
1656 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1657 
1658 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1659 	MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1660 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1661 	MODULE_ALIAS("block-major-" __stringify(major) "-*")
1662 
1663 #if defined(CONFIG_BLK_DEV_INTEGRITY)
1664 
1665 enum blk_integrity_flags {
1666 	BLK_INTEGRITY_VERIFY		= 1 << 0,
1667 	BLK_INTEGRITY_GENERATE		= 1 << 1,
1668 	BLK_INTEGRITY_DEVICE_CAPABLE	= 1 << 2,
1669 	BLK_INTEGRITY_IP_CHECKSUM	= 1 << 3,
1670 };
1671 
1672 struct blk_integrity_iter {
1673 	void			*prot_buf;
1674 	void			*data_buf;
1675 	sector_t		seed;
1676 	unsigned int		data_size;
1677 	unsigned short		interval;
1678 	const char		*disk_name;
1679 };
1680 
1681 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1682 typedef void (integrity_prepare_fn) (struct request *);
1683 typedef void (integrity_complete_fn) (struct request *, unsigned int);
1684 
1685 struct blk_integrity_profile {
1686 	integrity_processing_fn		*generate_fn;
1687 	integrity_processing_fn		*verify_fn;
1688 	integrity_prepare_fn		*prepare_fn;
1689 	integrity_complete_fn		*complete_fn;
1690 	const char			*name;
1691 };
1692 
1693 extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
1694 extern void blk_integrity_unregister(struct gendisk *);
1695 extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
1696 extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
1697 				   struct scatterlist *);
1698 extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
1699 
blk_get_integrity(struct gendisk * disk)1700 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1701 {
1702 	struct blk_integrity *bi = &disk->queue->integrity;
1703 
1704 	if (!bi->profile)
1705 		return NULL;
1706 
1707 	return bi;
1708 }
1709 
1710 static inline
bdev_get_integrity(struct block_device * bdev)1711 struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
1712 {
1713 	return blk_get_integrity(bdev->bd_disk);
1714 }
1715 
1716 static inline bool
blk_integrity_queue_supports_integrity(struct request_queue * q)1717 blk_integrity_queue_supports_integrity(struct request_queue *q)
1718 {
1719 	return q->integrity.profile;
1720 }
1721 
blk_integrity_rq(struct request * rq)1722 static inline bool blk_integrity_rq(struct request *rq)
1723 {
1724 	return rq->cmd_flags & REQ_INTEGRITY;
1725 }
1726 
blk_queue_max_integrity_segments(struct request_queue * q,unsigned int segs)1727 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1728 						    unsigned int segs)
1729 {
1730 	q->limits.max_integrity_segments = segs;
1731 }
1732 
1733 static inline unsigned short
queue_max_integrity_segments(const struct request_queue * q)1734 queue_max_integrity_segments(const struct request_queue *q)
1735 {
1736 	return q->limits.max_integrity_segments;
1737 }
1738 
1739 /**
1740  * bio_integrity_intervals - Return number of integrity intervals for a bio
1741  * @bi:		blk_integrity profile for device
1742  * @sectors:	Size of the bio in 512-byte sectors
1743  *
1744  * Description: The block layer calculates everything in 512 byte
1745  * sectors but integrity metadata is done in terms of the data integrity
1746  * interval size of the storage device.  Convert the block layer sectors
1747  * to the appropriate number of integrity intervals.
1748  */
bio_integrity_intervals(struct blk_integrity * bi,unsigned int sectors)1749 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1750 						   unsigned int sectors)
1751 {
1752 	return sectors >> (bi->interval_exp - 9);
1753 }
1754 
bio_integrity_bytes(struct blk_integrity * bi,unsigned int sectors)1755 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1756 					       unsigned int sectors)
1757 {
1758 	return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
1759 }
1760 
1761 /*
1762  * Return the first bvec that contains integrity data.  Only drivers that are
1763  * limited to a single integrity segment should use this helper.
1764  */
rq_integrity_vec(struct request * rq)1765 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1766 {
1767 	if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
1768 		return NULL;
1769 	return rq->bio->bi_integrity->bip_vec;
1770 }
1771 
1772 #else /* CONFIG_BLK_DEV_INTEGRITY */
1773 
1774 struct bio;
1775 struct block_device;
1776 struct gendisk;
1777 struct blk_integrity;
1778 
blk_integrity_rq(struct request * rq)1779 static inline int blk_integrity_rq(struct request *rq)
1780 {
1781 	return 0;
1782 }
blk_rq_count_integrity_sg(struct request_queue * q,struct bio * b)1783 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
1784 					    struct bio *b)
1785 {
1786 	return 0;
1787 }
blk_rq_map_integrity_sg(struct request_queue * q,struct bio * b,struct scatterlist * s)1788 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
1789 					  struct bio *b,
1790 					  struct scatterlist *s)
1791 {
1792 	return 0;
1793 }
bdev_get_integrity(struct block_device * b)1794 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
1795 {
1796 	return NULL;
1797 }
blk_get_integrity(struct gendisk * disk)1798 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
1799 {
1800 	return NULL;
1801 }
1802 static inline bool
blk_integrity_queue_supports_integrity(struct request_queue * q)1803 blk_integrity_queue_supports_integrity(struct request_queue *q)
1804 {
1805 	return false;
1806 }
blk_integrity_compare(struct gendisk * a,struct gendisk * b)1807 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
1808 {
1809 	return 0;
1810 }
blk_integrity_register(struct gendisk * d,struct blk_integrity * b)1811 static inline void blk_integrity_register(struct gendisk *d,
1812 					 struct blk_integrity *b)
1813 {
1814 }
blk_integrity_unregister(struct gendisk * d)1815 static inline void blk_integrity_unregister(struct gendisk *d)
1816 {
1817 }
blk_queue_max_integrity_segments(struct request_queue * q,unsigned int segs)1818 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
1819 						    unsigned int segs)
1820 {
1821 }
queue_max_integrity_segments(const struct request_queue * q)1822 static inline unsigned short queue_max_integrity_segments(const struct request_queue *q)
1823 {
1824 	return 0;
1825 }
1826 
bio_integrity_intervals(struct blk_integrity * bi,unsigned int sectors)1827 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
1828 						   unsigned int sectors)
1829 {
1830 	return 0;
1831 }
1832 
bio_integrity_bytes(struct blk_integrity * bi,unsigned int sectors)1833 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
1834 					       unsigned int sectors)
1835 {
1836 	return 0;
1837 }
1838 
rq_integrity_vec(struct request * rq)1839 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
1840 {
1841 	return NULL;
1842 }
1843 
1844 #endif /* CONFIG_BLK_DEV_INTEGRITY */
1845 
1846 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1847 
1848 bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q);
1849 
1850 void blk_ksm_unregister(struct request_queue *q);
1851 
1852 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1853 
blk_ksm_register(struct blk_keyslot_manager * ksm,struct request_queue * q)1854 static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm,
1855 				    struct request_queue *q)
1856 {
1857 	return true;
1858 }
1859 
blk_ksm_unregister(struct request_queue * q)1860 static inline void blk_ksm_unregister(struct request_queue *q) { }
1861 
1862 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1863 
1864 
1865 struct block_device_operations {
1866 	blk_qc_t (*submit_bio) (struct bio *bio);
1867 	int (*open) (struct block_device *, fmode_t);
1868 	void (*release) (struct gendisk *, fmode_t);
1869 	int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int);
1870 	int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1871 	int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
1872 	unsigned int (*check_events) (struct gendisk *disk,
1873 				      unsigned int clearing);
1874 	void (*unlock_native_capacity) (struct gendisk *);
1875 	int (*getgeo)(struct block_device *, struct hd_geometry *);
1876 	int (*set_read_only)(struct block_device *bdev, bool ro);
1877 	/* this callback is with swap_lock and sometimes page table lock held */
1878 	void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1879 	int (*report_zones)(struct gendisk *, sector_t sector,
1880 			unsigned int nr_zones, report_zones_cb cb, void *data);
1881 	char *(*devnode)(struct gendisk *disk, umode_t *mode);
1882 	struct module *owner;
1883 	const struct pr_ops *pr_ops;
1884 
1885 	/*
1886 	 * Special callback for probing GPT entry at a given sector.
1887 	 * Needed by Android devices, used by GPT scanner and MMC blk
1888 	 * driver.
1889 	 */
1890 	int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
1891 
1892 	ANDROID_KABI_RESERVE(1);
1893 	ANDROID_KABI_RESERVE(2);
1894 	ANDROID_OEM_DATA(1);
1895 };
1896 
1897 #ifdef CONFIG_COMPAT
1898 extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t,
1899 				      unsigned int, unsigned long);
1900 #else
1901 #define blkdev_compat_ptr_ioctl NULL
1902 #endif
1903 
1904 extern int bdev_read_page(struct block_device *, sector_t, struct page *);
1905 extern int bdev_write_page(struct block_device *, sector_t, struct page *,
1906 						struct writeback_control *);
1907 
1908 #ifdef CONFIG_BLK_DEV_ZONED
1909 bool blk_req_needs_zone_write_lock(struct request *rq);
1910 bool blk_req_zone_write_trylock(struct request *rq);
1911 void __blk_req_zone_write_lock(struct request *rq);
1912 void __blk_req_zone_write_unlock(struct request *rq);
1913 
blk_req_zone_write_lock(struct request * rq)1914 static inline void blk_req_zone_write_lock(struct request *rq)
1915 {
1916 	if (blk_req_needs_zone_write_lock(rq))
1917 		__blk_req_zone_write_lock(rq);
1918 }
1919 
blk_req_zone_write_unlock(struct request * rq)1920 static inline void blk_req_zone_write_unlock(struct request *rq)
1921 {
1922 	if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1923 		__blk_req_zone_write_unlock(rq);
1924 }
1925 
blk_req_zone_is_write_locked(struct request * rq)1926 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1927 {
1928 	return rq->q->seq_zones_wlock &&
1929 		test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
1930 }
1931 
blk_req_can_dispatch_to_zone(struct request * rq)1932 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1933 {
1934 	if (!blk_req_needs_zone_write_lock(rq))
1935 		return true;
1936 	return !blk_req_zone_is_write_locked(rq);
1937 }
1938 #else
blk_req_needs_zone_write_lock(struct request * rq)1939 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1940 {
1941 	return false;
1942 }
1943 
blk_req_zone_write_lock(struct request * rq)1944 static inline void blk_req_zone_write_lock(struct request *rq)
1945 {
1946 }
1947 
blk_req_zone_write_unlock(struct request * rq)1948 static inline void blk_req_zone_write_unlock(struct request *rq)
1949 {
1950 }
blk_req_zone_is_write_locked(struct request * rq)1951 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1952 {
1953 	return false;
1954 }
1955 
blk_req_can_dispatch_to_zone(struct request * rq)1956 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1957 {
1958 	return true;
1959 }
1960 #endif /* CONFIG_BLK_DEV_ZONED */
1961 
blk_wake_io_task(struct task_struct * waiter)1962 static inline void blk_wake_io_task(struct task_struct *waiter)
1963 {
1964 	/*
1965 	 * If we're polling, the task itself is doing the completions. For
1966 	 * that case, we don't need to signal a wakeup, it's enough to just
1967 	 * mark us as RUNNING.
1968 	 */
1969 	if (waiter == current)
1970 		__set_current_state(TASK_RUNNING);
1971 	else
1972 		wake_up_process(waiter);
1973 }
1974 
1975 unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1976 		unsigned int op);
1977 void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1978 		unsigned long start_time);
1979 
1980 void bio_start_io_acct_time(struct bio *bio, unsigned long start_time);
1981 unsigned long bio_start_io_acct(struct bio *bio);
1982 void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1983 		struct block_device *orig_bdev);
1984 
1985 /**
1986  * bio_end_io_acct - end I/O accounting for bio based drivers
1987  * @bio:	bio to end account for
1988  * @start:	start time returned by bio_start_io_acct()
1989  */
bio_end_io_acct(struct bio * bio,unsigned long start_time)1990 static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1991 {
1992 	return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1993 }
1994 
1995 int bdev_read_only(struct block_device *bdev);
1996 int set_blocksize(struct block_device *bdev, int size);
1997 
1998 const char *bdevname(struct block_device *bdev, char *buffer);
1999 int lookup_bdev(const char *pathname, dev_t *dev);
2000 
2001 void blkdev_show(struct seq_file *seqf, off_t offset);
2002 
2003 #define BDEVNAME_SIZE	32	/* Largest string for a blockdev identifier */
2004 #define BDEVT_SIZE	10	/* Largest string for MAJ:MIN for blkdev */
2005 #ifdef CONFIG_BLOCK
2006 #define BLKDEV_MAJOR_MAX	512
2007 #else
2008 #define BLKDEV_MAJOR_MAX	0
2009 #endif
2010 
2011 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
2012 		void *holder);
2013 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
2014 int bd_prepare_to_claim(struct block_device *bdev, void *holder);
2015 void bd_abort_claiming(struct block_device *bdev, void *holder);
2016 void blkdev_put(struct block_device *bdev, fmode_t mode);
2017 
2018 /* just for blk-cgroup, don't use elsewhere */
2019 struct block_device *blkdev_get_no_open(dev_t dev);
2020 void blkdev_put_no_open(struct block_device *bdev);
2021 
2022 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
2023 void bdev_add(struct block_device *bdev, dev_t dev);
2024 struct block_device *I_BDEV(struct inode *inode);
2025 int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
2026 		loff_t lend);
2027 
2028 #ifdef CONFIG_BLOCK
2029 void invalidate_bdev(struct block_device *bdev);
2030 int sync_blockdev(struct block_device *bdev);
2031 int sync_blockdev_nowait(struct block_device *bdev);
2032 void sync_bdevs(bool wait);
2033 #else
invalidate_bdev(struct block_device * bdev)2034 static inline void invalidate_bdev(struct block_device *bdev)
2035 {
2036 }
sync_blockdev(struct block_device * bdev)2037 static inline int sync_blockdev(struct block_device *bdev)
2038 {
2039 	return 0;
2040 }
sync_blockdev_nowait(struct block_device * bdev)2041 static inline int sync_blockdev_nowait(struct block_device *bdev)
2042 {
2043 	return 0;
2044 }
sync_bdevs(bool wait)2045 static inline void sync_bdevs(bool wait)
2046 {
2047 }
2048 #endif
2049 int fsync_bdev(struct block_device *bdev);
2050 
2051 int freeze_bdev(struct block_device *bdev);
2052 int thaw_bdev(struct block_device *bdev);
2053 
2054 #endif /* _LINUX_BLKDEV_H */
2055