block: require write_same and discard requests align to logical block size
PATCH 3/102 block: require write_same and discard requests align to logical block size
From: "Darrick J. Wong" <[email protected]>
Subject: block: require write_same and discard requests align to logical block size
Make sure that the offset and length arguments that we're using to construct WRITE SAME and DISCARD requests are actually aligned to the logical block size. Failure to do this causes other errors in other parts of the block layer or the SCSI layer because disks don't support partial logical block writes.
Signed-off-by Darrick J. Wong <[email protected]>
Reviewed-by Christoph Hellwig <[email protected]>
Reviewed-by Bart Van Assche <[email protected]>
Reviewed-by Martin K. Petersen <[email protected]>
Reviewed-by Hannes Reinecke <[email protected]>
Cc Theodore Ts'o <[email protected]>
Cc Mike Snitzer <[email protected]> # tweaked header
Cc Brian Foster <[email protected]>
Cc Jens Axboe <[email protected]>
Signed-off-by Andrew Morton <[email protected]>
---
block/blk-lib.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
block/blk-lib.c renamed from block/blk-lib.c~block-require-write_same-and-discard-requests-align-to-logical-block-size +15 -0
@@ -31,6 +31,7 @@ int __blkdev_issue_discard(struct block_unsigned int granularity;enum req_op op;int alignment; +sector_t bs_mask;if (!q)return -ENXIO ;
@@ -50,6 +51,10 @@ int __blkdev_issue_discard(struct block_ op =REQ_OP_DISCARD ; } + bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; +if ((sector | nr_sects) & bs_mask) +return -EINVAL ; +/* Zero-sector (unknown) and one-sector granularities are the same. */ granularity =max (q->limits.discard_granularity >>9 ,1U ); alignment = (bdev_discard_alignment (bdev) >>9 ) % granularity;
@@ -150,10 +155,15 @@ int blkdev_issue_write_same(struct blockunsigned int max_write_same_sectors;struct bio *bio =NULL ;int ret =0 ; +sector_t bs_mask;if (!q)return -ENXIO ; + bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; +if ((sector | nr_sects) & bs_mask) +return -EINVAL ; +/* Ensure that max_write_same_sectors doesn't overflow bi_size */ max_write_same_sectors =UINT_MAX >>9 ;
@@ -202,6 +212,11 @@ static int __blkdev_issue_zeroout(structint ret;struct bio *bio =NULL ;unsigned int sz; +sector_t bs_mask; + + bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; +if ((sector | nr_sects) & bs_mask) +return -EINVAL ;while (nr_sects !=0 ) { bio = next_bio(bio, min(nr_sects, (sector_t)BIO_MAX_PAGES),
_