blkdev.h 57 KB

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