blkdev.h 62 KB

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