@@ -105,9 +105,33 @@ static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
105105 return round_down (UINT_MAX , lim -> logical_block_size ) >> SECTOR_SHIFT ;
106106}
107107
108- static struct bio * bio_split_discard (struct bio * bio ,
109- const struct queue_limits * lim ,
110- unsigned * nsegs , struct bio_set * bs )
108+ static struct bio * bio_submit_split (struct bio * bio , int split_sectors )
109+ {
110+ if (unlikely (split_sectors < 0 )) {
111+ bio -> bi_status = errno_to_blk_status (split_sectors );
112+ bio_endio (bio );
113+ return NULL ;
114+ }
115+
116+ if (split_sectors ) {
117+ struct bio * split ;
118+
119+ split = bio_split (bio , split_sectors , GFP_NOIO ,
120+ & bio -> bi_bdev -> bd_disk -> bio_split );
121+ split -> bi_opf |= REQ_NOMERGE ;
122+ blkcg_bio_issue_init (split );
123+ bio_chain (split , bio );
124+ trace_block_split (split , bio -> bi_iter .bi_sector );
125+ WARN_ON_ONCE (bio_zone_write_plugging (bio ));
126+ submit_bio_noacct (bio );
127+ return split ;
128+ }
129+
130+ return bio ;
131+ }
132+
133+ struct bio * bio_split_discard (struct bio * bio , const struct queue_limits * lim ,
134+ unsigned * nsegs )
111135{
112136 unsigned int max_discard_sectors , granularity ;
113137 sector_t tmp ;
@@ -121,10 +145,10 @@ static struct bio *bio_split_discard(struct bio *bio,
121145 min (lim -> max_discard_sectors , bio_allowed_max_sectors (lim ));
122146 max_discard_sectors -= max_discard_sectors % granularity ;
123147 if (unlikely (!max_discard_sectors ))
124- return NULL ;
148+ return bio ;
125149
126150 if (bio_sectors (bio ) <= max_discard_sectors )
127- return NULL ;
151+ return bio ;
128152
129153 split_sectors = max_discard_sectors ;
130154
@@ -139,19 +163,18 @@ static struct bio *bio_split_discard(struct bio *bio,
139163 if (split_sectors > tmp )
140164 split_sectors -= tmp ;
141165
142- return bio_split (bio , split_sectors , GFP_NOIO , bs );
166+ return bio_submit_split (bio , split_sectors );
143167}
144168
145- static struct bio * bio_split_write_zeroes (struct bio * bio ,
146- const struct queue_limits * lim ,
147- unsigned * nsegs , struct bio_set * bs )
169+ struct bio * bio_split_write_zeroes (struct bio * bio ,
170+ const struct queue_limits * lim , unsigned * nsegs )
148171{
149172 * nsegs = 0 ;
150173 if (!lim -> max_write_zeroes_sectors )
151- return NULL ;
174+ return bio ;
152175 if (bio_sectors (bio ) <= lim -> max_write_zeroes_sectors )
153- return NULL ;
154- return bio_split (bio , lim -> max_write_zeroes_sectors , GFP_NOIO , bs );
176+ return bio ;
177+ return bio_submit_split (bio , lim -> max_write_zeroes_sectors );
155178}
156179
157180static inline unsigned int blk_boundary_sectors (const struct queue_limits * lim ,
@@ -274,27 +297,19 @@ static bool bvec_split_segs(const struct queue_limits *lim,
274297}
275298
276299/**
277- * bio_split_rw - split a bio in two bios
300+ * bio_split_rw_at - check if and where to split a read/write bio
278301 * @bio: [in] bio to be split
279302 * @lim: [in] queue limits to split based on
280303 * @segs: [out] number of segments in the bio with the first half of the sectors
281- * @bs: [in] bio set to allocate the clone from
282304 * @max_bytes: [in] maximum number of bytes per bio
283305 *
284- * Clone @bio, update the bi_iter of the clone to represent the first sectors
285- * of @bio and update @bio->bi_iter to represent the remaining sectors. The
286- * following is guaranteed for the cloned bio:
287- * - That it has at most @max_bytes worth of data
288- * - That it has at most queue_max_segments(@q) segments.
289- *
290- * Except for discard requests the cloned bio will point at the bi_io_vec of
291- * the original bio. It is the responsibility of the caller to ensure that the
292- * original bio is not freed before the cloned bio. The caller is also
293- * responsible for ensuring that @bs is only destroyed after processing of the
294- * split bio has finished.
306+ * Find out if @bio needs to be split to fit the queue limits in @lim and a
307+ * maximum size of @max_bytes. Returns a negative error number if @bio can't be
308+ * split, 0 if the bio doesn't have to be split, or a positive sector offset if
309+ * @bio needs to be split.
295310 */
296- struct bio * bio_split_rw (struct bio * bio , const struct queue_limits * lim ,
297- unsigned * segs , struct bio_set * bs , unsigned max_bytes )
311+ int bio_split_rw_at (struct bio * bio , const struct queue_limits * lim ,
312+ unsigned * segs , unsigned max_bytes )
298313{
299314 struct bio_vec bv , bvprv , * bvprvp = NULL ;
300315 struct bvec_iter iter ;
@@ -324,22 +339,17 @@ struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
324339 }
325340
326341 * segs = nsegs ;
327- return NULL ;
342+ return 0 ;
328343split :
329- if (bio -> bi_opf & REQ_ATOMIC ) {
330- bio -> bi_status = BLK_STS_INVAL ;
331- bio_endio (bio );
332- return ERR_PTR (- EINVAL );
333- }
344+ if (bio -> bi_opf & REQ_ATOMIC )
345+ return - EINVAL ;
346+
334347 /*
335348 * We can't sanely support splitting for a REQ_NOWAIT bio. End it
336349 * with EAGAIN if splitting is required and return an error pointer.
337350 */
338- if (bio -> bi_opf & REQ_NOWAIT ) {
339- bio -> bi_status = BLK_STS_AGAIN ;
340- bio_endio (bio );
341- return ERR_PTR (- EAGAIN );
342- }
351+ if (bio -> bi_opf & REQ_NOWAIT )
352+ return - EAGAIN ;
343353
344354 * segs = nsegs ;
345355
@@ -356,58 +366,16 @@ struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
356366 * big IO can be trival, disable iopoll when split needed.
357367 */
358368 bio_clear_polled (bio );
359- return bio_split ( bio , bytes >> SECTOR_SHIFT , GFP_NOIO , bs ) ;
369+ return bytes >> SECTOR_SHIFT ;
360370}
361- EXPORT_SYMBOL_GPL (bio_split_rw );
371+ EXPORT_SYMBOL_GPL (bio_split_rw_at );
362372
363- /**
364- * __bio_split_to_limits - split a bio to fit the queue limits
365- * @bio: bio to be split
366- * @lim: queue limits to split based on
367- * @nr_segs: returns the number of segments in the returned bio
368- *
369- * Check if @bio needs splitting based on the queue limits, and if so split off
370- * a bio fitting the limits from the beginning of @bio and return it. @bio is
371- * shortened to the remainder and re-submitted.
372- *
373- * The split bio is allocated from @q->bio_split, which is provided by the
374- * block layer.
375- */
376- struct bio * __bio_split_to_limits (struct bio * bio ,
377- const struct queue_limits * lim ,
378- unsigned int * nr_segs )
373+ struct bio * bio_split_rw (struct bio * bio , const struct queue_limits * lim ,
374+ unsigned * nr_segs )
379375{
380- struct bio_set * bs = & bio -> bi_bdev -> bd_disk -> bio_split ;
381- struct bio * split ;
382-
383- switch (bio_op (bio )) {
384- case REQ_OP_DISCARD :
385- case REQ_OP_SECURE_ERASE :
386- split = bio_split_discard (bio , lim , nr_segs , bs );
387- break ;
388- case REQ_OP_WRITE_ZEROES :
389- split = bio_split_write_zeroes (bio , lim , nr_segs , bs );
390- break ;
391- default :
392- split = bio_split_rw (bio , lim , nr_segs , bs ,
393- get_max_io_size (bio , lim ) << SECTOR_SHIFT );
394- if (IS_ERR (split ))
395- return NULL ;
396- break ;
397- }
398-
399- if (split ) {
400- /* there isn't chance to merge the split bio */
401- split -> bi_opf |= REQ_NOMERGE ;
402-
403- blkcg_bio_issue_init (split );
404- bio_chain (split , bio );
405- trace_block_split (split , bio -> bi_iter .bi_sector );
406- WARN_ON_ONCE (bio_zone_write_plugging (bio ));
407- submit_bio_noacct (bio );
408- return split ;
409- }
410- return bio ;
376+ return bio_submit_split (bio ,
377+ bio_split_rw_at (bio , lim , nr_segs ,
378+ get_max_io_size (bio , lim ) << SECTOR_SHIFT ));
411379}
412380
413381/**
@@ -426,9 +394,7 @@ struct bio *bio_split_to_limits(struct bio *bio)
426394 const struct queue_limits * lim = & bdev_get_queue (bio -> bi_bdev )-> limits ;
427395 unsigned int nr_segs ;
428396
429- if (bio_may_exceed_limits (bio , lim ))
430- return __bio_split_to_limits (bio , lim , & nr_segs );
431- return bio ;
397+ return __bio_split_to_limits (bio , lim , & nr_segs );
432398}
433399EXPORT_SYMBOL (bio_split_to_limits );
434400
0 commit comments