blk-barrier.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Functions related to barrier IO handling
  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. * blk_queue_ordered - does this queue support ordered writes
  11. * @q: the request queue
  12. * @ordered: one of QUEUE_ORDERED_*
  13. * @prepare_flush_fn: rq setup helper for cache flush ordered writes
  14. *
  15. * Description:
  16. * For journalled file systems, doing ordered writes on a commit
  17. * block instead of explicitly doing wait_on_buffer (which is bad
  18. * for performance) can be a big win. Block drivers supporting this
  19. * feature should call this function and indicate so.
  20. *
  21. **/
  22. int blk_queue_ordered(struct request_queue *q, unsigned ordered,
  23. prepare_flush_fn *prepare_flush_fn)
  24. {
  25. if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
  26. prepare_flush_fn == NULL) {
  27. printk(KERN_ERR "%s: prepare_flush_fn required\n",
  28. __FUNCTION__);
  29. return -EINVAL;
  30. }
  31. if (ordered != QUEUE_ORDERED_NONE &&
  32. ordered != QUEUE_ORDERED_DRAIN &&
  33. ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
  34. ordered != QUEUE_ORDERED_DRAIN_FUA &&
  35. ordered != QUEUE_ORDERED_TAG &&
  36. ordered != QUEUE_ORDERED_TAG_FLUSH &&
  37. ordered != QUEUE_ORDERED_TAG_FUA) {
  38. printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
  39. return -EINVAL;
  40. }
  41. q->ordered = ordered;
  42. q->next_ordered = ordered;
  43. q->prepare_flush_fn = prepare_flush_fn;
  44. return 0;
  45. }
  46. EXPORT_SYMBOL(blk_queue_ordered);
  47. /*
  48. * Cache flushing for ordered writes handling
  49. */
  50. inline unsigned blk_ordered_cur_seq(struct request_queue *q)
  51. {
  52. if (!q->ordseq)
  53. return 0;
  54. return 1 << ffz(q->ordseq);
  55. }
  56. unsigned blk_ordered_req_seq(struct request *rq)
  57. {
  58. struct request_queue *q = rq->q;
  59. BUG_ON(q->ordseq == 0);
  60. if (rq == &q->pre_flush_rq)
  61. return QUEUE_ORDSEQ_PREFLUSH;
  62. if (rq == &q->bar_rq)
  63. return QUEUE_ORDSEQ_BAR;
  64. if (rq == &q->post_flush_rq)
  65. return QUEUE_ORDSEQ_POSTFLUSH;
  66. /*
  67. * !fs requests don't need to follow barrier ordering. Always
  68. * put them at the front. This fixes the following deadlock.
  69. *
  70. * http://thread.gmane.org/gmane.linux.kernel/537473
  71. */
  72. if (!blk_fs_request(rq))
  73. return QUEUE_ORDSEQ_DRAIN;
  74. if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
  75. (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
  76. return QUEUE_ORDSEQ_DRAIN;
  77. else
  78. return QUEUE_ORDSEQ_DONE;
  79. }
  80. void blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
  81. {
  82. struct request *rq;
  83. if (error && !q->orderr)
  84. q->orderr = error;
  85. BUG_ON(q->ordseq & seq);
  86. q->ordseq |= seq;
  87. if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
  88. return;
  89. /*
  90. * Okay, sequence complete.
  91. */
  92. q->ordseq = 0;
  93. rq = q->orig_bar_rq;
  94. if (__blk_end_request(rq, q->orderr, blk_rq_bytes(rq)))
  95. BUG();
  96. }
  97. static void pre_flush_end_io(struct request *rq, int error)
  98. {
  99. elv_completed_request(rq->q, rq);
  100. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
  101. }
  102. static void bar_end_io(struct request *rq, int error)
  103. {
  104. elv_completed_request(rq->q, rq);
  105. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
  106. }
  107. static void post_flush_end_io(struct request *rq, int error)
  108. {
  109. elv_completed_request(rq->q, rq);
  110. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
  111. }
  112. static void queue_flush(struct request_queue *q, unsigned which)
  113. {
  114. struct request *rq;
  115. rq_end_io_fn *end_io;
  116. if (which == QUEUE_ORDERED_PREFLUSH) {
  117. rq = &q->pre_flush_rq;
  118. end_io = pre_flush_end_io;
  119. } else {
  120. rq = &q->post_flush_rq;
  121. end_io = post_flush_end_io;
  122. }
  123. rq->cmd_flags = REQ_HARDBARRIER;
  124. rq_init(q, rq);
  125. rq->elevator_private = NULL;
  126. rq->elevator_private2 = NULL;
  127. rq->rq_disk = q->bar_rq.rq_disk;
  128. rq->end_io = end_io;
  129. q->prepare_flush_fn(q, rq);
  130. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  131. }
  132. static inline struct request *start_ordered(struct request_queue *q,
  133. struct request *rq)
  134. {
  135. q->orderr = 0;
  136. q->ordered = q->next_ordered;
  137. q->ordseq |= QUEUE_ORDSEQ_STARTED;
  138. /*
  139. * Prep proxy barrier request.
  140. */
  141. blkdev_dequeue_request(rq);
  142. q->orig_bar_rq = rq;
  143. rq = &q->bar_rq;
  144. rq->cmd_flags = 0;
  145. rq_init(q, rq);
  146. if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
  147. rq->cmd_flags |= REQ_RW;
  148. if (q->ordered & QUEUE_ORDERED_FUA)
  149. rq->cmd_flags |= REQ_FUA;
  150. rq->elevator_private = NULL;
  151. rq->elevator_private2 = NULL;
  152. init_request_from_bio(rq, q->orig_bar_rq->bio);
  153. rq->end_io = bar_end_io;
  154. /*
  155. * Queue ordered sequence. As we stack them at the head, we
  156. * need to queue in reverse order. Note that we rely on that
  157. * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
  158. * request gets inbetween ordered sequence. If this request is
  159. * an empty barrier, we don't need to do a postflush ever since
  160. * there will be no data written between the pre and post flush.
  161. * Hence a single flush will suffice.
  162. */
  163. if ((q->ordered & QUEUE_ORDERED_POSTFLUSH) && !blk_empty_barrier(rq))
  164. queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
  165. else
  166. q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
  167. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  168. if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
  169. queue_flush(q, QUEUE_ORDERED_PREFLUSH);
  170. rq = &q->pre_flush_rq;
  171. } else
  172. q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
  173. if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
  174. q->ordseq |= QUEUE_ORDSEQ_DRAIN;
  175. else
  176. rq = NULL;
  177. return rq;
  178. }
  179. int blk_do_ordered(struct request_queue *q, struct request **rqp)
  180. {
  181. struct request *rq = *rqp;
  182. const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
  183. if (!q->ordseq) {
  184. if (!is_barrier)
  185. return 1;
  186. if (q->next_ordered != QUEUE_ORDERED_NONE) {
  187. *rqp = start_ordered(q, rq);
  188. return 1;
  189. } else {
  190. /*
  191. * This can happen when the queue switches to
  192. * ORDERED_NONE while this request is on it.
  193. */
  194. blkdev_dequeue_request(rq);
  195. if (__blk_end_request(rq, -EOPNOTSUPP,
  196. blk_rq_bytes(rq)))
  197. BUG();
  198. *rqp = NULL;
  199. return 0;
  200. }
  201. }
  202. /*
  203. * Ordered sequence in progress
  204. */
  205. /* Special requests are not subject to ordering rules. */
  206. if (!blk_fs_request(rq) &&
  207. rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
  208. return 1;
  209. if (q->ordered & QUEUE_ORDERED_TAG) {
  210. /* Ordered by tag. Blocking the next barrier is enough. */
  211. if (is_barrier && rq != &q->bar_rq)
  212. *rqp = NULL;
  213. } else {
  214. /* Ordered by draining. Wait for turn. */
  215. WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
  216. if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
  217. *rqp = NULL;
  218. }
  219. return 1;
  220. }
  221. static void bio_end_empty_barrier(struct bio *bio, int err)
  222. {
  223. if (err) {
  224. if (err == -EOPNOTSUPP)
  225. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  226. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  227. }
  228. complete(bio->bi_private);
  229. }
  230. /**
  231. * blkdev_issue_flush - queue a flush
  232. * @bdev: blockdev to issue flush for
  233. * @error_sector: error sector
  234. *
  235. * Description:
  236. * Issue a flush for the block device in question. Caller can supply
  237. * room for storing the error offset in case of a flush error, if they
  238. * wish to. Caller must run wait_for_completion() on its own.
  239. */
  240. int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
  241. {
  242. DECLARE_COMPLETION_ONSTACK(wait);
  243. struct request_queue *q;
  244. struct bio *bio;
  245. int ret;
  246. if (bdev->bd_disk == NULL)
  247. return -ENXIO;
  248. q = bdev_get_queue(bdev);
  249. if (!q)
  250. return -ENXIO;
  251. bio = bio_alloc(GFP_KERNEL, 0);
  252. if (!bio)
  253. return -ENOMEM;
  254. bio->bi_end_io = bio_end_empty_barrier;
  255. bio->bi_private = &wait;
  256. bio->bi_bdev = bdev;
  257. submit_bio(1 << BIO_RW_BARRIER, bio);
  258. wait_for_completion(&wait);
  259. /*
  260. * The driver must store the error location in ->bi_sector, if
  261. * it supports it. For non-stacked drivers, this should be copied
  262. * from rq->sector.
  263. */
  264. if (error_sector)
  265. *error_sector = bio->bi_sector;
  266. ret = 0;
  267. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  268. ret = -EOPNOTSUPP;
  269. else if (!bio_flagged(bio, BIO_UPTODATE))
  270. ret = -EIO;
  271. bio_put(bio);
  272. return ret;
  273. }
  274. EXPORT_SYMBOL(blkdev_issue_flush);