blk-barrier.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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_flags = REQ_HARDBARRIER | REQ_FLUSH;
  117. rq->rq_disk = q->bar_rq.rq_disk;
  118. rq->end_io = end_io;
  119. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  120. }
  121. static inline bool start_ordered(struct request_queue *q, struct request **rqp)
  122. {
  123. struct request *rq = *rqp;
  124. unsigned skip = 0;
  125. q->orderr = 0;
  126. q->ordered = q->next_ordered;
  127. q->ordseq |= QUEUE_ORDSEQ_STARTED;
  128. /*
  129. * For an empty barrier, there's no actual BAR request, which
  130. * in turn makes POSTFLUSH unnecessary. Mask them off.
  131. */
  132. if (!blk_rq_sectors(rq)) {
  133. q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
  134. QUEUE_ORDERED_DO_POSTFLUSH);
  135. /*
  136. * Empty barrier on a write-through device w/ ordered
  137. * tag has no command to issue and without any command
  138. * to issue, ordering by tag can't be used. Drain
  139. * instead.
  140. */
  141. if ((q->ordered & QUEUE_ORDERED_BY_TAG) &&
  142. !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) {
  143. q->ordered &= ~QUEUE_ORDERED_BY_TAG;
  144. q->ordered |= QUEUE_ORDERED_BY_DRAIN;
  145. }
  146. }
  147. /* stash away the original request */
  148. blk_dequeue_request(rq);
  149. q->orig_bar_rq = rq;
  150. rq = NULL;
  151. /*
  152. * Queue ordered sequence. As we stack them at the head, we
  153. * need to queue in reverse order. Note that we rely on that
  154. * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
  155. * request gets inbetween ordered sequence.
  156. */
  157. if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
  158. queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
  159. rq = &q->post_flush_rq;
  160. } else
  161. skip |= QUEUE_ORDSEQ_POSTFLUSH;
  162. if (q->ordered & QUEUE_ORDERED_DO_BAR) {
  163. rq = &q->bar_rq;
  164. /* initialize proxy request and queue it */
  165. blk_rq_init(q, rq);
  166. if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
  167. rq->cmd_flags |= REQ_WRITE;
  168. if (q->ordered & QUEUE_ORDERED_DO_FUA)
  169. rq->cmd_flags |= REQ_FUA;
  170. init_request_from_bio(rq, q->orig_bar_rq->bio);
  171. rq->end_io = bar_end_io;
  172. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  173. } else
  174. skip |= QUEUE_ORDSEQ_BAR;
  175. if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
  176. queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
  177. rq = &q->pre_flush_rq;
  178. } else
  179. skip |= QUEUE_ORDSEQ_PREFLUSH;
  180. if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
  181. rq = NULL;
  182. else
  183. skip |= QUEUE_ORDSEQ_DRAIN;
  184. *rqp = rq;
  185. /*
  186. * Complete skipped sequences. If whole sequence is complete,
  187. * return false to tell elevator that this request is gone.
  188. */
  189. return !blk_ordered_complete_seq(q, skip, 0);
  190. }
  191. bool blk_do_ordered(struct request_queue *q, struct request **rqp)
  192. {
  193. struct request *rq = *rqp;
  194. const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
  195. (rq->cmd_flags & REQ_HARDBARRIER);
  196. if (!q->ordseq) {
  197. if (!is_barrier)
  198. return true;
  199. if (q->next_ordered != QUEUE_ORDERED_NONE)
  200. return start_ordered(q, rqp);
  201. else {
  202. /*
  203. * Queue ordering not supported. Terminate
  204. * with prejudice.
  205. */
  206. blk_dequeue_request(rq);
  207. __blk_end_request_all(rq, -EOPNOTSUPP);
  208. *rqp = NULL;
  209. return false;
  210. }
  211. }
  212. /*
  213. * Ordered sequence in progress
  214. */
  215. /* Special requests are not subject to ordering rules. */
  216. if (rq->cmd_type != REQ_TYPE_FS &&
  217. rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
  218. return true;
  219. if (q->ordered & QUEUE_ORDERED_BY_TAG) {
  220. /* Ordered by tag. Blocking the next barrier is enough. */
  221. if (is_barrier && rq != &q->bar_rq)
  222. *rqp = NULL;
  223. } else {
  224. /* Ordered by draining. Wait for turn. */
  225. WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
  226. if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
  227. *rqp = NULL;
  228. }
  229. return true;
  230. }
  231. static void bio_end_empty_barrier(struct bio *bio, int err)
  232. {
  233. if (err) {
  234. if (err == -EOPNOTSUPP)
  235. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  236. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  237. }
  238. if (bio->bi_private)
  239. complete(bio->bi_private);
  240. bio_put(bio);
  241. }
  242. /**
  243. * blkdev_issue_flush - queue a flush
  244. * @bdev: blockdev to issue flush for
  245. * @gfp_mask: memory allocation flags (for bio_alloc)
  246. * @error_sector: error sector
  247. * @flags: BLKDEV_IFL_* flags to control behaviour
  248. *
  249. * Description:
  250. * Issue a flush for the block device in question. Caller can supply
  251. * room for storing the error offset in case of a flush error, if they
  252. * wish to. If WAIT flag is not passed then caller may check only what
  253. * request was pushed in some internal queue for later handling.
  254. */
  255. int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  256. sector_t *error_sector, unsigned long flags)
  257. {
  258. DECLARE_COMPLETION_ONSTACK(wait);
  259. struct request_queue *q;
  260. struct bio *bio;
  261. int ret = 0;
  262. if (bdev->bd_disk == NULL)
  263. return -ENXIO;
  264. q = bdev_get_queue(bdev);
  265. if (!q)
  266. return -ENXIO;
  267. bio = bio_alloc(gfp_mask, 0);
  268. bio->bi_end_io = bio_end_empty_barrier;
  269. bio->bi_bdev = bdev;
  270. if (test_bit(BLKDEV_WAIT, &flags))
  271. bio->bi_private = &wait;
  272. bio_get(bio);
  273. submit_bio(WRITE_BARRIER, bio);
  274. if (test_bit(BLKDEV_WAIT, &flags)) {
  275. wait_for_completion(&wait);
  276. /*
  277. * The driver must store the error location in ->bi_sector, if
  278. * it supports it. For non-stacked drivers, this should be
  279. * copied from blk_rq_pos(rq).
  280. */
  281. if (error_sector)
  282. *error_sector = bio->bi_sector;
  283. }
  284. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  285. ret = -EOPNOTSUPP;
  286. else if (!bio_flagged(bio, BIO_UPTODATE))
  287. ret = -EIO;
  288. bio_put(bio);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL(blkdev_issue_flush);