blk-exec.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Functions related to setting various queue properties from drivers
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/bio.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/blk-mq.h>
  9. #include <linux/sched/sysctl.h>
  10. #include "blk.h"
  11. #include "blk-mq-sched.h"
  12. /**
  13. * blk_end_sync_rq - executes a completion event on a request
  14. * @rq: request to complete
  15. * @error: end I/O status of the request
  16. */
  17. static void blk_end_sync_rq(struct request *rq, blk_status_t error)
  18. {
  19. struct completion *waiting = rq->end_io_data;
  20. rq->end_io_data = NULL;
  21. /*
  22. * complete last, if this is a stack request the process (and thus
  23. * the rq pointer) could be invalid right after this complete()
  24. */
  25. complete(waiting);
  26. }
  27. /**
  28. * blk_execute_rq_nowait - insert a request into queue for execution
  29. * @q: queue to insert the request in
  30. * @bd_disk: matching gendisk
  31. * @rq: request to insert
  32. * @at_head: insert request at head or tail of queue
  33. * @done: I/O completion handler
  34. *
  35. * Description:
  36. * Insert a fully prepared request at the back of the I/O scheduler queue
  37. * for execution. Don't wait for completion.
  38. *
  39. * Note:
  40. * This function will invoke @done directly if the queue is dead.
  41. */
  42. void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
  43. struct request *rq, int at_head,
  44. rq_end_io_fn *done)
  45. {
  46. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  47. WARN_ON(irqs_disabled());
  48. WARN_ON(!blk_rq_is_passthrough(rq));
  49. rq->rq_disk = bd_disk;
  50. rq->end_io = done;
  51. /*
  52. * don't check dying flag for MQ because the request won't
  53. * be reused after dying flag is set
  54. */
  55. if (q->mq_ops) {
  56. blk_mq_sched_insert_request(rq, at_head, true, false);
  57. return;
  58. }
  59. spin_lock_irq(q->queue_lock);
  60. if (unlikely(blk_queue_dying(q))) {
  61. rq->rq_flags |= RQF_QUIET;
  62. __blk_end_request_all(rq, BLK_STS_IOERR);
  63. spin_unlock_irq(q->queue_lock);
  64. return;
  65. }
  66. __elv_add_request(q, rq, where);
  67. __blk_run_queue(q);
  68. spin_unlock_irq(q->queue_lock);
  69. }
  70. EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
  71. /**
  72. * blk_execute_rq - insert a request into queue for execution
  73. * @q: queue to insert the request in
  74. * @bd_disk: matching gendisk
  75. * @rq: request to insert
  76. * @at_head: insert request at head or tail of queue
  77. *
  78. * Description:
  79. * Insert a fully prepared request at the back of the I/O scheduler queue
  80. * for execution and wait for completion.
  81. */
  82. void blk_execute_rq(struct request_queue *q, struct gendisk *bd_disk,
  83. struct request *rq, int at_head)
  84. {
  85. DECLARE_COMPLETION_ONSTACK(wait);
  86. unsigned long hang_check;
  87. rq->end_io_data = &wait;
  88. blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
  89. /* Prevent hang_check timer from firing at us during very long I/O */
  90. hang_check = sysctl_hung_task_timeout_secs;
  91. if (hang_check)
  92. while (!wait_for_completion_io_timeout(&wait, hang_check * (HZ/2)));
  93. else
  94. wait_for_completion_io(&wait);
  95. }
  96. EXPORT_SYMBOL(blk_execute_rq);