xref: /openbmc/linux/block/blk-merge.c (revision a892c8d5)
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  * @q:       [in] request queue pointer
287dad77584SBart Van Assche  * @bio:     [in, out] bio to be split
288dad77584SBart Van Assche  * @nr_segs: [out] number of segments in the first bio
289dad77584SBart Van Assche  *
290dad77584SBart Van Assche  * Split a bio into two bios, chain the two bios, submit the second half and
291dad77584SBart Van Assche  * store a pointer to the first half in *@bio. If the second bio is still too
292dad77584SBart Van Assche  * big it will be split by a recursive call to this function. Since this
293dad77584SBart Van Assche  * function may allocate a new bio from @q->bio_split, it is the responsibility
294dad77584SBart Van Assche  * of the caller to ensure that @q is only released after processing of the
295dad77584SBart Van Assche  * split bio has finished.
296dad77584SBart Van Assche  */
29714ccb66bSChristoph Hellwig void __blk_queue_split(struct request_queue *q, struct bio **bio,
29814ccb66bSChristoph Hellwig 		unsigned int *nr_segs)
29954efd50bSKent Overstreet {
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);
34154efd50bSKent Overstreet 		generic_make_request(*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  * @q:   [in] request queue pointer
349dad77584SBart Van Assche  * @bio: [in, out] bio to be split
350dad77584SBart Van Assche  *
351dad77584SBart Van Assche  * Split a bio into two bios, chains the two bios, submit the second half and
352dad77584SBart Van Assche  * store a pointer to the first half in *@bio. Since this function may allocate
353dad77584SBart Van Assche  * a new bio from @q->bio_split, it is the responsibility of the caller to
354dad77584SBart Van Assche  * ensure that @q is only released after processing of the split bio has
355dad77584SBart Van Assche  * finished.
356dad77584SBart Van Assche  */
35714ccb66bSChristoph Hellwig void blk_queue_split(struct request_queue *q, struct bio **bio)
35814ccb66bSChristoph Hellwig {
35914ccb66bSChristoph Hellwig 	unsigned int nr_segs;
36014ccb66bSChristoph Hellwig 
36114ccb66bSChristoph Hellwig 	__blk_queue_split(q, bio, &nr_segs);
36214ccb66bSChristoph Hellwig }
36354efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split);
36454efd50bSKent Overstreet 
365e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq)
366d6d48196SJens Axboe {
3676869875fSChristoph Hellwig 	unsigned int nr_phys_segs = 0;
368ff9811b3SBart Van Assche 	unsigned int nr_sectors = 0;
369e9cd19c0SChristoph Hellwig 	struct req_iterator iter;
3706869875fSChristoph Hellwig 	struct bio_vec bv;
371d6d48196SJens Axboe 
372e9cd19c0SChristoph Hellwig 	if (!rq->bio)
3731e428079SJens Axboe 		return 0;
374d6d48196SJens Axboe 
375e9cd19c0SChristoph Hellwig 	switch (bio_op(rq->bio)) {
376a6f0788eSChaitanya Kulkarni 	case REQ_OP_DISCARD:
377a6f0788eSChaitanya Kulkarni 	case REQ_OP_SECURE_ERASE:
378a6f0788eSChaitanya Kulkarni 	case REQ_OP_WRITE_ZEROES:
379f9d03f96SChristoph Hellwig 		return 0;
380f9d03f96SChristoph Hellwig 	case REQ_OP_WRITE_SAME:
3815cb8850cSKent Overstreet 		return 1;
382a6f0788eSChaitanya Kulkarni 	}
3835cb8850cSKent Overstreet 
384e9cd19c0SChristoph Hellwig 	rq_for_each_bvec(bv, rq, iter)
385ff9811b3SBart Van Assche 		bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors,
386708b25b3SBart Van Assche 				UINT_MAX, UINT_MAX);
3871e428079SJens Axboe 	return nr_phys_segs;
3881e428079SJens Axboe }
3891e428079SJens Axboe 
39048d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg,
391862e5a5eSMing Lei 		struct scatterlist *sglist)
392862e5a5eSMing Lei {
393862e5a5eSMing Lei 	if (!*sg)
394862e5a5eSMing Lei 		return sglist;
395862e5a5eSMing Lei 
396862e5a5eSMing Lei 	/*
397862e5a5eSMing Lei 	 * If the driver previously mapped a shorter list, we could see a
398862e5a5eSMing Lei 	 * termination bit prematurely unless it fully inits the sg table
399862e5a5eSMing Lei 	 * on each mapping. We KNOW that there must be more entries here
400862e5a5eSMing Lei 	 * or the driver would be buggy, so force clear the termination bit
401862e5a5eSMing Lei 	 * to avoid doing a full sg_init_table() in drivers for each command.
402862e5a5eSMing Lei 	 */
403862e5a5eSMing Lei 	sg_unmark_end(*sg);
404862e5a5eSMing Lei 	return sg_next(*sg);
405862e5a5eSMing Lei }
406862e5a5eSMing Lei 
407862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q,
408862e5a5eSMing Lei 		struct bio_vec *bvec, struct scatterlist *sglist,
409862e5a5eSMing Lei 		struct scatterlist **sg)
410862e5a5eSMing Lei {
411862e5a5eSMing Lei 	unsigned nbytes = bvec->bv_len;
4128a96a0e4SChristoph Hellwig 	unsigned nsegs = 0, total = 0;
413862e5a5eSMing Lei 
414862e5a5eSMing Lei 	while (nbytes > 0) {
4158a96a0e4SChristoph Hellwig 		unsigned offset = bvec->bv_offset + total;
416429120f3SMing Lei 		unsigned len = min(get_max_segment_size(q, bvec->bv_page,
417429120f3SMing Lei 					offset), nbytes);
418f9f76879SChristoph Hellwig 		struct page *page = bvec->bv_page;
419f9f76879SChristoph Hellwig 
420f9f76879SChristoph Hellwig 		/*
421f9f76879SChristoph Hellwig 		 * Unfortunately a fair number of drivers barf on scatterlists
422f9f76879SChristoph Hellwig 		 * that have an offset larger than PAGE_SIZE, despite other
423f9f76879SChristoph Hellwig 		 * subsystems dealing with that invariant just fine.  For now
424f9f76879SChristoph Hellwig 		 * stick to the legacy format where we never present those from
425f9f76879SChristoph Hellwig 		 * the block layer, but the code below should be removed once
426f9f76879SChristoph Hellwig 		 * these offenders (mostly MMC/SD drivers) are fixed.
427f9f76879SChristoph Hellwig 		 */
428f9f76879SChristoph Hellwig 		page += (offset >> PAGE_SHIFT);
429f9f76879SChristoph Hellwig 		offset &= ~PAGE_MASK;
430862e5a5eSMing Lei 
431862e5a5eSMing Lei 		*sg = blk_next_sg(sg, sglist);
432f9f76879SChristoph Hellwig 		sg_set_page(*sg, page, len, offset);
433862e5a5eSMing Lei 
4348a96a0e4SChristoph Hellwig 		total += len;
4358a96a0e4SChristoph Hellwig 		nbytes -= len;
436862e5a5eSMing Lei 		nsegs++;
437862e5a5eSMing Lei 	}
438862e5a5eSMing Lei 
439862e5a5eSMing Lei 	return nsegs;
440862e5a5eSMing Lei }
441862e5a5eSMing Lei 
44216e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv,
44316e3e418SMing Lei 		struct scatterlist *sglist, struct scatterlist **sg)
44416e3e418SMing Lei {
44516e3e418SMing Lei 	*sg = blk_next_sg(sg, sglist);
44616e3e418SMing Lei 	sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
44716e3e418SMing Lei 	return 1;
44816e3e418SMing Lei }
44916e3e418SMing Lei 
450f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */
451f6970f83SMing Lei static inline bool
452f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec,
453f6970f83SMing Lei 			   struct bio_vec *bvprv, struct scatterlist **sg)
454963ab9e5SAsias He {
455963ab9e5SAsias He 
456963ab9e5SAsias He 	int nbytes = bvec->bv_len;
457963ab9e5SAsias He 
458f6970f83SMing Lei 	if (!*sg)
459f6970f83SMing Lei 		return false;
460f6970f83SMing Lei 
461b4b6cb61SMing Lei 	if ((*sg)->length + nbytes > queue_max_segment_size(q))
462f6970f83SMing Lei 		return false;
463f6970f83SMing Lei 
4643dccdae5SChristoph Hellwig 	if (!biovec_phys_mergeable(q, bvprv, bvec))
465f6970f83SMing Lei 		return false;
466963ab9e5SAsias He 
467963ab9e5SAsias He 	(*sg)->length += nbytes;
468f6970f83SMing Lei 
469f6970f83SMing Lei 	return true;
470963ab9e5SAsias He }
471963ab9e5SAsias He 
4725cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
4735cb8850cSKent Overstreet 			     struct scatterlist *sglist,
4745cb8850cSKent Overstreet 			     struct scatterlist **sg)
4755cb8850cSKent Overstreet {
476b21e11c5SMing Lei 	struct bio_vec uninitialized_var(bvec), bvprv = { NULL };
4775cb8850cSKent Overstreet 	struct bvec_iter iter;
47838417468SChristoph Hellwig 	int nsegs = 0;
479f6970f83SMing Lei 	bool new_bio = false;
4805cb8850cSKent Overstreet 
481f6970f83SMing Lei 	for_each_bio(bio) {
482f6970f83SMing Lei 		bio_for_each_bvec(bvec, bio, iter) {
483f6970f83SMing Lei 			/*
484f6970f83SMing Lei 			 * Only try to merge bvecs from two bios given we
485f6970f83SMing Lei 			 * have done bio internal merge when adding pages
486f6970f83SMing Lei 			 * to bio
487f6970f83SMing Lei 			 */
488f6970f83SMing Lei 			if (new_bio &&
489f6970f83SMing Lei 			    __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg))
490f6970f83SMing Lei 				goto next_bvec;
491f6970f83SMing Lei 
492f6970f83SMing Lei 			if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE)
493f6970f83SMing Lei 				nsegs += __blk_bvec_map_sg(bvec, sglist, sg);
494f6970f83SMing Lei 			else
495f6970f83SMing Lei 				nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg);
496f6970f83SMing Lei  next_bvec:
497f6970f83SMing Lei 			new_bio = false;
498f6970f83SMing Lei 		}
499b21e11c5SMing Lei 		if (likely(bio->bi_iter.bi_size)) {
500f6970f83SMing Lei 			bvprv = bvec;
501f6970f83SMing Lei 			new_bio = true;
502f6970f83SMing Lei 		}
503b21e11c5SMing Lei 	}
5045cb8850cSKent Overstreet 
5055cb8850cSKent Overstreet 	return nsegs;
5065cb8850cSKent Overstreet }
5075cb8850cSKent Overstreet 
508d6d48196SJens Axboe /*
509d6d48196SJens Axboe  * map a request to scatterlist, return number of sg entries setup. Caller
510d6d48196SJens Axboe  * must make sure sg can hold rq->nr_phys_segments entries
511d6d48196SJens Axboe  */
51289de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
51389de1504SChristoph Hellwig 		struct scatterlist *sglist, struct scatterlist **last_sg)
514d6d48196SJens Axboe {
5155cb8850cSKent Overstreet 	int nsegs = 0;
516d6d48196SJens Axboe 
517f9d03f96SChristoph Hellwig 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
51889de1504SChristoph Hellwig 		nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg);
519f9d03f96SChristoph Hellwig 	else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME)
52089de1504SChristoph Hellwig 		nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg);
521f9d03f96SChristoph Hellwig 	else if (rq->bio)
52289de1504SChristoph Hellwig 		nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg);
523f18573abSFUJITA Tomonori 
52489de1504SChristoph Hellwig 	if (*last_sg)
52589de1504SChristoph Hellwig 		sg_mark_end(*last_sg);
526d6d48196SJens Axboe 
52712e57f59SMing Lei 	/*
52812e57f59SMing Lei 	 * Something must have been wrong if the figured number of
52912e57f59SMing Lei 	 * segment is bigger than number of req's physical segments
53012e57f59SMing Lei 	 */
531f9d03f96SChristoph Hellwig 	WARN_ON(nsegs > blk_rq_nr_phys_segments(rq));
53212e57f59SMing Lei 
533d6d48196SJens Axboe 	return nsegs;
534d6d48196SJens Axboe }
53589de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg);
536d6d48196SJens Axboe 
53714ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
53814ccb66bSChristoph Hellwig 		unsigned int nr_phys_segs)
539d6d48196SJens Axboe {
54014ccb66bSChristoph Hellwig 	if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(req->q))
54113f05c8dSMartin K. Petersen 		goto no_merge;
54213f05c8dSMartin K. Petersen 
54314ccb66bSChristoph Hellwig 	if (blk_integrity_merge_bio(req->q, req, bio) == false)
54413f05c8dSMartin K. Petersen 		goto no_merge;
545d6d48196SJens Axboe 
546d6d48196SJens Axboe 	/*
547d6d48196SJens Axboe 	 * This will form the start of a new hw segment.  Bump both
548d6d48196SJens Axboe 	 * counters.
549d6d48196SJens Axboe 	 */
550d6d48196SJens Axboe 	req->nr_phys_segments += nr_phys_segs;
551d6d48196SJens Axboe 	return 1;
55213f05c8dSMartin K. Petersen 
55313f05c8dSMartin K. Petersen no_merge:
55414ccb66bSChristoph Hellwig 	req_set_nomerge(req->q, req);
55513f05c8dSMartin K. Petersen 	return 0;
556d6d48196SJens Axboe }
557d6d48196SJens Axboe 
55814ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
559d6d48196SJens Axboe {
5605e7c4274SJens Axboe 	if (req_gap_back_merge(req, bio))
5615e7c4274SJens Axboe 		return 0;
5627f39add3SSagi Grimberg 	if (blk_integrity_rq(req) &&
5637f39add3SSagi Grimberg 	    integrity_req_gap_back_merge(req, bio))
5647f39add3SSagi Grimberg 		return 0;
565a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_back_mergeable(req, bio))
566a892c8d5SSatya Tangirala 		return 0;
567f31dc1cdSMartin K. Petersen 	if (blk_rq_sectors(req) + bio_sectors(bio) >
56817007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
56914ccb66bSChristoph Hellwig 		req_set_nomerge(req->q, req);
570d6d48196SJens Axboe 		return 0;
571d6d48196SJens Axboe 	}
572d6d48196SJens Axboe 
57314ccb66bSChristoph Hellwig 	return ll_new_hw_segment(req, bio, nr_segs);
574d6d48196SJens Axboe }
575d6d48196SJens Axboe 
57614ccb66bSChristoph Hellwig int ll_front_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
577d6d48196SJens Axboe {
5785e7c4274SJens Axboe 	if (req_gap_front_merge(req, bio))
5795e7c4274SJens Axboe 		return 0;
5807f39add3SSagi Grimberg 	if (blk_integrity_rq(req) &&
5817f39add3SSagi Grimberg 	    integrity_req_gap_front_merge(req, bio))
5827f39add3SSagi Grimberg 		return 0;
583a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_front_mergeable(req, bio))
584a892c8d5SSatya Tangirala 		return 0;
585f31dc1cdSMartin K. Petersen 	if (blk_rq_sectors(req) + bio_sectors(bio) >
58617007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
58714ccb66bSChristoph Hellwig 		req_set_nomerge(req->q, req);
588d6d48196SJens Axboe 		return 0;
589d6d48196SJens Axboe 	}
590d6d48196SJens Axboe 
59114ccb66bSChristoph Hellwig 	return ll_new_hw_segment(req, bio, nr_segs);
592d6d48196SJens Axboe }
593d6d48196SJens Axboe 
594445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req,
595445251d0SJens Axboe 		struct request *next)
596445251d0SJens Axboe {
597445251d0SJens Axboe 	unsigned short segments = blk_rq_nr_discard_segments(req);
598445251d0SJens Axboe 
599445251d0SJens Axboe 	if (segments >= queue_max_discard_segments(q))
600445251d0SJens Axboe 		goto no_merge;
601445251d0SJens Axboe 	if (blk_rq_sectors(req) + bio_sectors(next->bio) >
602445251d0SJens Axboe 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
603445251d0SJens Axboe 		goto no_merge;
604445251d0SJens Axboe 
605445251d0SJens Axboe 	req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next);
606445251d0SJens Axboe 	return true;
607445251d0SJens Axboe no_merge:
608445251d0SJens Axboe 	req_set_nomerge(q, req);
609445251d0SJens Axboe 	return false;
610445251d0SJens Axboe }
611445251d0SJens Axboe 
612d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
613d6d48196SJens Axboe 				struct request *next)
614d6d48196SJens Axboe {
615d6d48196SJens Axboe 	int total_phys_segments;
616d6d48196SJens Axboe 
6175e7c4274SJens Axboe 	if (req_gap_back_merge(req, next->bio))
618854fbb9cSKeith Busch 		return 0;
619854fbb9cSKeith Busch 
620d6d48196SJens Axboe 	/*
621d6d48196SJens Axboe 	 * Will it become too large?
622d6d48196SJens Axboe 	 */
623f31dc1cdSMartin K. Petersen 	if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
62417007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
625d6d48196SJens Axboe 		return 0;
626d6d48196SJens Axboe 
627d6d48196SJens Axboe 	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
6288a78362cSMartin K. Petersen 	if (total_phys_segments > queue_max_segments(q))
629d6d48196SJens Axboe 		return 0;
630d6d48196SJens Axboe 
6314eaf99beSMartin K. Petersen 	if (blk_integrity_merge_rq(q, req, next) == false)
63213f05c8dSMartin K. Petersen 		return 0;
63313f05c8dSMartin K. Petersen 
634a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_merge_rq(req, next))
635a892c8d5SSatya Tangirala 		return 0;
636a892c8d5SSatya Tangirala 
637d6d48196SJens Axboe 	/* Merge is OK... */
638d6d48196SJens Axboe 	req->nr_phys_segments = total_phys_segments;
639d6d48196SJens Axboe 	return 1;
640d6d48196SJens Axboe }
641d6d48196SJens Axboe 
64280a761fdSTejun Heo /**
64380a761fdSTejun Heo  * blk_rq_set_mixed_merge - mark a request as mixed merge
64480a761fdSTejun Heo  * @rq: request to mark as mixed merge
64580a761fdSTejun Heo  *
64680a761fdSTejun Heo  * Description:
64780a761fdSTejun Heo  *     @rq is about to be mixed merged.  Make sure the attributes
64880a761fdSTejun Heo  *     which can be mixed are set in each bio and mark @rq as mixed
64980a761fdSTejun Heo  *     merged.
65080a761fdSTejun Heo  */
65180a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq)
65280a761fdSTejun Heo {
65380a761fdSTejun Heo 	unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
65480a761fdSTejun Heo 	struct bio *bio;
65580a761fdSTejun Heo 
656e8064021SChristoph Hellwig 	if (rq->rq_flags & RQF_MIXED_MERGE)
65780a761fdSTejun Heo 		return;
65880a761fdSTejun Heo 
65980a761fdSTejun Heo 	/*
66080a761fdSTejun Heo 	 * @rq will no longer represent mixable attributes for all the
66180a761fdSTejun Heo 	 * contained bios.  It will just track those of the first one.
66280a761fdSTejun Heo 	 * Distributes the attributs to each bio.
66380a761fdSTejun Heo 	 */
66480a761fdSTejun Heo 	for (bio = rq->bio; bio; bio = bio->bi_next) {
6651eff9d32SJens Axboe 		WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
6661eff9d32SJens Axboe 			     (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
6671eff9d32SJens Axboe 		bio->bi_opf |= ff;
66880a761fdSTejun Heo 	}
669e8064021SChristoph Hellwig 	rq->rq_flags |= RQF_MIXED_MERGE;
67080a761fdSTejun Heo }
67180a761fdSTejun Heo 
67226308eabSJerome Marchand static void blk_account_io_merge(struct request *req)
67326308eabSJerome Marchand {
67426308eabSJerome Marchand 	if (blk_do_io_stat(req)) {
67526308eabSJerome Marchand 		struct hd_struct *part;
67626308eabSJerome Marchand 
677112f158fSMike Snitzer 		part_stat_lock();
67809e099d4SJerome Marchand 		part = req->part;
67926308eabSJerome Marchand 
680d62e26b3SJens Axboe 		part_dec_in_flight(req->q, part, rq_data_dir(req));
68126308eabSJerome Marchand 
6826c23a968SJens Axboe 		hd_struct_put(part);
68326308eabSJerome Marchand 		part_stat_unlock();
68426308eabSJerome Marchand 	}
68526308eabSJerome Marchand }
68669840466SJianchao Wang /*
68769840466SJianchao Wang  * Two cases of handling DISCARD merge:
68869840466SJianchao Wang  * If max_discard_segments > 1, the driver takes every bio
68969840466SJianchao Wang  * as a range and send them to controller together. The ranges
69069840466SJianchao Wang  * needn't to be contiguous.
69169840466SJianchao Wang  * Otherwise, the bios/requests will be handled as same as
69269840466SJianchao Wang  * others which should be contiguous.
69369840466SJianchao Wang  */
69469840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req)
69569840466SJianchao Wang {
69669840466SJianchao Wang 	if (req_op(req) == REQ_OP_DISCARD &&
69769840466SJianchao Wang 	    queue_max_discard_segments(req->q) > 1)
69869840466SJianchao Wang 		return true;
69969840466SJianchao Wang 	return false;
70069840466SJianchao Wang }
70169840466SJianchao Wang 
702e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req,
703e96c0d83SEric Biggers 					struct request *next)
70469840466SJianchao Wang {
70569840466SJianchao Wang 	if (blk_discard_mergable(req))
70669840466SJianchao Wang 		return ELEVATOR_DISCARD_MERGE;
70769840466SJianchao Wang 	else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
70869840466SJianchao Wang 		return ELEVATOR_BACK_MERGE;
70969840466SJianchao Wang 
71069840466SJianchao Wang 	return ELEVATOR_NO_MERGE;
71169840466SJianchao Wang }
71226308eabSJerome Marchand 
713d6d48196SJens Axboe /*
714b973cb7eSJens Axboe  * For non-mq, this has to be called with the request spinlock acquired.
715b973cb7eSJens Axboe  * For mq with scheduling, the appropriate queue wide lock should be held.
716d6d48196SJens Axboe  */
717b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q,
718b973cb7eSJens Axboe 				     struct request *req, struct request *next)
719d6d48196SJens Axboe {
720d6d48196SJens Axboe 	if (!rq_mergeable(req) || !rq_mergeable(next))
721b973cb7eSJens Axboe 		return NULL;
722d6d48196SJens Axboe 
723288dab8aSChristoph Hellwig 	if (req_op(req) != req_op(next))
724b973cb7eSJens Axboe 		return NULL;
725f31dc1cdSMartin K. Petersen 
726d6d48196SJens Axboe 	if (rq_data_dir(req) != rq_data_dir(next)
7272081a56bSJens Axboe 	    || req->rq_disk != next->rq_disk)
728b973cb7eSJens Axboe 		return NULL;
729d6d48196SJens Axboe 
7308fe0d473SMike Christie 	if (req_op(req) == REQ_OP_WRITE_SAME &&
7314363ac7cSMartin K. Petersen 	    !blk_write_same_mergeable(req->bio, next->bio))
732b973cb7eSJens Axboe 		return NULL;
7334363ac7cSMartin K. Petersen 
734d6d48196SJens Axboe 	/*
735cb6934f8SJens Axboe 	 * Don't allow merge of different write hints, or for a hint with
736cb6934f8SJens Axboe 	 * non-hint IO.
737cb6934f8SJens Axboe 	 */
738cb6934f8SJens Axboe 	if (req->write_hint != next->write_hint)
739cb6934f8SJens Axboe 		return NULL;
740cb6934f8SJens Axboe 
741668ffc03SDamien Le Moal 	if (req->ioprio != next->ioprio)
742668ffc03SDamien Le Moal 		return NULL;
743668ffc03SDamien Le Moal 
744cb6934f8SJens Axboe 	/*
745d6d48196SJens Axboe 	 * If we are allowed to merge, then append bio list
746d6d48196SJens Axboe 	 * from next to rq and release next. merge_requests_fn
747d6d48196SJens Axboe 	 * will have updated segment counts, update sector
748445251d0SJens Axboe 	 * counts here. Handle DISCARDs separately, as they
749445251d0SJens Axboe 	 * have separate settings.
750d6d48196SJens Axboe 	 */
75169840466SJianchao Wang 
75269840466SJianchao Wang 	switch (blk_try_req_merge(req, next)) {
75369840466SJianchao Wang 	case ELEVATOR_DISCARD_MERGE:
754445251d0SJens Axboe 		if (!req_attempt_discard_merge(q, req, next))
755445251d0SJens Axboe 			return NULL;
75669840466SJianchao Wang 		break;
75769840466SJianchao Wang 	case ELEVATOR_BACK_MERGE:
75869840466SJianchao Wang 		if (!ll_merge_requests_fn(q, req, next))
759b973cb7eSJens Axboe 			return NULL;
76069840466SJianchao Wang 		break;
76169840466SJianchao Wang 	default:
76269840466SJianchao Wang 		return NULL;
76369840466SJianchao Wang 	}
764d6d48196SJens Axboe 
765d6d48196SJens Axboe 	/*
76680a761fdSTejun Heo 	 * If failfast settings disagree or any of the two is already
76780a761fdSTejun Heo 	 * a mixed merge, mark both as mixed before proceeding.  This
76880a761fdSTejun Heo 	 * makes sure that all involved bios have mixable attributes
76980a761fdSTejun Heo 	 * set properly.
77080a761fdSTejun Heo 	 */
771e8064021SChristoph Hellwig 	if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
77280a761fdSTejun Heo 	    (req->cmd_flags & REQ_FAILFAST_MASK) !=
77380a761fdSTejun Heo 	    (next->cmd_flags & REQ_FAILFAST_MASK)) {
77480a761fdSTejun Heo 		blk_rq_set_mixed_merge(req);
77580a761fdSTejun Heo 		blk_rq_set_mixed_merge(next);
77680a761fdSTejun Heo 	}
77780a761fdSTejun Heo 
77880a761fdSTejun Heo 	/*
779522a7775SOmar Sandoval 	 * At this point we have either done a back merge or front merge. We
780522a7775SOmar Sandoval 	 * need the smaller start_time_ns of the merged requests to be the
781522a7775SOmar Sandoval 	 * current request for accounting purposes.
782d6d48196SJens Axboe 	 */
783522a7775SOmar Sandoval 	if (next->start_time_ns < req->start_time_ns)
784522a7775SOmar Sandoval 		req->start_time_ns = next->start_time_ns;
785d6d48196SJens Axboe 
786d6d48196SJens Axboe 	req->biotail->bi_next = next->bio;
787d6d48196SJens Axboe 	req->biotail = next->biotail;
788d6d48196SJens Axboe 
789a2dec7b3STejun Heo 	req->__data_len += blk_rq_bytes(next);
790d6d48196SJens Axboe 
7912a5cf35cSMing Lei 	if (!blk_discard_mergable(req))
792d6d48196SJens Axboe 		elv_merge_requests(q, req, next);
793d6d48196SJens Axboe 
79442dad764SJerome Marchand 	/*
79542dad764SJerome Marchand 	 * 'next' is going away, so update stats accordingly
79642dad764SJerome Marchand 	 */
79742dad764SJerome Marchand 	blk_account_io_merge(next);
798d6d48196SJens Axboe 
799e4d750c9SJens Axboe 	/*
800e4d750c9SJens Axboe 	 * ownership of bio passed from next to req, return 'next' for
801e4d750c9SJens Axboe 	 * the caller to free
802e4d750c9SJens Axboe 	 */
8031cd96c24SBoaz Harrosh 	next->bio = NULL;
804b973cb7eSJens Axboe 	return next;
805d6d48196SJens Axboe }
806d6d48196SJens Axboe 
807b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq)
808d6d48196SJens Axboe {
809d6d48196SJens Axboe 	struct request *next = elv_latter_request(q, rq);
810d6d48196SJens Axboe 
811d6d48196SJens Axboe 	if (next)
812d6d48196SJens Axboe 		return attempt_merge(q, rq, next);
813d6d48196SJens Axboe 
814b973cb7eSJens Axboe 	return NULL;
815d6d48196SJens Axboe }
816d6d48196SJens Axboe 
817b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq)
818d6d48196SJens Axboe {
819d6d48196SJens Axboe 	struct request *prev = elv_former_request(q, rq);
820d6d48196SJens Axboe 
821d6d48196SJens Axboe 	if (prev)
822d6d48196SJens Axboe 		return attempt_merge(q, prev, rq);
823d6d48196SJens Axboe 
824b973cb7eSJens Axboe 	return NULL;
825d6d48196SJens Axboe }
8265e84ea3aSJens Axboe 
8275e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
8285e84ea3aSJens Axboe 			  struct request *next)
8295e84ea3aSJens Axboe {
830e4d750c9SJens Axboe 	struct request *free;
83172ef799bSTahsin Erdogan 
832e4d750c9SJens Axboe 	free = attempt_merge(q, rq, next);
833e4d750c9SJens Axboe 	if (free) {
83492bc5a24SJens Axboe 		blk_put_request(free);
835e4d750c9SJens Axboe 		return 1;
836e4d750c9SJens Axboe 	}
837e4d750c9SJens Axboe 
838e4d750c9SJens Axboe 	return 0;
8395e84ea3aSJens Axboe }
840050c8ea8STejun Heo 
841050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
842050c8ea8STejun Heo {
843e2a60da7SMartin K. Petersen 	if (!rq_mergeable(rq) || !bio_mergeable(bio))
844050c8ea8STejun Heo 		return false;
845050c8ea8STejun Heo 
846288dab8aSChristoph Hellwig 	if (req_op(rq) != bio_op(bio))
847f31dc1cdSMartin K. Petersen 		return false;
848f31dc1cdSMartin K. Petersen 
849050c8ea8STejun Heo 	/* different data direction or already started, don't merge */
850050c8ea8STejun Heo 	if (bio_data_dir(bio) != rq_data_dir(rq))
851050c8ea8STejun Heo 		return false;
852050c8ea8STejun Heo 
8532081a56bSJens Axboe 	/* must be same device */
8542081a56bSJens Axboe 	if (rq->rq_disk != bio->bi_disk)
855050c8ea8STejun Heo 		return false;
856050c8ea8STejun Heo 
857050c8ea8STejun Heo 	/* only merge integrity protected bio into ditto rq */
8584eaf99beSMartin K. Petersen 	if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
859050c8ea8STejun Heo 		return false;
860050c8ea8STejun Heo 
861a892c8d5SSatya Tangirala 	/* Only merge if the crypt contexts are compatible */
862a892c8d5SSatya Tangirala 	if (!bio_crypt_rq_ctx_compatible(rq, bio))
863a892c8d5SSatya Tangirala 		return false;
864a892c8d5SSatya Tangirala 
8654363ac7cSMartin K. Petersen 	/* must be using the same buffer */
8668fe0d473SMike Christie 	if (req_op(rq) == REQ_OP_WRITE_SAME &&
8674363ac7cSMartin K. Petersen 	    !blk_write_same_mergeable(rq->bio, bio))
8684363ac7cSMartin K. Petersen 		return false;
8694363ac7cSMartin K. Petersen 
870cb6934f8SJens Axboe 	/*
871cb6934f8SJens Axboe 	 * Don't allow merge of different write hints, or for a hint with
872cb6934f8SJens Axboe 	 * non-hint IO.
873cb6934f8SJens Axboe 	 */
874cb6934f8SJens Axboe 	if (rq->write_hint != bio->bi_write_hint)
875cb6934f8SJens Axboe 		return false;
876cb6934f8SJens Axboe 
877668ffc03SDamien Le Moal 	if (rq->ioprio != bio_prio(bio))
878668ffc03SDamien Le Moal 		return false;
879668ffc03SDamien Le Moal 
880050c8ea8STejun Heo 	return true;
881050c8ea8STejun Heo }
882050c8ea8STejun Heo 
88334fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
884050c8ea8STejun Heo {
88569840466SJianchao Wang 	if (blk_discard_mergable(rq))
8861e739730SChristoph Hellwig 		return ELEVATOR_DISCARD_MERGE;
8871e739730SChristoph Hellwig 	else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
888050c8ea8STejun Heo 		return ELEVATOR_BACK_MERGE;
8894f024f37SKent Overstreet 	else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
890050c8ea8STejun Heo 		return ELEVATOR_FRONT_MERGE;
891050c8ea8STejun Heo 	return ELEVATOR_NO_MERGE;
892050c8ea8STejun Heo }
893