blob: f1be85ba2bb5adc2d2e1161a8ed13a0a8c3997b8 [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 Heodd831002010-09-03 11:56:16 +0200113static inline struct request *start_ordered(struct request_queue *q,
114 struct request *rq)
Jens Axboe86db1e22008-01-29 14:53:40 +0100115{
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900116 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);
Tejun Heodd831002010-09-03 11:56:16 +0200152 init_request_from_bio(rq, q->orig_bar_rq->bio);
Tejun Heof6716202008-11-28 13:32:04 +0900153 if (q->ordered & QUEUE_ORDERED_DO_FUA)
154 rq->cmd_flags |= REQ_FUA;
Tejun Heof6716202008-11-28 13:32:04 +0900155 rq->end_io = bar_end_io;
156
157 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
158 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900159 skip |= QUEUE_ORDSEQ_BAR;
Jens Axboe86db1e22008-01-29 14:53:40 +0100160
Tejun Heo313e4292008-11-28 13:32:02 +0900161 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
162 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
Jens Axboe86db1e22008-01-29 14:53:40 +0100163 rq = &q->pre_flush_rq;
164 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900165 skip |= QUEUE_ORDSEQ_PREFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100166
Tejun Heo6958f142010-09-03 11:56:16 +0200167 if (queue_in_flight(q))
Jens Axboe86db1e22008-01-29 14:53:40 +0100168 rq = NULL;
Tejun Heof6716202008-11-28 13:32:04 +0900169 else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900170 skip |= QUEUE_ORDSEQ_DRAIN;
Jens Axboe86db1e22008-01-29 14:53:40 +0100171
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900172 /*
173 * Complete skipped sequences. If whole sequence is complete,
Tejun Heodd831002010-09-03 11:56:16 +0200174 * return %NULL to tell elevator that this request is gone.
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900175 */
Tejun Heodd831002010-09-03 11:56:16 +0200176 if (blk_ordered_complete_seq(q, skip, 0))
177 rq = NULL;
178 return rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100179}
180
Tejun Heodd831002010-09-03 11:56:16 +0200181struct request *blk_do_ordered(struct request_queue *q, struct request *rq)
Jens Axboe86db1e22008-01-29 14:53:40 +0100182{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200183 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
184 (rq->cmd_flags & REQ_HARDBARRIER);
Jens Axboe86db1e22008-01-29 14:53:40 +0100185
186 if (!q->ordseq) {
187 if (!is_barrier)
Tejun Heodd831002010-09-03 11:56:16 +0200188 return rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100189
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900190 if (q->next_ordered != QUEUE_ORDERED_NONE)
Tejun Heodd831002010-09-03 11:56:16 +0200191 return start_ordered(q, rq);
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900192 else {
Jens Axboe86db1e22008-01-29 14:53:40 +0100193 /*
Tejun Heoa7384672008-11-28 13:32:03 +0900194 * Queue ordering not supported. Terminate
195 * with prejudice.
Jens Axboe86db1e22008-01-29 14:53:40 +0100196 */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900197 blk_dequeue_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900198 __blk_end_request_all(rq, -EOPNOTSUPP);
Tejun Heodd831002010-09-03 11:56:16 +0200199 return NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100200 }
201 }
202
203 /*
204 * Ordered sequence in progress
205 */
206
207 /* Special requests are not subject to ordering rules. */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200208 if (rq->cmd_type != REQ_TYPE_FS &&
Jens Axboe86db1e22008-01-29 14:53:40 +0100209 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
Tejun Heodd831002010-09-03 11:56:16 +0200210 return rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100211
Tejun Heo6958f142010-09-03 11:56:16 +0200212 /* Ordered by draining. Wait for turn. */
213 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
214 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
Tejun Heodd831002010-09-03 11:56:16 +0200215 rq = ERR_PTR(-EAGAIN);
Jens Axboe86db1e22008-01-29 14:53:40 +0100216
Tejun Heodd831002010-09-03 11:56:16 +0200217 return rq;
Jens Axboe86db1e22008-01-29 14:53:40 +0100218}
219
220static void bio_end_empty_barrier(struct bio *bio, int err)
221{
Jens Axboecc66b452008-03-04 11:47:46 +0100222 if (err) {
223 if (err == -EOPNOTSUPP)
224 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100225 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100226 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400227 if (bio->bi_private)
228 complete(bio->bi_private);
229 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100230}
231
232/**
233 * blkdev_issue_flush - queue a flush
234 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400235 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100236 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400237 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100238 *
239 * Description:
240 * Issue a flush for the block device in question. Caller can supply
241 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400242 * wish to. If WAIT flag is not passed then caller may check only what
243 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100244 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400245int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
246 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100247{
248 DECLARE_COMPLETION_ONSTACK(wait);
249 struct request_queue *q;
250 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400251 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100252
253 if (bdev->bd_disk == NULL)
254 return -ENXIO;
255
256 q = bdev_get_queue(bdev);
257 if (!q)
258 return -ENXIO;
259
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000260 /*
261 * some block devices may not have their queue correctly set up here
262 * (e.g. loop device without a backing file) and so issuing a flush
263 * here will panic. Ensure there is a request function before issuing
264 * the barrier.
265 */
266 if (!q->make_request_fn)
267 return -ENXIO;
268
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400269 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100270 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100271 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400272 if (test_bit(BLKDEV_WAIT, &flags))
273 bio->bi_private = &wait;
274
275 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100276 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400277 if (test_bit(BLKDEV_WAIT, &flags)) {
278 wait_for_completion(&wait);
279 /*
280 * The driver must store the error location in ->bi_sector, if
281 * it supports it. For non-stacked drivers, this should be
282 * copied from blk_rq_pos(rq).
283 */
284 if (error_sector)
285 *error_sector = bio->bi_sector;
286 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100287
Jens Axboecc66b452008-03-04 11:47:46 +0100288 if (bio_flagged(bio, BIO_EOPNOTSUPP))
289 ret = -EOPNOTSUPP;
290 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100291 ret = -EIO;
292
293 bio_put(bio);
294 return ret;
295}
Jens Axboe86db1e22008-01-29 14:53:40 +0100296EXPORT_SYMBOL(blkdev_issue_flush);