diff mbox series

[3/8] scsi: sd_zbc: Verify that the zone size is a power of two

Message ID 20220415201752.2793700-4-bvanassche@acm.org
State New
Headers show
Series Support zoned devices with gap zones | expand

Commit Message

Bart Van Assche April 15, 2022, 8:17 p.m. UTC
The following check in sd_zbc_cmnd_checks() can only work correctly if
the zone size is a power of two:

	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
		/* Unaligned request */
		return BLK_STS_IOERR;

Hence this patch that verifies that the zone size is a power of two.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/sd_zbc.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Damien Le Moal April 17, 2022, 11:09 p.m. UTC | #1
On 4/16/22 05:17, Bart Van Assche wrote:
> The following check in sd_zbc_cmnd_checks() can only work correctly if
> the zone size is a power of two:
> 
> 	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
> 		/* Unaligned request */
> 		return BLK_STS_IOERR;
> 
> Hence this patch that verifies that the zone size is a power of two.
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>

Note that this is already checked in blk_revalidate_disk_zones(), but it
does not hurt to add the check.

Reviewed-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>

> ---
>  drivers/scsi/sd_zbc.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c
> index d0275855b16c..c1f295859b27 100644
> --- a/drivers/scsi/sd_zbc.c
> +++ b/drivers/scsi/sd_zbc.c
> @@ -666,6 +666,13 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
>  
>  	*zblocks = zone_blocks;
>  
> +	if (!is_power_of_2(*zblocks)) {
> +		sd_printk(KERN_ERR, sdkp,
> +			  "Zone size %llu is not a power of two.\n",
> +			  zone_blocks);
> +		return -EINVAL;
> +	}
> +
>  	return 0;
>  }
>
Bart Van Assche April 18, 2022, 4:54 p.m. UTC | #2
On 4/17/22 16:09, Damien Le Moal wrote:
> On 4/16/22 05:17, Bart Van Assche wrote:
>> The following check in sd_zbc_cmnd_checks() can only work correctly if
>> the zone size is a power of two:
>>
>> 	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
>> 		/* Unaligned request */
>> 		return BLK_STS_IOERR;
>>
>> Hence this patch that verifies that the zone size is a power of two.
> 
> Note that this is already checked in blk_revalidate_disk_zones(), but it
> does not hurt to add the check.

If the block layer would be modified such that support is added for zones with
a size that is not a power of two I think we will really need this check.

Thanks,

Bart.
diff mbox series

Patch

diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c
index d0275855b16c..c1f295859b27 100644
--- a/drivers/scsi/sd_zbc.c
+++ b/drivers/scsi/sd_zbc.c
@@ -666,6 +666,13 @@  static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
 
 	*zblocks = zone_blocks;
 
+	if (!is_power_of_2(*zblocks)) {
+		sd_printk(KERN_ERR, sdkp,
+			  "Zone size %llu is not a power of two.\n",
+			  zone_blocks);
+		return -EINVAL;
+	}
+
 	return 0;
 }