Skip to contentSkip to conversation

osdep: warn if open(O_DIRECT) on fails with EINVAL

PATCH Applied Stefan Hajnoczi 5 participants 9 messages Aug 22, 2013 – Sep 18, 2013

lore
Share & tools
lore
#0 lore raw

cover osdep: warn if open(O_DIRECT) on fails with EINVAL

After trying out a few approaches, here is what I think is the simplest viable way to print a user-friendly warning if opening a file O_DIRECT fails with EINVAL. This happens on Linux tmpfs.

We don't really know why we got EINVAL but if O_DIRECT was used it's a good clue that the file system does not support O_DIRECT.

Stefan Hajnoczi (2):
libcacard: link against qemu-error.o for error_report()
osdep: warn if open(O_DIRECT) on fails with EINVAL

 libcacard/Makefile | 3 ++-
 util/osdep.c       | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 libcacard: link against qemu-error.o for error_report()

---
 libcacard/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libcacard/Makefile +2 -1
index 47827a0..4d15da4 100644
@@ -4,7 +4,8 @@ TOOLS += vscclient$(EXESUF)
 
 # objects linked into a shared library, built with libtool with -fPIC if required
 libcacard-obj-y = $(stub-obj-y) $(libcacard-y)
-libcacard-obj-y += util/osdep.o util/cutils.o util/qemu-timer-common.o util/error.o
+libcacard-obj-y += util/osdep.o util/cutils.o util/qemu-timer-common.o
+libcacard-obj-y += util/error.o util/qemu-error.o
 libcacard-obj-$(CONFIG_WIN32) += util/oslib-win32.o util/qemu-thread-win32.o
 libcacard-obj-$(CONFIG_POSIX) += util/oslib-posix.o util/qemu-thread-posix.o
 libcacard-obj-y += $(filter trace/%, $(util-obj-y))
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 osdep: warn if open(O_DIRECT) on fails with EINVAL

In reply to Stefan Hajnoczi

Print a warning when opening a file O_DIRECT fails with EINVAL. This saves users a lot of time trying to figure out the EINVAL error, which is typical when attempting to open a file O_DIRECT on Linux tmpfs.

---
 util/osdep.c | 7 +++++++
 1 file changed, 7 insertions(+)
util/osdep.c +7 -0
index 685c8ae..62072b4 100644
@@ -207,6 +207,13 @@ int qemu_open(const char *name, int flags, ...)
     }
 #endif
 
+#ifdef O_DIRECT
+    if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
+        error_report("file system may not support O_DIRECT");
+        errno = EINVAL; /* in case it was clobbered */
+    }
+#endif /* O_DIRECT */
+
     return ret;
 }
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 2/2
#3 lore raw
Quoted 15 lines · Stefan Hajnoczi

Print a warning when opening a file O_DIRECT fails with EINVAL. This saves users a lot of time trying to figure out the EINVAL error, which is typical when attempting to open a file O_DIRECT on Linux tmpfs.

---
 util/osdep.c | 7 +++++++
 1 file changed, 7 insertions(+)
util/osdep.c +0 -0
index 685c8ae..62072b4 100644

util/osdep.c int qemu_open(const char *name, int flags, ...) · line 210

Quoted 4 lines
util/osdep.c +1 -0
@@ -207,6 +207,13 @@ int qemu_open(const char *name, int flags, ...)
     }
 #endif
 
+#ifdef O_DIRECT

On some other projects I've worked with (hello gnulib!), the compatibility headers do:

#define O_DIRECT 0

so that the rest of the code can just blindly use open(...,|O_DIRECT) (provided, of course, that not having O_DIRECT semantics is not fatal...). If that is done, then this #ifdef will always be true...

util/osdep.c int qemu_open(const char *name, int flags, ...) · line 211

Quoted 1 line
util/osdep.c +1 -0
+    if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {

...but the '(flags & O_DIRECT)' subclause would be false, so that's safe.

If O_DIRECT were always defined, then '#if O_DIRECT', or better yet, 'if (O_DIRECT)', is a better way to conditionalize code that depends on it being a useful value (no false positives when defined to 0, and by moving the check from preprocessor to C, you get benefits of compiler verification of type safety to avoid bitrot in what is otherwise dead code).

Grepping through qemu, I see that block/raw-posix.c has some weird #ifdef'ery:

/* OS X does not have O_DSYNC */
#ifndef O_DSYNC
#ifdef O_SYNC
#define O_DSYNC O_SYNC
#elif defined(O_FSYNC)
#define O_DSYNC O_FSYNC
#endif
#endif

/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ #ifndef O_DIRECT
#define O_DIRECT O_DSYNC
#endif

which strikes me as odd (O_DSYNC and O_DIRECT have orthogonal, and somewhat opposing goals - the former says don't return from write() until data is on disk, which slows things down for safety; the latter says "I'm a special user space, get out of my way, by not caching anything, and leaving the flushing to me", to speed things up if the user knows what they are doing - then again, the Linux man page hints that they are often used together when worrying about guarantees for synchronous I/O).

But enough of that side diversion - that one #define of O_DIRECT is not related to the file you are touching. I like the elegance of your patch (nicer than the race in my double-open attempt).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
Quoted 5 lines · --On 22 August 2013 11:57:56 -0600 Eric Blake <[email protected]> wrote:
# define O_DIRECT 0

so that the rest of the code can just blindly use open(...,|O_DIRECT) (provided, of course, that not having O_DIRECT semantics is not fatal...). If that is done, then this #ifdef will always be true...

I think this is undesirable as the result of opening without O_DIRECT when you really wanted O_DIRECT could be subtle data corruption due to unexpected caching. Is an error not more appropriate here than proceeding regardless?

Signature · 1 line
Alex Bligh
#5 lore raw
Quoted 6 lines · Alex Bligh
Quoted 2 lines · --On 22 August 2013 11:57:56 -0600 Eric Blake <[email protected]> wrote:
# define O_DIRECT 0
Quoted 8 lines
Quoted 3 lines

so that the rest of the code can just blindly use open(...,|O_DIRECT) (provided, of course, that not having O_DIRECT semantics is not fatal...). If that is done, then this #ifdef will always be true...

I think this is undesirable as the result of opening without O_DIRECT when you really wanted O_DIRECT could be subtle data corruption due to unexpected caching. Is an error not more appropriate here than proceeding regardless?

As I said in the surrounding text you snipped:

Quoted 3 lines

On some other projects I've worked with (hello gnulib!), the compatibility headers do:

...

Quoted 3 lines

But enough of that side diversion - that one #define of O_DIRECT is not related to the file you are touching.

Qemu doesn't define O_DIRECT to 0 for the mere sake of compilation, and I'm not arguing that it should. I was more making sure that this patch was correct, by reassuring myself the policies that qemu uses for O_DIRECT (and since I demonstrated that other projects have other policies). As to whether other projects may have a bug when using O_DIRECT being 0, it is a problem for those projects to deal with. Besides, O_DIRECT is a non-POSIX extension, so anyone using it already has non-portability issues to think about, regardless of whether the fallback for platforms that lack it is done by always defining to 0 or always using #ifdef guards.

Thus, my diversion about other projects using O_DIRECT as 0 is a non-issue.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 32 lines · Stefan Hajnoczi

Print a warning when opening a file O_DIRECT fails with EINVAL. This saves users a lot of time trying to figure out the EINVAL error, which is typical when attempting to open a file O_DIRECT on Linux tmpfs.

---
 util/osdep.c | 7 +++++++
 1 file changed, 7 insertions(+)
util/osdep.c +7 -0
index 685c8ae..62072b4 100644
@@ -207,6 +207,13 @@ int qemu_open(const char *name, int flags, ...)
     }
 #endif
 
+#ifdef O_DIRECT
+    if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
+        error_report("file system may not support O_DIRECT");
+        errno = EINVAL; /* in case it was clobbered */
+    }
+#endif /* O_DIRECT */
+
     return ret;
 }
 
Signature · 1 line
1.8.3.1

What about putting something similar in error_setg_file_open(), in util/error.c? There are other occasions when O_DIRECT causes a file open to fail (e.g. live snapshots with 'cache=none' to a tmpfs filesystem). That would give additional info then for QMP commands. Of course, it would then be necessary to also pass the open flags (or other equivalent info) to error_setg_file_open().

#7 lore raw
Quoted 29 lines · Jeff Cody
Quoted 28 lines · Stefan Hajnoczi

Print a warning when opening a file O_DIRECT fails with EINVAL. This saves users a lot of time trying to figure out the EINVAL error, which is typical when attempting to open a file O_DIRECT on Linux tmpfs.

---
 util/osdep.c | 7 +++++++
 1 file changed, 7 insertions(+)
util/osdep.c +7 -0
index 685c8ae..62072b4 100644
@@ -207,6 +207,13 @@ int qemu_open(const char *name, int flags, ...)
     }
 #endif
 
+#ifdef O_DIRECT
+    if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
+        error_report("file system may not support O_DIRECT");
+        errno = EINVAL; /* in case it was clobbered */
+    }
+#endif /* O_DIRECT */
+
     return ret;
 }
 
Quoted 11 lines
Quoted 4 lines
Signature · 1 line
1.8.3.1

What about putting something similar in error_setg_file_open(), in util/error.c? There are other occasions when O_DIRECT causes a file open to fail (e.g. live snapshots with 'cache=none' to a tmpfs filesystem). That would give additional info then for QMP commands. Of course, it would then be necessary to also pass the open flags (or other equivalent info) to error_setg_file_open().

It would be nice to cover all file open cases. But I think error_setg_file_open() is a bit beyond the scope here.

error_setg_file_open() is not used in cases where O_DIRECT is relevant. I checked all callers and they don't seem to allow O_DIRECT. Some of them even use qemu_fopen().

Stefan

#8 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

After trying out a few approaches, here is what I think is the simplest viable way to print a user-friendly warning if opening a file O_DIRECT fails with EINVAL. This happens on Linux tmpfs.

We don't really know why we got EINVAL but if O_DIRECT was used it's a good clue that the file system does not support O_DIRECT.

Quoted 7 lines

Stefan Hajnoczi (2):
libcacard: link against qemu-error.o for error_report()
osdep: warn if open(O_DIRECT) on fails with EINVAL

 libcacard/Makefile | 3 ++-
 util/osdep.c       | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

Applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan