From patchwork Mon Apr 27 12:29:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 464976 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 85D8D140082 for ; Mon, 27 Apr 2015 22:29:51 +1000 (AEST) Received: from localhost ([::1]:54736 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YmiAX-00029o-Op for incoming@patchwork.ozlabs.org; Mon, 27 Apr 2015 08:29:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49981) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YmiAI-0001t8-EM for qemu-devel@nongnu.org; Mon, 27 Apr 2015 08:29:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YmiAD-0006Uj-Eo for qemu-devel@nongnu.org; Mon, 27 Apr 2015 08:29:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47562) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YmiAD-0006UW-9Z for qemu-devel@nongnu.org; Mon, 27 Apr 2015 08:29:29 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id ECC858E794 for ; Mon, 27 Apr 2015 12:29:28 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-36.ams2.redhat.com [10.36.112.36]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3RCTQo2027927; Mon, 27 Apr 2015 08:29:27 -0400 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 27 Apr 2015 14:29:25 +0200 Message-Id: <1430137765-13771-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: mst@redhat.com Subject: [Qemu-devel] [PATCH] virtio-scsi: fix "written length" field in the used ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org qsgl->size is the size of the data field after the request or response header (virtio-scsi currently supports only one such field; bidirectional requests are not supported). However, the used ring's len field is not concerned about the field after the request header, so do not count it unless req->mode signals the request was a read. Also, do not report that anything was written if the request failed, and subtract any residual bytes in case of buffer underrun. Signed-off-by: Paolo Bonzini Acked-by: Michael S. Tsirkin --- hw/scsi/virtio-scsi.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index c9bea06..4995f6e 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -65,6 +65,19 @@ void virtio_scsi_free_req(VirtIOSCSIReq *req) g_slice_free1(sizeof(*req) + vs->cdb_size, req); } +static size_t virtio_scsi_written_length(VirtIODevice *vdev, VirtIOSCSIReq *req) +{ + size_t sz = req->resp_iov.size; + + if (req->sreq && + req->mode == SCSI_XFER_FROM_DEV && + req->resp.cmd.response == VIRTIO_SCSI_S_OK) { + sz += req->qsgl.size - virtio_tswap32(vdev, req->resp.cmd.resid); + } + + return sz; +} + static void virtio_scsi_complete_req(VirtIOSCSIReq *req) { VirtIOSCSI *s = req->dev; @@ -76,7 +89,7 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req) assert(req->vq == NULL); virtio_scsi_vring_push_notify(req); } else { - virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size); + virtqueue_push(vq, &req->elem, virtio_scsi_written_length(vdev, req)); virtio_notify(vdev, vq); }