blk-exec.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/sched/sysctl.h>
  9. #include "blk.h"
  10. /*
  11. * for max sense size
  12. */
  13. #include <scsi/scsi_cmnd.h>
  14. /**
  15. * blk_end_sync_rq - executes a completion event on a request
  16. * @rq: request to complete
  17. * @error: end I/O status of the request
  18. */
  19. static void blk_end_sync_rq(struct request *rq, int error)
  20. {
  21. struct completion *waiting = rq->end_io_data;
  22. rq->end_io_data = NULL;
  23. /*
  24. * complete last, if this is a stack request the process (and thus
  25. * the rq pointer) could be invalid right after this complete()
  26. */
  27. complete(waiting);
  28. }
  29. /**
  30. * blk_execute_rq_nowait - insert a request into queue for execution
  31. * @q: queue to insert the request in
  32. * @bd_disk: matching gendisk
  33. * @rq: request to insert
  34. * @at_head: insert request at head or tail of queue
  35. * @done: I/O completion handler
  36. *
  37. * Description:
  38. * Insert a fully prepared request at the back of the I/O scheduler queue
  39. * for execution. Don't wait for completion.
  40. *
  41. * Note:
  42. * This function will invoke @done directly if the queue is dead.
  43. */
  44. void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
  45. struct request *rq, int at_head,
  46. rq_end_io_fn *done)
  47. {
  48. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  49. bool is_pm_resume;
  50. WARN_ON(irqs_disabled());
  51. rq->rq_disk = bd_disk;
  52. rq->end_io = done;
  53. /*
  54. * need to check this before __blk_run_queue(), because rq can
  55. * be freed before that returns.
  56. */
  57. is_pm_resume = rq->cmd_type == REQ_TYPE_PM_RESUME;
  58. spin_lock_irq(q->queue_lock);
  59. if (unlikely(blk_queue_dying(q))) {
  60. rq->cmd_flags |= REQ_QUIET;
  61. rq->errors = -ENXIO;
  62. __blk_end_request_all(rq, rq->errors);
  63. spin_unlock_irq(q->queue_lock);
  64. return;
  65. }
  66. __elv_add_request(q, rq, where);
  67. __blk_run_queue(q);
  68. /* the queue is stopped so it won't be run */
  69. if (is_pm_resume)
  70. __blk_run_queue_uncond(q);
  71. spin_unlock_irq(q->queue_lock);
  72. }
  73. EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
  74. /**
  75. * blk_execute_rq - insert a request into queue for execution
  76. * @q: queue to insert the request in
  77. * @bd_disk: matching gendisk
  78. * @rq: request to insert
  79. * @at_head: insert request at head or tail of queue
  80. *
  81. * Description:
  82. * Insert a fully prepared request at the back of the I/O scheduler queue
  83. * for execution and wait for completion.
  84. */
  85. int blk_execute_rq(struct request_queue *q, struct gendisk *bd_disk,
  86. struct request *rq, int at_head)
  87. {
  88. DECLARE_COMPLETION_ONSTACK(wait);
  89. char sense[SCSI_SENSE_BUFFERSIZE];
  90. int err = 0;
  91. unsigned long hang_check;
  92. if (!rq->sense) {
  93. memset(sense, 0, sizeof(sense));
  94. rq->sense = sense;
  95. rq->sense_len = 0;
  96. }
  97. rq->end_io_data = &wait;
  98. blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
  99. /* Prevent hang_check timer from firing at us during very long I/O */
  100. hang_check = sysctl_hung_task_timeout_secs;
  101. if (hang_check)
  102. while (!wait_for_completion_io_timeout(&wait, hang_check * (HZ/2)));
  103. else
  104. wait_for_completion_io(&wait);
  105. if (rq->errors)
  106. err = -EIO;
  107. return err;
  108. }
  109. EXPORT_SYMBOL(blk_execute_rq);