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