On Wed, Aug 21, 2013 at 9:08 AM, Markus Armbruster <arm...@redhat.com>wrote:
> Stefan Hajnoczi <stefa...@redhat.com> writes: > > > Print a warning when opening a file O_DIRECT on tmpfs fails. This saves > > Only when it fails with EINVAL, actually. Suggest "on tmpfs fails with > EINVAL." > > > users a lot of time trying to figure out the EINVAL error. > > > > Daniel P. Berrange <berra...@redhat.com> suggested opening the file > > without O_DIRECT as a portable way to check whether the file system > > supports O_DIRECT. That gets messy when flags contains O_CREAT since > > we'd create a file but return an error - or a race condition if we try > > to unlink the file. It's simpler to check the file system type. > > > > Reported-by: Deepak C Shetty <deepa...@linux.vnet.ibm.com> > > Signed-off-by: Stefan Hajnoczi <stefa...@redhat.com> > > --- > > util/osdep.c | 19 +++++++++++++++++++ > > 1 file changed, 19 insertions(+) > > > > diff --git a/util/osdep.c b/util/osdep.c > > index 685c8ae..446a1dc 100644 > > --- a/util/osdep.c > > +++ b/util/osdep.c > > @@ -30,6 +30,11 @@ > > #include <unistd.h> > > #include <fcntl.h> > > > > +#ifdef __linux__ > > +#include <sys/vfs.h> > > +#include <linux/magic.h> > > +#endif > > + > > /* Needed early for CONFIG_BSD etc. */ > > #include "config-host.h" > > > > @@ -207,6 +212,20 @@ int qemu_open(const char *name, int flags, ...) > > } > > #endif > > > > +#ifdef __linux__ > > + /* It is not possible to open files O_DIRECT on tmpfs. Provide a > hint that > > + * this may be the case (of course it could change in future kernel > > + * versions). > > + */ > > + if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) { > > + struct statfs st; > > + if (statfs(name, &st) == 0 && st.f_type == TMPFS_MAGIC) { > > + error_report("tmpfs file systems may not support O_DIRECT"); > > + } > > + errno = EINVAL; /* in case it was clobbered */ > > + } > > +#endif /* __linux__ */ > > + > > return ret; > > } > > In theory, the warning could be misleading, because we can get EINVAL > for reasons not related to flags & O_DIRECT. In practice, the warning > probably does much more good than harm. > > Actually EINVAL is only for O_DIRECT with open(). There are other filesystems that will return this other than tmpfs as well, but we were discussing /var/run so the assumption was tmpfs [1]. The code that performs the check to see if EINVAL should be returned checks to see if the direct_IO address space op is defined [2] (or get_xip_mem but that's another story). [1] https://www.redhat.com/archives/libvir-list/2013-August/msg00768.html [2] http://lxr.linux.no/#linux+v3.10.9/fs/open.c#L651 -- Doug Goldstein