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