blk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "blk-mq.h"
  7. /* Amount of time in which a process may batch requests */
  8. #define BLK_BATCH_TIME (HZ/50UL)
  9. /* Number of requests a "batching" process may submit */
  10. #define BLK_BATCH_REQ 32
  11. /* Max future timer expiry for timeouts */
  12. #define BLK_MAX_TIMEOUT (5 * HZ)
  13. #ifdef CONFIG_DEBUG_FS
  14. extern struct dentry *blk_debugfs_root;
  15. #endif
  16. struct blk_flush_queue {
  17. unsigned int flush_queue_delayed:1;
  18. unsigned int flush_pending_idx:1;
  19. unsigned int flush_running_idx:1;
  20. unsigned long flush_pending_since;
  21. struct list_head flush_queue[2];
  22. struct list_head flush_data_in_flight;
  23. struct request *flush_rq;
  24. /*
  25. * flush_rq shares tag with this rq, both can't be active
  26. * at the same time
  27. */
  28. struct request *orig_rq;
  29. spinlock_t mq_flush_lock;
  30. };
  31. extern struct kmem_cache *blk_requestq_cachep;
  32. extern struct kmem_cache *request_cachep;
  33. extern struct kobj_type blk_queue_ktype;
  34. extern struct ida blk_queue_ida;
  35. static inline struct blk_flush_queue *blk_get_flush_queue(
  36. struct request_queue *q, struct blk_mq_ctx *ctx)
  37. {
  38. if (q->mq_ops)
  39. return blk_mq_map_queue(q, ctx->cpu)->fq;
  40. return q->fq;
  41. }
  42. static inline void __blk_get_queue(struct request_queue *q)
  43. {
  44. kobject_get(&q->kobj);
  45. }
  46. struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
  47. int node, int cmd_size);
  48. void blk_free_flush_queue(struct blk_flush_queue *q);
  49. int blk_init_rl(struct request_list *rl, struct request_queue *q,
  50. gfp_t gfp_mask);
  51. void blk_exit_rl(struct request_queue *q, struct request_list *rl);
  52. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  53. struct bio *bio);
  54. void blk_queue_bypass_start(struct request_queue *q);
  55. void blk_queue_bypass_end(struct request_queue *q);
  56. void __blk_queue_free_tags(struct request_queue *q);
  57. void blk_freeze_queue(struct request_queue *q);
  58. static inline void blk_queue_enter_live(struct request_queue *q)
  59. {
  60. /*
  61. * Given that running in generic_make_request() context
  62. * guarantees that a live reference against q_usage_counter has
  63. * been established, further references under that same context
  64. * need not check that the queue has been frozen (marked dead).
  65. */
  66. percpu_ref_get(&q->q_usage_counter);
  67. }
  68. #ifdef CONFIG_BLK_DEV_INTEGRITY
  69. void blk_flush_integrity(void);
  70. bool __bio_integrity_endio(struct bio *);
  71. static inline bool bio_integrity_endio(struct bio *bio)
  72. {
  73. if (bio_integrity(bio))
  74. return __bio_integrity_endio(bio);
  75. return true;
  76. }
  77. #else
  78. static inline void blk_flush_integrity(void)
  79. {
  80. }
  81. static inline bool bio_integrity_endio(struct bio *bio)
  82. {
  83. return true;
  84. }
  85. #endif
  86. void blk_timeout_work(struct work_struct *work);
  87. unsigned long blk_rq_timeout(unsigned long timeout);
  88. void blk_add_timer(struct request *req);
  89. void blk_delete_timer(struct request *);
  90. bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
  91. struct bio *bio);
  92. bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
  93. struct bio *bio);
  94. bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
  95. struct bio *bio);
  96. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  97. unsigned int *request_count,
  98. struct request **same_queue_rq);
  99. unsigned int blk_plug_queued_count(struct request_queue *q);
  100. void blk_account_io_start(struct request *req, bool new_io);
  101. void blk_account_io_completion(struct request *req, unsigned int bytes);
  102. void blk_account_io_done(struct request *req);
  103. /*
  104. * Internal atomic flags for request handling
  105. */
  106. enum rq_atomic_flags {
  107. REQ_ATOM_COMPLETE = 0,
  108. REQ_ATOM_STARTED,
  109. REQ_ATOM_POLL_SLEPT,
  110. };
  111. /*
  112. * EH timer and IO completion will both attempt to 'grab' the request, make
  113. * sure that only one of them succeeds
  114. */
  115. static inline int blk_mark_rq_complete(struct request *rq)
  116. {
  117. return test_and_set_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
  118. }
  119. static inline void blk_clear_rq_complete(struct request *rq)
  120. {
  121. clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
  122. }
  123. /*
  124. * Internal elevator interface
  125. */
  126. #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
  127. void blk_insert_flush(struct request *rq);
  128. static inline struct request *__elv_next_request(struct request_queue *q)
  129. {
  130. struct request *rq;
  131. struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
  132. WARN_ON_ONCE(q->mq_ops);
  133. while (1) {
  134. if (!list_empty(&q->queue_head)) {
  135. rq = list_entry_rq(q->queue_head.next);
  136. return rq;
  137. }
  138. /*
  139. * Flush request is running and flush request isn't queueable
  140. * in the drive, we can hold the queue till flush request is
  141. * finished. Even we don't do this, driver can't dispatch next
  142. * requests and will requeue them. And this can improve
  143. * throughput too. For example, we have request flush1, write1,
  144. * flush 2. flush1 is dispatched, then queue is hold, write1
  145. * isn't inserted to queue. After flush1 is finished, flush2
  146. * will be dispatched. Since disk cache is already clean,
  147. * flush2 will be finished very soon, so looks like flush2 is
  148. * folded to flush1.
  149. * Since the queue is hold, a flag is set to indicate the queue
  150. * should be restarted later. Please see flush_end_io() for
  151. * details.
  152. */
  153. if (fq->flush_pending_idx != fq->flush_running_idx &&
  154. !queue_flush_queueable(q)) {
  155. fq->flush_queue_delayed = 1;
  156. return NULL;
  157. }
  158. if (unlikely(blk_queue_bypass(q)) ||
  159. !q->elevator->type->ops.sq.elevator_dispatch_fn(q, 0))
  160. return NULL;
  161. }
  162. }
  163. static inline void elv_activate_rq(struct request_queue *q, struct request *rq)
  164. {
  165. struct elevator_queue *e = q->elevator;
  166. if (e->type->ops.sq.elevator_activate_req_fn)
  167. e->type->ops.sq.elevator_activate_req_fn(q, rq);
  168. }
  169. static inline void elv_deactivate_rq(struct request_queue *q, struct request *rq)
  170. {
  171. struct elevator_queue *e = q->elevator;
  172. if (e->type->ops.sq.elevator_deactivate_req_fn)
  173. e->type->ops.sq.elevator_deactivate_req_fn(q, rq);
  174. }
  175. struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
  176. #ifdef CONFIG_FAIL_IO_TIMEOUT
  177. int blk_should_fake_timeout(struct request_queue *);
  178. ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
  179. ssize_t part_timeout_store(struct device *, struct device_attribute *,
  180. const char *, size_t);
  181. #else
  182. static inline int blk_should_fake_timeout(struct request_queue *q)
  183. {
  184. return 0;
  185. }
  186. #endif
  187. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  188. struct bio *bio);
  189. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  190. struct bio *bio);
  191. struct request *attempt_back_merge(struct request_queue *q, struct request *rq);
  192. struct request *attempt_front_merge(struct request_queue *q, struct request *rq);
  193. int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  194. struct request *next);
  195. void blk_recalc_rq_segments(struct request *rq);
  196. void blk_rq_set_mixed_merge(struct request *rq);
  197. bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
  198. enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
  199. void blk_queue_congestion_threshold(struct request_queue *q);
  200. int blk_dev_init(void);
  201. /*
  202. * Return the threshold (number of used requests) at which the queue is
  203. * considered to be congested. It include a little hysteresis to keep the
  204. * context switch rate down.
  205. */
  206. static inline int queue_congestion_on_threshold(struct request_queue *q)
  207. {
  208. return q->nr_congestion_on;
  209. }
  210. /*
  211. * The threshold at which a queue is considered to be uncongested
  212. */
  213. static inline int queue_congestion_off_threshold(struct request_queue *q)
  214. {
  215. return q->nr_congestion_off;
  216. }
  217. extern int blk_update_nr_requests(struct request_queue *, unsigned int);
  218. /*
  219. * Contribute to IO statistics IFF:
  220. *
  221. * a) it's attached to a gendisk, and
  222. * b) the queue had IO stats enabled when this request was started, and
  223. * c) it's a file system request
  224. */
  225. static inline int blk_do_io_stat(struct request *rq)
  226. {
  227. return rq->rq_disk &&
  228. (rq->rq_flags & RQF_IO_STAT) &&
  229. !blk_rq_is_passthrough(rq);
  230. }
  231. static inline void req_set_nomerge(struct request_queue *q, struct request *req)
  232. {
  233. req->cmd_flags |= REQ_NOMERGE;
  234. if (req == q->last_merge)
  235. q->last_merge = NULL;
  236. }
  237. /*
  238. * Internal io_context interface
  239. */
  240. void get_io_context(struct io_context *ioc);
  241. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
  242. struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
  243. gfp_t gfp_mask);
  244. void ioc_clear_queue(struct request_queue *q);
  245. int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
  246. /**
  247. * rq_ioc - determine io_context for request allocation
  248. * @bio: request being allocated is for this bio (can be %NULL)
  249. *
  250. * Determine io_context to use for request allocation for @bio. May return
  251. * %NULL if %current->io_context doesn't exist.
  252. */
  253. static inline struct io_context *rq_ioc(struct bio *bio)
  254. {
  255. #ifdef CONFIG_BLK_CGROUP
  256. if (bio && bio->bi_ioc)
  257. return bio->bi_ioc;
  258. #endif
  259. return current->io_context;
  260. }
  261. /**
  262. * create_io_context - try to create task->io_context
  263. * @gfp_mask: allocation mask
  264. * @node: allocation node
  265. *
  266. * If %current->io_context is %NULL, allocate a new io_context and install
  267. * it. Returns the current %current->io_context which may be %NULL if
  268. * allocation failed.
  269. *
  270. * Note that this function can't be called with IRQ disabled because
  271. * task_lock which protects %current->io_context is IRQ-unsafe.
  272. */
  273. static inline struct io_context *create_io_context(gfp_t gfp_mask, int node)
  274. {
  275. WARN_ON_ONCE(irqs_disabled());
  276. if (unlikely(!current->io_context))
  277. create_task_io_context(current, gfp_mask, node);
  278. return current->io_context;
  279. }
  280. /*
  281. * Internal throttling interface
  282. */
  283. #ifdef CONFIG_BLK_DEV_THROTTLING
  284. extern void blk_throtl_drain(struct request_queue *q);
  285. extern int blk_throtl_init(struct request_queue *q);
  286. extern void blk_throtl_exit(struct request_queue *q);
  287. extern void blk_throtl_register_queue(struct request_queue *q);
  288. #else /* CONFIG_BLK_DEV_THROTTLING */
  289. static inline void blk_throtl_drain(struct request_queue *q) { }
  290. static inline int blk_throtl_init(struct request_queue *q) { return 0; }
  291. static inline void blk_throtl_exit(struct request_queue *q) { }
  292. static inline void blk_throtl_register_queue(struct request_queue *q) { }
  293. #endif /* CONFIG_BLK_DEV_THROTTLING */
  294. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  295. extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
  296. extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
  297. const char *page, size_t count);
  298. extern void blk_throtl_bio_endio(struct bio *bio);
  299. extern void blk_throtl_stat_add(struct request *rq, u64 time);
  300. #else
  301. static inline void blk_throtl_bio_endio(struct bio *bio) { }
  302. static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
  303. #endif
  304. #ifdef CONFIG_BOUNCE
  305. extern int init_emergency_isa_pool(void);
  306. extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
  307. #else
  308. static inline int init_emergency_isa_pool(void)
  309. {
  310. return 0;
  311. }
  312. static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
  313. {
  314. }
  315. #endif /* CONFIG_BOUNCE */
  316. extern void blk_drain_queue(struct request_queue *q);
  317. #endif /* BLK_INTERNAL_H */