bridge: Properly check if local fdb entry can be deleted in br_fdb_change_mac_address
[linux/fpc-iii.git] / block / blk-merge.c
blob8f8adaa95466ccc8335cde7e313b551b375ed9b9
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 unsigned int __blk_recalc_rq_segments(struct request_queue *q,
13 struct bio *bio)
15 struct bio_vec bv, bvprv = { NULL };
16 int cluster, high, highprv = 1;
17 unsigned int seg_size, nr_phys_segs;
18 struct bio *fbio, *bbio;
19 struct bvec_iter iter;
21 if (!bio)
22 return 0;
24 fbio = bio;
25 cluster = blk_queue_cluster(q);
26 seg_size = 0;
27 nr_phys_segs = 0;
28 for_each_bio(bio) {
29 bio_for_each_segment(bv, bio, iter) {
31 * the trick here is making sure that a high page is
32 * never considered part of another segment, since that
33 * might change with the bounce page.
35 high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q);
36 if (!high && !highprv && cluster) {
37 if (seg_size + bv.bv_len
38 > queue_max_segment_size(q))
39 goto new_segment;
40 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
41 goto new_segment;
42 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
43 goto new_segment;
45 seg_size += bv.bv_len;
46 bvprv = bv;
47 continue;
49 new_segment:
50 if (nr_phys_segs == 1 && seg_size >
51 fbio->bi_seg_front_size)
52 fbio->bi_seg_front_size = seg_size;
54 nr_phys_segs++;
55 bvprv = bv;
56 seg_size = bv.bv_len;
57 highprv = high;
59 bbio = bio;
62 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
63 fbio->bi_seg_front_size = seg_size;
64 if (seg_size > bbio->bi_seg_back_size)
65 bbio->bi_seg_back_size = seg_size;
67 return nr_phys_segs;
70 void blk_recalc_rq_segments(struct request *rq)
72 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
75 void blk_recount_segments(struct request_queue *q, struct bio *bio)
77 struct bio *nxt = bio->bi_next;
79 bio->bi_next = NULL;
80 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
81 bio->bi_next = nxt;
82 bio->bi_flags |= (1 << BIO_SEG_VALID);
84 EXPORT_SYMBOL(blk_recount_segments);
86 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
87 struct bio *nxt)
89 struct bio_vec end_bv = { NULL }, nxt_bv;
90 struct bvec_iter iter;
92 if (!blk_queue_cluster(q))
93 return 0;
95 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
96 queue_max_segment_size(q))
97 return 0;
99 if (!bio_has_data(bio))
100 return 1;
102 bio_for_each_segment(end_bv, bio, iter)
103 if (end_bv.bv_len == iter.bi_size)
104 break;
106 nxt_bv = bio_iovec(nxt);
108 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
109 return 0;
112 * bio and nxt are contiguous in memory; check if the queue allows
113 * these two to be merged into one
115 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
116 return 1;
118 return 0;
121 static inline void
122 __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
123 struct scatterlist *sglist, struct bio_vec *bvprv,
124 struct scatterlist **sg, int *nsegs, int *cluster)
127 int nbytes = bvec->bv_len;
129 if (*sg && *cluster) {
130 if ((*sg)->length + nbytes > queue_max_segment_size(q))
131 goto new_segment;
133 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
134 goto new_segment;
135 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
136 goto new_segment;
138 (*sg)->length += nbytes;
139 } else {
140 new_segment:
141 if (!*sg)
142 *sg = sglist;
143 else {
145 * If the driver previously mapped a shorter
146 * list, we could see a termination bit
147 * prematurely unless it fully inits the sg
148 * table on each mapping. We KNOW that there
149 * must be more entries here or the driver
150 * would be buggy, so force clear the
151 * termination bit to avoid doing a full
152 * sg_init_table() in drivers for each command.
154 sg_unmark_end(*sg);
155 *sg = sg_next(*sg);
158 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
159 (*nsegs)++;
161 *bvprv = *bvec;
165 * map a request to scatterlist, return number of sg entries setup. Caller
166 * must make sure sg can hold rq->nr_phys_segments entries
168 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
169 struct scatterlist *sglist)
171 struct bio_vec bvec, bvprv = { NULL };
172 struct req_iterator iter;
173 struct scatterlist *sg;
174 int nsegs, cluster;
176 nsegs = 0;
177 cluster = blk_queue_cluster(q);
180 * for each bio in rq
182 sg = NULL;
183 rq_for_each_segment(bvec, rq, iter) {
184 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg,
185 &nsegs, &cluster);
186 } /* segments in rq */
189 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
190 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
191 unsigned int pad_len =
192 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
194 sg->length += pad_len;
195 rq->extra_len += pad_len;
198 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
199 if (rq->cmd_flags & REQ_WRITE)
200 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
202 sg->page_link &= ~0x02;
203 sg = sg_next(sg);
204 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
205 q->dma_drain_size,
206 ((unsigned long)q->dma_drain_buffer) &
207 (PAGE_SIZE - 1));
208 nsegs++;
209 rq->extra_len += q->dma_drain_size;
212 if (sg)
213 sg_mark_end(sg);
215 return nsegs;
217 EXPORT_SYMBOL(blk_rq_map_sg);
220 * blk_bio_map_sg - map a bio to a scatterlist
221 * @q: request_queue in question
222 * @bio: bio being mapped
223 * @sglist: scatterlist being mapped
225 * Note:
226 * Caller must make sure sg can hold bio->bi_phys_segments entries
228 * Will return the number of sg entries setup
230 int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
231 struct scatterlist *sglist)
233 struct bio_vec bvec, bvprv = { NULL };
234 struct scatterlist *sg;
235 int nsegs, cluster;
236 struct bvec_iter iter;
238 nsegs = 0;
239 cluster = blk_queue_cluster(q);
241 sg = NULL;
242 bio_for_each_segment(bvec, bio, iter) {
243 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg,
244 &nsegs, &cluster);
245 } /* segments in bio */
247 if (sg)
248 sg_mark_end(sg);
250 BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
251 return nsegs;
253 EXPORT_SYMBOL(blk_bio_map_sg);
255 static inline int ll_new_hw_segment(struct request_queue *q,
256 struct request *req,
257 struct bio *bio)
259 int nr_phys_segs = bio_phys_segments(q, bio);
261 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
262 goto no_merge;
264 if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio))
265 goto no_merge;
268 * This will form the start of a new hw segment. Bump both
269 * counters.
271 req->nr_phys_segments += nr_phys_segs;
272 return 1;
274 no_merge:
275 req->cmd_flags |= REQ_NOMERGE;
276 if (req == q->last_merge)
277 q->last_merge = NULL;
278 return 0;
281 int ll_back_merge_fn(struct request_queue *q, struct request *req,
282 struct bio *bio)
284 if (blk_rq_sectors(req) + bio_sectors(bio) >
285 blk_rq_get_max_sectors(req)) {
286 req->cmd_flags |= REQ_NOMERGE;
287 if (req == q->last_merge)
288 q->last_merge = NULL;
289 return 0;
291 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
292 blk_recount_segments(q, req->biotail);
293 if (!bio_flagged(bio, BIO_SEG_VALID))
294 blk_recount_segments(q, bio);
296 return ll_new_hw_segment(q, req, bio);
299 int ll_front_merge_fn(struct request_queue *q, struct request *req,
300 struct bio *bio)
302 if (blk_rq_sectors(req) + bio_sectors(bio) >
303 blk_rq_get_max_sectors(req)) {
304 req->cmd_flags |= REQ_NOMERGE;
305 if (req == q->last_merge)
306 q->last_merge = NULL;
307 return 0;
309 if (!bio_flagged(bio, BIO_SEG_VALID))
310 blk_recount_segments(q, bio);
311 if (!bio_flagged(req->bio, BIO_SEG_VALID))
312 blk_recount_segments(q, req->bio);
314 return ll_new_hw_segment(q, req, bio);
318 * blk-mq uses req->special to carry normal driver per-request payload, it
319 * does not indicate a prepared command that we cannot merge with.
321 static bool req_no_special_merge(struct request *req)
323 struct request_queue *q = req->q;
325 return !q->mq_ops && req->special;
328 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
329 struct request *next)
331 int total_phys_segments;
332 unsigned int seg_size =
333 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
336 * First check if the either of the requests are re-queued
337 * requests. Can't merge them if they are.
339 if (req_no_special_merge(req) || req_no_special_merge(next))
340 return 0;
343 * Will it become too large?
345 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
346 blk_rq_get_max_sectors(req))
347 return 0;
349 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
350 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
351 if (req->nr_phys_segments == 1)
352 req->bio->bi_seg_front_size = seg_size;
353 if (next->nr_phys_segments == 1)
354 next->biotail->bi_seg_back_size = seg_size;
355 total_phys_segments--;
358 if (total_phys_segments > queue_max_segments(q))
359 return 0;
361 if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next))
362 return 0;
364 /* Merge is OK... */
365 req->nr_phys_segments = total_phys_segments;
366 return 1;
370 * blk_rq_set_mixed_merge - mark a request as mixed merge
371 * @rq: request to mark as mixed merge
373 * Description:
374 * @rq is about to be mixed merged. Make sure the attributes
375 * which can be mixed are set in each bio and mark @rq as mixed
376 * merged.
378 void blk_rq_set_mixed_merge(struct request *rq)
380 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
381 struct bio *bio;
383 if (rq->cmd_flags & REQ_MIXED_MERGE)
384 return;
387 * @rq will no longer represent mixable attributes for all the
388 * contained bios. It will just track those of the first one.
389 * Distributes the attributs to each bio.
391 for (bio = rq->bio; bio; bio = bio->bi_next) {
392 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
393 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
394 bio->bi_rw |= ff;
396 rq->cmd_flags |= REQ_MIXED_MERGE;
399 static void blk_account_io_merge(struct request *req)
401 if (blk_do_io_stat(req)) {
402 struct hd_struct *part;
403 int cpu;
405 cpu = part_stat_lock();
406 part = req->part;
408 part_round_stats(cpu, part);
409 part_dec_in_flight(part, rq_data_dir(req));
411 hd_struct_put(part);
412 part_stat_unlock();
417 * Has to be called with the request spinlock acquired
419 static int attempt_merge(struct request_queue *q, struct request *req,
420 struct request *next)
422 if (!rq_mergeable(req) || !rq_mergeable(next))
423 return 0;
425 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
426 return 0;
429 * not contiguous
431 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
432 return 0;
434 if (rq_data_dir(req) != rq_data_dir(next)
435 || req->rq_disk != next->rq_disk
436 || req_no_special_merge(next))
437 return 0;
439 if (req->cmd_flags & REQ_WRITE_SAME &&
440 !blk_write_same_mergeable(req->bio, next->bio))
441 return 0;
444 * If we are allowed to merge, then append bio list
445 * from next to rq and release next. merge_requests_fn
446 * will have updated segment counts, update sector
447 * counts here.
449 if (!ll_merge_requests_fn(q, req, next))
450 return 0;
453 * If failfast settings disagree or any of the two is already
454 * a mixed merge, mark both as mixed before proceeding. This
455 * makes sure that all involved bios have mixable attributes
456 * set properly.
458 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
459 (req->cmd_flags & REQ_FAILFAST_MASK) !=
460 (next->cmd_flags & REQ_FAILFAST_MASK)) {
461 blk_rq_set_mixed_merge(req);
462 blk_rq_set_mixed_merge(next);
466 * At this point we have either done a back merge
467 * or front merge. We need the smaller start_time of
468 * the merged requests to be the current request
469 * for accounting purposes.
471 if (time_after(req->start_time, next->start_time))
472 req->start_time = next->start_time;
474 req->biotail->bi_next = next->bio;
475 req->biotail = next->biotail;
477 req->__data_len += blk_rq_bytes(next);
479 elv_merge_requests(q, req, next);
482 * 'next' is going away, so update stats accordingly
484 blk_account_io_merge(next);
486 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
487 if (blk_rq_cpu_valid(next))
488 req->cpu = next->cpu;
490 /* owner-ship of bio passed from next to req */
491 next->bio = NULL;
492 __blk_put_request(q, next);
493 return 1;
496 int attempt_back_merge(struct request_queue *q, struct request *rq)
498 struct request *next = elv_latter_request(q, rq);
500 if (next)
501 return attempt_merge(q, rq, next);
503 return 0;
506 int attempt_front_merge(struct request_queue *q, struct request *rq)
508 struct request *prev = elv_former_request(q, rq);
510 if (prev)
511 return attempt_merge(q, prev, rq);
513 return 0;
516 int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
517 struct request *next)
519 return attempt_merge(q, rq, next);
522 bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
524 if (!rq_mergeable(rq) || !bio_mergeable(bio))
525 return false;
527 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
528 return false;
530 /* different data direction or already started, don't merge */
531 if (bio_data_dir(bio) != rq_data_dir(rq))
532 return false;
534 /* must be same device and not a special request */
535 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
536 return false;
538 /* only merge integrity protected bio into ditto rq */
539 if (bio_integrity(bio) != blk_integrity_rq(rq))
540 return false;
542 /* must be using the same buffer */
543 if (rq->cmd_flags & REQ_WRITE_SAME &&
544 !blk_write_same_mergeable(rq->bio, bio))
545 return false;
547 return true;
550 int blk_try_merge(struct request *rq, struct bio *bio)
552 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
553 return ELEVATOR_BACK_MERGE;
554 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
555 return ELEVATOR_FRONT_MERGE;
556 return ELEVATOR_NO_MERGE;