perf: Use event__process_task from perf sched
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-barrier.c
blob0d710c9d403b72606d81a63676bff271858df34e
1 /*
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>
8 #include <linux/gfp.h>
10 #include "blk.h"
12 /**
13 * blk_queue_ordered - does this queue support ordered writes
14 * @q: the request queue
15 * @ordered: one of QUEUE_ORDERED_*
16 * @prepare_flush_fn: rq setup helper for cache flush ordered writes
18 * Description:
19 * For journalled file systems, doing ordered writes on a commit
20 * block instead of explicitly doing wait_on_buffer (which is bad
21 * for performance) can be a big win. Block drivers supporting this
22 * feature should call this function and indicate so.
24 **/
25 int blk_queue_ordered(struct request_queue *q, unsigned ordered,
26 prepare_flush_fn *prepare_flush_fn)
28 if (!prepare_flush_fn && (ordered & (QUEUE_ORDERED_DO_PREFLUSH |
29 QUEUE_ORDERED_DO_POSTFLUSH))) {
30 printk(KERN_ERR "%s: prepare_flush_fn required\n", __func__);
31 return -EINVAL;
34 if (ordered != QUEUE_ORDERED_NONE &&
35 ordered != QUEUE_ORDERED_DRAIN &&
36 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
37 ordered != QUEUE_ORDERED_DRAIN_FUA &&
38 ordered != QUEUE_ORDERED_TAG &&
39 ordered != QUEUE_ORDERED_TAG_FLUSH &&
40 ordered != QUEUE_ORDERED_TAG_FUA) {
41 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
42 return -EINVAL;
45 q->ordered = ordered;
46 q->next_ordered = ordered;
47 q->prepare_flush_fn = prepare_flush_fn;
49 return 0;
51 EXPORT_SYMBOL(blk_queue_ordered);
54 * Cache flushing for ordered writes handling
56 unsigned blk_ordered_cur_seq(struct request_queue *q)
58 if (!q->ordseq)
59 return 0;
60 return 1 << ffz(q->ordseq);
63 unsigned blk_ordered_req_seq(struct request *rq)
65 struct request_queue *q = rq->q;
67 BUG_ON(q->ordseq == 0);
69 if (rq == &q->pre_flush_rq)
70 return QUEUE_ORDSEQ_PREFLUSH;
71 if (rq == &q->bar_rq)
72 return QUEUE_ORDSEQ_BAR;
73 if (rq == &q->post_flush_rq)
74 return QUEUE_ORDSEQ_POSTFLUSH;
77 * !fs requests don't need to follow barrier ordering. Always
78 * put them at the front. This fixes the following deadlock.
80 * http://thread.gmane.org/gmane.linux.kernel/537473
82 if (!blk_fs_request(rq))
83 return QUEUE_ORDSEQ_DRAIN;
85 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
86 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
87 return QUEUE_ORDSEQ_DRAIN;
88 else
89 return QUEUE_ORDSEQ_DONE;
92 bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
94 struct request *rq;
96 if (error && !q->orderr)
97 q->orderr = error;
99 BUG_ON(q->ordseq & seq);
100 q->ordseq |= seq;
102 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
103 return false;
106 * Okay, sequence complete.
108 q->ordseq = 0;
109 rq = q->orig_bar_rq;
110 __blk_end_request_all(rq, q->orderr);
111 return true;
114 static void pre_flush_end_io(struct request *rq, int error)
116 elv_completed_request(rq->q, rq);
117 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
120 static void bar_end_io(struct request *rq, int error)
122 elv_completed_request(rq->q, rq);
123 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
126 static void post_flush_end_io(struct request *rq, int error)
128 elv_completed_request(rq->q, rq);
129 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
132 static void queue_flush(struct request_queue *q, unsigned which)
134 struct request *rq;
135 rq_end_io_fn *end_io;
137 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
138 rq = &q->pre_flush_rq;
139 end_io = pre_flush_end_io;
140 } else {
141 rq = &q->post_flush_rq;
142 end_io = post_flush_end_io;
145 blk_rq_init(q, rq);
146 rq->cmd_flags = REQ_HARDBARRIER;
147 rq->rq_disk = q->bar_rq.rq_disk;
148 rq->end_io = end_io;
149 q->prepare_flush_fn(q, rq);
151 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
154 static inline bool start_ordered(struct request_queue *q, struct request **rqp)
156 struct request *rq = *rqp;
157 unsigned skip = 0;
159 q->orderr = 0;
160 q->ordered = q->next_ordered;
161 q->ordseq |= QUEUE_ORDSEQ_STARTED;
164 * For an empty barrier, there's no actual BAR request, which
165 * in turn makes POSTFLUSH unnecessary. Mask them off.
167 if (!blk_rq_sectors(rq)) {
168 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
169 QUEUE_ORDERED_DO_POSTFLUSH);
171 * Empty barrier on a write-through device w/ ordered
172 * tag has no command to issue and without any command
173 * to issue, ordering by tag can't be used. Drain
174 * instead.
176 if ((q->ordered & QUEUE_ORDERED_BY_TAG) &&
177 !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) {
178 q->ordered &= ~QUEUE_ORDERED_BY_TAG;
179 q->ordered |= QUEUE_ORDERED_BY_DRAIN;
183 /* stash away the original request */
184 blk_dequeue_request(rq);
185 q->orig_bar_rq = rq;
186 rq = NULL;
189 * Queue ordered sequence. As we stack them at the head, we
190 * need to queue in reverse order. Note that we rely on that
191 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
192 * request gets inbetween ordered sequence.
194 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
195 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
196 rq = &q->post_flush_rq;
197 } else
198 skip |= QUEUE_ORDSEQ_POSTFLUSH;
200 if (q->ordered & QUEUE_ORDERED_DO_BAR) {
201 rq = &q->bar_rq;
203 /* initialize proxy request and queue it */
204 blk_rq_init(q, rq);
205 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
206 rq->cmd_flags |= REQ_RW;
207 if (q->ordered & QUEUE_ORDERED_DO_FUA)
208 rq->cmd_flags |= REQ_FUA;
209 init_request_from_bio(rq, q->orig_bar_rq->bio);
210 rq->end_io = bar_end_io;
212 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
213 } else
214 skip |= QUEUE_ORDSEQ_BAR;
216 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
217 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
218 rq = &q->pre_flush_rq;
219 } else
220 skip |= QUEUE_ORDSEQ_PREFLUSH;
222 if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
223 rq = NULL;
224 else
225 skip |= QUEUE_ORDSEQ_DRAIN;
227 *rqp = rq;
230 * Complete skipped sequences. If whole sequence is complete,
231 * return false to tell elevator that this request is gone.
233 return !blk_ordered_complete_seq(q, skip, 0);
236 bool blk_do_ordered(struct request_queue *q, struct request **rqp)
238 struct request *rq = *rqp;
239 const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
241 if (!q->ordseq) {
242 if (!is_barrier)
243 return true;
245 if (q->next_ordered != QUEUE_ORDERED_NONE)
246 return start_ordered(q, rqp);
247 else {
249 * Queue ordering not supported. Terminate
250 * with prejudice.
252 blk_dequeue_request(rq);
253 __blk_end_request_all(rq, -EOPNOTSUPP);
254 *rqp = NULL;
255 return false;
260 * Ordered sequence in progress
263 /* Special requests are not subject to ordering rules. */
264 if (!blk_fs_request(rq) &&
265 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
266 return true;
268 if (q->ordered & QUEUE_ORDERED_BY_TAG) {
269 /* Ordered by tag. Blocking the next barrier is enough. */
270 if (is_barrier && rq != &q->bar_rq)
271 *rqp = NULL;
272 } else {
273 /* Ordered by draining. Wait for turn. */
274 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
275 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
276 *rqp = NULL;
279 return true;
282 static void bio_end_empty_barrier(struct bio *bio, int err)
284 if (err) {
285 if (err == -EOPNOTSUPP)
286 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
287 clear_bit(BIO_UPTODATE, &bio->bi_flags);
289 if (bio->bi_private)
290 complete(bio->bi_private);
291 bio_put(bio);
295 * blkdev_issue_flush - queue a flush
296 * @bdev: blockdev to issue flush for
297 * @gfp_mask: memory allocation flags (for bio_alloc)
298 * @error_sector: error sector
299 * @flags: BLKDEV_IFL_* flags to control behaviour
301 * Description:
302 * Issue a flush for the block device in question. Caller can supply
303 * room for storing the error offset in case of a flush error, if they
304 * wish to. If WAIT flag is not passed then caller may check only what
305 * request was pushed in some internal queue for later handling.
307 int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
308 sector_t *error_sector, unsigned long flags)
310 DECLARE_COMPLETION_ONSTACK(wait);
311 struct request_queue *q;
312 struct bio *bio;
313 int ret = 0;
315 if (bdev->bd_disk == NULL)
316 return -ENXIO;
318 q = bdev_get_queue(bdev);
319 if (!q)
320 return -ENXIO;
322 bio = bio_alloc(gfp_mask, 0);
323 bio->bi_end_io = bio_end_empty_barrier;
324 bio->bi_bdev = bdev;
325 if (test_bit(BLKDEV_WAIT, &flags))
326 bio->bi_private = &wait;
328 bio_get(bio);
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).
337 if (error_sector)
338 *error_sector = bio->bi_sector;
341 if (bio_flagged(bio, BIO_EOPNOTSUPP))
342 ret = -EOPNOTSUPP;
343 else if (!bio_flagged(bio, BIO_UPTODATE))
344 ret = -EIO;
346 bio_put(bio);
347 return ret;
349 EXPORT_SYMBOL(blkdev_issue_flush);