Received: from eggs.gnu.org ([2001:4830:134:3::10]:49532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VgTmC-0003iv-So for qemu-devel@nongnu.org; Wed, 13 Nov 2013 01:18:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VgTm5-0002pK-He for qemu-devel@nongnu.org; Wed, 13 Nov 2013 01:18:08 -0500 Received: from ssl.dlhnet.de ([91.198.192.8]:39287 helo=ssl.dlh.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VgTm5-0002o1-80 for qemu-devel@nongnu.org; Wed, 13 Nov 2013 01:18:01 -0500 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) From: Peter Lieven In-Reply-To: <1384271389-20716-7-git-send-email-pbonzini@redhat.com> Date: Wed, 13 Nov 2013 07:18:13 +0100 Content-Transfer-Encoding: quoted-printable Message-Id: References: <1384271389-20716-1-git-send-email-pbonzini@redhat.com> <1384271389-20716-7-git-send-email-pbonzini@redhat.com> Subject: Re: [Qemu-devel] [PATCH 06/11] scsi-disk: correctly implement WRITE SAME List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: kwolf@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com Am 12.11.2013 um 16:49 schrieb Paolo Bonzini : > The WRITE SAME command is implemented incorrectly. WRITE SAME with = the > UNMAP bit set should _not_ unmap the sectors unless the written data > matches the payload of the WRITE SAME command; currently, QEMU is not > looking at the payload at all. >=20 > Thus, fetch the data to be written from the input buffer. If it is > all zeroes, we can use the write_zeroes call (possibly with the new > MAY_UNMAP flag). Otherwise, do as many write cycles as needed, = covering > 512k at a time to avoid allocating lots of memory for the bounce > buffer. Would it make sense to add a bdrv_write_same or is the use case for WRITE SAME with non-zero payload too rare? And secondly would it make sense to add an optimal request size field to the BlockLimits? >=20 > Strictly speaking, this is still incorrect because a zero cluster = should > only be written if the MAY_UNMAP flag is set. But this is a bug in = the > block layer, not here. Can you explain what exactly you mean? >=20 > Signed-off-by: Paolo Bonzini > --- > hw/scsi/scsi-disk.c | 140 = +++++++++++++++++++++++++++++++++++++++++++--------- > 1 file changed, 116 insertions(+), 24 deletions(-) >=20 > diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c > index 7e29760..cd5116c 100644 > --- a/hw/scsi/scsi-disk.c > +++ b/hw/scsi/scsi-disk.c > @@ -41,6 +41,7 @@ do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } = while (0) > #include > #endif >=20 > +#define SCSI_WRITE_SAME_MAX 524288 I would call this SCSI_MAX_WRITE_SAME_LEN (like max_ws_len) > #define SCSI_DMA_BUF_SIZE 131072 > #define SCSI_MAX_INQUIRY_LEN 256 > #define SCSI_MAX_MODE_LEN 256 > @@ -634,6 +635,8 @@ static int scsi_disk_emulate_inquiry(SCSIRequest = *req, uint8_t *outbuf) > buflen =3D 0x40; > memset(outbuf + 4, 0, buflen - 4); >=20 > + outbuf[4] =3D 0x1; /* wsnz */ > + > /* optimal transfer length granularity */ > outbuf[6] =3D (min_io_size >> 8) & 0xff; > outbuf[7] =3D min_io_size & 0xff; > @@ -1589,6 +1592,111 @@ invalid_field: > scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); > } >=20 > +typedef struct WriteSameCBData { > + SCSIDiskReq *r; > + int64_t sector; > + int nb_sectors; > + QEMUIOVector qiov; > + struct iovec iov; > +} WriteSameCBData; > + > +static void scsi_write_same_complete(void *opaque, int ret) > +{ > + WriteSameCBData *data =3D opaque; > + SCSIDiskReq *r =3D data->r; > + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, r->req.dev); > + > + assert(r->req.aiocb !=3D NULL); > + r->req.aiocb =3D NULL; > + bdrv_acct_done(s->qdev.conf.bs, &r->acct); > + if (r->req.io_canceled) { > + goto done; > + } > + > + if (ret < 0) { > + if (scsi_handle_rw_error(r, -ret)) { > + goto done; > + } > + } > + > + data->nb_sectors -=3D data->iov.iov_len / 512; > + data->sector +=3D data->iov.iov_len / 512; > + data->iov.iov_len =3D MIN(data->nb_sectors * 512, = data->iov.iov_len); > + if (data->iov.iov_len) { > + bdrv_acct_start(s->qdev.conf.bs, &r->acct, data->iov.iov_len, = BDRV_ACCT_WRITE); > + r->req.aiocb =3D bdrv_aio_writev(s->qdev.conf.bs, = data->sector, > + &data->qiov, data->iov.iov_len = / 512, > + scsi_write_same_complete, r); > + return; > + } > + > + scsi_req_complete(&r->req, GOOD); > + > +done: > + if (!r->req.io_canceled) { > + scsi_req_unref(&r->req); > + } > + g_free (data->iov.iov_base); > + g_free (data); > +} > + > +static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t = *inbuf) > +{ > + SCSIRequest *req =3D &r->req; > + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); > + uint32_t nb_sectors =3D scsi_data_cdb_length(r->req.cmd.buf); > + WriteSameCBData *data; > + uint8_t *buf; > + int i; > + > + /* Fail if PBDATA=3D1 or LBDATA=3D1 or ANCHOR=3D1. */ > + if (nb_sectors =3D=3D 0 || (req->cmd.buf[1] & 0x16)) { > + scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); > + return; > + } > + > + if (bdrv_is_read_only(s->qdev.conf.bs)) { > + scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); > + return; > + } > + if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) { > + scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); > + return; > + } > + > + if (buffer_is_zero(inbuf, s->qdev.blocksize)) { > + int flags =3D (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : = 0; > + > + /* The request is used as the AIO opaque value, so add a ref. = */ > + scsi_req_ref(&r->req); > + bdrv_acct_start(s->qdev.conf.bs, &r->acct, nb_sectors * = s->qdev.blocksize, > + BDRV_ACCT_WRITE); > + r->req.aiocb =3D bdrv_aio_write_zeroes(s->qdev.conf.bs, > + r->req.cmd.lba * = (s->qdev.blocksize / 512), > + nb_sectors * = (s->qdev.blocksize / 512), > + flags, = scsi_aio_complete, r); > + return; > + } > + > + data =3D g_new0(WriteSameCBData, 1); > + data->r =3D r; > + data->sector =3D r->req.cmd.lba * (s->qdev.blocksize / 512); > + data->nb_sectors =3D nb_sectors * (s->qdev.blocksize / 512); > + data->iov.iov_len =3D MIN(data->nb_sectors * 512, = SCSI_WRITE_SAME_MAX); > + data->iov.iov_base =3D buf =3D g_malloc(data->iov.iov_len); > + qemu_iovec_init_external(&data->qiov, &data->iov, 1); > + > + for (i =3D 0; i < data->iov.iov_len; i +=3D s->qdev.blocksize) { > + memcpy(&buf[i], inbuf, s->qdev.blocksize); > + } > + > + scsi_req_ref(&r->req); > + bdrv_acct_start(s->qdev.conf.bs, &r->acct, data->iov.iov_len, = BDRV_ACCT_WRITE); > + r->req.aiocb =3D bdrv_aio_writev(s->qdev.conf.bs, data->sector, > + &data->qiov, data->iov.iov_len / = 512, > + scsi_write_same_complete, data); > +} I asked myself many time if to use 512 or BDRV_SECTOR_SIZE. Both is used heavily in the whole block code. > + > static void scsi_disk_emulate_write_data(SCSIRequest *req) > { > SCSIDiskReq *r =3D DO_UPCAST(SCSIDiskReq, req, req); > @@ -1612,6 +1720,10 @@ static void = scsi_disk_emulate_write_data(SCSIRequest *req) > scsi_disk_emulate_unmap(r, r->iov.iov_base); > break; >=20 > + case WRITE_SAME_10: > + case WRITE_SAME_16: > + scsi_disk_emulate_write_same(r, r->iov.iov_base); > + break; > default: > abort(); > } > @@ -1854,30 +1966,10 @@ static int32_t = scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf) > break; > case WRITE_SAME_10: > case WRITE_SAME_16: > - nb_sectors =3D scsi_data_cdb_length(r->req.cmd.buf); > - if (bdrv_is_read_only(s->qdev.conf.bs)) { > - scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); > - return 0; > - } > - if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) { > - goto illegal_lba; > - } > - > - /* > - * We only support WRITE SAME with the unmap bit set for now. > - * Reject UNMAP=3D0 or ANCHOR=3D1. > - */ > - if (!(req->cmd.buf[1] & 0x8) || (req->cmd.buf[1] & 0x10)) { > - goto illegal_request; > - } > - > - /* The request is used as the AIO opaque value, so add a ref. = */ > - scsi_req_ref(&r->req); > - r->req.aiocb =3D bdrv_aio_discard(s->qdev.conf.bs, > - r->req.cmd.lba * = (s->qdev.blocksize / 512), > - nb_sectors * = (s->qdev.blocksize / 512), > - scsi_aio_complete, r); > - return 0; > + DPRINTF("WRITE SAME %d (len %lu)\n", > + req->cmd.buf[0] =3D=3D WRITE_SAME_10 ? 10 : 16, > + (long)r->req.cmd.xfer); > + break; > default: > DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]); > scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); > --=20 > 1.8.4.2 >=20 >=20 Peter=