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