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
)) {
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 void blk_recalc_rq_segments(struct request
*rq
)
45 unsigned int phys_size
;
47 struct bio_vec
*bv
, *bvprv
= NULL
;
51 struct req_iterator iter
;
52 int high
, highprv
= 1;
53 struct request_queue
*q
= rq
->q
;
58 cluster
= test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
59 hw_seg_size
= seg_size
= 0;
60 phys_size
= hw_size
= nr_phys_segs
= nr_hw_segs
= 0;
61 rq_for_each_segment(bv
, rq
, iter
) {
63 * the trick here is making sure that a high page is never
64 * considered part of another segment, since that might
65 * change with the bounce page.
67 high
= page_to_pfn(bv
->bv_page
) > q
->bounce_pfn
;
71 if (seg_size
+ bv
->bv_len
> q
->max_segment_size
)
73 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bv
))
75 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bv
))
77 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size
+ bv
->bv_len
))
80 seg_size
+= bv
->bv_len
;
81 hw_seg_size
+= bv
->bv_len
;
86 if (BIOVEC_VIRT_MERGEABLE(bvprv
, bv
) &&
87 !BIOVEC_VIRT_OVERSIZE(hw_seg_size
+ bv
->bv_len
))
88 hw_seg_size
+= bv
->bv_len
;
91 if (nr_hw_segs
== 1 &&
92 hw_seg_size
> rq
->bio
->bi_hw_front_size
)
93 rq
->bio
->bi_hw_front_size
= hw_seg_size
;
94 hw_seg_size
= BIOVEC_VIRT_START_SIZE(bv
) + bv
->bv_len
;
98 if (nr_phys_segs
== 1 && seg_size
> rq
->bio
->bi_seg_front_size
)
99 rq
->bio
->bi_seg_front_size
= seg_size
;
103 seg_size
= bv
->bv_len
;
107 if (nr_hw_segs
== 1 &&
108 hw_seg_size
> rq
->bio
->bi_hw_front_size
)
109 rq
->bio
->bi_hw_front_size
= hw_seg_size
;
110 if (hw_seg_size
> rq
->biotail
->bi_hw_back_size
)
111 rq
->biotail
->bi_hw_back_size
= hw_seg_size
;
112 if (nr_phys_segs
== 1 && seg_size
> rq
->bio
->bi_seg_front_size
)
113 rq
->bio
->bi_seg_front_size
= seg_size
;
114 if (seg_size
> rq
->biotail
->bi_seg_back_size
)
115 rq
->biotail
->bi_seg_back_size
= seg_size
;
116 rq
->nr_phys_segments
= nr_phys_segs
;
117 rq
->nr_hw_segments
= nr_hw_segs
;
120 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
123 struct bio
*nxt
= bio
->bi_next
;
125 rq
.bio
= rq
.biotail
= bio
;
127 blk_recalc_rq_segments(&rq
);
129 bio
->bi_phys_segments
= rq
.nr_phys_segments
;
130 bio
->bi_hw_segments
= rq
.nr_hw_segments
;
131 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
133 EXPORT_SYMBOL(blk_recount_segments
);
135 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
138 if (!test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
))
141 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
143 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
148 * bio and nxt are contigous in memory, check if the queue allows
149 * these two to be merged into one
151 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
157 static int blk_hw_contig_segment(struct request_queue
*q
, struct bio
*bio
,
160 if (!bio_flagged(bio
, BIO_SEG_VALID
))
161 blk_recount_segments(q
, bio
);
162 if (!bio_flagged(nxt
, BIO_SEG_VALID
))
163 blk_recount_segments(q
, nxt
);
164 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)) ||
165 BIOVEC_VIRT_OVERSIZE(bio
->bi_hw_back_size
+ nxt
->bi_hw_front_size
))
167 if (bio
->bi_hw_back_size
+ nxt
->bi_hw_front_size
> q
->max_segment_size
)
174 * map a request to scatterlist, return number of sg entries setup. Caller
175 * must make sure sg can hold rq->nr_phys_segments entries
177 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
178 struct scatterlist
*sglist
)
180 struct bio_vec
*bvec
, *bvprv
;
181 struct req_iterator iter
;
182 struct scatterlist
*sg
;
186 cluster
= test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
193 rq_for_each_segment(bvec
, rq
, iter
) {
194 int nbytes
= bvec
->bv_len
;
196 if (bvprv
&& cluster
) {
197 if (sg
->length
+ nbytes
> q
->max_segment_size
)
200 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
202 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
205 sg
->length
+= nbytes
;
212 * If the driver previously mapped a shorter
213 * list, we could see a termination bit
214 * prematurely unless it fully inits the sg
215 * table on each mapping. We KNOW that there
216 * must be more entries here or the driver
217 * would be buggy, so force clear the
218 * termination bit to avoid doing a full
219 * sg_init_table() in drivers for each command.
221 sg
->page_link
&= ~0x02;
225 sg_set_page(sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
229 } /* segments in rq */
232 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
233 (rq
->data_len
& q
->dma_pad_mask
)) {
234 unsigned int pad_len
= (q
->dma_pad_mask
& ~rq
->data_len
) + 1;
236 sg
->length
+= pad_len
;
237 rq
->extra_len
+= pad_len
;
240 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
241 if (rq
->cmd_flags
& REQ_RW
)
242 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
244 sg
->page_link
&= ~0x02;
246 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
248 ((unsigned long)q
->dma_drain_buffer
) &
251 rq
->extra_len
+= q
->dma_drain_size
;
259 EXPORT_SYMBOL(blk_rq_map_sg
);
261 static inline int ll_new_mergeable(struct request_queue
*q
,
265 int nr_phys_segs
= bio_phys_segments(q
, bio
);
267 if (req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
268 req
->cmd_flags
|= REQ_NOMERGE
;
269 if (req
== q
->last_merge
)
270 q
->last_merge
= NULL
;
275 * A hw segment is just getting larger, bump just the phys
278 req
->nr_phys_segments
+= nr_phys_segs
;
282 static inline int ll_new_hw_segment(struct request_queue
*q
,
286 int nr_hw_segs
= bio_hw_segments(q
, bio
);
287 int nr_phys_segs
= bio_phys_segments(q
, bio
);
289 if (req
->nr_hw_segments
+ nr_hw_segs
> q
->max_hw_segments
290 || req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
291 req
->cmd_flags
|= REQ_NOMERGE
;
292 if (req
== q
->last_merge
)
293 q
->last_merge
= NULL
;
298 * This will form the start of a new hw segment. Bump both
301 req
->nr_hw_segments
+= nr_hw_segs
;
302 req
->nr_phys_segments
+= nr_phys_segs
;
306 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
309 unsigned short max_sectors
;
312 if (unlikely(blk_pc_request(req
)))
313 max_sectors
= q
->max_hw_sectors
;
315 max_sectors
= q
->max_sectors
;
317 if (req
->nr_sectors
+ bio_sectors(bio
) > max_sectors
) {
318 req
->cmd_flags
|= REQ_NOMERGE
;
319 if (req
== q
->last_merge
)
320 q
->last_merge
= NULL
;
323 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
324 blk_recount_segments(q
, req
->biotail
);
325 if (!bio_flagged(bio
, BIO_SEG_VALID
))
326 blk_recount_segments(q
, bio
);
327 len
= req
->biotail
->bi_hw_back_size
+ bio
->bi_hw_front_size
;
328 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req
->biotail
), __BVEC_START(bio
))
329 && !BIOVEC_VIRT_OVERSIZE(len
)) {
330 int mergeable
= ll_new_mergeable(q
, req
, bio
);
333 if (req
->nr_hw_segments
== 1)
334 req
->bio
->bi_hw_front_size
= len
;
335 if (bio
->bi_hw_segments
== 1)
336 bio
->bi_hw_back_size
= len
;
341 return ll_new_hw_segment(q
, req
, bio
);
344 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
347 unsigned short max_sectors
;
350 if (unlikely(blk_pc_request(req
)))
351 max_sectors
= q
->max_hw_sectors
;
353 max_sectors
= q
->max_sectors
;
356 if (req
->nr_sectors
+ bio_sectors(bio
) > max_sectors
) {
357 req
->cmd_flags
|= REQ_NOMERGE
;
358 if (req
== q
->last_merge
)
359 q
->last_merge
= NULL
;
362 len
= bio
->bi_hw_back_size
+ req
->bio
->bi_hw_front_size
;
363 if (!bio_flagged(bio
, BIO_SEG_VALID
))
364 blk_recount_segments(q
, bio
);
365 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
366 blk_recount_segments(q
, req
->bio
);
367 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(req
->bio
)) &&
368 !BIOVEC_VIRT_OVERSIZE(len
)) {
369 int mergeable
= ll_new_mergeable(q
, req
, bio
);
372 if (bio
->bi_hw_segments
== 1)
373 bio
->bi_hw_front_size
= len
;
374 if (req
->nr_hw_segments
== 1)
375 req
->biotail
->bi_hw_back_size
= len
;
380 return ll_new_hw_segment(q
, req
, bio
);
383 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
384 struct request
*next
)
386 int total_phys_segments
;
387 int total_hw_segments
;
388 unsigned int seg_size
=
389 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
392 * First check if the either of the requests are re-queued
393 * requests. Can't merge them if they are.
395 if (req
->special
|| next
->special
)
399 * Will it become too large?
401 if ((req
->nr_sectors
+ next
->nr_sectors
) > q
->max_sectors
)
404 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
405 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
406 if (req
->nr_phys_segments
== 1)
407 req
->bio
->bi_seg_front_size
= seg_size
;
408 if (next
->nr_phys_segments
== 1)
409 next
->biotail
->bi_seg_back_size
= seg_size
;
410 total_phys_segments
--;
413 if (total_phys_segments
> q
->max_phys_segments
)
416 total_hw_segments
= req
->nr_hw_segments
+ next
->nr_hw_segments
;
417 if (blk_hw_contig_segment(q
, req
->biotail
, next
->bio
)) {
418 int len
= req
->biotail
->bi_hw_back_size
+
419 next
->bio
->bi_hw_front_size
;
421 * propagate the combined length to the end of the requests
423 if (req
->nr_hw_segments
== 1)
424 req
->bio
->bi_hw_front_size
= len
;
425 if (next
->nr_hw_segments
== 1)
426 next
->biotail
->bi_hw_back_size
= len
;
430 if (total_hw_segments
> q
->max_hw_segments
)
434 req
->nr_phys_segments
= total_phys_segments
;
435 req
->nr_hw_segments
= total_hw_segments
;
440 * Has to be called with the request spinlock acquired
442 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
443 struct request
*next
)
445 if (!rq_mergeable(req
) || !rq_mergeable(next
))
451 if (req
->sector
+ req
->nr_sectors
!= next
->sector
)
454 if (rq_data_dir(req
) != rq_data_dir(next
)
455 || req
->rq_disk
!= next
->rq_disk
459 if (blk_integrity_rq(req
) != blk_integrity_rq(next
))
463 * If we are allowed to merge, then append bio list
464 * from next to rq and release next. merge_requests_fn
465 * will have updated segment counts, update sector
468 if (!ll_merge_requests_fn(q
, req
, next
))
472 * At this point we have either done a back merge
473 * or front merge. We need the smaller start_time of
474 * the merged requests to be the current request
475 * for accounting purposes.
477 if (time_after(req
->start_time
, next
->start_time
))
478 req
->start_time
= next
->start_time
;
480 req
->biotail
->bi_next
= next
->bio
;
481 req
->biotail
= next
->biotail
;
483 req
->nr_sectors
= req
->hard_nr_sectors
+= next
->hard_nr_sectors
;
485 elv_merge_requests(q
, req
, next
);
488 struct hd_struct
*part
489 = get_part(req
->rq_disk
, req
->sector
);
490 disk_round_stats(req
->rq_disk
);
491 req
->rq_disk
->in_flight
--;
493 part_round_stats(part
);
498 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
500 __blk_put_request(q
, next
);
504 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
506 struct request
*next
= elv_latter_request(q
, rq
);
509 return attempt_merge(q
, rq
, next
);
514 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
516 struct request
*prev
= elv_former_request(q
, rq
);
519 return attempt_merge(q
, prev
, rq
);