@@ -105,9 +105,33 @@ static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
105
105
return round_down (UINT_MAX , lim -> logical_block_size ) >> SECTOR_SHIFT ;
106
106
}
107
107
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 )
111
135
{
112
136
unsigned int max_discard_sectors , granularity ;
113
137
sector_t tmp ;
@@ -121,10 +145,10 @@ static struct bio *bio_split_discard(struct bio *bio,
121
145
min (lim -> max_discard_sectors , bio_allowed_max_sectors (lim ));
122
146
max_discard_sectors -= max_discard_sectors % granularity ;
123
147
if (unlikely (!max_discard_sectors ))
124
- return NULL ;
148
+ return bio ;
125
149
126
150
if (bio_sectors (bio ) <= max_discard_sectors )
127
- return NULL ;
151
+ return bio ;
128
152
129
153
split_sectors = max_discard_sectors ;
130
154
@@ -139,19 +163,18 @@ static struct bio *bio_split_discard(struct bio *bio,
139
163
if (split_sectors > tmp )
140
164
split_sectors -= tmp ;
141
165
142
- return bio_split (bio , split_sectors , GFP_NOIO , bs );
166
+ return bio_submit_split (bio , split_sectors );
143
167
}
144
168
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 )
148
171
{
149
172
* nsegs = 0 ;
150
173
if (!lim -> max_write_zeroes_sectors )
151
- return NULL ;
174
+ return bio ;
152
175
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 );
155
178
}
156
179
157
180
static 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,
274
297
}
275
298
276
299
/**
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
278
301
* @bio: [in] bio to be split
279
302
* @lim: [in] queue limits to split based on
280
303
* @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
282
304
* @max_bytes: [in] maximum number of bytes per bio
283
305
*
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.
295
310
*/
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 )
298
313
{
299
314
struct bio_vec bv , bvprv , * bvprvp = NULL ;
300
315
struct bvec_iter iter ;
@@ -324,22 +339,17 @@ struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
324
339
}
325
340
326
341
* segs = nsegs ;
327
- return NULL ;
342
+ return 0 ;
328
343
split :
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
+
334
347
/*
335
348
* We can't sanely support splitting for a REQ_NOWAIT bio. End it
336
349
* with EAGAIN if splitting is required and return an error pointer.
337
350
*/
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 ;
343
353
344
354
* segs = nsegs ;
345
355
@@ -356,58 +366,16 @@ struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
356
366
* big IO can be trival, disable iopoll when split needed.
357
367
*/
358
368
bio_clear_polled (bio );
359
- return bio_split ( bio , bytes >> SECTOR_SHIFT , GFP_NOIO , bs ) ;
369
+ return bytes >> SECTOR_SHIFT ;
360
370
}
361
- EXPORT_SYMBOL_GPL (bio_split_rw );
371
+ EXPORT_SYMBOL_GPL (bio_split_rw_at );
362
372
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 )
379
375
{
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 ));
411
379
}
412
380
413
381
/**
@@ -426,9 +394,7 @@ struct bio *bio_split_to_limits(struct bio *bio)
426
394
const struct queue_limits * lim = & bdev_get_queue (bio -> bi_bdev )-> limits ;
427
395
unsigned int nr_segs ;
428
396
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 );
432
398
}
433
399
EXPORT_SYMBOL (bio_split_to_limits );
434
400
0 commit comments