decouple VIRTIO_BLK_F_SCSI from SG_IO support
cover decouple VIRTIO_BLK_F_SCSI from SG_IO support
Previous versions of these patches have been posted already, but they were lost. Sorry for realizing this quite late.
VIRTIO_BLK_F_SCSI is supposed to mean whether the host can *parse* SCSI requests, not *execute* them. So it should always be enabled, and the scsi=on/off property tied to a separate configuration variable that is not guest visible.
With this change, Linux has problems understanding failed requests, so patch 1 works around the Linux bugs.
Important: because we need to do this to fix a migration compatibility problem when QEMU might be invoked with an old machine type, we must do this unconditionally. This more or less assumes that no one ever invoked QEMU with scsi=off, as it breaks migration from new QEMU, scsi=off to old QEMU, also scsi=off. However new->old is not supported upstream.
S390 compile-tested only.
Paolo Bonzini (4):
virtio-blk: report non-zero status when failing SG_IO requests
virtio-blk: blockdev_mark_auto_del is transport-independent
virtio-blk: define VirtIOBlkConf
virtio-blk: always enable VIRTIO_BLK_F_SCSI
hw/s390-virtio-bus.c | 10 ++++--- hw/s390-virtio-bus.h | 4 +-- hw/virtio-blk.c | 80 ++++++++++++++++++++++++-------------------------- hw/virtio-blk.h | 14 +++++---- hw/virtio-pci.c | 11 +++---- hw/virtio-pci.h | 4 +-- hw/virtio.h | 4 +-- 7 files changed, 64 insertions(+), 63 deletions(-)
Signature · 1 line
1.7.10.1
PATCH 1/4 virtio-blk: report non-zero status when failing SG_IO requests
Linux really looks only at scsi->errors for SG_IO requests; it does not look at the virtio request status at all. Because of this, when a SG_IO request is failed early with virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP), without writing hdr.status, it will look like a success to the guest.
This is their bug, but we can make it safe for older guests now by forcing scsi->errors to have a non-zero value whenever a request has to be failed.
But if we fix the bug in the guest driver, we will have another problem because QEMU returns VIRTIO_BLK_S_IOERR if the status is non-zero, and Linux translates that to -EIO. Rather, the guest should succeed the request and pass the non-zero status via the userspace-provided SG_IO structure. So, remove the case where virtio_blk_handle_scsi can return VIRTIO_BLK_S_IOERR.
--- hw/virtio-blk.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-)
hw/virtio-blk.c +23 -28
@@ -145,20 +145,12 @@ static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)return req; } -#ifdef __linux__static void virtio_blk_handle_scsi (VirtIOBlockReq *req) { -struct sg_io_hdr hdr;int ret; -int status; +int status =VIRTIO_BLK_S_OK ;int i; -if ((req->dev->vdev.guest_features & (1 <<VIRTIO_BLK_F_SCSI )) ==0 ) { -virtio_blk_req_complete (req,VIRTIO_BLK_S_UNSUPP ); -g_free (req); -return ; - } -/* * We require at least one output segment each for the virtio_blk_outhdr * and the SCSI command block.
@@ -173,20 +165,26 @@ static void virtio_blk_handle_scsi(VirtIOBlockReq *req) }/* -* No support for bidirection commands yet. +* The scsi inhdr is placed in the second-to-last input segment, just +* before the regular inhdr. */ -if (req->elem.out_num >2 && req->elem.in_num >3 ) { -virtio_blk_req_complete (req,VIRTIO_BLK_S_UNSUPP ); -g_free (req); -return ; + req->scsi = (void *)req->elem.in_sg[req->elem.in_num -2 ].iov_base; + +if ((req->dev->vdev.guest_features & (1 <<VIRTIO_BLK_F_SCSI )) ==0 ) { + status =VIRTIO_BLK_S_UNSUPP ; + goto fail; }/* -* The scsi inhdr is placed in the second-to-last input segment, just -* before the regular inhdr. +* No support for bidirection commands yet. */ - req->scsi = (void *)req->elem.in_sg[req->elem.in_num -2 ].iov_base; +if (req->elem.out_num >2 && req->elem.in_num >3 ) { + status =VIRTIO_BLK_S_UNSUPP ; + goto fail; + } +#ifdef __linux__ +struct sg_io_hdr hdr;memset (&hdr,0 ,sizeof (struct sg_io_hdr )); hdr.interface_id ='S' ; hdr.cmd_len = req->elem.out_sg[1 ].iov_len;
@@ -230,12 +228,7 @@ static void virtio_blk_handle_scsi(VirtIOBlockReq *req) ret =bdrv_ioctl (req->dev->bs,SG_IO , &hdr);if (ret) { status =VIRTIO_BLK_S_UNSUPP ; - hdr.status = ret; - hdr.resid = hdr.dxfer_len; - }else if (hdr.status) { - status =VIRTIO_BLK_S_IOERR ; - }else { - status =VIRTIO_BLK_S_OK ; + goto fail; }/*
@@ -258,14 +251,16 @@ static void virtio_blk_handle_scsi(VirtIOBlockReq *req)virtio_blk_req_complete (req, status);g_free (req); -}#else -static void virtio_blk_handle_scsi (VirtIOBlockReq *req) -{ -virtio_blk_req_complete (req,VIRTIO_BLK_S_UNSUPP ); +abort (); +#endif + +fail: +/* Just put anything nonzero so that the ioctl fails in the guest. */ +stl_p (&req->scsi->errors,255 ); +virtio_blk_req_complete (req, status);g_free (req); } -#endif /* __linux__ */ typedef struct MultiReqBuffer { BlockRequest blkreq[32];
Signature · 1 line
1.7.10.1
PATCH 2/4 virtio-blk: blockdev_mark_auto_del is transport-independent
In reply to Paolo BonziniMove it from virtio_blk_exit_pci to virtio_blk_exit.
This is included here because the next patch removes proxy->block.
--- hw/virtio-blk.c | 1 + hw/virtio-pci.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)
hw/virtio-blk.c +1 -0
@@ -626,5 +626,6 @@ void virtio_blk_exit(VirtIODevice *vdev) {VirtIOBlock *s =to_virtio_blk (vdev);unregister_savevm (s->qdev,"virtio-blk" , s); +blockdev_mark_auto_del (s->bs);virtio_cleanup (vdev); }
hw/virtio-pci.c +0 -1
@@ -726,7 +726,6 @@ static int virtio_blk_exit_pci(PCIDevice *pci_dev)virtio_pci_stop_ioeventfd (proxy);virtio_blk_exit (proxy->vdev); -blockdev_mark_auto_del (proxy->block.bs);return virtio_exit_pci (pci_dev); }
Signature · 1 line
1.7.10.1
PATCH 3/4 virtio-blk: define VirtIOBlkConf
In reply to Paolo BonziniWe will have to add another field to the virtio-blk configuration in the next patch. Avoid a proliferation of arguments to virtio_blk_init.
--- hw/s390-virtio-bus.c | 7 +++---- hw/s390-virtio-bus.h | 4 ++-- hw/virtio-blk.c | 27 +++++++++++++-------------- hw/virtio-blk.h | 7 +++++++ hw/virtio-pci.c | 7 +++---- hw/virtio-pci.h | 4 ++-- hw/virtio.h | 4 ++-- 7 files changed, 32 insertions(+), 28 deletions(-)
hw/s390-virtio-bus.c +3 -4
@@ -163,8 +163,7 @@ static int s390_virtio_blk_init(VirtIOS390Device *dev) {VirtIODevice *vdev; - vdev =virtio_blk_init ((DeviceState *)dev, &dev->block, - &dev->block_serial); + vdev =virtio_blk_init ((DeviceState *)dev, &dev->blk);if (!vdev) {return -1 ; }
@@ -400,8 +399,8 @@ static TypeInfo s390_virtio_net = { };static Property s390_virtio_blk_properties[] = { -DEFINE_BLOCK_PROPERTIES (VirtIOS390Device, block), -DEFINE_PROP_STRING ("serial" , VirtIOS390Device, block_serial), +DEFINE_BLOCK_PROPERTIES (VirtIOS390Device, blk.conf), +DEFINE_PROP_STRING ("serial" , VirtIOS390Device, blk.serial),DEFINE_PROP_END_OF_LIST (), };
hw/s390-virtio-bus.h +2 -2
@@ -17,6 +17,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/>. */ +#include "virtio-blk.h" #include "virtio-net.h" #include "virtio-serial.h" #include "virtio-scsi.h"
@@ -64,8 +65,7 @@ struct VirtIOS390Device {ram_addr_t feat_offs;uint8_t feat_len;VirtIODevice *vdev; -BlockConf block; -char *block_serial; +VirtIOBlkConf blk;NICConf nic;uint32_t host_features;virtio_serial_conf serial;
hw/virtio-blk.c +13 -14
@@ -29,7 +29,7 @@ typedef struct VirtIOBlockvoid *rq;QEMUBH *bh;BlockConf *conf; -char *serial; +VirtIOBlkConf *blk;unsigned short sector_mask;DeviceState *qdev; } VirtIOBlock;
@@ -389,7 +389,7 @@ static void virtio_blk_handle_request(VirtIOBlockReq *req,* terminated by '\0' only when shorter than buffer. */ strncpy (req->elem.in_sg[0 ].iov_base, - s->serial ? s->serial :"" , + s->blk->serial ? s->blk->serial :"" ,MIN (req->elem.in_sg[0 ].iov_len,VIRTIO_BLK_ID_BYTES ));virtio_blk_req_complete (req,VIRTIO_BLK_S_OK );g_free (req);
@@ -568,28 +568,27 @@ static const BlockDevOps virtio_block_ops = { .resize_cb = virtio_blk_resize, }; -VirtIODevice *virtio_blk_init (DeviceState *dev,BlockConf *conf, -char **serial) +VirtIODevice *virtio_blk_init (DeviceState *dev,VirtIOBlkConf *blk) {VirtIOBlock *s;int cylinders, heads, secs;static int virtio_blk_id;DriveInfo *dinfo; -if (!conf->bs) { +if (!blk->conf.bs) {error_report ("drive property not set" );return NULL ; } -if (!bdrv_is_inserted (conf->bs)) { +if (!bdrv_is_inserted (blk->conf.bs)) {error_report ("Device needs media, but drive is empty" );return NULL ; } -if (!*serial) { +if (!blk->serial) {/* try to fall back to value set with legacy -drive serial=... */ - dinfo =drive_get_by_blockdev (conf->bs); + dinfo =drive_get_by_blockdev (blk->conf.bs);if (*dinfo->serial) { - *serial =strdup (dinfo->serial); + blk->serial =strdup (dinfo->serial); } }
@@ -600,9 +599,9 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf, s->vdev.get_config = virtio_blk_update_config; s->vdev.get_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; - s->bs = conf->bs; - s->conf = conf; - s->serial = *serial; + s->bs = blk->conf.bs; + s->conf = &blk->conf; + s->blk = blk; s->rq =NULL ; s->sector_mask = (s->conf->logical_block_size /BDRV_SECTOR_SIZE ) -1 ;bdrv_guess_geometry (s->bs, &cylinders, &heads, &secs);
@@ -614,10 +613,10 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,register_savevm (dev,"virtio-blk" , virtio_blk_id++,2 , virtio_blk_save, virtio_blk_load, s);bdrv_set_dev_ops (s->bs, &virtio_block_ops, s); -bdrv_set_buffer_alignment (s->bs, conf->logical_block_size); +bdrv_set_buffer_alignment (s->bs, s->conf->logical_block_size);bdrv_iostatus_enable (s->bs); -add_boot_device_path (conf->bootindex, dev,"/disk@0,0" ); +add_boot_device_path (s->conf->bootindex, dev,"/disk@0,0" );return &s->vdev; }
hw/virtio-blk.h +7 -0
@@ -97,6 +97,12 @@ struct virtio_scsi_inhdruint32_t residual; }; +struct VirtIOBlkConf +{ +BlockConf conf; +char *serial; +}; +#ifdef __linux__#define DEFINE_VIRTIO_BLK_FEATURES (_state, _field) \ DEFINE_VIRTIO_COMMON_FEATURES(_state, _field), \
@@ -105,4 +111,5 @@ struct virtio_scsi_inhdr#define DEFINE_VIRTIO_BLK_FEATURES (_state, _field) \ DEFINE_VIRTIO_COMMON_FEATURES(_state, _field)#endif +#endif
hw/virtio-pci.c +3 -4
@@ -697,8 +697,7 @@ static int virtio_blk_init_pci(PCIDevice *pci_dev) proxy->class_code != PCI_CLASS_STORAGE_OTHER) proxy->class_code =PCI_CLASS_STORAGE_SCSI ; - vdev =virtio_blk_init (&pci_dev->qdev, &proxy->block, - &proxy->block_serial); + vdev =virtio_blk_init (&pci_dev->qdev, &proxy->blk);if (!vdev) {return -1 ; }
@@ -813,8 +812,8 @@ static int virtio_balloon_exit_pci(PCIDevice *pci_dev)static Property virtio_blk_properties[] = {DEFINE_PROP_HEX32 ("class" , VirtIOPCIProxy, class_code,0 ), -DEFINE_BLOCK_PROPERTIES (VirtIOPCIProxy, block), -DEFINE_PROP_STRING ("serial" , VirtIOPCIProxy, block_serial), +DEFINE_BLOCK_PROPERTIES (VirtIOPCIProxy, blk.conf), +DEFINE_PROP_STRING ("serial" , VirtIOPCIProxy, blk.serial),DEFINE_PROP_BIT ("ioeventfd" , VirtIOPCIProxy, flags,VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT , true),DEFINE_PROP_UINT32 ("vectors" , VirtIOPCIProxy, nvectors,2 ),DEFINE_VIRTIO_BLK_FEATURES (VirtIOPCIProxy, host_features),
hw/virtio-pci.h +2 -2
@@ -15,6 +15,7 @@#ifndef QEMU_VIRTIO_PCI_H #define QEMU_VIRTIO_PCI_H +#include "virtio-blk.h" #include "virtio-net.h" #include "virtio-serial.h" #include "virtio-scsi.h"
@@ -32,8 +33,7 @@ typedef struct {uint32_t flags;uint32_t class_code;uint32_t nvectors; -BlockConf block; -char *block_serial; +VirtIOBlkConf blk;NICConf nic;uint32_t host_features;#ifdef CONFIG_LINUX
hw/virtio.h +2 -2
@@ -191,8 +191,8 @@ void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding, void *opaque);/* Base devices. */ -VirtIODevice *virtio_blk_init (DeviceState *dev,BlockConf *conf, -char **serial); +typedef struct VirtIOBlkConf VirtIOBlkConf ; +VirtIODevice *virtio_blk_init (DeviceState *dev,VirtIOBlkConf *blk);struct virtio_net_conf ;VirtIODevice *virtio_net_init (DeviceState *dev,NICConf *conf,struct virtio_net_conf *net);
Signature · 1 line
1.7.10.1
PATCH 4/4 virtio-blk: always enable VIRTIO_BLK_F_SCSI
In reply to Paolo BonziniVIRTIO_BLK_F_SCSI is supposed to mean whether the host can *parse* SCSI requests, not *execute* them. You could run QEMU with scsi=on and a file-backed disk, and QEMU would fail all SCSI requests even though it advertises VIRTIO_BLK_F_SCSI.
Because we need to do this to fix a migration compatibility problem related to how QEMU is invoked by management, we must do this unconditionally even on older machine types. This more or less assumes that no one ever invoked QEMU with scsi=off.
Here is how testing goes:
- old QEMU, scsi=on -> new QEMU, scsi=on
- new QEMU, scsi=on -> old QEMU, scsi=on
- old QEMU, scsi=off -> new QEMU, scsi=on
- new QEMU, scsi=off -> old QEMU, scsi=on
ok (new QEMU has VIRTIO_BLK_F_SCSI, adding host features is fine)
- old QEMU, scsi=off -> new QEMU, scsi=off
ok (new QEMU has VIRTIO_BLK_F_SCSI, adding host features is fine)
- old QEMU, scsi=on -> new QEMU, scsi=off
ok, bug fixed
- new QEMU, scsi=on -> old QEMU, scsi=off
doesn't work (same as: old QEMU, scsi=on -> old QEMU, scsi=off)
- new QEMU, scsi=off -> old QEMU, scsi=off
broken by the patch
--- hw/s390-virtio-bus.c | 3 +++ hw/virtio-blk.c | 3 ++- hw/virtio-blk.h | 7 +------ hw/virtio-pci.c | 3 +++ 4 files changed, 9 insertions(+), 7 deletions(-)
hw/s390-virtio-bus.c +3 -0
@@ -401,6 +401,9 @@ static TypeInfo s390_virtio_net = {static Property s390_virtio_blk_properties[] = {DEFINE_BLOCK_PROPERTIES (VirtIOS390Device, blk.conf),DEFINE_PROP_STRING ("serial" , VirtIOS390Device, blk.serial), +#ifdef __linux__ +DEFINE_PROP_BIT ("scsi" , VirtIOS390Device, blk.scsi,0 , true), +#endifDEFINE_PROP_END_OF_LIST (), };
hw/virtio-blk.c +2 -1
@@ -170,7 +170,7 @@ static void virtio_blk_handle_scsi(VirtIOBlockReq *req)*/ req->scsi = (void *)req->elem.in_sg[req->elem.in_num -2 ].iov_base; -if ((req->dev->vdev.guest_features & (1 <<VIRTIO_BLK_F_SCSI )) ==0 ) { +if (!req->dev->blk->scsi) { status =VIRTIO_BLK_S_UNSUPP ; goto fail; }
@@ -504,6 +504,7 @@ static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features) features |= (1 <<VIRTIO_BLK_F_GEOMETRY ); features |= (1 <<VIRTIO_BLK_F_TOPOLOGY ); features |= (1 <<VIRTIO_BLK_F_BLK_SIZE ); + features |= (1 <<VIRTIO_BLK_F_SCSI );if (bdrv_enable_write_cache (s->bs)) features |= (1 <<VIRTIO_BLK_F_WCACHE );
hw/virtio-blk.h +1 -6
@@ -101,15 +101,10 @@ struct VirtIOBlkConf {BlockConf conf;char *serial; +uint32_t scsi; }; -#ifdef __linux__ -#define DEFINE_VIRTIO_BLK_FEATURES (_state, _field) \ - DEFINE_VIRTIO_COMMON_FEATURES(_state, _field), \ - DEFINE_PROP_BIT("scsi", _state, _field, VIRTIO_BLK_F_SCSI, true) -#else #define DEFINE_VIRTIO_BLK_FEATURES (_state, _field) \ DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) -#endif #endif
hw/virtio-pci.c +3 -0
@@ -814,6 +814,9 @@ static Property virtio_blk_properties[] = {DEFINE_PROP_HEX32 ("class" , VirtIOPCIProxy, class_code,0 ),DEFINE_BLOCK_PROPERTIES (VirtIOPCIProxy, blk.conf),DEFINE_PROP_STRING ("serial" , VirtIOPCIProxy, blk.serial), +#ifdef __linux__ +DEFINE_PROP_BIT ("scsi" , VirtIOPCIProxy, blk.scsi,0 , true), +#endifDEFINE_PROP_BIT ("ioeventfd" , VirtIOPCIProxy, flags,VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT , true),DEFINE_PROP_UINT32 ("vectors" , VirtIOPCIProxy, nvectors,2 ), DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
Signature · 1 line
1.7.10.1
Il 16/05/2012 12:54, Paolo Bonzini ha scritto:
Quoted 25 lines
Previous versions of these patches have been posted already, but they were lost. Sorry for realizing this quite late.
VIRTIO_BLK_F_SCSIis supposed to mean whether the host can *parse* SCSI requests, not *execute* them. So it should always be enabled, and the scsi=on/off property tied to a separate configuration variable that is not guest visible.With this change, Linux has problems understanding failed requests, so patch 1 works around the Linux bugs.
Important: because we need to do this to fix a migration compatibility problem when QEMU might be invoked with an old machine type, we must do this unconditionally. This more or less assumes that no one ever invoked QEMU with scsi=off, as it breaks migration from new QEMU, scsi=off to old QEMU, also scsi=off. However
new->oldis not supported upstream.S390 compile-tested only.
Paolo Bonzini (4):
virtio-blk: report non-zero status when failingSG_IOrequests
virtio-blk:blockdev_mark_auto_delis transport-independent
virtio-blk: define VirtIOBlkConf
virtio-blk: always enableVIRTIO_BLK_F_SCSI
Quoted 9 lines
hw/s390-virtio-bus.c | 10 ++++--- hw/s390-virtio-bus.h | 4 +-- hw/virtio-blk.c | 80 ++++++++++++++++++++++++-------------------------- hw/virtio-blk.h | 14 +++++---- hw/virtio-pci.c | 11 +++---- hw/virtio-pci.h | 4 +-- hw/virtio.h | 4 +-- 7 files changed, 64 insertions(+), 63 deletions(-)
Ping?
Quoted 36 lines · Paolo Bonzini
Il 16/05/2012 12:54, Paolo Bonzini ha scritto:
Quoted 25 lines
Previous versions of these patches have been posted already, but they were lost. Sorry for realizing this quite late.
VIRTIO_BLK_F_SCSIis supposed to mean whether the host can *parse* SCSI requests, not *execute* them. So it should always be enabled, and the scsi=on/off property tied to a separate configuration variable that is not guest visible.With this change, Linux has problems understanding failed requests, so patch 1 works around the Linux bugs.
Important: because we need to do this to fix a migration compatibility problem when QEMU might be invoked with an old machine type, we must do this unconditionally. This more or less assumes that no one ever invoked QEMU with scsi=off, as it breaks migration from new QEMU, scsi=off to old QEMU, also scsi=off. However
new->oldis not supported upstream.S390 compile-tested only.
Paolo Bonzini (4):
virtio-blk: report non-zero status when failingSG_IOrequests
virtio-blk:blockdev_mark_auto_delis transport-independent
virtio-blk: define VirtIOBlkConf
virtio-blk: always enableVIRTIO_BLK_F_SCSIQuoted 8 lines
hw/s390-virtio-bus.c | 10 ++++--- hw/s390-virtio-bus.h | 4 +-- hw/virtio-blk.c | 80 ++++++++++++++++++++++++-------------------------- hw/virtio-blk.h | 14 +++++---- hw/virtio-pci.c | 11 +++---- hw/virtio-pci.h | 4 +-- hw/virtio.h | 4 +-- 7 files changed, 64 insertions(+), 63 deletions(-)
Quoted 1 line
Ping?
Ack. I'll queue it for -rc3.
Regards,
Anthony Liguori