blob: 7d1fc982e78f081393a06b37703d72f81b093d82 [file] [log] [blame]
Jens Axboe86db1e22008-01-29 14:53:40 +01001/*
Tejun Heo4fed9472010-09-03 11:56:17 +02002 * Functions to sequence FLUSH and FUA writes.
Jens Axboe86db1e22008-01-29 14:53:40 +01003 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/gfp.h>
Jens Axboe86db1e22008-01-29 14:53:40 +01009
10#include "blk.h"
11
Tejun Heo4fed9472010-09-03 11:56:17 +020012/* FLUSH/FUA sequences */
13enum {
14 QUEUE_FSEQ_STARTED = (1 << 0), /* flushing in progress */
15 QUEUE_FSEQ_PREFLUSH = (1 << 1), /* pre-flushing in progress */
16 QUEUE_FSEQ_DATA = (1 << 2), /* data write in progress */
17 QUEUE_FSEQ_POSTFLUSH = (1 << 3), /* post-flushing in progress */
18 QUEUE_FSEQ_DONE = (1 << 4),
19};
20
Tejun Heodd4c1332010-09-03 11:56:16 +020021static struct request *queue_next_fseq(struct request_queue *q);
Tejun Heo28e7d182010-09-03 11:56:16 +020022
Tejun Heodd4c1332010-09-03 11:56:16 +020023unsigned blk_flush_cur_seq(struct request_queue *q)
Jens Axboe86db1e22008-01-29 14:53:40 +010024{
Tejun Heodd4c1332010-09-03 11:56:16 +020025 if (!q->flush_seq)
Jens Axboe86db1e22008-01-29 14:53:40 +010026 return 0;
Tejun Heodd4c1332010-09-03 11:56:16 +020027 return 1 << ffz(q->flush_seq);
Jens Axboe86db1e22008-01-29 14:53:40 +010028}
29
Tejun Heodd4c1332010-09-03 11:56:16 +020030static struct request *blk_flush_complete_seq(struct request_queue *q,
31 unsigned seq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010032{
Tejun Heo28e7d182010-09-03 11:56:16 +020033 struct request *next_rq = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +010034
Tejun Heodd4c1332010-09-03 11:56:16 +020035 if (error && !q->flush_err)
36 q->flush_err = error;
Jens Axboe86db1e22008-01-29 14:53:40 +010037
Tejun Heodd4c1332010-09-03 11:56:16 +020038 BUG_ON(q->flush_seq & seq);
39 q->flush_seq |= seq;
Jens Axboe86db1e22008-01-29 14:53:40 +010040
Tejun Heodd4c1332010-09-03 11:56:16 +020041 if (blk_flush_cur_seq(q) != QUEUE_FSEQ_DONE) {
42 /* not complete yet, queue the next flush sequence */
43 next_rq = queue_next_fseq(q);
Tejun Heo28e7d182010-09-03 11:56:16 +020044 } else {
Tejun Heodd4c1332010-09-03 11:56:16 +020045 /* complete this flush request */
46 __blk_end_request_all(q->orig_flush_rq, q->flush_err);
47 q->orig_flush_rq = NULL;
48 q->flush_seq = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +010049
Tejun Heodd4c1332010-09-03 11:56:16 +020050 /* dispatch the next flush if there's one */
51 if (!list_empty(&q->pending_flushes)) {
52 next_rq = list_entry_rq(q->pending_flushes.next);
Tejun Heo28e7d182010-09-03 11:56:16 +020053 list_move(&next_rq->queuelist, &q->queue_head);
54 }
55 }
56 return next_rq;
Jens Axboe86db1e22008-01-29 14:53:40 +010057}
58
Tejun Heo47f70d52010-09-03 11:56:17 +020059static void blk_flush_complete_seq_end_io(struct request_queue *q,
60 unsigned seq, int error)
61{
62 bool was_empty = elv_queue_empty(q);
63 struct request *next_rq;
64
65 next_rq = blk_flush_complete_seq(q, seq, error);
66
67 /*
68 * Moving a request silently to empty queue_head may stall the
69 * queue. Kick the queue in those cases.
70 */
71 if (was_empty && next_rq)
72 __blk_run_queue(q);
73}
74
Jens Axboe86db1e22008-01-29 14:53:40 +010075static void pre_flush_end_io(struct request *rq, int error)
76{
77 elv_completed_request(rq->q, rq);
Tejun Heo47f70d52010-09-03 11:56:17 +020078 blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_PREFLUSH, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010079}
80
Tejun Heodd4c1332010-09-03 11:56:16 +020081static void flush_data_end_io(struct request *rq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010082{
83 elv_completed_request(rq->q, rq);
Tejun Heo47f70d52010-09-03 11:56:17 +020084 blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_DATA, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010085}
86
87static void post_flush_end_io(struct request *rq, int error)
88{
89 elv_completed_request(rq->q, rq);
Tejun Heo47f70d52010-09-03 11:56:17 +020090 blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_POSTFLUSH, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010091}
92
Christoph Hellwigcde4c402010-09-03 11:56:17 +020093static void init_flush_request(struct request *rq, struct gendisk *disk)
Jens Axboe86db1e22008-01-29 14:53:40 +010094{
FUJITA Tomonori28e18d02010-07-09 09:38:24 +090095 rq->cmd_type = REQ_TYPE_FS;
Tejun Heo337238b2010-09-03 11:56:17 +020096 rq->cmd_flags = WRITE_FLUSH;
Christoph Hellwigcde4c402010-09-03 11:56:17 +020097 rq->rq_disk = disk;
Jens Axboe86db1e22008-01-29 14:53:40 +010098}
99
Tejun Heodd4c1332010-09-03 11:56:16 +0200100static struct request *queue_next_fseq(struct request_queue *q)
Tejun Heo28e7d182010-09-03 11:56:16 +0200101{
Tejun Heo4fed9472010-09-03 11:56:17 +0200102 struct request *orig_rq = q->orig_flush_rq;
Tejun Heodd4c1332010-09-03 11:56:16 +0200103 struct request *rq = &q->flush_rq;
Tejun Heo28e7d182010-09-03 11:56:16 +0200104
Christoph Hellwigcde4c402010-09-03 11:56:17 +0200105 blk_rq_init(q, rq);
106
Tejun Heodd4c1332010-09-03 11:56:16 +0200107 switch (blk_flush_cur_seq(q)) {
108 case QUEUE_FSEQ_PREFLUSH:
Christoph Hellwigcde4c402010-09-03 11:56:17 +0200109 init_flush_request(rq, orig_rq->rq_disk);
110 rq->end_io = pre_flush_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +0200111 break;
Tejun Heodd4c1332010-09-03 11:56:16 +0200112 case QUEUE_FSEQ_DATA:
Tejun Heo4fed9472010-09-03 11:56:17 +0200113 init_request_from_bio(rq, orig_rq->bio);
Tejun Heo09d60c72010-09-03 11:56:17 +0200114 /*
115 * orig_rq->rq_disk may be different from
116 * bio->bi_bdev->bd_disk if orig_rq got here through
117 * remapping drivers. Make sure rq->rq_disk points
118 * to the same one as orig_rq.
119 */
120 rq->rq_disk = orig_rq->rq_disk;
Tejun Heo4fed9472010-09-03 11:56:17 +0200121 rq->cmd_flags &= ~(REQ_FLUSH | REQ_FUA);
122 rq->cmd_flags |= orig_rq->cmd_flags & (REQ_FLUSH | REQ_FUA);
Tejun Heodd4c1332010-09-03 11:56:16 +0200123 rq->end_io = flush_data_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +0200124 break;
Tejun Heodd4c1332010-09-03 11:56:16 +0200125 case QUEUE_FSEQ_POSTFLUSH:
Christoph Hellwigcde4c402010-09-03 11:56:17 +0200126 init_flush_request(rq, orig_rq->rq_disk);
127 rq->end_io = post_flush_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +0200128 break;
Tejun Heo28e7d182010-09-03 11:56:16 +0200129 default:
130 BUG();
131 }
Christoph Hellwigcde4c402010-09-03 11:56:17 +0200132
133 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
Tejun Heo28e7d182010-09-03 11:56:16 +0200134 return rq;
135}
136
Tejun Heodd4c1332010-09-03 11:56:16 +0200137struct request *blk_do_flush(struct request_queue *q, struct request *rq)
Jens Axboe86db1e22008-01-29 14:53:40 +0100138{
Tejun Heo4fed9472010-09-03 11:56:17 +0200139 unsigned int fflags = q->flush_flags; /* may change, cache it */
140 bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA;
141 bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH);
142 bool do_postflush = has_flush && !has_fua && (rq->cmd_flags & REQ_FUA);
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900143 unsigned skip = 0;
144
Tejun Heo4fed9472010-09-03 11:56:17 +0200145 /*
146 * Special case. If there's data but flush is not necessary,
147 * the request can be issued directly.
148 *
149 * Flush w/o data should be able to be issued directly too but
150 * currently some drivers assume that rq->bio contains
151 * non-zero data if it isn't NULL and empty FLUSH requests
152 * getting here usually have bio's without data.
153 */
154 if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) {
155 rq->cmd_flags &= ~REQ_FLUSH;
156 if (!has_fua)
157 rq->cmd_flags &= ~REQ_FUA;
Tejun Heo28e7d182010-09-03 11:56:16 +0200158 return rq;
Tejun Heo28e7d182010-09-03 11:56:16 +0200159 }
160
Tejun Heo4fed9472010-09-03 11:56:17 +0200161 /*
162 * Sequenced flushes can't be processed in parallel. If
163 * another one is already in progress, queue for later
164 * processing.
165 */
166 if (q->flush_seq) {
167 list_move_tail(&rq->queuelist, &q->pending_flushes);
Tejun Heo28e7d182010-09-03 11:56:16 +0200168 return NULL;
169 }
170
171 /*
Tejun Heodd4c1332010-09-03 11:56:16 +0200172 * Start a new flush sequence
Tejun Heo28e7d182010-09-03 11:56:16 +0200173 */
Tejun Heodd4c1332010-09-03 11:56:16 +0200174 q->flush_err = 0;
Tejun Heodd4c1332010-09-03 11:56:16 +0200175 q->flush_seq |= QUEUE_FSEQ_STARTED;
Jens Axboe86db1e22008-01-29 14:53:40 +0100176
Tejun Heo4fed9472010-09-03 11:56:17 +0200177 /* adjust FLUSH/FUA of the original request and stash it away */
178 rq->cmd_flags &= ~REQ_FLUSH;
179 if (!has_fua)
180 rq->cmd_flags &= ~REQ_FUA;
Tejun Heo9934c8c2009-05-08 11:54:16 +0900181 blk_dequeue_request(rq);
Tejun Heodd4c1332010-09-03 11:56:16 +0200182 q->orig_flush_rq = rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100183
Tejun Heo4fed9472010-09-03 11:56:17 +0200184 /* skip unneded sequences and return the first one */
185 if (!do_preflush)
Tejun Heodd4c1332010-09-03 11:56:16 +0200186 skip |= QUEUE_FSEQ_PREFLUSH;
Tejun Heo4fed9472010-09-03 11:56:17 +0200187 if (!blk_rq_sectors(rq))
Tejun Heodd4c1332010-09-03 11:56:16 +0200188 skip |= QUEUE_FSEQ_DATA;
Tejun Heo4fed9472010-09-03 11:56:17 +0200189 if (!do_postflush)
Tejun Heodd4c1332010-09-03 11:56:16 +0200190 skip |= QUEUE_FSEQ_POSTFLUSH;
Tejun Heodd4c1332010-09-03 11:56:16 +0200191 return blk_flush_complete_seq(q, skip, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100192}
193
194static void bio_end_empty_barrier(struct bio *bio, int err)
195{
Jens Axboecc66b452008-03-04 11:47:46 +0100196 if (err) {
197 if (err == -EOPNOTSUPP)
198 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100199 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100200 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400201 if (bio->bi_private)
202 complete(bio->bi_private);
203 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100204}
205
206/**
207 * blkdev_issue_flush - queue a flush
208 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400209 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100210 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400211 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100212 *
213 * Description:
214 * Issue a flush for the block device in question. Caller can supply
215 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400216 * wish to. If WAIT flag is not passed then caller may check only what
217 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100218 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400219int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
220 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100221{
222 DECLARE_COMPLETION_ONSTACK(wait);
223 struct request_queue *q;
224 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400225 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100226
227 if (bdev->bd_disk == NULL)
228 return -ENXIO;
229
230 q = bdev_get_queue(bdev);
231 if (!q)
232 return -ENXIO;
233
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000234 /*
235 * some block devices may not have their queue correctly set up here
236 * (e.g. loop device without a backing file) and so issuing a flush
237 * here will panic. Ensure there is a request function before issuing
238 * the barrier.
239 */
240 if (!q->make_request_fn)
241 return -ENXIO;
242
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400243 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100244 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100245 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400246 if (test_bit(BLKDEV_WAIT, &flags))
247 bio->bi_private = &wait;
248
249 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100250 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400251 if (test_bit(BLKDEV_WAIT, &flags)) {
252 wait_for_completion(&wait);
253 /*
254 * The driver must store the error location in ->bi_sector, if
255 * it supports it. For non-stacked drivers, this should be
256 * copied from blk_rq_pos(rq).
257 */
258 if (error_sector)
259 *error_sector = bio->bi_sector;
260 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100261
Jens Axboecc66b452008-03-04 11:47:46 +0100262 if (bio_flagged(bio, BIO_EOPNOTSUPP))
263 ret = -EOPNOTSUPP;
264 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100265 ret = -EIO;
266
267 bio_put(bio);
268 return ret;
269}
Jens Axboe86db1e22008-01-29 14:53:40 +0100270EXPORT_SYMBOL(blkdev_issue_flush);