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
,
43 unsigned int *seg_size_ptr
)
45 unsigned int phys_size
;
46 struct bio_vec
*bv
, *bvprv
= NULL
;
47 int cluster
, i
, high
, highprv
= 1;
48 unsigned int seg_size
, nr_phys_segs
;
55 cluster
= test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
57 phys_size
= nr_phys_segs
= 0;
59 bio_for_each_segment(bv
, bio
, i
) {
61 * the trick here is making sure that a high page is
62 * never considered part of another segment, since that
63 * might change with the bounce page.
65 high
= page_to_pfn(bv
->bv_page
) > q
->bounce_pfn
;
69 if (seg_size
+ bv
->bv_len
> q
->max_segment_size
)
71 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bv
))
73 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bv
))
76 seg_size
+= bv
->bv_len
;
81 if (nr_phys_segs
== 1 && seg_size
>
82 fbio
->bi_seg_front_size
)
83 fbio
->bi_seg_front_size
= seg_size
;
87 seg_size
= bv
->bv_len
;
93 *seg_size_ptr
= seg_size
;
98 void blk_recalc_rq_segments(struct request
*rq
)
100 unsigned int seg_size
= 0, phys_segs
;
102 phys_segs
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
, &seg_size
);
104 if (phys_segs
== 1 && seg_size
> rq
->bio
->bi_seg_front_size
)
105 rq
->bio
->bi_seg_front_size
= seg_size
;
106 if (seg_size
> rq
->biotail
->bi_seg_back_size
)
107 rq
->biotail
->bi_seg_back_size
= seg_size
;
109 rq
->nr_phys_segments
= phys_segs
;
112 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
114 struct bio
*nxt
= bio
->bi_next
;
117 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
, NULL
);
119 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
121 EXPORT_SYMBOL(blk_recount_segments
);
123 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
126 if (!test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
))
129 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
133 if (!bio_has_data(bio
))
136 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
140 * bio and nxt are contiguous in memory; check if the queue allows
141 * these two to be merged into one
143 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
150 * map a request to scatterlist, return number of sg entries setup. Caller
151 * must make sure sg can hold rq->nr_phys_segments entries
153 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
154 struct scatterlist
*sglist
)
156 struct bio_vec
*bvec
, *bvprv
;
157 struct req_iterator iter
;
158 struct scatterlist
*sg
;
162 cluster
= test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
169 rq_for_each_segment(bvec
, rq
, iter
) {
170 int nbytes
= bvec
->bv_len
;
172 if (bvprv
&& cluster
) {
173 if (sg
->length
+ nbytes
> q
->max_segment_size
)
176 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
178 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
181 sg
->length
+= nbytes
;
188 * If the driver previously mapped a shorter
189 * list, we could see a termination bit
190 * prematurely unless it fully inits the sg
191 * table on each mapping. We KNOW that there
192 * must be more entries here or the driver
193 * would be buggy, so force clear the
194 * termination bit to avoid doing a full
195 * sg_init_table() in drivers for each command.
197 sg
->page_link
&= ~0x02;
201 sg_set_page(sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
205 } /* segments in rq */
208 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
209 (rq
->data_len
& q
->dma_pad_mask
)) {
210 unsigned int pad_len
= (q
->dma_pad_mask
& ~rq
->data_len
) + 1;
212 sg
->length
+= pad_len
;
213 rq
->extra_len
+= pad_len
;
216 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
217 if (rq
->cmd_flags
& REQ_RW
)
218 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
220 sg
->page_link
&= ~0x02;
222 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
224 ((unsigned long)q
->dma_drain_buffer
) &
227 rq
->extra_len
+= q
->dma_drain_size
;
235 EXPORT_SYMBOL(blk_rq_map_sg
);
237 static inline int ll_new_hw_segment(struct request_queue
*q
,
241 int nr_phys_segs
= bio_phys_segments(q
, bio
);
243 if (req
->nr_phys_segments
+ nr_phys_segs
> q
->max_hw_segments
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
;
252 * This will form the start of a new hw segment. Bump both
255 req
->nr_phys_segments
+= nr_phys_segs
;
259 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
262 unsigned short max_sectors
;
264 if (unlikely(blk_pc_request(req
)))
265 max_sectors
= q
->max_hw_sectors
;
267 max_sectors
= q
->max_sectors
;
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
;
275 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
276 blk_recount_segments(q
, req
->biotail
);
277 if (!bio_flagged(bio
, BIO_SEG_VALID
))
278 blk_recount_segments(q
, bio
);
280 return ll_new_hw_segment(q
, req
, bio
);
283 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
286 unsigned short max_sectors
;
288 if (unlikely(blk_pc_request(req
)))
289 max_sectors
= q
->max_hw_sectors
;
291 max_sectors
= q
->max_sectors
;
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
;
300 if (!bio_flagged(bio
, BIO_SEG_VALID
))
301 blk_recount_segments(q
, bio
);
302 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
303 blk_recount_segments(q
, req
->bio
);
305 return ll_new_hw_segment(q
, req
, bio
);
308 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
309 struct request
*next
)
311 int total_phys_segments
;
312 unsigned int seg_size
=
313 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
316 * First check if the either of the requests are re-queued
317 * requests. Can't merge them if they are.
319 if (req
->special
|| next
->special
)
323 * Will it become too large?
325 if ((req
->nr_sectors
+ next
->nr_sectors
) > q
->max_sectors
)
328 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
329 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
330 if (req
->nr_phys_segments
== 1)
331 req
->bio
->bi_seg_front_size
= seg_size
;
332 if (next
->nr_phys_segments
== 1)
333 next
->biotail
->bi_seg_back_size
= seg_size
;
334 total_phys_segments
--;
337 if (total_phys_segments
> q
->max_phys_segments
)
340 if (total_phys_segments
> q
->max_hw_segments
)
344 req
->nr_phys_segments
= total_phys_segments
;
349 * Has to be called with the request spinlock acquired
351 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
352 struct request
*next
)
354 if (!rq_mergeable(req
) || !rq_mergeable(next
))
360 if (req
->sector
+ req
->nr_sectors
!= next
->sector
)
363 if (rq_data_dir(req
) != rq_data_dir(next
)
364 || req
->rq_disk
!= next
->rq_disk
368 if (blk_integrity_rq(req
) != blk_integrity_rq(next
))
372 * If we are allowed to merge, then append bio list
373 * from next to rq and release next. merge_requests_fn
374 * will have updated segment counts, update sector
377 if (!ll_merge_requests_fn(q
, req
, next
))
381 * At this point we have either done a back merge
382 * or front merge. We need the smaller start_time of
383 * the merged requests to be the current request
384 * for accounting purposes.
386 if (time_after(req
->start_time
, next
->start_time
))
387 req
->start_time
= next
->start_time
;
389 req
->biotail
->bi_next
= next
->bio
;
390 req
->biotail
= next
->biotail
;
392 req
->nr_sectors
= req
->hard_nr_sectors
+= next
->hard_nr_sectors
;
394 elv_merge_requests(q
, req
, next
);
397 struct hd_struct
*part
;
400 cpu
= part_stat_lock();
401 part
= disk_map_sector_rcu(req
->rq_disk
, req
->sector
);
403 part_round_stats(cpu
, part
);
404 part_dec_in_flight(part
);
409 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
410 if (blk_rq_cpu_valid(next
))
411 req
->cpu
= next
->cpu
;
413 __blk_put_request(q
, next
);
417 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
419 struct request
*next
= elv_latter_request(q
, rq
);
422 return attempt_merge(q
, rq
, next
);
427 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
429 struct request
*prev
= elv_former_request(q
, rq
);
432 return attempt_merge(q
, prev
, rq
);