blkdev.h 62 KB

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