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