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