blk-timeout.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <linux/fault-inject.h>
  8. #include "blk.h"
  9. #ifdef CONFIG_FAIL_IO_TIMEOUT
  10. static DECLARE_FAULT_ATTR(fail_io_timeout);
  11. static int __init setup_fail_io_timeout(char *str)
  12. {
  13. return setup_fault_attr(&fail_io_timeout, str);
  14. }
  15. __setup("fail_io_timeout=", setup_fail_io_timeout);
  16. int blk_should_fake_timeout(struct request_queue *q)
  17. {
  18. if (!test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
  19. return 0;
  20. return should_fail(&fail_io_timeout, 1);
  21. }
  22. static int __init fail_io_timeout_debugfs(void)
  23. {
  24. struct dentry *dir = fault_create_debugfs_attr("fail_io_timeout",
  25. NULL, &fail_io_timeout);
  26. return IS_ERR(dir) ? PTR_ERR(dir) : 0;
  27. }
  28. late_initcall(fail_io_timeout_debugfs);
  29. ssize_t part_timeout_show(struct device *dev, struct device_attribute *attr,
  30. char *buf)
  31. {
  32. struct gendisk *disk = dev_to_disk(dev);
  33. int set = test_bit(QUEUE_FLAG_FAIL_IO, &disk->queue->queue_flags);
  34. return sprintf(buf, "%d\n", set != 0);
  35. }
  36. ssize_t part_timeout_store(struct device *dev, struct device_attribute *attr,
  37. const char *buf, size_t count)
  38. {
  39. struct gendisk *disk = dev_to_disk(dev);
  40. int val;
  41. if (count) {
  42. struct request_queue *q = disk->queue;
  43. char *p = (char *) buf;
  44. val = simple_strtoul(p, &p, 10);
  45. spin_lock_irq(q->queue_lock);
  46. if (val)
  47. queue_flag_set(QUEUE_FLAG_FAIL_IO, q);
  48. else
  49. queue_flag_clear(QUEUE_FLAG_FAIL_IO, q);
  50. spin_unlock_irq(q->queue_lock);
  51. }
  52. return count;
  53. }
  54. #endif /* CONFIG_FAIL_IO_TIMEOUT */
  55. /*
  56. * blk_delete_timer - Delete/cancel timer for a given function.
  57. * @req: request that we are canceling timer for
  58. *
  59. */
  60. void blk_delete_timer(struct request *req)
  61. {
  62. list_del_init(&req->timeout_list);
  63. }
  64. static void blk_rq_timed_out(struct request *req)
  65. {
  66. struct request_queue *q = req->q;
  67. enum blk_eh_timer_return ret;
  68. ret = q->rq_timed_out_fn(req);
  69. switch (ret) {
  70. case BLK_EH_HANDLED:
  71. __blk_complete_request(req);
  72. break;
  73. case BLK_EH_RESET_TIMER:
  74. blk_add_timer(req);
  75. blk_clear_rq_complete(req);
  76. break;
  77. case BLK_EH_NOT_HANDLED:
  78. /*
  79. * LLD handles this for now but in the future
  80. * we can send a request msg to abort the command
  81. * and we can move more of the generic scsi eh code to
  82. * the blk layer.
  83. */
  84. break;
  85. default:
  86. printk(KERN_ERR "block: bad eh return: %d\n", ret);
  87. break;
  88. }
  89. }
  90. void blk_rq_timed_out_timer(unsigned long data)
  91. {
  92. struct request_queue *q = (struct request_queue *) data;
  93. unsigned long flags, next = 0;
  94. struct request *rq, *tmp;
  95. int next_set = 0;
  96. spin_lock_irqsave(q->queue_lock, flags);
  97. list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list) {
  98. if (time_after_eq(jiffies, rq->deadline)) {
  99. list_del_init(&rq->timeout_list);
  100. /*
  101. * Check if we raced with end io completion
  102. */
  103. if (blk_mark_rq_complete(rq))
  104. continue;
  105. blk_rq_timed_out(rq);
  106. } else if (!next_set || time_after(next, rq->deadline)) {
  107. next = rq->deadline;
  108. next_set = 1;
  109. }
  110. }
  111. if (next_set)
  112. mod_timer(&q->timeout, round_jiffies_up(next));
  113. spin_unlock_irqrestore(q->queue_lock, flags);
  114. }
  115. /**
  116. * blk_abort_request -- Request request recovery for the specified command
  117. * @req: pointer to the request of interest
  118. *
  119. * This function requests that the block layer start recovery for the
  120. * request by deleting the timer and calling the q's timeout function.
  121. * LLDDs who implement their own error recovery MAY ignore the timeout
  122. * event if they generated blk_abort_req. Must hold queue lock.
  123. */
  124. void blk_abort_request(struct request *req)
  125. {
  126. if (blk_mark_rq_complete(req))
  127. return;
  128. blk_delete_timer(req);
  129. blk_rq_timed_out(req);
  130. }
  131. EXPORT_SYMBOL_GPL(blk_abort_request);
  132. /**
  133. * blk_add_timer - Start timeout timer for a single request
  134. * @req: request that is about to start running.
  135. *
  136. * Notes:
  137. * Each request has its own timer, and as it is added to the queue, we
  138. * set up the timer. When the request completes, we cancel the timer.
  139. */
  140. void blk_add_timer(struct request *req)
  141. {
  142. struct request_queue *q = req->q;
  143. unsigned long expiry;
  144. if (!q->rq_timed_out_fn)
  145. return;
  146. BUG_ON(!list_empty(&req->timeout_list));
  147. /*
  148. * Some LLDs, like scsi, peek at the timeout to prevent a
  149. * command from being retried forever.
  150. */
  151. if (!req->timeout)
  152. req->timeout = q->rq_timeout;
  153. req->deadline = jiffies + req->timeout;
  154. list_add_tail(&req->timeout_list, &q->timeout_list);
  155. /*
  156. * If the timer isn't already pending or this timeout is earlier
  157. * than an existing one, modify the timer. Round up to next nearest
  158. * second.
  159. */
  160. expiry = round_jiffies_up(req->deadline);
  161. if (!timer_pending(&q->timeout) ||
  162. time_before(expiry, q->timeout.expires))
  163. mod_timer(&q->timeout, expiry);
  164. }
  165. /**
  166. * blk_abort_queue -- Abort all request on given queue
  167. * @queue: pointer to queue
  168. *
  169. */
  170. void blk_abort_queue(struct request_queue *q)
  171. {
  172. unsigned long flags;
  173. struct request *rq, *tmp;
  174. LIST_HEAD(list);
  175. /*
  176. * Not a request based block device, nothing to abort
  177. */
  178. if (!q->request_fn)
  179. return;
  180. spin_lock_irqsave(q->queue_lock, flags);
  181. elv_abort_queue(q);
  182. /*
  183. * Splice entries to local list, to avoid deadlocking if entries
  184. * get readded to the timeout list by error handling
  185. */
  186. list_splice_init(&q->timeout_list, &list);
  187. list_for_each_entry_safe(rq, tmp, &list, timeout_list)
  188. blk_abort_request(rq);
  189. /*
  190. * Occasionally, blk_abort_request() will return without
  191. * deleting the element from the list. Make sure we add those back
  192. * instead of leaving them on the local stack list.
  193. */
  194. list_splice(&list, &q->timeout_list);
  195. spin_unlock_irqrestore(q->queue_lock, flags);
  196. }
  197. EXPORT_SYMBOL_GPL(blk_abort_queue);