• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
blk_bio_discard_split(struct request_queue * q,struct bio * bio,struct bio_set * bs,unsigned * nsegs)12 static struct bio *blk_bio_discard_split(struct request_queue *q,
13 					 struct bio *bio,
14 					 struct bio_set *bs,
15 					 unsigned *nsegs)
16 {
17 	unsigned int max_discard_sectors, granularity;
18 	int alignment;
19 	sector_t tmp;
20 	unsigned split_sectors;
21 
22 	*nsegs = 1;
23 
24 	/* Zero-sector (unknown) and one-sector granularities are the same.  */
25 	granularity = max(q->limits.discard_granularity >> 9, 1U);
26 
27 	max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
28 	max_discard_sectors -= max_discard_sectors % granularity;
29 
30 	if (unlikely(!max_discard_sectors)) {
31 		/* XXX: warn */
32 		return NULL;
33 	}
34 
35 	if (bio_sectors(bio) <= max_discard_sectors)
36 		return NULL;
37 
38 	split_sectors = max_discard_sectors;
39 
40 	/*
41 	 * If the next starting sector would be misaligned, stop the discard at
42 	 * the previous aligned sector.
43 	 */
44 	alignment = (q->limits.discard_alignment >> 9) % granularity;
45 
46 	tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
47 	tmp = sector_div(tmp, granularity);
48 
49 	if (split_sectors > tmp)
50 		split_sectors -= tmp;
51 
52 	return bio_split(bio, split_sectors, GFP_NOIO, bs);
53 }
54 
blk_bio_write_same_split(struct request_queue * q,struct bio * bio,struct bio_set * bs,unsigned * nsegs)55 static struct bio *blk_bio_write_same_split(struct request_queue *q,
56 					    struct bio *bio,
57 					    struct bio_set *bs,
58 					    unsigned *nsegs)
59 {
60 	*nsegs = 1;
61 
62 	if (!q->limits.max_write_same_sectors)
63 		return NULL;
64 
65 	if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
66 		return NULL;
67 
68 	return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
69 }
70 
get_max_io_size(struct request_queue * q,struct bio * bio)71 static inline unsigned get_max_io_size(struct request_queue *q,
72 				       struct bio *bio)
73 {
74 	unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector);
75 	unsigned mask = queue_logical_block_size(q) - 1;
76 
77 	/* aligned to logical block size */
78 	sectors &= ~(mask >> 9);
79 
80 	return sectors;
81 }
82 
blk_bio_segment_split(struct request_queue * q,struct bio * bio,struct bio_set * bs,unsigned * segs)83 static struct bio *blk_bio_segment_split(struct request_queue *q,
84 					 struct bio *bio,
85 					 struct bio_set *bs,
86 					 unsigned *segs)
87 {
88 	struct bio_vec bv, bvprv, *bvprvp = NULL;
89 	struct bvec_iter iter;
90 	unsigned seg_size = 0, nsegs = 0, sectors = 0;
91 	unsigned front_seg_size = bio->bi_seg_front_size;
92 	bool do_split = true;
93 	struct bio *new = NULL;
94 	const unsigned max_sectors = get_max_io_size(q, bio);
95 	unsigned bvecs = 0;
96 
97 	bio_for_each_segment(bv, bio, iter) {
98 		/*
99 		 * With arbitrary bio size, the incoming bio may be very
100 		 * big. We have to split the bio into small bios so that
101 		 * each holds at most BIO_MAX_PAGES bvecs because
102 		 * bio_clone() can fail to allocate big bvecs.
103 		 *
104 		 * It should have been better to apply the limit per
105 		 * request queue in which bio_clone() is involved,
106 		 * instead of globally. The biggest blocker is the
107 		 * bio_clone() in bio bounce.
108 		 *
109 		 * If bio is splitted by this reason, we should have
110 		 * allowed to continue bios merging, but don't do
111 		 * that now for making the change simple.
112 		 *
113 		 * TODO: deal with bio bounce's bio_clone() gracefully
114 		 * and convert the global limit into per-queue limit.
115 		 */
116 		if (bvecs++ >= BIO_MAX_PAGES)
117 			goto split;
118 
119 		/*
120 		 * If the queue doesn't support SG gaps and adding this
121 		 * offset would create a gap, disallow it.
122 		 */
123 		if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
124 			goto split;
125 
126 		if (sectors + (bv.bv_len >> 9) > max_sectors) {
127 			/*
128 			 * Consider this a new segment if we're splitting in
129 			 * the middle of this vector.
130 			 */
131 			if (nsegs < queue_max_segments(q) &&
132 			    sectors < max_sectors) {
133 				nsegs++;
134 				sectors = max_sectors;
135 			}
136 			if (sectors)
137 				goto split;
138 			/* Make this single bvec as the 1st segment */
139 		}
140 
141 		if (bvprvp && blk_queue_cluster(q)) {
142 			if (seg_size + bv.bv_len > queue_max_segment_size(q))
143 				goto new_segment;
144 			if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
145 				goto new_segment;
146 			if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
147 				goto new_segment;
148 
149 			seg_size += bv.bv_len;
150 			bvprv = bv;
151 			bvprvp = &bvprv;
152 			sectors += bv.bv_len >> 9;
153 
154 			if (nsegs == 1 && seg_size > front_seg_size)
155 				front_seg_size = seg_size;
156 			continue;
157 		}
158 new_segment:
159 		if (nsegs == queue_max_segments(q))
160 			goto split;
161 
162 		nsegs++;
163 		bvprv = bv;
164 		bvprvp = &bvprv;
165 		seg_size = bv.bv_len;
166 		sectors += bv.bv_len >> 9;
167 
168 		if (nsegs == 1 && seg_size > front_seg_size)
169 			front_seg_size = seg_size;
170 	}
171 
172 	do_split = false;
173 split:
174 	*segs = nsegs;
175 
176 	if (do_split) {
177 		new = bio_split(bio, sectors, GFP_NOIO, bs);
178 		if (new)
179 			bio = new;
180 	}
181 
182 	bio->bi_seg_front_size = front_seg_size;
183 	if (seg_size > bio->bi_seg_back_size)
184 		bio->bi_seg_back_size = seg_size;
185 
186 	return do_split ? new : NULL;
187 }
188 
blk_queue_split(struct request_queue * q,struct bio ** bio,struct bio_set * bs)189 void blk_queue_split(struct request_queue *q, struct bio **bio,
190 		     struct bio_set *bs)
191 {
192 	struct bio *split, *res;
193 	unsigned nsegs;
194 
195 	if ((*bio)->bi_rw & REQ_DISCARD)
196 		split = blk_bio_discard_split(q, *bio, bs, &nsegs);
197 	else if ((*bio)->bi_rw & REQ_WRITE_SAME)
198 		split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
199 	else
200 		split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs);
201 
202 	/* physical segments can be figured out during splitting */
203 	res = split ? split : *bio;
204 	res->bi_phys_segments = nsegs;
205 	bio_set_flag(res, BIO_SEG_VALID);
206 
207 	if (split) {
208 		/* there isn't chance to merge the splitted bio */
209 		split->bi_rw |= REQ_NOMERGE;
210 
211 		bio_chain(split, *bio);
212 		generic_make_request(*bio);
213 		*bio = split;
214 	}
215 }
216 EXPORT_SYMBOL(blk_queue_split);
217 
__blk_recalc_rq_segments(struct request_queue * q,struct bio * bio,bool no_sg_merge)218 static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
219 					     struct bio *bio,
220 					     bool no_sg_merge)
221 {
222 	struct bio_vec bv, bvprv = { NULL };
223 	int cluster, prev = 0;
224 	unsigned int seg_size, nr_phys_segs;
225 	struct bio *fbio, *bbio;
226 	struct bvec_iter iter;
227 
228 	if (!bio)
229 		return 0;
230 
231 	/*
232 	 * This should probably be returning 0, but blk_add_request_payload()
233 	 * (Christoph!!!!)
234 	 */
235 	if (bio->bi_rw & REQ_DISCARD)
236 		return 1;
237 
238 	if (bio->bi_rw & REQ_WRITE_SAME)
239 		return 1;
240 
241 	fbio = bio;
242 	cluster = blk_queue_cluster(q);
243 	seg_size = 0;
244 	nr_phys_segs = 0;
245 	for_each_bio(bio) {
246 		bio_for_each_segment(bv, bio, iter) {
247 			/*
248 			 * If SG merging is disabled, each bio vector is
249 			 * a segment
250 			 */
251 			if (no_sg_merge)
252 				goto new_segment;
253 
254 			if (prev && cluster) {
255 				if (seg_size + bv.bv_len
256 				    > queue_max_segment_size(q))
257 					goto new_segment;
258 				if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
259 					goto new_segment;
260 				if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
261 					goto new_segment;
262 
263 				seg_size += bv.bv_len;
264 				bvprv = bv;
265 				continue;
266 			}
267 new_segment:
268 			if (nr_phys_segs == 1 && seg_size >
269 			    fbio->bi_seg_front_size)
270 				fbio->bi_seg_front_size = seg_size;
271 
272 			nr_phys_segs++;
273 			bvprv = bv;
274 			prev = 1;
275 			seg_size = bv.bv_len;
276 		}
277 		bbio = bio;
278 	}
279 
280 	if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
281 		fbio->bi_seg_front_size = seg_size;
282 	if (seg_size > bbio->bi_seg_back_size)
283 		bbio->bi_seg_back_size = seg_size;
284 
285 	return nr_phys_segs;
286 }
287 
blk_recalc_rq_segments(struct request * rq)288 void blk_recalc_rq_segments(struct request *rq)
289 {
290 	bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
291 			&rq->q->queue_flags);
292 
293 	rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
294 			no_sg_merge);
295 }
296 
blk_recount_segments(struct request_queue * q,struct bio * bio)297 void blk_recount_segments(struct request_queue *q, struct bio *bio)
298 {
299 	unsigned short seg_cnt = bio_segments(bio);
300 
301 	if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
302 			(seg_cnt < queue_max_segments(q)))
303 		bio->bi_phys_segments = seg_cnt;
304 	else {
305 		struct bio *nxt = bio->bi_next;
306 
307 		bio->bi_next = NULL;
308 		bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
309 		bio->bi_next = nxt;
310 	}
311 
312 	bio_set_flag(bio, BIO_SEG_VALID);
313 }
314 EXPORT_SYMBOL(blk_recount_segments);
315 
blk_phys_contig_segment(struct request_queue * q,struct bio * bio,struct bio * nxt)316 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
317 				   struct bio *nxt)
318 {
319 	struct bio_vec end_bv = { NULL }, nxt_bv;
320 	struct bvec_iter iter;
321 
322 	if (!blk_queue_cluster(q))
323 		return 0;
324 
325 	if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
326 	    queue_max_segment_size(q))
327 		return 0;
328 
329 	if (!bio_has_data(bio))
330 		return 1;
331 
332 	bio_for_each_segment(end_bv, bio, iter)
333 		if (end_bv.bv_len == iter.bi_size)
334 			break;
335 
336 	nxt_bv = bio_iovec(nxt);
337 
338 	if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
339 		return 0;
340 
341 	/*
342 	 * bio and nxt are contiguous in memory; check if the queue allows
343 	 * these two to be merged into one
344 	 */
345 	if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
346 		return 1;
347 
348 	return 0;
349 }
350 
351 static inline void
__blk_segment_map_sg(struct request_queue * q,struct bio_vec * bvec,struct scatterlist * sglist,struct bio_vec * bvprv,struct scatterlist ** sg,int * nsegs,int * cluster)352 __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
353 		     struct scatterlist *sglist, struct bio_vec *bvprv,
354 		     struct scatterlist **sg, int *nsegs, int *cluster)
355 {
356 
357 	int nbytes = bvec->bv_len;
358 
359 	if (*sg && *cluster) {
360 		if ((*sg)->length + nbytes > queue_max_segment_size(q))
361 			goto new_segment;
362 
363 		if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
364 			goto new_segment;
365 		if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
366 			goto new_segment;
367 
368 		(*sg)->length += nbytes;
369 	} else {
370 new_segment:
371 		if (!*sg)
372 			*sg = sglist;
373 		else {
374 			/*
375 			 * If the driver previously mapped a shorter
376 			 * list, we could see a termination bit
377 			 * prematurely unless it fully inits the sg
378 			 * table on each mapping. We KNOW that there
379 			 * must be more entries here or the driver
380 			 * would be buggy, so force clear the
381 			 * termination bit to avoid doing a full
382 			 * sg_init_table() in drivers for each command.
383 			 */
384 			sg_unmark_end(*sg);
385 			*sg = sg_next(*sg);
386 		}
387 
388 		sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
389 		(*nsegs)++;
390 	}
391 	*bvprv = *bvec;
392 }
393 
__blk_bios_map_sg(struct request_queue * q,struct bio * bio,struct scatterlist * sglist,struct scatterlist ** sg)394 static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
395 			     struct scatterlist *sglist,
396 			     struct scatterlist **sg)
397 {
398 	struct bio_vec bvec, bvprv = { NULL };
399 	struct bvec_iter iter;
400 	int nsegs, cluster;
401 
402 	nsegs = 0;
403 	cluster = blk_queue_cluster(q);
404 
405 	if (bio->bi_rw & REQ_DISCARD) {
406 		/*
407 		 * This is a hack - drivers should be neither modifying the
408 		 * biovec, nor relying on bi_vcnt - but because of
409 		 * blk_add_request_payload(), a discard bio may or may not have
410 		 * a payload we need to set up here (thank you Christoph) and
411 		 * bi_vcnt is really the only way of telling if we need to.
412 		 */
413 
414 		if (bio->bi_vcnt)
415 			goto single_segment;
416 
417 		return 0;
418 	}
419 
420 	if (bio->bi_rw & REQ_WRITE_SAME) {
421 single_segment:
422 		*sg = sglist;
423 		bvec = bio_iovec(bio);
424 		sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
425 		return 1;
426 	}
427 
428 	for_each_bio(bio)
429 		bio_for_each_segment(bvec, bio, iter)
430 			__blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
431 					     &nsegs, &cluster);
432 
433 	return nsegs;
434 }
435 
436 /*
437  * map a request to scatterlist, return number of sg entries setup. Caller
438  * must make sure sg can hold rq->nr_phys_segments entries
439  */
blk_rq_map_sg(struct request_queue * q,struct request * rq,struct scatterlist * sglist)440 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
441 		  struct scatterlist *sglist)
442 {
443 	struct scatterlist *sg = NULL;
444 	int nsegs = 0;
445 
446 	if (rq->bio)
447 		nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
448 
449 	if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
450 	    (blk_rq_bytes(rq) & q->dma_pad_mask)) {
451 		unsigned int pad_len =
452 			(q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
453 
454 		sg->length += pad_len;
455 		rq->extra_len += pad_len;
456 	}
457 
458 	if (q->dma_drain_size && q->dma_drain_needed(rq)) {
459 		if (rq->cmd_flags & REQ_WRITE)
460 			memset(q->dma_drain_buffer, 0, q->dma_drain_size);
461 
462 		sg_unmark_end(sg);
463 		sg = sg_next(sg);
464 		sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
465 			    q->dma_drain_size,
466 			    ((unsigned long)q->dma_drain_buffer) &
467 			    (PAGE_SIZE - 1));
468 		nsegs++;
469 		rq->extra_len += q->dma_drain_size;
470 	}
471 
472 	if (sg)
473 		sg_mark_end(sg);
474 
475 	/*
476 	 * Something must have been wrong if the figured number of
477 	 * segment is bigger than number of req's physical segments
478 	 */
479 	WARN_ON(nsegs > rq->nr_phys_segments);
480 
481 	return nsegs;
482 }
483 EXPORT_SYMBOL(blk_rq_map_sg);
484 
ll_new_hw_segment(struct request_queue * q,struct request * req,struct bio * bio)485 static inline int ll_new_hw_segment(struct request_queue *q,
486 				    struct request *req,
487 				    struct bio *bio)
488 {
489 	int nr_phys_segs = bio_phys_segments(q, bio);
490 
491 	if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
492 		goto no_merge;
493 
494 	if (blk_integrity_merge_bio(q, req, bio) == false)
495 		goto no_merge;
496 
497 	/*
498 	 * This will form the start of a new hw segment.  Bump both
499 	 * counters.
500 	 */
501 	req->nr_phys_segments += nr_phys_segs;
502 	return 1;
503 
504 no_merge:
505 	req->cmd_flags |= REQ_NOMERGE;
506 	if (req == q->last_merge)
507 		q->last_merge = NULL;
508 	return 0;
509 }
510 
ll_back_merge_fn(struct request_queue * q,struct request * req,struct bio * bio)511 int ll_back_merge_fn(struct request_queue *q, struct request *req,
512 		     struct bio *bio)
513 {
514 	if (req_gap_back_merge(req, bio))
515 		return 0;
516 	if (blk_integrity_rq(req) &&
517 	    integrity_req_gap_back_merge(req, bio))
518 		return 0;
519 	if (blk_rq_sectors(req) + bio_sectors(bio) >
520 	    blk_rq_get_max_sectors(req)) {
521 		req->cmd_flags |= REQ_NOMERGE;
522 		if (req == q->last_merge)
523 			q->last_merge = NULL;
524 		return 0;
525 	}
526 	if (!bio_flagged(req->biotail, BIO_SEG_VALID))
527 		blk_recount_segments(q, req->biotail);
528 	if (!bio_flagged(bio, BIO_SEG_VALID))
529 		blk_recount_segments(q, bio);
530 
531 	return ll_new_hw_segment(q, req, bio);
532 }
533 
ll_front_merge_fn(struct request_queue * q,struct request * req,struct bio * bio)534 int ll_front_merge_fn(struct request_queue *q, struct request *req,
535 		      struct bio *bio)
536 {
537 
538 	if (req_gap_front_merge(req, bio))
539 		return 0;
540 	if (blk_integrity_rq(req) &&
541 	    integrity_req_gap_front_merge(req, bio))
542 		return 0;
543 	if (blk_rq_sectors(req) + bio_sectors(bio) >
544 	    blk_rq_get_max_sectors(req)) {
545 		req->cmd_flags |= REQ_NOMERGE;
546 		if (req == q->last_merge)
547 			q->last_merge = NULL;
548 		return 0;
549 	}
550 	if (!bio_flagged(bio, BIO_SEG_VALID))
551 		blk_recount_segments(q, bio);
552 	if (!bio_flagged(req->bio, BIO_SEG_VALID))
553 		blk_recount_segments(q, req->bio);
554 
555 	return ll_new_hw_segment(q, req, bio);
556 }
557 
558 /*
559  * blk-mq uses req->special to carry normal driver per-request payload, it
560  * does not indicate a prepared command that we cannot merge with.
561  */
req_no_special_merge(struct request * req)562 static bool req_no_special_merge(struct request *req)
563 {
564 	struct request_queue *q = req->q;
565 
566 	return !q->mq_ops && req->special;
567 }
568 
ll_merge_requests_fn(struct request_queue * q,struct request * req,struct request * next)569 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
570 				struct request *next)
571 {
572 	int total_phys_segments;
573 	unsigned int seg_size =
574 		req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
575 
576 	/*
577 	 * First check if the either of the requests are re-queued
578 	 * requests.  Can't merge them if they are.
579 	 */
580 	if (req_no_special_merge(req) || req_no_special_merge(next))
581 		return 0;
582 
583 	if (req_gap_back_merge(req, next->bio))
584 		return 0;
585 
586 	/*
587 	 * Will it become too large?
588 	 */
589 	if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
590 	    blk_rq_get_max_sectors(req))
591 		return 0;
592 
593 	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
594 	if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
595 		if (req->nr_phys_segments == 1)
596 			req->bio->bi_seg_front_size = seg_size;
597 		if (next->nr_phys_segments == 1)
598 			next->biotail->bi_seg_back_size = seg_size;
599 		total_phys_segments--;
600 	}
601 
602 	if (total_phys_segments > queue_max_segments(q))
603 		return 0;
604 
605 	if (blk_integrity_merge_rq(q, req, next) == false)
606 		return 0;
607 
608 	/* Merge is OK... */
609 	req->nr_phys_segments = total_phys_segments;
610 	return 1;
611 }
612 
613 /**
614  * blk_rq_set_mixed_merge - mark a request as mixed merge
615  * @rq: request to mark as mixed merge
616  *
617  * Description:
618  *     @rq is about to be mixed merged.  Make sure the attributes
619  *     which can be mixed are set in each bio and mark @rq as mixed
620  *     merged.
621  */
blk_rq_set_mixed_merge(struct request * rq)622 void blk_rq_set_mixed_merge(struct request *rq)
623 {
624 	unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
625 	struct bio *bio;
626 
627 	if (rq->cmd_flags & REQ_MIXED_MERGE)
628 		return;
629 
630 	/*
631 	 * @rq will no longer represent mixable attributes for all the
632 	 * contained bios.  It will just track those of the first one.
633 	 * Distributes the attributs to each bio.
634 	 */
635 	for (bio = rq->bio; bio; bio = bio->bi_next) {
636 		WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
637 			     (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
638 		bio->bi_rw |= ff;
639 	}
640 	rq->cmd_flags |= REQ_MIXED_MERGE;
641 }
642 
blk_account_io_merge(struct request * req)643 static void blk_account_io_merge(struct request *req)
644 {
645 	if (blk_do_io_stat(req)) {
646 		struct hd_struct *part;
647 		int cpu;
648 
649 		cpu = part_stat_lock();
650 		part = req->part;
651 
652 		part_round_stats(cpu, part);
653 		part_dec_in_flight(part, rq_data_dir(req));
654 
655 		hd_struct_put(part);
656 		part_stat_unlock();
657 	}
658 }
659 
660 /*
661  * Has to be called with the request spinlock acquired
662  */
attempt_merge(struct request_queue * q,struct request * req,struct request * next)663 static int attempt_merge(struct request_queue *q, struct request *req,
664 			  struct request *next)
665 {
666 	if (!rq_mergeable(req) || !rq_mergeable(next))
667 		return 0;
668 
669 	if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
670 		return 0;
671 
672 	/*
673 	 * not contiguous
674 	 */
675 	if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
676 		return 0;
677 
678 	if (rq_data_dir(req) != rq_data_dir(next)
679 	    || req->rq_disk != next->rq_disk
680 	    || req_no_special_merge(next))
681 		return 0;
682 
683 	if (req->cmd_flags & REQ_WRITE_SAME &&
684 	    !blk_write_same_mergeable(req->bio, next->bio))
685 		return 0;
686 
687 	/*
688 	 * If we are allowed to merge, then append bio list
689 	 * from next to rq and release next. merge_requests_fn
690 	 * will have updated segment counts, update sector
691 	 * counts here.
692 	 */
693 	if (!ll_merge_requests_fn(q, req, next))
694 		return 0;
695 
696 	/*
697 	 * If failfast settings disagree or any of the two is already
698 	 * a mixed merge, mark both as mixed before proceeding.  This
699 	 * makes sure that all involved bios have mixable attributes
700 	 * set properly.
701 	 */
702 	if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
703 	    (req->cmd_flags & REQ_FAILFAST_MASK) !=
704 	    (next->cmd_flags & REQ_FAILFAST_MASK)) {
705 		blk_rq_set_mixed_merge(req);
706 		blk_rq_set_mixed_merge(next);
707 	}
708 
709 	/*
710 	 * At this point we have either done a back merge
711 	 * or front merge. We need the smaller start_time of
712 	 * the merged requests to be the current request
713 	 * for accounting purposes.
714 	 */
715 	if (time_after(req->start_time, next->start_time))
716 		req->start_time = next->start_time;
717 
718 	req->biotail->bi_next = next->bio;
719 	req->biotail = next->biotail;
720 
721 	req->__data_len += blk_rq_bytes(next);
722 
723 	elv_merge_requests(q, req, next);
724 
725 	/*
726 	 * 'next' is going away, so update stats accordingly
727 	 */
728 	blk_account_io_merge(next);
729 
730 	req->ioprio = ioprio_best(req->ioprio, next->ioprio);
731 	if (blk_rq_cpu_valid(next))
732 		req->cpu = next->cpu;
733 
734 	/* owner-ship of bio passed from next to req */
735 	next->bio = NULL;
736 	__blk_put_request(q, next);
737 	return 1;
738 }
739 
attempt_back_merge(struct request_queue * q,struct request * rq)740 int attempt_back_merge(struct request_queue *q, struct request *rq)
741 {
742 	struct request *next = elv_latter_request(q, rq);
743 
744 	if (next)
745 		return attempt_merge(q, rq, next);
746 
747 	return 0;
748 }
749 
attempt_front_merge(struct request_queue * q,struct request * rq)750 int attempt_front_merge(struct request_queue *q, struct request *rq)
751 {
752 	struct request *prev = elv_former_request(q, rq);
753 
754 	if (prev)
755 		return attempt_merge(q, prev, rq);
756 
757 	return 0;
758 }
759 
blk_attempt_req_merge(struct request_queue * q,struct request * rq,struct request * next)760 int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
761 			  struct request *next)
762 {
763 	return attempt_merge(q, rq, next);
764 }
765 
blk_rq_merge_ok(struct request * rq,struct bio * bio)766 bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
767 {
768 	if (!rq_mergeable(rq) || !bio_mergeable(bio))
769 		return false;
770 
771 	if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
772 		return false;
773 
774 	/* different data direction or already started, don't merge */
775 	if (bio_data_dir(bio) != rq_data_dir(rq))
776 		return false;
777 
778 	/* must be same device and not a special request */
779 	if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
780 		return false;
781 
782 	/* only merge integrity protected bio into ditto rq */
783 	if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
784 		return false;
785 
786 	/* must be using the same buffer */
787 	if (rq->cmd_flags & REQ_WRITE_SAME &&
788 	    !blk_write_same_mergeable(rq->bio, bio))
789 		return false;
790 
791 	return true;
792 }
793 
blk_try_merge(struct request * rq,struct bio * bio)794 int blk_try_merge(struct request *rq, struct bio *bio)
795 {
796 	if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
797 		return ELEVATOR_BACK_MERGE;
798 	else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
799 		return ELEVATOR_FRONT_MERGE;
800 	return ELEVATOR_NO_MERGE;
801 }
802