blk-exec.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "blk.h"
  9. /*
  10. * for max sense size
  11. */
  12. #include <scsi/scsi_cmnd.h>
  13. /**
  14. * blk_end_sync_rq - executes a completion event on a request
  15. * @rq: request to complete
  16. * @error: end io status of the request
  17. */
  18. void blk_end_sync_rq(struct request *rq, int error)
  19. {
  20. struct completion *waiting = rq->end_io_data;
  21. rq->end_io_data = NULL;
  22. __blk_put_request(rq->q, rq);
  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. EXPORT_SYMBOL(blk_end_sync_rq);
  30. /**
  31. * blk_execute_rq_nowait - insert a request into queue for execution
  32. * @q: queue to insert the request in
  33. * @bd_disk: matching gendisk
  34. * @rq: request to insert
  35. * @at_head: insert request at head or tail of queue
  36. * @done: I/O completion handler
  37. *
  38. * Description:
  39. * Insert a fully prepared request at the back of the io scheduler queue
  40. * for execution. Don't wait for completion.
  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. rq->rq_disk = bd_disk;
  48. rq->cmd_flags |= REQ_NOMERGE;
  49. rq->end_io = done;
  50. WARN_ON(irqs_disabled());
  51. spin_lock_irq(q->queue_lock);
  52. __elv_add_request(q, rq, where, 1);
  53. __generic_unplug_device(q);
  54. spin_unlock_irq(q->queue_lock);
  55. }
  56. EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
  57. /**
  58. * blk_execute_rq - insert a request into queue for execution
  59. * @q: queue to insert the request in
  60. * @bd_disk: matching gendisk
  61. * @rq: request to insert
  62. * @at_head: insert request at head or tail of queue
  63. *
  64. * Description:
  65. * Insert a fully prepared request at the back of the io scheduler queue
  66. * for execution and wait for completion.
  67. */
  68. int blk_execute_rq(struct request_queue *q, struct gendisk *bd_disk,
  69. struct request *rq, int at_head)
  70. {
  71. DECLARE_COMPLETION_ONSTACK(wait);
  72. char sense[SCSI_SENSE_BUFFERSIZE];
  73. int err = 0;
  74. /*
  75. * we need an extra reference to the request, so we can look at
  76. * it after io completion
  77. */
  78. rq->ref_count++;
  79. if (!rq->sense) {
  80. memset(sense, 0, sizeof(sense));
  81. rq->sense = sense;
  82. rq->sense_len = 0;
  83. }
  84. rq->end_io_data = &wait;
  85. blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
  86. wait_for_completion(&wait);
  87. if (rq->errors)
  88. err = -EIO;
  89. return err;
  90. }
  91. EXPORT_SYMBOL(blk_execute_rq);