]>
Commit | Line | Data |
---|---|---|
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 | 15 | unsigned 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 | ||
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 | */ | |
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 | 51 | bool 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 | ||
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 | ||
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 | ||
dd831006 TH |
113 | static inline struct request *start_ordered(struct request_queue *q, |
114 | struct request *rq) | |
86db1e29 | 115 | { |
8f11b3e9 TH |
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); | |
dd831006 | 152 | init_request_from_bio(rq, q->orig_bar_rq->bio); |
f671620e TH |
153 | if (q->ordered & QUEUE_ORDERED_DO_FUA) |
154 | rq->cmd_flags |= REQ_FUA; | |
f671620e TH |
155 | rq->end_io = bar_end_io; |
156 | ||
157 | elv_insert(q, rq, ELEVATOR_INSERT_FRONT); | |
158 | } else | |
8f11b3e9 | 159 | skip |= QUEUE_ORDSEQ_BAR; |
86db1e29 | 160 | |
313e4299 TH |
161 | if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) { |
162 | queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH); | |
86db1e29 JA |
163 | rq = &q->pre_flush_rq; |
164 | } else | |
8f11b3e9 | 165 | skip |= QUEUE_ORDSEQ_PREFLUSH; |
86db1e29 | 166 | |
6958f145 | 167 | if (queue_in_flight(q)) |
86db1e29 | 168 | rq = NULL; |
f671620e | 169 | else |
8f11b3e9 | 170 | skip |= QUEUE_ORDSEQ_DRAIN; |
86db1e29 | 171 | |
8f11b3e9 TH |
172 | /* |
173 | * Complete skipped sequences. If whole sequence is complete, | |
dd831006 | 174 | * return %NULL to tell elevator that this request is gone. |
8f11b3e9 | 175 | */ |
dd831006 TH |
176 | if (blk_ordered_complete_seq(q, skip, 0)) |
177 | rq = NULL; | |
178 | return rq; | |
86db1e29 JA |
179 | } |
180 | ||
dd831006 | 181 | struct request *blk_do_ordered(struct request_queue *q, struct request *rq) |
86db1e29 | 182 | { |
33659ebb CH |
183 | const int is_barrier = rq->cmd_type == REQ_TYPE_FS && |
184 | (rq->cmd_flags & REQ_HARDBARRIER); | |
86db1e29 JA |
185 | |
186 | if (!q->ordseq) { | |
187 | if (!is_barrier) | |
dd831006 | 188 | return rq; |
86db1e29 | 189 | |
8f11b3e9 | 190 | if (q->next_ordered != QUEUE_ORDERED_NONE) |
dd831006 | 191 | return start_ordered(q, rq); |
8f11b3e9 | 192 | else { |
86db1e29 | 193 | /* |
a7384677 TH |
194 | * Queue ordering not supported. Terminate |
195 | * with prejudice. | |
86db1e29 | 196 | */ |
9934c8c0 | 197 | blk_dequeue_request(rq); |
40cbbb78 | 198 | __blk_end_request_all(rq, -EOPNOTSUPP); |
dd831006 | 199 | return NULL; |
86db1e29 JA |
200 | } |
201 | } | |
202 | ||
203 | /* | |
204 | * Ordered sequence in progress | |
205 | */ | |
206 | ||
207 | /* Special requests are not subject to ordering rules. */ | |
33659ebb | 208 | if (rq->cmd_type != REQ_TYPE_FS && |
86db1e29 | 209 | rq != &q->pre_flush_rq && rq != &q->post_flush_rq) |
dd831006 | 210 | return rq; |
86db1e29 | 211 | |
6958f145 TH |
212 | /* Ordered by draining. Wait for turn. */ |
213 | WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q)); | |
214 | if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q)) | |
dd831006 | 215 | rq = ERR_PTR(-EAGAIN); |
86db1e29 | 216 | |
dd831006 | 217 | return rq; |
86db1e29 JA |
218 | } |
219 | ||
220 | static void bio_end_empty_barrier(struct bio *bio, int err) | |
221 | { | |
cc66b451 JA |
222 | if (err) { |
223 | if (err == -EOPNOTSUPP) | |
224 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); | |
86db1e29 | 225 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
cc66b451 | 226 | } |
f17e232e DM |
227 | if (bio->bi_private) |
228 | complete(bio->bi_private); | |
229 | bio_put(bio); | |
86db1e29 JA |
230 | } |
231 | ||
232 | /** | |
233 | * blkdev_issue_flush - queue a flush | |
234 | * @bdev: blockdev to issue flush for | |
fbd9b09a | 235 | * @gfp_mask: memory allocation flags (for bio_alloc) |
86db1e29 | 236 | * @error_sector: error sector |
fbd9b09a | 237 | * @flags: BLKDEV_IFL_* flags to control behaviour |
86db1e29 JA |
238 | * |
239 | * Description: | |
240 | * Issue a flush for the block device in question. Caller can supply | |
241 | * room for storing the error offset in case of a flush error, if they | |
f17e232e DM |
242 | * wish to. If WAIT flag is not passed then caller may check only what |
243 | * request was pushed in some internal queue for later handling. | |
86db1e29 | 244 | */ |
fbd9b09a DM |
245 | int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask, |
246 | sector_t *error_sector, unsigned long flags) | |
86db1e29 JA |
247 | { |
248 | DECLARE_COMPLETION_ONSTACK(wait); | |
249 | struct request_queue *q; | |
250 | struct bio *bio; | |
fbd9b09a | 251 | int ret = 0; |
86db1e29 JA |
252 | |
253 | if (bdev->bd_disk == NULL) | |
254 | return -ENXIO; | |
255 | ||
256 | q = bdev_get_queue(bdev); | |
257 | if (!q) | |
258 | return -ENXIO; | |
259 | ||
f10d9f61 DC |
260 | /* |
261 | * some block devices may not have their queue correctly set up here | |
262 | * (e.g. loop device without a backing file) and so issuing a flush | |
263 | * here will panic. Ensure there is a request function before issuing | |
264 | * the barrier. | |
265 | */ | |
266 | if (!q->make_request_fn) | |
267 | return -ENXIO; | |
268 | ||
fbd9b09a | 269 | bio = bio_alloc(gfp_mask, 0); |
86db1e29 | 270 | bio->bi_end_io = bio_end_empty_barrier; |
86db1e29 | 271 | bio->bi_bdev = bdev; |
f17e232e DM |
272 | if (test_bit(BLKDEV_WAIT, &flags)) |
273 | bio->bi_private = &wait; | |
86db1e29 | 274 | |
f17e232e DM |
275 | bio_get(bio); |
276 | submit_bio(WRITE_BARRIER, bio); | |
277 | if (test_bit(BLKDEV_WAIT, &flags)) { | |
278 | wait_for_completion(&wait); | |
279 | /* | |
280 | * The driver must store the error location in ->bi_sector, if | |
281 | * it supports it. For non-stacked drivers, this should be | |
282 | * copied from blk_rq_pos(rq). | |
283 | */ | |
284 | if (error_sector) | |
285 | *error_sector = bio->bi_sector; | |
286 | } | |
86db1e29 | 287 | |
cc66b451 JA |
288 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
289 | ret = -EOPNOTSUPP; | |
290 | else if (!bio_flagged(bio, BIO_UPTODATE)) | |
86db1e29 JA |
291 | ret = -EIO; |
292 | ||
293 | bio_put(bio); | |
294 | return ret; | |
295 | } | |
86db1e29 | 296 | EXPORT_SYMBOL(blkdev_issue_flush); |