1
2
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>
9
10#include "blk.h"
11
12void blk_recalc_rq_sectors(struct request *rq, int nsect)
13{
14 if (blk_fs_request(rq)) {
15 rq->hard_sector += nsect;
16 rq->hard_nr_sectors -= nsect;
17
18
19
20
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);
28 }
29
30
31
32
33
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;
37 }
38 }
39}
40
41void blk_recalc_rq_segments(struct request *rq)
42{
43 int nr_phys_segs;
44 int nr_hw_segs;
45 unsigned int phys_size;
46 unsigned int hw_size;
47 struct bio_vec *bv, *bvprv = NULL;
48 int seg_size;
49 int hw_seg_size;
50 int cluster;
51 struct req_iterator iter;
52 int high, highprv = 1;
53 struct request_queue *q = rq->q;
54
55 if (!rq->bio)
56 return;
57
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) {
62
63
64
65
66
67 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
68 if (high || highprv)
69 goto new_hw_segment;
70 if (cluster) {
71 if (seg_size + bv->bv_len > q->max_segment_size)
72 goto new_segment;
73 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
74 goto new_segment;
75 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
76 goto new_segment;
77 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
78 goto new_hw_segment;
79
80 seg_size += bv->bv_len;
81 hw_seg_size += bv->bv_len;
82 bvprv = bv;
83 continue;
84 }
85new_segment:
86 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
87 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
88 hw_seg_size += bv->bv_len;
89 else {
90new_hw_segment:
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;
95 nr_hw_segs++;
96 }
97
98 if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size)
99 rq->bio->bi_seg_front_size = seg_size;
100
101 nr_phys_segs++;
102 bvprv = bv;
103 seg_size = bv->bv_len;
104 highprv = high;
105 }
106
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;
118}
119
120void blk_recount_segments(struct request_queue *q, struct bio *bio)
121{
122 struct request rq;
123 struct bio *nxt = bio->bi_next;
124 rq.q = q;
125 rq.bio = rq.biotail = bio;
126 bio->bi_next = NULL;
127 blk_recalc_rq_segments(&rq);
128 bio->bi_next = nxt;
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);
132}
133EXPORT_SYMBOL(blk_recount_segments);
134
135static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
136 struct bio *nxt)
137{
138 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
139 return 0;
140
141 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
142 return 0;
143 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
144 q->max_segment_size)
145 return 0;
146
147
148
149
150
151 if (BIO_SEG_BOUNDARY(q, bio, nxt))
152 return 1;
153
154 return 0;
155}
156
157static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
158 struct bio *nxt)
159{
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))
166 return 0;
167 if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
168 return 0;
169
170 return 1;
171}
172
173
174
175
176
177int blk_rq_map_sg(struct request_queue *q, struct request *rq,
178 struct scatterlist *sglist)
179{
180 struct bio_vec *bvec, *bvprv;
181 struct req_iterator iter;
182 struct scatterlist *sg;
183 int nsegs, cluster;
184
185 nsegs = 0;
186 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
187
188
189
190
191 bvprv = NULL;
192 sg = NULL;
193 rq_for_each_segment(bvec, rq, iter) {
194 int nbytes = bvec->bv_len;
195
196 if (bvprv && cluster) {
197 if (sg->length + nbytes > q->max_segment_size)
198 goto new_segment;
199
200 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
201 goto new_segment;
202 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
203 goto new_segment;
204
205 sg->length += nbytes;
206 } else {
207new_segment:
208 if (!sg)
209 sg = sglist;
210 else {
211
212
213
214
215
216
217
218
219
220
221 sg->page_link &= ~0x02;
222 sg = sg_next(sg);
223 }
224
225 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
226 nsegs++;
227 }
228 bvprv = bvec;
229 }
230
231
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;
235
236 sg->length += pad_len;
237 rq->extra_len += pad_len;
238 }
239
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);
243
244 sg->page_link &= ~0x02;
245 sg = sg_next(sg);
246 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
247 q->dma_drain_size,
248 ((unsigned long)q->dma_drain_buffer) &
249 (PAGE_SIZE - 1));
250 nsegs++;
251 rq->extra_len += q->dma_drain_size;
252 }
253
254 if (sg)
255 sg_mark_end(sg);
256
257 return nsegs;
258}
259EXPORT_SYMBOL(blk_rq_map_sg);
260
261static inline int ll_new_mergeable(struct request_queue *q,
262 struct request *req,
263 struct bio *bio)
264{
265 int nr_phys_segs = bio_phys_segments(q, bio);
266
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;
271 return 0;
272 }
273
274
275
276
277
278 req->nr_phys_segments += nr_phys_segs;
279 return 1;
280}
281
282static inline int ll_new_hw_segment(struct request_queue *q,
283 struct request *req,
284 struct bio *bio)
285{
286 int nr_hw_segs = bio_hw_segments(q, bio);
287 int nr_phys_segs = bio_phys_segments(q, bio);
288
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;
294 return 0;
295 }
296
297
298
299
300
301 req->nr_hw_segments += nr_hw_segs;
302 req->nr_phys_segments += nr_phys_segs;
303 return 1;
304}
305
306int ll_back_merge_fn(struct request_queue *q, struct request *req,
307 struct bio *bio)
308{
309 unsigned short max_sectors;
310 int len;
311
312 if (unlikely(blk_pc_request(req)))
313 max_sectors = q->max_hw_sectors;
314 else
315 max_sectors = q->max_sectors;
316
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;
321 return 0;
322 }
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);
331
332 if (mergeable) {
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;
337 }
338 return mergeable;
339 }
340
341 return ll_new_hw_segment(q, req, bio);
342}
343
344int ll_front_merge_fn(struct request_queue *q, struct request *req,
345 struct bio *bio)
346{
347 unsigned short max_sectors;
348 int len;
349
350 if (unlikely(blk_pc_request(req)))
351 max_sectors = q->max_hw_sectors;
352 else
353 max_sectors = q->max_sectors;
354
355
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;
360 return 0;
361 }
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);
370
371 if (mergeable) {
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;
376 }
377 return mergeable;
378 }
379
380 return ll_new_hw_segment(q, req, bio);
381}
382
383static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
384 struct request *next)
385{
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;
390
391
392
393
394
395 if (req->special || next->special)
396 return 0;
397
398
399
400
401 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
402 return 0;
403
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--;
411 }
412
413 if (total_phys_segments > q->max_phys_segments)
414 return 0;
415
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;
420
421
422
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;
427 total_hw_segments--;
428 }
429
430 if (total_hw_segments > q->max_hw_segments)
431 return 0;
432
433
434 req->nr_phys_segments = total_phys_segments;
435 req->nr_hw_segments = total_hw_segments;
436 return 1;
437}
438
439
440
441
442static int attempt_merge(struct request_queue *q, struct request *req,
443 struct request *next)
444{
445 if (!rq_mergeable(req) || !rq_mergeable(next))
446 return 0;
447
448
449
450
451 if (req->sector + req->nr_sectors != next->sector)
452 return 0;
453
454 if (rq_data_dir(req) != rq_data_dir(next)
455 || req->rq_disk != next->rq_disk
456 || next->special)
457 return 0;
458
459 if (blk_integrity_rq(req) != blk_integrity_rq(next))
460 return 0;
461
462
463
464
465
466
467
468 if (!ll_merge_requests_fn(q, req, next))
469 return 0;
470
471
472
473
474
475
476
477 if (time_after(req->start_time, next->start_time))
478 req->start_time = next->start_time;
479
480 req->biotail->bi_next = next->bio;
481 req->biotail = next->biotail;
482
483 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
484
485 elv_merge_requests(q, req, next);
486
487 if (req->rq_disk) {
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--;
492 if (part) {
493 part_round_stats(part);
494 part->in_flight--;
495 }
496 }
497
498 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
499
500 __blk_put_request(q, next);
501 return 1;
502}
503
504int attempt_back_merge(struct request_queue *q, struct request *rq)
505{
506 struct request *next = elv_latter_request(q, rq);
507
508 if (next)
509 return attempt_merge(q, rq, next);
510
511 return 0;
512}
513
514int attempt_front_merge(struct request_queue *q, struct request *rq)
515{
516 struct request *prev = elv_former_request(q, rq);
517
518 if (prev)
519 return attempt_merge(q, prev, rq);
520
521 return 0;
522}
523