block: add a blk_alloc_discard_bio helper
Factor out a helper from __blkdev_issue_discard that chews off as much as possible from a discard range and allocates a bio for it. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20240506042027.2289826-5-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
81c2168c22
commit
e8b4869bc7
@ -35,31 +35,39 @@ static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
|
|||||||
return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
|
return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct bio *blk_alloc_discard_bio(struct block_device *bdev,
|
||||||
|
sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask)
|
||||||
|
{
|
||||||
|
sector_t bio_sects = min(*nr_sects, bio_discard_limit(bdev, *sector));
|
||||||
|
struct bio *bio;
|
||||||
|
|
||||||
|
if (!bio_sects)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
bio = bio_alloc(bdev, 0, REQ_OP_DISCARD, gfp_mask);
|
||||||
|
if (!bio)
|
||||||
|
return NULL;
|
||||||
|
bio->bi_iter.bi_sector = *sector;
|
||||||
|
bio->bi_iter.bi_size = bio_sects << SECTOR_SHIFT;
|
||||||
|
*sector += bio_sects;
|
||||||
|
*nr_sects -= bio_sects;
|
||||||
|
/*
|
||||||
|
* We can loop for a long time in here if someone does full device
|
||||||
|
* discards (like mkfs). Be nice and allow us to schedule out to avoid
|
||||||
|
* softlocking if preempt is disabled.
|
||||||
|
*/
|
||||||
|
cond_resched();
|
||||||
|
return bio;
|
||||||
|
}
|
||||||
|
|
||||||
int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
|
int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
|
||||||
sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
|
sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
|
||||||
{
|
{
|
||||||
struct bio *bio = *biop;
|
struct bio *bio;
|
||||||
|
|
||||||
while (nr_sects) {
|
while ((bio = blk_alloc_discard_bio(bdev, §or, &nr_sects,
|
||||||
sector_t req_sects =
|
gfp_mask)))
|
||||||
min(nr_sects, bio_discard_limit(bdev, sector));
|
*biop = bio_chain_and_submit(*biop, bio);
|
||||||
|
|
||||||
bio = blk_next_bio(bio, bdev, 0, REQ_OP_DISCARD, gfp_mask);
|
|
||||||
bio->bi_iter.bi_sector = sector;
|
|
||||||
bio->bi_iter.bi_size = req_sects << 9;
|
|
||||||
sector += req_sects;
|
|
||||||
nr_sects -= req_sects;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We can loop for a long time in here, if someone does
|
|
||||||
* full device discards (like mkfs). Be nice and allow
|
|
||||||
* us to schedule out to avoid softlocking if preempt
|
|
||||||
* is disabled.
|
|
||||||
*/
|
|
||||||
cond_resched();
|
|
||||||
}
|
|
||||||
|
|
||||||
*biop = bio;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(__blkdev_issue_discard);
|
EXPORT_SYMBOL(__blkdev_issue_discard);
|
||||||
|
@ -833,4 +833,7 @@ struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
|
|||||||
unsigned int nr_pages, blk_opf_t opf, gfp_t gfp);
|
unsigned int nr_pages, blk_opf_t opf, gfp_t gfp);
|
||||||
struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new);
|
struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new);
|
||||||
|
|
||||||
|
struct bio *blk_alloc_discard_bio(struct block_device *bdev,
|
||||||
|
sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask);
|
||||||
|
|
||||||
#endif /* __LINUX_BIO_H */
|
#endif /* __LINUX_BIO_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user