All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: "Kevin Wolf" <kwolf@redhat.com>,
	qemu-block@nongnu.org, qemu-devel@nongnu.org,
	"P J P" <ppandit@redhat.com>, "Max Reitz" <mreitz@redhat.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>
Subject: Re: [PATCH v2 2/3] util: support detailed error reporting for qemu_open
Date: Wed, 08 Jul 2020 08:55:26 +0200	[thread overview]
Message-ID: <8736624hsx.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20200702125612.2176194-3-berrange@redhat.com> ("Daniel P. Berrangé"'s message of "Thu, 2 Jul 2020 13:56:11 +0100")

Daniel P. Berrangé <berrange@redhat.com> writes:

> Create a "qemu_open_err" method which does the same as "qemu_open",
> but with a "Error **errp" for error reporting. There should be no
> behavioural difference for existing callers at this stage.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  include/qemu/osdep.h |  2 ++
>  util/osdep.c         | 66 +++++++++++++++++++++++++++++++++++---------
>  2 files changed, 55 insertions(+), 13 deletions(-)
>
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index 0d26a1b9bd..8506a978fa 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -483,6 +483,8 @@ int qemu_madvise(void *addr, size_t len, int advice);
>  int qemu_mprotect_rwx(void *addr, size_t size);
>  int qemu_mprotect_none(void *addr, size_t size);
>  
> +/* This is preferred over qemu_open for its improved error reporting */
> +int qemu_open_err(const char *name, int flags, Error **errp, ...);
>  int qemu_open(const char *name, int flags, ...);
>  int qemu_close(int fd);
>  int qemu_unlink(const char *name);

I still think renaming qemu_open() to something like
qemu_open_with_useless_errors() is the way to go.

> diff --git a/util/osdep.c b/util/osdep.c
> index e2b7507ee2..3de8bee463 100644
> --- a/util/osdep.c
> +++ b/util/osdep.c
> @@ -22,6 +22,7 @@
>   * THE SOFTWARE.
>   */
>  #include "qemu/osdep.h"
> +#include "qapi/error.h"
>  
>  /* Needed early for CONFIG_BSD etc. */
>  
> @@ -282,7 +283,7 @@ int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive)
>  /*
>   * Opens a file with FD_CLOEXEC set
>   */
> -int qemu_open(const char *name, int flags, ...)
> +static int qemu_openv(const char *name, int flags, Error **errp, va_list ap)
>  {
>      int ret;
>      int mode = 0;
> @@ -297,24 +298,31 @@ int qemu_open(const char *name, int flags, ...)
>  
>          fdset_id = qemu_parse_fdset(fdset_id_str);
>          if (fdset_id == -1) {
> +            error_setg(errp, "Could not parse fdset %s", name);
>              errno = EINVAL;
>              return -1;
>          }
>  
>          fd = monitor_fdset_get_fd(fdset_id, flags);
>          if (fd < 0) {
> +            error_setg_errno(errp, -fd, "Could not acquire FD for %s flags %x",
> +                             name, flags);
>              errno = -fd;
>              return -1;
>          }
>  
>          dupfd = qemu_dup_flags(fd, flags);
>          if (dupfd == -1) {
> +            error_setg_errno(errp, errno, "Could not dup FD for %s flags %x",
> +                             name, flags);
>              return -1;
>          }
>  
>          ret = monitor_fdset_dup_fd_add(fdset_id, dupfd);
>          if (ret == -1) {
>              close(dupfd);
> +            error_setg(errp, "Could not save FD for %s flags %x",
> +                       name, flags);
>              errno = EINVAL;
>              return -1;
>          }
> @@ -324,11 +332,7 @@ int qemu_open(const char *name, int flags, ...)
>  #endif
>  
>      if (flags & O_CREAT) {
> -        va_list ap;
> -
> -        va_start(ap, flags);
>          mode = va_arg(ap, int);
> -        va_end(ap);
>      }
>  
>  #ifdef O_CLOEXEC
> @@ -342,21 +346,57 @@ int qemu_open(const char *name, int flags, ...)
>      }
>  #endif
>  
> +    if (ret == -1) {
> +        const char *action = "open";
> +        if (flags & O_CREAT) {
> +            action = "create";
> +        }

I'd prefer the more concise single assignment

           const char *action = flags & O_CREAT ? "create" : "open";

I'd also put it right at the beginning of the function.

>  #ifdef O_DIRECT
> -    if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
> -        int newflags = flags & ~O_DIRECT;
> -        ret = open(name, newflags, mode);
> -        if (ret != -1) {
> -            close(ret);
> -            error_report("file system does not support O_DIRECT");
> -            errno = EINVAL;
> +        if (errno == EINVAL && (flags & O_DIRECT)) {
> +            int newflags = flags & ~O_DIRECT;
> +            ret = open(name, newflags, mode);
> +            if (ret != -1) {
> +                close(ret);
> +                error_setg(errp, "Could not %s '%s' flags 0x%x: "
> +                           "filesystem does not support O_DIRECT",
> +                           action, name, flags);
> +                errno = EINVAL;
> +                return -1;
> +            }
>          }
> -    }
>  #endif /* O_DIRECT */
> +        error_setg_errno(errp, errno, "Could not %s '%s' flags 0x%x",
> +                         action, name, flags);
> +    }
> +

Swapping with PATCH 1 will reduce churn here.

>  
>      return ret;
>  }
>  
> +int qemu_open_err(const char *name, int flags, Error **errp, ...)
> +{
> +    va_list ap;
> +    int rv;
> +
> +    va_start(ap, errp);
> +    rv = qemu_openv(name, flags, errp, ap);
> +    va_end(ap);
> +
> +    return rv;
> +}
> +
> +int qemu_open(const char *name, int flags, ...)
> +{
> +    va_list ap;
> +    int rv;
> +
> +    va_start(ap, flags);
> +    rv = qemu_openv(name, flags, NULL, ap);
> +    va_end(ap);
> +
> +    return rv;
> +}
> +
>  int qemu_close(int fd)
>  {
>      int64_t fdset_id;

Preferably with suggested improvements:
Reviewed-by: Markus Armbruster <armbru@redhat.com>



  reply	other threads:[~2020-07-08 22:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-02 12:56 [PATCH v2 0/3] block: improve error reporting for unsupported O_DIRECT Daniel P. Berrangé
2020-07-02 12:56 ` [PATCH v2 1/3] util: validate whether O_DIRECT is supported after failure Daniel P. Berrangé
2020-07-02 15:29   ` Philippe Mathieu-Daudé
2020-07-08  6:45   ` Markus Armbruster
2020-07-02 12:56 ` [PATCH v2 2/3] util: support detailed error reporting for qemu_open Daniel P. Berrangé
2020-07-08  6:55   ` Markus Armbruster [this message]
2020-07-02 12:56 ` [PATCH v2 3/3] block: switch to use qemu_open_err for improved errors Daniel P. Berrangé
2020-07-08  6:57   ` Markus Armbruster

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=8736624hsx.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=f4bug@amsat.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=ppandit@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.