scsi-generic: Fix emulated block limits VPD page
PATCH 1/1 scsi-generic: Fix emulated block limits VPD page
Commits 01ef8185b80 amd 24b36e9813e updated the way that the maximum transfer length is calculated for patching block limits VPD page in an INQUIRY response.
The same updates also need to be made for the case where the host device does not support the block limits VPD page at all and we emulate the whole page.
Without this fix, on host block devices a maximum transfer length of (INT_MAX - sector_size) bytes is advertised to the guest, resulting in I/O errors when a request that exceeds the host limits is made by the guest. (Prior to commit 24b36e9813e, this code path would use the max_transfer value from the host instead of INT_MAX, but still miss the fix from 01ef8185b80 where max_transfer is also capped to max_iov host pages, so it would be less wrong, but still wrong.)
--- hw/scsi/scsi-generic.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
hw/scsi/scsi-generic.c +14 -7
@@ -147,6 +147,18 @@ static int execute_command(BlockBackend *blk,return 0 ; } +static uint64_t calculate_max_transfer (SCSIDevice *s) +{ +uint64_t max_transfer =blk_get_max_hw_transfer (s->conf.blk); +uint32_t max_iov =blk_get_max_hw_iov (s->conf.blk); + +assert (max_transfer); + max_transfer =MIN_NON_ZERO (max_transfer, + max_iov *qemu_real_host_page_size ()); + +return max_transfer / s->blocksize; +} +static int scsi_handle_inquiry_reply (SCSIGenericReq *r,SCSIDevice *s,int len) {uint8_t page, page_idx;
@@ -179,12 +191,7 @@ static int scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s, int len) (r->req.cmd.buf[1] & 0x01)) { page = r->req.cmd.buf[2 ];if (page ==0xb0 ) { -uint64_t max_transfer =blk_get_max_hw_transfer (s->conf.blk); -uint32_t max_iov =blk_get_max_hw_iov (s->conf.blk); - -assert (max_transfer); - max_transfer =MIN_NON_ZERO (max_transfer, max_iov *qemu_real_host_page_size ()) - / s->blocksize; +uint64_t max_transfer =calculate_max_transfer (s);stl_be_p (&r->buf[8 ], max_transfer);/* Also take care of the opt xfer len. */ stl_be_p(&r->buf[12],
@@ -230,7 +237,7 @@ static int scsi_generic_emulate_block_limits(SCSIGenericReq *r, SCSIDevice *s)uint8_t buf[64 ];SCSIBlockLimits bl = { - .max_io_sectors =blk_get_max_transfer (s->conf.blk) / s->blocksize + .max_io_sectors =calculate_max_transfer (s), };memset (r->buf,0 , r->buflen);
Signature · 1 line
2.37.1
Quoted 17 lines · Kevin Wolf
Commits
01ef8185b80amd24b36e9813eupdated the way that the maximum transfer length is calculated for patching block limits VPD page in an INQUIRY response.The same updates also need to be made for the case where the host device does not support the block limits VPD page at all and we emulate the whole page.
Without this fix, on host block devices a maximum transfer length of (
INT_MAX-sector_size) bytes is advertised to the guest, resulting in I/O errors when a request that exceeds the host limits is made by the guest. (Prior to commit24b36e9813e, this code path would use themax_transfervalue from the host instead ofINT_MAX, but still miss the fix from01ef8185b80wheremax_transferis also capped tomax_iovhost pages, so it would be less wrong, but still wrong.)
Quoted 8 lines
Fixes 01ef8185b809af9d287e1a03a3f9d8ea8231118aFixes 24b36e9813ec15da7db62e3b3621730710c5f020Signed-off-by Kevin Wolf <[email protected]>--- hw/scsi/scsi-generic.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)