Skip to content

Commit

Permalink
block: factor out a blk_write_zeroes_limit helper
Browse files Browse the repository at this point in the history
Contrary to the comment in __blkdev_issue_write_zeroes, nothing here
checks for a potential bi_size overflow.  Add a helper mirroring
the secure erase code for the check.

Signed-off-by: Christoph Hellwig <[email protected]>
Reviewed-by: Martin K. Petersen <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Christoph Hellwig authored and axboe committed Jul 5, 2024
1 parent 2f20872 commit 73a768d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions block/blk-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,28 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
}
EXPORT_SYMBOL(blkdev_issue_discard);

static sector_t bio_write_zeroes_limit(struct block_device *bdev)
{
sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;

return min(bdev_write_zeroes_sectors(bdev),
(UINT_MAX >> SECTOR_SHIFT) & ~bs_mask);
}

static int __blkdev_issue_write_zeroes(struct block_device *bdev,
sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
struct bio **biop, unsigned flags)
{
struct bio *bio = *biop;
unsigned int max_sectors;

if (bdev_read_only(bdev))
return -EPERM;

/* Ensure that max_sectors doesn't overflow bi_size */
max_sectors = bdev_write_zeroes_sectors(bdev);

if (max_sectors == 0)
if (!bdev_write_zeroes_sectors(bdev))
return -EOPNOTSUPP;

while (nr_sects) {
unsigned int len = min_t(sector_t, nr_sects, max_sectors);
unsigned int len = min_t(sector_t, nr_sects,
bio_write_zeroes_limit(bdev));

bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
bio->bi_iter.bi_sector = sector;
Expand Down

0 comments on commit 73a768d

Please sign in to comment.