Blame SOURCES/kvm-scsi-disk-correctly-implement-WRITE-SAME.patch.patch.patch

0a122b
From 36d03c829bbf59e9346784b1e582803c482c4320 Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <36d03c829bbf59e9346784b1e582803c482c4320.1389014116.git.minovotn@redhat.com>
0a122b
In-Reply-To: <c8cc35838d42aa286242772d97e3a9be7bb786ba.1389014116.git.minovotn@redhat.com>
0a122b
References: <c8cc35838d42aa286242772d97e3a9be7bb786ba.1389014116.git.minovotn@redhat.com>
0a122b
From: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Date: Mon, 9 Dec 2013 14:09:25 +0100
0a122b
Subject: [PATCH 37/50] scsi-disk: correctly implement WRITE SAME
0a122b
0a122b
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Message-id: <1386598178-11845-40-git-send-email-pbonzini@redhat.com>
0a122b
Patchwork-id: 56076
0a122b
O-Subject: [RHEL 7.0 qemu-kvm PATCH 39/52] scsi-disk: correctly implement WRITE SAME
0a122b
Bugzilla: 1007815
0a122b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
Fetch the data to be written from the input buffer.  If it is all zeroes,
0a122b
we can use the write_zeroes call (possibly with the new MAY_UNMAP flag).
0a122b
Otherwise, do as many write cycles as needed, writing 512k at a time.
0a122b
0a122b
Strictly speaking, this is still incorrect because a zero cluster should
0a122b
only be written if the MAY_UNMAP flag is set.  But this is a bug in qcow2
0a122b
and the other formats, not in the SCSI code.
0a122b
0a122b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
(cherry picked from commit 84f94a9a82487639bc87d5f09f938c9f6a61f79a)
0a122b
---
0a122b
 hw/scsi/scsi-disk.c | 140 +++++++++++++++++++++++++++++++++++++++++++---------
0a122b
 1 file changed, 116 insertions(+), 24 deletions(-)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 hw/scsi/scsi-disk.c | 140 +++++++++++++++++++++++++++++++++++++++++++---------
0a122b
 1 file changed, 116 insertions(+), 24 deletions(-)
0a122b
0a122b
diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
0a122b
index 0640bb0..efadfc0 100644
0a122b
--- a/hw/scsi/scsi-disk.c
0a122b
+++ b/hw/scsi/scsi-disk.c
0a122b
@@ -41,6 +41,7 @@ do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
0a122b
 #include <scsi/sg.h>
0a122b
 #endif
0a122b
 
0a122b
+#define SCSI_WRITE_SAME_MAX         524288
0a122b
 #define SCSI_DMA_BUF_SIZE           131072
0a122b
 #define SCSI_MAX_INQUIRY_LEN        256
0a122b
 #define SCSI_MAX_MODE_LEN           256
0a122b
@@ -634,6 +635,8 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
0a122b
             buflen = 0x40;
0a122b
             memset(outbuf + 4, 0, buflen - 4);
0a122b
 
0a122b
+            outbuf[4] = 0x1; /* wsnz */
0a122b
+
0a122b
             /* optimal transfer length granularity */
0a122b
             outbuf[6] = (min_io_size >> 8) & 0xff;
0a122b
             outbuf[7] = min_io_size & 0xff;
0a122b
@@ -1589,6 +1592,111 @@ invalid_field:
0a122b
     scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
0a122b
 }
0a122b
 
0a122b
+typedef struct WriteSameCBData {
0a122b
+    SCSIDiskReq *r;
0a122b
+    int64_t sector;
0a122b
+    int nb_sectors;
0a122b
+    QEMUIOVector qiov;
0a122b
+    struct iovec iov;
0a122b
+} WriteSameCBData;
0a122b
+
0a122b
+static void scsi_write_same_complete(void *opaque, int ret)
0a122b
+{
0a122b
+    WriteSameCBData *data = opaque;
0a122b
+    SCSIDiskReq *r = data->r;
0a122b
+    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
0a122b
+
0a122b
+    assert(r->req.aiocb != NULL);
0a122b
+    r->req.aiocb = NULL;
0a122b
+    bdrv_acct_done(s->qdev.conf.bs, &r->acct);
0a122b
+    if (r->req.io_canceled) {
0a122b
+        goto done;
0a122b
+    }
0a122b
+
0a122b
+    if (ret < 0) {
0a122b
+        if (scsi_handle_rw_error(r, -ret)) {
0a122b
+            goto done;
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    data->nb_sectors -= data->iov.iov_len / 512;
0a122b
+    data->sector += data->iov.iov_len / 512;
0a122b
+    data->iov.iov_len = MIN(data->nb_sectors * 512, data->iov.iov_len);
0a122b
+    if (data->iov.iov_len) {
0a122b
+        bdrv_acct_start(s->qdev.conf.bs, &r->acct, data->iov.iov_len, BDRV_ACCT_WRITE);
0a122b
+        r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, data->sector,
0a122b
+                                       &data->qiov, data->iov.iov_len / 512,
0a122b
+                                       scsi_write_same_complete, r);
0a122b
+        return;
0a122b
+    }
0a122b
+
0a122b
+    scsi_req_complete(&r->req, GOOD);
0a122b
+
0a122b
+done:
0a122b
+    if (!r->req.io_canceled) {
0a122b
+        scsi_req_unref(&r->req);
0a122b
+    }
0a122b
+    qemu_vfree(data->iov.iov_base);
0a122b
+    g_free(data);
0a122b
+}
0a122b
+
0a122b
+static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
0a122b
+{
0a122b
+    SCSIRequest *req = &r->req;
0a122b
+    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
0a122b
+    uint32_t nb_sectors = scsi_data_cdb_length(r->req.cmd.buf);
0a122b
+    WriteSameCBData *data;
0a122b
+    uint8_t *buf;
0a122b
+    int i;
0a122b
+
0a122b
+    /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1.  */
0a122b
+    if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) {
0a122b
+        scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
0a122b
+        return;
0a122b
+    }
0a122b
+
0a122b
+    if (bdrv_is_read_only(s->qdev.conf.bs)) {
0a122b
+        scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
0a122b
+        return;
0a122b
+    }
0a122b
+    if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) {
0a122b
+        scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
0a122b
+        return;
0a122b
+    }
0a122b
+
0a122b
+    if (buffer_is_zero(inbuf, s->qdev.blocksize)) {
0a122b
+        int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0;
0a122b
+
0a122b
+        /* The request is used as the AIO opaque value, so add a ref.  */
0a122b
+        scsi_req_ref(&r->req);
0a122b
+        bdrv_acct_start(s->qdev.conf.bs, &r->acct, nb_sectors * s->qdev.blocksize,
0a122b
+                        BDRV_ACCT_WRITE);
0a122b
+        r->req.aiocb = bdrv_aio_write_zeroes(s->qdev.conf.bs,
0a122b
+                                             r->req.cmd.lba * (s->qdev.blocksize / 512),
0a122b
+                                             nb_sectors * (s->qdev.blocksize / 512),
0a122b
+                                             flags, scsi_aio_complete, r);
0a122b
+        return;
0a122b
+    }
0a122b
+
0a122b
+    data = g_new0(WriteSameCBData, 1);
0a122b
+    data->r = r;
0a122b
+    data->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
0a122b
+    data->nb_sectors = nb_sectors * (s->qdev.blocksize / 512);
0a122b
+    data->iov.iov_len = MIN(data->nb_sectors * 512, SCSI_WRITE_SAME_MAX);
0a122b
+    data->iov.iov_base = buf = qemu_blockalign(s->qdev.conf.bs, data->iov.iov_len);
0a122b
+    qemu_iovec_init_external(&data->qiov, &data->iov, 1);
0a122b
+
0a122b
+    for (i = 0; i < data->iov.iov_len; i += s->qdev.blocksize) {
0a122b
+        memcpy(&buf[i], inbuf, s->qdev.blocksize);
0a122b
+    }
0a122b
+
0a122b
+    scsi_req_ref(&r->req);
0a122b
+    bdrv_acct_start(s->qdev.conf.bs, &r->acct, data->iov.iov_len, BDRV_ACCT_WRITE);
0a122b
+    r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, data->sector,
0a122b
+                                   &data->qiov, data->iov.iov_len / 512,
0a122b
+                                   scsi_write_same_complete, data);
0a122b
+}
0a122b
+
0a122b
 static void scsi_disk_emulate_write_data(SCSIRequest *req)
0a122b
 {
0a122b
     SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
0a122b
@@ -1612,6 +1720,10 @@ static void scsi_disk_emulate_write_data(SCSIRequest *req)
0a122b
         scsi_disk_emulate_unmap(r, r->iov.iov_base);
0a122b
         break;
0a122b
 
0a122b
+    case WRITE_SAME_10:
0a122b
+    case WRITE_SAME_16:
0a122b
+        scsi_disk_emulate_write_same(r, r->iov.iov_base);
0a122b
+        break;
0a122b
     default:
0a122b
         abort();
0a122b
     }
0a122b
@@ -1854,30 +1966,10 @@ static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf)
0a122b
         break;
0a122b
     case WRITE_SAME_10:
0a122b
     case WRITE_SAME_16:
0a122b
-        nb_sectors = scsi_data_cdb_length(r->req.cmd.buf);
0a122b
-        if (bdrv_is_read_only(s->qdev.conf.bs)) {
0a122b
-            scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
0a122b
-            return 0;
0a122b
-        }
0a122b
-        if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) {
0a122b
-            goto illegal_lba;
0a122b
-        }
0a122b
-
0a122b
-        /*
0a122b
-         * We only support WRITE SAME with the unmap bit set for now.
0a122b
-         * Reject UNMAP=0 or ANCHOR=1.
0a122b
-         */
0a122b
-        if (!(req->cmd.buf[1] & 0x8) || (req->cmd.buf[1] & 0x10)) {
0a122b
-            goto illegal_request;
0a122b
-        }
0a122b
-
0a122b
-        /* The request is used as the AIO opaque value, so add a ref.  */
0a122b
-        scsi_req_ref(&r->req);
0a122b
-        r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs,
0a122b
-                                        r->req.cmd.lba * (s->qdev.blocksize / 512),
0a122b
-                                        nb_sectors * (s->qdev.blocksize / 512),
0a122b
-                                        scsi_aio_complete, r);
0a122b
-        return 0;
0a122b
+        DPRINTF("WRITE SAME %d (len %lu)\n",
0a122b
+                req->cmd.buf[0] == WRITE_SAME_10 ? 10 : 16,
0a122b
+                (long)r->req.cmd.xfer);
0a122b
+        break;
0a122b
     default:
0a122b
         DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
0a122b
         scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
0a122b
-- 
0a122b
1.7.11.7
0a122b