blob: c807e9ca3a68ad2a6782572300ee3cc044610537 [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 &&
Tejun Heo6958f142010-09-03 11:56:16 +020029 ordered != QUEUE_ORDERED_DRAIN_FUA) {
Jens Axboe86db1e22008-01-29 14:53:40 +010030 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
31 return -EINVAL;
32 }
33
34 q->ordered = ordered;
35 q->next_ordered = ordered;
Jens Axboe86db1e22008-01-29 14:53:40 +010036
37 return 0;
38}
Jens Axboe86db1e22008-01-29 14:53:40 +010039EXPORT_SYMBOL(blk_queue_ordered);
40
41/*
42 * Cache flushing for ordered writes handling
43 */
Adrian Bunk6f6a0362008-04-29 09:49:06 +020044unsigned blk_ordered_cur_seq(struct request_queue *q)
Jens Axboe86db1e22008-01-29 14:53:40 +010045{
46 if (!q->ordseq)
47 return 0;
48 return 1 << ffz(q->ordseq);
49}
50
51unsigned blk_ordered_req_seq(struct request *rq)
52{
53 struct request_queue *q = rq->q;
54
55 BUG_ON(q->ordseq == 0);
56
57 if (rq == &q->pre_flush_rq)
58 return QUEUE_ORDSEQ_PREFLUSH;
59 if (rq == &q->bar_rq)
60 return QUEUE_ORDSEQ_BAR;
61 if (rq == &q->post_flush_rq)
62 return QUEUE_ORDSEQ_POSTFLUSH;
63
64 /*
65 * !fs requests don't need to follow barrier ordering. Always
66 * put them at the front. This fixes the following deadlock.
67 *
68 * http://thread.gmane.org/gmane.linux.kernel/537473
69 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +020070 if (rq->cmd_type != REQ_TYPE_FS)
Jens Axboe86db1e22008-01-29 14:53:40 +010071 return QUEUE_ORDSEQ_DRAIN;
72
73 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
74 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
75 return QUEUE_ORDSEQ_DRAIN;
76 else
77 return QUEUE_ORDSEQ_DONE;
78}
79
Tejun Heo8f11b3e2008-11-28 13:32:05 +090080bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
Jens Axboe86db1e22008-01-29 14:53:40 +010081{
82 struct request *rq;
83
84 if (error && !q->orderr)
85 q->orderr = error;
86
87 BUG_ON(q->ordseq & seq);
88 q->ordseq |= seq;
89
90 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
Tejun Heo8f11b3e2008-11-28 13:32:05 +090091 return false;
Jens Axboe86db1e22008-01-29 14:53:40 +010092
93 /*
94 * Okay, sequence complete.
95 */
96 q->ordseq = 0;
97 rq = q->orig_bar_rq;
Tejun Heo40cbbb72009-04-23 11:05:19 +090098 __blk_end_request_all(rq, q->orderr);
Tejun Heo8f11b3e2008-11-28 13:32:05 +090099 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100100}
101
102static void pre_flush_end_io(struct request *rq, int error)
103{
104 elv_completed_request(rq->q, rq);
105 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
106}
107
108static void bar_end_io(struct request *rq, int error)
109{
110 elv_completed_request(rq->q, rq);
111 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
112}
113
114static void post_flush_end_io(struct request *rq, int error)
115{
116 elv_completed_request(rq->q, rq);
117 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
118}
119
120static void queue_flush(struct request_queue *q, unsigned which)
121{
122 struct request *rq;
123 rq_end_io_fn *end_io;
124
Tejun Heo313e4292008-11-28 13:32:02 +0900125 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
Jens Axboe86db1e22008-01-29 14:53:40 +0100126 rq = &q->pre_flush_rq;
127 end_io = pre_flush_end_io;
128 } else {
129 rq = &q->post_flush_rq;
130 end_io = post_flush_end_io;
131 }
132
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200133 blk_rq_init(q, rq);
FUJITA Tomonori28e18d02010-07-09 09:38:24 +0900134 rq->cmd_type = REQ_TYPE_FS;
FUJITA Tomonori87495342010-07-03 17:45:32 +0900135 rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
FUJITA Tomonori16f23192010-07-09 09:38:25 +0900136 rq->rq_disk = q->orig_bar_rq->rq_disk;
Jens Axboe86db1e22008-01-29 14:53:40 +0100137 rq->end_io = end_io;
Jens Axboe86db1e22008-01-29 14:53:40 +0100138
139 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
140}
141
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900142static inline bool start_ordered(struct request_queue *q, struct request **rqp)
Jens Axboe86db1e22008-01-29 14:53:40 +0100143{
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900144 struct request *rq = *rqp;
145 unsigned skip = 0;
146
Jens Axboe86db1e22008-01-29 14:53:40 +0100147 q->orderr = 0;
148 q->ordered = q->next_ordered;
149 q->ordseq |= QUEUE_ORDSEQ_STARTED;
150
Tejun Heo58eea922008-11-28 13:32:06 +0900151 /*
152 * For an empty barrier, there's no actual BAR request, which
153 * in turn makes POSTFLUSH unnecessary. Mask them off.
154 */
Tejun Heo6958f142010-09-03 11:56:16 +0200155 if (!blk_rq_sectors(rq))
Tejun Heo58eea922008-11-28 13:32:06 +0900156 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
157 QUEUE_ORDERED_DO_POSTFLUSH);
158
Tejun Heof6716202008-11-28 13:32:04 +0900159 /* stash away the original request */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900160 blk_dequeue_request(rq);
Jens Axboe86db1e22008-01-29 14:53:40 +0100161 q->orig_bar_rq = rq;
Tejun Heof6716202008-11-28 13:32:04 +0900162 rq = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100163
164 /*
165 * Queue ordered sequence. As we stack them at the head, we
166 * need to queue in reverse order. Note that we rely on that
167 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
Tejun Heo58eea922008-11-28 13:32:06 +0900168 * request gets inbetween ordered sequence.
Jens Axboe86db1e22008-01-29 14:53:40 +0100169 */
Tejun Heo58eea922008-11-28 13:32:06 +0900170 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
Tejun Heo313e4292008-11-28 13:32:02 +0900171 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
Tejun Heof6716202008-11-28 13:32:04 +0900172 rq = &q->post_flush_rq;
173 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900174 skip |= QUEUE_ORDSEQ_POSTFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100175
Tejun Heof6716202008-11-28 13:32:04 +0900176 if (q->ordered & QUEUE_ORDERED_DO_BAR) {
177 rq = &q->bar_rq;
178
179 /* initialize proxy request and queue it */
180 blk_rq_init(q, rq);
181 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200182 rq->cmd_flags |= REQ_WRITE;
Tejun Heof6716202008-11-28 13:32:04 +0900183 if (q->ordered & QUEUE_ORDERED_DO_FUA)
184 rq->cmd_flags |= REQ_FUA;
185 init_request_from_bio(rq, q->orig_bar_rq->bio);
186 rq->end_io = bar_end_io;
187
188 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
189 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900190 skip |= QUEUE_ORDSEQ_BAR;
Jens Axboe86db1e22008-01-29 14:53:40 +0100191
Tejun Heo313e4292008-11-28 13:32:02 +0900192 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
193 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
Jens Axboe86db1e22008-01-29 14:53:40 +0100194 rq = &q->pre_flush_rq;
195 } else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900196 skip |= QUEUE_ORDSEQ_PREFLUSH;
Jens Axboe86db1e22008-01-29 14:53:40 +0100197
Tejun Heo6958f142010-09-03 11:56:16 +0200198 if (queue_in_flight(q))
Jens Axboe86db1e22008-01-29 14:53:40 +0100199 rq = NULL;
Tejun Heof6716202008-11-28 13:32:04 +0900200 else
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900201 skip |= QUEUE_ORDSEQ_DRAIN;
Jens Axboe86db1e22008-01-29 14:53:40 +0100202
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900203 *rqp = rq;
204
205 /*
206 * Complete skipped sequences. If whole sequence is complete,
207 * return false to tell elevator that this request is gone.
208 */
209 return !blk_ordered_complete_seq(q, skip, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100210}
211
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900212bool blk_do_ordered(struct request_queue *q, struct request **rqp)
Jens Axboe86db1e22008-01-29 14:53:40 +0100213{
214 struct request *rq = *rqp;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200215 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
216 (rq->cmd_flags & REQ_HARDBARRIER);
Jens Axboe86db1e22008-01-29 14:53:40 +0100217
218 if (!q->ordseq) {
219 if (!is_barrier)
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900220 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100221
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900222 if (q->next_ordered != QUEUE_ORDERED_NONE)
223 return start_ordered(q, rqp);
224 else {
Jens Axboe86db1e22008-01-29 14:53:40 +0100225 /*
Tejun Heoa7384672008-11-28 13:32:03 +0900226 * Queue ordering not supported. Terminate
227 * with prejudice.
Jens Axboe86db1e22008-01-29 14:53:40 +0100228 */
Tejun Heo9934c8c2009-05-08 11:54:16 +0900229 blk_dequeue_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900230 __blk_end_request_all(rq, -EOPNOTSUPP);
Jens Axboe86db1e22008-01-29 14:53:40 +0100231 *rqp = NULL;
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900232 return false;
Jens Axboe86db1e22008-01-29 14:53:40 +0100233 }
234 }
235
236 /*
237 * Ordered sequence in progress
238 */
239
240 /* Special requests are not subject to ordering rules. */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200241 if (rq->cmd_type != REQ_TYPE_FS &&
Jens Axboe86db1e22008-01-29 14:53:40 +0100242 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900243 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100244
Tejun Heo6958f142010-09-03 11:56:16 +0200245 /* Ordered by draining. Wait for turn. */
246 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
247 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
248 *rqp = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100249
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900250 return true;
Jens Axboe86db1e22008-01-29 14:53:40 +0100251}
252
253static void bio_end_empty_barrier(struct bio *bio, int err)
254{
Jens Axboecc66b452008-03-04 11:47:46 +0100255 if (err) {
256 if (err == -EOPNOTSUPP)
257 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
Jens Axboe86db1e22008-01-29 14:53:40 +0100258 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jens Axboecc66b452008-03-04 11:47:46 +0100259 }
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400260 if (bio->bi_private)
261 complete(bio->bi_private);
262 bio_put(bio);
Jens Axboe86db1e22008-01-29 14:53:40 +0100263}
264
265/**
266 * blkdev_issue_flush - queue a flush
267 * @bdev: blockdev to issue flush for
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400268 * @gfp_mask: memory allocation flags (for bio_alloc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100269 * @error_sector: error sector
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400270 * @flags: BLKDEV_IFL_* flags to control behaviour
Jens Axboe86db1e22008-01-29 14:53:40 +0100271 *
272 * Description:
273 * Issue a flush for the block device in question. Caller can supply
274 * room for storing the error offset in case of a flush error, if they
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400275 * wish to. If WAIT flag is not passed then caller may check only what
276 * request was pushed in some internal queue for later handling.
Jens Axboe86db1e22008-01-29 14:53:40 +0100277 */
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400278int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
279 sector_t *error_sector, unsigned long flags)
Jens Axboe86db1e22008-01-29 14:53:40 +0100280{
281 DECLARE_COMPLETION_ONSTACK(wait);
282 struct request_queue *q;
283 struct bio *bio;
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400284 int ret = 0;
Jens Axboe86db1e22008-01-29 14:53:40 +0100285
286 if (bdev->bd_disk == NULL)
287 return -ENXIO;
288
289 q = bdev_get_queue(bdev);
290 if (!q)
291 return -ENXIO;
292
Dave Chinnerf10d9f62010-07-13 17:50:50 +1000293 /*
294 * some block devices may not have their queue correctly set up here
295 * (e.g. loop device without a backing file) and so issuing a flush
296 * here will panic. Ensure there is a request function before issuing
297 * the barrier.
298 */
299 if (!q->make_request_fn)
300 return -ENXIO;
301
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400302 bio = bio_alloc(gfp_mask, 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100303 bio->bi_end_io = bio_end_empty_barrier;
Jens Axboe86db1e22008-01-29 14:53:40 +0100304 bio->bi_bdev = bdev;
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400305 if (test_bit(BLKDEV_WAIT, &flags))
306 bio->bi_private = &wait;
307
308 bio_get(bio);
OGAWA Hirofumi2ebca852008-08-11 17:07:08 +0100309 submit_bio(WRITE_BARRIER, bio);
Dmitry Monakhovf17e2322010-04-28 17:55:07 +0400310 if (test_bit(BLKDEV_WAIT, &flags)) {
311 wait_for_completion(&wait);
312 /*
313 * The driver must store the error location in ->bi_sector, if
314 * it supports it. For non-stacked drivers, this should be
315 * copied from blk_rq_pos(rq).
316 */
317 if (error_sector)
318 *error_sector = bio->bi_sector;
319 }
Jens Axboe86db1e22008-01-29 14:53:40 +0100320
Jens Axboecc66b452008-03-04 11:47:46 +0100321 if (bio_flagged(bio, BIO_EOPNOTSUPP))
322 ret = -EOPNOTSUPP;
323 else if (!bio_flagged(bio, BIO_UPTODATE))
Jens Axboe86db1e22008-01-29 14:53:40 +0100324 ret = -EIO;
325
326 bio_put(bio);
327 return ret;
328}
Jens Axboe86db1e22008-01-29 14:53:40 +0100329EXPORT_SYMBOL(blkdev_issue_flush);