blk-flush.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /*
  96. * orig_rq->rq_disk may be different from
  97. * bio->bi_bdev->bd_disk if orig_rq got here through
  98. * remapping drivers. Make sure rq->rq_disk points
  99. * to the same one as orig_rq.
  100. */
  101. rq->rq_disk = orig_rq->rq_disk;
  102. rq->cmd_flags &= ~(REQ_FLUSH | REQ_FUA);
  103. rq->cmd_flags |= orig_rq->cmd_flags & (REQ_FLUSH | REQ_FUA);
  104. rq->end_io = flush_data_end_io;
  105. break;
  106. case QUEUE_FSEQ_POSTFLUSH:
  107. init_flush_request(rq, orig_rq->rq_disk);
  108. rq->end_io = post_flush_end_io;
  109. break;
  110. default:
  111. BUG();
  112. }
  113. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  114. return rq;
  115. }
  116. struct request *blk_do_flush(struct request_queue *q, struct request *rq)
  117. {
  118. unsigned int fflags = q->flush_flags; /* may change, cache it */
  119. bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA;
  120. bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH);
  121. bool do_postflush = has_flush && !has_fua && (rq->cmd_flags & REQ_FUA);
  122. unsigned skip = 0;
  123. /*
  124. * Special case. If there's data but flush is not necessary,
  125. * the request can be issued directly.
  126. *
  127. * Flush w/o data should be able to be issued directly too but
  128. * currently some drivers assume that rq->bio contains
  129. * non-zero data if it isn't NULL and empty FLUSH requests
  130. * getting here usually have bio's without data.
  131. */
  132. if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) {
  133. rq->cmd_flags &= ~REQ_FLUSH;
  134. if (!has_fua)
  135. rq->cmd_flags &= ~REQ_FUA;
  136. return rq;
  137. }
  138. /*
  139. * Sequenced flushes can't be processed in parallel. If
  140. * another one is already in progress, queue for later
  141. * processing.
  142. */
  143. if (q->flush_seq) {
  144. list_move_tail(&rq->queuelist, &q->pending_flushes);
  145. return NULL;
  146. }
  147. /*
  148. * Start a new flush sequence
  149. */
  150. q->flush_err = 0;
  151. q->flush_seq |= QUEUE_FSEQ_STARTED;
  152. /* adjust FLUSH/FUA of the original request and stash it away */
  153. rq->cmd_flags &= ~REQ_FLUSH;
  154. if (!has_fua)
  155. rq->cmd_flags &= ~REQ_FUA;
  156. blk_dequeue_request(rq);
  157. q->orig_flush_rq = rq;
  158. /* skip unneded sequences and return the first one */
  159. if (!do_preflush)
  160. skip |= QUEUE_FSEQ_PREFLUSH;
  161. if (!blk_rq_sectors(rq))
  162. skip |= QUEUE_FSEQ_DATA;
  163. if (!do_postflush)
  164. skip |= QUEUE_FSEQ_POSTFLUSH;
  165. return blk_flush_complete_seq(q, skip, 0);
  166. }
  167. static void bio_end_flush(struct bio *bio, int err)
  168. {
  169. if (err)
  170. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  171. if (bio->bi_private)
  172. complete(bio->bi_private);
  173. bio_put(bio);
  174. }
  175. /**
  176. * blkdev_issue_flush - queue a flush
  177. * @bdev: blockdev to issue flush for
  178. * @gfp_mask: memory allocation flags (for bio_alloc)
  179. * @error_sector: error sector
  180. *
  181. * Description:
  182. * Issue a flush for the block device in question. Caller can supply
  183. * room for storing the error offset in case of a flush error, if they
  184. * wish to. If WAIT flag is not passed then caller may check only what
  185. * request was pushed in some internal queue for later handling.
  186. */
  187. int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  188. sector_t *error_sector)
  189. {
  190. DECLARE_COMPLETION_ONSTACK(wait);
  191. struct request_queue *q;
  192. struct bio *bio;
  193. int ret = 0;
  194. if (bdev->bd_disk == NULL)
  195. return -ENXIO;
  196. q = bdev_get_queue(bdev);
  197. if (!q)
  198. return -ENXIO;
  199. /*
  200. * some block devices may not have their queue correctly set up here
  201. * (e.g. loop device without a backing file) and so issuing a flush
  202. * here will panic. Ensure there is a request function before issuing
  203. * the flush.
  204. */
  205. if (!q->make_request_fn)
  206. return -ENXIO;
  207. bio = bio_alloc(gfp_mask, 0);
  208. bio->bi_end_io = bio_end_flush;
  209. bio->bi_bdev = bdev;
  210. bio->bi_private = &wait;
  211. bio_get(bio);
  212. submit_bio(WRITE_FLUSH, bio);
  213. wait_for_completion(&wait);
  214. /*
  215. * The driver must store the error location in ->bi_sector, if
  216. * it supports it. For non-stacked drivers, this should be
  217. * copied from blk_rq_pos(rq).
  218. */
  219. if (error_sector)
  220. *error_sector = bio->bi_sector;
  221. if (!bio_flagged(bio, BIO_UPTODATE))
  222. ret = -EIO;
  223. bio_put(bio);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL(blkdev_issue_flush);