blk-flush.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Functions to sequence FLUSH and FUA writes.
  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. /* FLUSH/FUA sequences */
  11. enum {
  12. QUEUE_FSEQ_STARTED = (1 << 0), /* flushing in progress */
  13. QUEUE_FSEQ_PREFLUSH = (1 << 1), /* pre-flushing in progress */
  14. QUEUE_FSEQ_DATA = (1 << 2), /* data write in progress */
  15. QUEUE_FSEQ_POSTFLUSH = (1 << 3), /* post-flushing in progress */
  16. QUEUE_FSEQ_DONE = (1 << 4),
  17. };
  18. static struct request *queue_next_fseq(struct request_queue *q);
  19. unsigned blk_flush_cur_seq(struct request_queue *q)
  20. {
  21. if (!q->flush_seq)
  22. return 0;
  23. return 1 << ffz(q->flush_seq);
  24. }
  25. static struct request *blk_flush_complete_seq(struct request_queue *q,
  26. unsigned seq, int error)
  27. {
  28. struct request *next_rq = NULL;
  29. if (error && !q->flush_err)
  30. q->flush_err = error;
  31. BUG_ON(q->flush_seq & seq);
  32. q->flush_seq |= seq;
  33. if (blk_flush_cur_seq(q) != QUEUE_FSEQ_DONE) {
  34. /* not complete yet, queue the next flush sequence */
  35. next_rq = queue_next_fseq(q);
  36. } else {
  37. /* complete this flush request */
  38. __blk_end_request_all(q->orig_flush_rq, q->flush_err);
  39. q->orig_flush_rq = NULL;
  40. q->flush_seq = 0;
  41. /* dispatch the next flush if there's one */
  42. if (!list_empty(&q->pending_flushes)) {
  43. next_rq = list_entry_rq(q->pending_flushes.next);
  44. list_move(&next_rq->queuelist, &q->queue_head);
  45. }
  46. }
  47. return next_rq;
  48. }
  49. static void pre_flush_end_io(struct request *rq, int error)
  50. {
  51. elv_completed_request(rq->q, rq);
  52. blk_flush_complete_seq(rq->q, QUEUE_FSEQ_PREFLUSH, error);
  53. }
  54. static void flush_data_end_io(struct request *rq, int error)
  55. {
  56. elv_completed_request(rq->q, rq);
  57. blk_flush_complete_seq(rq->q, QUEUE_FSEQ_DATA, error);
  58. }
  59. static void post_flush_end_io(struct request *rq, int error)
  60. {
  61. elv_completed_request(rq->q, rq);
  62. blk_flush_complete_seq(rq->q, QUEUE_FSEQ_POSTFLUSH, error);
  63. }
  64. static void queue_flush(struct request_queue *q, struct request *rq,
  65. rq_end_io_fn *end_io)
  66. {
  67. blk_rq_init(q, rq);
  68. rq->cmd_type = REQ_TYPE_FS;
  69. rq->cmd_flags = REQ_FLUSH;
  70. rq->rq_disk = q->orig_flush_rq->rq_disk;
  71. rq->end_io = end_io;
  72. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  73. }
  74. static struct request *queue_next_fseq(struct request_queue *q)
  75. {
  76. struct request *orig_rq = q->orig_flush_rq;
  77. struct request *rq = &q->flush_rq;
  78. switch (blk_flush_cur_seq(q)) {
  79. case QUEUE_FSEQ_PREFLUSH:
  80. queue_flush(q, rq, pre_flush_end_io);
  81. break;
  82. case QUEUE_FSEQ_DATA:
  83. /* initialize proxy request, inherit FLUSH/FUA and queue it */
  84. blk_rq_init(q, rq);
  85. init_request_from_bio(rq, orig_rq->bio);
  86. rq->cmd_flags &= ~(REQ_FLUSH | REQ_FUA);
  87. rq->cmd_flags |= orig_rq->cmd_flags & (REQ_FLUSH | REQ_FUA);
  88. rq->end_io = flush_data_end_io;
  89. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  90. break;
  91. case QUEUE_FSEQ_POSTFLUSH:
  92. queue_flush(q, rq, post_flush_end_io);
  93. break;
  94. default:
  95. BUG();
  96. }
  97. return rq;
  98. }
  99. struct request *blk_do_flush(struct request_queue *q, struct request *rq)
  100. {
  101. unsigned int fflags = q->flush_flags; /* may change, cache it */
  102. bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA;
  103. bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH);
  104. bool do_postflush = has_flush && !has_fua && (rq->cmd_flags & REQ_FUA);
  105. unsigned skip = 0;
  106. /*
  107. * Special case. If there's data but flush is not necessary,
  108. * the request can be issued directly.
  109. *
  110. * Flush w/o data should be able to be issued directly too but
  111. * currently some drivers assume that rq->bio contains
  112. * non-zero data if it isn't NULL and empty FLUSH requests
  113. * getting here usually have bio's without data.
  114. */
  115. if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) {
  116. rq->cmd_flags &= ~REQ_FLUSH;
  117. if (!has_fua)
  118. rq->cmd_flags &= ~REQ_FUA;
  119. return rq;
  120. }
  121. /*
  122. * Sequenced flushes can't be processed in parallel. If
  123. * another one is already in progress, queue for later
  124. * processing.
  125. */
  126. if (q->flush_seq) {
  127. list_move_tail(&rq->queuelist, &q->pending_flushes);
  128. return NULL;
  129. }
  130. /*
  131. * Start a new flush sequence
  132. */
  133. q->flush_err = 0;
  134. q->flush_seq |= QUEUE_FSEQ_STARTED;
  135. /* adjust FLUSH/FUA of the original request and stash it away */
  136. rq->cmd_flags &= ~REQ_FLUSH;
  137. if (!has_fua)
  138. rq->cmd_flags &= ~REQ_FUA;
  139. blk_dequeue_request(rq);
  140. q->orig_flush_rq = rq;
  141. /* skip unneded sequences and return the first one */
  142. if (!do_preflush)
  143. skip |= QUEUE_FSEQ_PREFLUSH;
  144. if (!blk_rq_sectors(rq))
  145. skip |= QUEUE_FSEQ_DATA;
  146. if (!do_postflush)
  147. skip |= QUEUE_FSEQ_POSTFLUSH;
  148. return blk_flush_complete_seq(q, skip, 0);
  149. }
  150. static void bio_end_empty_barrier(struct bio *bio, int err)
  151. {
  152. if (err) {
  153. if (err == -EOPNOTSUPP)
  154. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  155. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  156. }
  157. if (bio->bi_private)
  158. complete(bio->bi_private);
  159. bio_put(bio);
  160. }
  161. /**
  162. * blkdev_issue_flush - queue a flush
  163. * @bdev: blockdev to issue flush for
  164. * @gfp_mask: memory allocation flags (for bio_alloc)
  165. * @error_sector: error sector
  166. * @flags: BLKDEV_IFL_* flags to control behaviour
  167. *
  168. * Description:
  169. * Issue a flush for the block device in question. Caller can supply
  170. * room for storing the error offset in case of a flush error, if they
  171. * wish to. If WAIT flag is not passed then caller may check only what
  172. * request was pushed in some internal queue for later handling.
  173. */
  174. int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  175. sector_t *error_sector, unsigned long flags)
  176. {
  177. DECLARE_COMPLETION_ONSTACK(wait);
  178. struct request_queue *q;
  179. struct bio *bio;
  180. int ret = 0;
  181. if (bdev->bd_disk == NULL)
  182. return -ENXIO;
  183. q = bdev_get_queue(bdev);
  184. if (!q)
  185. return -ENXIO;
  186. /*
  187. * some block devices may not have their queue correctly set up here
  188. * (e.g. loop device without a backing file) and so issuing a flush
  189. * here will panic. Ensure there is a request function before issuing
  190. * the barrier.
  191. */
  192. if (!q->make_request_fn)
  193. return -ENXIO;
  194. bio = bio_alloc(gfp_mask, 0);
  195. bio->bi_end_io = bio_end_empty_barrier;
  196. bio->bi_bdev = bdev;
  197. if (test_bit(BLKDEV_WAIT, &flags))
  198. bio->bi_private = &wait;
  199. bio_get(bio);
  200. submit_bio(WRITE_BARRIER, bio);
  201. if (test_bit(BLKDEV_WAIT, &flags)) {
  202. wait_for_completion(&wait);
  203. /*
  204. * The driver must store the error location in ->bi_sector, if
  205. * it supports it. For non-stacked drivers, this should be
  206. * copied from blk_rq_pos(rq).
  207. */
  208. if (error_sector)
  209. *error_sector = bio->bi_sector;
  210. }
  211. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  212. ret = -EOPNOTSUPP;
  213. else if (!bio_flagged(bio, BIO_UPTODATE))
  214. ret = -EIO;
  215. bio_put(bio);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(blkdev_issue_flush);