scatterlist: remove open coded sg_unmark_end instances
[linux/fpc-iii.git] / block / blk-merge.c
blob25ebac8560cf3322ab65f20276ace85c10d83bdb
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>
10 #include "blk.h"
12 static struct bio *blk_bio_discard_split(struct request_queue *q,
13 struct bio *bio,
14 struct bio_set *bs)
16 unsigned int max_discard_sectors, granularity;
17 int alignment;
18 sector_t tmp;
19 unsigned split_sectors;
21 /* Zero-sector (unknown) and one-sector granularities are the same. */
22 granularity = max(q->limits.discard_granularity >> 9, 1U);
24 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
25 max_discard_sectors -= max_discard_sectors % granularity;
27 if (unlikely(!max_discard_sectors)) {
28 /* XXX: warn */
29 return NULL;
32 if (bio_sectors(bio) <= max_discard_sectors)
33 return NULL;
35 split_sectors = max_discard_sectors;
38 * If the next starting sector would be misaligned, stop the discard at
39 * the previous aligned sector.
41 alignment = (q->limits.discard_alignment >> 9) % granularity;
43 tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
44 tmp = sector_div(tmp, granularity);
46 if (split_sectors > tmp)
47 split_sectors -= tmp;
49 return bio_split(bio, split_sectors, GFP_NOIO, bs);
52 static struct bio *blk_bio_write_same_split(struct request_queue *q,
53 struct bio *bio,
54 struct bio_set *bs)
56 if (!q->limits.max_write_same_sectors)
57 return NULL;
59 if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
60 return NULL;
62 return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
65 static struct bio *blk_bio_segment_split(struct request_queue *q,
66 struct bio *bio,
67 struct bio_set *bs)
69 struct bio *split;
70 struct bio_vec bv, bvprv;
71 struct bvec_iter iter;
72 unsigned seg_size = 0, nsegs = 0, sectors = 0;
73 int prev = 0;
75 bio_for_each_segment(bv, bio, iter) {
76 sectors += bv.bv_len >> 9;
78 if (sectors > queue_max_sectors(q))
79 goto split;
82 * If the queue doesn't support SG gaps and adding this
83 * offset would create a gap, disallow it.
85 if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS) &&
86 prev && bvec_gap_to_prev(&bvprv, bv.bv_offset))
87 goto split;
89 if (prev && blk_queue_cluster(q)) {
90 if (seg_size + bv.bv_len > queue_max_segment_size(q))
91 goto new_segment;
92 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
93 goto new_segment;
94 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
95 goto new_segment;
97 seg_size += bv.bv_len;
98 bvprv = bv;
99 prev = 1;
100 continue;
102 new_segment:
103 if (nsegs == queue_max_segments(q))
104 goto split;
106 nsegs++;
107 bvprv = bv;
108 prev = 1;
109 seg_size = bv.bv_len;
112 return NULL;
113 split:
114 split = bio_clone_bioset(bio, GFP_NOIO, bs);
116 split->bi_iter.bi_size -= iter.bi_size;
117 bio->bi_iter = iter;
119 if (bio_integrity(bio)) {
120 bio_integrity_advance(bio, split->bi_iter.bi_size);
121 bio_integrity_trim(split, 0, bio_sectors(split));
124 return split;
127 void blk_queue_split(struct request_queue *q, struct bio **bio,
128 struct bio_set *bs)
130 struct bio *split;
132 if ((*bio)->bi_rw & REQ_DISCARD)
133 split = blk_bio_discard_split(q, *bio, bs);
134 else if ((*bio)->bi_rw & REQ_WRITE_SAME)
135 split = blk_bio_write_same_split(q, *bio, bs);
136 else
137 split = blk_bio_segment_split(q, *bio, q->bio_split);
139 if (split) {
140 bio_chain(split, *bio);
141 generic_make_request(*bio);
142 *bio = split;
145 EXPORT_SYMBOL(blk_queue_split);
147 static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
148 struct bio *bio,
149 bool no_sg_merge)
151 struct bio_vec bv, bvprv = { NULL };
152 int cluster, prev = 0;
153 unsigned int seg_size, nr_phys_segs;
154 struct bio *fbio, *bbio;
155 struct bvec_iter iter;
157 if (!bio)
158 return 0;
161 * This should probably be returning 0, but blk_add_request_payload()
162 * (Christoph!!!!)
164 if (bio->bi_rw & REQ_DISCARD)
165 return 1;
167 if (bio->bi_rw & REQ_WRITE_SAME)
168 return 1;
170 fbio = bio;
171 cluster = blk_queue_cluster(q);
172 seg_size = 0;
173 nr_phys_segs = 0;
174 for_each_bio(bio) {
175 bio_for_each_segment(bv, bio, iter) {
177 * If SG merging is disabled, each bio vector is
178 * a segment
180 if (no_sg_merge)
181 goto new_segment;
183 if (prev && cluster) {
184 if (seg_size + bv.bv_len
185 > queue_max_segment_size(q))
186 goto new_segment;
187 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
188 goto new_segment;
189 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
190 goto new_segment;
192 seg_size += bv.bv_len;
193 bvprv = bv;
194 continue;
196 new_segment:
197 if (nr_phys_segs == 1 && seg_size >
198 fbio->bi_seg_front_size)
199 fbio->bi_seg_front_size = seg_size;
201 nr_phys_segs++;
202 bvprv = bv;
203 prev = 1;
204 seg_size = bv.bv_len;
206 bbio = bio;
209 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
210 fbio->bi_seg_front_size = seg_size;
211 if (seg_size > bbio->bi_seg_back_size)
212 bbio->bi_seg_back_size = seg_size;
214 return nr_phys_segs;
217 void blk_recalc_rq_segments(struct request *rq)
219 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
220 &rq->q->queue_flags);
222 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
223 no_sg_merge);
226 void blk_recount_segments(struct request_queue *q, struct bio *bio)
228 unsigned short seg_cnt;
230 /* estimate segment number by bi_vcnt for non-cloned bio */
231 if (bio_flagged(bio, BIO_CLONED))
232 seg_cnt = bio_segments(bio);
233 else
234 seg_cnt = bio->bi_vcnt;
236 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
237 (seg_cnt < queue_max_segments(q)))
238 bio->bi_phys_segments = seg_cnt;
239 else {
240 struct bio *nxt = bio->bi_next;
242 bio->bi_next = NULL;
243 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
244 bio->bi_next = nxt;
247 bio_set_flag(bio, BIO_SEG_VALID);
249 EXPORT_SYMBOL(blk_recount_segments);
251 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
252 struct bio *nxt)
254 struct bio_vec end_bv = { NULL }, nxt_bv;
255 struct bvec_iter iter;
257 if (!blk_queue_cluster(q))
258 return 0;
260 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
261 queue_max_segment_size(q))
262 return 0;
264 if (!bio_has_data(bio))
265 return 1;
267 bio_for_each_segment(end_bv, bio, iter)
268 if (end_bv.bv_len == iter.bi_size)
269 break;
271 nxt_bv = bio_iovec(nxt);
273 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
274 return 0;
277 * bio and nxt are contiguous in memory; check if the queue allows
278 * these two to be merged into one
280 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
281 return 1;
283 return 0;
286 static inline void
287 __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
288 struct scatterlist *sglist, struct bio_vec *bvprv,
289 struct scatterlist **sg, int *nsegs, int *cluster)
292 int nbytes = bvec->bv_len;
294 if (*sg && *cluster) {
295 if ((*sg)->length + nbytes > queue_max_segment_size(q))
296 goto new_segment;
298 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
299 goto new_segment;
300 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
301 goto new_segment;
303 (*sg)->length += nbytes;
304 } else {
305 new_segment:
306 if (!*sg)
307 *sg = sglist;
308 else {
310 * If the driver previously mapped a shorter
311 * list, we could see a termination bit
312 * prematurely unless it fully inits the sg
313 * table on each mapping. We KNOW that there
314 * must be more entries here or the driver
315 * would be buggy, so force clear the
316 * termination bit to avoid doing a full
317 * sg_init_table() in drivers for each command.
319 sg_unmark_end(*sg);
320 *sg = sg_next(*sg);
323 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
324 (*nsegs)++;
326 *bvprv = *bvec;
329 static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
330 struct scatterlist *sglist,
331 struct scatterlist **sg)
333 struct bio_vec bvec, bvprv = { NULL };
334 struct bvec_iter iter;
335 int nsegs, cluster;
337 nsegs = 0;
338 cluster = blk_queue_cluster(q);
340 if (bio->bi_rw & REQ_DISCARD) {
342 * This is a hack - drivers should be neither modifying the
343 * biovec, nor relying on bi_vcnt - but because of
344 * blk_add_request_payload(), a discard bio may or may not have
345 * a payload we need to set up here (thank you Christoph) and
346 * bi_vcnt is really the only way of telling if we need to.
349 if (bio->bi_vcnt)
350 goto single_segment;
352 return 0;
355 if (bio->bi_rw & REQ_WRITE_SAME) {
356 single_segment:
357 *sg = sglist;
358 bvec = bio_iovec(bio);
359 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
360 return 1;
363 for_each_bio(bio)
364 bio_for_each_segment(bvec, bio, iter)
365 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
366 &nsegs, &cluster);
368 return nsegs;
372 * map a request to scatterlist, return number of sg entries setup. Caller
373 * must make sure sg can hold rq->nr_phys_segments entries
375 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
376 struct scatterlist *sglist)
378 struct scatterlist *sg = NULL;
379 int nsegs = 0;
381 if (rq->bio)
382 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
384 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
385 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
386 unsigned int pad_len =
387 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
389 sg->length += pad_len;
390 rq->extra_len += pad_len;
393 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
394 if (rq->cmd_flags & REQ_WRITE)
395 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
397 sg_unmark_end(sg);
398 sg = sg_next(sg);
399 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
400 q->dma_drain_size,
401 ((unsigned long)q->dma_drain_buffer) &
402 (PAGE_SIZE - 1));
403 nsegs++;
404 rq->extra_len += q->dma_drain_size;
407 if (sg)
408 sg_mark_end(sg);
410 return nsegs;
412 EXPORT_SYMBOL(blk_rq_map_sg);
414 static inline int ll_new_hw_segment(struct request_queue *q,
415 struct request *req,
416 struct bio *bio)
418 int nr_phys_segs = bio_phys_segments(q, bio);
420 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
421 goto no_merge;
423 if (blk_integrity_merge_bio(q, req, bio) == false)
424 goto no_merge;
427 * This will form the start of a new hw segment. Bump both
428 * counters.
430 req->nr_phys_segments += nr_phys_segs;
431 return 1;
433 no_merge:
434 req->cmd_flags |= REQ_NOMERGE;
435 if (req == q->last_merge)
436 q->last_merge = NULL;
437 return 0;
440 int ll_back_merge_fn(struct request_queue *q, struct request *req,
441 struct bio *bio)
443 if (blk_rq_sectors(req) + bio_sectors(bio) >
444 blk_rq_get_max_sectors(req)) {
445 req->cmd_flags |= REQ_NOMERGE;
446 if (req == q->last_merge)
447 q->last_merge = NULL;
448 return 0;
450 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
451 blk_recount_segments(q, req->biotail);
452 if (!bio_flagged(bio, BIO_SEG_VALID))
453 blk_recount_segments(q, bio);
455 return ll_new_hw_segment(q, req, bio);
458 int ll_front_merge_fn(struct request_queue *q, struct request *req,
459 struct bio *bio)
461 if (blk_rq_sectors(req) + bio_sectors(bio) >
462 blk_rq_get_max_sectors(req)) {
463 req->cmd_flags |= REQ_NOMERGE;
464 if (req == q->last_merge)
465 q->last_merge = NULL;
466 return 0;
468 if (!bio_flagged(bio, BIO_SEG_VALID))
469 blk_recount_segments(q, bio);
470 if (!bio_flagged(req->bio, BIO_SEG_VALID))
471 blk_recount_segments(q, req->bio);
473 return ll_new_hw_segment(q, req, bio);
477 * blk-mq uses req->special to carry normal driver per-request payload, it
478 * does not indicate a prepared command that we cannot merge with.
480 static bool req_no_special_merge(struct request *req)
482 struct request_queue *q = req->q;
484 return !q->mq_ops && req->special;
487 static int req_gap_to_prev(struct request *req, struct request *next)
489 struct bio *prev = req->biotail;
491 return bvec_gap_to_prev(&prev->bi_io_vec[prev->bi_vcnt - 1],
492 next->bio->bi_io_vec[0].bv_offset);
495 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
496 struct request *next)
498 int total_phys_segments;
499 unsigned int seg_size =
500 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
503 * First check if the either of the requests are re-queued
504 * requests. Can't merge them if they are.
506 if (req_no_special_merge(req) || req_no_special_merge(next))
507 return 0;
509 if (test_bit(QUEUE_FLAG_SG_GAPS, &q->queue_flags) &&
510 req_gap_to_prev(req, next))
511 return 0;
514 * Will it become too large?
516 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
517 blk_rq_get_max_sectors(req))
518 return 0;
520 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
521 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
522 if (req->nr_phys_segments == 1)
523 req->bio->bi_seg_front_size = seg_size;
524 if (next->nr_phys_segments == 1)
525 next->biotail->bi_seg_back_size = seg_size;
526 total_phys_segments--;
529 if (total_phys_segments > queue_max_segments(q))
530 return 0;
532 if (blk_integrity_merge_rq(q, req, next) == false)
533 return 0;
535 /* Merge is OK... */
536 req->nr_phys_segments = total_phys_segments;
537 return 1;
541 * blk_rq_set_mixed_merge - mark a request as mixed merge
542 * @rq: request to mark as mixed merge
544 * Description:
545 * @rq is about to be mixed merged. Make sure the attributes
546 * which can be mixed are set in each bio and mark @rq as mixed
547 * merged.
549 void blk_rq_set_mixed_merge(struct request *rq)
551 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
552 struct bio *bio;
554 if (rq->cmd_flags & REQ_MIXED_MERGE)
555 return;
558 * @rq will no longer represent mixable attributes for all the
559 * contained bios. It will just track those of the first one.
560 * Distributes the attributs to each bio.
562 for (bio = rq->bio; bio; bio = bio->bi_next) {
563 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
564 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
565 bio->bi_rw |= ff;
567 rq->cmd_flags |= REQ_MIXED_MERGE;
570 static void blk_account_io_merge(struct request *req)
572 if (blk_do_io_stat(req)) {
573 struct hd_struct *part;
574 int cpu;
576 cpu = part_stat_lock();
577 part = req->part;
579 part_round_stats(cpu, part);
580 part_dec_in_flight(part, rq_data_dir(req));
582 hd_struct_put(part);
583 part_stat_unlock();
588 * Has to be called with the request spinlock acquired
590 static int attempt_merge(struct request_queue *q, struct request *req,
591 struct request *next)
593 if (!rq_mergeable(req) || !rq_mergeable(next))
594 return 0;
596 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
597 return 0;
600 * not contiguous
602 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
603 return 0;
605 if (rq_data_dir(req) != rq_data_dir(next)
606 || req->rq_disk != next->rq_disk
607 || req_no_special_merge(next))
608 return 0;
610 if (req->cmd_flags & REQ_WRITE_SAME &&
611 !blk_write_same_mergeable(req->bio, next->bio))
612 return 0;
615 * If we are allowed to merge, then append bio list
616 * from next to rq and release next. merge_requests_fn
617 * will have updated segment counts, update sector
618 * counts here.
620 if (!ll_merge_requests_fn(q, req, next))
621 return 0;
624 * If failfast settings disagree or any of the two is already
625 * a mixed merge, mark both as mixed before proceeding. This
626 * makes sure that all involved bios have mixable attributes
627 * set properly.
629 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
630 (req->cmd_flags & REQ_FAILFAST_MASK) !=
631 (next->cmd_flags & REQ_FAILFAST_MASK)) {
632 blk_rq_set_mixed_merge(req);
633 blk_rq_set_mixed_merge(next);
637 * At this point we have either done a back merge
638 * or front merge. We need the smaller start_time of
639 * the merged requests to be the current request
640 * for accounting purposes.
642 if (time_after(req->start_time, next->start_time))
643 req->start_time = next->start_time;
645 req->biotail->bi_next = next->bio;
646 req->biotail = next->biotail;
648 req->__data_len += blk_rq_bytes(next);
650 elv_merge_requests(q, req, next);
653 * 'next' is going away, so update stats accordingly
655 blk_account_io_merge(next);
657 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
658 if (blk_rq_cpu_valid(next))
659 req->cpu = next->cpu;
661 /* owner-ship of bio passed from next to req */
662 next->bio = NULL;
663 __blk_put_request(q, next);
664 return 1;
667 int attempt_back_merge(struct request_queue *q, struct request *rq)
669 struct request *next = elv_latter_request(q, rq);
671 if (next)
672 return attempt_merge(q, rq, next);
674 return 0;
677 int attempt_front_merge(struct request_queue *q, struct request *rq)
679 struct request *prev = elv_former_request(q, rq);
681 if (prev)
682 return attempt_merge(q, prev, rq);
684 return 0;
687 int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
688 struct request *next)
690 return attempt_merge(q, rq, next);
693 bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
695 struct request_queue *q = rq->q;
697 if (!rq_mergeable(rq) || !bio_mergeable(bio))
698 return false;
700 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
701 return false;
703 /* different data direction or already started, don't merge */
704 if (bio_data_dir(bio) != rq_data_dir(rq))
705 return false;
707 /* must be same device and not a special request */
708 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
709 return false;
711 /* only merge integrity protected bio into ditto rq */
712 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
713 return false;
715 /* must be using the same buffer */
716 if (rq->cmd_flags & REQ_WRITE_SAME &&
717 !blk_write_same_mergeable(rq->bio, bio))
718 return false;
720 /* Only check gaps if the bio carries data */
721 if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS) && bio_has_data(bio)) {
722 struct bio_vec *bprev;
724 bprev = &rq->biotail->bi_io_vec[rq->biotail->bi_vcnt - 1];
725 if (bvec_gap_to_prev(bprev, bio->bi_io_vec[0].bv_offset))
726 return false;
729 return true;
732 int blk_try_merge(struct request *rq, struct bio *bio)
734 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
735 return ELEVATOR_BACK_MERGE;
736 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
737 return ELEVATOR_FRONT_MERGE;
738 return ELEVATOR_NO_MERGE;