blk-exec.c 2.6 KB

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