blob: 72905f862d31b79ff79d80045c8a450845c26713 [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
59static void pre_flush_end_io(struct request *rq, int error)
60{
61 elv_completed_request(rq->q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020062 blk_flush_complete_seq(rq->q, QUEUE_FSEQ_PREFLUSH, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010063}
64
Tejun Heodd4c1332010-09-03 11:56:16 +020065static void flush_data_end_io(struct request *rq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010066{
67 elv_completed_request(rq->q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020068 blk_flush_complete_seq(rq->q, QUEUE_FSEQ_DATA, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010069}
70
71static void post_flush_end_io(struct request *rq, int error)
72{
73 elv_completed_request(rq->q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020074 blk_flush_complete_seq(rq->q, QUEUE_FSEQ_POSTFLUSH, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010075}
76
Christoph Hellwigcde4c402010-09-03 11:56:17 +020077static void init_flush_request(struct request *rq, struct gendisk *disk)
Jens Axboe86db1e22008-01-29 14:53:40 +010078{
FUJITA Tomonori28e18d02010-07-09 09:38:24 +090079 rq->cmd_type = REQ_TYPE_FS;
Tejun Heo28e7d182010-09-03 11:56:16 +020080 rq->cmd_flags = REQ_FLUSH;
Christoph Hellwigcde4c402010-09-03 11:56:17 +020081 rq->rq_disk = disk;
Jens Axboe86db1e22008-01-29 14:53:40 +010082}
83
Tejun Heodd4c1332010-09-03 11:56:16 +020084static struct request *queue_next_fseq(struct request_queue *q)
Tejun Heo28e7d182010-09-03 11:56:16 +020085{
Tejun Heo4fed9472010-09-03 11:56:17 +020086 struct request *orig_rq = q->orig_flush_rq;
Tejun Heodd4c1332010-09-03 11:56:16 +020087 struct request *rq = &q->flush_rq;
Tejun Heo28e7d182010-09-03 11:56:16 +020088
Christoph Hellwigcde4c402010-09-03 11:56:17 +020089 blk_rq_init(q, rq);
90
Tejun Heodd4c1332010-09-03 11:56:16 +020091 switch (blk_flush_cur_seq(q)) {
92 case QUEUE_FSEQ_PREFLUSH:
Christoph Hellwigcde4c402010-09-03 11:56:17 +020093 init_flush_request(rq, orig_rq->rq_disk);
94 rq->end_io = pre_flush_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +020095 break;
Tejun Heodd4c1332010-09-03 11:56:16 +020096 case QUEUE_FSEQ_DATA:
Tejun Heo4fed9472010-09-03 11:56:17 +020097 init_request_from_bio(rq, orig_rq->bio);
98 rq->cmd_flags &= ~(REQ_FLUSH | REQ_FUA);
99 rq->cmd_flags |= orig_rq->cmd_flags & (REQ_FLUSH | REQ_FUA);
Tejun Heodd4c1332010-09-03 11:56:16 +0200100 rq->end_io = flush_data_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +0200101 break;
Tejun Heodd4c1332010-09-03 11:56:16 +0200102 case QUEUE_FSEQ_POSTFLUSH:
Christoph Hellwigcde4c402010-09-03 11:56:17 +0200103 init_flush_request(rq, orig_rq->rq_disk);
104 rq->end_io = post_flush_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +0200105 break;
Tejun Heo28e7d182010-09-03 11:56:16 +0200106 default:
107 BUG();
108 }
Christoph Hellwigcde4c402010-09-03 11:56:17 +0200109
110 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
Tejun Heo28e7d182010-09-03 11:56:16 +0200111 return rq;
112}
113
Tejun Heodd4c1332010-09-03 11:56:16 +0200114struct request *blk_do_flush(struct request_queue *q, struct request *rq)
Jens Axboe86db1e22008-01-29 14:53:40 +0100115{
Tejun Heo4fed9472010-09-03 11:56:17 +0200116 unsigned int fflags = q->flush_flags; /* may change, cache it */
117 bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA;
118 bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH);
119 bool do_postflush = has_flush && !has_fua && (rq->cmd_flags & REQ_FUA);
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900120 unsigned skip = 0;
121
Tejun Heo4fed9472010-09-03 11:56:17 +0200122 /*
123 * Special case. If there's data but flush is not necessary,
124 * the request can be issued directly.
125 *
126 * Flush w/o data should be able to be issued directly too but
127 * currently some drivers assume that rq->bio contains
128 * non-zero data if it isn't NULL and empty FLUSH requests
129 * getting here usually have bio's without data.
130 */
131 if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) {
132 rq->cmd_flags &= ~REQ_FLUSH;
133 if (!has_fua)
134 rq->cmd_flags &= ~REQ_FUA;
Tejun Heo28e7d182010-09-03 11:56:16 +0200135 return rq;
Tejun Heo28e7d182010-09-03 11:56:16 +0200136 }
137
Tejun Heo4fed9472010-09-03 11:56:17 +0200138 /*
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);
Tejun Heo28e7d182010-09-03 11:56:16 +0200145 return NULL;
146 }
147
148 /*
Tejun Heodd4c1332010-09-03 11:56:16 +0200149 * Start a new flush sequence
Tejun Heo28e7d182010-09-03 11:56:16 +0200150 */
Tejun Heodd4c1332010-09-03 11:56:16 +0200151 q->flush_err = 0;
Tejun Heodd4c1332010-09-03 11:56:16 +0200152 q->flush_seq |= QUEUE_FSEQ_STARTED;
Jens Axboe86db1e22008-01-29 14:53:40 +0100153
Tejun Heo4fed9472010-09-03 11:56:17 +0200154 /* 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;
Tejun Heo9934c8c2009-05-08 11:54:16 +0900158 blk_dequeue_request(rq);
Tejun Heodd4c1332010-09-03 11:56:16 +0200159 q->orig_flush_rq = rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100160
Tejun Heo4fed9472010-09-03 11:56:17 +0200161 /* skip unneded sequences and return the first one */
162 if (!do_preflush)
Tejun Heodd4c1332010-09-03 11:56:16 +0200163 skip |= QUEUE_FSEQ_PREFLUSH;
Tejun Heo4fed9472010-09-03 11:56:17 +0200164 if (!blk_rq_sectors(rq))
Tejun Heodd4c1332010-09-03 11:56:16 +0200165 skip |= QUEUE_FSEQ_DATA;
Tejun Heo4fed9472010-09-03 11:56:17 +0200166 if (!do_postflush)
Tejun Heodd4c1332010-09-03 11:56:16 +0200167 skip |= QUEUE_FSEQ_POSTFLUSH;
Tejun Heodd4c1332010-09-03 11:56:16 +0200168 return blk_flush_complete_seq(q, skip, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100169}
170
171static void bio_end_empty_barrier(struct bio *bio, int err)
172{
Jens Axboecc66b452008-03-04 11:47:46 +0100173 if (err) {
174 if (err == -EOPNOTSUPP)
175 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100176 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100177 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400178 if (bio->bi_private)
179 complete(bio->bi_private);
180 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100181}
182
183/**
184 * blkdev_issue_flush - queue a flush
185 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400186 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100187 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400188 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100189 *
190 * Description:
191 * Issue a flush for the block device in question. Caller can supply
192 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400193 * wish to. If WAIT flag is not passed then caller may check only what
194 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100195 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400196int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
197 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100198{
199 DECLARE_COMPLETION_ONSTACK(wait);
200 struct request_queue *q;
201 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400202 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100203
204 if (bdev->bd_disk == NULL)
205 return -ENXIO;
206
207 q = bdev_get_queue(bdev);
208 if (!q)
209 return -ENXIO;
210
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000211 /*
212 * some block devices may not have their queue correctly set up here
213 * (e.g. loop device without a backing file) and so issuing a flush
214 * here will panic. Ensure there is a request function before issuing
215 * the barrier.
216 */
217 if (!q->make_request_fn)
218 return -ENXIO;
219
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400220 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100221 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100222 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400223 if (test_bit(BLKDEV_WAIT, &flags))
224 bio->bi_private = &wait;
225
226 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100227 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400228 if (test_bit(BLKDEV_WAIT, &flags)) {
229 wait_for_completion(&wait);
230 /*
231 * The driver must store the error location in ->bi_sector, if
232 * it supports it. For non-stacked drivers, this should be
233 * copied from blk_rq_pos(rq).
234 */
235 if (error_sector)
236 *error_sector = bio->bi_sector;
237 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100238
Jens Axboecc66b452008-03-04 11:47:46 +0100239 if (bio_flagged(bio, BIO_EOPNOTSUPP))
240 ret = -EOPNOTSUPP;
241 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100242 ret = -EIO;
243
244 bio_put(bio);
245 return ret;
246}
Jens Axboe86db1e22008-01-29 14:53:40 +0100247EXPORT_SYMBOL(blkdev_issue_flush);