blob: 557f69360b6a027285b2507c8ad139c8ecea31c2 [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
12/**
13 * blk_queue_ordered - does this queue support ordered writes
14 * @q: the request queue
15 * @ordered: one of QUEUE_ORDERED_*
Jens Axboe86db1e22008-01-29 14:53:40 +010016 *
17 * Description:
18 * For journalled file systems, doing ordered writes on a commit
19 * block instead of explicitly doing wait_on_buffer (which is bad
20 * for performance) can be a big win. Block drivers supporting this
21 * feature should call this function and indicate so.
22 *
23 **/
FUJITA Tomonori00fff262010-07-03 17:45:40 +090024int blk_queue_ordered(struct request_queue *q, unsigned ordered)
Jens Axboe86db1e22008-01-29 14:53:40 +010025{
Jens Axboe86db1e22008-01-29 14:53:40 +010026 if (ordered != QUEUE_ORDERED_NONE &&
27 ordered != QUEUE_ORDERED_DRAIN &&
28 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
29 ordered != QUEUE_ORDERED_DRAIN_FUA &&
30 ordered != QUEUE_ORDERED_TAG &&
31 ordered != QUEUE_ORDERED_TAG_FLUSH &&
32 ordered != QUEUE_ORDERED_TAG_FUA) {
33 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
34 return -EINVAL;
35 }
36
37 q->ordered = ordered;
38 q->next_ordered = ordered;
Jens Axboe86db1e22008-01-29 14:53:40 +010039
40 return 0;
41}
Jens Axboe86db1e22008-01-29 14:53:40 +010042EXPORT_SYMBOL(blk_queue_ordered);
43
44/*
45 * Cache flushing for ordered writes handling
46 */
Adrian Bunk6f6a0362008-04-29 09:49:06 +020047unsigned blk_ordered_cur_seq(struct request_queue *q)
Jens Axboe86db1e22008-01-29 14:53:40 +010048{
49 if (!q->ordseq)
50 return 0;
51 return 1 << ffz(q->ordseq);
52}
53
54unsigned blk_ordered_req_seq(struct request *rq)
55{
56 struct request_queue *q = rq->q;
57
58 BUG_ON(q->ordseq == 0);
59
60 if (rq == &q->pre_flush_rq)
61 return QUEUE_ORDSEQ_PREFLUSH;
62 if (rq == &q->bar_rq)
63 return QUEUE_ORDSEQ_BAR;
64 if (rq == &q->post_flush_rq)
65 return QUEUE_ORDSEQ_POSTFLUSH;
66
67 /*
68 * !fs requests don't need to follow barrier ordering. Always
69 * put them at the front. This fixes the following deadlock.
70 *
71 * http://thread.gmane.org/gmane.linux.kernel/537473
72 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +020073 if (rq->cmd_type != REQ_TYPE_FS)
Jens Axboe86db1e22008-01-29 14:53:40 +010074 return QUEUE_ORDSEQ_DRAIN;
75
76 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
77 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
78 return QUEUE_ORDSEQ_DRAIN;
79 else
80 return QUEUE_ORDSEQ_DONE;
81}
82
Tejun Heo8f11b3e2008-11-28 13:32:05 +090083bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010084{
85 struct request *rq;
86
87 if (error && !q->orderr)
88 q->orderr = error;
89
90 BUG_ON(q->ordseq & seq);
91 q->ordseq |= seq;
92
93 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
Tejun Heo8f11b3e2008-11-28 13:32:05 +090094 return false;
Jens Axboe86db1e22008-01-29 14:53:40 +010095
96 /*
97 * Okay, sequence complete.
98 */
99 q->ordseq = 0;
100 rq = q->orig_bar_rq;
Tejun Heo40cbbb72009-04-23 11:05:19 +0900101 __blk_end_request_all(rq, q->orderr);
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900102 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100103}
104
105static void pre_flush_end_io(struct request *rq, int error)
106{
107 elv_completed_request(rq->q, rq);
108 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
109}
110
111static void bar_end_io(struct request *rq, int error)
112{
113 elv_completed_request(rq->q, rq);
114 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
115}
116
117static void post_flush_end_io(struct request *rq, int error)
118{
119 elv_completed_request(rq->q, rq);
120 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
121}
122
123static void queue_flush(struct request_queue *q, unsigned which)
124{
125 struct request *rq;
126 rq_end_io_fn *end_io;
127
Tejun Heo313e4292008-11-28 13:32:02 +0900128 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
Jens Axboe86db1e22008-01-29 14:53:40 +0100129 rq = &q->pre_flush_rq;
130 end_io = pre_flush_end_io;
131 } else {
132 rq = &q->post_flush_rq;
133 end_io = post_flush_end_io;
134 }
135
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200136 blk_rq_init(q, rq);
FUJITA Tomonori87495342010-07-03 17:45:32 +0900137 rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100138 rq->rq_disk = q->bar_rq.rq_disk;
139 rq->end_io = end_io;
Jens Axboe86db1e22008-01-29 14:53:40 +0100140
141 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
142}
143
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900144static inline bool start_ordered(struct request_queue *q, struct request **rqp)
Jens Axboe86db1e22008-01-29 14:53:40 +0100145{
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900146 struct request *rq = *rqp;
147 unsigned skip = 0;
148
Jens Axboe86db1e22008-01-29 14:53:40 +0100149 q->orderr = 0;
150 q->ordered = q->next_ordered;
151 q->ordseq |= QUEUE_ORDSEQ_STARTED;
152
Tejun Heo58eea922008-11-28 13:32:06 +0900153 /*
154 * For an empty barrier, there's no actual BAR request, which
155 * in turn makes POSTFLUSH unnecessary. Mask them off.
156 */
Tejun Heo5b936292009-05-07 22:24:38 +0900157 if (!blk_rq_sectors(rq)) {
Tejun Heo58eea922008-11-28 13:32:06 +0900158 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
159 QUEUE_ORDERED_DO_POSTFLUSH);
Tejun Heoa185eb42008-11-28 13:32:07 +0900160 /*
161 * Empty barrier on a write-through device w/ ordered
162 * tag has no command to issue and without any command
163 * to issue, ordering by tag can't be used. Drain
164 * instead.
165 */
166 if ((q->ordered & QUEUE_ORDERED_BY_TAG) &&
167 !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) {
168 q->ordered &= ~QUEUE_ORDERED_BY_TAG;
169 q->ordered |= QUEUE_ORDERED_BY_DRAIN;
170 }
171 }
Tejun Heo58eea922008-11-28 13:32:06 +0900172
Tejun Heof6716202008-11-28 13:32:04 +0900173 /* stash away the original request */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900174 blk_dequeue_request(rq);
Jens Axboe86db1e22008-01-29 14:53:40 +0100175 q->orig_bar_rq = rq;
Tejun Heof6716202008-11-28 13:32:04 +0900176 rq = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100177
178 /*
179 * Queue ordered sequence. As we stack them at the head, we
180 * need to queue in reverse order. Note that we rely on that
181 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
Tejun Heo58eea922008-11-28 13:32:06 +0900182 * request gets inbetween ordered sequence.
Jens Axboe86db1e22008-01-29 14:53:40 +0100183 */
Tejun Heo58eea922008-11-28 13:32:06 +0900184 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
Tejun Heo313e4292008-11-28 13:32:02 +0900185 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
Tejun Heof6716202008-11-28 13:32:04 +0900186 rq = &q->post_flush_rq;
187 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900188 skip |= QUEUE_ORDSEQ_POSTFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100189
Tejun Heof6716202008-11-28 13:32:04 +0900190 if (q->ordered & QUEUE_ORDERED_DO_BAR) {
191 rq = &q->bar_rq;
192
193 /* initialize proxy request and queue it */
194 blk_rq_init(q, rq);
195 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200196 rq->cmd_flags |= REQ_WRITE;
Tejun Heof6716202008-11-28 13:32:04 +0900197 if (q->ordered & QUEUE_ORDERED_DO_FUA)
198 rq->cmd_flags |= REQ_FUA;
199 init_request_from_bio(rq, q->orig_bar_rq->bio);
200 rq->end_io = bar_end_io;
201
202 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
203 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900204 skip |= QUEUE_ORDSEQ_BAR;
Jens Axboe86db1e22008-01-29 14:53:40 +0100205
Tejun Heo313e4292008-11-28 13:32:02 +0900206 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
207 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
Jens Axboe86db1e22008-01-29 14:53:40 +0100208 rq = &q->pre_flush_rq;
209 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900210 skip |= QUEUE_ORDSEQ_PREFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100211
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200212 if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
Jens Axboe86db1e22008-01-29 14:53:40 +0100213 rq = NULL;
Tejun Heof6716202008-11-28 13:32:04 +0900214 else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900215 skip |= QUEUE_ORDSEQ_DRAIN;
Jens Axboe86db1e22008-01-29 14:53:40 +0100216
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900217 *rqp = rq;
218
219 /*
220 * Complete skipped sequences. If whole sequence is complete,
221 * return false to tell elevator that this request is gone.
222 */
223 return !blk_ordered_complete_seq(q, skip, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100224}
225
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900226bool blk_do_ordered(struct request_queue *q, struct request **rqp)
Jens Axboe86db1e22008-01-29 14:53:40 +0100227{
228 struct request *rq = *rqp;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200229 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
230 (rq->cmd_flags & REQ_HARDBARRIER);
Jens Axboe86db1e22008-01-29 14:53:40 +0100231
232 if (!q->ordseq) {
233 if (!is_barrier)
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900234 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100235
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900236 if (q->next_ordered != QUEUE_ORDERED_NONE)
237 return start_ordered(q, rqp);
238 else {
Jens Axboe86db1e22008-01-29 14:53:40 +0100239 /*
Tejun Heoa7384672008-11-28 13:32:03 +0900240 * Queue ordering not supported. Terminate
241 * with prejudice.
Jens Axboe86db1e22008-01-29 14:53:40 +0100242 */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900243 blk_dequeue_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900244 __blk_end_request_all(rq, -EOPNOTSUPP);
Jens Axboe86db1e22008-01-29 14:53:40 +0100245 *rqp = NULL;
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900246 return false;
Jens Axboe86db1e22008-01-29 14:53:40 +0100247 }
248 }
249
250 /*
251 * Ordered sequence in progress
252 */
253
254 /* Special requests are not subject to ordering rules. */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200255 if (rq->cmd_type != REQ_TYPE_FS &&
Jens Axboe86db1e22008-01-29 14:53:40 +0100256 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900257 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100258
Tejun Heo313e4292008-11-28 13:32:02 +0900259 if (q->ordered & QUEUE_ORDERED_BY_TAG) {
Jens Axboe86db1e22008-01-29 14:53:40 +0100260 /* Ordered by tag. Blocking the next barrier is enough. */
261 if (is_barrier && rq != &q->bar_rq)
262 *rqp = NULL;
263 } else {
264 /* Ordered by draining. Wait for turn. */
265 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
266 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
267 *rqp = NULL;
268 }
269
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900270 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100271}
272
273static void bio_end_empty_barrier(struct bio *bio, int err)
274{
Jens Axboecc66b452008-03-04 11:47:46 +0100275 if (err) {
276 if (err == -EOPNOTSUPP)
277 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100278 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100279 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400280 if (bio->bi_private)
281 complete(bio->bi_private);
282 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100283}
284
285/**
286 * blkdev_issue_flush - queue a flush
287 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400288 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100289 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400290 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100291 *
292 * Description:
293 * Issue a flush for the block device in question. Caller can supply
294 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400295 * wish to. If WAIT flag is not passed then caller may check only what
296 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100297 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400298int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
299 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100300{
301 DECLARE_COMPLETION_ONSTACK(wait);
302 struct request_queue *q;
303 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400304 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100305
306 if (bdev->bd_disk == NULL)
307 return -ENXIO;
308
309 q = bdev_get_queue(bdev);
310 if (!q)
311 return -ENXIO;
312
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000313 /*
314 * some block devices may not have their queue correctly set up here
315 * (e.g. loop device without a backing file) and so issuing a flush
316 * here will panic. Ensure there is a request function before issuing
317 * the barrier.
318 */
319 if (!q->make_request_fn)
320 return -ENXIO;
321
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400322 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100323 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100324 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400325 if (test_bit(BLKDEV_WAIT, &flags))
326 bio->bi_private = &wait;
327
328 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100329 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400330 if (test_bit(BLKDEV_WAIT, &flags)) {
331 wait_for_completion(&wait);
332 /*
333 * The driver must store the error location in ->bi_sector, if
334 * it supports it. For non-stacked drivers, this should be
335 * copied from blk_rq_pos(rq).
336 */
337 if (error_sector)
338 *error_sector = bio->bi_sector;
339 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100340
Jens Axboecc66b452008-03-04 11:47:46 +0100341 if (bio_flagged(bio, BIO_EOPNOTSUPP))
342 ret = -EOPNOTSUPP;
343 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100344 ret = -EIO;
345
346 bio_put(bio);
347 return ret;
348}
Jens Axboe86db1e22008-01-29 14:53:40 +0100349EXPORT_SYMBOL(blkdev_issue_flush);