blk.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BLK_INTERNAL_H
  3. #define BLK_INTERNAL_H
  4. #include <linux/bio-integrity.h>
  5. #include <linux/blk-crypto.h>
  6. #include <linux/lockdep.h>
  7. #include <linux/memblock.h> /* for max_pfn/max_low_pfn */
  8. #include <linux/sched/sysctl.h>
  9. #include <linux/timekeeping.h>
  10. #include <xen/xen.h>
  11. #include "blk-crypto-internal.h"
  12. struct elevator_type;
  13. /* Max future timer expiry for timeouts */
  14. #define BLK_MAX_TIMEOUT (5 * HZ)
  15. extern struct dentry *blk_debugfs_root;
  16. struct blk_flush_queue {
  17. spinlock_t mq_flush_lock;
  18. unsigned int flush_pending_idx:1;
  19. unsigned int flush_running_idx:1;
  20. blk_status_t rq_status;
  21. unsigned long flush_pending_since;
  22. struct list_head flush_queue[2];
  23. unsigned long flush_data_in_flight;
  24. struct request *flush_rq;
  25. };
  26. bool is_flush_rq(struct request *req);
  27. struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,
  28. gfp_t flags);
  29. void blk_free_flush_queue(struct blk_flush_queue *q);
  30. void blk_freeze_queue(struct request_queue *q);
  31. bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic);
  32. bool blk_queue_start_drain(struct request_queue *q);
  33. bool __blk_freeze_queue_start(struct request_queue *q,
  34. struct task_struct *owner);
  35. int __bio_queue_enter(struct request_queue *q, struct bio *bio);
  36. void submit_bio_noacct_nocheck(struct bio *bio);
  37. void bio_await_chain(struct bio *bio);
  38. static inline bool blk_try_enter_queue(struct request_queue *q, bool pm)
  39. {
  40. rcu_read_lock();
  41. if (!percpu_ref_tryget_live_rcu(&q->q_usage_counter))
  42. goto fail;
  43. /*
  44. * The code that increments the pm_only counter must ensure that the
  45. * counter is globally visible before the queue is unfrozen.
  46. */
  47. if (blk_queue_pm_only(q) &&
  48. (!pm || queue_rpm_status(q) == RPM_SUSPENDED))
  49. goto fail_put;
  50. rcu_read_unlock();
  51. return true;
  52. fail_put:
  53. blk_queue_exit(q);
  54. fail:
  55. rcu_read_unlock();
  56. return false;
  57. }
  58. static inline int bio_queue_enter(struct bio *bio)
  59. {
  60. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  61. if (blk_try_enter_queue(q, false)) {
  62. rwsem_acquire_read(&q->io_lockdep_map, 0, 0, _RET_IP_);
  63. rwsem_release(&q->io_lockdep_map, _RET_IP_);
  64. return 0;
  65. }
  66. return __bio_queue_enter(q, bio);
  67. }
  68. static inline void blk_wait_io(struct completion *done)
  69. {
  70. /* Prevent hang_check timer from firing at us during very long I/O */
  71. unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
  72. if (timeout)
  73. while (!wait_for_completion_io_timeout(done, timeout))
  74. ;
  75. else
  76. wait_for_completion_io(done);
  77. }
  78. #define BIO_INLINE_VECS 4
  79. struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
  80. gfp_t gfp_mask);
  81. void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs);
  82. bool bvec_try_merge_hw_page(struct request_queue *q, struct bio_vec *bv,
  83. struct page *page, unsigned len, unsigned offset,
  84. bool *same_page);
  85. static inline bool biovec_phys_mergeable(struct request_queue *q,
  86. struct bio_vec *vec1, struct bio_vec *vec2)
  87. {
  88. unsigned long mask = queue_segment_boundary(q);
  89. phys_addr_t addr1 = bvec_phys(vec1);
  90. phys_addr_t addr2 = bvec_phys(vec2);
  91. /*
  92. * Merging adjacent physical pages may not work correctly under KMSAN
  93. * if their metadata pages aren't adjacent. Just disable merging.
  94. */
  95. if (IS_ENABLED(CONFIG_KMSAN))
  96. return false;
  97. if (addr1 + vec1->bv_len != addr2)
  98. return false;
  99. if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page))
  100. return false;
  101. if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask))
  102. return false;
  103. return true;
  104. }
  105. static inline bool __bvec_gap_to_prev(const struct queue_limits *lim,
  106. struct bio_vec *bprv, unsigned int offset)
  107. {
  108. return (offset & lim->virt_boundary_mask) ||
  109. ((bprv->bv_offset + bprv->bv_len) & lim->virt_boundary_mask);
  110. }
  111. /*
  112. * Check if adding a bio_vec after bprv with offset would create a gap in
  113. * the SG list. Most drivers don't care about this, but some do.
  114. */
  115. static inline bool bvec_gap_to_prev(const struct queue_limits *lim,
  116. struct bio_vec *bprv, unsigned int offset)
  117. {
  118. if (!lim->virt_boundary_mask)
  119. return false;
  120. return __bvec_gap_to_prev(lim, bprv, offset);
  121. }
  122. static inline bool rq_mergeable(struct request *rq)
  123. {
  124. if (blk_rq_is_passthrough(rq))
  125. return false;
  126. if (req_op(rq) == REQ_OP_FLUSH)
  127. return false;
  128. if (req_op(rq) == REQ_OP_WRITE_ZEROES)
  129. return false;
  130. if (req_op(rq) == REQ_OP_ZONE_APPEND)
  131. return false;
  132. if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
  133. return false;
  134. if (rq->rq_flags & RQF_NOMERGE_FLAGS)
  135. return false;
  136. return true;
  137. }
  138. /*
  139. * There are two different ways to handle DISCARD merges:
  140. * 1) If max_discard_segments > 1, the driver treats every bio as a range and
  141. * send the bios to controller together. The ranges don't need to be
  142. * contiguous.
  143. * 2) Otherwise, the request will be normal read/write requests. The ranges
  144. * need to be contiguous.
  145. */
  146. static inline bool blk_discard_mergable(struct request *req)
  147. {
  148. if (req_op(req) == REQ_OP_DISCARD &&
  149. queue_max_discard_segments(req->q) > 1)
  150. return true;
  151. return false;
  152. }
  153. static inline unsigned int blk_rq_get_max_segments(struct request *rq)
  154. {
  155. if (req_op(rq) == REQ_OP_DISCARD)
  156. return queue_max_discard_segments(rq->q);
  157. return queue_max_segments(rq->q);
  158. }
  159. static inline unsigned int blk_queue_get_max_sectors(struct request *rq)
  160. {
  161. struct request_queue *q = rq->q;
  162. enum req_op op = req_op(rq);
  163. if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
  164. return min(q->limits.max_discard_sectors,
  165. UINT_MAX >> SECTOR_SHIFT);
  166. if (unlikely(op == REQ_OP_WRITE_ZEROES))
  167. return q->limits.max_write_zeroes_sectors;
  168. if (rq->cmd_flags & REQ_ATOMIC)
  169. return q->limits.atomic_write_max_sectors;
  170. return q->limits.max_sectors;
  171. }
  172. #ifdef CONFIG_BLK_DEV_INTEGRITY
  173. void blk_flush_integrity(void);
  174. void bio_integrity_free(struct bio *bio);
  175. /*
  176. * Integrity payloads can either be owned by the submitter, in which case
  177. * bio_uninit will free them, or owned and generated by the block layer,
  178. * in which case we'll verify them here (for reads) and free them before
  179. * the bio is handed back to the submitted.
  180. */
  181. bool __bio_integrity_endio(struct bio *bio);
  182. static inline bool bio_integrity_endio(struct bio *bio)
  183. {
  184. struct bio_integrity_payload *bip = bio_integrity(bio);
  185. if (bip && (bip->bip_flags & BIP_BLOCK_INTEGRITY))
  186. return __bio_integrity_endio(bio);
  187. return true;
  188. }
  189. bool blk_integrity_merge_rq(struct request_queue *, struct request *,
  190. struct request *);
  191. bool blk_integrity_merge_bio(struct request_queue *, struct request *,
  192. struct bio *);
  193. static inline bool integrity_req_gap_back_merge(struct request *req,
  194. struct bio *next)
  195. {
  196. struct bio_integrity_payload *bip = bio_integrity(req->bio);
  197. struct bio_integrity_payload *bip_next = bio_integrity(next);
  198. return bvec_gap_to_prev(&req->q->limits,
  199. &bip->bip_vec[bip->bip_vcnt - 1],
  200. bip_next->bip_vec[0].bv_offset);
  201. }
  202. static inline bool integrity_req_gap_front_merge(struct request *req,
  203. struct bio *bio)
  204. {
  205. struct bio_integrity_payload *bip = bio_integrity(bio);
  206. struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
  207. return bvec_gap_to_prev(&req->q->limits,
  208. &bip->bip_vec[bip->bip_vcnt - 1],
  209. bip_next->bip_vec[0].bv_offset);
  210. }
  211. extern const struct attribute_group blk_integrity_attr_group;
  212. #else /* CONFIG_BLK_DEV_INTEGRITY */
  213. static inline bool blk_integrity_merge_rq(struct request_queue *rq,
  214. struct request *r1, struct request *r2)
  215. {
  216. return true;
  217. }
  218. static inline bool blk_integrity_merge_bio(struct request_queue *rq,
  219. struct request *r, struct bio *b)
  220. {
  221. return true;
  222. }
  223. static inline bool integrity_req_gap_back_merge(struct request *req,
  224. struct bio *next)
  225. {
  226. return false;
  227. }
  228. static inline bool integrity_req_gap_front_merge(struct request *req,
  229. struct bio *bio)
  230. {
  231. return false;
  232. }
  233. static inline void blk_flush_integrity(void)
  234. {
  235. }
  236. static inline bool bio_integrity_endio(struct bio *bio)
  237. {
  238. return true;
  239. }
  240. static inline void bio_integrity_free(struct bio *bio)
  241. {
  242. }
  243. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  244. unsigned long blk_rq_timeout(unsigned long timeout);
  245. void blk_add_timer(struct request *req);
  246. enum bio_merge_status {
  247. BIO_MERGE_OK,
  248. BIO_MERGE_NONE,
  249. BIO_MERGE_FAILED,
  250. };
  251. enum bio_merge_status bio_attempt_back_merge(struct request *req,
  252. struct bio *bio, unsigned int nr_segs);
  253. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  254. unsigned int nr_segs);
  255. bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
  256. struct bio *bio, unsigned int nr_segs);
  257. /*
  258. * Plug flush limits
  259. */
  260. #define BLK_MAX_REQUEST_COUNT 32
  261. #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
  262. /*
  263. * Internal elevator interface
  264. */
  265. #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
  266. bool blk_insert_flush(struct request *rq);
  267. int elevator_switch(struct request_queue *q, struct elevator_type *new_e);
  268. void elevator_disable(struct request_queue *q);
  269. void elevator_exit(struct request_queue *q);
  270. int elv_register_queue(struct request_queue *q, bool uevent);
  271. void elv_unregister_queue(struct request_queue *q);
  272. ssize_t part_size_show(struct device *dev, struct device_attribute *attr,
  273. char *buf);
  274. ssize_t part_stat_show(struct device *dev, struct device_attribute *attr,
  275. char *buf);
  276. ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
  277. char *buf);
  278. ssize_t part_fail_show(struct device *dev, struct device_attribute *attr,
  279. char *buf);
  280. ssize_t part_fail_store(struct device *dev, struct device_attribute *attr,
  281. const char *buf, size_t count);
  282. ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
  283. ssize_t part_timeout_store(struct device *, struct device_attribute *,
  284. const char *, size_t);
  285. struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim,
  286. unsigned *nsegs);
  287. struct bio *bio_split_write_zeroes(struct bio *bio,
  288. const struct queue_limits *lim, unsigned *nsegs);
  289. struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
  290. unsigned *nr_segs);
  291. struct bio *bio_split_zone_append(struct bio *bio,
  292. const struct queue_limits *lim, unsigned *nr_segs);
  293. /*
  294. * All drivers must accept single-segments bios that are smaller than PAGE_SIZE.
  295. *
  296. * This is a quick and dirty check that relies on the fact that bi_io_vec[0] is
  297. * always valid if a bio has data. The check might lead to occasional false
  298. * positives when bios are cloned, but compared to the performance impact of
  299. * cloned bios themselves the loop below doesn't matter anyway.
  300. */
  301. static inline bool bio_may_need_split(struct bio *bio,
  302. const struct queue_limits *lim)
  303. {
  304. return lim->chunk_sectors || bio->bi_vcnt != 1 ||
  305. bio->bi_io_vec->bv_len + bio->bi_io_vec->bv_offset > PAGE_SIZE;
  306. }
  307. /**
  308. * __bio_split_to_limits - split a bio to fit the queue limits
  309. * @bio: bio to be split
  310. * @lim: queue limits to split based on
  311. * @nr_segs: returns the number of segments in the returned bio
  312. *
  313. * Check if @bio needs splitting based on the queue limits, and if so split off
  314. * a bio fitting the limits from the beginning of @bio and return it. @bio is
  315. * shortened to the remainder and re-submitted.
  316. *
  317. * The split bio is allocated from @q->bio_split, which is provided by the
  318. * block layer.
  319. */
  320. static inline struct bio *__bio_split_to_limits(struct bio *bio,
  321. const struct queue_limits *lim, unsigned int *nr_segs)
  322. {
  323. switch (bio_op(bio)) {
  324. case REQ_OP_READ:
  325. case REQ_OP_WRITE:
  326. if (bio_may_need_split(bio, lim))
  327. return bio_split_rw(bio, lim, nr_segs);
  328. *nr_segs = 1;
  329. return bio;
  330. case REQ_OP_ZONE_APPEND:
  331. return bio_split_zone_append(bio, lim, nr_segs);
  332. case REQ_OP_DISCARD:
  333. case REQ_OP_SECURE_ERASE:
  334. return bio_split_discard(bio, lim, nr_segs);
  335. case REQ_OP_WRITE_ZEROES:
  336. return bio_split_write_zeroes(bio, lim, nr_segs);
  337. default:
  338. /* other operations can't be split */
  339. *nr_segs = 0;
  340. return bio;
  341. }
  342. }
  343. int ll_back_merge_fn(struct request *req, struct bio *bio,
  344. unsigned int nr_segs);
  345. bool blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  346. struct request *next);
  347. unsigned int blk_recalc_rq_segments(struct request *rq);
  348. bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
  349. enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
  350. int blk_set_default_limits(struct queue_limits *lim);
  351. void blk_apply_bdi_limits(struct backing_dev_info *bdi,
  352. struct queue_limits *lim);
  353. int blk_dev_init(void);
  354. /*
  355. * Contribute to IO statistics IFF:
  356. *
  357. * a) it's attached to a gendisk, and
  358. * b) the queue had IO stats enabled when this request was started
  359. */
  360. static inline bool blk_do_io_stat(struct request *rq)
  361. {
  362. return (rq->rq_flags & RQF_IO_STAT) && !blk_rq_is_passthrough(rq);
  363. }
  364. void update_io_ticks(struct block_device *part, unsigned long now, bool end);
  365. unsigned int part_in_flight(struct block_device *part);
  366. static inline void req_set_nomerge(struct request_queue *q, struct request *req)
  367. {
  368. req->cmd_flags |= REQ_NOMERGE;
  369. if (req == q->last_merge)
  370. q->last_merge = NULL;
  371. }
  372. /*
  373. * Internal io_context interface
  374. */
  375. struct io_cq *ioc_find_get_icq(struct request_queue *q);
  376. struct io_cq *ioc_lookup_icq(struct request_queue *q);
  377. #ifdef CONFIG_BLK_ICQ
  378. void ioc_clear_queue(struct request_queue *q);
  379. #else
  380. static inline void ioc_clear_queue(struct request_queue *q)
  381. {
  382. }
  383. #endif /* CONFIG_BLK_ICQ */
  384. struct bio *__blk_queue_bounce(struct bio *bio, struct request_queue *q);
  385. static inline bool blk_queue_may_bounce(struct request_queue *q)
  386. {
  387. return IS_ENABLED(CONFIG_BOUNCE) &&
  388. (q->limits.features & BLK_FEAT_BOUNCE_HIGH) &&
  389. max_low_pfn >= max_pfn;
  390. }
  391. static inline struct bio *blk_queue_bounce(struct bio *bio,
  392. struct request_queue *q)
  393. {
  394. if (unlikely(blk_queue_may_bounce(q) && bio_has_data(bio)))
  395. return __blk_queue_bounce(bio, q);
  396. return bio;
  397. }
  398. #ifdef CONFIG_BLK_DEV_ZONED
  399. void disk_init_zone_resources(struct gendisk *disk);
  400. void disk_free_zone_resources(struct gendisk *disk);
  401. static inline bool bio_zone_write_plugging(struct bio *bio)
  402. {
  403. return bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING);
  404. }
  405. void blk_zone_write_plug_bio_merged(struct bio *bio);
  406. void blk_zone_write_plug_init_request(struct request *rq);
  407. static inline void blk_zone_update_request_bio(struct request *rq,
  408. struct bio *bio)
  409. {
  410. /*
  411. * For zone append requests, the request sector indicates the location
  412. * at which the BIO data was written. Return this value to the BIO
  413. * issuer through the BIO iter sector.
  414. * For plugged zone writes, which include emulated zone append, we need
  415. * the original BIO sector so that blk_zone_write_plug_bio_endio() can
  416. * lookup the zone write plug.
  417. */
  418. if (req_op(rq) == REQ_OP_ZONE_APPEND ||
  419. bio_flagged(bio, BIO_EMULATES_ZONE_APPEND))
  420. bio->bi_iter.bi_sector = rq->__sector;
  421. }
  422. void blk_zone_write_plug_bio_endio(struct bio *bio);
  423. static inline void blk_zone_bio_endio(struct bio *bio)
  424. {
  425. /*
  426. * For write BIOs to zoned devices, signal the completion of the BIO so
  427. * that the next write BIO can be submitted by zone write plugging.
  428. */
  429. if (bio_zone_write_plugging(bio))
  430. blk_zone_write_plug_bio_endio(bio);
  431. }
  432. void blk_zone_write_plug_finish_request(struct request *rq);
  433. static inline void blk_zone_finish_request(struct request *rq)
  434. {
  435. if (rq->rq_flags & RQF_ZONE_WRITE_PLUGGING)
  436. blk_zone_write_plug_finish_request(rq);
  437. }
  438. int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
  439. unsigned long arg);
  440. int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
  441. unsigned int cmd, unsigned long arg);
  442. #else /* CONFIG_BLK_DEV_ZONED */
  443. static inline void disk_init_zone_resources(struct gendisk *disk)
  444. {
  445. }
  446. static inline void disk_free_zone_resources(struct gendisk *disk)
  447. {
  448. }
  449. static inline bool bio_zone_write_plugging(struct bio *bio)
  450. {
  451. return false;
  452. }
  453. static inline void blk_zone_write_plug_bio_merged(struct bio *bio)
  454. {
  455. }
  456. static inline void blk_zone_write_plug_init_request(struct request *rq)
  457. {
  458. }
  459. static inline void blk_zone_update_request_bio(struct request *rq,
  460. struct bio *bio)
  461. {
  462. }
  463. static inline void blk_zone_bio_endio(struct bio *bio)
  464. {
  465. }
  466. static inline void blk_zone_finish_request(struct request *rq)
  467. {
  468. }
  469. static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
  470. unsigned int cmd, unsigned long arg)
  471. {
  472. return -ENOTTY;
  473. }
  474. static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
  475. blk_mode_t mode, unsigned int cmd, unsigned long arg)
  476. {
  477. return -ENOTTY;
  478. }
  479. #endif /* CONFIG_BLK_DEV_ZONED */
  480. struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
  481. void bdev_add(struct block_device *bdev, dev_t dev);
  482. void bdev_unhash(struct block_device *bdev);
  483. void bdev_drop(struct block_device *bdev);
  484. int blk_alloc_ext_minor(void);
  485. void blk_free_ext_minor(unsigned int minor);
  486. #define ADDPART_FLAG_NONE 0
  487. #define ADDPART_FLAG_RAID 1
  488. #define ADDPART_FLAG_WHOLEDISK 2
  489. int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
  490. sector_t length);
  491. int bdev_del_partition(struct gendisk *disk, int partno);
  492. int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
  493. sector_t length);
  494. void drop_partition(struct block_device *part);
  495. void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors);
  496. struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,
  497. struct lock_class_key *lkclass);
  498. int bio_add_hw_page(struct request_queue *q, struct bio *bio,
  499. struct page *page, unsigned int len, unsigned int offset,
  500. unsigned int max_sectors, bool *same_page);
  501. int bio_add_hw_folio(struct request_queue *q, struct bio *bio,
  502. struct folio *folio, size_t len, size_t offset,
  503. unsigned int max_sectors, bool *same_page);
  504. /*
  505. * Clean up a page appropriately, where the page may be pinned, may have a
  506. * ref taken on it or neither.
  507. */
  508. static inline void bio_release_page(struct bio *bio, struct page *page)
  509. {
  510. if (bio_flagged(bio, BIO_PAGE_PINNED))
  511. unpin_user_page(page);
  512. }
  513. struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id);
  514. int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode);
  515. int disk_alloc_events(struct gendisk *disk);
  516. void disk_add_events(struct gendisk *disk);
  517. void disk_del_events(struct gendisk *disk);
  518. void disk_release_events(struct gendisk *disk);
  519. void disk_block_events(struct gendisk *disk);
  520. void disk_unblock_events(struct gendisk *disk);
  521. void disk_flush_events(struct gendisk *disk, unsigned int mask);
  522. extern struct device_attribute dev_attr_events;
  523. extern struct device_attribute dev_attr_events_async;
  524. extern struct device_attribute dev_attr_events_poll_msecs;
  525. extern struct attribute_group blk_trace_attr_group;
  526. blk_mode_t file_to_blk_mode(struct file *file);
  527. int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
  528. loff_t lstart, loff_t lend);
  529. long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
  530. int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags);
  531. long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
  532. extern const struct address_space_operations def_blk_aops;
  533. int disk_register_independent_access_ranges(struct gendisk *disk);
  534. void disk_unregister_independent_access_ranges(struct gendisk *disk);
  535. #ifdef CONFIG_FAIL_MAKE_REQUEST
  536. bool should_fail_request(struct block_device *part, unsigned int bytes);
  537. #else /* CONFIG_FAIL_MAKE_REQUEST */
  538. static inline bool should_fail_request(struct block_device *part,
  539. unsigned int bytes)
  540. {
  541. return false;
  542. }
  543. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  544. /*
  545. * Optimized request reference counting. Ideally we'd make timeouts be more
  546. * clever, as that's the only reason we need references at all... But until
  547. * this happens, this is faster than using refcount_t. Also see:
  548. *
  549. * abc54d634334 ("io_uring: switch to atomic_t for io_kiocb reference count")
  550. */
  551. #define req_ref_zero_or_close_to_overflow(req) \
  552. ((unsigned int) atomic_read(&(req->ref)) + 127u <= 127u)
  553. static inline bool req_ref_inc_not_zero(struct request *req)
  554. {
  555. return atomic_inc_not_zero(&req->ref);
  556. }
  557. static inline bool req_ref_put_and_test(struct request *req)
  558. {
  559. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  560. return atomic_dec_and_test(&req->ref);
  561. }
  562. static inline void req_ref_set(struct request *req, int value)
  563. {
  564. atomic_set(&req->ref, value);
  565. }
  566. static inline int req_ref_read(struct request *req)
  567. {
  568. return atomic_read(&req->ref);
  569. }
  570. static inline u64 blk_time_get_ns(void)
  571. {
  572. struct blk_plug *plug = current->plug;
  573. if (!plug || !in_task())
  574. return ktime_get_ns();
  575. /*
  576. * 0 could very well be a valid time, but rather than flag "this is
  577. * a valid timestamp" separately, just accept that we'll do an extra
  578. * ktime_get_ns() if we just happen to get 0 as the current time.
  579. */
  580. if (!plug->cur_ktime) {
  581. plug->cur_ktime = ktime_get_ns();
  582. current->flags |= PF_BLOCK_TS;
  583. }
  584. return plug->cur_ktime;
  585. }
  586. static inline ktime_t blk_time_get(void)
  587. {
  588. return ns_to_ktime(blk_time_get_ns());
  589. }
  590. /*
  591. * From most significant bit:
  592. * 1 bit: reserved for other usage, see below
  593. * 12 bits: original size of bio
  594. * 51 bits: issue time of bio
  595. */
  596. #define BIO_ISSUE_RES_BITS 1
  597. #define BIO_ISSUE_SIZE_BITS 12
  598. #define BIO_ISSUE_RES_SHIFT (64 - BIO_ISSUE_RES_BITS)
  599. #define BIO_ISSUE_SIZE_SHIFT (BIO_ISSUE_RES_SHIFT - BIO_ISSUE_SIZE_BITS)
  600. #define BIO_ISSUE_TIME_MASK ((1ULL << BIO_ISSUE_SIZE_SHIFT) - 1)
  601. #define BIO_ISSUE_SIZE_MASK \
  602. (((1ULL << BIO_ISSUE_SIZE_BITS) - 1) << BIO_ISSUE_SIZE_SHIFT)
  603. #define BIO_ISSUE_RES_MASK (~((1ULL << BIO_ISSUE_RES_SHIFT) - 1))
  604. /* Reserved bit for blk-throtl */
  605. #define BIO_ISSUE_THROTL_SKIP_LATENCY (1ULL << 63)
  606. static inline u64 __bio_issue_time(u64 time)
  607. {
  608. return time & BIO_ISSUE_TIME_MASK;
  609. }
  610. static inline u64 bio_issue_time(struct bio_issue *issue)
  611. {
  612. return __bio_issue_time(issue->value);
  613. }
  614. static inline sector_t bio_issue_size(struct bio_issue *issue)
  615. {
  616. return ((issue->value & BIO_ISSUE_SIZE_MASK) >> BIO_ISSUE_SIZE_SHIFT);
  617. }
  618. static inline void bio_issue_init(struct bio_issue *issue,
  619. sector_t size)
  620. {
  621. size &= (1ULL << BIO_ISSUE_SIZE_BITS) - 1;
  622. issue->value = ((issue->value & BIO_ISSUE_RES_MASK) |
  623. (blk_time_get_ns() & BIO_ISSUE_TIME_MASK) |
  624. ((u64)size << BIO_ISSUE_SIZE_SHIFT));
  625. }
  626. void bdev_release(struct file *bdev_file);
  627. int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,
  628. const struct blk_holder_ops *hops, struct file *bdev_file);
  629. int bdev_permission(dev_t dev, blk_mode_t mode, void *holder);
  630. void blk_integrity_generate(struct bio *bio);
  631. void blk_integrity_verify(struct bio *bio);
  632. void blk_integrity_prepare(struct request *rq);
  633. void blk_integrity_complete(struct request *rq, unsigned int nr_bytes);
  634. static inline void blk_freeze_acquire_lock(struct request_queue *q, bool
  635. disk_dead, bool queue_dying)
  636. {
  637. if (!disk_dead)
  638. rwsem_acquire(&q->io_lockdep_map, 0, 1, _RET_IP_);
  639. if (!queue_dying)
  640. rwsem_acquire(&q->q_lockdep_map, 0, 1, _RET_IP_);
  641. }
  642. static inline void blk_unfreeze_release_lock(struct request_queue *q, bool
  643. disk_dead, bool queue_dying)
  644. {
  645. if (!queue_dying)
  646. rwsem_release(&q->q_lockdep_map, _RET_IP_);
  647. if (!disk_dead)
  648. rwsem_release(&q->io_lockdep_map, _RET_IP_);
  649. }
  650. #endif /* BLK_INTERNAL_H */