blk.h 9.1 KB

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