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