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