block: Fix max nb_sectors in bdrv_make_zero
PATCH 1/1 block: Fix max nb_sectors in bdrv_make_zero
In bdrv_rw_co we report -EINVAL for nb_sectors > INT_MAX / BDRV_SECTOR_SIZE, so a caller shouldn't exceed it.
--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block.c +2 -2
@@ -2790,8 +2790,8 @@ int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)if (nb_sectors <=0 ) {return 0 ; } -if (nb_sectors >INT_MAX ) { - nb_sectors =INT_MAX ; +if (nb_sectors >INT_MAX /BDRV_SECTOR_SIZE ) { + nb_sectors =INT_MAX /BDRV_SECTOR_SIZE ; } ret =bdrv_get_block_status (bs, sector_num, nb_sectors, &n);if (ret <0 ) {
Signature · 1 line
1.9.3
Quoted 2 lines · Fam Zheng
In
bdrv_rw_cowe report-EINVALfornb_sectors>INT_MAX/BDRV_SECTOR_SIZE, so a caller shouldn't exceed it.
It's not obvious to me why we do that there. iovec member iov_len is size_t, not int.
Quoted 10 lines
Signed-off-by Fam Zheng <[email protected]>--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)block.c +0
-0
block.c int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) · line 2797
Quoted 10 lines
block.c +2
-2@@ -2790,8 +2790,8 @@ int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)if (nb_sectors <=0 ) {return 0 ; } -if (nb_sectors >INT_MAX ) { - nb_sectors =INT_MAX ; +if (nb_sectors >INT_MAX /BDRV_SECTOR_SIZE ) { + nb_sectors =INT_MAX /BDRV_SECTOR_SIZE ; } ret =bdrv_get_block_status (bs, sector_num, nb_sectors, &n);if (ret <0 ) {
Noticed while checkout out bdrv_get_block_status(): function comment of bdrv_co_get_block_status() claims it returns true/false. It doesn't.
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Fam Zheng
In
bdrv_rw_cowe report-EINVALfornb_sectors>INT_MAX/BDRV_SECTOR_SIZE, so a caller shouldn't exceed it.
I noticed this while testing unmap / zero write with scsi_debug:
# dd if=/dev/zero of=/tmp/a bs=1M count=32 # modprobe scsi_debug dev_size_mb=1024 lbpu=1 # qemu-img convert -t none /tmp/a /dev/sde qemu-img: error writing zeroes at sector 0: Invalid argument
Quoted 3 lines
It's not obvious to me why we do that there. iovec member
iov_lenissize_t, not int.
Deeper in the call stack we use int bytes everywhere:
static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
int64_t offset, unsigned int bytes, QEMUIOVector *qiov, BdrvRequestFlags flags);
static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
int64_t offset, unsigned int bytes, QEMUIOVector *qiov, BdrvRequestFlags flags);
static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
BdrvTrackedRequest *req, int64_t offset, unsigned int bytes, int64_t align, QEMUIOVector *qiov, int flags)
So this is a easier way to fix the specific bug :)
Quoted 16 lines
Quoted 16 lines
Signed-off-by Fam Zheng <[email protected]>--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)block.c +0
-2@@ -2790,8 +2790,8 @@ int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)if (nb_sectors <=0 ) {return 0 ; } -if (nb_sectors >INT_MAX ) { - nb_sectors =INT_MAX ;
Quoted 9 lines
block.c int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) · line 2797
Quoted 5 lines
block.c +2
-0+if (nb_sectors >INT_MAX /BDRV_SECTOR_SIZE ) { + nb_sectors =INT_MAX /BDRV_SECTOR_SIZE ; } ret =bdrv_get_block_status (bs, sector_num, nb_sectors, &n);if (ret <0 ) {Noticed while checkout out
bdrv_get_block_status(): function comment ofbdrv_co_get_block_status()claims it returns true/false. It doesn't.
Yeah, we should fix that.
Fam
Markus Armbruster
Reviewed-byQuoted 26 lines · Fam Zheng
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Fam Zheng
In
bdrv_rw_cowe report-EINVALfornb_sectors>INT_MAX/BDRV_SECTOR_SIZE, so a caller shouldn't exceed it.I noticed this while testing unmap / zero write with
scsi_debug:# dd if=/dev/zero of=/tmp/a bs=1M count=32 # modprobe scsi_debug dev_size_mb=1024 lbpu=1 # qemu-img convert -t none /tmp/a /dev/sde qemu-img: error writing zeroes at sector 0: Invalid argumentQuoted 3 lines
It's not obvious to me why we do that there. iovec member
iov_lenissize_t, not int.Deeper in the call stack we use int bytes everywhere:
static int
coroutine_fnbdrv_co_do_preadv(BlockDriverState *bs,
int64_toffset, unsigned int bytes, QEMUIOVector *qiov, BdrvRequestFlags flags);
static intcoroutine_fnbdrv_co_do_pwritev(BlockDriverState *bs,
int64_toffset, unsigned int bytes, QEMUIOVector *qiov, BdrvRequestFlags flags);
Quoted 5 lines
static int
coroutine_fnbdrv_aligned_preadv(BlockDriverState *bs,
BdrvTrackedRequest *req,int64_toffset, unsigned int bytes,int64_talign, QEMUIOVector *qiov, int flags)So this is a easier way to fix the specific bug :)
Okay, that's enough for my R-by.
Aside: using bytes makes sense, using int not so much.
[...]
Stefan Hajnoczi
AppliedQuoted 7 lines · Fam Zheng
In
bdrv_rw_cowe report-EINVALfornb_sectors>INT_MAX/BDRV_SECTOR_SIZE, so a caller shouldn't exceed it.Signed-off-by Fam Zheng <[email protected]>--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan