xref: /openbmc/linux/block/blk-merge.c (revision 943b40c8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d6d48196SJens Axboe /*
3d6d48196SJens Axboe  * Functions related to segment and merge handling
4d6d48196SJens Axboe  */
5d6d48196SJens Axboe #include <linux/kernel.h>
6d6d48196SJens Axboe #include <linux/module.h>
7d6d48196SJens Axboe #include <linux/bio.h>
8d6d48196SJens Axboe #include <linux/blkdev.h>
9d6d48196SJens Axboe #include <linux/scatterlist.h>
10d6d48196SJens Axboe 
11cda22646SMike Krinkin #include <trace/events/block.h>
12cda22646SMike Krinkin 
13d6d48196SJens Axboe #include "blk.h"
14d6d48196SJens Axboe 
15e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q,
16e9907009SChristoph Hellwig 		struct request *prev_rq, struct bio *prev, struct bio *next)
17e9907009SChristoph Hellwig {
18e9907009SChristoph Hellwig 	struct bio_vec pb, nb;
19e9907009SChristoph Hellwig 
20e9907009SChristoph Hellwig 	if (!bio_has_data(prev) || !queue_virt_boundary(q))
21e9907009SChristoph Hellwig 		return false;
22e9907009SChristoph Hellwig 
23e9907009SChristoph Hellwig 	/*
24e9907009SChristoph Hellwig 	 * Don't merge if the 1st bio starts with non-zero offset, otherwise it
25e9907009SChristoph Hellwig 	 * is quite difficult to respect the sg gap limit.  We work hard to
26e9907009SChristoph Hellwig 	 * merge a huge number of small single bios in case of mkfs.
27e9907009SChristoph Hellwig 	 */
28e9907009SChristoph Hellwig 	if (prev_rq)
29e9907009SChristoph Hellwig 		bio_get_first_bvec(prev_rq->bio, &pb);
30e9907009SChristoph Hellwig 	else
31e9907009SChristoph Hellwig 		bio_get_first_bvec(prev, &pb);
32df376b2eSJohannes Thumshirn 	if (pb.bv_offset & queue_virt_boundary(q))
33e9907009SChristoph Hellwig 		return true;
34e9907009SChristoph Hellwig 
35e9907009SChristoph Hellwig 	/*
36e9907009SChristoph Hellwig 	 * We don't need to worry about the situation that the merged segment
37e9907009SChristoph Hellwig 	 * ends in unaligned virt boundary:
38e9907009SChristoph Hellwig 	 *
39e9907009SChristoph Hellwig 	 * - if 'pb' ends aligned, the merged segment ends aligned
40e9907009SChristoph Hellwig 	 * - if 'pb' ends unaligned, the next bio must include
41e9907009SChristoph Hellwig 	 *   one single bvec of 'nb', otherwise the 'nb' can't
42e9907009SChristoph Hellwig 	 *   merge with 'pb'
43e9907009SChristoph Hellwig 	 */
44e9907009SChristoph Hellwig 	bio_get_last_bvec(prev, &pb);
45e9907009SChristoph Hellwig 	bio_get_first_bvec(next, &nb);
46200a9affSChristoph Hellwig 	if (biovec_phys_mergeable(q, &pb, &nb))
47e9907009SChristoph Hellwig 		return false;
48e9907009SChristoph Hellwig 	return __bvec_gap_to_prev(q, &pb, nb.bv_offset);
49e9907009SChristoph Hellwig }
50e9907009SChristoph Hellwig 
51e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
52e9907009SChristoph Hellwig {
53e9907009SChristoph Hellwig 	return bio_will_gap(req->q, req, req->biotail, bio);
54e9907009SChristoph Hellwig }
55e9907009SChristoph Hellwig 
56e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
57e9907009SChristoph Hellwig {
58e9907009SChristoph Hellwig 	return bio_will_gap(req->q, NULL, bio, req->bio);
59e9907009SChristoph Hellwig }
60e9907009SChristoph Hellwig 
6154efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q,
6254efd50bSKent Overstreet 					 struct bio *bio,
63bdced438SMing Lei 					 struct bio_set *bs,
64bdced438SMing Lei 					 unsigned *nsegs)
6554efd50bSKent Overstreet {
6654efd50bSKent Overstreet 	unsigned int max_discard_sectors, granularity;
6754efd50bSKent Overstreet 	int alignment;
6854efd50bSKent Overstreet 	sector_t tmp;
6954efd50bSKent Overstreet 	unsigned split_sectors;
7054efd50bSKent Overstreet 
71bdced438SMing Lei 	*nsegs = 1;
72bdced438SMing Lei 
7354efd50bSKent Overstreet 	/* Zero-sector (unknown) and one-sector granularities are the same.  */
7454efd50bSKent Overstreet 	granularity = max(q->limits.discard_granularity >> 9, 1U);
7554efd50bSKent Overstreet 
761adfc5e4SMing Lei 	max_discard_sectors = min(q->limits.max_discard_sectors,
771adfc5e4SMing Lei 			bio_allowed_max_sectors(q));
7854efd50bSKent Overstreet 	max_discard_sectors -= max_discard_sectors % granularity;
7954efd50bSKent Overstreet 
8054efd50bSKent Overstreet 	if (unlikely(!max_discard_sectors)) {
8154efd50bSKent Overstreet 		/* XXX: warn */
8254efd50bSKent Overstreet 		return NULL;
8354efd50bSKent Overstreet 	}
8454efd50bSKent Overstreet 
8554efd50bSKent Overstreet 	if (bio_sectors(bio) <= max_discard_sectors)
8654efd50bSKent Overstreet 		return NULL;
8754efd50bSKent Overstreet 
8854efd50bSKent Overstreet 	split_sectors = max_discard_sectors;
8954efd50bSKent Overstreet 
9054efd50bSKent Overstreet 	/*
9154efd50bSKent Overstreet 	 * If the next starting sector would be misaligned, stop the discard at
9254efd50bSKent Overstreet 	 * the previous aligned sector.
9354efd50bSKent Overstreet 	 */
9454efd50bSKent Overstreet 	alignment = (q->limits.discard_alignment >> 9) % granularity;
9554efd50bSKent Overstreet 
9654efd50bSKent Overstreet 	tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
9754efd50bSKent Overstreet 	tmp = sector_div(tmp, granularity);
9854efd50bSKent Overstreet 
9954efd50bSKent Overstreet 	if (split_sectors > tmp)
10054efd50bSKent Overstreet 		split_sectors -= tmp;
10154efd50bSKent Overstreet 
10254efd50bSKent Overstreet 	return bio_split(bio, split_sectors, GFP_NOIO, bs);
10354efd50bSKent Overstreet }
10454efd50bSKent Overstreet 
105885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q,
106885fa13fSChristoph Hellwig 		struct bio *bio, struct bio_set *bs, unsigned *nsegs)
107885fa13fSChristoph Hellwig {
108d665e12aSChristoph Hellwig 	*nsegs = 0;
109885fa13fSChristoph Hellwig 
110885fa13fSChristoph Hellwig 	if (!q->limits.max_write_zeroes_sectors)
111885fa13fSChristoph Hellwig 		return NULL;
112885fa13fSChristoph Hellwig 
113885fa13fSChristoph Hellwig 	if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors)
114885fa13fSChristoph Hellwig 		return NULL;
115885fa13fSChristoph Hellwig 
116885fa13fSChristoph Hellwig 	return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs);
117885fa13fSChristoph Hellwig }
118885fa13fSChristoph Hellwig 
11954efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q,
12054efd50bSKent Overstreet 					    struct bio *bio,
121bdced438SMing Lei 					    struct bio_set *bs,
122bdced438SMing Lei 					    unsigned *nsegs)
12354efd50bSKent Overstreet {
124bdced438SMing Lei 	*nsegs = 1;
125bdced438SMing Lei 
12654efd50bSKent Overstreet 	if (!q->limits.max_write_same_sectors)
12754efd50bSKent Overstreet 		return NULL;
12854efd50bSKent Overstreet 
12954efd50bSKent Overstreet 	if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
13054efd50bSKent Overstreet 		return NULL;
13154efd50bSKent Overstreet 
13254efd50bSKent Overstreet 	return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
13354efd50bSKent Overstreet }
13454efd50bSKent Overstreet 
1359cc5169cSBart Van Assche /*
1369cc5169cSBart Van Assche  * Return the maximum number of sectors from the start of a bio that may be
1379cc5169cSBart Van Assche  * submitted as a single request to a block device. If enough sectors remain,
1389cc5169cSBart Van Assche  * align the end to the physical block size. Otherwise align the end to the
1399cc5169cSBart Van Assche  * logical block size. This approach minimizes the number of non-aligned
1409cc5169cSBart Van Assche  * requests that are submitted to a block device if the start of a bio is not
1419cc5169cSBart Van Assche  * aligned to a physical block boundary.
1429cc5169cSBart Van Assche  */
143d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q,
144d0e5fbb0SMing Lei 				       struct bio *bio)
145d0e5fbb0SMing Lei {
146d0e5fbb0SMing Lei 	unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector);
1479cc5169cSBart Van Assche 	unsigned max_sectors = sectors;
1489cc5169cSBart Van Assche 	unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT;
1499cc5169cSBart Van Assche 	unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT;
1509cc5169cSBart Van Assche 	unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1);
151d0e5fbb0SMing Lei 
1529cc5169cSBart Van Assche 	max_sectors += start_offset;
1539cc5169cSBart Van Assche 	max_sectors &= ~(pbs - 1);
1549cc5169cSBart Van Assche 	if (max_sectors > start_offset)
1559cc5169cSBart Van Assche 		return max_sectors - start_offset;
156d0e5fbb0SMing Lei 
1579cc5169cSBart Van Assche 	return sectors & (lbs - 1);
158d0e5fbb0SMing Lei }
159d0e5fbb0SMing Lei 
160429120f3SMing Lei static inline unsigned get_max_segment_size(const struct request_queue *q,
161429120f3SMing Lei 					    struct page *start_page,
162429120f3SMing Lei 					    unsigned long offset)
163dcebd755SMing Lei {
164dcebd755SMing Lei 	unsigned long mask = queue_segment_boundary(q);
165dcebd755SMing Lei 
166429120f3SMing Lei 	offset = mask & (page_to_phys(start_page) + offset);
1674a2f704eSMing Lei 
1684a2f704eSMing Lei 	/*
1694a2f704eSMing Lei 	 * overflow may be triggered in case of zero page physical address
1704a2f704eSMing Lei 	 * on 32bit arch, use queue's max segment size when that happens.
1714a2f704eSMing Lei 	 */
1724a2f704eSMing Lei 	return min_not_zero(mask - offset + 1,
1734a2f704eSMing Lei 			(unsigned long)queue_max_segment_size(q));
174dcebd755SMing Lei }
175dcebd755SMing Lei 
176708b25b3SBart Van Assche /**
177708b25b3SBart Van Assche  * bvec_split_segs - verify whether or not a bvec should be split in the middle
178708b25b3SBart Van Assche  * @q:        [in] request queue associated with the bio associated with @bv
179708b25b3SBart Van Assche  * @bv:       [in] bvec to examine
180708b25b3SBart Van Assche  * @nsegs:    [in,out] Number of segments in the bio being built. Incremented
181708b25b3SBart Van Assche  *            by the number of segments from @bv that may be appended to that
182708b25b3SBart Van Assche  *            bio without exceeding @max_segs
183708b25b3SBart Van Assche  * @sectors:  [in,out] Number of sectors in the bio being built. Incremented
184708b25b3SBart Van Assche  *            by the number of sectors from @bv that may be appended to that
185708b25b3SBart Van Assche  *            bio without exceeding @max_sectors
186708b25b3SBart Van Assche  * @max_segs: [in] upper bound for *@nsegs
187708b25b3SBart Van Assche  * @max_sectors: [in] upper bound for *@sectors
188708b25b3SBart Van Assche  *
189708b25b3SBart Van Assche  * When splitting a bio, it can happen that a bvec is encountered that is too
190708b25b3SBart Van Assche  * big to fit in a single segment and hence that it has to be split in the
191708b25b3SBart Van Assche  * middle. This function verifies whether or not that should happen. The value
192708b25b3SBart Van Assche  * %true is returned if and only if appending the entire @bv to a bio with
193708b25b3SBart Van Assche  * *@nsegs segments and *@sectors sectors would make that bio unacceptable for
194708b25b3SBart Van Assche  * the block driver.
195dcebd755SMing Lei  */
196af2c68feSBart Van Assche static bool bvec_split_segs(const struct request_queue *q,
197af2c68feSBart Van Assche 			    const struct bio_vec *bv, unsigned *nsegs,
198708b25b3SBart Van Assche 			    unsigned *sectors, unsigned max_segs,
199708b25b3SBart Van Assche 			    unsigned max_sectors)
200dcebd755SMing Lei {
201708b25b3SBart Van Assche 	unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9;
202708b25b3SBart Van Assche 	unsigned len = min(bv->bv_len, max_len);
203dcebd755SMing Lei 	unsigned total_len = 0;
204ff9811b3SBart Van Assche 	unsigned seg_size = 0;
205dcebd755SMing Lei 
206ff9811b3SBart Van Assche 	while (len && *nsegs < max_segs) {
207429120f3SMing Lei 		seg_size = get_max_segment_size(q, bv->bv_page,
208429120f3SMing Lei 						bv->bv_offset + total_len);
209dcebd755SMing Lei 		seg_size = min(seg_size, len);
210dcebd755SMing Lei 
211ff9811b3SBart Van Assche 		(*nsegs)++;
212dcebd755SMing Lei 		total_len += seg_size;
213dcebd755SMing Lei 		len -= seg_size;
214dcebd755SMing Lei 
215dcebd755SMing Lei 		if ((bv->bv_offset + total_len) & queue_virt_boundary(q))
216dcebd755SMing Lei 			break;
217dcebd755SMing Lei 	}
218dcebd755SMing Lei 
219dcebd755SMing Lei 	*sectors += total_len >> 9;
220dcebd755SMing Lei 
221708b25b3SBart Van Assche 	/* tell the caller to split the bvec if it is too big to fit */
222708b25b3SBart Van Assche 	return len > 0 || bv->bv_len > max_len;
223dcebd755SMing Lei }
224dcebd755SMing Lei 
225dad77584SBart Van Assche /**
226dad77584SBart Van Assche  * blk_bio_segment_split - split a bio in two bios
227dad77584SBart Van Assche  * @q:    [in] request queue pointer
228dad77584SBart Van Assche  * @bio:  [in] bio to be split
229dad77584SBart Van Assche  * @bs:	  [in] bio set to allocate the clone from
230dad77584SBart Van Assche  * @segs: [out] number of segments in the bio with the first half of the sectors
231dad77584SBart Van Assche  *
232dad77584SBart Van Assche  * Clone @bio, update the bi_iter of the clone to represent the first sectors
233dad77584SBart Van Assche  * of @bio and update @bio->bi_iter to represent the remaining sectors. The
234dad77584SBart Van Assche  * following is guaranteed for the cloned bio:
235dad77584SBart Van Assche  * - That it has at most get_max_io_size(@q, @bio) sectors.
236dad77584SBart Van Assche  * - That it has at most queue_max_segments(@q) segments.
237dad77584SBart Van Assche  *
238dad77584SBart Van Assche  * Except for discard requests the cloned bio will point at the bi_io_vec of
239dad77584SBart Van Assche  * the original bio. It is the responsibility of the caller to ensure that the
240dad77584SBart Van Assche  * original bio is not freed before the cloned bio. The caller is also
241dad77584SBart Van Assche  * responsible for ensuring that @bs is only destroyed after processing of the
242dad77584SBart Van Assche  * split bio has finished.
243dad77584SBart Van Assche  */
24454efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q,
24554efd50bSKent Overstreet 					 struct bio *bio,
246bdced438SMing Lei 					 struct bio_set *bs,
247bdced438SMing Lei 					 unsigned *segs)
24854efd50bSKent Overstreet {
2495014c311SJens Axboe 	struct bio_vec bv, bvprv, *bvprvp = NULL;
25054efd50bSKent Overstreet 	struct bvec_iter iter;
2516869875fSChristoph Hellwig 	unsigned nsegs = 0, sectors = 0;
252d0e5fbb0SMing Lei 	const unsigned max_sectors = get_max_io_size(q, bio);
25305b700baSMing Lei 	const unsigned max_segs = queue_max_segments(q);
25454efd50bSKent Overstreet 
255dcebd755SMing Lei 	bio_for_each_bvec(bv, bio, iter) {
25654efd50bSKent Overstreet 		/*
25754efd50bSKent Overstreet 		 * If the queue doesn't support SG gaps and adding this
25854efd50bSKent Overstreet 		 * offset would create a gap, disallow it.
25954efd50bSKent Overstreet 		 */
2605014c311SJens Axboe 		if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
26154efd50bSKent Overstreet 			goto split;
26254efd50bSKent Overstreet 
26305b700baSMing Lei 		if (nsegs < max_segs &&
264708b25b3SBart Van Assche 		    sectors + (bv.bv_len >> 9) <= max_sectors &&
265708b25b3SBart Van Assche 		    bv.bv_offset + bv.bv_len <= PAGE_SIZE) {
266708b25b3SBart Van Assche 			nsegs++;
267708b25b3SBart Van Assche 			sectors += bv.bv_len >> 9;
268708b25b3SBart Van Assche 		} else if (bvec_split_segs(q, &bv, &nsegs, &sectors, max_segs,
269708b25b3SBart Van Assche 					 max_sectors)) {
270e36f6204SKeith Busch 			goto split;
271e36f6204SKeith Busch 		}
272e36f6204SKeith Busch 
27354efd50bSKent Overstreet 		bvprv = bv;
274578270bfSMing Lei 		bvprvp = &bvprv;
27554efd50bSKent Overstreet 	}
27654efd50bSKent Overstreet 
277d627065dSChristoph Hellwig 	*segs = nsegs;
278d627065dSChristoph Hellwig 	return NULL;
27954efd50bSKent Overstreet split:
280bdced438SMing Lei 	*segs = nsegs;
281d627065dSChristoph Hellwig 	return bio_split(bio, sectors, GFP_NOIO, bs);
28254efd50bSKent Overstreet }
28354efd50bSKent Overstreet 
284dad77584SBart Van Assche /**
285dad77584SBart Van Assche  * __blk_queue_split - split a bio and submit the second half
286dad77584SBart Van Assche  * @bio:     [in, out] bio to be split
287dad77584SBart Van Assche  * @nr_segs: [out] number of segments in the first bio
288dad77584SBart Van Assche  *
289dad77584SBart Van Assche  * Split a bio into two bios, chain the two bios, submit the second half and
290dad77584SBart Van Assche  * store a pointer to the first half in *@bio. If the second bio is still too
291dad77584SBart Van Assche  * big it will be split by a recursive call to this function. Since this
292f695ca38SChristoph Hellwig  * function may allocate a new bio from @bio->bi_disk->queue->bio_split, it is
293f695ca38SChristoph Hellwig  * the responsibility of the caller to ensure that
294f695ca38SChristoph Hellwig  * @bio->bi_disk->queue->bio_split is only released after processing of the
295dad77584SBart Van Assche  * split bio has finished.
296dad77584SBart Van Assche  */
297f695ca38SChristoph Hellwig void __blk_queue_split(struct bio **bio, unsigned int *nr_segs)
29854efd50bSKent Overstreet {
299f695ca38SChristoph Hellwig 	struct request_queue *q = (*bio)->bi_disk->queue;
300fa532287SChristoph Hellwig 	struct bio *split = NULL;
30154efd50bSKent Overstreet 
3027afafc8aSAdrian Hunter 	switch (bio_op(*bio)) {
3037afafc8aSAdrian Hunter 	case REQ_OP_DISCARD:
3047afafc8aSAdrian Hunter 	case REQ_OP_SECURE_ERASE:
30514ccb66bSChristoph Hellwig 		split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs);
3067afafc8aSAdrian Hunter 		break;
307a6f0788eSChaitanya Kulkarni 	case REQ_OP_WRITE_ZEROES:
30814ccb66bSChristoph Hellwig 		split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split,
30914ccb66bSChristoph Hellwig 				nr_segs);
310a6f0788eSChaitanya Kulkarni 		break;
3117afafc8aSAdrian Hunter 	case REQ_OP_WRITE_SAME:
31214ccb66bSChristoph Hellwig 		split = blk_bio_write_same_split(q, *bio, &q->bio_split,
31314ccb66bSChristoph Hellwig 				nr_segs);
3147afafc8aSAdrian Hunter 		break;
3157afafc8aSAdrian Hunter 	default:
316fa532287SChristoph Hellwig 		/*
317fa532287SChristoph Hellwig 		 * All drivers must accept single-segments bios that are <=
318fa532287SChristoph Hellwig 		 * PAGE_SIZE.  This is a quick and dirty check that relies on
319fa532287SChristoph Hellwig 		 * the fact that bi_io_vec[0] is always valid if a bio has data.
320fa532287SChristoph Hellwig 		 * The check might lead to occasional false negatives when bios
321fa532287SChristoph Hellwig 		 * are cloned, but compared to the performance impact of cloned
322fa532287SChristoph Hellwig 		 * bios themselves the loop below doesn't matter anyway.
323fa532287SChristoph Hellwig 		 */
324fa532287SChristoph Hellwig 		if (!q->limits.chunk_sectors &&
325fa532287SChristoph Hellwig 		    (*bio)->bi_vcnt == 1 &&
32659db8ba2SMing Lei 		    ((*bio)->bi_io_vec[0].bv_len +
3271e279153SJens Axboe 		     (*bio)->bi_io_vec[0].bv_offset) <= PAGE_SIZE) {
328fa532287SChristoph Hellwig 			*nr_segs = 1;
329fa532287SChristoph Hellwig 			break;
330fa532287SChristoph Hellwig 		}
33114ccb66bSChristoph Hellwig 		split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs);
3327afafc8aSAdrian Hunter 		break;
3337afafc8aSAdrian Hunter 	}
334bdced438SMing Lei 
33554efd50bSKent Overstreet 	if (split) {
3366ac45aebSMing Lei 		/* there isn't chance to merge the splitted bio */
3371eff9d32SJens Axboe 		split->bi_opf |= REQ_NOMERGE;
3386ac45aebSMing Lei 
33954efd50bSKent Overstreet 		bio_chain(split, *bio);
340cda22646SMike Krinkin 		trace_block_split(q, split, (*bio)->bi_iter.bi_sector);
341ed00aabdSChristoph Hellwig 		submit_bio_noacct(*bio);
34254efd50bSKent Overstreet 		*bio = split;
34354efd50bSKent Overstreet 	}
34454efd50bSKent Overstreet }
34514ccb66bSChristoph Hellwig 
346dad77584SBart Van Assche /**
347dad77584SBart Van Assche  * blk_queue_split - split a bio and submit the second half
348dad77584SBart Van Assche  * @bio: [in, out] bio to be split
349dad77584SBart Van Assche  *
350dad77584SBart Van Assche  * Split a bio into two bios, chains the two bios, submit the second half and
351dad77584SBart Van Assche  * store a pointer to the first half in *@bio. Since this function may allocate
352f695ca38SChristoph Hellwig  * a new bio from @bio->bi_disk->queue->bio_split, it is the responsibility of
353f695ca38SChristoph Hellwig  * the caller to ensure that @bio->bi_disk->queue->bio_split is only released
354f695ca38SChristoph Hellwig  * after processing of the split bio has finished.
355dad77584SBart Van Assche  */
356f695ca38SChristoph Hellwig void blk_queue_split(struct bio **bio)
35714ccb66bSChristoph Hellwig {
35814ccb66bSChristoph Hellwig 	unsigned int nr_segs;
35914ccb66bSChristoph Hellwig 
360f695ca38SChristoph Hellwig 	__blk_queue_split(bio, &nr_segs);
36114ccb66bSChristoph Hellwig }
36254efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split);
36354efd50bSKent Overstreet 
364e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq)
365d6d48196SJens Axboe {
3666869875fSChristoph Hellwig 	unsigned int nr_phys_segs = 0;
367ff9811b3SBart Van Assche 	unsigned int nr_sectors = 0;
368e9cd19c0SChristoph Hellwig 	struct req_iterator iter;
3696869875fSChristoph Hellwig 	struct bio_vec bv;
370d6d48196SJens Axboe 
371e9cd19c0SChristoph Hellwig 	if (!rq->bio)
3721e428079SJens Axboe 		return 0;
373d6d48196SJens Axboe 
374e9cd19c0SChristoph Hellwig 	switch (bio_op(rq->bio)) {
375a6f0788eSChaitanya Kulkarni 	case REQ_OP_DISCARD:
376a6f0788eSChaitanya Kulkarni 	case REQ_OP_SECURE_ERASE:
377a6f0788eSChaitanya Kulkarni 	case REQ_OP_WRITE_ZEROES:
378f9d03f96SChristoph Hellwig 		return 0;
379f9d03f96SChristoph Hellwig 	case REQ_OP_WRITE_SAME:
3805cb8850cSKent Overstreet 		return 1;
381a6f0788eSChaitanya Kulkarni 	}
3825cb8850cSKent Overstreet 
383e9cd19c0SChristoph Hellwig 	rq_for_each_bvec(bv, rq, iter)
384ff9811b3SBart Van Assche 		bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors,
385708b25b3SBart Van Assche 				UINT_MAX, UINT_MAX);
3861e428079SJens Axboe 	return nr_phys_segs;
3871e428079SJens Axboe }
3881e428079SJens Axboe 
38948d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg,
390862e5a5eSMing Lei 		struct scatterlist *sglist)
391862e5a5eSMing Lei {
392862e5a5eSMing Lei 	if (!*sg)
393862e5a5eSMing Lei 		return sglist;
394862e5a5eSMing Lei 
395862e5a5eSMing Lei 	/*
396862e5a5eSMing Lei 	 * If the driver previously mapped a shorter list, we could see a
397862e5a5eSMing Lei 	 * termination bit prematurely unless it fully inits the sg table
398862e5a5eSMing Lei 	 * on each mapping. We KNOW that there must be more entries here
399862e5a5eSMing Lei 	 * or the driver would be buggy, so force clear the termination bit
400862e5a5eSMing Lei 	 * to avoid doing a full sg_init_table() in drivers for each command.
401862e5a5eSMing Lei 	 */
402862e5a5eSMing Lei 	sg_unmark_end(*sg);
403862e5a5eSMing Lei 	return sg_next(*sg);
404862e5a5eSMing Lei }
405862e5a5eSMing Lei 
406862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q,
407862e5a5eSMing Lei 		struct bio_vec *bvec, struct scatterlist *sglist,
408862e5a5eSMing Lei 		struct scatterlist **sg)
409862e5a5eSMing Lei {
410862e5a5eSMing Lei 	unsigned nbytes = bvec->bv_len;
4118a96a0e4SChristoph Hellwig 	unsigned nsegs = 0, total = 0;
412862e5a5eSMing Lei 
413862e5a5eSMing Lei 	while (nbytes > 0) {
4148a96a0e4SChristoph Hellwig 		unsigned offset = bvec->bv_offset + total;
415429120f3SMing Lei 		unsigned len = min(get_max_segment_size(q, bvec->bv_page,
416429120f3SMing Lei 					offset), nbytes);
417f9f76879SChristoph Hellwig 		struct page *page = bvec->bv_page;
418f9f76879SChristoph Hellwig 
419f9f76879SChristoph Hellwig 		/*
420f9f76879SChristoph Hellwig 		 * Unfortunately a fair number of drivers barf on scatterlists
421f9f76879SChristoph Hellwig 		 * that have an offset larger than PAGE_SIZE, despite other
422f9f76879SChristoph Hellwig 		 * subsystems dealing with that invariant just fine.  For now
423f9f76879SChristoph Hellwig 		 * stick to the legacy format where we never present those from
424f9f76879SChristoph Hellwig 		 * the block layer, but the code below should be removed once
425f9f76879SChristoph Hellwig 		 * these offenders (mostly MMC/SD drivers) are fixed.
426f9f76879SChristoph Hellwig 		 */
427f9f76879SChristoph Hellwig 		page += (offset >> PAGE_SHIFT);
428f9f76879SChristoph Hellwig 		offset &= ~PAGE_MASK;
429862e5a5eSMing Lei 
430862e5a5eSMing Lei 		*sg = blk_next_sg(sg, sglist);
431f9f76879SChristoph Hellwig 		sg_set_page(*sg, page, len, offset);
432862e5a5eSMing Lei 
4338a96a0e4SChristoph Hellwig 		total += len;
4348a96a0e4SChristoph Hellwig 		nbytes -= len;
435862e5a5eSMing Lei 		nsegs++;
436862e5a5eSMing Lei 	}
437862e5a5eSMing Lei 
438862e5a5eSMing Lei 	return nsegs;
439862e5a5eSMing Lei }
440862e5a5eSMing Lei 
44116e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv,
44216e3e418SMing Lei 		struct scatterlist *sglist, struct scatterlist **sg)
44316e3e418SMing Lei {
44416e3e418SMing Lei 	*sg = blk_next_sg(sg, sglist);
44516e3e418SMing Lei 	sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
44616e3e418SMing Lei 	return 1;
44716e3e418SMing Lei }
44816e3e418SMing Lei 
449f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */
450f6970f83SMing Lei static inline bool
451f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec,
452f6970f83SMing Lei 			   struct bio_vec *bvprv, struct scatterlist **sg)
453963ab9e5SAsias He {
454963ab9e5SAsias He 
455963ab9e5SAsias He 	int nbytes = bvec->bv_len;
456963ab9e5SAsias He 
457f6970f83SMing Lei 	if (!*sg)
458f6970f83SMing Lei 		return false;
459f6970f83SMing Lei 
460b4b6cb61SMing Lei 	if ((*sg)->length + nbytes > queue_max_segment_size(q))
461f6970f83SMing Lei 		return false;
462f6970f83SMing Lei 
4633dccdae5SChristoph Hellwig 	if (!biovec_phys_mergeable(q, bvprv, bvec))
464f6970f83SMing Lei 		return false;
465963ab9e5SAsias He 
466963ab9e5SAsias He 	(*sg)->length += nbytes;
467f6970f83SMing Lei 
468f6970f83SMing Lei 	return true;
469963ab9e5SAsias He }
470963ab9e5SAsias He 
4715cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
4725cb8850cSKent Overstreet 			     struct scatterlist *sglist,
4735cb8850cSKent Overstreet 			     struct scatterlist **sg)
4745cb8850cSKent Overstreet {
4753f649ab7SKees Cook 	struct bio_vec bvec, bvprv = { NULL };
4765cb8850cSKent Overstreet 	struct bvec_iter iter;
47738417468SChristoph Hellwig 	int nsegs = 0;
478f6970f83SMing Lei 	bool new_bio = false;
4795cb8850cSKent Overstreet 
480f6970f83SMing Lei 	for_each_bio(bio) {
481f6970f83SMing Lei 		bio_for_each_bvec(bvec, bio, iter) {
482f6970f83SMing Lei 			/*
483f6970f83SMing Lei 			 * Only try to merge bvecs from two bios given we
484f6970f83SMing Lei 			 * have done bio internal merge when adding pages
485f6970f83SMing Lei 			 * to bio
486f6970f83SMing Lei 			 */
487f6970f83SMing Lei 			if (new_bio &&
488f6970f83SMing Lei 			    __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg))
489f6970f83SMing Lei 				goto next_bvec;
490f6970f83SMing Lei 
491f6970f83SMing Lei 			if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE)
492f6970f83SMing Lei 				nsegs += __blk_bvec_map_sg(bvec, sglist, sg);
493f6970f83SMing Lei 			else
494f6970f83SMing Lei 				nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg);
495f6970f83SMing Lei  next_bvec:
496f6970f83SMing Lei 			new_bio = false;
497f6970f83SMing Lei 		}
498b21e11c5SMing Lei 		if (likely(bio->bi_iter.bi_size)) {
499f6970f83SMing Lei 			bvprv = bvec;
500f6970f83SMing Lei 			new_bio = true;
501f6970f83SMing Lei 		}
502b21e11c5SMing Lei 	}
5035cb8850cSKent Overstreet 
5045cb8850cSKent Overstreet 	return nsegs;
5055cb8850cSKent Overstreet }
5065cb8850cSKent Overstreet 
507d6d48196SJens Axboe /*
508d6d48196SJens Axboe  * map a request to scatterlist, return number of sg entries setup. Caller
509d6d48196SJens Axboe  * must make sure sg can hold rq->nr_phys_segments entries
510d6d48196SJens Axboe  */
51189de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
51289de1504SChristoph Hellwig 		struct scatterlist *sglist, struct scatterlist **last_sg)
513d6d48196SJens Axboe {
5145cb8850cSKent Overstreet 	int nsegs = 0;
515d6d48196SJens Axboe 
516f9d03f96SChristoph Hellwig 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
51789de1504SChristoph Hellwig 		nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg);
518f9d03f96SChristoph Hellwig 	else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME)
51989de1504SChristoph Hellwig 		nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg);
520f9d03f96SChristoph Hellwig 	else if (rq->bio)
52189de1504SChristoph Hellwig 		nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg);
522f18573abSFUJITA Tomonori 
52389de1504SChristoph Hellwig 	if (*last_sg)
52489de1504SChristoph Hellwig 		sg_mark_end(*last_sg);
525d6d48196SJens Axboe 
52612e57f59SMing Lei 	/*
52712e57f59SMing Lei 	 * Something must have been wrong if the figured number of
52812e57f59SMing Lei 	 * segment is bigger than number of req's physical segments
52912e57f59SMing Lei 	 */
530f9d03f96SChristoph Hellwig 	WARN_ON(nsegs > blk_rq_nr_phys_segments(rq));
53112e57f59SMing Lei 
532d6d48196SJens Axboe 	return nsegs;
533d6d48196SJens Axboe }
53489de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg);
535d6d48196SJens Axboe 
536943b40c8SMing Lei static inline unsigned int blk_rq_get_max_segments(struct request *rq)
537943b40c8SMing Lei {
538943b40c8SMing Lei 	if (req_op(rq) == REQ_OP_DISCARD)
539943b40c8SMing Lei 		return queue_max_discard_segments(rq->q);
540943b40c8SMing Lei 	return queue_max_segments(rq->q);
541943b40c8SMing Lei }
542943b40c8SMing Lei 
54314ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
54414ccb66bSChristoph Hellwig 		unsigned int nr_phys_segs)
545d6d48196SJens Axboe {
546943b40c8SMing Lei 	if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req))
54713f05c8dSMartin K. Petersen 		goto no_merge;
54813f05c8dSMartin K. Petersen 
54914ccb66bSChristoph Hellwig 	if (blk_integrity_merge_bio(req->q, req, bio) == false)
55013f05c8dSMartin K. Petersen 		goto no_merge;
551d6d48196SJens Axboe 
552d6d48196SJens Axboe 	/*
553d6d48196SJens Axboe 	 * This will form the start of a new hw segment.  Bump both
554d6d48196SJens Axboe 	 * counters.
555d6d48196SJens Axboe 	 */
556d6d48196SJens Axboe 	req->nr_phys_segments += nr_phys_segs;
557d6d48196SJens Axboe 	return 1;
55813f05c8dSMartin K. Petersen 
55913f05c8dSMartin K. Petersen no_merge:
56014ccb66bSChristoph Hellwig 	req_set_nomerge(req->q, req);
56113f05c8dSMartin K. Petersen 	return 0;
562d6d48196SJens Axboe }
563d6d48196SJens Axboe 
56414ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
565d6d48196SJens Axboe {
5665e7c4274SJens Axboe 	if (req_gap_back_merge(req, bio))
5675e7c4274SJens Axboe 		return 0;
5687f39add3SSagi Grimberg 	if (blk_integrity_rq(req) &&
5697f39add3SSagi Grimberg 	    integrity_req_gap_back_merge(req, bio))
5707f39add3SSagi Grimberg 		return 0;
571a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_back_mergeable(req, bio))
572a892c8d5SSatya Tangirala 		return 0;
573f31dc1cdSMartin K. Petersen 	if (blk_rq_sectors(req) + bio_sectors(bio) >
57417007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
57514ccb66bSChristoph Hellwig 		req_set_nomerge(req->q, req);
576d6d48196SJens Axboe 		return 0;
577d6d48196SJens Axboe 	}
578d6d48196SJens Axboe 
57914ccb66bSChristoph Hellwig 	return ll_new_hw_segment(req, bio, nr_segs);
580d6d48196SJens Axboe }
581d6d48196SJens Axboe 
58214ccb66bSChristoph Hellwig int ll_front_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
583d6d48196SJens Axboe {
5845e7c4274SJens Axboe 	if (req_gap_front_merge(req, bio))
5855e7c4274SJens Axboe 		return 0;
5867f39add3SSagi Grimberg 	if (blk_integrity_rq(req) &&
5877f39add3SSagi Grimberg 	    integrity_req_gap_front_merge(req, bio))
5887f39add3SSagi Grimberg 		return 0;
589a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_front_mergeable(req, bio))
590a892c8d5SSatya Tangirala 		return 0;
591f31dc1cdSMartin K. Petersen 	if (blk_rq_sectors(req) + bio_sectors(bio) >
59217007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
59314ccb66bSChristoph Hellwig 		req_set_nomerge(req->q, req);
594d6d48196SJens Axboe 		return 0;
595d6d48196SJens Axboe 	}
596d6d48196SJens Axboe 
59714ccb66bSChristoph Hellwig 	return ll_new_hw_segment(req, bio, nr_segs);
598d6d48196SJens Axboe }
599d6d48196SJens Axboe 
600445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req,
601445251d0SJens Axboe 		struct request *next)
602445251d0SJens Axboe {
603445251d0SJens Axboe 	unsigned short segments = blk_rq_nr_discard_segments(req);
604445251d0SJens Axboe 
605445251d0SJens Axboe 	if (segments >= queue_max_discard_segments(q))
606445251d0SJens Axboe 		goto no_merge;
607445251d0SJens Axboe 	if (blk_rq_sectors(req) + bio_sectors(next->bio) >
608445251d0SJens Axboe 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
609445251d0SJens Axboe 		goto no_merge;
610445251d0SJens Axboe 
611445251d0SJens Axboe 	req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next);
612445251d0SJens Axboe 	return true;
613445251d0SJens Axboe no_merge:
614445251d0SJens Axboe 	req_set_nomerge(q, req);
615445251d0SJens Axboe 	return false;
616445251d0SJens Axboe }
617445251d0SJens Axboe 
618d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
619d6d48196SJens Axboe 				struct request *next)
620d6d48196SJens Axboe {
621d6d48196SJens Axboe 	int total_phys_segments;
622d6d48196SJens Axboe 
6235e7c4274SJens Axboe 	if (req_gap_back_merge(req, next->bio))
624854fbb9cSKeith Busch 		return 0;
625854fbb9cSKeith Busch 
626d6d48196SJens Axboe 	/*
627d6d48196SJens Axboe 	 * Will it become too large?
628d6d48196SJens Axboe 	 */
629f31dc1cdSMartin K. Petersen 	if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
63017007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
631d6d48196SJens Axboe 		return 0;
632d6d48196SJens Axboe 
633d6d48196SJens Axboe 	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
634943b40c8SMing Lei 	if (total_phys_segments > blk_rq_get_max_segments(req))
635d6d48196SJens Axboe 		return 0;
636d6d48196SJens Axboe 
6374eaf99beSMartin K. Petersen 	if (blk_integrity_merge_rq(q, req, next) == false)
63813f05c8dSMartin K. Petersen 		return 0;
63913f05c8dSMartin K. Petersen 
640a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_merge_rq(req, next))
641a892c8d5SSatya Tangirala 		return 0;
642a892c8d5SSatya Tangirala 
643d6d48196SJens Axboe 	/* Merge is OK... */
644d6d48196SJens Axboe 	req->nr_phys_segments = total_phys_segments;
645d6d48196SJens Axboe 	return 1;
646d6d48196SJens Axboe }
647d6d48196SJens Axboe 
64880a761fdSTejun Heo /**
64980a761fdSTejun Heo  * blk_rq_set_mixed_merge - mark a request as mixed merge
65080a761fdSTejun Heo  * @rq: request to mark as mixed merge
65180a761fdSTejun Heo  *
65280a761fdSTejun Heo  * Description:
65380a761fdSTejun Heo  *     @rq is about to be mixed merged.  Make sure the attributes
65480a761fdSTejun Heo  *     which can be mixed are set in each bio and mark @rq as mixed
65580a761fdSTejun Heo  *     merged.
65680a761fdSTejun Heo  */
65780a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq)
65880a761fdSTejun Heo {
65980a761fdSTejun Heo 	unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
66080a761fdSTejun Heo 	struct bio *bio;
66180a761fdSTejun Heo 
662e8064021SChristoph Hellwig 	if (rq->rq_flags & RQF_MIXED_MERGE)
66380a761fdSTejun Heo 		return;
66480a761fdSTejun Heo 
66580a761fdSTejun Heo 	/*
66680a761fdSTejun Heo 	 * @rq will no longer represent mixable attributes for all the
66780a761fdSTejun Heo 	 * contained bios.  It will just track those of the first one.
66880a761fdSTejun Heo 	 * Distributes the attributs to each bio.
66980a761fdSTejun Heo 	 */
67080a761fdSTejun Heo 	for (bio = rq->bio; bio; bio = bio->bi_next) {
6711eff9d32SJens Axboe 		WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
6721eff9d32SJens Axboe 			     (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
6731eff9d32SJens Axboe 		bio->bi_opf |= ff;
67480a761fdSTejun Heo 	}
675e8064021SChristoph Hellwig 	rq->rq_flags |= RQF_MIXED_MERGE;
67680a761fdSTejun Heo }
67780a761fdSTejun Heo 
678b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req)
67926308eabSJerome Marchand {
68026308eabSJerome Marchand 	if (blk_do_io_stat(req)) {
681112f158fSMike Snitzer 		part_stat_lock();
682b9c54f56SKonstantin Khlebnikov 		part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
68326308eabSJerome Marchand 		part_stat_unlock();
684524f9ffdSChristoph Hellwig 
685524f9ffdSChristoph Hellwig 		hd_struct_put(req->part);
68626308eabSJerome Marchand 	}
68726308eabSJerome Marchand }
688b9c54f56SKonstantin Khlebnikov 
68969840466SJianchao Wang /*
69069840466SJianchao Wang  * Two cases of handling DISCARD merge:
69169840466SJianchao Wang  * If max_discard_segments > 1, the driver takes every bio
69269840466SJianchao Wang  * as a range and send them to controller together. The ranges
69369840466SJianchao Wang  * needn't to be contiguous.
69469840466SJianchao Wang  * Otherwise, the bios/requests will be handled as same as
69569840466SJianchao Wang  * others which should be contiguous.
69669840466SJianchao Wang  */
69769840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req)
69869840466SJianchao Wang {
69969840466SJianchao Wang 	if (req_op(req) == REQ_OP_DISCARD &&
70069840466SJianchao Wang 	    queue_max_discard_segments(req->q) > 1)
70169840466SJianchao Wang 		return true;
70269840466SJianchao Wang 	return false;
70369840466SJianchao Wang }
70469840466SJianchao Wang 
705e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req,
706e96c0d83SEric Biggers 					struct request *next)
70769840466SJianchao Wang {
70869840466SJianchao Wang 	if (blk_discard_mergable(req))
70969840466SJianchao Wang 		return ELEVATOR_DISCARD_MERGE;
71069840466SJianchao Wang 	else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
71169840466SJianchao Wang 		return ELEVATOR_BACK_MERGE;
71269840466SJianchao Wang 
71369840466SJianchao Wang 	return ELEVATOR_NO_MERGE;
71469840466SJianchao Wang }
71526308eabSJerome Marchand 
716d6d48196SJens Axboe /*
717b973cb7eSJens Axboe  * For non-mq, this has to be called with the request spinlock acquired.
718b973cb7eSJens Axboe  * For mq with scheduling, the appropriate queue wide lock should be held.
719d6d48196SJens Axboe  */
720b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q,
721b973cb7eSJens Axboe 				     struct request *req, struct request *next)
722d6d48196SJens Axboe {
723d6d48196SJens Axboe 	if (!rq_mergeable(req) || !rq_mergeable(next))
724b973cb7eSJens Axboe 		return NULL;
725d6d48196SJens Axboe 
726288dab8aSChristoph Hellwig 	if (req_op(req) != req_op(next))
727b973cb7eSJens Axboe 		return NULL;
728f31dc1cdSMartin K. Petersen 
729d6d48196SJens Axboe 	if (rq_data_dir(req) != rq_data_dir(next)
7302081a56bSJens Axboe 	    || req->rq_disk != next->rq_disk)
731b973cb7eSJens Axboe 		return NULL;
732d6d48196SJens Axboe 
7338fe0d473SMike Christie 	if (req_op(req) == REQ_OP_WRITE_SAME &&
7344363ac7cSMartin K. Petersen 	    !blk_write_same_mergeable(req->bio, next->bio))
735b973cb7eSJens Axboe 		return NULL;
7364363ac7cSMartin K. Petersen 
737d6d48196SJens Axboe 	/*
738cb6934f8SJens Axboe 	 * Don't allow merge of different write hints, or for a hint with
739cb6934f8SJens Axboe 	 * non-hint IO.
740cb6934f8SJens Axboe 	 */
741cb6934f8SJens Axboe 	if (req->write_hint != next->write_hint)
742cb6934f8SJens Axboe 		return NULL;
743cb6934f8SJens Axboe 
744668ffc03SDamien Le Moal 	if (req->ioprio != next->ioprio)
745668ffc03SDamien Le Moal 		return NULL;
746668ffc03SDamien Le Moal 
747cb6934f8SJens Axboe 	/*
748d6d48196SJens Axboe 	 * If we are allowed to merge, then append bio list
749d6d48196SJens Axboe 	 * from next to rq and release next. merge_requests_fn
750d6d48196SJens Axboe 	 * will have updated segment counts, update sector
751445251d0SJens Axboe 	 * counts here. Handle DISCARDs separately, as they
752445251d0SJens Axboe 	 * have separate settings.
753d6d48196SJens Axboe 	 */
75469840466SJianchao Wang 
75569840466SJianchao Wang 	switch (blk_try_req_merge(req, next)) {
75669840466SJianchao Wang 	case ELEVATOR_DISCARD_MERGE:
757445251d0SJens Axboe 		if (!req_attempt_discard_merge(q, req, next))
758445251d0SJens Axboe 			return NULL;
75969840466SJianchao Wang 		break;
76069840466SJianchao Wang 	case ELEVATOR_BACK_MERGE:
76169840466SJianchao Wang 		if (!ll_merge_requests_fn(q, req, next))
762b973cb7eSJens Axboe 			return NULL;
76369840466SJianchao Wang 		break;
76469840466SJianchao Wang 	default:
76569840466SJianchao Wang 		return NULL;
76669840466SJianchao Wang 	}
767d6d48196SJens Axboe 
768d6d48196SJens Axboe 	/*
76980a761fdSTejun Heo 	 * If failfast settings disagree or any of the two is already
77080a761fdSTejun Heo 	 * a mixed merge, mark both as mixed before proceeding.  This
77180a761fdSTejun Heo 	 * makes sure that all involved bios have mixable attributes
77280a761fdSTejun Heo 	 * set properly.
77380a761fdSTejun Heo 	 */
774e8064021SChristoph Hellwig 	if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
77580a761fdSTejun Heo 	    (req->cmd_flags & REQ_FAILFAST_MASK) !=
77680a761fdSTejun Heo 	    (next->cmd_flags & REQ_FAILFAST_MASK)) {
77780a761fdSTejun Heo 		blk_rq_set_mixed_merge(req);
77880a761fdSTejun Heo 		blk_rq_set_mixed_merge(next);
77980a761fdSTejun Heo 	}
78080a761fdSTejun Heo 
78180a761fdSTejun Heo 	/*
782522a7775SOmar Sandoval 	 * At this point we have either done a back merge or front merge. We
783522a7775SOmar Sandoval 	 * need the smaller start_time_ns of the merged requests to be the
784522a7775SOmar Sandoval 	 * current request for accounting purposes.
785d6d48196SJens Axboe 	 */
786522a7775SOmar Sandoval 	if (next->start_time_ns < req->start_time_ns)
787522a7775SOmar Sandoval 		req->start_time_ns = next->start_time_ns;
788d6d48196SJens Axboe 
789d6d48196SJens Axboe 	req->biotail->bi_next = next->bio;
790d6d48196SJens Axboe 	req->biotail = next->biotail;
791d6d48196SJens Axboe 
792a2dec7b3STejun Heo 	req->__data_len += blk_rq_bytes(next);
793d6d48196SJens Axboe 
7942a5cf35cSMing Lei 	if (!blk_discard_mergable(req))
795d6d48196SJens Axboe 		elv_merge_requests(q, req, next);
796d6d48196SJens Axboe 
79742dad764SJerome Marchand 	/*
79842dad764SJerome Marchand 	 * 'next' is going away, so update stats accordingly
79942dad764SJerome Marchand 	 */
800b9c54f56SKonstantin Khlebnikov 	blk_account_io_merge_request(next);
801d6d48196SJens Axboe 
802f3bdc62fSJan Kara 	trace_block_rq_merge(q, next);
803f3bdc62fSJan Kara 
804e4d750c9SJens Axboe 	/*
805e4d750c9SJens Axboe 	 * ownership of bio passed from next to req, return 'next' for
806e4d750c9SJens Axboe 	 * the caller to free
807e4d750c9SJens Axboe 	 */
8081cd96c24SBoaz Harrosh 	next->bio = NULL;
809b973cb7eSJens Axboe 	return next;
810d6d48196SJens Axboe }
811d6d48196SJens Axboe 
812b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq)
813d6d48196SJens Axboe {
814d6d48196SJens Axboe 	struct request *next = elv_latter_request(q, rq);
815d6d48196SJens Axboe 
816d6d48196SJens Axboe 	if (next)
817d6d48196SJens Axboe 		return attempt_merge(q, rq, next);
818d6d48196SJens Axboe 
819b973cb7eSJens Axboe 	return NULL;
820d6d48196SJens Axboe }
821d6d48196SJens Axboe 
822b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq)
823d6d48196SJens Axboe {
824d6d48196SJens Axboe 	struct request *prev = elv_former_request(q, rq);
825d6d48196SJens Axboe 
826d6d48196SJens Axboe 	if (prev)
827d6d48196SJens Axboe 		return attempt_merge(q, prev, rq);
828d6d48196SJens Axboe 
829b973cb7eSJens Axboe 	return NULL;
830d6d48196SJens Axboe }
8315e84ea3aSJens Axboe 
8325e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
8335e84ea3aSJens Axboe 			  struct request *next)
8345e84ea3aSJens Axboe {
835e4d750c9SJens Axboe 	struct request *free;
83672ef799bSTahsin Erdogan 
837e4d750c9SJens Axboe 	free = attempt_merge(q, rq, next);
838e4d750c9SJens Axboe 	if (free) {
83992bc5a24SJens Axboe 		blk_put_request(free);
840e4d750c9SJens Axboe 		return 1;
841e4d750c9SJens Axboe 	}
842e4d750c9SJens Axboe 
843e4d750c9SJens Axboe 	return 0;
8445e84ea3aSJens Axboe }
845050c8ea8STejun Heo 
846050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
847050c8ea8STejun Heo {
848e2a60da7SMartin K. Petersen 	if (!rq_mergeable(rq) || !bio_mergeable(bio))
849050c8ea8STejun Heo 		return false;
850050c8ea8STejun Heo 
851288dab8aSChristoph Hellwig 	if (req_op(rq) != bio_op(bio))
852f31dc1cdSMartin K. Petersen 		return false;
853f31dc1cdSMartin K. Petersen 
854050c8ea8STejun Heo 	/* different data direction or already started, don't merge */
855050c8ea8STejun Heo 	if (bio_data_dir(bio) != rq_data_dir(rq))
856050c8ea8STejun Heo 		return false;
857050c8ea8STejun Heo 
8582081a56bSJens Axboe 	/* must be same device */
8592081a56bSJens Axboe 	if (rq->rq_disk != bio->bi_disk)
860050c8ea8STejun Heo 		return false;
861050c8ea8STejun Heo 
862050c8ea8STejun Heo 	/* only merge integrity protected bio into ditto rq */
8634eaf99beSMartin K. Petersen 	if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
864050c8ea8STejun Heo 		return false;
865050c8ea8STejun Heo 
866a892c8d5SSatya Tangirala 	/* Only merge if the crypt contexts are compatible */
867a892c8d5SSatya Tangirala 	if (!bio_crypt_rq_ctx_compatible(rq, bio))
868a892c8d5SSatya Tangirala 		return false;
869a892c8d5SSatya Tangirala 
8704363ac7cSMartin K. Petersen 	/* must be using the same buffer */
8718fe0d473SMike Christie 	if (req_op(rq) == REQ_OP_WRITE_SAME &&
8724363ac7cSMartin K. Petersen 	    !blk_write_same_mergeable(rq->bio, bio))
8734363ac7cSMartin K. Petersen 		return false;
8744363ac7cSMartin K. Petersen 
875cb6934f8SJens Axboe 	/*
876cb6934f8SJens Axboe 	 * Don't allow merge of different write hints, or for a hint with
877cb6934f8SJens Axboe 	 * non-hint IO.
878cb6934f8SJens Axboe 	 */
879cb6934f8SJens Axboe 	if (rq->write_hint != bio->bi_write_hint)
880cb6934f8SJens Axboe 		return false;
881cb6934f8SJens Axboe 
882668ffc03SDamien Le Moal 	if (rq->ioprio != bio_prio(bio))
883668ffc03SDamien Le Moal 		return false;
884668ffc03SDamien Le Moal 
885050c8ea8STejun Heo 	return true;
886050c8ea8STejun Heo }
887050c8ea8STejun Heo 
88834fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
889050c8ea8STejun Heo {
89069840466SJianchao Wang 	if (blk_discard_mergable(rq))
8911e739730SChristoph Hellwig 		return ELEVATOR_DISCARD_MERGE;
8921e739730SChristoph Hellwig 	else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
893050c8ea8STejun Heo 		return ELEVATOR_BACK_MERGE;
8944f024f37SKent Overstreet 	else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
895050c8ea8STejun Heo 		return ELEVATOR_FRONT_MERGE;
896050c8ea8STejun Heo 	return ELEVATOR_NO_MERGE;
897050c8ea8STejun Heo }
898