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 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 12 | static unsigned int __blk_recalc_rq_segments(struct request_queue *q, |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 13 | struct bio *bio) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 14 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 15 | struct bio_vec *bv, *bvprv = NULL; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 16 | int cluster, i, high, highprv = 1; |
| 17 | unsigned int seg_size, nr_phys_segs; |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 18 | struct bio *fbio, *bbio; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 19 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 20 | if (!bio) |
| 21 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 22 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 23 | fbio = bio; |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 24 | cluster = blk_queue_cluster(q); |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 25 | seg_size = 0; |
Andi Kleen | 2c8919d | 2010-06-21 11:02:47 +0200 | [diff] [blame] | 26 | nr_phys_segs = 0; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 27 | for_each_bio(bio) { |
| 28 | bio_for_each_segment(bv, bio, i) { |
| 29 | /* |
| 30 | * the trick here is making sure that a high page is |
| 31 | * never considered part of another segment, since that |
| 32 | * might change with the bounce page. |
| 33 | */ |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 34 | high = page_to_pfn(bv->bv_page) > queue_bounce_pfn(q); |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 35 | if (high || highprv) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 36 | goto new_segment; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 37 | if (cluster) { |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 38 | if (seg_size + bv->bv_len |
| 39 | > queue_max_segment_size(q)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 40 | goto new_segment; |
| 41 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv)) |
| 42 | goto new_segment; |
| 43 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv)) |
| 44 | goto new_segment; |
Subhash Jadavani | 6e25ce3 | 2013-01-10 02:15:13 +0530 | [diff] [blame] | 45 | if ((bvprv->bv_page != bv->bv_page) && |
| 46 | (bvprv->bv_page + 1) != bv->bv_page) |
| 47 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 48 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 49 | seg_size += bv->bv_len; |
| 50 | bvprv = bv; |
| 51 | continue; |
| 52 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 53 | new_segment: |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 54 | if (nr_phys_segs == 1 && seg_size > |
| 55 | fbio->bi_seg_front_size) |
| 56 | fbio->bi_seg_front_size = seg_size; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 57 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 58 | nr_phys_segs++; |
| 59 | bvprv = bv; |
| 60 | seg_size = bv->bv_len; |
| 61 | highprv = high; |
| 62 | } |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 63 | bbio = bio; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 66 | if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) |
| 67 | fbio->bi_seg_front_size = seg_size; |
| 68 | if (seg_size > bbio->bi_seg_back_size) |
| 69 | bbio->bi_seg_back_size = seg_size; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 70 | |
| 71 | return nr_phys_segs; |
| 72 | } |
| 73 | |
| 74 | void blk_recalc_rq_segments(struct request *rq) |
| 75 | { |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 76 | rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void blk_recount_segments(struct request_queue *q, struct bio *bio) |
| 80 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 81 | struct bio *nxt = bio->bi_next; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 82 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 83 | bio->bi_next = NULL; |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 84 | bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 85 | bio->bi_next = nxt; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 86 | bio->bi_flags |= (1 << BIO_SEG_VALID); |
| 87 | } |
| 88 | EXPORT_SYMBOL(blk_recount_segments); |
| 89 | |
| 90 | static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, |
| 91 | struct bio *nxt) |
| 92 | { |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 93 | if (!blk_queue_cluster(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 94 | return 0; |
| 95 | |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 96 | if (bio->bi_seg_back_size + nxt->bi_seg_front_size > |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 97 | queue_max_segment_size(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 98 | return 0; |
| 99 | |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 100 | if (!bio_has_data(bio)) |
| 101 | return 1; |
| 102 | |
| 103 | if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt))) |
| 104 | return 0; |
| 105 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 106 | /* |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 107 | * bio and nxt are contiguous in memory; check if the queue allows |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 108 | * these two to be merged into one |
| 109 | */ |
| 110 | if (BIO_SEG_BOUNDARY(q, bio, nxt)) |
| 111 | return 1; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 116 | /* |
| 117 | * map a request to scatterlist, return number of sg entries setup. Caller |
| 118 | * must make sure sg can hold rq->nr_phys_segments entries |
| 119 | */ |
| 120 | int blk_rq_map_sg(struct request_queue *q, struct request *rq, |
| 121 | struct scatterlist *sglist) |
| 122 | { |
| 123 | struct bio_vec *bvec, *bvprv; |
| 124 | struct req_iterator iter; |
| 125 | struct scatterlist *sg; |
| 126 | int nsegs, cluster; |
| 127 | |
| 128 | nsegs = 0; |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 129 | cluster = blk_queue_cluster(q); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * for each bio in rq |
| 133 | */ |
| 134 | bvprv = NULL; |
| 135 | sg = NULL; |
| 136 | rq_for_each_segment(bvec, rq, iter) { |
| 137 | int nbytes = bvec->bv_len; |
| 138 | |
| 139 | if (bvprv && cluster) { |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 140 | if (sg->length + nbytes > queue_max_segment_size(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 141 | goto new_segment; |
| 142 | |
| 143 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) |
| 144 | goto new_segment; |
| 145 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) |
| 146 | goto new_segment; |
Subhash Jadavani | 6e25ce3 | 2013-01-10 02:15:13 +0530 | [diff] [blame] | 147 | if ((bvprv->bv_page != bvec->bv_page) && |
| 148 | ((bvprv->bv_page + 1) != bvec->bv_page)) |
| 149 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 150 | |
| 151 | sg->length += nbytes; |
| 152 | } else { |
| 153 | new_segment: |
| 154 | if (!sg) |
| 155 | sg = sglist; |
| 156 | else { |
| 157 | /* |
| 158 | * If the driver previously mapped a shorter |
| 159 | * list, we could see a termination bit |
| 160 | * prematurely unless it fully inits the sg |
| 161 | * table on each mapping. We KNOW that there |
| 162 | * must be more entries here or the driver |
| 163 | * would be buggy, so force clear the |
| 164 | * termination bit to avoid doing a full |
| 165 | * sg_init_table() in drivers for each command. |
| 166 | */ |
| 167 | sg->page_link &= ~0x02; |
| 168 | sg = sg_next(sg); |
| 169 | } |
| 170 | |
| 171 | sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset); |
| 172 | nsegs++; |
| 173 | } |
| 174 | bvprv = bvec; |
| 175 | } /* segments in rq */ |
| 176 | |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 177 | |
| 178 | if (unlikely(rq->cmd_flags & REQ_COPY_USER) && |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 179 | (blk_rq_bytes(rq) & q->dma_pad_mask)) { |
| 180 | unsigned int pad_len = |
| 181 | (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 182 | |
| 183 | sg->length += pad_len; |
| 184 | rq->extra_len += pad_len; |
| 185 | } |
| 186 | |
Tejun Heo | 2fb98e8 | 2008-02-19 11:36:53 +0100 | [diff] [blame] | 187 | if (q->dma_drain_size && q->dma_drain_needed(rq)) { |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 188 | if (rq->cmd_flags & REQ_WRITE) |
Tejun Heo | db0a2e0 | 2008-02-19 11:36:55 +0100 | [diff] [blame] | 189 | memset(q->dma_drain_buffer, 0, q->dma_drain_size); |
| 190 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 191 | sg->page_link &= ~0x02; |
| 192 | sg = sg_next(sg); |
| 193 | sg_set_page(sg, virt_to_page(q->dma_drain_buffer), |
| 194 | q->dma_drain_size, |
| 195 | ((unsigned long)q->dma_drain_buffer) & |
| 196 | (PAGE_SIZE - 1)); |
| 197 | nsegs++; |
FUJITA Tomonori | 7a85f88 | 2008-03-04 11:17:11 +0100 | [diff] [blame] | 198 | rq->extra_len += q->dma_drain_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (sg) |
| 202 | sg_mark_end(sg); |
| 203 | |
| 204 | return nsegs; |
| 205 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 206 | EXPORT_SYMBOL(blk_rq_map_sg); |
| 207 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 208 | static inline int ll_new_hw_segment(struct request_queue *q, |
| 209 | struct request *req, |
| 210 | struct bio *bio) |
| 211 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 212 | int nr_phys_segs = bio_phys_segments(q, bio); |
| 213 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 214 | if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) |
| 215 | goto no_merge; |
| 216 | |
| 217 | if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio)) |
| 218 | goto no_merge; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * This will form the start of a new hw segment. Bump both |
| 222 | * counters. |
| 223 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 224 | req->nr_phys_segments += nr_phys_segs; |
| 225 | return 1; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 226 | |
| 227 | no_merge: |
| 228 | req->cmd_flags |= REQ_NOMERGE; |
| 229 | if (req == q->last_merge) |
| 230 | q->last_merge = NULL; |
| 231 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | int ll_back_merge_fn(struct request_queue *q, struct request *req, |
| 235 | struct bio *bio) |
| 236 | { |
| 237 | unsigned short max_sectors; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 238 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 239 | if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC)) |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 240 | max_sectors = queue_max_hw_sectors(q); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 241 | else |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 242 | max_sectors = queue_max_sectors(q); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 243 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 244 | if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 245 | req->cmd_flags |= REQ_NOMERGE; |
| 246 | if (req == q->last_merge) |
| 247 | q->last_merge = NULL; |
| 248 | return 0; |
| 249 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 250 | if (!bio_flagged(req->biotail, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 251 | blk_recount_segments(q, req->biotail); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 252 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 253 | blk_recount_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 254 | |
| 255 | return ll_new_hw_segment(q, req, bio); |
| 256 | } |
| 257 | |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 258 | int ll_front_merge_fn(struct request_queue *q, struct request *req, |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 259 | struct bio *bio) |
| 260 | { |
| 261 | unsigned short max_sectors; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 262 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 263 | if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC)) |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 264 | max_sectors = queue_max_hw_sectors(q); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 265 | else |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 266 | max_sectors = queue_max_sectors(q); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 267 | |
| 268 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 269 | if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 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(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 276 | blk_recount_segments(q, bio); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 277 | if (!bio_flagged(req->bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 278 | blk_recount_segments(q, req->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 | |
| 283 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, |
| 284 | struct request *next) |
| 285 | { |
| 286 | int total_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 287 | unsigned int seg_size = |
| 288 | req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * First check if the either of the requests are re-queued |
| 292 | * requests. Can't merge them if they are. |
| 293 | */ |
| 294 | if (req->special || next->special) |
| 295 | return 0; |
| 296 | |
| 297 | /* |
| 298 | * Will it become too large? |
| 299 | */ |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 300 | if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > queue_max_sectors(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 301 | return 0; |
| 302 | |
| 303 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 304 | if (blk_phys_contig_segment(q, req->biotail, next->bio)) { |
| 305 | if (req->nr_phys_segments == 1) |
| 306 | req->bio->bi_seg_front_size = seg_size; |
| 307 | if (next->nr_phys_segments == 1) |
| 308 | next->biotail->bi_seg_back_size = seg_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 309 | total_phys_segments--; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 310 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 311 | |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 312 | if (total_phys_segments > queue_max_segments(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 313 | return 0; |
| 314 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 315 | if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next)) |
| 316 | return 0; |
| 317 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 318 | /* Merge is OK... */ |
| 319 | req->nr_phys_segments = total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 323 | /** |
| 324 | * blk_rq_set_mixed_merge - mark a request as mixed merge |
| 325 | * @rq: request to mark as mixed merge |
| 326 | * |
| 327 | * Description: |
| 328 | * @rq is about to be mixed merged. Make sure the attributes |
| 329 | * which can be mixed are set in each bio and mark @rq as mixed |
| 330 | * merged. |
| 331 | */ |
| 332 | void blk_rq_set_mixed_merge(struct request *rq) |
| 333 | { |
| 334 | unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; |
| 335 | struct bio *bio; |
| 336 | |
| 337 | if (rq->cmd_flags & REQ_MIXED_MERGE) |
| 338 | return; |
| 339 | |
| 340 | /* |
| 341 | * @rq will no longer represent mixable attributes for all the |
| 342 | * contained bios. It will just track those of the first one. |
| 343 | * Distributes the attributs to each bio. |
| 344 | */ |
| 345 | for (bio = rq->bio; bio; bio = bio->bi_next) { |
| 346 | WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && |
| 347 | (bio->bi_rw & REQ_FAILFAST_MASK) != ff); |
| 348 | bio->bi_rw |= ff; |
| 349 | } |
| 350 | rq->cmd_flags |= REQ_MIXED_MERGE; |
| 351 | } |
| 352 | |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 353 | static void blk_account_io_merge(struct request *req) |
| 354 | { |
| 355 | if (blk_do_io_stat(req)) { |
| 356 | struct hd_struct *part; |
| 357 | int cpu; |
| 358 | |
| 359 | cpu = part_stat_lock(); |
Jerome Marchand | 09e099d | 2011-01-05 16:57:38 +0100 | [diff] [blame] | 360 | part = req->part; |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 361 | |
| 362 | part_round_stats(cpu, part); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 363 | part_dec_in_flight(part, rq_data_dir(req)); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 364 | |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 365 | hd_struct_put(part); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 366 | part_stat_unlock(); |
| 367 | } |
| 368 | } |
| 369 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 370 | /* |
| 371 | * Has to be called with the request spinlock acquired |
| 372 | */ |
| 373 | static int attempt_merge(struct request_queue *q, struct request *req, |
| 374 | struct request *next) |
| 375 | { |
| 376 | if (!rq_mergeable(req) || !rq_mergeable(next)) |
| 377 | return 0; |
| 378 | |
| 379 | /* |
Adrian Hunter | f281fb5 | 2010-09-25 12:42:55 +0200 | [diff] [blame] | 380 | * Don't merge file system requests and discard requests |
| 381 | */ |
| 382 | if ((req->cmd_flags & REQ_DISCARD) != (next->cmd_flags & REQ_DISCARD)) |
| 383 | return 0; |
| 384 | |
| 385 | /* |
| 386 | * Don't merge discard requests and secure discard requests |
| 387 | */ |
| 388 | if ((req->cmd_flags & REQ_SECURE) != (next->cmd_flags & REQ_SECURE)) |
| 389 | return 0; |
| 390 | |
| 391 | /* |
Maya Erez | 73937f5 | 2012-05-24 23:33:05 +0300 | [diff] [blame] | 392 | * Don't merge file system requests and sanitize requests |
| 393 | */ |
| 394 | if ((req->cmd_flags & REQ_SANITIZE) != (next->cmd_flags & REQ_SANITIZE)) |
| 395 | return 0; |
| 396 | |
| 397 | /* |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 398 | * not contiguous |
| 399 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 400 | if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 401 | return 0; |
| 402 | |
| 403 | if (rq_data_dir(req) != rq_data_dir(next) |
| 404 | || req->rq_disk != next->rq_disk |
| 405 | || next->special) |
| 406 | return 0; |
| 407 | |
| 408 | /* |
| 409 | * If we are allowed to merge, then append bio list |
| 410 | * from next to rq and release next. merge_requests_fn |
| 411 | * will have updated segment counts, update sector |
| 412 | * counts here. |
| 413 | */ |
| 414 | if (!ll_merge_requests_fn(q, req, next)) |
| 415 | return 0; |
| 416 | |
| 417 | /* |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 418 | * If failfast settings disagree or any of the two is already |
| 419 | * a mixed merge, mark both as mixed before proceeding. This |
| 420 | * makes sure that all involved bios have mixable attributes |
| 421 | * set properly. |
| 422 | */ |
| 423 | if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || |
| 424 | (req->cmd_flags & REQ_FAILFAST_MASK) != |
| 425 | (next->cmd_flags & REQ_FAILFAST_MASK)) { |
| 426 | blk_rq_set_mixed_merge(req); |
| 427 | blk_rq_set_mixed_merge(next); |
| 428 | } |
| 429 | |
| 430 | /* |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 431 | * At this point we have either done a back merge |
| 432 | * or front merge. We need the smaller start_time of |
| 433 | * the merged requests to be the current request |
| 434 | * for accounting purposes. |
| 435 | */ |
| 436 | if (time_after(req->start_time, next->start_time)) |
| 437 | req->start_time = next->start_time; |
| 438 | |
| 439 | req->biotail->bi_next = next->bio; |
| 440 | req->biotail = next->biotail; |
| 441 | |
Tejun Heo | a2dec7b | 2009-05-07 22:24:44 +0900 | [diff] [blame] | 442 | req->__data_len += blk_rq_bytes(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 443 | |
| 444 | elv_merge_requests(q, req, next); |
| 445 | |
Jerome Marchand | 42dad76 | 2009-04-22 14:01:49 +0200 | [diff] [blame] | 446 | /* |
| 447 | * 'next' is going away, so update stats accordingly |
| 448 | */ |
| 449 | blk_account_io_merge(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 450 | |
| 451 | req->ioprio = ioprio_best(req->ioprio, next->ioprio); |
Jens Axboe | ab780f1 | 2008-08-26 10:25:02 +0200 | [diff] [blame] | 452 | if (blk_rq_cpu_valid(next)) |
| 453 | req->cpu = next->cpu; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 454 | |
Boaz Harrosh | 1cd96c2 | 2009-03-24 12:35:07 +0100 | [diff] [blame] | 455 | /* owner-ship of bio passed from next to req */ |
| 456 | next->bio = NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 457 | __blk_put_request(q, next); |
| 458 | return 1; |
| 459 | } |
| 460 | |
| 461 | int attempt_back_merge(struct request_queue *q, struct request *rq) |
| 462 | { |
| 463 | struct request *next = elv_latter_request(q, rq); |
| 464 | |
| 465 | if (next) |
| 466 | return attempt_merge(q, rq, next); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int attempt_front_merge(struct request_queue *q, struct request *rq) |
| 472 | { |
| 473 | struct request *prev = elv_former_request(q, rq); |
| 474 | |
| 475 | if (prev) |
| 476 | return attempt_merge(q, prev, rq); |
| 477 | |
| 478 | return 0; |
| 479 | } |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 480 | |
| 481 | int blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
| 482 | struct request *next) |
| 483 | { |
| 484 | return attempt_merge(q, rq, next); |
| 485 | } |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 486 | |
| 487 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio) |
| 488 | { |
| 489 | if (!rq_mergeable(rq)) |
| 490 | return false; |
| 491 | |
| 492 | /* don't merge file system requests and discard requests */ |
| 493 | if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD)) |
| 494 | return false; |
| 495 | |
| 496 | /* don't merge discard requests and secure discard requests */ |
| 497 | if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE)) |
| 498 | return false; |
| 499 | |
| 500 | /* different data direction or already started, don't merge */ |
| 501 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 502 | return false; |
| 503 | |
| 504 | /* must be same device and not a special request */ |
| 505 | if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special) |
| 506 | return false; |
| 507 | |
| 508 | /* only merge integrity protected bio into ditto rq */ |
| 509 | if (bio_integrity(bio) != blk_integrity_rq(rq)) |
| 510 | return false; |
| 511 | |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | int blk_try_merge(struct request *rq, struct bio *bio) |
| 516 | { |
| 517 | if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_sector) |
| 518 | return ELEVATOR_BACK_MERGE; |
| 519 | else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_sector) |
| 520 | return ELEVATOR_FRONT_MERGE; |
| 521 | return ELEVATOR_NO_MERGE; |
| 522 | } |