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