blk-timeout.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Functions related to generic timeout handling of requests.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/blkdev.h>
  7. #include "blk.h"
  8. /*
  9. * blk_delete_timer - Delete/cancel timer for a given function.
  10. * @req: request that we are canceling timer for
  11. *
  12. */
  13. void blk_delete_timer(struct request *req)
  14. {
  15. struct request_queue *q = req->q;
  16. /*
  17. * Nothing to detach
  18. */
  19. if (!q->rq_timed_out_fn || !req->deadline)
  20. return;
  21. list_del_init(&req->timeout_list);
  22. if (list_empty(&q->timeout_list))
  23. del_timer(&q->timeout);
  24. }
  25. static void blk_rq_timed_out(struct request *req)
  26. {
  27. struct request_queue *q = req->q;
  28. enum blk_eh_timer_return ret;
  29. ret = q->rq_timed_out_fn(req);
  30. switch (ret) {
  31. case BLK_EH_HANDLED:
  32. __blk_complete_request(req);
  33. break;
  34. case BLK_EH_RESET_TIMER:
  35. blk_clear_rq_complete(req);
  36. blk_add_timer(req);
  37. break;
  38. case BLK_EH_NOT_HANDLED:
  39. /*
  40. * LLD handles this for now but in the future
  41. * we can send a request msg to abort the command
  42. * and we can move more of the generic scsi eh code to
  43. * the blk layer.
  44. */
  45. break;
  46. default:
  47. printk(KERN_ERR "block: bad eh return: %d\n", ret);
  48. break;
  49. }
  50. }
  51. void blk_rq_timed_out_timer(unsigned long data)
  52. {
  53. struct request_queue *q = (struct request_queue *) data;
  54. unsigned long flags, uninitialized_var(next), next_set = 0;
  55. struct request *rq, *tmp;
  56. spin_lock_irqsave(q->queue_lock, flags);
  57. list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list) {
  58. if (time_after_eq(jiffies, rq->deadline)) {
  59. list_del_init(&rq->timeout_list);
  60. /*
  61. * Check if we raced with end io completion
  62. */
  63. if (blk_mark_rq_complete(rq))
  64. continue;
  65. blk_rq_timed_out(rq);
  66. }
  67. if (!next_set) {
  68. next = rq->deadline;
  69. next_set = 1;
  70. } else if (time_after(next, rq->deadline))
  71. next = rq->deadline;
  72. }
  73. if (next_set && !list_empty(&q->timeout_list))
  74. mod_timer(&q->timeout, round_jiffies(next));
  75. spin_unlock_irqrestore(q->queue_lock, flags);
  76. }
  77. /**
  78. * blk_abort_request -- Request request recovery for the specified command
  79. * @req: pointer to the request of interest
  80. *
  81. * This function requests that the block layer start recovery for the
  82. * request by deleting the timer and calling the q's timeout function.
  83. * LLDDs who implement their own error recovery MAY ignore the timeout
  84. * event if they generated blk_abort_req. Must hold queue lock.
  85. */
  86. void blk_abort_request(struct request *req)
  87. {
  88. blk_delete_timer(req);
  89. blk_rq_timed_out(req);
  90. }
  91. EXPORT_SYMBOL_GPL(blk_abort_request);
  92. /**
  93. * blk_add_timer - Start timeout timer for a single request
  94. * @req: request that is about to start running.
  95. *
  96. * Notes:
  97. * Each request has its own timer, and as it is added to the queue, we
  98. * set up the timer. When the request completes, we cancel the timer.
  99. */
  100. void blk_add_timer(struct request *req)
  101. {
  102. struct request_queue *q = req->q;
  103. unsigned long expiry;
  104. if (!q->rq_timed_out_fn)
  105. return;
  106. BUG_ON(!list_empty(&req->timeout_list));
  107. BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
  108. if (req->timeout)
  109. req->deadline = jiffies + req->timeout;
  110. else {
  111. req->deadline = jiffies + q->rq_timeout;
  112. /*
  113. * Some LLDs, like scsi, peek at the timeout to prevent
  114. * a command from being retried forever.
  115. */
  116. req->timeout = q->rq_timeout;
  117. }
  118. list_add_tail(&req->timeout_list, &q->timeout_list);
  119. /*
  120. * If the timer isn't already pending or this timeout is earlier
  121. * than an existing one, modify the timer. Round to next nearest
  122. * second.
  123. */
  124. expiry = round_jiffies(req->deadline);
  125. /*
  126. * We use ->deadline == 0 to detect whether a timer was added or
  127. * not, so just increase to next jiffy for that specific case
  128. */
  129. if (unlikely(!req->deadline))
  130. req->deadline = 1;
  131. if (!timer_pending(&q->timeout) ||
  132. time_before(expiry, q->timeout.expires))
  133. mod_timer(&q->timeout, expiry);
  134. }