Blame view

block/blk-flush.c 14.8 KB
86db1e297   Jens Axboe   block: continue l...
1
  /*
4fed947cb   Tejun Heo   block: implement ...
2
   * Functions to sequence FLUSH and FUA writes.
ae1b15396   Tejun Heo   block: reimplemen...
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
   *
   * Copyright (C) 2011		Max Planck Institute for Gravitational Physics
   * Copyright (C) 2011		Tejun Heo <tj@kernel.org>
   *
   * This file is released under the GPLv2.
   *
   * REQ_{FLUSH|FUA} requests are decomposed to sequences consisted of three
   * optional steps - PREFLUSH, DATA and POSTFLUSH - according to the request
   * properties and hardware capability.
   *
   * If a request doesn't have data, only REQ_FLUSH makes sense, which
   * indicates a simple flush request.  If there is data, REQ_FLUSH indicates
   * that the device cache should be flushed before the data is executed, and
   * REQ_FUA means that the data must be on non-volatile media on request
   * completion.
   *
   * If the device doesn't have writeback cache, FLUSH and FUA don't make any
   * difference.  The requests are either completed immediately if there's no
   * data or executed as normal requests otherwise.
   *
   * If the device has writeback cache and supports FUA, REQ_FLUSH is
   * translated to PREFLUSH but REQ_FUA is passed down directly with DATA.
   *
   * If the device has writeback cache and doesn't support FUA, REQ_FLUSH is
   * translated to PREFLUSH and REQ_FUA to POSTFLUSH.
   *
   * The actual execution of flush is double buffered.  Whenever a request
   * needs to execute PRE or POSTFLUSH, it queues at
7c94e1c15   Ming Lei   block: introduce ...
31
   * fq->flush_queue[fq->flush_pending_idx].  Once certain criteria are met, a
ae1b15396   Tejun Heo   block: reimplemen...
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
   * flush is issued and the pending_idx is toggled.  When the flush
   * completes, all the requests which were pending are proceeded to the next
   * step.  This allows arbitrary merging of different types of FLUSH/FUA
   * requests.
   *
   * Currently, the following conditions are used to determine when to issue
   * flush.
   *
   * C1. At any given time, only one flush shall be in progress.  This makes
   *     double buffering sufficient.
   *
   * C2. Flush is deferred if any request is executing DATA of its sequence.
   *     This avoids issuing separate POSTFLUSHes for requests which shared
   *     PREFLUSH.
   *
   * C3. The second condition is ignored if there is a request which has
   *     waited longer than FLUSH_PENDING_TIMEOUT.  This is to avoid
   *     starvation in the unlikely case where there are continuous stream of
   *     FUA (without FLUSH) requests.
   *
   * For devices which support FUA, it isn't clear whether C2 (and thus C3)
   * is beneficial.
   *
   * Note that a sequenced FLUSH/FUA request with DATA is completed twice.
   * Once while executing DATA and again after the whole sequence is
   * complete.  The first completion updates the contained bio but doesn't
   * finish it so that the bio submitter is notified only after the whole
   * sequence is complete.  This is implemented by testing REQ_FLUSH_SEQ in
   * req_bio_endio().
   *
   * The above peculiarity requires that each FLUSH/FUA request has only one
   * bio attached to it, which is guaranteed as they aren't allowed to be
   * merged in the usual way.
86db1e297   Jens Axboe   block: continue l...
65
   */
ae1b15396   Tejun Heo   block: reimplemen...
66

86db1e297   Jens Axboe   block: continue l...
67
68
69
70
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/bio.h>
  #include <linux/blkdev.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
71
  #include <linux/gfp.h>
320ae51fe   Jens Axboe   blk-mq: new multi...
72
  #include <linux/blk-mq.h>
86db1e297   Jens Axboe   block: continue l...
73
74
  
  #include "blk.h"
320ae51fe   Jens Axboe   blk-mq: new multi...
75
  #include "blk-mq.h"
86db1e297   Jens Axboe   block: continue l...
76

4fed947cb   Tejun Heo   block: implement ...
77
78
  /* FLUSH/FUA sequences */
  enum {
ae1b15396   Tejun Heo   block: reimplemen...
79
80
81
82
83
84
85
86
87
88
89
90
91
  	REQ_FSEQ_PREFLUSH	= (1 << 0), /* pre-flushing in progress */
  	REQ_FSEQ_DATA		= (1 << 1), /* data write in progress */
  	REQ_FSEQ_POSTFLUSH	= (1 << 2), /* post-flushing in progress */
  	REQ_FSEQ_DONE		= (1 << 3),
  
  	REQ_FSEQ_ACTIONS	= REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
  				  REQ_FSEQ_POSTFLUSH,
  
  	/*
  	 * If flush has been pending longer than the following timeout,
  	 * it's issued even if flush_data requests are still in flight.
  	 */
  	FLUSH_PENDING_TIMEOUT	= 5 * HZ,
4fed947cb   Tejun Heo   block: implement ...
92
  };
0bae352da   Ming Lei   block: flush: avo...
93
94
  static bool blk_kick_flush(struct request_queue *q,
  			   struct blk_flush_queue *fq);
28e7d1845   Tejun Heo   block: drop barri...
95

ae1b15396   Tejun Heo   block: reimplemen...
96
  static unsigned int blk_flush_policy(unsigned int fflags, struct request *rq)
86db1e297   Jens Axboe   block: continue l...
97
  {
ae1b15396   Tejun Heo   block: reimplemen...
98
  	unsigned int policy = 0;
86db1e297   Jens Axboe   block: continue l...
99

fa1bf42ff   Jeff Moyer   allow blk_flush_p...
100
101
  	if (blk_rq_sectors(rq))
  		policy |= REQ_FSEQ_DATA;
ae1b15396   Tejun Heo   block: reimplemen...
102
103
104
  	if (fflags & REQ_FLUSH) {
  		if (rq->cmd_flags & REQ_FLUSH)
  			policy |= REQ_FSEQ_PREFLUSH;
ae1b15396   Tejun Heo   block: reimplemen...
105
106
  		if (!(fflags & REQ_FUA) && (rq->cmd_flags & REQ_FUA))
  			policy |= REQ_FSEQ_POSTFLUSH;
28e7d1845   Tejun Heo   block: drop barri...
107
  	}
ae1b15396   Tejun Heo   block: reimplemen...
108
  	return policy;
86db1e297   Jens Axboe   block: continue l...
109
  }
ae1b15396   Tejun Heo   block: reimplemen...
110
  static unsigned int blk_flush_cur_seq(struct request *rq)
47f70d5a6   Tejun Heo   block: kick queue...
111
  {
ae1b15396   Tejun Heo   block: reimplemen...
112
113
  	return 1 << ffz(rq->flush.seq);
  }
47f70d5a6   Tejun Heo   block: kick queue...
114

ae1b15396   Tejun Heo   block: reimplemen...
115
116
  static void blk_flush_restore_request(struct request *rq)
  {
47f70d5a6   Tejun Heo   block: kick queue...
117
  	/*
ae1b15396   Tejun Heo   block: reimplemen...
118
119
120
  	 * After flush data completion, @rq->bio is %NULL but we need to
  	 * complete the bio again.  @rq->biotail is guaranteed to equal the
  	 * original @rq->bio.  Restore it.
47f70d5a6   Tejun Heo   block: kick queue...
121
  	 */
ae1b15396   Tejun Heo   block: reimplemen...
122
123
124
125
  	rq->bio = rq->biotail;
  
  	/* make @rq a normal request */
  	rq->cmd_flags &= ~REQ_FLUSH_SEQ;
4853abaae   Jeff Moyer   block: fix flush ...
126
  	rq->end_io = rq->flush.saved_end_io;
320ae51fe   Jens Axboe   blk-mq: new multi...
127
  }
10beafc19   Mike Snitzer   block: change flu...
128
  static bool blk_flush_queue_rq(struct request *rq, bool add_front)
320ae51fe   Jens Axboe   blk-mq: new multi...
129
  {
18741986a   Christoph Hellwig   blk-mq: rework fl...
130
  	if (rq->q->mq_ops) {
6fca6a611   Christoph Hellwig   blk-mq: add helpe...
131
132
133
134
  		struct request_queue *q = rq->q;
  
  		blk_mq_add_to_requeue_list(rq, add_front);
  		blk_mq_kick_requeue_list(q);
18741986a   Christoph Hellwig   blk-mq: rework fl...
135
136
  		return false;
  	} else {
10beafc19   Mike Snitzer   block: change flu...
137
138
139
140
  		if (add_front)
  			list_add(&rq->queuelist, &rq->q->queue_head);
  		else
  			list_add_tail(&rq->queuelist, &rq->q->queue_head);
18741986a   Christoph Hellwig   blk-mq: rework fl...
141
142
  		return true;
  	}
47f70d5a6   Tejun Heo   block: kick queue...
143
  }
ae1b15396   Tejun Heo   block: reimplemen...
144
145
146
  /**
   * blk_flush_complete_seq - complete flush sequence
   * @rq: FLUSH/FUA request being sequenced
0bae352da   Ming Lei   block: flush: avo...
147
   * @fq: flush queue
ae1b15396   Tejun Heo   block: reimplemen...
148
149
150
151
152
153
154
   * @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
   * @error: whether an error occurred
   *
   * @rq just completed @seq part of its flush sequence, record the
   * completion and trigger the next step.
   *
   * CONTEXT:
7c94e1c15   Ming Lei   block: introduce ...
155
   * spin_lock_irq(q->queue_lock or fq->mq_flush_lock)
ae1b15396   Tejun Heo   block: reimplemen...
156
157
158
159
   *
   * RETURNS:
   * %true if requests were added to the dispatch queue, %false otherwise.
   */
0bae352da   Ming Lei   block: flush: avo...
160
161
162
  static bool blk_flush_complete_seq(struct request *rq,
  				   struct blk_flush_queue *fq,
  				   unsigned int seq, int error)
86db1e297   Jens Axboe   block: continue l...
163
  {
ae1b15396   Tejun Heo   block: reimplemen...
164
  	struct request_queue *q = rq->q;
7c94e1c15   Ming Lei   block: introduce ...
165
  	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
320ae51fe   Jens Axboe   blk-mq: new multi...
166
  	bool queued = false, kicked;
ae1b15396   Tejun Heo   block: reimplemen...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  
  	BUG_ON(rq->flush.seq & seq);
  	rq->flush.seq |= seq;
  
  	if (likely(!error))
  		seq = blk_flush_cur_seq(rq);
  	else
  		seq = REQ_FSEQ_DONE;
  
  	switch (seq) {
  	case REQ_FSEQ_PREFLUSH:
  	case REQ_FSEQ_POSTFLUSH:
  		/* queue for flush */
  		if (list_empty(pending))
7c94e1c15   Ming Lei   block: introduce ...
181
  			fq->flush_pending_since = jiffies;
ae1b15396   Tejun Heo   block: reimplemen...
182
183
184
185
  		list_move_tail(&rq->flush.list, pending);
  		break;
  
  	case REQ_FSEQ_DATA:
7c94e1c15   Ming Lei   block: introduce ...
186
  		list_move_tail(&rq->flush.list, &fq->flush_data_in_flight);
10beafc19   Mike Snitzer   block: change flu...
187
  		queued = blk_flush_queue_rq(rq, true);
ae1b15396   Tejun Heo   block: reimplemen...
188
189
190
191
192
193
194
195
196
197
198
199
  		break;
  
  	case REQ_FSEQ_DONE:
  		/*
  		 * @rq was previously adjusted by blk_flush_issue() for
  		 * flush sequencing and may already have gone through the
  		 * flush data request completion path.  Restore @rq for
  		 * normal completion and end it.
  		 */
  		BUG_ON(!list_empty(&rq->queuelist));
  		list_del_init(&rq->flush.list);
  		blk_flush_restore_request(rq);
320ae51fe   Jens Axboe   blk-mq: new multi...
200
  		if (q->mq_ops)
c8a446ad6   Christoph Hellwig   blk-mq: rename bl...
201
  			blk_mq_end_request(rq, error);
320ae51fe   Jens Axboe   blk-mq: new multi...
202
203
  		else
  			__blk_end_request_all(rq, error);
ae1b15396   Tejun Heo   block: reimplemen...
204
205
206
207
208
  		break;
  
  	default:
  		BUG();
  	}
0bae352da   Ming Lei   block: flush: avo...
209
  	kicked = blk_kick_flush(q, fq);
320ae51fe   Jens Axboe   blk-mq: new multi...
210
  	return kicked | queued;
86db1e297   Jens Axboe   block: continue l...
211
  }
ae1b15396   Tejun Heo   block: reimplemen...
212
  static void flush_end_io(struct request *flush_rq, int error)
86db1e297   Jens Axboe   block: continue l...
213
  {
ae1b15396   Tejun Heo   block: reimplemen...
214
  	struct request_queue *q = flush_rq->q;
320ae51fe   Jens Axboe   blk-mq: new multi...
215
  	struct list_head *running;
ae1b15396   Tejun Heo   block: reimplemen...
216
217
  	bool queued = false;
  	struct request *rq, *n;
320ae51fe   Jens Axboe   blk-mq: new multi...
218
  	unsigned long flags = 0;
e97c293cd   Ming Lei   block: introduce ...
219
  	struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx);
ae1b15396   Tejun Heo   block: reimplemen...
220

223023750   Shaohua Li   blk-mq: blk_mq_ta...
221
  	if (q->mq_ops) {
7c94e1c15   Ming Lei   block: introduce ...
222
  		spin_lock_irqsave(&fq->mq_flush_lock, flags);
7ddab5de5   Ming Lei   block: avoid to u...
223
  		flush_rq->tag = -1;
223023750   Shaohua Li   blk-mq: blk_mq_ta...
224
  	}
18741986a   Christoph Hellwig   blk-mq: rework fl...
225

7c94e1c15   Ming Lei   block: introduce ...
226
227
  	running = &fq->flush_queue[fq->flush_running_idx];
  	BUG_ON(fq->flush_pending_idx == fq->flush_running_idx);
ae1b15396   Tejun Heo   block: reimplemen...
228
229
  
  	/* account completion of the flush request */
7c94e1c15   Ming Lei   block: introduce ...
230
  	fq->flush_running_idx ^= 1;
320ae51fe   Jens Axboe   blk-mq: new multi...
231
232
233
  
  	if (!q->mq_ops)
  		elv_completed_request(q, flush_rq);
ae1b15396   Tejun Heo   block: reimplemen...
234
235
236
237
238
239
  
  	/* and push the waiting requests to the next stage */
  	list_for_each_entry_safe(rq, n, running, flush.list) {
  		unsigned int seq = blk_flush_cur_seq(rq);
  
  		BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
0bae352da   Ming Lei   block: flush: avo...
240
  		queued |= blk_flush_complete_seq(rq, fq, seq, error);
ae1b15396   Tejun Heo   block: reimplemen...
241
  	}
47f70d5a6   Tejun Heo   block: kick queue...
242
  	/*
3ac0cc450   shaohua.li@intel.com   block: hold queue...
243
244
245
246
247
248
249
250
251
  	 * Kick the queue to avoid stall for two cases:
  	 * 1. Moving a request silently to empty queue_head may stall the
  	 * queue.
  	 * 2. When flush request is running in non-queueable queue, the
  	 * queue is hold. Restart the queue after flush request is finished
  	 * to avoid stall.
  	 * This function is called from request completion path and calling
  	 * directly into request_fn may confuse the driver.  Always use
  	 * kblockd.
47f70d5a6   Tejun Heo   block: kick queue...
252
  	 */
7c94e1c15   Ming Lei   block: introduce ...
253
  	if (queued || fq->flush_queue_delayed) {
18741986a   Christoph Hellwig   blk-mq: rework fl...
254
255
  		WARN_ON(q->mq_ops);
  		blk_run_queue_async(q);
320ae51fe   Jens Axboe   blk-mq: new multi...
256
  	}
7c94e1c15   Ming Lei   block: introduce ...
257
  	fq->flush_queue_delayed = 0;
320ae51fe   Jens Axboe   blk-mq: new multi...
258
  	if (q->mq_ops)
7c94e1c15   Ming Lei   block: introduce ...
259
  		spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
320ae51fe   Jens Axboe   blk-mq: new multi...
260
  }
ae1b15396   Tejun Heo   block: reimplemen...
261
262
263
  /**
   * blk_kick_flush - consider issuing flush request
   * @q: request_queue being kicked
0bae352da   Ming Lei   block: flush: avo...
264
   * @fq: flush queue
ae1b15396   Tejun Heo   block: reimplemen...
265
266
267
268
269
   *
   * Flush related states of @q have changed, consider issuing flush request.
   * Please read the comment at the top of this file for more info.
   *
   * CONTEXT:
7c94e1c15   Ming Lei   block: introduce ...
270
   * spin_lock_irq(q->queue_lock or fq->mq_flush_lock)
ae1b15396   Tejun Heo   block: reimplemen...
271
272
273
274
   *
   * RETURNS:
   * %true if flush was issued, %false otherwise.
   */
0bae352da   Ming Lei   block: flush: avo...
275
  static bool blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq)
86db1e297   Jens Axboe   block: continue l...
276
  {
7c94e1c15   Ming Lei   block: introduce ...
277
  	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
ae1b15396   Tejun Heo   block: reimplemen...
278
279
  	struct request *first_rq =
  		list_first_entry(pending, struct request, flush.list);
7c94e1c15   Ming Lei   block: introduce ...
280
  	struct request *flush_rq = fq->flush_rq;
ae1b15396   Tejun Heo   block: reimplemen...
281
282
  
  	/* C1 described at the top of this file */
7c94e1c15   Ming Lei   block: introduce ...
283
  	if (fq->flush_pending_idx != fq->flush_running_idx || list_empty(pending))
ae1b15396   Tejun Heo   block: reimplemen...
284
285
286
  		return false;
  
  	/* C2 and C3 */
7c94e1c15   Ming Lei   block: introduce ...
287
  	if (!list_empty(&fq->flush_data_in_flight) &&
ae1b15396   Tejun Heo   block: reimplemen...
288
  	    time_before(jiffies,
7c94e1c15   Ming Lei   block: introduce ...
289
  			fq->flush_pending_since + FLUSH_PENDING_TIMEOUT))
ae1b15396   Tejun Heo   block: reimplemen...
290
291
292
293
294
295
  		return false;
  
  	/*
  	 * Issue flush and toggle pending_idx.  This makes pending_idx
  	 * different from running_idx, which means flush is in flight.
  	 */
7c94e1c15   Ming Lei   block: introduce ...
296
  	fq->flush_pending_idx ^= 1;
18741986a   Christoph Hellwig   blk-mq: rework fl...
297

7ddab5de5   Ming Lei   block: avoid to u...
298
  	blk_rq_init(q, flush_rq);
f70ced091   Ming Lei   blk-mq: support p...
299
300
301
302
303
304
305
306
307
  
  	/*
  	 * Borrow tag from the first request since they can't
  	 * be in flight at the same time.
  	 */
  	if (q->mq_ops) {
  		flush_rq->mq_ctx = first_rq->mq_ctx;
  		flush_rq->tag = first_rq->tag;
  	}
320ae51fe   Jens Axboe   blk-mq: new multi...
308

7ddab5de5   Ming Lei   block: avoid to u...
309
310
311
312
  	flush_rq->cmd_type = REQ_TYPE_FS;
  	flush_rq->cmd_flags = WRITE_FLUSH | REQ_FLUSH_SEQ;
  	flush_rq->rq_disk = first_rq->rq_disk;
  	flush_rq->end_io = flush_end_io;
ae1b15396   Tejun Heo   block: reimplemen...
313

7ddab5de5   Ming Lei   block: avoid to u...
314
  	return blk_flush_queue_rq(flush_rq, false);
86db1e297   Jens Axboe   block: continue l...
315
  }
ae1b15396   Tejun Heo   block: reimplemen...
316
  static void flush_data_end_io(struct request *rq, int error)
86db1e297   Jens Axboe   block: continue l...
317
  {
ae1b15396   Tejun Heo   block: reimplemen...
318
  	struct request_queue *q = rq->q;
e97c293cd   Ming Lei   block: introduce ...
319
  	struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
ae1b15396   Tejun Heo   block: reimplemen...
320

e83a46bbb   Tejun Heo   Merge branch 'for...
321
322
323
324
  	/*
  	 * After populating an empty queue, kick it to avoid stall.  Read
  	 * the comment in flush_end_io().
  	 */
0bae352da   Ming Lei   block: flush: avo...
325
  	if (blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error))
24ecfbe27   Christoph Hellwig   block: add blk_ru...
326
  		blk_run_queue_async(q);
86db1e297   Jens Axboe   block: continue l...
327
  }
320ae51fe   Jens Axboe   blk-mq: new multi...
328
329
330
331
  static void mq_flush_data_end_io(struct request *rq, int error)
  {
  	struct request_queue *q = rq->q;
  	struct blk_mq_hw_ctx *hctx;
e97c293cd   Ming Lei   block: introduce ...
332
  	struct blk_mq_ctx *ctx = rq->mq_ctx;
320ae51fe   Jens Axboe   blk-mq: new multi...
333
  	unsigned long flags;
e97c293cd   Ming Lei   block: introduce ...
334
  	struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx);
320ae51fe   Jens Axboe   blk-mq: new multi...
335

320ae51fe   Jens Axboe   blk-mq: new multi...
336
337
338
339
340
341
  	hctx = q->mq_ops->map_queue(q, ctx->cpu);
  
  	/*
  	 * After populating an empty queue, kick it to avoid stall.  Read
  	 * the comment in flush_end_io().
  	 */
7c94e1c15   Ming Lei   block: introduce ...
342
  	spin_lock_irqsave(&fq->mq_flush_lock, flags);
0bae352da   Ming Lei   block: flush: avo...
343
  	if (blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error))
320ae51fe   Jens Axboe   blk-mq: new multi...
344
  		blk_mq_run_hw_queue(hctx, true);
7c94e1c15   Ming Lei   block: introduce ...
345
  	spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
320ae51fe   Jens Axboe   blk-mq: new multi...
346
  }
ae1b15396   Tejun Heo   block: reimplemen...
347
348
349
350
  /**
   * blk_insert_flush - insert a new FLUSH/FUA request
   * @rq: request to insert
   *
b710a4805   Jens Axboe   block: get rid of...
351
   * To be called from __elv_add_request() for %ELEVATOR_INSERT_FLUSH insertions.
320ae51fe   Jens Axboe   blk-mq: new multi...
352
   * or __blk_mq_run_hw_queue() to dispatch request.
ae1b15396   Tejun Heo   block: reimplemen...
353
354
355
356
   * @rq is being submitted.  Analyze what needs to be done and put it on the
   * right queue.
   *
   * CONTEXT:
320ae51fe   Jens Axboe   blk-mq: new multi...
357
   * spin_lock_irq(q->queue_lock) in !mq case
ae1b15396   Tejun Heo   block: reimplemen...
358
359
   */
  void blk_insert_flush(struct request *rq)
86db1e297   Jens Axboe   block: continue l...
360
  {
ae1b15396   Tejun Heo   block: reimplemen...
361
362
363
  	struct request_queue *q = rq->q;
  	unsigned int fflags = q->flush_flags;	/* may change, cache */
  	unsigned int policy = blk_flush_policy(fflags, rq);
e97c293cd   Ming Lei   block: introduce ...
364
  	struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
86db1e297   Jens Axboe   block: continue l...
365

ae1b15396   Tejun Heo   block: reimplemen...
366
367
368
369
  	/*
  	 * @policy now records what operations need to be done.  Adjust
  	 * REQ_FLUSH and FUA for the driver.
  	 */
18741986a   Christoph Hellwig   blk-mq: rework fl...
370
  	rq->cmd_flags &= ~REQ_FLUSH;
ae1b15396   Tejun Heo   block: reimplemen...
371
372
373
374
  	if (!(fflags & REQ_FUA))
  		rq->cmd_flags &= ~REQ_FUA;
  
  	/*
4853abaae   Jeff Moyer   block: fix flush ...
375
376
377
378
379
380
  	 * An empty flush handed down from a stacking driver may
  	 * translate into nothing if the underlying device does not
  	 * advertise a write-back cache.  In this case, simply
  	 * complete the request.
  	 */
  	if (!policy) {
320ae51fe   Jens Axboe   blk-mq: new multi...
381
  		if (q->mq_ops)
c8a446ad6   Christoph Hellwig   blk-mq: rename bl...
382
  			blk_mq_end_request(rq, 0);
320ae51fe   Jens Axboe   blk-mq: new multi...
383
384
  		else
  			__blk_end_bidi_request(rq, 0, 0, 0);
4853abaae   Jeff Moyer   block: fix flush ...
385
386
  		return;
  	}
834f9f61a   Jeff Moyer   blk-flush: fix in...
387
  	BUG_ON(rq->bio != rq->biotail); /*assumes zero or single bio rq */
4853abaae   Jeff Moyer   block: fix flush ...
388
389
  
  	/*
ae1b15396   Tejun Heo   block: reimplemen...
390
391
392
393
394
395
  	 * If there's data but flush is not necessary, the request can be
  	 * processed directly without going through flush machinery.  Queue
  	 * for normal execution.
  	 */
  	if ((policy & REQ_FSEQ_DATA) &&
  	    !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
320ae51fe   Jens Axboe   blk-mq: new multi...
396
  		if (q->mq_ops) {
feb71dae1   Christoph Hellwig   blk-mq: merge blk...
397
  			blk_mq_insert_request(rq, false, false, true);
320ae51fe   Jens Axboe   blk-mq: new multi...
398
399
  		} else
  			list_add_tail(&rq->queuelist, &q->queue_head);
ae1b15396   Tejun Heo   block: reimplemen...
400
  		return;
28e7d1845   Tejun Heo   block: drop barri...
401
  	}
cde4c406d   Christoph Hellwig   block: simplify q...
402

ae1b15396   Tejun Heo   block: reimplemen...
403
404
405
406
407
408
  	/*
  	 * @rq should go through flush machinery.  Mark it part of flush
  	 * sequence and submit for further processing.
  	 */
  	memset(&rq->flush, 0, sizeof(rq->flush));
  	INIT_LIST_HEAD(&rq->flush.list);
414b4ff5e   Tejun Heo   block: add REQ_FL...
409
  	rq->cmd_flags |= REQ_FLUSH_SEQ;
4853abaae   Jeff Moyer   block: fix flush ...
410
  	rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
320ae51fe   Jens Axboe   blk-mq: new multi...
411
412
  	if (q->mq_ops) {
  		rq->end_io = mq_flush_data_end_io;
7c94e1c15   Ming Lei   block: introduce ...
413
  		spin_lock_irq(&fq->mq_flush_lock);
0bae352da   Ming Lei   block: flush: avo...
414
  		blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
7c94e1c15   Ming Lei   block: introduce ...
415
  		spin_unlock_irq(&fq->mq_flush_lock);
320ae51fe   Jens Axboe   blk-mq: new multi...
416
417
  		return;
  	}
ae1b15396   Tejun Heo   block: reimplemen...
418
  	rq->end_io = flush_data_end_io;
0bae352da   Ming Lei   block: flush: avo...
419
  	blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
86db1e297   Jens Axboe   block: continue l...
420
  }
ae1b15396   Tejun Heo   block: reimplemen...
421
  /**
86db1e297   Jens Axboe   block: continue l...
422
423
   * blkdev_issue_flush - queue a flush
   * @bdev:	blockdev to issue flush for
fbd9b09a1   Dmitry Monakhov   blkdev: generaliz...
424
   * @gfp_mask:	memory allocation flags (for bio_alloc)
86db1e297   Jens Axboe   block: continue l...
425
426
427
428
429
   * @error_sector:	error sector
   *
   * Description:
   *    Issue a flush for the block device in question. Caller can supply
   *    room for storing the error offset in case of a flush error, if they
f17e232e9   Dmitry Monakhov   blkdev: allow asy...
430
431
   *    wish to. If WAIT flag is not passed then caller may check only what
   *    request was pushed in some internal queue for later handling.
86db1e297   Jens Axboe   block: continue l...
432
   */
fbd9b09a1   Dmitry Monakhov   blkdev: generaliz...
433
  int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
dd3932edd   Christoph Hellwig   block: remove BLK...
434
  		sector_t *error_sector)
86db1e297   Jens Axboe   block: continue l...
435
  {
86db1e297   Jens Axboe   block: continue l...
436
437
  	struct request_queue *q;
  	struct bio *bio;
fbd9b09a1   Dmitry Monakhov   blkdev: generaliz...
438
  	int ret = 0;
86db1e297   Jens Axboe   block: continue l...
439
440
441
442
443
444
445
  
  	if (bdev->bd_disk == NULL)
  		return -ENXIO;
  
  	q = bdev_get_queue(bdev);
  	if (!q)
  		return -ENXIO;
f10d9f617   Dave Chinner   blkdev: check for...
446
447
448
449
  	/*
  	 * some block devices may not have their queue correctly set up here
  	 * (e.g. loop device without a backing file) and so issuing a flush
  	 * here will panic. Ensure there is a request function before issuing
d391a2dda   Tejun Heo   block: use REQ_FL...
450
  	 * the flush.
f10d9f617   Dave Chinner   blkdev: check for...
451
452
453
  	 */
  	if (!q->make_request_fn)
  		return -ENXIO;
fbd9b09a1   Dmitry Monakhov   blkdev: generaliz...
454
  	bio = bio_alloc(gfp_mask, 0);
86db1e297   Jens Axboe   block: continue l...
455
  	bio->bi_bdev = bdev;
86db1e297   Jens Axboe   block: continue l...
456

33879d451   Kent Overstreet   block: submit_bio...
457
  	ret = submit_bio_wait(WRITE_FLUSH, bio);
dd3932edd   Christoph Hellwig   block: remove BLK...
458
459
460
461
462
463
464
  
  	/*
  	 * The driver must store the error location in ->bi_sector, if
  	 * it supports it. For non-stacked drivers, this should be
  	 * copied from blk_rq_pos(rq).
  	 */
  	if (error_sector)
4f024f379   Kent Overstreet   block: Abstract o...
465
  		*error_sector = bio->bi_iter.bi_sector;
86db1e297   Jens Axboe   block: continue l...
466

86db1e297   Jens Axboe   block: continue l...
467
468
469
  	bio_put(bio);
  	return ret;
  }
86db1e297   Jens Axboe   block: continue l...
470
  EXPORT_SYMBOL(blkdev_issue_flush);
320ae51fe   Jens Axboe   blk-mq: new multi...
471

f70ced091   Ming Lei   blk-mq: support p...
472
473
  struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
  		int node, int cmd_size)
320ae51fe   Jens Axboe   blk-mq: new multi...
474
  {
7c94e1c15   Ming Lei   block: introduce ...
475
476
  	struct blk_flush_queue *fq;
  	int rq_sz = sizeof(struct request);
1bcb1eada   Ming Lei   blk-mq: allocate ...
477

f70ced091   Ming Lei   blk-mq: support p...
478
  	fq = kzalloc_node(sizeof(*fq), GFP_KERNEL, node);
7c94e1c15   Ming Lei   block: introduce ...
479
480
  	if (!fq)
  		goto fail;
1bcb1eada   Ming Lei   blk-mq: allocate ...
481

7c94e1c15   Ming Lei   block: introduce ...
482
483
  	if (q->mq_ops) {
  		spin_lock_init(&fq->mq_flush_lock);
f70ced091   Ming Lei   blk-mq: support p...
484
  		rq_sz = round_up(rq_sz + cmd_size, cache_line_size());
7c94e1c15   Ming Lei   block: introduce ...
485
  	}
f70ced091   Ming Lei   blk-mq: support p...
486
  	fq->flush_rq = kzalloc_node(rq_sz, GFP_KERNEL, node);
7c94e1c15   Ming Lei   block: introduce ...
487
488
489
490
491
492
493
494
495
496
497
498
499
  	if (!fq->flush_rq)
  		goto fail_rq;
  
  	INIT_LIST_HEAD(&fq->flush_queue[0]);
  	INIT_LIST_HEAD(&fq->flush_queue[1]);
  	INIT_LIST_HEAD(&fq->flush_data_in_flight);
  
  	return fq;
  
   fail_rq:
  	kfree(fq);
   fail:
  	return NULL;
320ae51fe   Jens Axboe   blk-mq: new multi...
500
  }
f35526557   Ming Lei   block: introduce ...
501

ba483388e   Ming Lei   block: remove blk...
502
  void blk_free_flush_queue(struct blk_flush_queue *fq)
f35526557   Ming Lei   block: introduce ...
503
  {
7c94e1c15   Ming Lei   block: introduce ...
504
505
506
  	/* bio based request queue hasn't flush queue */
  	if (!fq)
  		return;
3c09676c1   Ming Lei   block: move flush...
507

7c94e1c15   Ming Lei   block: introduce ...
508
509
510
  	kfree(fq->flush_rq);
  	kfree(fq);
  }