blk.h 23 KB

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