blk-barrier.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <linux/gfp.h>
  9. #include "blk.h"
  10. /**
  11. * blk_queue_ordered - does this queue support ordered writes
  12. * @q: the request queue
  13. * @ordered: one of QUEUE_ORDERED_*
  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. {
  24. if (ordered != QUEUE_ORDERED_NONE &&
  25. ordered != QUEUE_ORDERED_DRAIN &&
  26. ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
  27. ordered != QUEUE_ORDERED_DRAIN_FUA &&
  28. ordered != QUEUE_ORDERED_TAG &&
  29. ordered != QUEUE_ORDERED_TAG_FLUSH &&
  30. ordered != QUEUE_ORDERED_TAG_FUA) {
  31. printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
  32. return -EINVAL;
  33. }
  34. q->ordered = ordered;
  35. q->next_ordered = ordered;
  36. return 0;
  37. }
  38. EXPORT_SYMBOL(blk_queue_ordered);
  39. /*
  40. * Cache flushing for ordered writes handling
  41. */
  42. unsigned blk_ordered_cur_seq(struct request_queue *q)
  43. {
  44. if (!q->ordseq)
  45. return 0;
  46. return 1 << ffz(q->ordseq);
  47. }
  48. unsigned blk_ordered_req_seq(struct request *rq)
  49. {
  50. struct request_queue *q = rq->q;
  51. BUG_ON(q->ordseq == 0);
  52. if (rq == &q->pre_flush_rq)
  53. return QUEUE_ORDSEQ_PREFLUSH;
  54. if (rq == &q->bar_rq)
  55. return QUEUE_ORDSEQ_BAR;
  56. if (rq == &q->post_flush_rq)
  57. return QUEUE_ORDSEQ_POSTFLUSH;
  58. /*
  59. * !fs requests don't need to follow barrier ordering. Always
  60. * put them at the front. This fixes the following deadlock.
  61. *
  62. * http://thread.gmane.org/gmane.linux.kernel/537473
  63. */
  64. if (rq->cmd_type != REQ_TYPE_FS)
  65. return QUEUE_ORDSEQ_DRAIN;
  66. if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
  67. (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
  68. return QUEUE_ORDSEQ_DRAIN;
  69. else
  70. return QUEUE_ORDSEQ_DONE;
  71. }
  72. bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
  73. {
  74. struct request *rq;
  75. if (error && !q->orderr)
  76. q->orderr = error;
  77. BUG_ON(q->ordseq & seq);
  78. q->ordseq |= seq;
  79. if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
  80. return false;
  81. /*
  82. * Okay, sequence complete.
  83. */
  84. q->ordseq = 0;
  85. rq = q->orig_bar_rq;
  86. __blk_end_request_all(rq, q->orderr);
  87. return true;
  88. }
  89. static void pre_flush_end_io(struct request *rq, int error)
  90. {
  91. elv_completed_request(rq->q, rq);
  92. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
  93. }
  94. static void bar_end_io(struct request *rq, int error)
  95. {
  96. elv_completed_request(rq->q, rq);
  97. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
  98. }
  99. static void post_flush_end_io(struct request *rq, int error)
  100. {
  101. elv_completed_request(rq->q, rq);
  102. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
  103. }
  104. static void queue_flush(struct request_queue *q, unsigned which)
  105. {
  106. struct request *rq;
  107. rq_end_io_fn *end_io;
  108. if (which == QUEUE_ORDERED_DO_PREFLUSH) {
  109. rq = &q->pre_flush_rq;
  110. end_io = pre_flush_end_io;
  111. } else {
  112. rq = &q->post_flush_rq;
  113. end_io = post_flush_end_io;
  114. }
  115. blk_rq_init(q, rq);
  116. rq->cmd_type = REQ_TYPE_FS;
  117. rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
  118. rq->rq_disk = q->orig_bar_rq->rq_disk;
  119. rq->end_io = end_io;
  120. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  121. }
  122. static inline bool start_ordered(struct request_queue *q, struct request **rqp)
  123. {
  124. struct request *rq = *rqp;
  125. unsigned skip = 0;
  126. q->orderr = 0;
  127. q->ordered = q->next_ordered;
  128. q->ordseq |= QUEUE_ORDSEQ_STARTED;
  129. /*
  130. * For an empty barrier, there's no actual BAR request, which
  131. * in turn makes POSTFLUSH unnecessary. Mask them off.
  132. */
  133. if (!blk_rq_sectors(rq)) {
  134. q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
  135. QUEUE_ORDERED_DO_POSTFLUSH);
  136. /*
  137. * Empty barrier on a write-through device w/ ordered
  138. * tag has no command to issue and without any command
  139. * to issue, ordering by tag can't be used. Drain
  140. * instead.
  141. */
  142. if ((q->ordered & QUEUE_ORDERED_BY_TAG) &&
  143. !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) {
  144. q->ordered &= ~QUEUE_ORDERED_BY_TAG;
  145. q->ordered |= QUEUE_ORDERED_BY_DRAIN;
  146. }
  147. }
  148. /* stash away the original request */
  149. blk_dequeue_request(rq);
  150. q->orig_bar_rq = rq;
  151. rq = NULL;
  152. /*
  153. * Queue ordered sequence. As we stack them at the head, we
  154. * need to queue in reverse order. Note that we rely on that
  155. * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
  156. * request gets inbetween ordered sequence.
  157. */
  158. if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
  159. queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
  160. rq = &q->post_flush_rq;
  161. } else
  162. skip |= QUEUE_ORDSEQ_POSTFLUSH;
  163. if (q->ordered & QUEUE_ORDERED_DO_BAR) {
  164. rq = &q->bar_rq;
  165. /* initialize proxy request and queue it */
  166. blk_rq_init(q, rq);
  167. if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
  168. rq->cmd_flags |= REQ_WRITE;
  169. if (q->ordered & QUEUE_ORDERED_DO_FUA)
  170. rq->cmd_flags |= REQ_FUA;
  171. init_request_from_bio(rq, q->orig_bar_rq->bio);
  172. rq->end_io = bar_end_io;
  173. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  174. } else
  175. skip |= QUEUE_ORDSEQ_BAR;
  176. if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
  177. queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
  178. rq = &q->pre_flush_rq;
  179. } else
  180. skip |= QUEUE_ORDSEQ_PREFLUSH;
  181. if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
  182. rq = NULL;
  183. else
  184. skip |= QUEUE_ORDSEQ_DRAIN;
  185. *rqp = rq;
  186. /*
  187. * Complete skipped sequences. If whole sequence is complete,
  188. * return false to tell elevator that this request is gone.
  189. */
  190. return !blk_ordered_complete_seq(q, skip, 0);
  191. }
  192. bool blk_do_ordered(struct request_queue *q, struct request **rqp)
  193. {
  194. struct request *rq = *rqp;
  195. const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
  196. (rq->cmd_flags & REQ_HARDBARRIER);
  197. if (!q->ordseq) {
  198. if (!is_barrier)
  199. return true;
  200. if (q->next_ordered != QUEUE_ORDERED_NONE)
  201. return start_ordered(q, rqp);
  202. else {
  203. /*
  204. * Queue ordering not supported. Terminate
  205. * with prejudice.
  206. */
  207. blk_dequeue_request(rq);
  208. __blk_end_request_all(rq, -EOPNOTSUPP);
  209. *rqp = NULL;
  210. return false;
  211. }
  212. }
  213. /*
  214. * Ordered sequence in progress
  215. */
  216. /* Special requests are not subject to ordering rules. */
  217. if (rq->cmd_type != REQ_TYPE_FS &&
  218. rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
  219. return true;
  220. if (q->ordered & QUEUE_ORDERED_BY_TAG) {
  221. /* Ordered by tag. Blocking the next barrier is enough. */
  222. if (is_barrier && rq != &q->bar_rq)
  223. *rqp = NULL;
  224. } else {
  225. /* Ordered by draining. Wait for turn. */
  226. WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
  227. if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
  228. *rqp = NULL;
  229. }
  230. return true;
  231. }
  232. static void bio_end_empty_barrier(struct bio *bio, int err)
  233. {
  234. if (err) {
  235. if (err == -EOPNOTSUPP)
  236. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  237. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  238. }
  239. if (bio->bi_private)
  240. complete(bio->bi_private);
  241. bio_put(bio);
  242. }
  243. /**
  244. * blkdev_issue_flush - queue a flush
  245. * @bdev: blockdev to issue flush for
  246. * @gfp_mask: memory allocation flags (for bio_alloc)
  247. * @error_sector: error sector
  248. * @flags: BLKDEV_IFL_* flags to control behaviour
  249. *
  250. * Description:
  251. * Issue a flush for the block device in question. Caller can supply
  252. * room for storing the error offset in case of a flush error, if they
  253. * wish to. If WAIT flag is not passed then caller may check only what
  254. * request was pushed in some internal queue for later handling.
  255. */
  256. int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  257. sector_t *error_sector, unsigned long flags)
  258. {
  259. DECLARE_COMPLETION_ONSTACK(wait);
  260. struct request_queue *q;
  261. struct bio *bio;
  262. int ret = 0;
  263. if (bdev->bd_disk == NULL)
  264. return -ENXIO;
  265. q = bdev_get_queue(bdev);
  266. if (!q)
  267. return -ENXIO;
  268. /*
  269. * some block devices may not have their queue correctly set up here
  270. * (e.g. loop device without a backing file) and so issuing a flush
  271. * here will panic. Ensure there is a request function before issuing
  272. * the barrier.
  273. */
  274. if (!q->make_request_fn)
  275. return -ENXIO;
  276. bio = bio_alloc(gfp_mask, 0);
  277. bio->bi_end_io = bio_end_empty_barrier;
  278. bio->bi_bdev = bdev;
  279. if (test_bit(BLKDEV_WAIT, &flags))
  280. bio->bi_private = &wait;
  281. bio_get(bio);
  282. submit_bio(WRITE_BARRIER, bio);
  283. if (test_bit(BLKDEV_WAIT, &flags)) {
  284. wait_for_completion(&wait);
  285. /*
  286. * The driver must store the error location in ->bi_sector, if
  287. * it supports it. For non-stacked drivers, this should be
  288. * copied from blk_rq_pos(rq).
  289. */
  290. if (error_sector)
  291. *error_sector = bio->bi_sector;
  292. }
  293. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  294. ret = -EOPNOTSUPP;
  295. else if (!bio_flagged(bio, BIO_UPTODATE))
  296. ret = -EIO;
  297. bio_put(bio);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL(blkdev_issue_flush);