blk.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BLK_INTERNAL_H
  3. #define BLK_INTERNAL_H
  4. #include <linux/idr.h>
  5. #include <linux/blk-mq.h>
  6. #include <linux/part_stat.h>
  7. #include <linux/blk-crypto.h>
  8. #include <xen/xen.h>
  9. #include "blk-crypto-internal.h"
  10. #include "blk-mq.h"
  11. #include "blk-mq-sched.h"
  12. /* Max future timer expiry for timeouts */
  13. #define BLK_MAX_TIMEOUT (5 * HZ)
  14. extern struct dentry *blk_debugfs_root;
  15. struct blk_flush_queue {
  16. unsigned int flush_pending_idx:1;
  17. unsigned int flush_running_idx:1;
  18. blk_status_t rq_status;
  19. unsigned long flush_pending_since;
  20. struct list_head flush_queue[2];
  21. struct list_head flush_data_in_flight;
  22. struct request *flush_rq;
  23. struct lock_class_key key;
  24. spinlock_t mq_flush_lock;
  25. };
  26. extern struct kmem_cache *blk_requestq_cachep;
  27. extern struct kobj_type blk_queue_ktype;
  28. extern struct ida blk_queue_ida;
  29. static inline struct blk_flush_queue *
  30. blk_get_flush_queue(struct request_queue *q, struct blk_mq_ctx *ctx)
  31. {
  32. return blk_mq_map_queue(q, REQ_OP_FLUSH, ctx)->fq;
  33. }
  34. static inline void __blk_get_queue(struct request_queue *q)
  35. {
  36. kobject_get(&q->kobj);
  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. void blk_freeze_queue(struct request_queue *q);
  43. static inline bool biovec_phys_mergeable(struct request_queue *q,
  44. struct bio_vec *vec1, struct bio_vec *vec2)
  45. {
  46. unsigned long mask = queue_segment_boundary(q);
  47. phys_addr_t addr1 = page_to_phys(vec1->bv_page) + vec1->bv_offset;
  48. phys_addr_t addr2 = page_to_phys(vec2->bv_page) + vec2->bv_offset;
  49. if (addr1 + vec1->bv_len != addr2)
  50. return false;
  51. if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page))
  52. return false;
  53. if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask))
  54. return false;
  55. return true;
  56. }
  57. static inline bool __bvec_gap_to_prev(struct request_queue *q,
  58. struct bio_vec *bprv, unsigned int offset)
  59. {
  60. return (offset & queue_virt_boundary(q)) ||
  61. ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q));
  62. }
  63. /*
  64. * Check if adding a bio_vec after bprv with offset would create a gap in
  65. * the SG list. Most drivers don't care about this, but some do.
  66. */
  67. static inline bool bvec_gap_to_prev(struct request_queue *q,
  68. struct bio_vec *bprv, unsigned int offset)
  69. {
  70. if (!queue_virt_boundary(q))
  71. return false;
  72. return __bvec_gap_to_prev(q, bprv, offset);
  73. }
  74. static inline void blk_rq_bio_prep(struct request *rq, struct bio *bio,
  75. unsigned int nr_segs)
  76. {
  77. rq->nr_phys_segments = nr_segs;
  78. rq->__data_len = bio->bi_iter.bi_size;
  79. rq->bio = rq->biotail = bio;
  80. rq->ioprio = bio_prio(bio);
  81. if (bio->bi_disk)
  82. rq->rq_disk = bio->bi_disk;
  83. }
  84. #ifdef CONFIG_BLK_DEV_INTEGRITY
  85. void blk_flush_integrity(void);
  86. bool __bio_integrity_endio(struct bio *);
  87. void bio_integrity_free(struct bio *bio);
  88. static inline bool bio_integrity_endio(struct bio *bio)
  89. {
  90. if (bio_integrity(bio))
  91. return __bio_integrity_endio(bio);
  92. return true;
  93. }
  94. bool blk_integrity_merge_rq(struct request_queue *, struct request *,
  95. struct request *);
  96. bool blk_integrity_merge_bio(struct request_queue *, struct request *,
  97. struct bio *);
  98. static inline bool integrity_req_gap_back_merge(struct request *req,
  99. struct bio *next)
  100. {
  101. struct bio_integrity_payload *bip = bio_integrity(req->bio);
  102. struct bio_integrity_payload *bip_next = bio_integrity(next);
  103. return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
  104. bip_next->bip_vec[0].bv_offset);
  105. }
  106. static inline bool integrity_req_gap_front_merge(struct request *req,
  107. struct bio *bio)
  108. {
  109. struct bio_integrity_payload *bip = bio_integrity(bio);
  110. struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
  111. return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
  112. bip_next->bip_vec[0].bv_offset);
  113. }
  114. void blk_integrity_add(struct gendisk *);
  115. void blk_integrity_del(struct gendisk *);
  116. #else /* CONFIG_BLK_DEV_INTEGRITY */
  117. static inline bool blk_integrity_merge_rq(struct request_queue *rq,
  118. struct request *r1, struct request *r2)
  119. {
  120. return true;
  121. }
  122. static inline bool blk_integrity_merge_bio(struct request_queue *rq,
  123. struct request *r, struct bio *b)
  124. {
  125. return true;
  126. }
  127. static inline bool integrity_req_gap_back_merge(struct request *req,
  128. struct bio *next)
  129. {
  130. return false;
  131. }
  132. static inline bool integrity_req_gap_front_merge(struct request *req,
  133. struct bio *bio)
  134. {
  135. return false;
  136. }
  137. static inline void blk_flush_integrity(void)
  138. {
  139. }
  140. static inline bool bio_integrity_endio(struct bio *bio)
  141. {
  142. return true;
  143. }
  144. static inline void bio_integrity_free(struct bio *bio)
  145. {
  146. }
  147. static inline void blk_integrity_add(struct gendisk *disk)
  148. {
  149. }
  150. static inline void blk_integrity_del(struct gendisk *disk)
  151. {
  152. }
  153. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  154. unsigned long blk_rq_timeout(unsigned long timeout);
  155. void blk_add_timer(struct request *req);
  156. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  157. unsigned int nr_segs, struct request **same_queue_rq);
  158. bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
  159. struct bio *bio, unsigned int nr_segs);
  160. void blk_account_io_start(struct request *req);
  161. void blk_account_io_done(struct request *req, u64 now);
  162. /*
  163. * Plug flush limits
  164. */
  165. #define BLK_MAX_REQUEST_COUNT 32
  166. #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
  167. /*
  168. * Internal elevator interface
  169. */
  170. #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
  171. void blk_insert_flush(struct request *rq);
  172. void elevator_init_mq(struct request_queue *q);
  173. int elevator_switch_mq(struct request_queue *q,
  174. struct elevator_type *new_e);
  175. void __elevator_exit(struct request_queue *, struct elevator_queue *);
  176. int elv_register_queue(struct request_queue *q, bool uevent);
  177. void elv_unregister_queue(struct request_queue *q);
  178. static inline void elevator_exit(struct request_queue *q,
  179. struct elevator_queue *e)
  180. {
  181. lockdep_assert_held(&q->sysfs_lock);
  182. blk_mq_sched_free_requests(q);
  183. __elevator_exit(q, e);
  184. }
  185. struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
  186. ssize_t part_size_show(struct device *dev, struct device_attribute *attr,
  187. char *buf);
  188. ssize_t part_stat_show(struct device *dev, struct device_attribute *attr,
  189. char *buf);
  190. ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
  191. char *buf);
  192. ssize_t part_fail_show(struct device *dev, struct device_attribute *attr,
  193. char *buf);
  194. ssize_t part_fail_store(struct device *dev, struct device_attribute *attr,
  195. const char *buf, size_t count);
  196. ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
  197. ssize_t part_timeout_store(struct device *, struct device_attribute *,
  198. const char *, size_t);
  199. void __blk_queue_split(struct bio **bio, unsigned int *nr_segs);
  200. int ll_back_merge_fn(struct request *req, struct bio *bio,
  201. unsigned int nr_segs);
  202. int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  203. struct request *next);
  204. unsigned int blk_recalc_rq_segments(struct request *rq);
  205. void blk_rq_set_mixed_merge(struct request *rq);
  206. bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
  207. enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
  208. int blk_dev_init(void);
  209. /*
  210. * Contribute to IO statistics IFF:
  211. *
  212. * a) it's attached to a gendisk, and
  213. * b) the queue had IO stats enabled when this request was started
  214. */
  215. static inline bool blk_do_io_stat(struct request *rq)
  216. {
  217. return rq->rq_disk && (rq->rq_flags & RQF_IO_STAT);
  218. }
  219. static inline void req_set_nomerge(struct request_queue *q, struct request *req)
  220. {
  221. req->cmd_flags |= REQ_NOMERGE;
  222. if (req == q->last_merge)
  223. q->last_merge = NULL;
  224. }
  225. /*
  226. * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
  227. * is defined as 'unsigned int', meantime it has to aligned to with logical
  228. * block size which is the minimum accepted unit by hardware.
  229. */
  230. static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
  231. {
  232. return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
  233. }
  234. /*
  235. * The max bio size which is aligned to q->limits.discard_granularity. This
  236. * is a hint to split large discard bio in generic block layer, then if device
  237. * driver needs to split the discard bio into smaller ones, their bi_size can
  238. * be very probably and easily aligned to discard_granularity of the device's
  239. * queue.
  240. */
  241. static inline unsigned int bio_aligned_discard_max_sectors(
  242. struct request_queue *q)
  243. {
  244. return round_down(UINT_MAX, q->limits.discard_granularity) >>
  245. SECTOR_SHIFT;
  246. }
  247. /*
  248. * Internal io_context interface
  249. */
  250. void get_io_context(struct io_context *ioc);
  251. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
  252. struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
  253. gfp_t gfp_mask);
  254. void ioc_clear_queue(struct request_queue *q);
  255. int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
  256. /*
  257. * Internal throttling interface
  258. */
  259. #ifdef CONFIG_BLK_DEV_THROTTLING
  260. extern int blk_throtl_init(struct request_queue *q);
  261. extern void blk_throtl_exit(struct request_queue *q);
  262. extern void blk_throtl_register_queue(struct request_queue *q);
  263. extern void blk_throtl_charge_bio_split(struct bio *bio);
  264. bool blk_throtl_bio(struct bio *bio);
  265. #else /* CONFIG_BLK_DEV_THROTTLING */
  266. static inline int blk_throtl_init(struct request_queue *q) { return 0; }
  267. static inline void blk_throtl_exit(struct request_queue *q) { }
  268. static inline void blk_throtl_register_queue(struct request_queue *q) { }
  269. static inline void blk_throtl_charge_bio_split(struct bio *bio) { }
  270. static inline bool blk_throtl_bio(struct bio *bio) { return false; }
  271. #endif /* CONFIG_BLK_DEV_THROTTLING */
  272. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  273. extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
  274. extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
  275. const char *page, size_t count);
  276. extern void blk_throtl_bio_endio(struct bio *bio);
  277. extern void blk_throtl_stat_add(struct request *rq, u64 time);
  278. #else
  279. static inline void blk_throtl_bio_endio(struct bio *bio) { }
  280. static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
  281. #endif
  282. #ifdef CONFIG_BOUNCE
  283. extern int init_emergency_isa_pool(void);
  284. extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
  285. #else
  286. static inline int init_emergency_isa_pool(void)
  287. {
  288. return 0;
  289. }
  290. static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
  291. {
  292. }
  293. #endif /* CONFIG_BOUNCE */
  294. #ifdef CONFIG_BLK_CGROUP_IOLATENCY
  295. extern int blk_iolatency_init(struct request_queue *q);
  296. #else
  297. static inline int blk_iolatency_init(struct request_queue *q) { return 0; }
  298. #endif
  299. struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
  300. #ifdef CONFIG_BLK_DEV_ZONED
  301. void blk_queue_free_zone_bitmaps(struct request_queue *q);
  302. #else
  303. static inline void blk_queue_free_zone_bitmaps(struct request_queue *q) {}
  304. #endif
  305. struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector);
  306. int blk_alloc_devt(struct hd_struct *part, dev_t *devt);
  307. void blk_free_devt(dev_t devt);
  308. void blk_invalidate_devt(dev_t devt);
  309. char *disk_name(struct gendisk *hd, int partno, char *buf);
  310. #define ADDPART_FLAG_NONE 0
  311. #define ADDPART_FLAG_RAID 1
  312. #define ADDPART_FLAG_WHOLEDISK 2
  313. void delete_partition(struct hd_struct *part);
  314. int bdev_add_partition(struct block_device *bdev, int partno,
  315. sector_t start, sector_t length);
  316. int bdev_del_partition(struct block_device *bdev, int partno);
  317. int bdev_resize_partition(struct block_device *bdev, int partno,
  318. sector_t start, sector_t length);
  319. int disk_expand_part_tbl(struct gendisk *disk, int target);
  320. int hd_ref_init(struct hd_struct *part);
  321. /* no need to get/put refcount of part0 */
  322. static inline int hd_struct_try_get(struct hd_struct *part)
  323. {
  324. if (part->partno)
  325. return percpu_ref_tryget_live(&part->ref);
  326. return 1;
  327. }
  328. static inline void hd_struct_put(struct hd_struct *part)
  329. {
  330. if (part->partno)
  331. percpu_ref_put(&part->ref);
  332. }
  333. static inline void hd_free_part(struct hd_struct *part)
  334. {
  335. free_percpu(part->dkstats);
  336. kfree(part->info);
  337. percpu_ref_exit(&part->ref);
  338. }
  339. /*
  340. * Any access of part->nr_sects which is not protected by partition
  341. * bd_mutex or gendisk bdev bd_mutex, should be done using this
  342. * accessor function.
  343. *
  344. * Code written along the lines of i_size_read() and i_size_write().
  345. * CONFIG_PREEMPTION case optimizes the case of UP kernel with preemption
  346. * on.
  347. */
  348. static inline sector_t part_nr_sects_read(struct hd_struct *part)
  349. {
  350. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  351. sector_t nr_sects;
  352. unsigned seq;
  353. do {
  354. seq = read_seqcount_begin(&part->nr_sects_seq);
  355. nr_sects = part->nr_sects;
  356. } while (read_seqcount_retry(&part->nr_sects_seq, seq));
  357. return nr_sects;
  358. #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
  359. sector_t nr_sects;
  360. preempt_disable();
  361. nr_sects = part->nr_sects;
  362. preempt_enable();
  363. return nr_sects;
  364. #else
  365. return part->nr_sects;
  366. #endif
  367. }
  368. /*
  369. * Should be called with mutex lock held (typically bd_mutex) of partition
  370. * to provide mutual exlusion among writers otherwise seqcount might be
  371. * left in wrong state leaving the readers spinning infinitely.
  372. */
  373. static inline void part_nr_sects_write(struct hd_struct *part, sector_t size)
  374. {
  375. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  376. preempt_disable();
  377. write_seqcount_begin(&part->nr_sects_seq);
  378. part->nr_sects = size;
  379. write_seqcount_end(&part->nr_sects_seq);
  380. preempt_enable();
  381. #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
  382. preempt_disable();
  383. part->nr_sects = size;
  384. preempt_enable();
  385. #else
  386. part->nr_sects = size;
  387. #endif
  388. }
  389. int bio_add_hw_page(struct request_queue *q, struct bio *bio,
  390. struct page *page, unsigned int len, unsigned int offset,
  391. unsigned int max_sectors, bool *same_page);
  392. #endif /* BLK_INTERNAL_H */