block: remove spurious uses of REQ_HARDBARRIER
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / block / blk-barrier.c
CommitLineData
86db1e29
JA
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>
5a0e3ad6 8#include <linux/gfp.h>
86db1e29
JA
9
10#include "blk.h"
11
86db1e29
JA
12/*
13 * Cache flushing for ordered writes handling
14 */
6f6a036e 15unsigned blk_ordered_cur_seq(struct request_queue *q)
86db1e29
JA
16{
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 */
33659ebb 41 if (rq->cmd_type != REQ_TYPE_FS)
86db1e29
JA
42 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
8f11b3e9 51bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
86db1e29
JA
52{
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)
8f11b3e9 62 return false;
86db1e29
JA
63
64 /*
65 * Okay, sequence complete.
66 */
67 q->ordseq = 0;
68 rq = q->orig_bar_rq;
40cbbb78 69 __blk_end_request_all(rq, q->orderr);
8f11b3e9 70 return true;
86db1e29
JA
71}
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
313e4299 96 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
86db1e29
JA
97 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
2a4aa30c 104 blk_rq_init(q, rq);
28e18d01 105 rq->cmd_type = REQ_TYPE_FS;
8749534f 106 rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
16f2319f 107 rq->rq_disk = q->orig_bar_rq->rq_disk;
86db1e29 108 rq->end_io = end_io;
86db1e29
JA
109
110 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
111}
112
8f11b3e9 113static inline bool start_ordered(struct request_queue *q, struct request **rqp)
86db1e29 114{
8f11b3e9
TH
115 struct request *rq = *rqp;
116 unsigned skip = 0;
117
86db1e29
JA
118 q->orderr = 0;
119 q->ordered = q->next_ordered;
120 q->ordseq |= QUEUE_ORDSEQ_STARTED;
121
58eea927
TH
122 /*
123 * For an empty barrier, there's no actual BAR request, which
124 * in turn makes POSTFLUSH unnecessary. Mask them off.
125 */
6958f145 126 if (!blk_rq_sectors(rq))
58eea927
TH
127 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
128 QUEUE_ORDERED_DO_POSTFLUSH);
129
f671620e 130 /* stash away the original request */
9934c8c0 131 blk_dequeue_request(rq);
86db1e29 132 q->orig_bar_rq = rq;
f671620e 133 rq = NULL;
86db1e29
JA
134
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
58eea927 139 * request gets inbetween ordered sequence.
86db1e29 140 */
58eea927 141 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
313e4299 142 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
f671620e
TH
143 rq = &q->post_flush_rq;
144 } else
8f11b3e9 145 skip |= QUEUE_ORDSEQ_POSTFLUSH;
86db1e29 146
f671620e
TH
147 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);
152 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
7b6d91da 153 rq->cmd_flags |= REQ_WRITE;
f671620e
TH
154 if (q->ordered & QUEUE_ORDERED_DO_FUA)
155 rq->cmd_flags |= REQ_FUA;
156 init_request_from_bio(rq, q->orig_bar_rq->bio);
157 rq->end_io = bar_end_io;
158
159 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
160 } else
8f11b3e9 161 skip |= QUEUE_ORDSEQ_BAR;
86db1e29 162
313e4299
TH
163 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
164 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
86db1e29
JA
165 rq = &q->pre_flush_rq;
166 } else
8f11b3e9 167 skip |= QUEUE_ORDSEQ_PREFLUSH;
86db1e29 168
6958f145 169 if (queue_in_flight(q))
86db1e29 170 rq = NULL;
f671620e 171 else
8f11b3e9 172 skip |= QUEUE_ORDSEQ_DRAIN;
86db1e29 173
8f11b3e9
TH
174 *rqp = rq;
175
176 /*
177 * Complete skipped sequences. If whole sequence is complete,
178 * return false to tell elevator that this request is gone.
179 */
180 return !blk_ordered_complete_seq(q, skip, 0);
86db1e29
JA
181}
182
8f11b3e9 183bool blk_do_ordered(struct request_queue *q, struct request **rqp)
86db1e29
JA
184{
185 struct request *rq = *rqp;
33659ebb
CH
186 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
187 (rq->cmd_flags & REQ_HARDBARRIER);
86db1e29
JA
188
189 if (!q->ordseq) {
190 if (!is_barrier)
8f11b3e9 191 return true;
86db1e29 192
8f11b3e9
TH
193 if (q->next_ordered != QUEUE_ORDERED_NONE)
194 return start_ordered(q, rqp);
195 else {
86db1e29 196 /*
a7384677
TH
197 * Queue ordering not supported. Terminate
198 * with prejudice.
86db1e29 199 */
9934c8c0 200 blk_dequeue_request(rq);
40cbbb78 201 __blk_end_request_all(rq, -EOPNOTSUPP);
86db1e29 202 *rqp = NULL;
8f11b3e9 203 return false;
86db1e29
JA
204 }
205 }
206
207 /*
208 * Ordered sequence in progress
209 */
210
211 /* Special requests are not subject to ordering rules. */
33659ebb 212 if (rq->cmd_type != REQ_TYPE_FS &&
86db1e29 213 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
8f11b3e9 214 return true;
86db1e29 215
6958f145
TH
216 /* Ordered by draining. Wait for turn. */
217 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
218 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
219 *rqp = NULL;
86db1e29 220
8f11b3e9 221 return true;
86db1e29
JA
222}
223
224static void bio_end_empty_barrier(struct bio *bio, int err)
225{
cc66b451
JA
226 if (err) {
227 if (err == -EOPNOTSUPP)
228 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
86db1e29 229 clear_bit(BIO_UPTODATE, &bio->bi_flags);
cc66b451 230 }
f17e232e
DM
231 if (bio->bi_private)
232 complete(bio->bi_private);
233 bio_put(bio);
86db1e29
JA
234}
235
236/**
237 * blkdev_issue_flush - queue a flush
238 * @bdev: blockdev to issue flush for
fbd9b09a 239 * @gfp_mask: memory allocation flags (for bio_alloc)
86db1e29 240 * @error_sector: error sector
fbd9b09a 241 * @flags: BLKDEV_IFL_* flags to control behaviour
86db1e29
JA
242 *
243 * Description:
244 * Issue a flush for the block device in question. Caller can supply
245 * room for storing the error offset in case of a flush error, if they
f17e232e
DM
246 * wish to. If WAIT flag is not passed then caller may check only what
247 * request was pushed in some internal queue for later handling.
86db1e29 248 */
fbd9b09a
DM
249int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
250 sector_t *error_sector, unsigned long flags)
86db1e29
JA
251{
252 DECLARE_COMPLETION_ONSTACK(wait);
253 struct request_queue *q;
254 struct bio *bio;
fbd9b09a 255 int ret = 0;
86db1e29
JA
256
257 if (bdev->bd_disk == NULL)
258 return -ENXIO;
259
260 q = bdev_get_queue(bdev);
261 if (!q)
262 return -ENXIO;
263
f10d9f61
DC
264 /*
265 * some block devices may not have their queue correctly set up here
266 * (e.g. loop device without a backing file) and so issuing a flush
267 * here will panic. Ensure there is a request function before issuing
268 * the barrier.
269 */
270 if (!q->make_request_fn)
271 return -ENXIO;
272
fbd9b09a 273 bio = bio_alloc(gfp_mask, 0);
86db1e29 274 bio->bi_end_io = bio_end_empty_barrier;
86db1e29 275 bio->bi_bdev = bdev;
f17e232e
DM
276 if (test_bit(BLKDEV_WAIT, &flags))
277 bio->bi_private = &wait;
86db1e29 278
f17e232e
DM
279 bio_get(bio);
280 submit_bio(WRITE_BARRIER, bio);
281 if (test_bit(BLKDEV_WAIT, &flags)) {
282 wait_for_completion(&wait);
283 /*
284 * The driver must store the error location in ->bi_sector, if
285 * it supports it. For non-stacked drivers, this should be
286 * copied from blk_rq_pos(rq).
287 */
288 if (error_sector)
289 *error_sector = bio->bi_sector;
290 }
86db1e29 291
cc66b451
JA
292 if (bio_flagged(bio, BIO_EOPNOTSUPP))
293 ret = -EOPNOTSUPP;
294 else if (!bio_flagged(bio, BIO_UPTODATE))
86db1e29
JA
295 ret = -EIO;
296
297 bio_put(bio);
298 return ret;
299}
86db1e29 300EXPORT_SYMBOL(blkdev_issue_flush);