blob: ed0aba5463abefbf9ce4b28992384f1ac2a9bc15 [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
Jens Axboe86db1e22008-01-29 14:53:40 +010012/*
13 * Cache flushing for ordered writes handling
14 */
Adrian Bunk6f6a0362008-04-29 09:49:06 +020015unsigned blk_ordered_cur_seq(struct request_queue *q)
Jens Axboe86db1e22008-01-29 14:53:40 +010016{
17 if (!q->ordseq)
18 return 0;
19 return 1 << ffz(q->ordseq);
20}
21
22unsigned blk_ordered_req_seq(struct request *rq)
23{
24 struct request_queue *q = rq->q;
25
26 BUG_ON(q->ordseq == 0);
27
28 if (rq == &q->pre_flush_rq)
29 return QUEUE_ORDSEQ_PREFLUSH;
30 if (rq == &q->bar_rq)
31 return QUEUE_ORDSEQ_BAR;
32 if (rq == &q->post_flush_rq)
33 return QUEUE_ORDSEQ_POSTFLUSH;
34
35 /*
36 * !fs requests don't need to follow barrier ordering. Always
37 * put them at the front. This fixes the following deadlock.
38 *
39 * http://thread.gmane.org/gmane.linux.kernel/537473
40 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +020041 if (rq->cmd_type != REQ_TYPE_FS)
Jens Axboe86db1e22008-01-29 14:53:40 +010042 return QUEUE_ORDSEQ_DRAIN;
43
44 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
45 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
46 return QUEUE_ORDSEQ_DRAIN;
47 else
48 return QUEUE_ORDSEQ_DONE;
49}
50
Tejun Heo8f11b3e2008-11-28 13:32:05 +090051bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010052{
53 struct request *rq;
54
55 if (error && !q->orderr)
56 q->orderr = error;
57
58 BUG_ON(q->ordseq & seq);
59 q->ordseq |= seq;
60
61 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
Tejun Heo8f11b3e2008-11-28 13:32:05 +090062 return false;
Jens Axboe86db1e22008-01-29 14:53:40 +010063
64 /*
65 * Okay, sequence complete.
66 */
67 q->ordseq = 0;
68 rq = q->orig_bar_rq;
Tejun Heo40cbbb72009-04-23 11:05:19 +090069 __blk_end_request_all(rq, q->orderr);
Tejun Heo8f11b3e2008-11-28 13:32:05 +090070 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +010071}
72
73static void pre_flush_end_io(struct request *rq, int error)
74{
75 elv_completed_request(rq->q, rq);
76 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
77}
78
79static void bar_end_io(struct request *rq, int error)
80{
81 elv_completed_request(rq->q, rq);
82 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
83}
84
85static void post_flush_end_io(struct request *rq, int error)
86{
87 elv_completed_request(rq->q, rq);
88 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
89}
90
91static void queue_flush(struct request_queue *q, unsigned which)
92{
93 struct request *rq;
94 rq_end_io_fn *end_io;
95
Tejun Heo313e4292008-11-28 13:32:02 +090096 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
Jens Axboe86db1e22008-01-29 14:53:40 +010097 rq = &q->pre_flush_rq;
98 end_io = pre_flush_end_io;
99 } else {
100 rq = &q->post_flush_rq;
101 end_io = post_flush_end_io;
102 }
103
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200104 blk_rq_init(q, rq);
FUJITA Tomonori28e18d02010-07-09 09:38:24 +0900105 rq->cmd_type = REQ_TYPE_FS;
FUJITA Tomonori87495342010-07-03 17:45:32 +0900106 rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
FUJITA Tomonori16f23192010-07-09 09:38:25 +0900107 rq->rq_disk = q->orig_bar_rq->rq_disk;
Jens Axboe86db1e22008-01-29 14:53:40 +0100108 rq->end_io = end_io;
Jens Axboe86db1e22008-01-29 14:53:40 +0100109
110 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
111}
112
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900113static inline bool start_ordered(struct request_queue *q, struct request **rqp)
Jens Axboe86db1e22008-01-29 14:53:40 +0100114{
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900115 struct request *rq = *rqp;
116 unsigned skip = 0;
117
Jens Axboe86db1e22008-01-29 14:53:40 +0100118 q->orderr = 0;
119 q->ordered = q->next_ordered;
120 q->ordseq |= QUEUE_ORDSEQ_STARTED;
121
Tejun Heo58eea922008-11-28 13:32:06 +0900122 /*
123 * For an empty barrier, there's no actual BAR request, which
124 * in turn makes POSTFLUSH unnecessary. Mask them off.
125 */
Tejun Heo6958f142010-09-03 11:56:16 +0200126 if (!blk_rq_sectors(rq))
Tejun Heo58eea922008-11-28 13:32:06 +0900127 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
128 QUEUE_ORDERED_DO_POSTFLUSH);
129
Tejun Heof6716202008-11-28 13:32:04 +0900130 /* stash away the original request */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900131 blk_dequeue_request(rq);
Jens Axboe86db1e22008-01-29 14:53:40 +0100132 q->orig_bar_rq = rq;
Tejun Heof6716202008-11-28 13:32:04 +0900133 rq = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100134
135 /*
136 * Queue ordered sequence. As we stack them at the head, we
137 * need to queue in reverse order. Note that we rely on that
138 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
Tejun Heo58eea922008-11-28 13:32:06 +0900139 * request gets inbetween ordered sequence.
Jens Axboe86db1e22008-01-29 14:53:40 +0100140 */
Tejun Heo58eea922008-11-28 13:32:06 +0900141 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
Tejun Heo313e4292008-11-28 13:32:02 +0900142 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
Tejun Heof6716202008-11-28 13:32:04 +0900143 rq = &q->post_flush_rq;
144 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900145 skip |= QUEUE_ORDSEQ_POSTFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100146
Tejun Heof6716202008-11-28 13:32:04 +0900147 if (q->ordered & QUEUE_ORDERED_DO_BAR) {
148 rq = &q->bar_rq;
149
150 /* initialize proxy request and queue it */
151 blk_rq_init(q, rq);
152 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200153 rq->cmd_flags |= REQ_WRITE;
Tejun Heof6716202008-11-28 13:32:04 +0900154 if (q->ordered & QUEUE_ORDERED_DO_FUA)
155 rq->cmd_flags |= REQ_FUA;
156 init_request_from_bio(rq, q->orig_bar_rq->bio);
157 rq->end_io = bar_end_io;
158
159 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
160 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900161 skip |= QUEUE_ORDSEQ_BAR;
Jens Axboe86db1e22008-01-29 14:53:40 +0100162
Tejun Heo313e4292008-11-28 13:32:02 +0900163 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
164 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
Jens Axboe86db1e22008-01-29 14:53:40 +0100165 rq = &q->pre_flush_rq;
166 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900167 skip |= QUEUE_ORDSEQ_PREFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100168
Tejun Heo6958f142010-09-03 11:56:16 +0200169 if (queue_in_flight(q))
Jens Axboe86db1e22008-01-29 14:53:40 +0100170 rq = NULL;
Tejun Heof6716202008-11-28 13:32:04 +0900171 else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900172 skip |= QUEUE_ORDSEQ_DRAIN;
Jens Axboe86db1e22008-01-29 14:53:40 +0100173
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900174 *rqp = rq;
175
176 /*
177 * Complete skipped sequences. If whole sequence is complete,
178 * return false to tell elevator that this request is gone.
179 */
180 return !blk_ordered_complete_seq(q, skip, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100181}
182
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900183bool blk_do_ordered(struct request_queue *q, struct request **rqp)
Jens Axboe86db1e22008-01-29 14:53:40 +0100184{
185 struct request *rq = *rqp;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200186 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
187 (rq->cmd_flags & REQ_HARDBARRIER);
Jens Axboe86db1e22008-01-29 14:53:40 +0100188
189 if (!q->ordseq) {
190 if (!is_barrier)
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900191 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100192
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900193 if (q->next_ordered != QUEUE_ORDERED_NONE)
194 return start_ordered(q, rqp);
195 else {
Jens Axboe86db1e22008-01-29 14:53:40 +0100196 /*
Tejun Heoa7384672008-11-28 13:32:03 +0900197 * Queue ordering not supported. Terminate
198 * with prejudice.
Jens Axboe86db1e22008-01-29 14:53:40 +0100199 */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900200 blk_dequeue_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900201 __blk_end_request_all(rq, -EOPNOTSUPP);
Jens Axboe86db1e22008-01-29 14:53:40 +0100202 *rqp = NULL;
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900203 return false;
Jens Axboe86db1e22008-01-29 14:53:40 +0100204 }
205 }
206
207 /*
208 * Ordered sequence in progress
209 */
210
211 /* Special requests are not subject to ordering rules. */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200212 if (rq->cmd_type != REQ_TYPE_FS &&
Jens Axboe86db1e22008-01-29 14:53:40 +0100213 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900214 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100215
Tejun Heo6958f142010-09-03 11:56:16 +0200216 /* Ordered by draining. Wait for turn. */
217 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
218 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
219 *rqp = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100220
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900221 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100222}
223
224static void bio_end_empty_barrier(struct bio *bio, int err)
225{
Jens Axboecc66b452008-03-04 11:47:46 +0100226 if (err) {
227 if (err == -EOPNOTSUPP)
228 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100229 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100230 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400231 if (bio->bi_private)
232 complete(bio->bi_private);
233 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100234}
235
236/**
237 * blkdev_issue_flush - queue a flush
238 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400239 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100240 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400241 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100242 *
243 * Description:
244 * Issue a flush for the block device in question. Caller can supply
245 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400246 * wish to. If WAIT flag is not passed then caller may check only what
247 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100248 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400249int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
250 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100251{
252 DECLARE_COMPLETION_ONSTACK(wait);
253 struct request_queue *q;
254 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400255 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100256
257 if (bdev->bd_disk == NULL)
258 return -ENXIO;
259
260 q = bdev_get_queue(bdev);
261 if (!q)
262 return -ENXIO;
263
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000264 /*
265 * some block devices may not have their queue correctly set up here
266 * (e.g. loop device without a backing file) and so issuing a flush
267 * here will panic. Ensure there is a request function before issuing
268 * the barrier.
269 */
270 if (!q->make_request_fn)
271 return -ENXIO;
272
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400273 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100274 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100275 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400276 if (test_bit(BLKDEV_WAIT, &flags))
277 bio->bi_private = &wait;
278
279 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100280 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400281 if (test_bit(BLKDEV_WAIT, &flags)) {
282 wait_for_completion(&wait);
283 /*
284 * The driver must store the error location in ->bi_sector, if
285 * it supports it. For non-stacked drivers, this should be
286 * copied from blk_rq_pos(rq).
287 */
288 if (error_sector)
289 *error_sector = bio->bi_sector;
290 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100291
Jens Axboecc66b452008-03-04 11:47:46 +0100292 if (bio_flagged(bio, BIO_EOPNOTSUPP))
293 ret = -EOPNOTSUPP;
294 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100295 ret = -EIO;
296
297 bio_put(bio);
298 return ret;
299}
Jens Axboe86db1e22008-01-29 14:53:40 +0100300EXPORT_SYMBOL(blkdev_issue_flush);