Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 1 | /* |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 2 | * Functions to sequence FLUSH and FUA writes. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/bio.h> |
| 7 | #include <linux/blkdev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/gfp.h> |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 9 | |
| 10 | #include "blk.h" |
| 11 | |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 12 | /* FLUSH/FUA sequences */ |
| 13 | enum { |
| 14 | QUEUE_FSEQ_STARTED = (1 << 0), /* flushing in progress */ |
| 15 | QUEUE_FSEQ_PREFLUSH = (1 << 1), /* pre-flushing in progress */ |
| 16 | QUEUE_FSEQ_DATA = (1 << 2), /* data write in progress */ |
| 17 | QUEUE_FSEQ_POSTFLUSH = (1 << 3), /* post-flushing in progress */ |
| 18 | QUEUE_FSEQ_DONE = (1 << 4), |
| 19 | }; |
| 20 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 21 | static struct request *queue_next_fseq(struct request_queue *q); |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 22 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 23 | unsigned blk_flush_cur_seq(struct request_queue *q) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 24 | { |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 25 | if (!q->flush_seq) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 26 | return 0; |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 27 | return 1 << ffz(q->flush_seq); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 30 | static struct request *blk_flush_complete_seq(struct request_queue *q, |
| 31 | unsigned seq, int error) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 32 | { |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 33 | struct request *next_rq = NULL; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 34 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 35 | if (error && !q->flush_err) |
| 36 | q->flush_err = error; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 37 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 38 | BUG_ON(q->flush_seq & seq); |
| 39 | q->flush_seq |= seq; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 40 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 41 | if (blk_flush_cur_seq(q) != QUEUE_FSEQ_DONE) { |
| 42 | /* not complete yet, queue the next flush sequence */ |
| 43 | next_rq = queue_next_fseq(q); |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 44 | } else { |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 45 | /* complete this flush request */ |
| 46 | __blk_end_request_all(q->orig_flush_rq, q->flush_err); |
| 47 | q->orig_flush_rq = NULL; |
| 48 | q->flush_seq = 0; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 49 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 50 | /* dispatch the next flush if there's one */ |
| 51 | if (!list_empty(&q->pending_flushes)) { |
| 52 | next_rq = list_entry_rq(q->pending_flushes.next); |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 53 | list_move(&next_rq->queuelist, &q->queue_head); |
| 54 | } |
| 55 | } |
| 56 | return next_rq; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Tejun Heo | 47f70d5 | 2010-09-03 11:56:17 +0200 | [diff] [blame^] | 59 | static void blk_flush_complete_seq_end_io(struct request_queue *q, |
| 60 | unsigned seq, int error) |
| 61 | { |
| 62 | bool was_empty = elv_queue_empty(q); |
| 63 | struct request *next_rq; |
| 64 | |
| 65 | next_rq = blk_flush_complete_seq(q, seq, error); |
| 66 | |
| 67 | /* |
| 68 | * Moving a request silently to empty queue_head may stall the |
| 69 | * queue. Kick the queue in those cases. |
| 70 | */ |
| 71 | if (was_empty && next_rq) |
| 72 | __blk_run_queue(q); |
| 73 | } |
| 74 | |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 75 | static void pre_flush_end_io(struct request *rq, int error) |
| 76 | { |
| 77 | elv_completed_request(rq->q, rq); |
Tejun Heo | 47f70d5 | 2010-09-03 11:56:17 +0200 | [diff] [blame^] | 78 | blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_PREFLUSH, error); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 81 | static void flush_data_end_io(struct request *rq, int error) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 82 | { |
| 83 | elv_completed_request(rq->q, rq); |
Tejun Heo | 47f70d5 | 2010-09-03 11:56:17 +0200 | [diff] [blame^] | 84 | blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_DATA, error); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void post_flush_end_io(struct request *rq, int error) |
| 88 | { |
| 89 | elv_completed_request(rq->q, rq); |
Tejun Heo | 47f70d5 | 2010-09-03 11:56:17 +0200 | [diff] [blame^] | 90 | blk_flush_complete_seq_end_io(rq->q, QUEUE_FSEQ_POSTFLUSH, error); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Christoph Hellwig | cde4c40 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 93 | static void init_flush_request(struct request *rq, struct gendisk *disk) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 94 | { |
FUJITA Tomonori | 28e18d0 | 2010-07-09 09:38:24 +0900 | [diff] [blame] | 95 | rq->cmd_type = REQ_TYPE_FS; |
Tejun Heo | 337238b | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 96 | rq->cmd_flags = WRITE_FLUSH; |
Christoph Hellwig | cde4c40 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 97 | rq->rq_disk = disk; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 100 | static struct request *queue_next_fseq(struct request_queue *q) |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 101 | { |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 102 | struct request *orig_rq = q->orig_flush_rq; |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 103 | struct request *rq = &q->flush_rq; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 104 | |
Christoph Hellwig | cde4c40 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 105 | blk_rq_init(q, rq); |
| 106 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 107 | switch (blk_flush_cur_seq(q)) { |
| 108 | case QUEUE_FSEQ_PREFLUSH: |
Christoph Hellwig | cde4c40 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 109 | init_flush_request(rq, orig_rq->rq_disk); |
| 110 | rq->end_io = pre_flush_end_io; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 111 | break; |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 112 | case QUEUE_FSEQ_DATA: |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 113 | init_request_from_bio(rq, orig_rq->bio); |
| 114 | rq->cmd_flags &= ~(REQ_FLUSH | REQ_FUA); |
| 115 | rq->cmd_flags |= orig_rq->cmd_flags & (REQ_FLUSH | REQ_FUA); |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 116 | rq->end_io = flush_data_end_io; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 117 | break; |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 118 | case QUEUE_FSEQ_POSTFLUSH: |
Christoph Hellwig | cde4c40 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 119 | init_flush_request(rq, orig_rq->rq_disk); |
| 120 | rq->end_io = post_flush_end_io; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 121 | break; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 122 | default: |
| 123 | BUG(); |
| 124 | } |
Christoph Hellwig | cde4c40 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 125 | |
| 126 | elv_insert(q, rq, ELEVATOR_INSERT_FRONT); |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 127 | return rq; |
| 128 | } |
| 129 | |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 130 | struct request *blk_do_flush(struct request_queue *q, struct request *rq) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 131 | { |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 132 | unsigned int fflags = q->flush_flags; /* may change, cache it */ |
| 133 | bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA; |
| 134 | bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH); |
| 135 | bool do_postflush = has_flush && !has_fua && (rq->cmd_flags & REQ_FUA); |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 136 | unsigned skip = 0; |
| 137 | |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Special case. If there's data but flush is not necessary, |
| 140 | * the request can be issued directly. |
| 141 | * |
| 142 | * Flush w/o data should be able to be issued directly too but |
| 143 | * currently some drivers assume that rq->bio contains |
| 144 | * non-zero data if it isn't NULL and empty FLUSH requests |
| 145 | * getting here usually have bio's without data. |
| 146 | */ |
| 147 | if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) { |
| 148 | rq->cmd_flags &= ~REQ_FLUSH; |
| 149 | if (!has_fua) |
| 150 | rq->cmd_flags &= ~REQ_FUA; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 151 | return rq; |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Sequenced flushes can't be processed in parallel. If |
| 156 | * another one is already in progress, queue for later |
| 157 | * processing. |
| 158 | */ |
| 159 | if (q->flush_seq) { |
| 160 | list_move_tail(&rq->queuelist, &q->pending_flushes); |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | /* |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 165 | * Start a new flush sequence |
Tejun Heo | 28e7d18 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 166 | */ |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 167 | q->flush_err = 0; |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 168 | q->flush_seq |= QUEUE_FSEQ_STARTED; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 169 | |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 170 | /* adjust FLUSH/FUA of the original request and stash it away */ |
| 171 | rq->cmd_flags &= ~REQ_FLUSH; |
| 172 | if (!has_fua) |
| 173 | rq->cmd_flags &= ~REQ_FUA; |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 174 | blk_dequeue_request(rq); |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 175 | q->orig_flush_rq = rq; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 176 | |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 177 | /* skip unneded sequences and return the first one */ |
| 178 | if (!do_preflush) |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 179 | skip |= QUEUE_FSEQ_PREFLUSH; |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 180 | if (!blk_rq_sectors(rq)) |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 181 | skip |= QUEUE_FSEQ_DATA; |
Tejun Heo | 4fed947 | 2010-09-03 11:56:17 +0200 | [diff] [blame] | 182 | if (!do_postflush) |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 183 | skip |= QUEUE_FSEQ_POSTFLUSH; |
Tejun Heo | dd4c133 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 184 | return blk_flush_complete_seq(q, skip, 0); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void bio_end_empty_barrier(struct bio *bio, int err) |
| 188 | { |
Jens Axboe | cc66b45 | 2008-03-04 11:47:46 +0100 | [diff] [blame] | 189 | if (err) { |
| 190 | if (err == -EOPNOTSUPP) |
| 191 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 192 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Jens Axboe | cc66b45 | 2008-03-04 11:47:46 +0100 | [diff] [blame] | 193 | } |
Dmitry Monakhov | f17e232 | 2010-04-28 17:55:07 +0400 | [diff] [blame] | 194 | if (bio->bi_private) |
| 195 | complete(bio->bi_private); |
| 196 | bio_put(bio); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * blkdev_issue_flush - queue a flush |
| 201 | * @bdev: blockdev to issue flush for |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 202 | * @gfp_mask: memory allocation flags (for bio_alloc) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 203 | * @error_sector: error sector |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 204 | * @flags: BLKDEV_IFL_* flags to control behaviour |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 205 | * |
| 206 | * Description: |
| 207 | * Issue a flush for the block device in question. Caller can supply |
| 208 | * room for storing the error offset in case of a flush error, if they |
Dmitry Monakhov | f17e232 | 2010-04-28 17:55:07 +0400 | [diff] [blame] | 209 | * wish to. If WAIT flag is not passed then caller may check only what |
| 210 | * request was pushed in some internal queue for later handling. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 211 | */ |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 212 | int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask, |
| 213 | sector_t *error_sector, unsigned long flags) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 214 | { |
| 215 | DECLARE_COMPLETION_ONSTACK(wait); |
| 216 | struct request_queue *q; |
| 217 | struct bio *bio; |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 218 | int ret = 0; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 219 | |
| 220 | if (bdev->bd_disk == NULL) |
| 221 | return -ENXIO; |
| 222 | |
| 223 | q = bdev_get_queue(bdev); |
| 224 | if (!q) |
| 225 | return -ENXIO; |
| 226 | |
Dave Chinner | f10d9f6 | 2010-07-13 17:50:50 +1000 | [diff] [blame] | 227 | /* |
| 228 | * some block devices may not have their queue correctly set up here |
| 229 | * (e.g. loop device without a backing file) and so issuing a flush |
| 230 | * here will panic. Ensure there is a request function before issuing |
| 231 | * the barrier. |
| 232 | */ |
| 233 | if (!q->make_request_fn) |
| 234 | return -ENXIO; |
| 235 | |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 236 | bio = bio_alloc(gfp_mask, 0); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 237 | bio->bi_end_io = bio_end_empty_barrier; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 238 | bio->bi_bdev = bdev; |
Dmitry Monakhov | f17e232 | 2010-04-28 17:55:07 +0400 | [diff] [blame] | 239 | if (test_bit(BLKDEV_WAIT, &flags)) |
| 240 | bio->bi_private = &wait; |
| 241 | |
| 242 | bio_get(bio); |
OGAWA Hirofumi | 2ebca85 | 2008-08-11 17:07:08 +0100 | [diff] [blame] | 243 | submit_bio(WRITE_BARRIER, bio); |
Dmitry Monakhov | f17e232 | 2010-04-28 17:55:07 +0400 | [diff] [blame] | 244 | if (test_bit(BLKDEV_WAIT, &flags)) { |
| 245 | wait_for_completion(&wait); |
| 246 | /* |
| 247 | * The driver must store the error location in ->bi_sector, if |
| 248 | * it supports it. For non-stacked drivers, this should be |
| 249 | * copied from blk_rq_pos(rq). |
| 250 | */ |
| 251 | if (error_sector) |
| 252 | *error_sector = bio->bi_sector; |
| 253 | } |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 254 | |
Jens Axboe | cc66b45 | 2008-03-04 11:47:46 +0100 | [diff] [blame] | 255 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 256 | ret = -EOPNOTSUPP; |
| 257 | else if (!bio_flagged(bio, BIO_UPTODATE)) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 258 | ret = -EIO; |
| 259 | |
| 260 | bio_put(bio); |
| 261 | return ret; |
| 262 | } |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 263 | EXPORT_SYMBOL(blkdev_issue_flush); |