1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#include <linux/kernel.h>
68#include <linux/module.h>
69#include <linux/bio.h>
70#include <linux/blkdev.h>
71#include <linux/gfp.h>
72#include <linux/blk-mq.h>
73
74#include "blk.h"
75#include "blk-mq.h"
76
77
78enum {
79 REQ_FSEQ_PREFLUSH = (1 << 0),
80 REQ_FSEQ_DATA = (1 << 1),
81 REQ_FSEQ_POSTFLUSH = (1 << 2),
82 REQ_FSEQ_DONE = (1 << 3),
83
84 REQ_FSEQ_ACTIONS = REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
85 REQ_FSEQ_POSTFLUSH,
86
87
88
89
90
91 FLUSH_PENDING_TIMEOUT = 5 * HZ,
92};
93
94static bool blk_kick_flush(struct request_queue *q);
95
96static unsigned int blk_flush_policy(unsigned int fflags, struct request *rq)
97{
98 unsigned int policy = 0;
99
100 if (blk_rq_sectors(rq))
101 policy |= REQ_FSEQ_DATA;
102
103 if (fflags & REQ_FLUSH) {
104 if (rq->cmd_flags & REQ_FLUSH)
105 policy |= REQ_FSEQ_PREFLUSH;
106 if (!(fflags & REQ_FUA) && (rq->cmd_flags & REQ_FUA))
107 policy |= REQ_FSEQ_POSTFLUSH;
108 }
109 return policy;
110}
111
112static unsigned int blk_flush_cur_seq(struct request *rq)
113{
114 return 1 << ffz(rq->flush.seq);
115}
116
117static void blk_flush_restore_request(struct request *rq)
118{
119
120
121
122
123
124 rq->bio = rq->biotail;
125
126
127 rq->cmd_flags &= ~REQ_FLUSH_SEQ;
128 rq->end_io = rq->flush.saved_end_io;
129
130 blk_clear_rq_complete(rq);
131}
132
133static void mq_flush_run(struct work_struct *work)
134{
135 struct request *rq;
136
137 rq = container_of(work, struct request, mq_flush_work);
138
139 memset(&rq->csd, 0, sizeof(rq->csd));
140 blk_mq_insert_request(rq, false, true, false);
141}
142
143static bool blk_flush_queue_rq(struct request *rq, bool add_front)
144{
145 if (rq->q->mq_ops) {
146 INIT_WORK(&rq->mq_flush_work, mq_flush_run);
147 kblockd_schedule_work(rq->q, &rq->mq_flush_work);
148 return false;
149 } else {
150 if (add_front)
151 list_add(&rq->queuelist, &rq->q->queue_head);
152 else
153 list_add_tail(&rq->queuelist, &rq->q->queue_head);
154 return true;
155 }
156}
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173static bool blk_flush_complete_seq(struct request *rq, unsigned int seq,
174 int error)
175{
176 struct request_queue *q = rq->q;
177 struct list_head *pending = &q->flush_queue[q->flush_pending_idx];
178 bool queued = false, kicked;
179
180 BUG_ON(rq->flush.seq & seq);
181 rq->flush.seq |= seq;
182
183 if (likely(!error))
184 seq = blk_flush_cur_seq(rq);
185 else
186 seq = REQ_FSEQ_DONE;
187
188 switch (seq) {
189 case REQ_FSEQ_PREFLUSH:
190 case REQ_FSEQ_POSTFLUSH:
191
192 if (list_empty(pending))
193 q->flush_pending_since = jiffies;
194 list_move_tail(&rq->flush.list, pending);
195 break;
196
197 case REQ_FSEQ_DATA:
198 list_move_tail(&rq->flush.list, &q->flush_data_in_flight);
199 queued = blk_flush_queue_rq(rq, true);
200 break;
201
202 case REQ_FSEQ_DONE:
203
204
205
206
207
208
209 BUG_ON(!list_empty(&rq->queuelist));
210 list_del_init(&rq->flush.list);
211 blk_flush_restore_request(rq);
212 if (q->mq_ops)
213 blk_mq_end_io(rq, error);
214 else
215 __blk_end_request_all(rq, error);
216 break;
217
218 default:
219 BUG();
220 }
221
222 kicked = blk_kick_flush(q);
223 return kicked | queued;
224}
225
226static void flush_end_io(struct request *flush_rq, int error)
227{
228 struct request_queue *q = flush_rq->q;
229 struct list_head *running;
230 bool queued = false;
231 struct request *rq, *n;
232 unsigned long flags = 0;
233
234 if (q->mq_ops)
235 spin_lock_irqsave(&q->mq_flush_lock, flags);
236
237 running = &q->flush_queue[q->flush_running_idx];
238 BUG_ON(q->flush_pending_idx == q->flush_running_idx);
239
240
241 q->flush_running_idx ^= 1;
242
243 if (!q->mq_ops)
244 elv_completed_request(q, flush_rq);
245
246
247 list_for_each_entry_safe(rq, n, running, flush.list) {
248 unsigned int seq = blk_flush_cur_seq(rq);
249
250 BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
251 queued |= blk_flush_complete_seq(rq, seq, error);
252 }
253
254
255
256
257
258
259
260
261
262
263
264
265 if (queued || q->flush_queue_delayed) {
266 WARN_ON(q->mq_ops);
267 blk_run_queue_async(q);
268 }
269 q->flush_queue_delayed = 0;
270 if (q->mq_ops)
271 spin_unlock_irqrestore(&q->mq_flush_lock, flags);
272}
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287static bool blk_kick_flush(struct request_queue *q)
288{
289 struct list_head *pending = &q->flush_queue[q->flush_pending_idx];
290 struct request *first_rq =
291 list_first_entry(pending, struct request, flush.list);
292
293
294 if (q->flush_pending_idx != q->flush_running_idx || list_empty(pending))
295 return false;
296
297
298 if (!list_empty(&q->flush_data_in_flight) &&
299 time_before(jiffies,
300 q->flush_pending_since + FLUSH_PENDING_TIMEOUT))
301 return false;
302
303
304
305
306
307 q->flush_pending_idx ^= 1;
308
309 if (q->mq_ops) {
310 struct blk_mq_ctx *ctx = first_rq->mq_ctx;
311 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
312
313 blk_mq_rq_init(hctx, q->flush_rq);
314 q->flush_rq->mq_ctx = ctx;
315
316
317
318
319
320
321
322 q->flush_rq->tag = first_rq->tag;
323 } else {
324 blk_rq_init(q, q->flush_rq);
325 }
326
327 q->flush_rq->cmd_type = REQ_TYPE_FS;
328 q->flush_rq->cmd_flags = WRITE_FLUSH | REQ_FLUSH_SEQ;
329 q->flush_rq->rq_disk = first_rq->rq_disk;
330 q->flush_rq->end_io = flush_end_io;
331
332 return blk_flush_queue_rq(q->flush_rq, false);
333}
334
335static void flush_data_end_io(struct request *rq, int error)
336{
337 struct request_queue *q = rq->q;
338
339
340
341
342
343 if (blk_flush_complete_seq(rq, REQ_FSEQ_DATA, error))
344 blk_run_queue_async(q);
345}
346
347static void mq_flush_data_end_io(struct request *rq, int error)
348{
349 struct request_queue *q = rq->q;
350 struct blk_mq_hw_ctx *hctx;
351 struct blk_mq_ctx *ctx;
352 unsigned long flags;
353
354 ctx = rq->mq_ctx;
355 hctx = q->mq_ops->map_queue(q, ctx->cpu);
356
357
358
359
360
361 spin_lock_irqsave(&q->mq_flush_lock, flags);
362 if (blk_flush_complete_seq(rq, REQ_FSEQ_DATA, error))
363 blk_mq_run_hw_queue(hctx, true);
364 spin_unlock_irqrestore(&q->mq_flush_lock, flags);
365}
366
367
368
369
370
371
372
373
374
375
376
377
378
379void blk_insert_flush(struct request *rq)
380{
381 struct request_queue *q = rq->q;
382 unsigned int fflags = q->flush_flags;
383 unsigned int policy = blk_flush_policy(fflags, rq);
384
385
386
387
388
389 rq->cmd_flags &= ~REQ_FLUSH;
390 if (!(fflags & REQ_FUA))
391 rq->cmd_flags &= ~REQ_FUA;
392
393
394
395
396
397
398
399 if (!policy) {
400 if (q->mq_ops)
401 blk_mq_end_io(rq, 0);
402 else
403 __blk_end_bidi_request(rq, 0, 0, 0);
404 return;
405 }
406
407 BUG_ON(rq->bio != rq->biotail);
408
409
410
411
412
413
414 if ((policy & REQ_FSEQ_DATA) &&
415 !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
416 if (q->mq_ops) {
417 blk_mq_insert_request(rq, false, false, true);
418 } else
419 list_add_tail(&rq->queuelist, &q->queue_head);
420 return;
421 }
422
423
424
425
426
427 memset(&rq->flush, 0, sizeof(rq->flush));
428 INIT_LIST_HEAD(&rq->flush.list);
429 rq->cmd_flags |= REQ_FLUSH_SEQ;
430 rq->flush.saved_end_io = rq->end_io;
431 if (q->mq_ops) {
432 rq->end_io = mq_flush_data_end_io;
433
434 spin_lock_irq(&q->mq_flush_lock);
435 blk_flush_complete_seq(rq, REQ_FSEQ_ACTIONS & ~policy, 0);
436 spin_unlock_irq(&q->mq_flush_lock);
437 return;
438 }
439 rq->end_io = flush_data_end_io;
440
441 blk_flush_complete_seq(rq, REQ_FSEQ_ACTIONS & ~policy, 0);
442}
443
444
445
446
447
448
449
450
451
452
453
454void blk_abort_flushes(struct request_queue *q)
455{
456 struct request *rq, *n;
457 int i;
458
459
460
461
462
463 list_for_each_entry_safe(rq, n, &q->flush_data_in_flight, flush.list) {
464 list_del_init(&rq->flush.list);
465 blk_flush_restore_request(rq);
466 }
467
468
469
470
471
472 for (i = 0; i < ARRAY_SIZE(q->flush_queue); i++) {
473 list_for_each_entry_safe(rq, n, &q->flush_queue[i],
474 flush.list) {
475 list_del_init(&rq->flush.list);
476 blk_flush_restore_request(rq);
477 list_add_tail(&rq->queuelist, &q->queue_head);
478 }
479 }
480}
481
482
483
484
485
486
487
488
489
490
491
492
493
494int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
495 sector_t *error_sector)
496{
497 struct request_queue *q;
498 struct bio *bio;
499 int ret = 0;
500
501 if (bdev->bd_disk == NULL)
502 return -ENXIO;
503
504 q = bdev_get_queue(bdev);
505 if (!q)
506 return -ENXIO;
507
508
509
510
511
512
513
514 if (!q->make_request_fn)
515 return -ENXIO;
516
517 bio = bio_alloc(gfp_mask, 0);
518 bio->bi_bdev = bdev;
519
520 ret = submit_bio_wait(WRITE_FLUSH, bio);
521
522
523
524
525
526
527 if (error_sector)
528 *error_sector = bio->bi_iter.bi_sector;
529
530 bio_put(bio);
531 return ret;
532}
533EXPORT_SYMBOL(blkdev_issue_flush);
534
535void blk_mq_init_flush(struct request_queue *q)
536{
537 spin_lock_init(&q->mq_flush_lock);
538}
539