block: drop virtual merging accounting
[linux-3.10.git] / block / blk-merge.c
1 /*
2  * Functions related to segment and merge handling
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
12 void blk_recalc_rq_sectors(struct request *rq, int nsect)
13 {
14         if (blk_fs_request(rq) || blk_discard_rq(rq)) {
15                 rq->hard_sector += nsect;
16                 rq->hard_nr_sectors -= nsect;
17
18                 /*
19                  * Move the I/O submission pointers ahead if required.
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                  * if total number of sectors is less than the first segment
32                  * size, something has gone terribly wrong
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
41 void 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                  * 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.
66                  */
67                 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
68                 if (high || highprv)
69                         goto new_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
78                         seg_size += bv->bv_len;
79                         hw_seg_size += bv->bv_len;
80                         bvprv = bv;
81                         continue;
82                 }
83 new_segment:
84                 if (nr_hw_segs == 1 &&
85                     hw_seg_size > rq->bio->bi_hw_front_size)
86                         rq->bio->bi_hw_front_size = hw_seg_size;
87                 hw_seg_size = bv->bv_len;
88                 nr_hw_segs++;
89
90                 nr_phys_segs++;
91                 bvprv = bv;
92                 seg_size = bv->bv_len;
93                 highprv = high;
94         }
95
96         if (nr_hw_segs == 1 &&
97             hw_seg_size > rq->bio->bi_hw_front_size)
98                 rq->bio->bi_hw_front_size = hw_seg_size;
99         if (hw_seg_size > rq->biotail->bi_hw_back_size)
100                 rq->biotail->bi_hw_back_size = hw_seg_size;
101         rq->nr_phys_segments = nr_phys_segs;
102         rq->nr_hw_segments = nr_hw_segs;
103 }
104
105 void blk_recount_segments(struct request_queue *q, struct bio *bio)
106 {
107         struct request rq;
108         struct bio *nxt = bio->bi_next;
109         rq.q = q;
110         rq.bio = rq.biotail = bio;
111         bio->bi_next = NULL;
112         blk_recalc_rq_segments(&rq);
113         bio->bi_next = nxt;
114         bio->bi_phys_segments = rq.nr_phys_segments;
115         bio->bi_hw_segments = rq.nr_hw_segments;
116         bio->bi_flags |= (1 << BIO_SEG_VALID);
117 }
118 EXPORT_SYMBOL(blk_recount_segments);
119
120 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
121                                    struct bio *nxt)
122 {
123         if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
124                 return 0;
125
126         if (bio->bi_size + nxt->bi_size > q->max_segment_size)
127                 return 0;
128
129         if (!bio_has_data(bio))
130                 return 1;
131
132         if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
133                 return 0;
134
135         /*
136          * bio and nxt are contiguous in memory; check if the queue allows
137          * these two to be merged into one
138          */
139         if (BIO_SEG_BOUNDARY(q, bio, nxt))
140                 return 1;
141
142         return 0;
143 }
144
145 /*
146  * map a request to scatterlist, return number of sg entries setup. Caller
147  * must make sure sg can hold rq->nr_phys_segments entries
148  */
149 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
150                   struct scatterlist *sglist)
151 {
152         struct bio_vec *bvec, *bvprv;
153         struct req_iterator iter;
154         struct scatterlist *sg;
155         int nsegs, cluster;
156
157         nsegs = 0;
158         cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
159
160         /*
161          * for each bio in rq
162          */
163         bvprv = NULL;
164         sg = NULL;
165         rq_for_each_segment(bvec, rq, iter) {
166                 int nbytes = bvec->bv_len;
167
168                 if (bvprv && cluster) {
169                         if (sg->length + nbytes > q->max_segment_size)
170                                 goto new_segment;
171
172                         if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
173                                 goto new_segment;
174                         if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
175                                 goto new_segment;
176
177                         sg->length += nbytes;
178                 } else {
179 new_segment:
180                         if (!sg)
181                                 sg = sglist;
182                         else {
183                                 /*
184                                  * If the driver previously mapped a shorter
185                                  * list, we could see a termination bit
186                                  * prematurely unless it fully inits the sg
187                                  * table on each mapping. We KNOW that there
188                                  * must be more entries here or the driver
189                                  * would be buggy, so force clear the
190                                  * termination bit to avoid doing a full
191                                  * sg_init_table() in drivers for each command.
192                                  */
193                                 sg->page_link &= ~0x02;
194                                 sg = sg_next(sg);
195                         }
196
197                         sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
198                         nsegs++;
199                 }
200                 bvprv = bvec;
201         } /* segments in rq */
202
203
204         if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
205             (rq->data_len & q->dma_pad_mask)) {
206                 unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1;
207
208                 sg->length += pad_len;
209                 rq->extra_len += pad_len;
210         }
211
212         if (q->dma_drain_size && q->dma_drain_needed(rq)) {
213                 if (rq->cmd_flags & REQ_RW)
214                         memset(q->dma_drain_buffer, 0, q->dma_drain_size);
215
216                 sg->page_link &= ~0x02;
217                 sg = sg_next(sg);
218                 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
219                             q->dma_drain_size,
220                             ((unsigned long)q->dma_drain_buffer) &
221                             (PAGE_SIZE - 1));
222                 nsegs++;
223                 rq->extra_len += q->dma_drain_size;
224         }
225
226         if (sg)
227                 sg_mark_end(sg);
228
229         return nsegs;
230 }
231 EXPORT_SYMBOL(blk_rq_map_sg);
232
233 static inline int ll_new_mergeable(struct request_queue *q,
234                                    struct request *req,
235                                    struct bio *bio)
236 {
237         int nr_phys_segs = bio_phys_segments(q, bio);
238
239         if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
240                 req->cmd_flags |= REQ_NOMERGE;
241                 if (req == q->last_merge)
242                         q->last_merge = NULL;
243                 return 0;
244         }
245
246         /*
247          * A hw segment is just getting larger, bump just the phys
248          * counter.
249          */
250         req->nr_phys_segments += nr_phys_segs;
251         return 1;
252 }
253
254 static inline int ll_new_hw_segment(struct request_queue *q,
255                                     struct request *req,
256                                     struct bio *bio)
257 {
258         int nr_hw_segs = bio_hw_segments(q, bio);
259         int nr_phys_segs = bio_phys_segments(q, bio);
260
261         if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
262             || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
263                 req->cmd_flags |= REQ_NOMERGE;
264                 if (req == q->last_merge)
265                         q->last_merge = NULL;
266                 return 0;
267         }
268
269         /*
270          * This will form the start of a new hw segment.  Bump both
271          * counters.
272          */
273         req->nr_hw_segments += nr_hw_segs;
274         req->nr_phys_segments += nr_phys_segs;
275         return 1;
276 }
277
278 int ll_back_merge_fn(struct request_queue *q, struct request *req,
279                      struct bio *bio)
280 {
281         unsigned short max_sectors;
282
283         if (unlikely(blk_pc_request(req)))
284                 max_sectors = q->max_hw_sectors;
285         else
286                 max_sectors = q->max_sectors;
287
288         if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
289                 req->cmd_flags |= REQ_NOMERGE;
290                 if (req == q->last_merge)
291                         q->last_merge = NULL;
292                 return 0;
293         }
294         if (!bio_flagged(req->biotail, BIO_SEG_VALID))
295                 blk_recount_segments(q, req->biotail);
296         if (!bio_flagged(bio, BIO_SEG_VALID))
297                 blk_recount_segments(q, bio);
298
299         return ll_new_hw_segment(q, req, bio);
300 }
301
302 int ll_front_merge_fn(struct request_queue *q, struct request *req,
303                       struct bio *bio)
304 {
305         unsigned short max_sectors;
306
307         if (unlikely(blk_pc_request(req)))
308                 max_sectors = q->max_hw_sectors;
309         else
310                 max_sectors = q->max_sectors;
311
312
313         if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
314                 req->cmd_flags |= REQ_NOMERGE;
315                 if (req == q->last_merge)
316                         q->last_merge = NULL;
317                 return 0;
318         }
319         if (!bio_flagged(bio, BIO_SEG_VALID))
320                 blk_recount_segments(q, bio);
321         if (!bio_flagged(req->bio, BIO_SEG_VALID))
322                 blk_recount_segments(q, req->bio);
323
324         return ll_new_hw_segment(q, req, bio);
325 }
326
327 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
328                                 struct request *next)
329 {
330         int total_phys_segments;
331         int total_hw_segments;
332
333         /*
334          * First check if the either of the requests are re-queued
335          * requests.  Can't merge them if they are.
336          */
337         if (req->special || next->special)
338                 return 0;
339
340         /*
341          * Will it become too large?
342          */
343         if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
344                 return 0;
345
346         total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
347         if (blk_phys_contig_segment(q, req->biotail, next->bio))
348                 total_phys_segments--;
349
350         if (total_phys_segments > q->max_phys_segments)
351                 return 0;
352
353         total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
354
355         if (total_hw_segments > q->max_hw_segments)
356                 return 0;
357
358         /* Merge is OK... */
359         req->nr_phys_segments = total_phys_segments;
360         req->nr_hw_segments = total_hw_segments;
361         return 1;
362 }
363
364 /*
365  * Has to be called with the request spinlock acquired
366  */
367 static int attempt_merge(struct request_queue *q, struct request *req,
368                           struct request *next)
369 {
370         if (!rq_mergeable(req) || !rq_mergeable(next))
371                 return 0;
372
373         /*
374          * not contiguous
375          */
376         if (req->sector + req->nr_sectors != next->sector)
377                 return 0;
378
379         if (rq_data_dir(req) != rq_data_dir(next)
380             || req->rq_disk != next->rq_disk
381             || next->special)
382                 return 0;
383
384         if (blk_integrity_rq(req) != blk_integrity_rq(next))
385                 return 0;
386
387         /*
388          * If we are allowed to merge, then append bio list
389          * from next to rq and release next. merge_requests_fn
390          * will have updated segment counts, update sector
391          * counts here.
392          */
393         if (!ll_merge_requests_fn(q, req, next))
394                 return 0;
395
396         /*
397          * At this point we have either done a back merge
398          * or front merge. We need the smaller start_time of
399          * the merged requests to be the current request
400          * for accounting purposes.
401          */
402         if (time_after(req->start_time, next->start_time))
403                 req->start_time = next->start_time;
404
405         req->biotail->bi_next = next->bio;
406         req->biotail = next->biotail;
407
408         req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
409
410         elv_merge_requests(q, req, next);
411
412         if (req->rq_disk) {
413                 struct hd_struct *part
414                         = get_part(req->rq_disk, req->sector);
415                 disk_round_stats(req->rq_disk);
416                 req->rq_disk->in_flight--;
417                 if (part) {
418                         part_round_stats(part);
419                         part->in_flight--;
420                 }
421         }
422
423         req->ioprio = ioprio_best(req->ioprio, next->ioprio);
424
425         __blk_put_request(q, next);
426         return 1;
427 }
428
429 int attempt_back_merge(struct request_queue *q, struct request *rq)
430 {
431         struct request *next = elv_latter_request(q, rq);
432
433         if (next)
434                 return attempt_merge(q, rq, next);
435
436         return 0;
437 }
438
439 int attempt_front_merge(struct request_queue *q, struct request *rq)
440 {
441         struct request *prev = elv_former_request(q, rq);
442
443         if (prev)
444                 return attempt_merge(q, prev, rq);
445
446         return 0;
447 }