]>
Commit | Line | Data |
---|---|---|
1 | #ifndef _LINUX_BLKDEV_H | |
2 | #define _LINUX_BLKDEV_H | |
3 | ||
4 | #include <linux/sched.h> | |
5 | ||
6 | #ifdef CONFIG_BLOCK | |
7 | ||
8 | #include <linux/major.h> | |
9 | #include <linux/genhd.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/llist.h> | |
12 | #include <linux/timer.h> | |
13 | #include <linux/workqueue.h> | |
14 | #include <linux/pagemap.h> | |
15 | #include <linux/backing-dev-defs.h> | |
16 | #include <linux/wait.h> | |
17 | #include <linux/mempool.h> | |
18 | #include <linux/pfn.h> | |
19 | #include <linux/bio.h> | |
20 | #include <linux/stringify.h> | |
21 | #include <linux/gfp.h> | |
22 | #include <linux/bsg.h> | |
23 | #include <linux/smp.h> | |
24 | #include <linux/rcupdate.h> | |
25 | #include <linux/percpu-refcount.h> | |
26 | #include <linux/scatterlist.h> | |
27 | #include <linux/blkzoned.h> | |
28 | ||
29 | struct module; | |
30 | struct scsi_ioctl_command; | |
31 | ||
32 | struct request_queue; | |
33 | struct elevator_queue; | |
34 | struct blk_trace; | |
35 | struct request; | |
36 | struct sg_io_hdr; | |
37 | struct bsg_job; | |
38 | struct blkcg_gq; | |
39 | struct blk_flush_queue; | |
40 | struct pr_ops; | |
41 | struct rq_wb; | |
42 | ||
43 | #define BLKDEV_MIN_RQ 4 | |
44 | #define BLKDEV_MAX_RQ 128 /* Default maximum */ | |
45 | ||
46 | /* | |
47 | * Maximum number of blkcg policies allowed to be registered concurrently. | |
48 | * Defined here to simplify include dependency. | |
49 | */ | |
50 | #define BLKCG_MAX_POLS 2 | |
51 | ||
52 | typedef void (rq_end_io_fn)(struct request *, int); | |
53 | ||
54 | #define BLK_RL_SYNCFULL (1U << 0) | |
55 | #define BLK_RL_ASYNCFULL (1U << 1) | |
56 | ||
57 | struct request_list { | |
58 | struct request_queue *q; /* the queue this rl belongs to */ | |
59 | #ifdef CONFIG_BLK_CGROUP | |
60 | struct blkcg_gq *blkg; /* blkg this request pool belongs to */ | |
61 | #endif | |
62 | /* | |
63 | * count[], starved[], and wait[] are indexed by | |
64 | * BLK_RW_SYNC/BLK_RW_ASYNC | |
65 | */ | |
66 | int count[2]; | |
67 | int starved[2]; | |
68 | mempool_t *rq_pool; | |
69 | wait_queue_head_t wait[2]; | |
70 | unsigned int flags; | |
71 | }; | |
72 | ||
73 | /* | |
74 | * request command types | |
75 | */ | |
76 | enum rq_cmd_type_bits { | |
77 | REQ_TYPE_FS = 1, /* fs request */ | |
78 | REQ_TYPE_BLOCK_PC, /* scsi command */ | |
79 | REQ_TYPE_DRV_PRIV, /* driver defined types from here */ | |
80 | }; | |
81 | ||
82 | /* | |
83 | * request flags */ | |
84 | typedef __u32 __bitwise req_flags_t; | |
85 | ||
86 | /* elevator knows about this request */ | |
87 | #define RQF_SORTED ((__force req_flags_t)(1 << 0)) | |
88 | /* drive already may have started this one */ | |
89 | #define RQF_STARTED ((__force req_flags_t)(1 << 1)) | |
90 | /* uses tagged queueing */ | |
91 | #define RQF_QUEUED ((__force req_flags_t)(1 << 2)) | |
92 | /* may not be passed by ioscheduler */ | |
93 | #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3)) | |
94 | /* request for flush sequence */ | |
95 | #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4)) | |
96 | /* merge of different types, fail separately */ | |
97 | #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5)) | |
98 | /* track inflight for MQ */ | |
99 | #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6)) | |
100 | /* don't call prep for this one */ | |
101 | #define RQF_DONTPREP ((__force req_flags_t)(1 << 7)) | |
102 | /* set for "ide_preempt" requests and also for requests for which the SCSI | |
103 | "quiesce" state must be ignored. */ | |
104 | #define RQF_PREEMPT ((__force req_flags_t)(1 << 8)) | |
105 | /* contains copies of user pages */ | |
106 | #define RQF_COPY_USER ((__force req_flags_t)(1 << 9)) | |
107 | /* vaguely specified driver internal error. Ignored by the block layer */ | |
108 | #define RQF_FAILED ((__force req_flags_t)(1 << 10)) | |
109 | /* don't warn about errors */ | |
110 | #define RQF_QUIET ((__force req_flags_t)(1 << 11)) | |
111 | /* elevator private data attached */ | |
112 | #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12)) | |
113 | /* account I/O stat */ | |
114 | #define RQF_IO_STAT ((__force req_flags_t)(1 << 13)) | |
115 | /* request came from our alloc pool */ | |
116 | #define RQF_ALLOCED ((__force req_flags_t)(1 << 14)) | |
117 | /* runtime pm request */ | |
118 | #define RQF_PM ((__force req_flags_t)(1 << 15)) | |
119 | /* on IO scheduler merge hash */ | |
120 | #define RQF_HASHED ((__force req_flags_t)(1 << 16)) | |
121 | /* IO stats tracking on */ | |
122 | #define RQF_STATS ((__force req_flags_t)(1 << 17)) | |
123 | /* Look at ->special_vec for the actual data payload instead of the | |
124 | bio chain. */ | |
125 | #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18)) | |
126 | ||
127 | /* flags that prevent us from merging requests: */ | |
128 | #define RQF_NOMERGE_FLAGS \ | |
129 | (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD) | |
130 | ||
131 | /* | |
132 | * Try to put the fields that are referenced together in the same cacheline. | |
133 | * | |
134 | * If you modify this structure, make sure to update blk_rq_init() and | |
135 | * especially blk_mq_rq_ctx_init() to take care of the added fields. | |
136 | */ | |
137 | struct request { | |
138 | struct list_head queuelist; | |
139 | union { | |
140 | struct call_single_data csd; | |
141 | u64 fifo_time; | |
142 | }; | |
143 | ||
144 | struct request_queue *q; | |
145 | struct blk_mq_ctx *mq_ctx; | |
146 | ||
147 | int cpu; | |
148 | unsigned cmd_type; | |
149 | unsigned int cmd_flags; /* op and common flags */ | |
150 | req_flags_t rq_flags; | |
151 | unsigned long atomic_flags; | |
152 | ||
153 | /* the following two fields are internal, NEVER access directly */ | |
154 | unsigned int __data_len; /* total data len */ | |
155 | int tag; | |
156 | sector_t __sector; /* sector cursor */ | |
157 | ||
158 | struct bio *bio; | |
159 | struct bio *biotail; | |
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 | }; | |
183 | ||
184 | /* | |
185 | * Three pointers are available for the IO schedulers, if they need | |
186 | * more they have to dynamically allocate it. Flush requests are | |
187 | * never put on the IO scheduler. So let the flush fields share | |
188 | * space with the elevator data. | |
189 | */ | |
190 | union { | |
191 | struct { | |
192 | struct io_cq *icq; | |
193 | void *priv[2]; | |
194 | } elv; | |
195 | ||
196 | struct { | |
197 | unsigned int seq; | |
198 | struct list_head list; | |
199 | rq_end_io_fn *saved_end_io; | |
200 | } flush; | |
201 | }; | |
202 | ||
203 | struct gendisk *rq_disk; | |
204 | struct hd_struct *part; | |
205 | unsigned long start_time; | |
206 | struct blk_issue_stat issue_stat; | |
207 | #ifdef CONFIG_BLK_CGROUP | |
208 | struct request_list *rl; /* rl this rq is alloced from */ | |
209 | unsigned long long start_time_ns; | |
210 | unsigned long long io_start_time_ns; /* when passed to hardware */ | |
211 | #endif | |
212 | /* Number of scatter-gather DMA addr+len pairs after | |
213 | * physical address coalescing is performed. | |
214 | */ | |
215 | unsigned short nr_phys_segments; | |
216 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
217 | unsigned short nr_integrity_segments; | |
218 | #endif | |
219 | ||
220 | unsigned short ioprio; | |
221 | ||
222 | int internal_tag; | |
223 | ||
224 | void *special; /* opaque pointer available for LLD use */ | |
225 | ||
226 | int errors; | |
227 | ||
228 | unsigned int extra_len; /* length of alignment and padding */ | |
229 | ||
230 | unsigned long deadline; | |
231 | struct list_head timeout_list; | |
232 | unsigned int timeout; | |
233 | int retries; | |
234 | ||
235 | /* | |
236 | * completion callback. | |
237 | */ | |
238 | rq_end_io_fn *end_io; | |
239 | void *end_io_data; | |
240 | ||
241 | /* for bidi */ | |
242 | struct request *next_rq; | |
243 | }; | |
244 | ||
245 | static inline bool blk_rq_is_passthrough(struct request *rq) | |
246 | { | |
247 | return rq->cmd_type != REQ_TYPE_FS; | |
248 | } | |
249 | ||
250 | static inline unsigned short req_get_ioprio(struct request *req) | |
251 | { | |
252 | return req->ioprio; | |
253 | } | |
254 | ||
255 | #include <linux/elevator.h> | |
256 | ||
257 | struct blk_queue_ctx; | |
258 | ||
259 | typedef void (request_fn_proc) (struct request_queue *q); | |
260 | typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio); | |
261 | typedef int (prep_rq_fn) (struct request_queue *, struct request *); | |
262 | typedef void (unprep_rq_fn) (struct request_queue *, struct request *); | |
263 | ||
264 | struct bio_vec; | |
265 | typedef void (softirq_done_fn)(struct request *); | |
266 | typedef int (dma_drain_needed_fn)(struct request *); | |
267 | typedef int (lld_busy_fn) (struct request_queue *q); | |
268 | typedef int (bsg_job_fn) (struct bsg_job *); | |
269 | typedef int (init_rq_fn)(struct request_queue *, struct request *, gfp_t); | |
270 | typedef void (exit_rq_fn)(struct request_queue *, struct request *); | |
271 | ||
272 | enum blk_eh_timer_return { | |
273 | BLK_EH_NOT_HANDLED, | |
274 | BLK_EH_HANDLED, | |
275 | BLK_EH_RESET_TIMER, | |
276 | }; | |
277 | ||
278 | typedef enum blk_eh_timer_return (rq_timed_out_fn)(struct request *); | |
279 | ||
280 | enum blk_queue_state { | |
281 | Queue_down, | |
282 | Queue_up, | |
283 | }; | |
284 | ||
285 | struct blk_queue_tag { | |
286 | struct request **tag_index; /* map of busy tags */ | |
287 | unsigned long *tag_map; /* bit map of free/busy tags */ | |
288 | int max_depth; /* what we will send to device */ | |
289 | int real_max_depth; /* what the array can hold */ | |
290 | atomic_t refcnt; /* map can be shared */ | |
291 | int alloc_policy; /* tag allocation policy */ | |
292 | int next_tag; /* next tag */ | |
293 | }; | |
294 | #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */ | |
295 | #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */ | |
296 | ||
297 | #define BLK_SCSI_MAX_CMDS (256) | |
298 | #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8)) | |
299 | ||
300 | /* | |
301 | * Zoned block device models (zoned limit). | |
302 | */ | |
303 | enum blk_zoned_model { | |
304 | BLK_ZONED_NONE, /* Regular block device */ | |
305 | BLK_ZONED_HA, /* Host-aware zoned block device */ | |
306 | BLK_ZONED_HM, /* Host-managed zoned block device */ | |
307 | }; | |
308 | ||
309 | struct queue_limits { | |
310 | unsigned long bounce_pfn; | |
311 | unsigned long seg_boundary_mask; | |
312 | unsigned long virt_boundary_mask; | |
313 | ||
314 | unsigned int max_hw_sectors; | |
315 | unsigned int max_dev_sectors; | |
316 | unsigned int chunk_sectors; | |
317 | unsigned int max_sectors; | |
318 | unsigned int max_segment_size; | |
319 | unsigned int physical_block_size; | |
320 | unsigned int alignment_offset; | |
321 | unsigned int io_min; | |
322 | unsigned int io_opt; | |
323 | unsigned int max_discard_sectors; | |
324 | unsigned int max_hw_discard_sectors; | |
325 | unsigned int max_write_same_sectors; | |
326 | unsigned int max_write_zeroes_sectors; | |
327 | unsigned int discard_granularity; | |
328 | unsigned int discard_alignment; | |
329 | ||
330 | unsigned short logical_block_size; | |
331 | unsigned short max_segments; | |
332 | unsigned short max_integrity_segments; | |
333 | ||
334 | unsigned char misaligned; | |
335 | unsigned char discard_misaligned; | |
336 | unsigned char cluster; | |
337 | unsigned char discard_zeroes_data; | |
338 | unsigned char raid_partial_stripes_expensive; | |
339 | enum blk_zoned_model zoned; | |
340 | }; | |
341 | ||
342 | #ifdef CONFIG_BLK_DEV_ZONED | |
343 | ||
344 | struct blk_zone_report_hdr { | |
345 | unsigned int nr_zones; | |
346 | u8 padding[60]; | |
347 | }; | |
348 | ||
349 | extern int blkdev_report_zones(struct block_device *bdev, | |
350 | sector_t sector, struct blk_zone *zones, | |
351 | unsigned int *nr_zones, gfp_t gfp_mask); | |
352 | extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors, | |
353 | sector_t nr_sectors, gfp_t gfp_mask); | |
354 | ||
355 | extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode, | |
356 | unsigned int cmd, unsigned long arg); | |
357 | extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode, | |
358 | unsigned int cmd, unsigned long arg); | |
359 | ||
360 | #else /* CONFIG_BLK_DEV_ZONED */ | |
361 | ||
362 | static inline int blkdev_report_zones_ioctl(struct block_device *bdev, | |
363 | fmode_t mode, unsigned int cmd, | |
364 | unsigned long arg) | |
365 | { | |
366 | return -ENOTTY; | |
367 | } | |
368 | ||
369 | static inline int blkdev_reset_zones_ioctl(struct block_device *bdev, | |
370 | fmode_t mode, unsigned int cmd, | |
371 | unsigned long arg) | |
372 | { | |
373 | return -ENOTTY; | |
374 | } | |
375 | ||
376 | #endif /* CONFIG_BLK_DEV_ZONED */ | |
377 | ||
378 | struct request_queue { | |
379 | /* | |
380 | * Together with queue_head for cacheline sharing | |
381 | */ | |
382 | struct list_head queue_head; | |
383 | struct request *last_merge; | |
384 | struct elevator_queue *elevator; | |
385 | int nr_rqs[2]; /* # allocated [a]sync rqs */ | |
386 | int nr_rqs_elvpriv; /* # allocated rqs w/ elvpriv */ | |
387 | ||
388 | struct rq_wb *rq_wb; | |
389 | ||
390 | /* | |
391 | * If blkcg is not used, @q->root_rl serves all requests. If blkcg | |
392 | * is used, root blkg allocates from @q->root_rl and all other | |
393 | * blkgs from their own blkg->rl. Which one to use should be | |
394 | * determined using bio_request_list(). | |
395 | */ | |
396 | struct request_list root_rl; | |
397 | ||
398 | request_fn_proc *request_fn; | |
399 | make_request_fn *make_request_fn; | |
400 | prep_rq_fn *prep_rq_fn; | |
401 | unprep_rq_fn *unprep_rq_fn; | |
402 | softirq_done_fn *softirq_done_fn; | |
403 | rq_timed_out_fn *rq_timed_out_fn; | |
404 | dma_drain_needed_fn *dma_drain_needed; | |
405 | lld_busy_fn *lld_busy_fn; | |
406 | init_rq_fn *init_rq_fn; | |
407 | exit_rq_fn *exit_rq_fn; | |
408 | ||
409 | const struct blk_mq_ops *mq_ops; | |
410 | ||
411 | unsigned int *mq_map; | |
412 | ||
413 | /* sw queues */ | |
414 | struct blk_mq_ctx __percpu *queue_ctx; | |
415 | unsigned int nr_queues; | |
416 | ||
417 | unsigned int queue_depth; | |
418 | ||
419 | /* hw dispatch queues */ | |
420 | struct blk_mq_hw_ctx **queue_hw_ctx; | |
421 | unsigned int nr_hw_queues; | |
422 | ||
423 | /* | |
424 | * Dispatch queue sorting | |
425 | */ | |
426 | sector_t end_sector; | |
427 | struct request *boundary_rq; | |
428 | ||
429 | /* | |
430 | * Delayed queue handling | |
431 | */ | |
432 | struct delayed_work delay_work; | |
433 | ||
434 | struct backing_dev_info backing_dev_info; | |
435 | ||
436 | /* | |
437 | * The queue owner gets to use this for whatever they like. | |
438 | * ll_rw_blk doesn't touch it. | |
439 | */ | |
440 | void *queuedata; | |
441 | ||
442 | /* | |
443 | * various queue flags, see QUEUE_* below | |
444 | */ | |
445 | unsigned long queue_flags; | |
446 | ||
447 | /* | |
448 | * ida allocated id for this queue. Used to index queues from | |
449 | * ioctx. | |
450 | */ | |
451 | int id; | |
452 | ||
453 | /* | |
454 | * queue needs bounce pages for pages above this limit | |
455 | */ | |
456 | gfp_t bounce_gfp; | |
457 | ||
458 | /* | |
459 | * protects queue structures from reentrancy. ->__queue_lock should | |
460 | * _never_ be used directly, it is queue private. always use | |
461 | * ->queue_lock. | |
462 | */ | |
463 | spinlock_t __queue_lock; | |
464 | spinlock_t *queue_lock; | |
465 | ||
466 | /* | |
467 | * queue kobject | |
468 | */ | |
469 | struct kobject kobj; | |
470 | ||
471 | /* | |
472 | * mq queue kobject | |
473 | */ | |
474 | struct kobject mq_kobj; | |
475 | ||
476 | #ifdef CONFIG_BLK_DEV_INTEGRITY | |
477 | struct blk_integrity integrity; | |
478 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
479 | ||
480 | #ifdef CONFIG_PM | |
481 | struct device *dev; | |
482 | int rpm_status; | |
483 | unsigned int nr_pending; | |
484 | #endif | |
485 | ||
486 | /* | |
487 | * queue settings | |
488 | */ | |
489 | unsigned long nr_requests; /* Max # of requests */ | |
490 | unsigned int nr_congestion_on; | |
491 | unsigned int nr_congestion_off; | |
492 | unsigned int nr_batching; | |
493 | ||
494 | unsigned int dma_drain_size; | |
495 | void *dma_drain_buffer; | |
496 | unsigned int dma_pad_mask; | |
497 | unsigned int dma_alignment; | |
498 | ||
499 | struct blk_queue_tag *queue_tags; | |
500 | struct list_head tag_busy_list; | |
501 | ||
502 | unsigned int nr_sorted; | |
503 | unsigned int in_flight[2]; | |
504 | ||
505 | struct blk_rq_stat rq_stats[2]; | |
506 | ||
507 | /* | |
508 | * Number of active block driver functions for which blk_drain_queue() | |
509 | * must wait. Must be incremented around functions that unlock the | |
510 | * queue_lock internally, e.g. scsi_request_fn(). | |
511 | */ | |
512 | unsigned int request_fn_active; | |
513 | ||
514 | unsigned int rq_timeout; | |
515 | int poll_nsec; | |
516 | struct timer_list timeout; | |
517 | struct work_struct timeout_work; | |
518 | struct list_head timeout_list; | |
519 | ||
520 | struct list_head icq_list; | |
521 | #ifdef CONFIG_BLK_CGROUP | |
522 | DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS); | |
523 | struct blkcg_gq *root_blkg; | |
524 | struct list_head blkg_list; | |
525 | #endif | |
526 | ||
527 | struct queue_limits limits; | |
528 | ||
529 | /* | |
530 | * sg stuff | |
531 | */ | |
532 | unsigned int sg_timeout; | |
533 | unsigned int sg_reserved_size; | |
534 | int node; | |
535 | #ifdef CONFIG_BLK_DEV_IO_TRACE | |
536 | struct blk_trace *blk_trace; | |
537 | #endif | |
538 | /* | |
539 | * for flush operations | |
540 | */ | |
541 | struct blk_flush_queue *fq; | |
542 | ||
543 | struct list_head requeue_list; | |
544 | spinlock_t requeue_lock; | |
545 | struct delayed_work requeue_work; | |
546 | ||
547 | struct mutex sysfs_lock; | |
548 | ||
549 | int bypass_depth; | |
550 | atomic_t mq_freeze_depth; | |
551 | ||
552 | #if defined(CONFIG_BLK_DEV_BSG) | |
553 | bsg_job_fn *bsg_job_fn; | |
554 | int bsg_job_size; | |
555 | struct bsg_class_device bsg_dev; | |
556 | #endif | |
557 | ||
558 | #ifdef CONFIG_BLK_DEV_THROTTLING | |
559 | /* Throttle data */ | |
560 | struct throtl_data *td; | |
561 | #endif | |
562 | struct rcu_head rcu_head; | |
563 | wait_queue_head_t mq_freeze_wq; | |
564 | struct percpu_ref q_usage_counter; | |
565 | struct list_head all_q_node; | |
566 | ||
567 | struct blk_mq_tag_set *tag_set; | |
568 | struct list_head tag_set_list; | |
569 | struct bio_set *bio_split; | |
570 | ||
571 | #ifdef CONFIG_DEBUG_FS | |
572 | struct dentry *debugfs_dir; | |
573 | struct dentry *mq_debugfs_dir; | |
574 | #endif | |
575 | ||
576 | bool mq_sysfs_init_done; | |
577 | ||
578 | size_t cmd_size; | |
579 | void *rq_alloc_data; | |
580 | }; | |
581 | ||
582 | #define QUEUE_FLAG_QUEUED 1 /* uses generic tag queueing */ | |
583 | #define QUEUE_FLAG_STOPPED 2 /* queue is stopped */ | |
584 | #define QUEUE_FLAG_SYNCFULL 3 /* read queue has been filled */ | |
585 | #define QUEUE_FLAG_ASYNCFULL 4 /* write queue has been filled */ | |
586 | #define QUEUE_FLAG_DYING 5 /* queue being torn down */ | |
587 | #define QUEUE_FLAG_BYPASS 6 /* act as dumb FIFO queue */ | |
588 | #define QUEUE_FLAG_BIDI 7 /* queue supports bidi requests */ | |
589 | #define QUEUE_FLAG_NOMERGES 8 /* disable merge attempts */ | |
590 | #define QUEUE_FLAG_SAME_COMP 9 /* complete on same CPU-group */ | |
591 | #define QUEUE_FLAG_FAIL_IO 10 /* fake timeout */ | |
592 | #define QUEUE_FLAG_STACKABLE 11 /* supports request stacking */ | |
593 | #define QUEUE_FLAG_NONROT 12 /* non-rotational device (SSD) */ | |
594 | #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */ | |
595 | #define QUEUE_FLAG_IO_STAT 13 /* do IO stats */ | |
596 | #define QUEUE_FLAG_DISCARD 14 /* supports DISCARD */ | |
597 | #define QUEUE_FLAG_NOXMERGES 15 /* No extended merges */ | |
598 | #define QUEUE_FLAG_ADD_RANDOM 16 /* Contributes to random pool */ | |
599 | #define QUEUE_FLAG_SECERASE 17 /* supports secure erase */ | |
600 | #define QUEUE_FLAG_SAME_FORCE 18 /* force complete on same CPU */ | |
601 | #define QUEUE_FLAG_DEAD 19 /* queue tear-down finished */ | |
602 | #define QUEUE_FLAG_INIT_DONE 20 /* queue is initialized */ | |
603 | #define QUEUE_FLAG_NO_SG_MERGE 21 /* don't attempt to merge SG segments*/ | |
604 | #define QUEUE_FLAG_POLL 22 /* IO polling enabled if set */ | |
605 | #define QUEUE_FLAG_WC 23 /* Write back caching */ | |
606 | #define QUEUE_FLAG_FUA 24 /* device supports FUA writes */ | |
607 | #define QUEUE_FLAG_FLUSH_NQ 25 /* flush not queueuable */ | |
608 | #define QUEUE_FLAG_DAX 26 /* device supports DAX */ | |
609 | #define QUEUE_FLAG_STATS 27 /* track rq completion times */ | |
610 | #define QUEUE_FLAG_RESTART 28 /* queue needs restart at completion */ | |
611 | ||
612 | #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \ | |
613 | (1 << QUEUE_FLAG_STACKABLE) | \ | |
614 | (1 << QUEUE_FLAG_SAME_COMP) | \ | |
615 | (1 << QUEUE_FLAG_ADD_RANDOM)) | |
616 | ||
617 | #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \ | |
618 | (1 << QUEUE_FLAG_STACKABLE) | \ | |
619 | (1 << QUEUE_FLAG_SAME_COMP) | \ | |
620 | (1 << QUEUE_FLAG_POLL)) | |
621 | ||
622 | static inline void queue_lockdep_assert_held(struct request_queue *q) | |
623 | { | |
624 | if (q->queue_lock) | |
625 | lockdep_assert_held(q->queue_lock); | |
626 | } | |
627 | ||
628 | static inline void queue_flag_set_unlocked(unsigned int flag, | |
629 | struct request_queue *q) | |
630 | { | |
631 | __set_bit(flag, &q->queue_flags); | |
632 | } | |
633 | ||
634 | static inline int queue_flag_test_and_clear(unsigned int flag, | |
635 | struct request_queue *q) | |
636 | { | |
637 | queue_lockdep_assert_held(q); | |
638 | ||
639 | if (test_bit(flag, &q->queue_flags)) { | |
640 | __clear_bit(flag, &q->queue_flags); | |
641 | return 1; | |
642 | } | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
647 | static inline int queue_flag_test_and_set(unsigned int flag, | |
648 | struct request_queue *q) | |
649 | { | |
650 | queue_lockdep_assert_held(q); | |
651 | ||
652 | if (!test_bit(flag, &q->queue_flags)) { | |
653 | __set_bit(flag, &q->queue_flags); | |
654 | return 0; | |
655 | } | |
656 | ||
657 | return 1; | |
658 | } | |
659 | ||
660 | static inline void queue_flag_set(unsigned int flag, struct request_queue *q) | |
661 | { | |
662 | queue_lockdep_assert_held(q); | |
663 | __set_bit(flag, &q->queue_flags); | |
664 | } | |
665 | ||
666 | static inline void queue_flag_clear_unlocked(unsigned int flag, | |
667 | struct request_queue *q) | |
668 | { | |
669 | __clear_bit(flag, &q->queue_flags); | |
670 | } | |
671 | ||
672 | static inline int queue_in_flight(struct request_queue *q) | |
673 | { | |
674 | return q->in_flight[0] + q->in_flight[1]; | |
675 | } | |
676 | ||
677 | static inline void queue_flag_clear(unsigned int flag, struct request_queue *q) | |
678 | { | |
679 | queue_lockdep_assert_held(q); | |
680 | __clear_bit(flag, &q->queue_flags); | |
681 | } | |
682 | ||
683 | #define blk_queue_tagged(q) test_bit(QUEUE_FLAG_QUEUED, &(q)->queue_flags) | |
684 | #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags) | |
685 | #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags) | |
686 | #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags) | |
687 | #define blk_queue_bypass(q) test_bit(QUEUE_FLAG_BYPASS, &(q)->queue_flags) | |
688 | #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags) | |
689 | #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags) | |
690 | #define blk_queue_noxmerges(q) \ | |
691 | test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags) | |
692 | #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags) | |
693 | #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags) | |
694 | #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags) | |
695 | #define blk_queue_stackable(q) \ | |
696 | test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags) | |
697 | #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags) | |
698 | #define blk_queue_secure_erase(q) \ | |
699 | (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags)) | |
700 | #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags) | |
701 | ||
702 | #define blk_noretry_request(rq) \ | |
703 | ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \ | |
704 | REQ_FAILFAST_DRIVER)) | |
705 | ||
706 | static inline bool blk_account_rq(struct request *rq) | |
707 | { | |
708 | return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq); | |
709 | } | |
710 | ||
711 | #define blk_rq_cpu_valid(rq) ((rq)->cpu != -1) | |
712 | #define blk_bidi_rq(rq) ((rq)->next_rq != NULL) | |
713 | /* rq->queuelist of dequeued request must be list_empty() */ | |
714 | #define blk_queued_rq(rq) (!list_empty(&(rq)->queuelist)) | |
715 | ||
716 | #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist) | |
717 | ||
718 | #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ) | |
719 | ||
720 | /* | |
721 | * Driver can handle struct request, if it either has an old style | |
722 | * request_fn defined, or is blk-mq based. | |
723 | */ | |
724 | static inline bool queue_is_rq_based(struct request_queue *q) | |
725 | { | |
726 | return q->request_fn || q->mq_ops; | |
727 | } | |
728 | ||
729 | static inline unsigned int blk_queue_cluster(struct request_queue *q) | |
730 | { | |
731 | return q->limits.cluster; | |
732 | } | |
733 | ||
734 | static inline enum blk_zoned_model | |
735 | blk_queue_zoned_model(struct request_queue *q) | |
736 | { | |
737 | return q->limits.zoned; | |
738 | } | |
739 | ||
740 | static inline bool blk_queue_is_zoned(struct request_queue *q) | |
741 | { | |
742 | switch (blk_queue_zoned_model(q)) { | |
743 | case BLK_ZONED_HA: | |
744 | case BLK_ZONED_HM: | |
745 | return true; | |
746 | default: | |
747 | return false; | |
748 | } | |
749 | } | |
750 | ||
751 | static inline unsigned int blk_queue_zone_sectors(struct request_queue *q) | |
752 | { | |
753 | return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0; | |
754 | } | |
755 | ||
756 | static inline bool rq_is_sync(struct request *rq) | |
757 | { | |
758 | return op_is_sync(rq->cmd_flags); | |
759 | } | |
760 | ||
761 | static inline bool blk_rl_full(struct request_list *rl, bool sync) | |
762 | { | |
763 | unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL; | |
764 | ||
765 | return rl->flags & flag; | |
766 | } | |
767 | ||
768 | static inline void blk_set_rl_full(struct request_list *rl, bool sync) | |
769 | { | |
770 | unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL; | |
771 | ||
772 | rl->flags |= flag; | |
773 | } | |
774 | ||
775 | static inline void blk_clear_rl_full(struct request_list *rl, bool sync) | |
776 | { | |
777 | unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL; | |
778 | ||
779 | rl->flags &= ~flag; | |
780 | } | |
781 | ||
782 | static inline bool rq_mergeable(struct request *rq) | |
783 | { | |
784 | if (blk_rq_is_passthrough(rq)) | |
785 | return false; | |
786 | ||
787 | if (req_op(rq) == REQ_OP_FLUSH) | |
788 | return false; | |
789 | ||
790 | if (req_op(rq) == REQ_OP_WRITE_ZEROES) | |
791 | return false; | |
792 | ||
793 | if (rq->cmd_flags & REQ_NOMERGE_FLAGS) | |
794 | return false; | |
795 | if (rq->rq_flags & RQF_NOMERGE_FLAGS) | |
796 | return false; | |
797 | ||
798 | return true; | |
799 | } | |
800 | ||
801 | static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b) | |
802 | { | |
803 | if (bio_data(a) == bio_data(b)) | |
804 | return true; | |
805 | ||
806 | return false; | |
807 | } | |
808 | ||
809 | static inline unsigned int blk_queue_depth(struct request_queue *q) | |
810 | { | |
811 | if (q->queue_depth) | |
812 | return q->queue_depth; | |
813 | ||
814 | return q->nr_requests; | |
815 | } | |
816 | ||
817 | /* | |
818 | * q->prep_rq_fn return values | |
819 | */ | |
820 | enum { | |
821 | BLKPREP_OK, /* serve it */ | |
822 | BLKPREP_KILL, /* fatal error, kill, return -EIO */ | |
823 | BLKPREP_DEFER, /* leave on queue */ | |
824 | BLKPREP_INVALID, /* invalid command, kill, return -EREMOTEIO */ | |
825 | }; | |
826 | ||
827 | extern unsigned long blk_max_low_pfn, blk_max_pfn; | |
828 | ||
829 | /* | |
830 | * standard bounce addresses: | |
831 | * | |
832 | * BLK_BOUNCE_HIGH : bounce all highmem pages | |
833 | * BLK_BOUNCE_ANY : don't bounce anything | |
834 | * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary | |
835 | */ | |
836 | ||
837 | #if BITS_PER_LONG == 32 | |
838 | #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT) | |
839 | #else | |
840 | #define BLK_BOUNCE_HIGH -1ULL | |
841 | #endif | |
842 | #define BLK_BOUNCE_ANY (-1ULL) | |
843 | #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24)) | |
844 | ||
845 | /* | |
846 | * default timeout for SG_IO if none specified | |
847 | */ | |
848 | #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ) | |
849 | #define BLK_MIN_SG_TIMEOUT (7 * HZ) | |
850 | ||
851 | #ifdef CONFIG_BOUNCE | |
852 | extern int init_emergency_isa_pool(void); | |
853 | extern void blk_queue_bounce(struct request_queue *q, struct bio **bio); | |
854 | #else | |
855 | static inline int init_emergency_isa_pool(void) | |
856 | { | |
857 | return 0; | |
858 | } | |
859 | static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio) | |
860 | { | |
861 | } | |
862 | #endif /* CONFIG_MMU */ | |
863 | ||
864 | struct rq_map_data { | |
865 | struct page **pages; | |
866 | int page_order; | |
867 | int nr_entries; | |
868 | unsigned long offset; | |
869 | int null_mapped; | |
870 | int from_user; | |
871 | }; | |
872 | ||
873 | struct req_iterator { | |
874 | struct bvec_iter iter; | |
875 | struct bio *bio; | |
876 | }; | |
877 | ||
878 | /* This should not be used directly - use rq_for_each_segment */ | |
879 | #define for_each_bio(_bio) \ | |
880 | for (; _bio; _bio = _bio->bi_next) | |
881 | #define __rq_for_each_bio(_bio, rq) \ | |
882 | if ((rq->bio)) \ | |
883 | for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next) | |
884 | ||
885 | #define rq_for_each_segment(bvl, _rq, _iter) \ | |
886 | __rq_for_each_bio(_iter.bio, _rq) \ | |
887 | bio_for_each_segment(bvl, _iter.bio, _iter.iter) | |
888 | ||
889 | #define rq_iter_last(bvec, _iter) \ | |
890 | (_iter.bio->bi_next == NULL && \ | |
891 | bio_iter_last(bvec, _iter.iter)) | |
892 | ||
893 | #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE | |
894 | # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform" | |
895 | #endif | |
896 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE | |
897 | extern void rq_flush_dcache_pages(struct request *rq); | |
898 | #else | |
899 | static inline void rq_flush_dcache_pages(struct request *rq) | |
900 | { | |
901 | } | |
902 | #endif | |
903 | ||
904 | #ifdef CONFIG_PRINTK | |
905 | #define vfs_msg(sb, level, fmt, ...) \ | |
906 | __vfs_msg(sb, level, fmt, ##__VA_ARGS__) | |
907 | #else | |
908 | #define vfs_msg(sb, level, fmt, ...) \ | |
909 | do { \ | |
910 | no_printk(fmt, ##__VA_ARGS__); \ | |
911 | __vfs_msg(sb, "", " "); \ | |
912 | } while (0) | |
913 | #endif | |
914 | ||
915 | extern int blk_register_queue(struct gendisk *disk); | |
916 | extern void blk_unregister_queue(struct gendisk *disk); | |
917 | extern blk_qc_t generic_make_request(struct bio *bio); | |
918 | extern void blk_rq_init(struct request_queue *q, struct request *rq); | |
919 | extern void blk_put_request(struct request *); | |
920 | extern void __blk_put_request(struct request_queue *, struct request *); | |
921 | extern struct request *blk_get_request(struct request_queue *, int, gfp_t); | |
922 | extern void blk_requeue_request(struct request_queue *, struct request *); | |
923 | extern int blk_lld_busy(struct request_queue *q); | |
924 | extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src, | |
925 | struct bio_set *bs, gfp_t gfp_mask, | |
926 | int (*bio_ctr)(struct bio *, struct bio *, void *), | |
927 | void *data); | |
928 | extern void blk_rq_unprep_clone(struct request *rq); | |
929 | extern int blk_insert_cloned_request(struct request_queue *q, | |
930 | struct request *rq); | |
931 | extern int blk_rq_append_bio(struct request *rq, struct bio *bio); | |
932 | extern void blk_delay_queue(struct request_queue *, unsigned long); | |
933 | extern void blk_queue_split(struct request_queue *, struct bio **, | |
934 | struct bio_set *); | |
935 | extern void blk_recount_segments(struct request_queue *, struct bio *); | |
936 | extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int); | |
937 | extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t, | |
938 | unsigned int, void __user *); | |
939 | extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t, | |
940 | unsigned int, void __user *); | |
941 | extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t, | |
942 | struct scsi_ioctl_command __user *); | |
943 | ||
944 | extern int blk_queue_enter(struct request_queue *q, bool nowait); | |
945 | extern void blk_queue_exit(struct request_queue *q); | |
946 | extern void blk_start_queue(struct request_queue *q); | |
947 | extern void blk_start_queue_async(struct request_queue *q); | |
948 | extern void blk_stop_queue(struct request_queue *q); | |
949 | extern void blk_sync_queue(struct request_queue *q); | |
950 | extern void __blk_stop_queue(struct request_queue *q); | |
951 | extern void __blk_run_queue(struct request_queue *q); | |
952 | extern void __blk_run_queue_uncond(struct request_queue *q); | |
953 | extern void blk_run_queue(struct request_queue *); | |
954 | extern void blk_run_queue_async(struct request_queue *q); | |
955 | extern void blk_mq_quiesce_queue(struct request_queue *q); | |
956 | extern int blk_rq_map_user(struct request_queue *, struct request *, | |
957 | struct rq_map_data *, void __user *, unsigned long, | |
958 | gfp_t); | |
959 | extern int blk_rq_unmap_user(struct bio *); | |
960 | extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t); | |
961 | extern int blk_rq_map_user_iov(struct request_queue *, struct request *, | |
962 | struct rq_map_data *, const struct iov_iter *, | |
963 | gfp_t); | |
964 | extern int blk_execute_rq(struct request_queue *, struct gendisk *, | |
965 | struct request *, int); | |
966 | extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *, | |
967 | struct request *, int, rq_end_io_fn *); | |
968 | ||
969 | bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie); | |
970 | ||
971 | static inline struct request_queue *bdev_get_queue(struct block_device *bdev) | |
972 | { | |
973 | return bdev->bd_disk->queue; /* this is never NULL */ | |
974 | } | |
975 | ||
976 | /* | |
977 | * blk_rq_pos() : the current sector | |
978 | * blk_rq_bytes() : bytes left in the entire request | |
979 | * blk_rq_cur_bytes() : bytes left in the current segment | |
980 | * blk_rq_err_bytes() : bytes left till the next error boundary | |
981 | * blk_rq_sectors() : sectors left in the entire request | |
982 | * blk_rq_cur_sectors() : sectors left in the current segment | |
983 | */ | |
984 | static inline sector_t blk_rq_pos(const struct request *rq) | |
985 | { | |
986 | return rq->__sector; | |
987 | } | |
988 | ||
989 | static inline unsigned int blk_rq_bytes(const struct request *rq) | |
990 | { | |
991 | return rq->__data_len; | |
992 | } | |
993 | ||
994 | static inline int blk_rq_cur_bytes(const struct request *rq) | |
995 | { | |
996 | return rq->bio ? bio_cur_bytes(rq->bio) : 0; | |
997 | } | |
998 | ||
999 | extern unsigned int blk_rq_err_bytes(const struct request *rq); | |
1000 | ||
1001 | static inline unsigned int blk_rq_sectors(const struct request *rq) | |
1002 | { | |
1003 | return blk_rq_bytes(rq) >> 9; | |
1004 | } | |
1005 | ||
1006 | static inline unsigned int blk_rq_cur_sectors(const struct request *rq) | |
1007 | { | |
1008 | return blk_rq_cur_bytes(rq) >> 9; | |
1009 | } | |
1010 | ||
1011 | /* | |
1012 | * Some commands like WRITE SAME have a payload or data transfer size which | |
1013 | * is different from the size of the request. Any driver that supports such | |
1014 | * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to | |
1015 | * calculate the data transfer size. | |
1016 | */ | |
1017 | static inline unsigned int blk_rq_payload_bytes(struct request *rq) | |
1018 | { | |
1019 | if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) | |
1020 | return rq->special_vec.bv_len; | |
1021 | return blk_rq_bytes(rq); | |
1022 | } | |
1023 | ||
1024 | static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q, | |
1025 | int op) | |
1026 | { | |
1027 | if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)) | |
1028 | return min(q->limits.max_discard_sectors, UINT_MAX >> 9); | |
1029 | ||
1030 | if (unlikely(op == REQ_OP_WRITE_SAME)) | |
1031 | return q->limits.max_write_same_sectors; | |
1032 | ||
1033 | if (unlikely(op == REQ_OP_WRITE_ZEROES)) | |
1034 | return q->limits.max_write_zeroes_sectors; | |
1035 | ||
1036 | return q->limits.max_sectors; | |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * Return maximum size of a request at given offset. Only valid for | |
1041 | * file system requests. | |
1042 | */ | |
1043 | static inline unsigned int blk_max_size_offset(struct request_queue *q, | |
1044 | sector_t offset) | |
1045 | { | |
1046 | if (!q->limits.chunk_sectors) | |
1047 | return q->limits.max_sectors; | |
1048 | ||
1049 | return q->limits.chunk_sectors - | |
1050 | (offset & (q->limits.chunk_sectors - 1)); | |
1051 | } | |
1052 | ||
1053 | static inline unsigned int blk_rq_get_max_sectors(struct request *rq, | |
1054 | sector_t offset) | |
1055 | { | |
1056 | struct request_queue *q = rq->q; | |
1057 | ||
1058 | if (blk_rq_is_passthrough(rq)) | |
1059 | return q->limits.max_hw_sectors; | |
1060 | ||
1061 | if (!q->limits.chunk_sectors || | |
1062 | req_op(rq) == REQ_OP_DISCARD || | |
1063 | req_op(rq) == REQ_OP_SECURE_ERASE) | |
1064 | return blk_queue_get_max_sectors(q, req_op(rq)); | |
1065 | ||
1066 | return min(blk_max_size_offset(q, offset), | |
1067 | blk_queue_get_max_sectors(q, req_op(rq))); | |
1068 | } | |
1069 | ||
1070 | static inline unsigned int blk_rq_count_bios(struct request *rq) | |
1071 | { | |
1072 | unsigned int nr_bios = 0; | |
1073 | struct bio *bio; | |
1074 | ||
1075 | __rq_for_each_bio(bio, rq) | |
1076 | nr_bios++; | |
1077 | ||
1078 | return nr_bios; | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * blk_rq_set_prio - associate a request with prio from ioc | |
1083 | * @rq: request of interest | |
1084 | * @ioc: target iocontext | |
1085 | * | |
1086 | * Assocate request prio with ioc prio so request based drivers | |
1087 | * can leverage priority information. | |
1088 | */ | |
1089 | static inline void blk_rq_set_prio(struct request *rq, struct io_context *ioc) | |
1090 | { | |
1091 | if (ioc) | |
1092 | rq->ioprio = ioc->ioprio; | |
1093 | } | |
1094 | ||
1095 | /* | |
1096 | * Request issue related functions. | |
1097 | */ | |
1098 | extern struct request *blk_peek_request(struct request_queue *q); | |
1099 | extern void blk_start_request(struct request *rq); | |
1100 | extern struct request *blk_fetch_request(struct request_queue *q); | |
1101 | ||
1102 | /* | |
1103 | * Request completion related functions. | |
1104 | * | |
1105 | * blk_update_request() completes given number of bytes and updates | |
1106 | * the request without completing it. | |
1107 | * | |
1108 | * blk_end_request() and friends. __blk_end_request() must be called | |
1109 | * with the request queue spinlock acquired. | |
1110 | * | |
1111 | * Several drivers define their own end_request and call | |
1112 | * blk_end_request() for parts of the original function. | |
1113 | * This prevents code duplication in drivers. | |
1114 | */ | |
1115 | extern bool blk_update_request(struct request *rq, int error, | |
1116 | unsigned int nr_bytes); | |
1117 | extern void blk_finish_request(struct request *rq, int error); | |
1118 | extern bool blk_end_request(struct request *rq, int error, | |
1119 | unsigned int nr_bytes); | |
1120 | extern void blk_end_request_all(struct request *rq, int error); | |
1121 | extern bool blk_end_request_cur(struct request *rq, int error); | |
1122 | extern bool blk_end_request_err(struct request *rq, int error); | |
1123 | extern bool __blk_end_request(struct request *rq, int error, | |
1124 | unsigned int nr_bytes); | |
1125 | extern void __blk_end_request_all(struct request *rq, int error); | |
1126 | extern bool __blk_end_request_cur(struct request *rq, int error); | |
1127 | extern bool __blk_end_request_err(struct request *rq, int error); | |
1128 | ||
1129 | extern void blk_complete_request(struct request *); | |
1130 | extern void __blk_complete_request(struct request *); | |
1131 | extern void blk_abort_request(struct request *); | |
1132 | extern void blk_unprep_request(struct request *); | |
1133 | ||
1134 | /* | |
1135 | * Access functions for manipulating queue properties | |
1136 | */ | |
1137 | extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn, | |
1138 | spinlock_t *lock, int node_id); | |
1139 | extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *); | |
1140 | extern int blk_init_allocated_queue(struct request_queue *); | |
1141 | extern void blk_cleanup_queue(struct request_queue *); | |
1142 | extern void blk_queue_make_request(struct request_queue *, make_request_fn *); | |
1143 | extern void blk_queue_bounce_limit(struct request_queue *, u64); | |
1144 | extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int); | |
1145 | extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int); | |
1146 | extern void blk_queue_max_segments(struct request_queue *, unsigned short); | |
1147 | extern void blk_queue_max_segment_size(struct request_queue *, unsigned int); | |
1148 | extern void blk_queue_max_discard_sectors(struct request_queue *q, | |
1149 | unsigned int max_discard_sectors); | |
1150 | extern void blk_queue_max_write_same_sectors(struct request_queue *q, | |
1151 | unsigned int max_write_same_sectors); | |
1152 | extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q, | |
1153 | unsigned int max_write_same_sectors); | |
1154 | extern void blk_queue_logical_block_size(struct request_queue *, unsigned short); | |
1155 | extern void blk_queue_physical_block_size(struct request_queue *, unsigned int); | |
1156 | extern void blk_queue_alignment_offset(struct request_queue *q, | |
1157 | unsigned int alignment); | |
1158 | extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min); | |
1159 | extern void blk_queue_io_min(struct request_queue *q, unsigned int min); | |
1160 | extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt); | |
1161 | extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt); | |
1162 | extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth); | |
1163 | extern void blk_set_default_limits(struct queue_limits *lim); | |
1164 | extern void blk_set_stacking_limits(struct queue_limits *lim); | |
1165 | extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, | |
1166 | sector_t offset); | |
1167 | extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev, | |
1168 | sector_t offset); | |
1169 | extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev, | |
1170 | sector_t offset); | |
1171 | extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b); | |
1172 | extern void blk_queue_dma_pad(struct request_queue *, unsigned int); | |
1173 | extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int); | |
1174 | extern int blk_queue_dma_drain(struct request_queue *q, | |
1175 | dma_drain_needed_fn *dma_drain_needed, | |
1176 | void *buf, unsigned int size); | |
1177 | extern void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn); | |
1178 | extern void blk_queue_segment_boundary(struct request_queue *, unsigned long); | |
1179 | extern void blk_queue_virt_boundary(struct request_queue *, unsigned long); | |
1180 | extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn); | |
1181 | extern void blk_queue_unprep_rq(struct request_queue *, unprep_rq_fn *ufn); | |
1182 | extern void blk_queue_dma_alignment(struct request_queue *, int); | |
1183 | extern void blk_queue_update_dma_alignment(struct request_queue *, int); | |
1184 | extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *); | |
1185 | extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *); | |
1186 | extern void blk_queue_rq_timeout(struct request_queue *, unsigned int); | |
1187 | extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable); | |
1188 | extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua); | |
1189 | extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev); | |
1190 | ||
1191 | static inline unsigned short blk_rq_nr_phys_segments(struct request *rq) | |
1192 | { | |
1193 | if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) | |
1194 | return 1; | |
1195 | return rq->nr_phys_segments; | |
1196 | } | |
1197 | ||
1198 | extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *); | |
1199 | extern void blk_dump_rq_flags(struct request *, char *); | |
1200 | extern long nr_blockdev_pages(void); | |
1201 | ||
1202 | bool __must_check blk_get_queue(struct request_queue *); | |
1203 | struct request_queue *blk_alloc_queue(gfp_t); | |
1204 | struct request_queue *blk_alloc_queue_node(gfp_t, int); | |
1205 | extern void blk_put_queue(struct request_queue *); | |
1206 | extern void blk_set_queue_dying(struct request_queue *); | |
1207 | ||
1208 | /* | |
1209 | * block layer runtime pm functions | |
1210 | */ | |
1211 | #ifdef CONFIG_PM | |
1212 | extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev); | |
1213 | extern int blk_pre_runtime_suspend(struct request_queue *q); | |
1214 | extern void blk_post_runtime_suspend(struct request_queue *q, int err); | |
1215 | extern void blk_pre_runtime_resume(struct request_queue *q); | |
1216 | extern void blk_post_runtime_resume(struct request_queue *q, int err); | |
1217 | extern void blk_set_runtime_active(struct request_queue *q); | |
1218 | #else | |
1219 | static inline void blk_pm_runtime_init(struct request_queue *q, | |
1220 | struct device *dev) {} | |
1221 | static inline int blk_pre_runtime_suspend(struct request_queue *q) | |
1222 | { | |
1223 | return -ENOSYS; | |
1224 | } | |
1225 | static inline void blk_post_runtime_suspend(struct request_queue *q, int err) {} | |
1226 | static inline void blk_pre_runtime_resume(struct request_queue *q) {} | |
1227 | static inline void blk_post_runtime_resume(struct request_queue *q, int err) {} | |
1228 | static inline void blk_set_runtime_active(struct request_queue *q) {} | |
1229 | #endif | |
1230 | ||
1231 | /* | |
1232 | * blk_plug permits building a queue of related requests by holding the I/O | |
1233 | * fragments for a short period. This allows merging of sequential requests | |
1234 | * into single larger request. As the requests are moved from a per-task list to | |
1235 | * the device's request_queue in a batch, this results in improved scalability | |
1236 | * as the lock contention for request_queue lock is reduced. | |
1237 | * | |
1238 | * It is ok not to disable preemption when adding the request to the plug list | |
1239 | * or when attempting a merge, because blk_schedule_flush_list() will only flush | |
1240 | * the plug list when the task sleeps by itself. For details, please see | |
1241 | * schedule() where blk_schedule_flush_plug() is called. | |
1242 | */ | |
1243 | struct blk_plug { | |
1244 | struct list_head list; /* requests */ | |
1245 | struct list_head mq_list; /* blk-mq requests */ | |
1246 | struct list_head cb_list; /* md requires an unplug callback */ | |
1247 | }; | |
1248 | #define BLK_MAX_REQUEST_COUNT 16 | |
1249 | #define BLK_PLUG_FLUSH_SIZE (128 * 1024) | |
1250 | ||
1251 | struct blk_plug_cb; | |
1252 | typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool); | |
1253 | struct blk_plug_cb { | |
1254 | struct list_head list; | |
1255 | blk_plug_cb_fn callback; | |
1256 | void *data; | |
1257 | }; | |
1258 | extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, | |
1259 | void *data, int size); | |
1260 | extern void blk_start_plug(struct blk_plug *); | |
1261 | extern void blk_finish_plug(struct blk_plug *); | |
1262 | extern void blk_flush_plug_list(struct blk_plug *, bool); | |
1263 | ||
1264 | static inline void blk_flush_plug(struct task_struct *tsk) | |
1265 | { | |
1266 | struct blk_plug *plug = tsk->plug; | |
1267 | ||
1268 | if (plug) | |
1269 | blk_flush_plug_list(plug, false); | |
1270 | } | |
1271 | ||
1272 | static inline void blk_schedule_flush_plug(struct task_struct *tsk) | |
1273 | { | |
1274 | struct blk_plug *plug = tsk->plug; | |
1275 | ||
1276 | if (plug) | |
1277 | blk_flush_plug_list(plug, true); | |
1278 | } | |
1279 | ||
1280 | static inline bool blk_needs_flush_plug(struct task_struct *tsk) | |
1281 | { | |
1282 | struct blk_plug *plug = tsk->plug; | |
1283 | ||
1284 | return plug && | |
1285 | (!list_empty(&plug->list) || | |
1286 | !list_empty(&plug->mq_list) || | |
1287 | !list_empty(&plug->cb_list)); | |
1288 | } | |
1289 | ||
1290 | /* | |
1291 | * tag stuff | |
1292 | */ | |
1293 | extern int blk_queue_start_tag(struct request_queue *, struct request *); | |
1294 | extern struct request *blk_queue_find_tag(struct request_queue *, int); | |
1295 | extern void blk_queue_end_tag(struct request_queue *, struct request *); | |
1296 | extern int blk_queue_init_tags(struct request_queue *, int, struct blk_queue_tag *, int); | |
1297 | extern void blk_queue_free_tags(struct request_queue *); | |
1298 | extern int blk_queue_resize_tags(struct request_queue *, int); | |
1299 | extern void blk_queue_invalidate_tags(struct request_queue *); | |
1300 | extern struct blk_queue_tag *blk_init_tags(int, int); | |
1301 | extern void blk_free_tags(struct blk_queue_tag *); | |
1302 | ||
1303 | static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt, | |
1304 | int tag) | |
1305 | { | |
1306 | if (unlikely(bqt == NULL || tag >= bqt->real_max_depth)) | |
1307 | return NULL; | |
1308 | return bqt->tag_index[tag]; | |
1309 | } | |
1310 | ||
1311 | ||
1312 | #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */ | |
1313 | #define BLKDEV_DISCARD_ZERO (1 << 1) /* must reliably zero data */ | |
1314 | ||
1315 | extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *); | |
1316 | extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector, | |
1317 | sector_t nr_sects, gfp_t gfp_mask, unsigned long flags); | |
1318 | extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, | |
1319 | sector_t nr_sects, gfp_t gfp_mask, int flags, | |
1320 | struct bio **biop); | |
1321 | extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, | |
1322 | sector_t nr_sects, gfp_t gfp_mask, struct page *page); | |
1323 | extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, | |
1324 | sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, | |
1325 | bool discard); | |
1326 | extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, | |
1327 | sector_t nr_sects, gfp_t gfp_mask, bool discard); | |
1328 | static inline int sb_issue_discard(struct super_block *sb, sector_t block, | |
1329 | sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags) | |
1330 | { | |
1331 | return blkdev_issue_discard(sb->s_bdev, block << (sb->s_blocksize_bits - 9), | |
1332 | nr_blocks << (sb->s_blocksize_bits - 9), | |
1333 | gfp_mask, flags); | |
1334 | } | |
1335 | static inline int sb_issue_zeroout(struct super_block *sb, sector_t block, | |
1336 | sector_t nr_blocks, gfp_t gfp_mask) | |
1337 | { | |
1338 | return blkdev_issue_zeroout(sb->s_bdev, | |
1339 | block << (sb->s_blocksize_bits - 9), | |
1340 | nr_blocks << (sb->s_blocksize_bits - 9), | |
1341 | gfp_mask, true); | |
1342 | } | |
1343 | ||
1344 | extern int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm); | |
1345 | ||
1346 | enum blk_default_limits { | |
1347 | BLK_MAX_SEGMENTS = 128, | |
1348 | BLK_SAFE_MAX_SECTORS = 255, | |
1349 | BLK_DEF_MAX_SECTORS = 2560, | |
1350 | BLK_MAX_SEGMENT_SIZE = 65536, | |
1351 | BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL, | |
1352 | }; | |
1353 | ||
1354 | #define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist) | |
1355 | ||
1356 | static inline unsigned long queue_bounce_pfn(struct request_queue *q) | |
1357 | { | |
1358 | return q->limits.bounce_pfn; | |
1359 | } | |
1360 | ||
1361 | static inline unsigned long queue_segment_boundary(struct request_queue *q) | |
1362 | { | |
1363 | return q->limits.seg_boundary_mask; | |
1364 | } | |
1365 | ||
1366 | static inline unsigned long queue_virt_boundary(struct request_queue *q) | |
1367 | { | |
1368 | return q->limits.virt_boundary_mask; | |
1369 | } | |
1370 | ||
1371 | static inline unsigned int queue_max_sectors(struct request_queue *q) | |
1372 | { | |
1373 | return q->limits.max_sectors; | |
1374 | } | |
1375 | ||
1376 | static inline unsigned int queue_max_hw_sectors(struct request_queue *q) | |
1377 | { | |
1378 | return q->limits.max_hw_sectors; | |
1379 | } | |
1380 | ||
1381 | static inline unsigned short queue_max_segments(struct request_queue *q) | |
1382 | { | |
1383 | return q->limits.max_segments; | |
1384 | } | |
1385 | ||
1386 | static inline unsigned int queue_max_segment_size(struct request_queue *q) | |
1387 | { | |
1388 | return q->limits.max_segment_size; | |
1389 | } | |
1390 | ||
1391 | static inline unsigned short queue_logical_block_size(struct request_queue *q) | |
1392 | { | |
1393 | int retval = 512; | |
1394 | ||
1395 | if (q && q->limits.logical_block_size) | |
1396 | retval = q->limits.logical_block_size; | |
1397 | ||
1398 | return retval; | |
1399 | } | |
1400 | ||
1401 | static inline unsigned short bdev_logical_block_size(struct block_device *bdev) | |
1402 | { | |
1403 | return queue_logical_block_size(bdev_get_queue(bdev)); | |
1404 | } | |
1405 | ||
1406 | static inline unsigned int queue_physical_block_size(struct request_queue *q) | |
1407 | { | |
1408 | return q->limits.physical_block_size; | |
1409 | } | |
1410 | ||
1411 | static inline unsigned int bdev_physical_block_size(struct block_device *bdev) | |
1412 | { | |
1413 | return queue_physical_block_size(bdev_get_queue(bdev)); | |
1414 | } | |
1415 | ||
1416 | static inline unsigned int queue_io_min(struct request_queue *q) | |
1417 | { | |
1418 | return q->limits.io_min; | |
1419 | } | |
1420 | ||
1421 | static inline int bdev_io_min(struct block_device *bdev) | |
1422 | { | |
1423 | return queue_io_min(bdev_get_queue(bdev)); | |
1424 | } | |
1425 | ||
1426 | static inline unsigned int queue_io_opt(struct request_queue *q) | |
1427 | { | |
1428 | return q->limits.io_opt; | |
1429 | } | |
1430 | ||
1431 | static inline int bdev_io_opt(struct block_device *bdev) | |
1432 | { | |
1433 | return queue_io_opt(bdev_get_queue(bdev)); | |
1434 | } | |
1435 | ||
1436 | static inline int queue_alignment_offset(struct request_queue *q) | |
1437 | { | |
1438 | if (q->limits.misaligned) | |
1439 | return -1; | |
1440 | ||
1441 | return q->limits.alignment_offset; | |
1442 | } | |
1443 | ||
1444 | static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector) | |
1445 | { | |
1446 | unsigned int granularity = max(lim->physical_block_size, lim->io_min); | |
1447 | unsigned int alignment = sector_div(sector, granularity >> 9) << 9; | |
1448 | ||
1449 | return (granularity + lim->alignment_offset - alignment) % granularity; | |
1450 | } | |
1451 | ||
1452 | static inline int bdev_alignment_offset(struct block_device *bdev) | |
1453 | { | |
1454 | struct request_queue *q = bdev_get_queue(bdev); | |
1455 | ||
1456 | if (q->limits.misaligned) | |
1457 | return -1; | |
1458 | ||
1459 | if (bdev != bdev->bd_contains) | |
1460 | return bdev->bd_part->alignment_offset; | |
1461 | ||
1462 | return q->limits.alignment_offset; | |
1463 | } | |
1464 | ||
1465 | static inline int queue_discard_alignment(struct request_queue *q) | |
1466 | { | |
1467 | if (q->limits.discard_misaligned) | |
1468 | return -1; | |
1469 | ||
1470 | return q->limits.discard_alignment; | |
1471 | } | |
1472 | ||
1473 | static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector) | |
1474 | { | |
1475 | unsigned int alignment, granularity, offset; | |
1476 | ||
1477 | if (!lim->max_discard_sectors) | |
1478 | return 0; | |
1479 | ||
1480 | /* Why are these in bytes, not sectors? */ | |
1481 | alignment = lim->discard_alignment >> 9; | |
1482 | granularity = lim->discard_granularity >> 9; | |
1483 | if (!granularity) | |
1484 | return 0; | |
1485 | ||
1486 | /* Offset of the partition start in 'granularity' sectors */ | |
1487 | offset = sector_div(sector, granularity); | |
1488 | ||
1489 | /* And why do we do this modulus *again* in blkdev_issue_discard()? */ | |
1490 | offset = (granularity + alignment - offset) % granularity; | |
1491 | ||
1492 | /* Turn it back into bytes, gaah */ | |
1493 | return offset << 9; | |
1494 | } | |
1495 | ||
1496 | static inline int bdev_discard_alignment(struct block_device *bdev) | |
1497 | { | |
1498 | struct request_queue *q = bdev_get_queue(bdev); | |
1499 | ||
1500 | if (bdev != bdev->bd_contains) | |
1501 | return bdev->bd_part->discard_alignment; | |
1502 | ||
1503 | return q->limits.discard_alignment; | |
1504 | } | |
1505 | ||
1506 | static inline unsigned int queue_discard_zeroes_data(struct request_queue *q) | |
1507 | { | |
1508 | if (q->limits.max_discard_sectors && q->limits.discard_zeroes_data == 1) | |
1509 | return 1; | |
1510 | ||
1511 | return 0; | |
1512 | } | |
1513 | ||
1514 | static inline unsigned int bdev_discard_zeroes_data(struct block_device *bdev) | |
1515 | { | |
1516 | return queue_discard_zeroes_data(bdev_get_queue(bdev)); | |
1517 | } | |
1518 | ||
1519 | static inline unsigned int bdev_write_same(struct block_device *bdev) | |
1520 | { | |
1521 | struct request_queue *q = bdev_get_queue(bdev); | |
1522 | ||
1523 | if (q) | |
1524 | return q->limits.max_write_same_sectors; | |
1525 | ||
1526 | return 0; | |
1527 | } | |
1528 | ||
1529 | static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev) | |
1530 | { | |
1531 | struct request_queue *q = bdev_get_queue(bdev); | |
1532 | ||
1533 | if (q) | |
1534 | return q->limits.max_write_zeroes_sectors; | |
1535 | ||
1536 | return 0; | |
1537 | } | |
1538 | ||
1539 | static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev) | |
1540 | { | |
1541 | struct request_queue *q = bdev_get_queue(bdev); | |
1542 | ||
1543 | if (q) | |
1544 | return blk_queue_zoned_model(q); | |
1545 | ||
1546 | return BLK_ZONED_NONE; | |
1547 | } | |
1548 | ||
1549 | static inline bool bdev_is_zoned(struct block_device *bdev) | |
1550 | { | |
1551 | struct request_queue *q = bdev_get_queue(bdev); | |
1552 | ||
1553 | if (q) | |
1554 | return blk_queue_is_zoned(q); | |
1555 | ||
1556 | return false; | |
1557 | } | |
1558 | ||
1559 | static inline unsigned int bdev_zone_sectors(struct block_device *bdev) | |
1560 | { | |
1561 | struct request_queue *q = bdev_get_queue(bdev); | |
1562 | ||
1563 | if (q) | |
1564 | return blk_queue_zone_sectors(q); | |
1565 | ||
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | static inline int queue_dma_alignment(struct request_queue *q) | |
1570 | { | |
1571 | return q ? q->dma_alignment : 511; | |
1572 | } | |
1573 | ||
1574 | static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr, | |
1575 | unsigned int len) | |
1576 | { | |
1577 | unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask; | |
1578 | return !(addr & alignment) && !(len & alignment); | |
1579 | } | |
1580 | ||
1581 | /* assumes size > 256 */ | |
1582 | static inline unsigned int blksize_bits(unsigned int size) | |
1583 | { | |
1584 | unsigned int bits = 8; | |
1585 | do { | |
1586 | bits++; | |
1587 | size >>= 1; | |
1588 | } while (size > 256); | |
1589 | return bits; | |
1590 | } | |
1591 | ||
1592 | static inline unsigned int block_size(struct block_device *bdev) | |
1593 | { | |
1594 | return bdev->bd_block_size; | |
1595 | } | |
1596 | ||
1597 | static inline bool queue_flush_queueable(struct request_queue *q) | |
1598 | { | |
1599 | return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags); | |
1600 | } | |
1601 | ||
1602 | typedef struct {struct page *v;} Sector; | |
1603 | ||
1604 | unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *); | |
1605 | ||
1606 | static inline void put_dev_sector(Sector p) | |
1607 | { | |
1608 | put_page(p.v); | |
1609 | } | |
1610 | ||
1611 | static inline bool __bvec_gap_to_prev(struct request_queue *q, | |
1612 | struct bio_vec *bprv, unsigned int offset) | |
1613 | { | |
1614 | return offset || | |
1615 | ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q)); | |
1616 | } | |
1617 | ||
1618 | /* | |
1619 | * Check if adding a bio_vec after bprv with offset would create a gap in | |
1620 | * the SG list. Most drivers don't care about this, but some do. | |
1621 | */ | |
1622 | static inline bool bvec_gap_to_prev(struct request_queue *q, | |
1623 | struct bio_vec *bprv, unsigned int offset) | |
1624 | { | |
1625 | if (!queue_virt_boundary(q)) | |
1626 | return false; | |
1627 | return __bvec_gap_to_prev(q, bprv, offset); | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * Check if the two bvecs from two bios can be merged to one segment. | |
1632 | * If yes, no need to check gap between the two bios since the 1st bio | |
1633 | * and the 1st bvec in the 2nd bio can be handled in one segment. | |
1634 | */ | |
1635 | static inline bool bios_segs_mergeable(struct request_queue *q, | |
1636 | struct bio *prev, struct bio_vec *prev_last_bv, | |
1637 | struct bio_vec *next_first_bv) | |
1638 | { | |
1639 | if (!BIOVEC_PHYS_MERGEABLE(prev_last_bv, next_first_bv)) | |
1640 | return false; | |
1641 | if (!BIOVEC_SEG_BOUNDARY(q, prev_last_bv, next_first_bv)) | |
1642 | return false; | |
1643 | if (prev->bi_seg_back_size + next_first_bv->bv_len > | |
1644 | queue_max_segment_size(q)) | |
1645 | return false; | |
1646 | return true; | |
1647 | } | |
1648 | ||
1649 | static inline bool bio_will_gap(struct request_queue *q, struct bio *prev, | |
1650 | struct bio *next) | |
1651 | { | |
1652 | if (bio_has_data(prev) && queue_virt_boundary(q)) { | |
1653 | struct bio_vec pb, nb; | |
1654 | ||
1655 | bio_get_last_bvec(prev, &pb); | |
1656 | bio_get_first_bvec(next, &nb); | |
1657 | ||
1658 | if (!bios_segs_mergeable(q, prev, &pb, &nb)) | |
1659 | return __bvec_gap_to_prev(q, &pb, nb.bv_offset); | |
1660 | } | |
1661 | ||
1662 | return false; | |
1663 | } | |
1664 | ||
1665 | static inline bool req_gap_back_merge(struct request *req, struct bio *bio) | |
1666 | { | |
1667 | return bio_will_gap(req->q, req->biotail, bio); | |
1668 | } | |
1669 | ||
1670 | static inline bool req_gap_front_merge(struct request *req, struct bio *bio) | |
1671 | { | |
1672 | return bio_will_gap(req->q, bio, req->bio); | |
1673 | } | |
1674 | ||
1675 | int kblockd_schedule_work(struct work_struct *work); | |
1676 | int kblockd_schedule_work_on(int cpu, struct work_struct *work); | |
1677 | int kblockd_schedule_delayed_work(struct delayed_work *dwork, unsigned long delay); | |
1678 | int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay); | |
1679 | ||
1680 | #ifdef CONFIG_BLK_CGROUP | |
1681 | /* | |
1682 | * This should not be using sched_clock(). A real patch is in progress | |
1683 | * to fix this up, until that is in place we need to disable preemption | |
1684 | * around sched_clock() in this function and set_io_start_time_ns(). | |
1685 | */ | |
1686 | static inline void set_start_time_ns(struct request *req) | |
1687 | { | |
1688 | preempt_disable(); | |
1689 | req->start_time_ns = sched_clock(); | |
1690 | preempt_enable(); | |
1691 | } | |
1692 | ||
1693 | static inline void set_io_start_time_ns(struct request *req) | |
1694 | { | |
1695 | preempt_disable(); | |
1696 | req->io_start_time_ns = sched_clock(); | |
1697 | preempt_enable(); | |
1698 | } | |
1699 | ||
1700 | static inline uint64_t rq_start_time_ns(struct request *req) | |
1701 | { | |
1702 | return req->start_time_ns; | |
1703 | } | |
1704 | ||
1705 | static inline uint64_t rq_io_start_time_ns(struct request *req) | |
1706 | { | |
1707 | return req->io_start_time_ns; | |
1708 | } | |
1709 | #else | |
1710 | static inline void set_start_time_ns(struct request *req) {} | |
1711 | static inline void set_io_start_time_ns(struct request *req) {} | |
1712 | static inline uint64_t rq_start_time_ns(struct request *req) | |
1713 | { | |
1714 | return 0; | |
1715 | } | |
1716 | static inline uint64_t rq_io_start_time_ns(struct request *req) | |
1717 | { | |
1718 | return 0; | |
1719 | } | |
1720 | #endif | |
1721 | ||
1722 | #define MODULE_ALIAS_BLOCKDEV(major,minor) \ | |
1723 | MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor)) | |
1724 | #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \ | |
1725 | MODULE_ALIAS("block-major-" __stringify(major) "-*") | |
1726 | ||
1727 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
1728 | ||
1729 | enum blk_integrity_flags { | |
1730 | BLK_INTEGRITY_VERIFY = 1 << 0, | |
1731 | BLK_INTEGRITY_GENERATE = 1 << 1, | |
1732 | BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2, | |
1733 | BLK_INTEGRITY_IP_CHECKSUM = 1 << 3, | |
1734 | }; | |
1735 | ||
1736 | struct blk_integrity_iter { | |
1737 | void *prot_buf; | |
1738 | void *data_buf; | |
1739 | sector_t seed; | |
1740 | unsigned int data_size; | |
1741 | unsigned short interval; | |
1742 | const char *disk_name; | |
1743 | }; | |
1744 | ||
1745 | typedef int (integrity_processing_fn) (struct blk_integrity_iter *); | |
1746 | ||
1747 | struct blk_integrity_profile { | |
1748 | integrity_processing_fn *generate_fn; | |
1749 | integrity_processing_fn *verify_fn; | |
1750 | const char *name; | |
1751 | }; | |
1752 | ||
1753 | extern void blk_integrity_register(struct gendisk *, struct blk_integrity *); | |
1754 | extern void blk_integrity_unregister(struct gendisk *); | |
1755 | extern int blk_integrity_compare(struct gendisk *, struct gendisk *); | |
1756 | extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *, | |
1757 | struct scatterlist *); | |
1758 | extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *); | |
1759 | extern bool blk_integrity_merge_rq(struct request_queue *, struct request *, | |
1760 | struct request *); | |
1761 | extern bool blk_integrity_merge_bio(struct request_queue *, struct request *, | |
1762 | struct bio *); | |
1763 | ||
1764 | static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk) | |
1765 | { | |
1766 | struct blk_integrity *bi = &disk->queue->integrity; | |
1767 | ||
1768 | if (!bi->profile) | |
1769 | return NULL; | |
1770 | ||
1771 | return bi; | |
1772 | } | |
1773 | ||
1774 | static inline | |
1775 | struct blk_integrity *bdev_get_integrity(struct block_device *bdev) | |
1776 | { | |
1777 | return blk_get_integrity(bdev->bd_disk); | |
1778 | } | |
1779 | ||
1780 | static inline bool blk_integrity_rq(struct request *rq) | |
1781 | { | |
1782 | return rq->cmd_flags & REQ_INTEGRITY; | |
1783 | } | |
1784 | ||
1785 | static inline void blk_queue_max_integrity_segments(struct request_queue *q, | |
1786 | unsigned int segs) | |
1787 | { | |
1788 | q->limits.max_integrity_segments = segs; | |
1789 | } | |
1790 | ||
1791 | static inline unsigned short | |
1792 | queue_max_integrity_segments(struct request_queue *q) | |
1793 | { | |
1794 | return q->limits.max_integrity_segments; | |
1795 | } | |
1796 | ||
1797 | static inline bool integrity_req_gap_back_merge(struct request *req, | |
1798 | struct bio *next) | |
1799 | { | |
1800 | struct bio_integrity_payload *bip = bio_integrity(req->bio); | |
1801 | struct bio_integrity_payload *bip_next = bio_integrity(next); | |
1802 | ||
1803 | return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1], | |
1804 | bip_next->bip_vec[0].bv_offset); | |
1805 | } | |
1806 | ||
1807 | static inline bool integrity_req_gap_front_merge(struct request *req, | |
1808 | struct bio *bio) | |
1809 | { | |
1810 | struct bio_integrity_payload *bip = bio_integrity(bio); | |
1811 | struct bio_integrity_payload *bip_next = bio_integrity(req->bio); | |
1812 | ||
1813 | return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1], | |
1814 | bip_next->bip_vec[0].bv_offset); | |
1815 | } | |
1816 | ||
1817 | #else /* CONFIG_BLK_DEV_INTEGRITY */ | |
1818 | ||
1819 | struct bio; | |
1820 | struct block_device; | |
1821 | struct gendisk; | |
1822 | struct blk_integrity; | |
1823 | ||
1824 | static inline int blk_integrity_rq(struct request *rq) | |
1825 | { | |
1826 | return 0; | |
1827 | } | |
1828 | static inline int blk_rq_count_integrity_sg(struct request_queue *q, | |
1829 | struct bio *b) | |
1830 | { | |
1831 | return 0; | |
1832 | } | |
1833 | static inline int blk_rq_map_integrity_sg(struct request_queue *q, | |
1834 | struct bio *b, | |
1835 | struct scatterlist *s) | |
1836 | { | |
1837 | return 0; | |
1838 | } | |
1839 | static inline struct blk_integrity *bdev_get_integrity(struct block_device *b) | |
1840 | { | |
1841 | return NULL; | |
1842 | } | |
1843 | static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk) | |
1844 | { | |
1845 | return NULL; | |
1846 | } | |
1847 | static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b) | |
1848 | { | |
1849 | return 0; | |
1850 | } | |
1851 | static inline void blk_integrity_register(struct gendisk *d, | |
1852 | struct blk_integrity *b) | |
1853 | { | |
1854 | } | |
1855 | static inline void blk_integrity_unregister(struct gendisk *d) | |
1856 | { | |
1857 | } | |
1858 | static inline void blk_queue_max_integrity_segments(struct request_queue *q, | |
1859 | unsigned int segs) | |
1860 | { | |
1861 | } | |
1862 | static inline unsigned short queue_max_integrity_segments(struct request_queue *q) | |
1863 | { | |
1864 | return 0; | |
1865 | } | |
1866 | static inline bool blk_integrity_merge_rq(struct request_queue *rq, | |
1867 | struct request *r1, | |
1868 | struct request *r2) | |
1869 | { | |
1870 | return true; | |
1871 | } | |
1872 | static inline bool blk_integrity_merge_bio(struct request_queue *rq, | |
1873 | struct request *r, | |
1874 | struct bio *b) | |
1875 | { | |
1876 | return true; | |
1877 | } | |
1878 | ||
1879 | static inline bool integrity_req_gap_back_merge(struct request *req, | |
1880 | struct bio *next) | |
1881 | { | |
1882 | return false; | |
1883 | } | |
1884 | static inline bool integrity_req_gap_front_merge(struct request *req, | |
1885 | struct bio *bio) | |
1886 | { | |
1887 | return false; | |
1888 | } | |
1889 | ||
1890 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
1891 | ||
1892 | /** | |
1893 | * struct blk_dax_ctl - control and output parameters for ->direct_access | |
1894 | * @sector: (input) offset relative to a block_device | |
1895 | * @addr: (output) kernel virtual address for @sector populated by driver | |
1896 | * @pfn: (output) page frame number for @addr populated by driver | |
1897 | * @size: (input) number of bytes requested | |
1898 | */ | |
1899 | struct blk_dax_ctl { | |
1900 | sector_t sector; | |
1901 | void *addr; | |
1902 | long size; | |
1903 | pfn_t pfn; | |
1904 | }; | |
1905 | ||
1906 | struct block_device_operations { | |
1907 | int (*open) (struct block_device *, fmode_t); | |
1908 | void (*release) (struct gendisk *, fmode_t); | |
1909 | int (*rw_page)(struct block_device *, sector_t, struct page *, bool); | |
1910 | int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long); | |
1911 | int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long); | |
1912 | long (*direct_access)(struct block_device *, sector_t, void **, pfn_t *, | |
1913 | long); | |
1914 | unsigned int (*check_events) (struct gendisk *disk, | |
1915 | unsigned int clearing); | |
1916 | /* ->media_changed() is DEPRECATED, use ->check_events() instead */ | |
1917 | int (*media_changed) (struct gendisk *); | |
1918 | void (*unlock_native_capacity) (struct gendisk *); | |
1919 | int (*revalidate_disk) (struct gendisk *); | |
1920 | int (*getgeo)(struct block_device *, struct hd_geometry *); | |
1921 | /* this callback is with swap_lock and sometimes page table lock held */ | |
1922 | void (*swap_slot_free_notify) (struct block_device *, unsigned long); | |
1923 | struct module *owner; | |
1924 | const struct pr_ops *pr_ops; | |
1925 | }; | |
1926 | ||
1927 | extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int, | |
1928 | unsigned long); | |
1929 | extern int bdev_read_page(struct block_device *, sector_t, struct page *); | |
1930 | extern int bdev_write_page(struct block_device *, sector_t, struct page *, | |
1931 | struct writeback_control *); | |
1932 | extern long bdev_direct_access(struct block_device *, struct blk_dax_ctl *); | |
1933 | extern int bdev_dax_supported(struct super_block *, int); | |
1934 | extern bool bdev_dax_capable(struct block_device *); | |
1935 | #else /* CONFIG_BLOCK */ | |
1936 | ||
1937 | struct block_device; | |
1938 | ||
1939 | /* | |
1940 | * stubs for when the block layer is configured out | |
1941 | */ | |
1942 | #define buffer_heads_over_limit 0 | |
1943 | ||
1944 | static inline long nr_blockdev_pages(void) | |
1945 | { | |
1946 | return 0; | |
1947 | } | |
1948 | ||
1949 | struct blk_plug { | |
1950 | }; | |
1951 | ||
1952 | static inline void blk_start_plug(struct blk_plug *plug) | |
1953 | { | |
1954 | } | |
1955 | ||
1956 | static inline void blk_finish_plug(struct blk_plug *plug) | |
1957 | { | |
1958 | } | |
1959 | ||
1960 | static inline void blk_flush_plug(struct task_struct *task) | |
1961 | { | |
1962 | } | |
1963 | ||
1964 | static inline void blk_schedule_flush_plug(struct task_struct *task) | |
1965 | { | |
1966 | } | |
1967 | ||
1968 | ||
1969 | static inline bool blk_needs_flush_plug(struct task_struct *tsk) | |
1970 | { | |
1971 | return false; | |
1972 | } | |
1973 | ||
1974 | static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask, | |
1975 | sector_t *error_sector) | |
1976 | { | |
1977 | return 0; | |
1978 | } | |
1979 | ||
1980 | #endif /* CONFIG_BLOCK */ | |
1981 | ||
1982 | #endif |