blk-flush.c 6.7 KB
Newer Older
J
Jens Axboe 已提交
1
/*
2
 * Functions to sequence FLUSH and FUA writes.
J
Jens Axboe 已提交
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>
J
Jens Axboe 已提交
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)
J
Jens Axboe 已提交
24
{
25
	if (!q->flush_seq)
J
Jens Axboe 已提交
26
		return 0;
27
	return 1 << ffz(q->flush_seq);
J
Jens Axboe 已提交
28 29
}

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

35 36
	if (error && !q->flush_err)
		q->flush_err = error;
J
Jens Axboe 已提交
37

38 39
	BUG_ON(q->flush_seq & seq);
	q->flush_seq |= seq;
J
Jens Axboe 已提交
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;
J
Jens Axboe 已提交
57 58
}

59 60 61 62 63 64 65 66 67 68 69 70 71
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)
72
		__blk_run_queue(q, false);
73 74
}

J
Jens Axboe 已提交
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);
J
Jens Axboe 已提交
79 80
}

81
static void flush_data_end_io(struct request *rq, int error)
J
Jens Axboe 已提交
82 83
{
	elv_completed_request(rq->q, rq);
84
	blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_DATA, error);
J
Jens Axboe 已提交
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);
J
Jens Axboe 已提交
91 92
}

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

100
static struct request *queue_next_fseq(struct request_queue *q)
J
Jens Axboe 已提交
101
{
102
	struct request *orig_rq = q->orig_flush_rq;
103
	struct request *rq = &q->flush_rq;
J
Jens Axboe 已提交
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);
T
Tejun Heo 已提交
134
	return rq;
J
Jens Axboe 已提交
135 136
}

137
struct request *blk_do_flush(struct request_queue *q, struct request *rq)
J
Jens Axboe 已提交
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;
	}

J
Jens Axboe 已提交
171
	/*
172
	 * Start a new flush sequence
J
Jens Axboe 已提交
173
	 */
174 175
	q->flush_err = 0;
	q->flush_seq |= QUEUE_FSEQ_STARTED;
J
Jens Axboe 已提交
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;
J
Jens Axboe 已提交
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);
J
Jens Axboe 已提交
192 193
}

194
static void bio_end_flush(struct bio *bio, int err)
J
Jens Axboe 已提交
195
{
196
	if (err)
J
Jens Axboe 已提交
197
		clear_bit(BIO_UPTODATE, &bio->bi_flags);
198 199 200
	if (bio->bi_private)
		complete(bio->bi_private);
	bio_put(bio);
J
Jens Axboe 已提交
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)
J
Jens Axboe 已提交
207 208 209 210 211
 * @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
212 213
 *    wish to. If WAIT flag is not passed then caller may check only what
 *    request was pushed in some internal queue for later handling.
J
Jens Axboe 已提交
214
 */
215
int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
216
		sector_t *error_sector)
J
Jens Axboe 已提交
217 218 219 220
{
	DECLARE_COMPLETION_ONSTACK(wait);
	struct request_queue *q;
	struct bio *bio;
221
	int ret = 0;
J
Jens Axboe 已提交
222 223 224 225 226 227 228 229

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

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

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

239
	bio = bio_alloc(gfp_mask, 0);
240
	bio->bi_end_io = bio_end_flush;
J
Jens Axboe 已提交
241
	bio->bi_bdev = bdev;
242
	bio->bi_private = &wait;
J
Jens Axboe 已提交
243

244
	bio_get(bio);
245
	submit_bio(WRITE_FLUSH, bio);
246 247 248 249 250 251 252 253 254
	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;
J
Jens Axboe 已提交
255

256
	if (!bio_flagged(bio, BIO_UPTODATE))
J
Jens Axboe 已提交
257 258 259 260 261 262
		ret = -EIO;

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