blob: dd873225da972be6ac0b71ed57a2e8e7a7bf3597 [file] [log] [blame]
Jens Axboe86db1e22008-01-29 14:53:40 +01001/*
2 * Functions related to barrier IO handling
3 */
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 Heodd4c1332010-09-03 11:56:16 +020012static struct request *queue_next_fseq(struct request_queue *q);
Tejun Heo28e7d182010-09-03 11:56:16 +020013
Tejun Heodd4c1332010-09-03 11:56:16 +020014unsigned blk_flush_cur_seq(struct request_queue *q)
Jens Axboe86db1e22008-01-29 14:53:40 +010015{
Tejun Heodd4c1332010-09-03 11:56:16 +020016 if (!q->flush_seq)
Jens Axboe86db1e22008-01-29 14:53:40 +010017 return 0;
Tejun Heodd4c1332010-09-03 11:56:16 +020018 return 1 << ffz(q->flush_seq);
Jens Axboe86db1e22008-01-29 14:53:40 +010019}
20
Tejun Heodd4c1332010-09-03 11:56:16 +020021static struct request *blk_flush_complete_seq(struct request_queue *q,
22 unsigned seq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010023{
Tejun Heo28e7d182010-09-03 11:56:16 +020024 struct request *next_rq = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +010025
Tejun Heodd4c1332010-09-03 11:56:16 +020026 if (error && !q->flush_err)
27 q->flush_err = error;
Jens Axboe86db1e22008-01-29 14:53:40 +010028
Tejun Heodd4c1332010-09-03 11:56:16 +020029 BUG_ON(q->flush_seq & seq);
30 q->flush_seq |= seq;
Jens Axboe86db1e22008-01-29 14:53:40 +010031
Tejun Heodd4c1332010-09-03 11:56:16 +020032 if (blk_flush_cur_seq(q) != QUEUE_FSEQ_DONE) {
33 /* not complete yet, queue the next flush sequence */
34 next_rq = queue_next_fseq(q);
Tejun Heo28e7d182010-09-03 11:56:16 +020035 } else {
Tejun Heodd4c1332010-09-03 11:56:16 +020036 /* complete this flush request */
37 __blk_end_request_all(q->orig_flush_rq, q->flush_err);
38 q->orig_flush_rq = NULL;
39 q->flush_seq = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +010040
Tejun Heodd4c1332010-09-03 11:56:16 +020041 /* 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);
Tejun Heo28e7d182010-09-03 11:56:16 +020044 list_move(&next_rq->queuelist, &q->queue_head);
45 }
46 }
47 return next_rq;
Jens Axboe86db1e22008-01-29 14:53:40 +010048}
49
50static void pre_flush_end_io(struct request *rq, int error)
51{
52 elv_completed_request(rq->q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020053 blk_flush_complete_seq(rq->q, QUEUE_FSEQ_PREFLUSH, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010054}
55
Tejun Heodd4c1332010-09-03 11:56:16 +020056static void flush_data_end_io(struct request *rq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010057{
58 elv_completed_request(rq->q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020059 blk_flush_complete_seq(rq->q, QUEUE_FSEQ_DATA, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010060}
61
62static void post_flush_end_io(struct request *rq, int error)
63{
64 elv_completed_request(rq->q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020065 blk_flush_complete_seq(rq->q, QUEUE_FSEQ_POSTFLUSH, error);
Jens Axboe86db1e22008-01-29 14:53:40 +010066}
67
Tejun Heo28e7d182010-09-03 11:56:16 +020068static void queue_flush(struct request_queue *q, struct request *rq,
69 rq_end_io_fn *end_io)
Jens Axboe86db1e22008-01-29 14:53:40 +010070{
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +020071 blk_rq_init(q, rq);
FUJITA Tomonori28e18d02010-07-09 09:38:24 +090072 rq->cmd_type = REQ_TYPE_FS;
Tejun Heo28e7d182010-09-03 11:56:16 +020073 rq->cmd_flags = REQ_FLUSH;
Tejun Heodd4c1332010-09-03 11:56:16 +020074 rq->rq_disk = q->orig_flush_rq->rq_disk;
Jens Axboe86db1e22008-01-29 14:53:40 +010075 rq->end_io = end_io;
Jens Axboe86db1e22008-01-29 14:53:40 +010076
77 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
78}
79
Tejun Heodd4c1332010-09-03 11:56:16 +020080static struct request *queue_next_fseq(struct request_queue *q)
Tejun Heo28e7d182010-09-03 11:56:16 +020081{
Tejun Heodd4c1332010-09-03 11:56:16 +020082 struct request *rq = &q->flush_rq;
Tejun Heo28e7d182010-09-03 11:56:16 +020083
Tejun Heodd4c1332010-09-03 11:56:16 +020084 switch (blk_flush_cur_seq(q)) {
85 case QUEUE_FSEQ_PREFLUSH:
Tejun Heo28e7d182010-09-03 11:56:16 +020086 queue_flush(q, rq, pre_flush_end_io);
87 break;
88
Tejun Heodd4c1332010-09-03 11:56:16 +020089 case QUEUE_FSEQ_DATA:
Tejun Heo28e7d182010-09-03 11:56:16 +020090 /* initialize proxy request and queue it */
91 blk_rq_init(q, rq);
Tejun Heodd4c1332010-09-03 11:56:16 +020092 init_request_from_bio(rq, q->orig_flush_rq->bio);
Tejun Heo28e7d182010-09-03 11:56:16 +020093 rq->cmd_flags &= ~REQ_HARDBARRIER;
94 if (q->ordered & QUEUE_ORDERED_DO_FUA)
95 rq->cmd_flags |= REQ_FUA;
Tejun Heodd4c1332010-09-03 11:56:16 +020096 rq->end_io = flush_data_end_io;
Tejun Heo28e7d182010-09-03 11:56:16 +020097
98 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
99 break;
100
Tejun Heodd4c1332010-09-03 11:56:16 +0200101 case QUEUE_FSEQ_POSTFLUSH:
Tejun Heo28e7d182010-09-03 11:56:16 +0200102 queue_flush(q, rq, post_flush_end_io);
103 break;
104
105 default:
106 BUG();
107 }
108 return rq;
109}
110
Tejun Heodd4c1332010-09-03 11:56:16 +0200111struct request *blk_do_flush(struct request_queue *q, struct request *rq)
Jens Axboe86db1e22008-01-29 14:53:40 +0100112{
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900113 unsigned skip = 0;
114
Tejun Heo28e7d182010-09-03 11:56:16 +0200115 if (!(rq->cmd_flags & REQ_HARDBARRIER))
116 return rq;
117
Tejun Heodd4c1332010-09-03 11:56:16 +0200118 if (q->flush_seq) {
Tejun Heo28e7d182010-09-03 11:56:16 +0200119 /*
Tejun Heodd4c1332010-09-03 11:56:16 +0200120 * Sequenced flush is already in progress and they
121 * can't be processed in parallel. Queue for later
122 * processing.
Tejun Heo28e7d182010-09-03 11:56:16 +0200123 */
Tejun Heodd4c1332010-09-03 11:56:16 +0200124 list_move_tail(&rq->queuelist, &q->pending_flushes);
Tejun Heo28e7d182010-09-03 11:56:16 +0200125 return NULL;
126 }
127
128 if (unlikely(q->next_ordered == QUEUE_ORDERED_NONE)) {
129 /*
130 * Queue ordering not supported. Terminate
131 * with prejudice.
132 */
133 blk_dequeue_request(rq);
134 __blk_end_request_all(rq, -EOPNOTSUPP);
135 return NULL;
136 }
137
138 /*
Tejun Heodd4c1332010-09-03 11:56:16 +0200139 * Start a new flush sequence
Tejun Heo28e7d182010-09-03 11:56:16 +0200140 */
Tejun Heodd4c1332010-09-03 11:56:16 +0200141 q->flush_err = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100142 q->ordered = q->next_ordered;
Tejun Heodd4c1332010-09-03 11:56:16 +0200143 q->flush_seq |= QUEUE_FSEQ_STARTED;
Jens Axboe86db1e22008-01-29 14:53:40 +0100144
Tejun Heo58eea922008-11-28 13:32:06 +0900145 /*
146 * For an empty barrier, there's no actual BAR request, which
147 * in turn makes POSTFLUSH unnecessary. Mask them off.
148 */
Tejun Heo6958f142010-09-03 11:56:16 +0200149 if (!blk_rq_sectors(rq))
Tejun Heo58eea922008-11-28 13:32:06 +0900150 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
151 QUEUE_ORDERED_DO_POSTFLUSH);
152
Tejun Heof6716202008-11-28 13:32:04 +0900153 /* stash away the original request */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900154 blk_dequeue_request(rq);
Tejun Heodd4c1332010-09-03 11:56:16 +0200155 q->orig_flush_rq = rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100156
Tejun Heo28e7d182010-09-03 11:56:16 +0200157 if (!(q->ordered & QUEUE_ORDERED_DO_PREFLUSH))
Tejun Heodd4c1332010-09-03 11:56:16 +0200158 skip |= QUEUE_FSEQ_PREFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100159
Tejun Heo28e7d182010-09-03 11:56:16 +0200160 if (!(q->ordered & QUEUE_ORDERED_DO_BAR))
Tejun Heodd4c1332010-09-03 11:56:16 +0200161 skip |= QUEUE_FSEQ_DATA;
Jens Axboe86db1e22008-01-29 14:53:40 +0100162
Tejun Heo28e7d182010-09-03 11:56:16 +0200163 if (!(q->ordered & QUEUE_ORDERED_DO_POSTFLUSH))
Tejun Heodd4c1332010-09-03 11:56:16 +0200164 skip |= QUEUE_FSEQ_POSTFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100165
Tejun Heo28e7d182010-09-03 11:56:16 +0200166 /* complete skipped sequences and return the first sequence */
Tejun Heodd4c1332010-09-03 11:56:16 +0200167 return blk_flush_complete_seq(q, skip, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100168}
169
170static void bio_end_empty_barrier(struct bio *bio, int err)
171{
Jens Axboecc66b452008-03-04 11:47:46 +0100172 if (err) {
173 if (err == -EOPNOTSUPP)
174 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100175 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100176 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400177 if (bio->bi_private)
178 complete(bio->bi_private);
179 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100180}
181
182/**
183 * blkdev_issue_flush - queue a flush
184 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400185 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100186 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400187 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100188 *
189 * Description:
190 * Issue a flush for the block device in question. Caller can supply
191 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400192 * wish to. If WAIT flag is not passed then caller may check only what
193 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100194 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400195int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
196 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100197{
198 DECLARE_COMPLETION_ONSTACK(wait);
199 struct request_queue *q;
200 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400201 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100202
203 if (bdev->bd_disk == NULL)
204 return -ENXIO;
205
206 q = bdev_get_queue(bdev);
207 if (!q)
208 return -ENXIO;
209
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000210 /*
211 * some block devices may not have their queue correctly set up here
212 * (e.g. loop device without a backing file) and so issuing a flush
213 * here will panic. Ensure there is a request function before issuing
214 * the barrier.
215 */
216 if (!q->make_request_fn)
217 return -ENXIO;
218
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400219 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100220 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100221 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400222 if (test_bit(BLKDEV_WAIT, &flags))
223 bio->bi_private = &wait;
224
225 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100226 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400227 if (test_bit(BLKDEV_WAIT, &flags)) {
228 wait_for_completion(&wait);
229 /*
230 * The driver must store the error location in ->bi_sector, if
231 * it supports it. For non-stacked drivers, this should be
232 * copied from blk_rq_pos(rq).
233 */
234 if (error_sector)
235 *error_sector = bio->bi_sector;
236 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100237
Jens Axboecc66b452008-03-04 11:47:46 +0100238 if (bio_flagged(bio, BIO_EOPNOTSUPP))
239 ret = -EOPNOTSUPP;
240 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100241 ret = -EIO;
242
243 bio_put(bio);
244 return ret;
245}
Jens Axboe86db1e22008-01-29 14:53:40 +0100246EXPORT_SYMBOL(blkdev_issue_flush);