blk-flush.c 6.84 KB
Newer Older
1
/*
2
 * Functions to sequence FLUSH and FUA writes.
3
4
5
6
7
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
8
#include <linux/gfp.h>
9
10
11

#include "blk.h"

12
13
14
15
16
17
18
19
20
/* FLUSH/FUA sequences */
enum {
	QUEUE_FSEQ_STARTED	= (1 << 0), /* flushing in progress */
	QUEUE_FSEQ_PREFLUSH	= (1 << 1), /* pre-flushing in progress */
	QUEUE_FSEQ_DATA		= (1 << 2), /* data write in progress */
	QUEUE_FSEQ_POSTFLUSH	= (1 << 3), /* post-flushing in progress */
	QUEUE_FSEQ_DONE		= (1 << 4),
};

21
static struct request *queue_next_fseq(struct request_queue *q);
22

23
unsigned blk_flush_cur_seq(struct request_queue *q)
24
{
25
	if (!q->flush_seq)
26
		return 0;
27
	return 1 << ffz(q->flush_seq);
28
29
}

30
31
static struct request *blk_flush_complete_seq(struct request_queue *q,
					      unsigned seq, int error)
32
{
33
	struct request *next_rq = NULL;
34

35
36
	if (error && !q->flush_err)
		q->flush_err = error;
37

38
39
	BUG_ON(q->flush_seq & seq);
	q->flush_seq |= seq;
40

41
42
43
	if (blk_flush_cur_seq(q) != QUEUE_FSEQ_DONE) {
		/* not complete yet, queue the next flush sequence */
		next_rq = queue_next_fseq(q);
44
	} else {
45
46
47
48
49
50
51
52
		/* complete this flush request */
		__blk_end_request_all(q->orig_flush_rq, q->flush_err);
		q->orig_flush_rq = NULL;
		q->flush_seq = 0;

		/* dispatch the next flush if there's one */
		if (!list_empty(&q->pending_flushes)) {
			next_rq = list_entry_rq(q->pending_flushes.next);
53
54
55
56
			list_move(&next_rq->queuelist, &q->queue_head);
		}
	}
	return next_rq;
57
58
}

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
static void blk_flush_complete_seq_end_io(struct request_queue *q,
					  unsigned seq, int error)
{
	bool was_empty = elv_queue_empty(q);
	struct request *next_rq;

	next_rq = blk_flush_complete_seq(q, seq, error);

	/*
	 * Moving a request silently to empty queue_head may stall the
	 * queue.  Kick the queue in those cases.
	 */
	if (was_empty && next_rq)
		__blk_run_queue(q);
}

75
76
77
static void pre_flush_end_io(struct request *rq, int error)
{
	elv_completed_request(rq->q, rq);
78
	blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_PREFLUSH, error);
79
80
}

81
static void flush_data_end_io(struct request *rq, int error)
82
83
{
	elv_completed_request(rq->q, rq);
84
	blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_DATA, error);
85
86
87
88
89
}

static void post_flush_end_io(struct request *rq, int error)
{
	elv_completed_request(rq->q, rq);
90
	blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_POSTFLUSH, error);
91
92
}

93
static void init_flush_request(struct request *rq, struct gendisk *disk)
94
{
95
	rq->cmd_type = REQ_TYPE_FS;
96
	rq->cmd_flags = WRITE_FLUSH;
97
	rq->rq_disk = disk;
98
99
}

100
static struct request *queue_next_fseq(struct request_queue *q)
101
{
102
	struct request *orig_rq = q->orig_flush_rq;
103
	struct request *rq = &q->flush_rq;
104

105
106
	blk_rq_init(q, rq);

107
108
	switch (blk_flush_cur_seq(q)) {
	case QUEUE_FSEQ_PREFLUSH:
109
110
		init_flush_request(rq, orig_rq->rq_disk);
		rq->end_io = pre_flush_end_io;
111
		break;
112
	case QUEUE_FSEQ_DATA:
113
		init_request_from_bio(rq, orig_rq->bio);
114
115
116
117
118
119
120
		/*
		 * orig_rq->rq_disk may be different from
		 * bio->bi_bdev->bd_disk if orig_rq got here through
		 * remapping drivers.  Make sure rq->rq_disk points
		 * to the same one as orig_rq.
		 */
		rq->rq_disk = orig_rq->rq_disk;
121
122
		rq->cmd_flags &= ~(REQ_FLUSH | REQ_FUA);
		rq->cmd_flags |= orig_rq->cmd_flags & (REQ_FLUSH | REQ_FUA);
123
		rq->end_io = flush_data_end_io;
124
		break;
125
	case QUEUE_FSEQ_POSTFLUSH:
126
127
		init_flush_request(rq, orig_rq->rq_disk);
		rq->end_io = post_flush_end_io;
128
129
130
131
		break;
	default:
		BUG();
	}
132
133

	elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
134
	return rq;
135
136
}

137
struct request *blk_do_flush(struct request_queue *q, struct request *rq)
138
{
139
140
141
142
	unsigned int fflags = q->flush_flags; /* may change, cache it */
	bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA;
	bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH);
	bool do_postflush = has_flush && !has_fua && (rq->cmd_flags & REQ_FUA);
143
144
	unsigned skip = 0;

145
146
147
148
149
150
151
152
153
154
155
156
157
	/*
	 * Special case.  If there's data but flush is not necessary,
	 * the request can be issued directly.
	 *
	 * Flush w/o data should be able to be issued directly too but
	 * currently some drivers assume that rq->bio contains
	 * non-zero data if it isn't NULL and empty FLUSH requests
	 * getting here usually have bio's without data.
	 */
	if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) {
		rq->cmd_flags &= ~REQ_FLUSH;
		if (!has_fua)
			rq->cmd_flags &= ~REQ_FUA;
158
		return rq;
159
	}
160

161
162
163
164
165
	/*
	 * Sequenced flushes can't be processed in parallel.  If
	 * another one is already in progress, queue for later
	 * processing.
	 */
166
167
	if (q->flush_seq) {
		list_move_tail(&rq->queuelist, &q->pending_flushes);
168
169
170
		return NULL;
	}

171
	/*
172
	 * Start a new flush sequence
173
	 */
174
175
	q->flush_err = 0;
	q->flush_seq |= QUEUE_FSEQ_STARTED;
176

177
178
179
180
	/* adjust FLUSH/FUA of the original request and stash it away */
	rq->cmd_flags &= ~REQ_FLUSH;
	if (!has_fua)
		rq->cmd_flags &= ~REQ_FUA;
181
	blk_dequeue_request(rq);
182
	q->orig_flush_rq = rq;
183

184
185
	/* skip unneded sequences and return the first one */
	if (!do_preflush)
186
		skip |= QUEUE_FSEQ_PREFLUSH;
187
	if (!blk_rq_sectors(rq))
188
		skip |= QUEUE_FSEQ_DATA;
189
	if (!do_postflush)
190
191
		skip |= QUEUE_FSEQ_POSTFLUSH;
	return blk_flush_complete_seq(q, skip, 0);
192
193
}

194
static void bio_end_flush(struct bio *bio, int err)
195
{
196
	if (err)
197
		clear_bit(BIO_UPTODATE, &bio->bi_flags);
198
199
200
	if (bio->bi_private)
		complete(bio->bi_private);
	bio_put(bio);
201
202
203
204
205
}

/**
 * blkdev_issue_flush - queue a flush
 * @bdev:	blockdev to issue flush for
206
 * @gfp_mask:	memory allocation flags (for bio_alloc)
207
 * @error_sector:	error sector
208
 * @flags:	BLKDEV_IFL_* flags to control behaviour
209
210
211
212
 *
 * 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
213
214
 *    wish to. If WAIT flag is not passed then caller may check only what
 *    request was pushed in some internal queue for later handling.
215
 */
216
217
int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
		sector_t *error_sector, unsigned long flags)
218
219
220
221
{
	DECLARE_COMPLETION_ONSTACK(wait);
	struct request_queue *q;
	struct bio *bio;
222
	int ret = 0;
223
224
225
226
227
228
229
230

	if (bdev->bd_disk == NULL)
		return -ENXIO;

	q = bdev_get_queue(bdev);
	if (!q)
		return -ENXIO;

231
232
233
234
	/*
	 * 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
235
	 * the flush.
236
237
238
239
	 */
	if (!q->make_request_fn)
		return -ENXIO;

240
	bio = bio_alloc(gfp_mask, 0);
241
	bio->bi_end_io = bio_end_flush;
242
	bio->bi_bdev = bdev;
243
244
	if (test_bit(BLKDEV_WAIT, &flags))
		bio->bi_private = &wait;
245

246
	bio_get(bio);
247
	submit_bio(WRITE_FLUSH, bio);
248
249
250
251
252
253
254
255
256
257
	if (test_bit(BLKDEV_WAIT, &flags)) {
		wait_for_completion(&wait);
		/*
		 * 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)
			*error_sector = bio->bi_sector;
	}
258

259
	if (!bio_flagged(bio, BIO_UPTODATE))
260
261
262
263
264
265
		ret = -EIO;

	bio_put(bio);
	return ret;
}
EXPORT_SYMBOL(blkdev_issue_flush);