]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-merge.c
block: add partition attribute for partition number
[mirror_ubuntu-bionic-kernel.git] / block / blk-merge.c
CommitLineData
d6d48196
JA
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
12void blk_recalc_rq_sectors(struct request *rq, int nsect)
13{
e17fc0a1 14 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
d6d48196
JA
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) {
6728cb0e 35 printk(KERN_ERR "blk: request botched\n");
d6d48196
JA
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;
d6d48196 44 unsigned int phys_size;
d6d48196
JA
45 struct bio_vec *bv, *bvprv = NULL;
46 int seg_size;
d6d48196
JA
47 int cluster;
48 struct req_iterator iter;
49 int high, highprv = 1;
50 struct request_queue *q = rq->q;
51
52 if (!rq->bio)
53 return;
54
75ad23bc 55 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
5df97b91
MP
56 seg_size = 0;
57 phys_size = nr_phys_segs = 0;
d6d48196
JA
58 rq_for_each_segment(bv, rq, iter) {
59 /*
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.
63 */
64 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
65 if (high || highprv)
b8b3e16c 66 goto new_segment;
d6d48196
JA
67 if (cluster) {
68 if (seg_size + bv->bv_len > q->max_segment_size)
69 goto new_segment;
70 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
71 goto new_segment;
72 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
73 goto new_segment;
d6d48196
JA
74
75 seg_size += bv->bv_len;
d6d48196
JA
76 bvprv = bv;
77 continue;
78 }
79new_segment:
d6d48196
JA
80 nr_phys_segs++;
81 bvprv = bv;
82 seg_size = bv->bv_len;
83 highprv = high;
84 }
85
d6d48196 86 rq->nr_phys_segments = nr_phys_segs;
d6d48196
JA
87}
88
89void blk_recount_segments(struct request_queue *q, struct bio *bio)
90{
91 struct request rq;
92 struct bio *nxt = bio->bi_next;
93 rq.q = q;
94 rq.bio = rq.biotail = bio;
95 bio->bi_next = NULL;
96 blk_recalc_rq_segments(&rq);
97 bio->bi_next = nxt;
98 bio->bi_phys_segments = rq.nr_phys_segments;
d6d48196
JA
99 bio->bi_flags |= (1 << BIO_SEG_VALID);
100}
101EXPORT_SYMBOL(blk_recount_segments);
102
103static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
104 struct bio *nxt)
105{
75ad23bc 106 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
d6d48196
JA
107 return 0;
108
d6d48196
JA
109 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
110 return 0;
111
e17fc0a1
DW
112 if (!bio_has_data(bio))
113 return 1;
114
115 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
116 return 0;
117
d6d48196 118 /*
e17fc0a1 119 * bio and nxt are contiguous in memory; check if the queue allows
d6d48196
JA
120 * these two to be merged into one
121 */
122 if (BIO_SEG_BOUNDARY(q, bio, nxt))
123 return 1;
124
125 return 0;
126}
127
d6d48196
JA
128/*
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
131 */
132int blk_rq_map_sg(struct request_queue *q, struct request *rq,
133 struct scatterlist *sglist)
134{
135 struct bio_vec *bvec, *bvprv;
136 struct req_iterator iter;
137 struct scatterlist *sg;
138 int nsegs, cluster;
139
140 nsegs = 0;
75ad23bc 141 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
d6d48196
JA
142
143 /*
144 * for each bio in rq
145 */
146 bvprv = NULL;
147 sg = NULL;
148 rq_for_each_segment(bvec, rq, iter) {
149 int nbytes = bvec->bv_len;
150
151 if (bvprv && cluster) {
152 if (sg->length + nbytes > q->max_segment_size)
153 goto new_segment;
154
155 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
156 goto new_segment;
157 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
158 goto new_segment;
159
160 sg->length += nbytes;
161 } else {
162new_segment:
163 if (!sg)
164 sg = sglist;
165 else {
166 /*
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.
175 */
176 sg->page_link &= ~0x02;
177 sg = sg_next(sg);
178 }
179
180 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
181 nsegs++;
182 }
183 bvprv = bvec;
184 } /* segments in rq */
185
f18573ab
FT
186
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;
190
191 sg->length += pad_len;
192 rq->extra_len += pad_len;
193 }
194
2fb98e84 195 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
db0a2e00
TH
196 if (rq->cmd_flags & REQ_RW)
197 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
198
d6d48196
JA
199 sg->page_link &= ~0x02;
200 sg = sg_next(sg);
201 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
202 q->dma_drain_size,
203 ((unsigned long)q->dma_drain_buffer) &
204 (PAGE_SIZE - 1));
205 nsegs++;
7a85f889 206 rq->extra_len += q->dma_drain_size;
d6d48196
JA
207 }
208
209 if (sg)
210 sg_mark_end(sg);
211
212 return nsegs;
213}
d6d48196
JA
214EXPORT_SYMBOL(blk_rq_map_sg);
215
216static inline int ll_new_mergeable(struct request_queue *q,
217 struct request *req,
218 struct bio *bio)
219{
220 int nr_phys_segs = bio_phys_segments(q, bio);
221
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;
226 return 0;
227 }
228
229 /*
230 * A hw segment is just getting larger, bump just the phys
231 * counter.
232 */
233 req->nr_phys_segments += nr_phys_segs;
234 return 1;
235}
236
237static inline int ll_new_hw_segment(struct request_queue *q,
238 struct request *req,
239 struct bio *bio)
240{
d6d48196
JA
241 int nr_phys_segs = bio_phys_segments(q, bio);
242
5df97b91 243 if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
d6d48196
JA
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;
248 return 0;
249 }
250
251 /*
252 * This will form the start of a new hw segment. Bump both
253 * counters.
254 */
d6d48196
JA
255 req->nr_phys_segments += nr_phys_segs;
256 return 1;
257}
258
259int ll_back_merge_fn(struct request_queue *q, struct request *req,
260 struct bio *bio)
261{
262 unsigned short max_sectors;
d6d48196
JA
263
264 if (unlikely(blk_pc_request(req)))
265 max_sectors = q->max_hw_sectors;
266 else
267 max_sectors = q->max_sectors;
268
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;
273 return 0;
274 }
2cdf79ca 275 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
d6d48196 276 blk_recount_segments(q, req->biotail);
2cdf79ca 277 if (!bio_flagged(bio, BIO_SEG_VALID))
d6d48196 278 blk_recount_segments(q, bio);
d6d48196
JA
279
280 return ll_new_hw_segment(q, req, bio);
281}
282
6728cb0e 283int ll_front_merge_fn(struct request_queue *q, struct request *req,
d6d48196
JA
284 struct bio *bio)
285{
286 unsigned short max_sectors;
d6d48196
JA
287
288 if (unlikely(blk_pc_request(req)))
289 max_sectors = q->max_hw_sectors;
290 else
291 max_sectors = q->max_sectors;
292
293
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;
298 return 0;
299 }
2cdf79ca 300 if (!bio_flagged(bio, BIO_SEG_VALID))
d6d48196 301 blk_recount_segments(q, bio);
2cdf79ca 302 if (!bio_flagged(req->bio, BIO_SEG_VALID))
d6d48196 303 blk_recount_segments(q, req->bio);
d6d48196
JA
304
305 return ll_new_hw_segment(q, req, bio);
306}
307
308static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
309 struct request *next)
310{
311 int total_phys_segments;
d6d48196
JA
312
313 /*
314 * First check if the either of the requests are re-queued
315 * requests. Can't merge them if they are.
316 */
317 if (req->special || next->special)
318 return 0;
319
320 /*
321 * Will it become too large?
322 */
323 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
324 return 0;
325
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--;
329
330 if (total_phys_segments > q->max_phys_segments)
331 return 0;
332
5df97b91 333 if (total_phys_segments > q->max_hw_segments)
d6d48196
JA
334 return 0;
335
336 /* Merge is OK... */
337 req->nr_phys_segments = total_phys_segments;
d6d48196
JA
338 return 1;
339}
340
341/*
342 * Has to be called with the request spinlock acquired
343 */
344static int attempt_merge(struct request_queue *q, struct request *req,
345 struct request *next)
346{
347 if (!rq_mergeable(req) || !rq_mergeable(next))
348 return 0;
349
350 /*
351 * not contiguous
352 */
353 if (req->sector + req->nr_sectors != next->sector)
354 return 0;
355
356 if (rq_data_dir(req) != rq_data_dir(next)
357 || req->rq_disk != next->rq_disk
358 || next->special)
359 return 0;
360
7ba1ba12
MP
361 if (blk_integrity_rq(req) != blk_integrity_rq(next))
362 return 0;
363
d6d48196
JA
364 /*
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
368 * counts here.
369 */
370 if (!ll_merge_requests_fn(q, req, next))
371 return 0;
372
373 /*
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.
378 */
379 if (time_after(req->start_time, next->start_time))
380 req->start_time = next->start_time;
381
382 req->biotail->bi_next = next->bio;
383 req->biotail = next->biotail;
384
385 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
386
387 elv_merge_requests(q, req, next);
388
389 if (req->rq_disk) {
e71bf0d0 390 struct hd_struct *part;
c9959059 391 int cpu;
e71bf0d0 392
074a7aca 393 cpu = part_stat_lock();
e71bf0d0 394 part = disk_map_sector_rcu(req->rq_disk, req->sector);
c9959059 395
074a7aca
TH
396 part_round_stats(cpu, part);
397 part_dec_in_flight(part);
e71bf0d0 398
074a7aca 399 part_stat_unlock();
d6d48196
JA
400 }
401
402 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
ab780f1e
JA
403 if (blk_rq_cpu_valid(next))
404 req->cpu = next->cpu;
d6d48196
JA
405
406 __blk_put_request(q, next);
407 return 1;
408}
409
410int attempt_back_merge(struct request_queue *q, struct request *rq)
411{
412 struct request *next = elv_latter_request(q, rq);
413
414 if (next)
415 return attempt_merge(q, rq, next);
416
417 return 0;
418}
419
420int attempt_front_merge(struct request_queue *q, struct request *rq)
421{
422 struct request *prev = elv_former_request(q, rq);
423
424 if (prev)
425 return attempt_merge(q, prev, rq);
426
427 return 0;
428}