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