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