2 * Functions related to barrier IO handling
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/blkdev.h>
13 * blk_queue_ordered - does this queue support ordered writes
14 * @q: the request queue
15 * @ordered: one of QUEUE_ORDERED_*
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.
24 int blk_queue_ordered(struct request_queue *q, unsigned ordered)
26 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);
38 q->next_ordered = ordered;
42 EXPORT_SYMBOL(blk_queue_ordered);
45 * Cache flushing for ordered writes handling
47 unsigned blk_ordered_cur_seq(struct request_queue *q)
51 return 1 << ffz(q->ordseq);
54 unsigned blk_ordered_req_seq(struct request *rq)
56 struct request_queue *q = rq->q;
58 BUG_ON(q->ordseq == 0);
60 if (rq == &q->pre_flush_rq)
61 return QUEUE_ORDSEQ_PREFLUSH;
63 return QUEUE_ORDSEQ_BAR;
64 if (rq == &q->post_flush_rq)
65 return QUEUE_ORDSEQ_POSTFLUSH;
68 * !fs requests don't need to follow barrier ordering. Always
69 * put them at the front. This fixes the following deadlock.
71 * http://thread.gmane.org/gmane.linux.kernel/537473
73 if (rq->cmd_type != REQ_TYPE_FS)
74 return QUEUE_ORDSEQ_DRAIN;
76 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
77 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
78 return QUEUE_ORDSEQ_DRAIN;
80 return QUEUE_ORDSEQ_DONE;
83 bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
87 if (error && !q->orderr)
90 BUG_ON(q->ordseq & seq);
93 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
97 * Okay, sequence complete.
101 __blk_end_request_all(rq, q->orderr);
105 static void pre_flush_end_io(struct request *rq, int error)
107 elv_completed_request(rq->q, rq);
108 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
111 static void bar_end_io(struct request *rq, int error)
113 elv_completed_request(rq->q, rq);
114 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
117 static void post_flush_end_io(struct request *rq, int error)
119 elv_completed_request(rq->q, rq);
120 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
123 static void queue_flush(struct request_queue *q, unsigned which)
126 rq_end_io_fn *end_io;
128 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
129 rq = &q->pre_flush_rq;
130 end_io = pre_flush_end_io;
132 rq = &q->post_flush_rq;
133 end_io = post_flush_end_io;
137 rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
138 rq->rq_disk = q->bar_rq.rq_disk;
141 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
144 static inline bool start_ordered(struct request_queue *q, struct request **rqp)
146 struct request *rq = *rqp;
150 q->ordered = q->next_ordered;
151 q->ordseq |= QUEUE_ORDSEQ_STARTED;
154 * For an empty barrier, there's no actual BAR request, which
155 * in turn makes POSTFLUSH unnecessary. Mask them off.
157 if (!blk_rq_sectors(rq)) {
158 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
159 QUEUE_ORDERED_DO_POSTFLUSH);
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
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;
173 /* stash away the original request */
174 blk_dequeue_request(rq);
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
182 * request gets inbetween ordered sequence.
184 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
185 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
186 rq = &q->post_flush_rq;
188 skip |= QUEUE_ORDSEQ_POSTFLUSH;
190 if (q->ordered & QUEUE_ORDERED_DO_BAR) {
193 /* initialize proxy request and queue it */
195 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
196 rq->cmd_flags |= REQ_WRITE;
197 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;
202 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
204 skip |= QUEUE_ORDSEQ_BAR;
206 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
207 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
208 rq = &q->pre_flush_rq;
210 skip |= QUEUE_ORDSEQ_PREFLUSH;
212 if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
215 skip |= QUEUE_ORDSEQ_DRAIN;
220 * Complete skipped sequences. If whole sequence is complete,
221 * return false to tell elevator that this request is gone.
223 return !blk_ordered_complete_seq(q, skip, 0);
226 bool blk_do_ordered(struct request_queue *q, struct request **rqp)
228 struct request *rq = *rqp;
229 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
230 (rq->cmd_flags & REQ_HARDBARRIER);
236 if (q->next_ordered != QUEUE_ORDERED_NONE)
237 return start_ordered(q, rqp);
240 * Queue ordering not supported. Terminate
243 blk_dequeue_request(rq);
244 __blk_end_request_all(rq, -EOPNOTSUPP);
251 * Ordered sequence in progress
254 /* Special requests are not subject to ordering rules. */
255 if (rq->cmd_type != REQ_TYPE_FS &&
256 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
259 if (q->ordered & QUEUE_ORDERED_BY_TAG) {
260 /* Ordered by tag. Blocking the next barrier is enough. */
261 if (is_barrier && rq != &q->bar_rq)
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))
273 static void bio_end_empty_barrier(struct bio *bio, int err)
276 if (err == -EOPNOTSUPP)
277 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
278 clear_bit(BIO_UPTODATE, &bio->bi_flags);
281 complete(bio->bi_private);
286 * blkdev_issue_flush - queue a flush
287 * @bdev: blockdev to issue flush for
288 * @gfp_mask: memory allocation flags (for bio_alloc)
289 * @error_sector: error sector
290 * @flags: BLKDEV_IFL_* flags to control behaviour
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
295 * 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.
298 int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
299 sector_t *error_sector, unsigned long flags)
301 DECLARE_COMPLETION_ONSTACK(wait);
302 struct request_queue *q;
306 if (bdev->bd_disk == NULL)
309 q = bdev_get_queue(bdev);
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
319 if (!q->make_request_fn)
322 bio = bio_alloc(gfp_mask, 0);
323 bio->bi_end_io = bio_end_empty_barrier;
325 if (test_bit(BLKDEV_WAIT, &flags))
326 bio->bi_private = &wait;
329 submit_bio(WRITE_BARRIER, bio);
330 if (test_bit(BLKDEV_WAIT, &flags)) {
331 wait_for_completion(&wait);
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).
338 *error_sector = bio->bi_sector;
341 if (bio_flagged(bio, BIO_EOPNOTSUPP))
343 else if (!bio_flagged(bio, BIO_UPTODATE))
349 EXPORT_SYMBOL(blkdev_issue_flush);