blkdev.h 57 KB

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