blk-flush.c 6.8 KB

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