block: remove spurious uses of REQ_HARDBARRIER
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / block / blk-barrier.c
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>
9
10 #include "blk.h"
11
12 /*
13 * Cache flushing for ordered writes handling
14 */
15 unsigned blk_ordered_cur_seq(struct request_queue *q)
16 {
17 if (!q->ordseq)
18 return 0;
19 return 1 << ffz(q->ordseq);
20 }
21
22 unsigned 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 */
41 if (rq->cmd_type != REQ_TYPE_FS)
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
51 bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
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)
62 return false;
63
64 /*
65 * Okay, sequence complete.
66 */
67 q->ordseq = 0;
68 rq = q->orig_bar_rq;
69 __blk_end_request_all(rq, q->orderr);
70 return true;
71 }
72
73 static 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
79 static 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
85 static 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
91 static void queue_flush(struct request_queue *q, unsigned which)
92 {
93 struct request *rq;
94 rq_end_io_fn *end_io;
95
96 if (which == QUEUE_ORDERED_DO_PREFLUSH) {
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
104 blk_rq_init(q, rq);
105 rq->cmd_type = REQ_TYPE_FS;
106 rq->cmd_flags = REQ_HARDBARRIER | REQ_FLUSH;
107 rq->rq_disk = q->orig_bar_rq->rq_disk;
108 rq->end_io = end_io;
109
110 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
111 }
112
113 static inline bool start_ordered(struct request_queue *q, struct request **rqp)
114 {
115 struct request *rq = *rqp;
116 unsigned skip = 0;
117
118 q->orderr = 0;
119 q->ordered = q->next_ordered;
120 q->ordseq |= QUEUE_ORDSEQ_STARTED;
121
122 /*
123 * For an empty barrier, there's no actual BAR request, which
124 * in turn makes POSTFLUSH unnecessary. Mask them off.
125 */
126 if (!blk_rq_sectors(rq))
127 q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
128 QUEUE_ORDERED_DO_POSTFLUSH);
129
130 /* stash away the original request */
131 blk_dequeue_request(rq);
132 q->orig_bar_rq = rq;
133 rq = NULL;
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
139 * request gets inbetween ordered sequence.
140 */
141 if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
142 queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
143 rq = &q->post_flush_rq;
144 } else
145 skip |= QUEUE_ORDSEQ_POSTFLUSH;
146
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)
153 rq->cmd_flags |= REQ_WRITE;
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
161 skip |= QUEUE_ORDSEQ_BAR;
162
163 if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
164 queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
165 rq = &q->pre_flush_rq;
166 } else
167 skip |= QUEUE_ORDSEQ_PREFLUSH;
168
169 if (queue_in_flight(q))
170 rq = NULL;
171 else
172 skip |= QUEUE_ORDSEQ_DRAIN;
173
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);
181 }
182
183 bool blk_do_ordered(struct request_queue *q, struct request **rqp)
184 {
185 struct request *rq = *rqp;
186 const int is_barrier = rq->cmd_type == REQ_TYPE_FS &&
187 (rq->cmd_flags & REQ_HARDBARRIER);
188
189 if (!q->ordseq) {
190 if (!is_barrier)
191 return true;
192
193 if (q->next_ordered != QUEUE_ORDERED_NONE)
194 return start_ordered(q, rqp);
195 else {
196 /*
197 * Queue ordering not supported. Terminate
198 * with prejudice.
199 */
200 blk_dequeue_request(rq);
201 __blk_end_request_all(rq, -EOPNOTSUPP);
202 *rqp = NULL;
203 return false;
204 }
205 }
206
207 /*
208 * Ordered sequence in progress
209 */
210
211 /* Special requests are not subject to ordering rules. */
212 if (rq->cmd_type != REQ_TYPE_FS &&
213 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
214 return true;
215
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;
220
221 return true;
222 }
223
224 static void bio_end_empty_barrier(struct bio *bio, int err)
225 {
226 if (err) {
227 if (err == -EOPNOTSUPP)
228 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
229 clear_bit(BIO_UPTODATE, &bio->bi_flags);
230 }
231 if (bio->bi_private)
232 complete(bio->bi_private);
233 bio_put(bio);
234 }
235
236 /**
237 * blkdev_issue_flush - queue a flush
238 * @bdev: blockdev to issue flush for
239 * @gfp_mask: memory allocation flags (for bio_alloc)
240 * @error_sector: error sector
241 * @flags: BLKDEV_IFL_* flags to control behaviour
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
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.
248 */
249 int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
250 sector_t *error_sector, unsigned long flags)
251 {
252 DECLARE_COMPLETION_ONSTACK(wait);
253 struct request_queue *q;
254 struct bio *bio;
255 int ret = 0;
256
257 if (bdev->bd_disk == NULL)
258 return -ENXIO;
259
260 q = bdev_get_queue(bdev);
261 if (!q)
262 return -ENXIO;
263
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
273 bio = bio_alloc(gfp_mask, 0);
274 bio->bi_end_io = bio_end_empty_barrier;
275 bio->bi_bdev = bdev;
276 if (test_bit(BLKDEV_WAIT, &flags))
277 bio->bi_private = &wait;
278
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 }
291
292 if (bio_flagged(bio, BIO_EOPNOTSUPP))
293 ret = -EOPNOTSUPP;
294 else if (!bio_flagged(bio, BIO_UPTODATE))
295 ret = -EIO;
296
297 bio_put(bio);
298 return ret;
299 }
300 EXPORT_SYMBOL(blkdev_issue_flush);