blk-flush.c 6.8 KB

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