Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions related to barrier IO handling |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/bio.h> |
| 7 | #include <linux/blkdev.h> |
| 8 | |
| 9 | #include "blk.h" |
| 10 | |
| 11 | /** |
| 12 | * blk_queue_ordered - does this queue support ordered writes |
| 13 | * @q: the request queue |
| 14 | * @ordered: one of QUEUE_ORDERED_* |
| 15 | * @prepare_flush_fn: rq setup helper for cache flush ordered writes |
| 16 | * |
| 17 | * Description: |
| 18 | * For journalled file systems, doing ordered writes on a commit |
| 19 | * block instead of explicitly doing wait_on_buffer (which is bad |
| 20 | * for performance) can be a big win. Block drivers supporting this |
| 21 | * feature should call this function and indicate so. |
| 22 | * |
| 23 | **/ |
| 24 | int blk_queue_ordered(struct request_queue *q, unsigned ordered, |
| 25 | prepare_flush_fn *prepare_flush_fn) |
| 26 | { |
Tejun Heo | 313e429 | 2008-11-28 13:32:02 +0900 | [diff] [blame] | 27 | if (!prepare_flush_fn && (ordered & (QUEUE_ORDERED_DO_PREFLUSH | |
| 28 | QUEUE_ORDERED_DO_POSTFLUSH))) { |
Harvey Harrison | 24c03d4 | 2008-05-01 04:35:17 -0700 | [diff] [blame] | 29 | printk(KERN_ERR "%s: prepare_flush_fn required\n", __func__); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 30 | return -EINVAL; |
| 31 | } |
| 32 | |
| 33 | if (ordered != QUEUE_ORDERED_NONE && |
| 34 | ordered != QUEUE_ORDERED_DRAIN && |
| 35 | ordered != QUEUE_ORDERED_DRAIN_FLUSH && |
| 36 | ordered != QUEUE_ORDERED_DRAIN_FUA && |
| 37 | ordered != QUEUE_ORDERED_TAG && |
| 38 | ordered != QUEUE_ORDERED_TAG_FLUSH && |
| 39 | ordered != QUEUE_ORDERED_TAG_FUA) { |
| 40 | printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered); |
| 41 | return -EINVAL; |
| 42 | } |
| 43 | |
| 44 | q->ordered = ordered; |
| 45 | q->next_ordered = ordered; |
| 46 | q->prepare_flush_fn = prepare_flush_fn; |
| 47 | |
| 48 | return 0; |
| 49 | } |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 50 | EXPORT_SYMBOL(blk_queue_ordered); |
| 51 | |
| 52 | /* |
| 53 | * Cache flushing for ordered writes handling |
| 54 | */ |
Adrian Bunk | 6f6a036 | 2008-04-29 09:49:06 +0200 | [diff] [blame] | 55 | unsigned blk_ordered_cur_seq(struct request_queue *q) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 56 | { |
| 57 | if (!q->ordseq) |
| 58 | return 0; |
| 59 | return 1 << ffz(q->ordseq); |
| 60 | } |
| 61 | |
| 62 | unsigned blk_ordered_req_seq(struct request *rq) |
| 63 | { |
| 64 | struct request_queue *q = rq->q; |
| 65 | |
| 66 | BUG_ON(q->ordseq == 0); |
| 67 | |
| 68 | if (rq == &q->pre_flush_rq) |
| 69 | return QUEUE_ORDSEQ_PREFLUSH; |
| 70 | if (rq == &q->bar_rq) |
| 71 | return QUEUE_ORDSEQ_BAR; |
| 72 | if (rq == &q->post_flush_rq) |
| 73 | return QUEUE_ORDSEQ_POSTFLUSH; |
| 74 | |
| 75 | /* |
| 76 | * !fs requests don't need to follow barrier ordering. Always |
| 77 | * put them at the front. This fixes the following deadlock. |
| 78 | * |
| 79 | * http://thread.gmane.org/gmane.linux.kernel/537473 |
| 80 | */ |
| 81 | if (!blk_fs_request(rq)) |
| 82 | return QUEUE_ORDSEQ_DRAIN; |
| 83 | |
| 84 | if ((rq->cmd_flags & REQ_ORDERED_COLOR) == |
| 85 | (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR)) |
| 86 | return QUEUE_ORDSEQ_DRAIN; |
| 87 | else |
| 88 | return QUEUE_ORDSEQ_DONE; |
| 89 | } |
| 90 | |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 91 | bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 92 | { |
| 93 | struct request *rq; |
| 94 | |
| 95 | if (error && !q->orderr) |
| 96 | q->orderr = error; |
| 97 | |
| 98 | BUG_ON(q->ordseq & seq); |
| 99 | q->ordseq |= seq; |
| 100 | |
| 101 | if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE) |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 102 | return false; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Okay, sequence complete. |
| 106 | */ |
| 107 | q->ordseq = 0; |
| 108 | rq = q->orig_bar_rq; |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame^] | 109 | __blk_end_request_all(rq, q->orderr); |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 110 | return true; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void pre_flush_end_io(struct request *rq, int error) |
| 114 | { |
| 115 | elv_completed_request(rq->q, rq); |
| 116 | blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error); |
| 117 | } |
| 118 | |
| 119 | static void bar_end_io(struct request *rq, int error) |
| 120 | { |
| 121 | elv_completed_request(rq->q, rq); |
| 122 | blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error); |
| 123 | } |
| 124 | |
| 125 | static void post_flush_end_io(struct request *rq, int error) |
| 126 | { |
| 127 | elv_completed_request(rq->q, rq); |
| 128 | blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error); |
| 129 | } |
| 130 | |
| 131 | static void queue_flush(struct request_queue *q, unsigned which) |
| 132 | { |
| 133 | struct request *rq; |
| 134 | rq_end_io_fn *end_io; |
| 135 | |
Tejun Heo | 313e429 | 2008-11-28 13:32:02 +0900 | [diff] [blame] | 136 | if (which == QUEUE_ORDERED_DO_PREFLUSH) { |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 137 | rq = &q->pre_flush_rq; |
| 138 | end_io = pre_flush_end_io; |
| 139 | } else { |
| 140 | rq = &q->post_flush_rq; |
| 141 | end_io = post_flush_end_io; |
| 142 | } |
| 143 | |
FUJITA Tomonori | 2a4aa30 | 2008-04-29 09:54:36 +0200 | [diff] [blame] | 144 | blk_rq_init(q, rq); |
FUJITA Tomonori | 1afb20f | 2008-04-25 12:26:28 +0200 | [diff] [blame] | 145 | rq->cmd_flags = REQ_HARDBARRIER; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 146 | rq->rq_disk = q->bar_rq.rq_disk; |
| 147 | rq->end_io = end_io; |
| 148 | q->prepare_flush_fn(q, rq); |
| 149 | |
| 150 | elv_insert(q, rq, ELEVATOR_INSERT_FRONT); |
| 151 | } |
| 152 | |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 153 | static inline bool start_ordered(struct request_queue *q, struct request **rqp) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 154 | { |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 155 | struct request *rq = *rqp; |
| 156 | unsigned skip = 0; |
| 157 | |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 158 | q->orderr = 0; |
| 159 | q->ordered = q->next_ordered; |
| 160 | q->ordseq |= QUEUE_ORDSEQ_STARTED; |
| 161 | |
Tejun Heo | 58eea92 | 2008-11-28 13:32:06 +0900 | [diff] [blame] | 162 | /* |
| 163 | * For an empty barrier, there's no actual BAR request, which |
| 164 | * in turn makes POSTFLUSH unnecessary. Mask them off. |
| 165 | */ |
Tejun Heo | a185eb4 | 2008-11-28 13:32:07 +0900 | [diff] [blame] | 166 | if (!rq->hard_nr_sectors) { |
Tejun Heo | 58eea92 | 2008-11-28 13:32:06 +0900 | [diff] [blame] | 167 | q->ordered &= ~(QUEUE_ORDERED_DO_BAR | |
| 168 | QUEUE_ORDERED_DO_POSTFLUSH); |
Tejun Heo | a185eb4 | 2008-11-28 13:32:07 +0900 | [diff] [blame] | 169 | /* |
| 170 | * Empty barrier on a write-through device w/ ordered |
| 171 | * tag has no command to issue and without any command |
| 172 | * to issue, ordering by tag can't be used. Drain |
| 173 | * instead. |
| 174 | */ |
| 175 | if ((q->ordered & QUEUE_ORDERED_BY_TAG) && |
| 176 | !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) { |
| 177 | q->ordered &= ~QUEUE_ORDERED_BY_TAG; |
| 178 | q->ordered |= QUEUE_ORDERED_BY_DRAIN; |
| 179 | } |
| 180 | } |
Tejun Heo | 58eea92 | 2008-11-28 13:32:06 +0900 | [diff] [blame] | 181 | |
Tejun Heo | f671620 | 2008-11-28 13:32:04 +0900 | [diff] [blame] | 182 | /* stash away the original request */ |
Tejun Heo | 53a0880 | 2008-12-03 12:41:26 +0100 | [diff] [blame] | 183 | elv_dequeue_request(q, rq); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 184 | q->orig_bar_rq = rq; |
Tejun Heo | f671620 | 2008-11-28 13:32:04 +0900 | [diff] [blame] | 185 | rq = NULL; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Queue ordered sequence. As we stack them at the head, we |
| 189 | * need to queue in reverse order. Note that we rely on that |
| 190 | * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs |
Tejun Heo | 58eea92 | 2008-11-28 13:32:06 +0900 | [diff] [blame] | 191 | * request gets inbetween ordered sequence. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 192 | */ |
Tejun Heo | 58eea92 | 2008-11-28 13:32:06 +0900 | [diff] [blame] | 193 | if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) { |
Tejun Heo | 313e429 | 2008-11-28 13:32:02 +0900 | [diff] [blame] | 194 | queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH); |
Tejun Heo | f671620 | 2008-11-28 13:32:04 +0900 | [diff] [blame] | 195 | rq = &q->post_flush_rq; |
| 196 | } else |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 197 | skip |= QUEUE_ORDSEQ_POSTFLUSH; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 198 | |
Tejun Heo | f671620 | 2008-11-28 13:32:04 +0900 | [diff] [blame] | 199 | if (q->ordered & QUEUE_ORDERED_DO_BAR) { |
| 200 | rq = &q->bar_rq; |
| 201 | |
| 202 | /* initialize proxy request and queue it */ |
| 203 | blk_rq_init(q, rq); |
| 204 | if (bio_data_dir(q->orig_bar_rq->bio) == WRITE) |
| 205 | rq->cmd_flags |= REQ_RW; |
| 206 | if (q->ordered & QUEUE_ORDERED_DO_FUA) |
| 207 | rq->cmd_flags |= REQ_FUA; |
| 208 | init_request_from_bio(rq, q->orig_bar_rq->bio); |
| 209 | rq->end_io = bar_end_io; |
| 210 | |
| 211 | elv_insert(q, rq, ELEVATOR_INSERT_FRONT); |
| 212 | } else |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 213 | skip |= QUEUE_ORDSEQ_BAR; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 214 | |
Tejun Heo | 313e429 | 2008-11-28 13:32:02 +0900 | [diff] [blame] | 215 | if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) { |
| 216 | queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 217 | rq = &q->pre_flush_rq; |
| 218 | } else |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 219 | skip |= QUEUE_ORDSEQ_PREFLUSH; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 220 | |
Tejun Heo | f671620 | 2008-11-28 13:32:04 +0900 | [diff] [blame] | 221 | if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && q->in_flight) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 222 | rq = NULL; |
Tejun Heo | f671620 | 2008-11-28 13:32:04 +0900 | [diff] [blame] | 223 | else |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 224 | skip |= QUEUE_ORDSEQ_DRAIN; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 225 | |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 226 | *rqp = rq; |
| 227 | |
| 228 | /* |
| 229 | * Complete skipped sequences. If whole sequence is complete, |
| 230 | * return false to tell elevator that this request is gone. |
| 231 | */ |
| 232 | return !blk_ordered_complete_seq(q, skip, 0); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 235 | bool blk_do_ordered(struct request_queue *q, struct request **rqp) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 236 | { |
| 237 | struct request *rq = *rqp; |
| 238 | const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq); |
| 239 | |
| 240 | if (!q->ordseq) { |
| 241 | if (!is_barrier) |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 242 | return true; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 243 | |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 244 | if (q->next_ordered != QUEUE_ORDERED_NONE) |
| 245 | return start_ordered(q, rqp); |
| 246 | else { |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 247 | /* |
Tejun Heo | a738467 | 2008-11-28 13:32:03 +0900 | [diff] [blame] | 248 | * Queue ordering not supported. Terminate |
| 249 | * with prejudice. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 250 | */ |
Tejun Heo | 53a0880 | 2008-12-03 12:41:26 +0100 | [diff] [blame] | 251 | elv_dequeue_request(q, rq); |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame^] | 252 | __blk_end_request_all(rq, -EOPNOTSUPP); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 253 | *rqp = NULL; |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 254 | return false; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Ordered sequence in progress |
| 260 | */ |
| 261 | |
| 262 | /* Special requests are not subject to ordering rules. */ |
| 263 | if (!blk_fs_request(rq) && |
| 264 | rq != &q->pre_flush_rq && rq != &q->post_flush_rq) |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 265 | return true; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 266 | |
Tejun Heo | 313e429 | 2008-11-28 13:32:02 +0900 | [diff] [blame] | 267 | if (q->ordered & QUEUE_ORDERED_BY_TAG) { |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 268 | /* Ordered by tag. Blocking the next barrier is enough. */ |
| 269 | if (is_barrier && rq != &q->bar_rq) |
| 270 | *rqp = NULL; |
| 271 | } else { |
| 272 | /* Ordered by draining. Wait for turn. */ |
| 273 | WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q)); |
| 274 | if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q)) |
| 275 | *rqp = NULL; |
| 276 | } |
| 277 | |
Tejun Heo | 8f11b3e | 2008-11-28 13:32:05 +0900 | [diff] [blame] | 278 | return true; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void bio_end_empty_barrier(struct bio *bio, int err) |
| 282 | { |
Jens Axboe | cc66b45 | 2008-03-04 11:47:46 +0100 | [diff] [blame] | 283 | if (err) { |
| 284 | if (err == -EOPNOTSUPP) |
| 285 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 286 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Jens Axboe | cc66b45 | 2008-03-04 11:47:46 +0100 | [diff] [blame] | 287 | } |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 288 | |
| 289 | complete(bio->bi_private); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * blkdev_issue_flush - queue a flush |
| 294 | * @bdev: blockdev to issue flush for |
| 295 | * @error_sector: error sector |
| 296 | * |
| 297 | * Description: |
| 298 | * Issue a flush for the block device in question. Caller can supply |
| 299 | * room for storing the error offset in case of a flush error, if they |
Theodore Ts'o | dbdac9b | 2009-01-13 15:27:32 +0100 | [diff] [blame] | 300 | * wish to. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 301 | */ |
| 302 | int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector) |
| 303 | { |
| 304 | DECLARE_COMPLETION_ONSTACK(wait); |
| 305 | struct request_queue *q; |
| 306 | struct bio *bio; |
| 307 | int ret; |
| 308 | |
| 309 | if (bdev->bd_disk == NULL) |
| 310 | return -ENXIO; |
| 311 | |
| 312 | q = bdev_get_queue(bdev); |
| 313 | if (!q) |
| 314 | return -ENXIO; |
| 315 | |
| 316 | bio = bio_alloc(GFP_KERNEL, 0); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 317 | bio->bi_end_io = bio_end_empty_barrier; |
| 318 | bio->bi_private = &wait; |
| 319 | bio->bi_bdev = bdev; |
OGAWA Hirofumi | 2ebca85 | 2008-08-11 17:07:08 +0100 | [diff] [blame] | 320 | submit_bio(WRITE_BARRIER, bio); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 321 | |
| 322 | wait_for_completion(&wait); |
| 323 | |
| 324 | /* |
| 325 | * The driver must store the error location in ->bi_sector, if |
| 326 | * it supports it. For non-stacked drivers, this should be copied |
| 327 | * from rq->sector. |
| 328 | */ |
| 329 | if (error_sector) |
| 330 | *error_sector = bio->bi_sector; |
| 331 | |
| 332 | ret = 0; |
Jens Axboe | cc66b45 | 2008-03-04 11:47:46 +0100 | [diff] [blame] | 333 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 334 | ret = -EOPNOTSUPP; |
| 335 | else if (!bio_flagged(bio, BIO_UPTODATE)) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 336 | ret = -EIO; |
| 337 | |
| 338 | bio_put(bio); |
| 339 | return ret; |
| 340 | } |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 341 | EXPORT_SYMBOL(blkdev_issue_flush); |
David Woodhouse | fb2dce8 | 2008-08-05 18:01:53 +0100 | [diff] [blame] | 342 | |
| 343 | static void blkdev_discard_end_io(struct bio *bio, int err) |
| 344 | { |
| 345 | if (err) { |
| 346 | if (err == -EOPNOTSUPP) |
| 347 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); |
| 348 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
| 349 | } |
| 350 | |
| 351 | bio_put(bio); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * blkdev_issue_discard - queue a discard |
| 356 | * @bdev: blockdev to issue discard for |
| 357 | * @sector: start sector |
| 358 | * @nr_sects: number of sectors to discard |
Hugh Dickins | 3e6053d | 2008-09-11 10:57:55 +0200 | [diff] [blame] | 359 | * @gfp_mask: memory allocation flags (for bio_alloc) |
David Woodhouse | fb2dce8 | 2008-08-05 18:01:53 +0100 | [diff] [blame] | 360 | * |
| 361 | * Description: |
| 362 | * Issue a discard request for the sectors in question. Does not wait. |
| 363 | */ |
Hugh Dickins | 3e6053d | 2008-09-11 10:57:55 +0200 | [diff] [blame] | 364 | int blkdev_issue_discard(struct block_device *bdev, |
| 365 | sector_t sector, sector_t nr_sects, gfp_t gfp_mask) |
David Woodhouse | fb2dce8 | 2008-08-05 18:01:53 +0100 | [diff] [blame] | 366 | { |
| 367 | struct request_queue *q; |
| 368 | struct bio *bio; |
| 369 | int ret = 0; |
| 370 | |
| 371 | if (bdev->bd_disk == NULL) |
| 372 | return -ENXIO; |
| 373 | |
| 374 | q = bdev_get_queue(bdev); |
| 375 | if (!q) |
| 376 | return -ENXIO; |
| 377 | |
| 378 | if (!q->prepare_discard_fn) |
| 379 | return -EOPNOTSUPP; |
| 380 | |
| 381 | while (nr_sects && !ret) { |
Hugh Dickins | 3e6053d | 2008-09-11 10:57:55 +0200 | [diff] [blame] | 382 | bio = bio_alloc(gfp_mask, 0); |
David Woodhouse | fb2dce8 | 2008-08-05 18:01:53 +0100 | [diff] [blame] | 383 | if (!bio) |
| 384 | return -ENOMEM; |
| 385 | |
| 386 | bio->bi_end_io = blkdev_discard_end_io; |
| 387 | bio->bi_bdev = bdev; |
| 388 | |
| 389 | bio->bi_sector = sector; |
| 390 | |
| 391 | if (nr_sects > q->max_hw_sectors) { |
| 392 | bio->bi_size = q->max_hw_sectors << 9; |
| 393 | nr_sects -= q->max_hw_sectors; |
| 394 | sector += q->max_hw_sectors; |
| 395 | } else { |
| 396 | bio->bi_size = nr_sects << 9; |
| 397 | nr_sects = 0; |
| 398 | } |
| 399 | bio_get(bio); |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 400 | submit_bio(DISCARD_BARRIER, bio); |
David Woodhouse | fb2dce8 | 2008-08-05 18:01:53 +0100 | [diff] [blame] | 401 | |
| 402 | /* Check if it failed immediately */ |
| 403 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 404 | ret = -EOPNOTSUPP; |
| 405 | else if (!bio_flagged(bio, BIO_UPTODATE)) |
| 406 | ret = -EIO; |
| 407 | bio_put(bio); |
| 408 | } |
| 409 | return ret; |
| 410 | } |
| 411 | EXPORT_SYMBOL(blkdev_issue_discard); |