2 * Functions related to segment and merge handling
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/blkdev.h>
8 #include <linux/scatterlist.h>
12 void blk_recalc_rq_sectors(struct request *rq, int nsect)
14 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
15 rq->hard_sector += nsect;
16 rq->hard_nr_sectors -= nsect;
19 * Move the I/O submission pointers ahead if required.
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);
31 * if total number of sectors is less than the first segment
32 * size, something has gone terribly wrong
34 if (rq->nr_sectors < rq->current_nr_sectors) {
35 printk(KERN_ERR "blk: request botched\n");
36 rq->nr_sectors = rq->current_nr_sectors;
41 static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
44 unsigned int phys_size;
45 struct bio_vec *bv, *bvprv = NULL;
46 int cluster, i, high, highprv = 1;
47 unsigned int seg_size, nr_phys_segs;
48 struct bio *fbio, *bbio;
54 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
56 phys_size = nr_phys_segs = 0;
58 bio_for_each_segment(bv, bio, i) {
60 * the trick here is making sure that a high page is
61 * never considered part of another segment, since that
62 * might change with the bounce page.
64 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
68 if (seg_size + bv->bv_len > q->max_segment_size)
70 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
72 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
75 seg_size += bv->bv_len;
80 if (nr_phys_segs == 1 && seg_size >
81 fbio->bi_seg_front_size)
82 fbio->bi_seg_front_size = seg_size;
86 seg_size = bv->bv_len;
92 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
93 fbio->bi_seg_front_size = seg_size;
94 if (seg_size > bbio->bi_seg_back_size)
95 bbio->bi_seg_back_size = seg_size;
100 void blk_recalc_rq_segments(struct request *rq)
102 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
105 void blk_recount_segments(struct request_queue *q, struct bio *bio)
107 struct bio *nxt = bio->bi_next;
110 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
112 bio->bi_flags |= (1 << BIO_SEG_VALID);
114 EXPORT_SYMBOL(blk_recount_segments);
116 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
119 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
122 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
126 if (!bio_has_data(bio))
129 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
133 * bio and nxt are contiguous in memory; check if the queue allows
134 * these two to be merged into one
136 if (BIO_SEG_BOUNDARY(q, bio, nxt))
143 * map a request to scatterlist, return number of sg entries setup. Caller
144 * must make sure sg can hold rq->nr_phys_segments entries
146 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
147 struct scatterlist *sglist)
149 struct bio_vec *bvec, *bvprv;
150 struct req_iterator iter;
151 struct scatterlist *sg;
155 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
162 rq_for_each_segment(bvec, rq, iter) {
163 int nbytes = bvec->bv_len;
165 if (bvprv && cluster) {
166 if (sg->length + nbytes > q->max_segment_size)
169 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
171 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
174 sg->length += nbytes;
181 * If the driver previously mapped a shorter
182 * list, we could see a termination bit
183 * prematurely unless it fully inits the sg
184 * table on each mapping. We KNOW that there
185 * must be more entries here or the driver
186 * would be buggy, so force clear the
187 * termination bit to avoid doing a full
188 * sg_init_table() in drivers for each command.
190 sg->page_link &= ~0x02;
194 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
198 } /* segments in rq */
201 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
202 (rq->data_len & q->dma_pad_mask)) {
203 unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1;
205 sg->length += pad_len;
206 rq->extra_len += pad_len;
209 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
210 if (rq->cmd_flags & REQ_RW)
211 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
213 sg->page_link &= ~0x02;
215 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
217 ((unsigned long)q->dma_drain_buffer) &
220 rq->extra_len += q->dma_drain_size;
228 EXPORT_SYMBOL(blk_rq_map_sg);
230 static inline int ll_new_hw_segment(struct request_queue *q,
234 int nr_phys_segs = bio_phys_segments(q, bio);
236 if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
237 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
238 req->cmd_flags |= REQ_NOMERGE;
239 if (req == q->last_merge)
240 q->last_merge = NULL;
245 * This will form the start of a new hw segment. Bump both
248 req->nr_phys_segments += nr_phys_segs;
252 int ll_back_merge_fn(struct request_queue *q, struct request *req,
255 unsigned short max_sectors;
257 if (unlikely(blk_pc_request(req)))
258 max_sectors = q->max_hw_sectors;
260 max_sectors = q->max_sectors;
262 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
263 req->cmd_flags |= REQ_NOMERGE;
264 if (req == q->last_merge)
265 q->last_merge = NULL;
268 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
269 blk_recount_segments(q, req->biotail);
270 if (!bio_flagged(bio, BIO_SEG_VALID))
271 blk_recount_segments(q, bio);
273 return ll_new_hw_segment(q, req, bio);
276 int ll_front_merge_fn(struct request_queue *q, struct request *req,
279 unsigned short max_sectors;
281 if (unlikely(blk_pc_request(req)))
282 max_sectors = q->max_hw_sectors;
284 max_sectors = q->max_sectors;
287 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
288 req->cmd_flags |= REQ_NOMERGE;
289 if (req == q->last_merge)
290 q->last_merge = NULL;
293 if (!bio_flagged(bio, BIO_SEG_VALID))
294 blk_recount_segments(q, bio);
295 if (!bio_flagged(req->bio, BIO_SEG_VALID))
296 blk_recount_segments(q, req->bio);
298 return ll_new_hw_segment(q, req, bio);
301 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
302 struct request *next)
304 int total_phys_segments;
305 unsigned int seg_size =
306 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
309 * First check if the either of the requests are re-queued
310 * requests. Can't merge them if they are.
312 if (req->special || next->special)
316 * Will it become too large?
318 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
321 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
322 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
323 if (req->nr_phys_segments == 1)
324 req->bio->bi_seg_front_size = seg_size;
325 if (next->nr_phys_segments == 1)
326 next->biotail->bi_seg_back_size = seg_size;
327 total_phys_segments--;
330 if (total_phys_segments > q->max_phys_segments)
333 if (total_phys_segments > q->max_hw_segments)
337 req->nr_phys_segments = total_phys_segments;
341 static void blk_account_io_merge(struct request *req)
343 if (blk_do_io_stat(req)) {
344 struct hd_struct *part;
347 cpu = part_stat_lock();
348 part = disk_map_sector_rcu(req->rq_disk, req->sector);
350 part_round_stats(cpu, part);
351 part_dec_in_flight(part);
358 * Has to be called with the request spinlock acquired
360 static int attempt_merge(struct request_queue *q, struct request *req,
361 struct request *next)
363 if (!rq_mergeable(req) || !rq_mergeable(next))
369 if (req->sector + req->nr_sectors != next->sector)
372 if (rq_data_dir(req) != rq_data_dir(next)
373 || req->rq_disk != next->rq_disk
377 if (blk_integrity_rq(req) != blk_integrity_rq(next))
381 * If we are allowed to merge, then append bio list
382 * from next to rq and release next. merge_requests_fn
383 * will have updated segment counts, update sector
386 if (!ll_merge_requests_fn(q, req, next))
390 * At this point we have either done a back merge
391 * or front merge. We need the smaller start_time of
392 * the merged requests to be the current request
393 * for accounting purposes.
395 if (time_after(req->start_time, next->start_time))
396 req->start_time = next->start_time;
398 req->biotail->bi_next = next->bio;
399 req->biotail = next->biotail;
401 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
403 elv_merge_requests(q, req, next);
406 * 'next' is going away, so update stats accordingly
408 blk_account_io_merge(next);
410 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
411 if (blk_rq_cpu_valid(next))
412 req->cpu = next->cpu;
414 /* owner-ship of bio passed from next to req */
416 __blk_put_request(q, next);
420 int attempt_back_merge(struct request_queue *q, struct request *rq)
422 struct request *next = elv_latter_request(q, rq);
425 return attempt_merge(q, rq, next);
430 int attempt_front_merge(struct request_queue *q, struct request *rq)
432 struct request *prev = elv_former_request(q, rq);
435 return attempt_merge(q, prev, rq);