Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions related to segment and merge handling |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/bio.h> |
| 7 | #include <linux/blkdev.h> |
| 8 | #include <linux/scatterlist.h> |
| 9 | |
| 10 | #include "blk.h" |
| 11 | |
| 12 | void blk_recalc_rq_sectors(struct request *rq, int nsect) |
| 13 | { |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 14 | if (blk_fs_request(rq) || blk_discard_rq(rq)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 15 | rq->hard_sector += nsect; |
| 16 | rq->hard_nr_sectors -= nsect; |
| 17 | |
| 18 | /* |
| 19 | * Move the I/O submission pointers ahead if required. |
| 20 | */ |
| 21 | if ((rq->nr_sectors >= rq->hard_nr_sectors) && |
| 22 | (rq->sector <= rq->hard_sector)) { |
| 23 | rq->sector = rq->hard_sector; |
| 24 | rq->nr_sectors = rq->hard_nr_sectors; |
| 25 | rq->hard_cur_sectors = bio_cur_sectors(rq->bio); |
| 26 | rq->current_nr_sectors = rq->hard_cur_sectors; |
| 27 | rq->buffer = bio_data(rq->bio); |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * if total number of sectors is less than the first segment |
| 32 | * size, something has gone terribly wrong |
| 33 | */ |
| 34 | if (rq->nr_sectors < rq->current_nr_sectors) { |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 35 | printk(KERN_ERR "blk: request botched\n"); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 36 | rq->nr_sectors = rq->current_nr_sectors; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void blk_recalc_rq_segments(struct request *rq) |
| 42 | { |
| 43 | int nr_phys_segs; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 44 | unsigned int phys_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 45 | struct bio_vec *bv, *bvprv = NULL; |
| 46 | int seg_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 47 | int cluster; |
| 48 | struct req_iterator iter; |
| 49 | int high, highprv = 1; |
| 50 | struct request_queue *q = rq->q; |
| 51 | |
| 52 | if (!rq->bio) |
| 53 | return; |
| 54 | |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 55 | cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags); |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 56 | seg_size = 0; |
| 57 | phys_size = nr_phys_segs = 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 58 | rq_for_each_segment(bv, rq, iter) { |
| 59 | /* |
| 60 | * the trick here is making sure that a high page is never |
| 61 | * considered part of another segment, since that might |
| 62 | * change with the bounce page. |
| 63 | */ |
| 64 | high = page_to_pfn(bv->bv_page) > q->bounce_pfn; |
| 65 | if (high || highprv) |
Mikulas Patocka | b8b3e16 | 2008-08-15 10:15:19 +0200 | [diff] [blame] | 66 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 67 | if (cluster) { |
| 68 | if (seg_size + bv->bv_len > q->max_segment_size) |
| 69 | goto new_segment; |
| 70 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv)) |
| 71 | goto new_segment; |
| 72 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv)) |
| 73 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 74 | |
| 75 | seg_size += bv->bv_len; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 76 | bvprv = bv; |
| 77 | continue; |
| 78 | } |
| 79 | new_segment: |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 80 | nr_phys_segs++; |
| 81 | bvprv = bv; |
| 82 | seg_size = bv->bv_len; |
| 83 | highprv = high; |
| 84 | } |
| 85 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 86 | rq->nr_phys_segments = nr_phys_segs; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void blk_recount_segments(struct request_queue *q, struct bio *bio) |
| 90 | { |
| 91 | struct request rq; |
| 92 | struct bio *nxt = bio->bi_next; |
| 93 | rq.q = q; |
| 94 | rq.bio = rq.biotail = bio; |
| 95 | bio->bi_next = NULL; |
| 96 | blk_recalc_rq_segments(&rq); |
| 97 | bio->bi_next = nxt; |
| 98 | bio->bi_phys_segments = rq.nr_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 99 | bio->bi_flags |= (1 << BIO_SEG_VALID); |
| 100 | } |
| 101 | EXPORT_SYMBOL(blk_recount_segments); |
| 102 | |
| 103 | static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, |
| 104 | struct bio *nxt) |
| 105 | { |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 106 | if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 107 | return 0; |
| 108 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 109 | if (bio->bi_size + nxt->bi_size > q->max_segment_size) |
| 110 | return 0; |
| 111 | |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 112 | if (!bio_has_data(bio)) |
| 113 | return 1; |
| 114 | |
| 115 | if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt))) |
| 116 | return 0; |
| 117 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 118 | /* |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 119 | * bio and nxt are contiguous in memory; check if the queue allows |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 120 | * these two to be merged into one |
| 121 | */ |
| 122 | if (BIO_SEG_BOUNDARY(q, bio, nxt)) |
| 123 | return 1; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 128 | /* |
| 129 | * map a request to scatterlist, return number of sg entries setup. Caller |
| 130 | * must make sure sg can hold rq->nr_phys_segments entries |
| 131 | */ |
| 132 | int blk_rq_map_sg(struct request_queue *q, struct request *rq, |
| 133 | struct scatterlist *sglist) |
| 134 | { |
| 135 | struct bio_vec *bvec, *bvprv; |
| 136 | struct req_iterator iter; |
| 137 | struct scatterlist *sg; |
| 138 | int nsegs, cluster; |
| 139 | |
| 140 | nsegs = 0; |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 141 | cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * for each bio in rq |
| 145 | */ |
| 146 | bvprv = NULL; |
| 147 | sg = NULL; |
| 148 | rq_for_each_segment(bvec, rq, iter) { |
| 149 | int nbytes = bvec->bv_len; |
| 150 | |
| 151 | if (bvprv && cluster) { |
| 152 | if (sg->length + nbytes > q->max_segment_size) |
| 153 | goto new_segment; |
| 154 | |
| 155 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) |
| 156 | goto new_segment; |
| 157 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) |
| 158 | goto new_segment; |
| 159 | |
| 160 | sg->length += nbytes; |
| 161 | } else { |
| 162 | new_segment: |
| 163 | if (!sg) |
| 164 | sg = sglist; |
| 165 | else { |
| 166 | /* |
| 167 | * If the driver previously mapped a shorter |
| 168 | * list, we could see a termination bit |
| 169 | * prematurely unless it fully inits the sg |
| 170 | * table on each mapping. We KNOW that there |
| 171 | * must be more entries here or the driver |
| 172 | * would be buggy, so force clear the |
| 173 | * termination bit to avoid doing a full |
| 174 | * sg_init_table() in drivers for each command. |
| 175 | */ |
| 176 | sg->page_link &= ~0x02; |
| 177 | sg = sg_next(sg); |
| 178 | } |
| 179 | |
| 180 | sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset); |
| 181 | nsegs++; |
| 182 | } |
| 183 | bvprv = bvec; |
| 184 | } /* segments in rq */ |
| 185 | |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 186 | |
| 187 | if (unlikely(rq->cmd_flags & REQ_COPY_USER) && |
| 188 | (rq->data_len & q->dma_pad_mask)) { |
| 189 | unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1; |
| 190 | |
| 191 | sg->length += pad_len; |
| 192 | rq->extra_len += pad_len; |
| 193 | } |
| 194 | |
Tejun Heo | 2fb98e8 | 2008-02-19 11:36:53 +0100 | [diff] [blame] | 195 | if (q->dma_drain_size && q->dma_drain_needed(rq)) { |
Tejun Heo | db0a2e0 | 2008-02-19 11:36:55 +0100 | [diff] [blame] | 196 | if (rq->cmd_flags & REQ_RW) |
| 197 | memset(q->dma_drain_buffer, 0, q->dma_drain_size); |
| 198 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 199 | sg->page_link &= ~0x02; |
| 200 | sg = sg_next(sg); |
| 201 | sg_set_page(sg, virt_to_page(q->dma_drain_buffer), |
| 202 | q->dma_drain_size, |
| 203 | ((unsigned long)q->dma_drain_buffer) & |
| 204 | (PAGE_SIZE - 1)); |
| 205 | nsegs++; |
FUJITA Tomonori | 7a85f88 | 2008-03-04 11:17:11 +0100 | [diff] [blame] | 206 | rq->extra_len += q->dma_drain_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (sg) |
| 210 | sg_mark_end(sg); |
| 211 | |
| 212 | return nsegs; |
| 213 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 214 | EXPORT_SYMBOL(blk_rq_map_sg); |
| 215 | |
| 216 | static inline int ll_new_mergeable(struct request_queue *q, |
| 217 | struct request *req, |
| 218 | struct bio *bio) |
| 219 | { |
| 220 | int nr_phys_segs = bio_phys_segments(q, bio); |
| 221 | |
| 222 | if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { |
| 223 | req->cmd_flags |= REQ_NOMERGE; |
| 224 | if (req == q->last_merge) |
| 225 | q->last_merge = NULL; |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * A hw segment is just getting larger, bump just the phys |
| 231 | * counter. |
| 232 | */ |
| 233 | req->nr_phys_segments += nr_phys_segs; |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | static inline int ll_new_hw_segment(struct request_queue *q, |
| 238 | struct request *req, |
| 239 | struct bio *bio) |
| 240 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 241 | int nr_phys_segs = bio_phys_segments(q, bio); |
| 242 | |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 243 | if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 244 | || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { |
| 245 | req->cmd_flags |= REQ_NOMERGE; |
| 246 | if (req == q->last_merge) |
| 247 | q->last_merge = NULL; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * This will form the start of a new hw segment. Bump both |
| 253 | * counters. |
| 254 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 255 | req->nr_phys_segments += nr_phys_segs; |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | int ll_back_merge_fn(struct request_queue *q, struct request *req, |
| 260 | struct bio *bio) |
| 261 | { |
| 262 | unsigned short max_sectors; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 263 | |
| 264 | if (unlikely(blk_pc_request(req))) |
| 265 | max_sectors = q->max_hw_sectors; |
| 266 | else |
| 267 | max_sectors = q->max_sectors; |
| 268 | |
| 269 | if (req->nr_sectors + bio_sectors(bio) > max_sectors) { |
| 270 | req->cmd_flags |= REQ_NOMERGE; |
| 271 | if (req == q->last_merge) |
| 272 | q->last_merge = NULL; |
| 273 | return 0; |
| 274 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 275 | if (!bio_flagged(req->biotail, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 276 | blk_recount_segments(q, req->biotail); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 277 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 278 | blk_recount_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 279 | |
| 280 | return ll_new_hw_segment(q, req, bio); |
| 281 | } |
| 282 | |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 283 | int ll_front_merge_fn(struct request_queue *q, struct request *req, |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 284 | struct bio *bio) |
| 285 | { |
| 286 | unsigned short max_sectors; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 287 | |
| 288 | if (unlikely(blk_pc_request(req))) |
| 289 | max_sectors = q->max_hw_sectors; |
| 290 | else |
| 291 | max_sectors = q->max_sectors; |
| 292 | |
| 293 | |
| 294 | if (req->nr_sectors + bio_sectors(bio) > max_sectors) { |
| 295 | req->cmd_flags |= REQ_NOMERGE; |
| 296 | if (req == q->last_merge) |
| 297 | q->last_merge = NULL; |
| 298 | return 0; |
| 299 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 300 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 301 | blk_recount_segments(q, bio); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 302 | if (!bio_flagged(req->bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 303 | blk_recount_segments(q, req->bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 304 | |
| 305 | return ll_new_hw_segment(q, req, bio); |
| 306 | } |
| 307 | |
| 308 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, |
| 309 | struct request *next) |
| 310 | { |
| 311 | int total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * First check if the either of the requests are re-queued |
| 315 | * requests. Can't merge them if they are. |
| 316 | */ |
| 317 | if (req->special || next->special) |
| 318 | return 0; |
| 319 | |
| 320 | /* |
| 321 | * Will it become too large? |
| 322 | */ |
| 323 | if ((req->nr_sectors + next->nr_sectors) > q->max_sectors) |
| 324 | return 0; |
| 325 | |
| 326 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; |
| 327 | if (blk_phys_contig_segment(q, req->biotail, next->bio)) |
| 328 | total_phys_segments--; |
| 329 | |
| 330 | if (total_phys_segments > q->max_phys_segments) |
| 331 | return 0; |
| 332 | |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 333 | if (total_phys_segments > q->max_hw_segments) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 334 | return 0; |
| 335 | |
| 336 | /* Merge is OK... */ |
| 337 | req->nr_phys_segments = total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Has to be called with the request spinlock acquired |
| 343 | */ |
| 344 | static int attempt_merge(struct request_queue *q, struct request *req, |
| 345 | struct request *next) |
| 346 | { |
| 347 | if (!rq_mergeable(req) || !rq_mergeable(next)) |
| 348 | return 0; |
| 349 | |
| 350 | /* |
| 351 | * not contiguous |
| 352 | */ |
| 353 | if (req->sector + req->nr_sectors != next->sector) |
| 354 | return 0; |
| 355 | |
| 356 | if (rq_data_dir(req) != rq_data_dir(next) |
| 357 | || req->rq_disk != next->rq_disk |
| 358 | || next->special) |
| 359 | return 0; |
| 360 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 361 | if (blk_integrity_rq(req) != blk_integrity_rq(next)) |
| 362 | return 0; |
| 363 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 364 | /* |
| 365 | * If we are allowed to merge, then append bio list |
| 366 | * from next to rq and release next. merge_requests_fn |
| 367 | * will have updated segment counts, update sector |
| 368 | * counts here. |
| 369 | */ |
| 370 | if (!ll_merge_requests_fn(q, req, next)) |
| 371 | return 0; |
| 372 | |
| 373 | /* |
| 374 | * At this point we have either done a back merge |
| 375 | * or front merge. We need the smaller start_time of |
| 376 | * the merged requests to be the current request |
| 377 | * for accounting purposes. |
| 378 | */ |
| 379 | if (time_after(req->start_time, next->start_time)) |
| 380 | req->start_time = next->start_time; |
| 381 | |
| 382 | req->biotail->bi_next = next->bio; |
| 383 | req->biotail = next->biotail; |
| 384 | |
| 385 | req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors; |
| 386 | |
| 387 | elv_merge_requests(q, req, next); |
| 388 | |
| 389 | if (req->rq_disk) { |
Tejun Heo | 310a2c1 | 2008-08-25 19:47:17 +0900 | [diff] [blame] | 390 | struct hd_struct *part = |
| 391 | disk_map_sector(req->rq_disk, req->sector); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 392 | disk_round_stats(req->rq_disk); |
| 393 | req->rq_disk->in_flight--; |
Jerome Marchand | 6f2576a | 2008-02-08 11:04:35 +0100 | [diff] [blame] | 394 | if (part) { |
| 395 | part_round_stats(part); |
| 396 | part->in_flight--; |
| 397 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | req->ioprio = ioprio_best(req->ioprio, next->ioprio); |
| 401 | |
| 402 | __blk_put_request(q, next); |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | int attempt_back_merge(struct request_queue *q, struct request *rq) |
| 407 | { |
| 408 | struct request *next = elv_latter_request(q, rq); |
| 409 | |
| 410 | if (next) |
| 411 | return attempt_merge(q, rq, next); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int attempt_front_merge(struct request_queue *q, struct request *rq) |
| 417 | { |
| 418 | struct request *prev = elv_former_request(q, rq); |
| 419 | |
| 420 | if (prev) |
| 421 | return attempt_merge(q, prev, rq); |
| 422 | |
| 423 | return 0; |
| 424 | } |