blkdev.h 57 KB

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