[{"id":1763926,"web_url":"http://patchwork.ozlabs.org/comment/1763926/","msgid":"<20170906085235.GD15510@redhat.com>","list_archive_url":null,"date":"2017-09-06T08:52:35","subject":"Re: [Qemu-devel] [PATCH 3/3] nbd: Use new qio_channel_*_all()\n\tfunctions","submitter":{"id":2694,"url":"http://patchwork.ozlabs.org/api/people/2694/","name":"Daniel P. Berrangé","email":"berrange@redhat.com"},"content":"On Tue, Sep 05, 2017 at 02:11:14PM -0500, Eric Blake wrote:\n> Rather than open-coding our own read/write-all functions, we\n> can make use of the recently-added qio code.  It slightly\n> changes the error message in one of the iotests.\n> \n> Signed-off-by: Eric Blake <eblake@redhat.com>\n> ---\n>  include/block/nbd.h        |  2 --\n>  nbd/nbd-internal.h         | 41 +++++++----------------------------------\n>  block/nbd-client.c         | 15 +++++++--------\n>  nbd/common.c               | 45 ---------------------------------------------\n>  tests/qemu-iotests/083.out |  8 ++++----\n>  5 files changed, 18 insertions(+), 93 deletions(-)\n> \n> diff --git a/include/block/nbd.h b/include/block/nbd.h\n> index 040cdd2e60..707fd37575 100644\n> --- a/include/block/nbd.h\n> +++ b/include/block/nbd.h\n> @@ -155,8 +155,6 @@ struct NBDExportInfo {\n>  };\n>  typedef struct NBDExportInfo NBDExportInfo;\n> \n> -ssize_t nbd_rwv(QIOChannel *ioc, struct iovec *iov, size_t niov, size_t length,\n> -                bool do_read, Error **errp);\n>  int nbd_receive_negotiate(QIOChannel *ioc, const char *name,\n>                            QCryptoTLSCreds *tlscreds, const char *hostname,\n>                            QIOChannel **outioc, NBDExportInfo *info,\n> diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h\n> index 03549e3f39..8a609a227f 100644\n> --- a/nbd/nbd-internal.h\n> +++ b/nbd/nbd-internal.h\n> @@ -85,28 +85,14 @@\n>  static inline int nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,\n>                                 Error **errp)\n>  {\n> -    struct iovec iov = { .iov_base = buffer, .iov_len = size };\n> -    ssize_t ret;\n> -\n> -    /* Sockets are kept in blocking mode in the negotiation phase.  After\n> -     * that, a non-readable socket simply means that another thread stole\n> -     * our request/reply.  Synchronization is done with recv_coroutine, so\n> -     * that this is coroutine-safe.\n> -     */\n> +    int ret;\n> \n>      assert(size);\n> -\n> -    ret = nbd_rwv(ioc, &iov, 1, size, true, errp);\n> -    if (ret <= 0) {\n> -        return ret;\n> +    ret = qio_channel_read_all_eof(ioc, buffer, size, errp);\n> +    if (ret < 0) {\n> +        ret = -EIO;\n>      }\n> -\n> -    if (ret != size) {\n> -        error_setg(errp, \"End of file\");\n> -        return -EINVAL;\n> -    }\n> -\n> -    return 1;\n> +    return ret;\n>  }\n> \n>  /* nbd_read\n> @@ -115,14 +101,7 @@ static inline int nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,\n>  static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,\n>                             Error **errp)\n>  {\n> -    int ret = nbd_read_eof(ioc, buffer, size, errp);\n> -\n> -    if (ret == 0) {\n> -        ret = -EINVAL;\n> -        error_setg(errp, \"End of file\");\n> -    }\n> -\n> -    return ret < 0 ? ret : 0;\n> +    return qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;\n>  }\n> \n>  /* nbd_write\n> @@ -131,13 +110,7 @@ static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,\n>  static inline int nbd_write(QIOChannel *ioc, const void *buffer, size_t size,\n>                              Error **errp)\n>  {\n> -    struct iovec iov = { .iov_base = (void *) buffer, .iov_len = size };\n> -\n> -    ssize_t ret = nbd_rwv(ioc, &iov, 1, size, false, errp);\n> -\n> -    assert(ret < 0 || ret == size);\n> -\n> -    return ret < 0 ? ret : 0;\n> +    return qio_channel_write_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;\n>  }\n> \n>  struct NBDTLSHandshakeData {\n> diff --git a/block/nbd-client.c b/block/nbd-client.c\n> index f0dbea24d3..ee7f758e68 100644\n> --- a/block/nbd-client.c\n> +++ b/block/nbd-client.c\n> @@ -121,7 +121,7 @@ static int nbd_co_send_request(BlockDriverState *bs,\n>                                 QEMUIOVector *qiov)\n>  {\n>      NBDClientSession *s = nbd_get_client_session(bs);\n> -    int rc, ret, i;\n> +    int rc, i;\n> \n>      qemu_co_mutex_lock(&s->send_mutex);\n>      while (s->in_flight == MAX_NBD_REQUESTS) {\n> @@ -156,9 +156,9 @@ static int nbd_co_send_request(BlockDriverState *bs,\n>          qio_channel_set_cork(s->ioc, true);\n>          rc = nbd_send_request(s->ioc, request);\n>          if (rc >= 0 && !s->quit) {\n> -            ret = nbd_rwv(s->ioc, qiov->iov, qiov->niov, request->len, false,\n> -                          NULL);\n> -            if (ret != request->len) {\n> +            assert(request->len == iov_size(qiov->iov, qiov->niov));\n> +            if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,\n> +                                       NULL) < 0) {\n>                  rc = -EIO;\n>              }\n>          }\n> @@ -184,7 +184,6 @@ static void nbd_co_receive_reply(NBDClientSession *s,\n>                                   QEMUIOVector *qiov)\n>  {\n>      int i = HANDLE_TO_INDEX(s, request->handle);\n> -    int ret;\n> \n>      /* Wait until we're woken up by nbd_read_reply_entry.  */\n>      s->requests[i].receiving = true;\n> @@ -195,9 +194,9 @@ static void nbd_co_receive_reply(NBDClientSession *s,\n>          reply->error = EIO;\n>      } else {\n>          if (qiov && reply->error == 0) {\n> -            ret = nbd_rwv(s->ioc, qiov->iov, qiov->niov, request->len, true,\n> -                          NULL);\n> -            if (ret != request->len) {\n> +            assert(request->len == iov_size(qiov->iov, qiov->niov));\n> +            if (qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,\n> +                                      NULL) < 0) {\n>                  reply->error = EIO;\n>                  s->quit = true;\n>              }\n> diff --git a/nbd/common.c b/nbd/common.c\n> index e288d1b972..59a5316be9 100644\n> --- a/nbd/common.c\n> +++ b/nbd/common.c\n> @@ -20,51 +20,6 @@\n>  #include \"qapi/error.h\"\n>  #include \"nbd-internal.h\"\n> \n> -/* nbd_wr_syncv\n> - * The function may be called from coroutine or from non-coroutine context.\n> - * When called from non-coroutine context @ioc must be in blocking mode.\n> - */\n> -ssize_t nbd_rwv(QIOChannel *ioc, struct iovec *iov, size_t niov, size_t length,\n> -                bool do_read, Error **errp)\n> -{\n> -    ssize_t done = 0;\n> -    struct iovec *local_iov = g_new(struct iovec, niov);\n> -    struct iovec *local_iov_head = local_iov;\n> -    unsigned int nlocal_iov = niov;\n> -\n> -    nlocal_iov = iov_copy(local_iov, nlocal_iov, iov, niov, 0, length);\n> -\n> -    while (nlocal_iov > 0) {\n> -        ssize_t len;\n> -        if (do_read) {\n> -            len = qio_channel_readv(ioc, local_iov, nlocal_iov, errp);\n> -        } else {\n> -            len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);\n> -        }\n> -        if (len == QIO_CHANNEL_ERR_BLOCK) {\n> -            /* errp should not be set */\n> -            assert(qemu_in_coroutine());\n> -            qio_channel_yield(ioc, do_read ? G_IO_IN : G_IO_OUT);\n> -            continue;\n> -        }\n> -        if (len < 0) {\n> -            done = -EIO;\n> -            goto cleanup;\n> -        }\n> -\n> -        if (do_read && len == 0) {\n> -            break;\n> -        }\n> -\n> -        iov_discard_front(&local_iov, &nlocal_iov, len);\n> -        done += len;\n> -    }\n> -\n> - cleanup:\n> -    g_free(local_iov_head);\n> -    return done;\n> -}\n> -\n>  /* Discard length bytes from channel.  Return -errno on failure and 0 on\n>   * success */\n>  int nbd_drop(QIOChannel *ioc, size_t size, Error **errp)\n> diff --git a/tests/qemu-iotests/083.out b/tests/qemu-iotests/083.out\n> index fb71b6f8ad..25dde519e3 100644\n> --- a/tests/qemu-iotests/083.out\n> +++ b/tests/qemu-iotests/083.out\n> @@ -69,12 +69,12 @@ read failed: Input/output error\n> \n>  === Check disconnect 4 reply ===\n> \n> -End of file\n> +Unexpected end-of-file before all bytes were read\n>  read failed: Input/output error\n> \n>  === Check disconnect 8 reply ===\n> \n> -End of file\n> +Unexpected end-of-file before all bytes were read\n>  read failed: Input/output error\n> \n>  === Check disconnect before data ===\n> @@ -180,12 +180,12 @@ read failed: Input/output error\n> \n>  === Check disconnect 4 reply ===\n> \n> -End of file\n> +Unexpected end-of-file before all bytes were read\n>  read failed: Input/output error\n> \n>  === Check disconnect 8 reply ===\n> \n> -End of file\n> +Unexpected end-of-file before all bytes were read\n>  read failed: Input/output error\n> \n>  === Check disconnect before data ===\n\nReviewed-by: Daniel P. Berrange <berrange@redhat.com>\n\nRegards,\nDaniel","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx10.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx10.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=berrange@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xnHRm1pCBz9sBZ\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed,  6 Sep 2017 18:53:24 +1000 (AEST)","from localhost ([::1]:35029 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dpW5K-0007Xc-D7\n\tfor incoming@patchwork.ozlabs.org; Wed, 06 Sep 2017 04:53:22 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:55534)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <berrange@redhat.com>) id 1dpW4m-0007UN-3T\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 04:52:50 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <berrange@redhat.com>) id 1dpW4k-0005gU-J4\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 04:52:48 -0400","from mx1.redhat.com ([209.132.183.28]:60250)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <berrange@redhat.com>)\n\tid 1dpW4d-0005cA-Fu; Wed, 06 Sep 2017 04:52:39 -0400","from smtp.corp.redhat.com\n\t(int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 75B115F739;\n\tWed,  6 Sep 2017 08:52:38 +0000 (UTC)","from redhat.com (unknown [10.42.22.189])\n\tby smtp.corp.redhat.com (Postfix) with ESMTPS id 48DB87B9F9;\n\tWed,  6 Sep 2017 08:52:37 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 75B115F739","Date":"Wed, 6 Sep 2017 09:52:35 +0100","From":"\"Daniel P. Berrange\" <berrange@redhat.com>","To":"Eric Blake <eblake@redhat.com>","Message-ID":"<20170906085235.GD15510@redhat.com>","References":"<20170905191114.5959-1-eblake@redhat.com>\n\t<20170905191114.5959-4-eblake@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20170905191114.5959-4-eblake@redhat.com>","User-Agent":"Mutt/1.8.3 (2017-05-23)","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.14","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.39]);\n\tWed, 06 Sep 2017 08:52:38 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"Re: [Qemu-devel] [PATCH 3/3] nbd: Use new qio_channel_*_all()\n\tfunctions","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Reply-To":"\"Daniel P. Berrange\" <berrange@redhat.com>","Cc":"Kevin Wolf <kwolf@redhat.com>, pbonzini@redhat.com, qemu-devel@nongnu.org,\n\tqemu-block@nongnu.org, Max Reitz <mreitz@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}}]