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 void blk_recalc_rq_segments(struct request
*rq
)
44 unsigned int phys_size
;
45 struct bio_vec
*bv
, *bvprv
= NULL
;
48 struct req_iterator iter
;
49 int high
, highprv
= 1;
50 struct request_queue
*q
= rq
->q
;
55 cluster
= test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
57 phys_size
= nr_phys_segs
= 0;
58 rq_for_each_segment(bv
, rq
, iter
) {
60 * the trick here is making sure that a high page is never
61 * considered part of another segment, since that might
62 * 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
;
82 seg_size
= bv
->bv_len
;
86 rq
->nr_phys_segments
= nr_phys_segs
;
89 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
92 struct bio
*nxt
= bio
->bi_next
;
94 rq
.bio
= rq
.biotail
= bio
;
96 blk_recalc_rq_segments(&rq
);
98 bio
->bi_phys_segments
= rq
.nr_phys_segments
;
99 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
101 EXPORT_SYMBOL(blk_recount_segments
);
103 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
106 if (!test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
))
109 if (bio
->bi_size
+ nxt
->bi_size
> q
->max_segment_size
)
112 if (!bio_has_data(bio
))
115 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
119 * bio and nxt are contiguous in memory; check if the queue allows
120 * these two to be merged into one
122 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
129 * map a request to scatterlist, return number of sg entries setup. Caller
130 * must make sure sg can hold rq->nr_phys_segments entries
132 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
133 struct scatterlist
*sglist
)
135 struct bio_vec
*bvec
, *bvprv
;
136 struct req_iterator iter
;
137 struct scatterlist
*sg
;
141 cluster
= test_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
148 rq_for_each_segment(bvec
, rq
, iter
) {
149 int nbytes
= bvec
->bv_len
;
151 if (bvprv
&& cluster
) {
152 if (sg
->length
+ nbytes
> q
->max_segment_size
)
155 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
157 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
160 sg
->length
+= nbytes
;
167 * If the driver previously mapped a shorter
168 * list, we could see a termination bit
169 * prematurely unless it fully inits the sg
170 * table on each mapping. We KNOW that there
171 * must be more entries here or the driver
172 * would be buggy, so force clear the
173 * termination bit to avoid doing a full
174 * sg_init_table() in drivers for each command.
176 sg
->page_link
&= ~0x02;
180 sg_set_page(sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
184 } /* segments in rq */
187 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
188 (rq
->data_len
& q
->dma_pad_mask
)) {
189 unsigned int pad_len
= (q
->dma_pad_mask
& ~rq
->data_len
) + 1;
191 sg
->length
+= pad_len
;
192 rq
->extra_len
+= pad_len
;
195 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
196 if (rq
->cmd_flags
& REQ_RW
)
197 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
199 sg
->page_link
&= ~0x02;
201 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
203 ((unsigned long)q
->dma_drain_buffer
) &
206 rq
->extra_len
+= q
->dma_drain_size
;
214 EXPORT_SYMBOL(blk_rq_map_sg
);
216 static inline int ll_new_mergeable(struct request_queue
*q
,
220 int nr_phys_segs
= bio_phys_segments(q
, bio
);
222 if (req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
223 req
->cmd_flags
|= REQ_NOMERGE
;
224 if (req
== q
->last_merge
)
225 q
->last_merge
= NULL
;
230 * A hw segment is just getting larger, bump just the phys
233 req
->nr_phys_segments
+= nr_phys_segs
;
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
;
314 * First check if the either of the requests are re-queued
315 * requests. Can't merge them if they are.
317 if (req
->special
|| next
->special
)
321 * Will it become too large?
323 if ((req
->nr_sectors
+ next
->nr_sectors
) > q
->max_sectors
)
326 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
327 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
))
328 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
;
342 * Has to be called with the request spinlock acquired
344 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
345 struct request
*next
)
347 if (!rq_mergeable(req
) || !rq_mergeable(next
))
353 if (req
->sector
+ req
->nr_sectors
!= next
->sector
)
356 if (rq_data_dir(req
) != rq_data_dir(next
)
357 || req
->rq_disk
!= next
->rq_disk
361 if (blk_integrity_rq(req
) != blk_integrity_rq(next
))
365 * If we are allowed to merge, then append bio list
366 * from next to rq and release next. merge_requests_fn
367 * will have updated segment counts, update sector
370 if (!ll_merge_requests_fn(q
, req
, next
))
374 * At this point we have either done a back merge
375 * or front merge. We need the smaller start_time of
376 * the merged requests to be the current request
377 * for accounting purposes.
379 if (time_after(req
->start_time
, next
->start_time
))
380 req
->start_time
= next
->start_time
;
382 req
->biotail
->bi_next
= next
->bio
;
383 req
->biotail
= next
->biotail
;
385 req
->nr_sectors
= req
->hard_nr_sectors
+= next
->hard_nr_sectors
;
387 elv_merge_requests(q
, req
, next
);
390 struct hd_struct
*part
;
393 cpu
= part_stat_lock();
394 part
= disk_map_sector_rcu(req
->rq_disk
, req
->sector
);
396 part_round_stats(cpu
, part
);
397 part_dec_in_flight(part
);
402 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
403 if (blk_rq_cpu_valid(next
))
404 req
->cpu
= next
->cpu
;
406 __blk_put_request(q
, next
);
410 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
412 struct request
*next
= elv_latter_request(q
, rq
);
415 return attempt_merge(q
, rq
, next
);
420 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
422 struct request
*prev
= elv_former_request(q
, rq
);
425 return attempt_merge(q
, prev
, rq
);