qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	qemu-block@nongnu.org, qemu-devel@nongnu.org
Cc: mreitz@redhat.com, kwolf@redhat.com, pbonzini@redhat.com, den@openvz.org
Subject: Re: [Qemu-devel] [PATCH 02/17] nbd/client: refactor nbd_read_eof
Date: Mon, 7 Aug 2017 06:42:10 -0500	[thread overview]
Message-ID: <484ffeb7-6571-af84-ee4b-0bfcb83df7ad@redhat.com> (raw)
In-Reply-To: <20170804151440.320927-3-vsementsov@virtuozzo.com>

[-- Attachment #1: Type: text/plain, Size: 4529 bytes --]

On 08/04/2017 10:14 AM, Vladimir Sementsov-Ogievskiy wrote:
> Refactor nbd_read_eof to return 1 on success, 0 on eof, when no
> data was read and <0 for other cases, because returned size of
> read data is not actually used.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>  nbd/nbd-internal.h         | 34 +++++++++++++++++++++++++---------
>  nbd/client.c               |  5 -----
>  tests/qemu-iotests/083.out |  4 ++--
>  3 files changed, 27 insertions(+), 16 deletions(-)
> 
> diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h
> index 396ddb4d3e..3fb0b6098a 100644
> --- a/nbd/nbd-internal.h
> +++ b/nbd/nbd-internal.h
> @@ -77,21 +77,37 @@
>  #define NBD_ESHUTDOWN  108
>  
>  /* nbd_read_eof
> - * Tries to read @size bytes from @ioc. Returns number of bytes actually read.
> - * May return a value >= 0 and < size only on EOF, i.e. when iteratively called
> - * qio_channel_readv() returns 0. So, there is no need to call nbd_read_eof
> - * iteratively.
> + * Tries to read @size bytes from @ioc.
> + * Returns 1 on success
> + *         0 on eof, when no data was read (errp is not set)
> + *         -EINVAL on eof, when some data < @size was read until eof
> + *         < 0 on read fail

In general, mixing negative errno value and generic < 0 in the same
function is most likely ambiguous.

>   */
> -static inline ssize_t nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,
> -                                   Error **errp)
> +static inline int nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,
> +                               Error **errp)
>  {
>      struct iovec iov = { .iov_base = buffer, .iov_len = size };
> +    ssize_t ret;
> +
>      /* Sockets are kept in blocking mode in the negotiation phase.  After
>       * that, a non-readable socket simply means that another thread stole
>       * our request/reply.  Synchronization is done with recv_coroutine, so
>       * that this is coroutine-safe.
>       */
> -    return nbd_rwv(ioc, &iov, 1, size, true, errp);
> +
> +    assert(size > 0);

Effectively the same as assert(size != 0).

> +
> +    ret = nbd_rwv(ioc, &iov, 1, size, true, errp);
> +    if (ret <= 0) {
> +        return ret;
> +    }

So this is a negative errno (or 0 on EOF),

> +
> +    if (ret != size) {
> +        error_setg(errp, "End of file");
> +        return -EINVAL;

and so is this. Which makes the function documentation not quite
accurate; you aren't mixing a generic < 0.

> +    }
> +
> +    return 1;
>  }
>  
>  /* nbd_read
> @@ -100,9 +116,9 @@ static inline ssize_t nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,
>  static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
>                             Error **errp)
>  {
> -    ssize_t ret = nbd_read_eof(ioc, buffer, size, errp);
> +    int ret = nbd_read_eof(ioc, buffer, size, errp);
>  
> -    if (ret >= 0 && ret != size) {
> +    if (ret == 0) {
>          ret = -EINVAL;
>          error_setg(errp, "End of file");

Why do we have to set errp here instead of in nbd_read_eof()? Is there
ever any case where hitting early EOF is not something that should be
treated as an error?

>      }
> diff --git a/nbd/client.c b/nbd/client.c
> index f1c16b588f..4556056daa 100644
> --- a/nbd/client.c
> +++ b/nbd/client.c
> @@ -925,11 +925,6 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
>          return ret;
>      }
>  
> -    if (ret != sizeof(buf)) {
> -        error_setg(errp, "read failed");
> -        return -EINVAL;
> -    }
> -
>      /* Reply
>         [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
>         [ 4 ..  7]    error   (0 == no error)
> diff --git a/tests/qemu-iotests/083.out b/tests/qemu-iotests/083.out
> index a24c6bfece..d3bea1b2f5 100644
> --- a/tests/qemu-iotests/083.out
> +++ b/tests/qemu-iotests/083.out
> @@ -69,12 +69,12 @@ read failed: Input/output error
>  
>  === Check disconnect 4 reply ===
>  
> -read failed
> +End of file
>  read failed: Input/output error

At least you tracked that your changes tweak the error message.  But I'm
not yet convinced whether this change simplifies anything.  Is there a
later patch that is easier to write with the new semantics which was not
possible with the pre-patch semantics?

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

  reply	other threads:[~2017-08-07 11:42 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-04 15:14 [Qemu-devel] [PATCH 00/17] nbd client refactoring and fixing Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 01/17] nbd/client: fix nbd_opt_go Vladimir Sementsov-Ogievskiy
2017-08-07 11:31   ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 02/17] nbd/client: refactor nbd_read_eof Vladimir Sementsov-Ogievskiy
2017-08-07 11:42   ` Eric Blake [this message]
2017-08-07 12:05     ` Vladimir Sementsov-Ogievskiy
2017-08-25 19:22       ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 03/17] nbd/client: refactor nbd_receive_reply Vladimir Sementsov-Ogievskiy
2017-08-25 21:16   ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 04/17] nbd/client: fix nbd_send_request to return int Vladimir Sementsov-Ogievskiy
2017-08-07  8:23   ` Daniel P. Berrange
2017-08-07  8:57     ` Vladimir Sementsov-Ogievskiy
2017-08-07 11:49       ` Eric Blake
2017-08-07 12:03       ` Daniel P. Berrange
2017-08-25 21:20   ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 05/17] block/nbd-client: get rid of ssize_t Vladimir Sementsov-Ogievskiy
2017-08-04 16:11   ` Daniel P. Berrange
2017-08-07  6:57     ` Vladimir Sementsov-Ogievskiy
2017-08-07  8:24       ` Daniel P. Berrange
2017-08-25 21:25   ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 06/17] block/nbd-client: fix nbd_read_reply_entry Vladimir Sementsov-Ogievskiy
2017-08-07 11:52   ` Eric Blake
2017-08-07 12:56     ` Vladimir Sementsov-Ogievskiy
2017-08-07 15:13       ` Eric Blake
2017-08-07 15:33         ` [Qemu-devel] [Qemu-block] " Eric Blake
2017-08-07 16:09           ` Vladimir Sementsov-Ogievskiy
2017-08-07 16:18             ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 07/17] block/nbd-client: refactor request send/receive Vladimir Sementsov-Ogievskiy
2017-08-25 18:49   ` Eric Blake
2017-08-25 19:08     ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 08/17] block/nbd-client: rename nbd_recv_coroutines_enter_all Vladimir Sementsov-Ogievskiy
2017-08-25 18:43   ` Eric Blake
2017-08-25 21:48     ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 09/17] block/nbd-client: move nbd_co_receive_reply content into nbd_co_request Vladimir Sementsov-Ogievskiy
2017-08-25 18:52   ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 10/17] block/nbd-client: move nbd_coroutine_end " Vladimir Sementsov-Ogievskiy
2017-08-25 21:57   ` Eric Blake
2017-08-04 15:14 ` [Qemu-devel] [PATCH 11/17] block/nbd-client: fix nbd_co_request: set s->reply.handle to 0 on error Vladimir Sementsov-Ogievskiy
2017-08-07 11:55   ` Eric Blake
2017-08-07 13:17     ` Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 12/17] block/nbd-client: refactor nbd_co_request Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 13/17] block/nbd-client: refactor NBDClientSession.recv_coroutine Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 14/17] block/nbd-client: exit reply-reading coroutine on incorrect handle Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 15/17] block/nbd-client: refactor reading reply Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 16/17] block/nbd-client: drop reply field from NBDClientSession Vladimir Sementsov-Ogievskiy
2017-08-04 15:14 ` [Qemu-devel] [PATCH 17/17] block/nbd-client: always return EIO on and after the first io channel error Vladimir Sementsov-Ogievskiy
2017-08-16 21:21 ` [Qemu-devel] [PATCH 00/17] nbd client refactoring and fixing Eric Blake
2017-08-17  7:37   ` Vladimir Sementsov-Ogievskiy
2017-08-25 22:10 ` Eric Blake
2017-08-29 22:12   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=484ffeb7-6571-af84-ee4b-0bfcb83df7ad@redhat.com \
    --to=eblake@redhat.com \
    --cc=den@openvz.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).