Message ID | 20200702125612.2176194-3-berrange@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | block: improve error reporting for unsupported O_DIRECT | expand |
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>
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); 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"; + } #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); + } + 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;
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(-)