From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Deepak C Shetty <deepakcs@linux.vnet.ibm.com>
Subject: [Qemu-devel] [RFC 2/2] osdep: warn if opening a file O_DIRECT on tmpfs fails
Date: Thu, 22 Aug 2013 10:34:59 +0200 [thread overview]
Message-ID: <1377160499-11323-3-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1377160499-11323-1-git-send-email-stefanha@redhat.com>
Print a warning when opening a file O_DIRECT on tmpfs fails with EINVAL.
This saves users a lot of time trying to figure out the EINVAL error.
Reported-by: Deepak C Shetty <deepakcs@linux.vnet.ibm.com>
Suggested-by: Eric Blake <eblake@redhat.com>
Suggested-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
util/osdep.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 55 insertions(+), 7 deletions(-)
diff --git a/util/osdep.c b/util/osdep.c
index 685c8ae..cbee2b7 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -148,6 +148,23 @@ static int qemu_parse_fdset(const char *param)
#endif
/*
+ * Open a file with O_CLOEXEC semantics
+ */
+static int open_cloexec(const char *name, int flags, int mode)
+{
+ int ret;
+#ifdef O_CLOEXEC
+ ret = open(name, flags | O_CLOEXEC, mode);
+#else
+ ret = open(name, flags, mode);
+ if (ret >= 0) {
+ qemu_set_cloexec(ret);
+ }
+#endif
+ return ret;
+}
+
+/*
* Opens a file with FD_CLOEXEC set
*/
int qemu_open(const char *name, int flags, ...)
@@ -198,14 +215,45 @@ int qemu_open(const char *name, int flags, ...)
va_end(ap);
}
-#ifdef O_CLOEXEC
- ret = open(name, flags | O_CLOEXEC, mode);
-#else
- ret = open(name, flags, mode);
- if (ret >= 0) {
- qemu_set_cloexec(ret);
+ /* Some file systems do not support O_DIRECT and fail with EINVAL. Confirm
+ * the problem by trying again without O_DIRECT and printing a warning.
+ *
+ * Sounds easy enough but we need to be careful with O_CREAT since this
+ * function should not create a file when an error is returned.
+ */
+ if (flags & (O_CREAT | O_DIRECT) == O_CREAT | O_DIRECT) {
+ ret = open_cloexec(name, flags | O_EXCL, mode);
+
+ if (ret >= 0) {
+ return ret;
+ }
+
+ if (errno == EINVAL) {
+ error_report("file system may not support O_DIRECT");
+ errno = EINVAL; /* in case it was clobbered */
+ return ret;
+ } else if (errno == EEXIST && (flags & O_EXCL) == 0) {
+ /* We know the file existed, drop O_CREAT so the following open
+ * attempts do not create a file if O_DIRECT produces EINVAL. Note
+ * there is a race condition here if the file is deleted while we
+ * perform our open calls.
+ */
+ flags &= O_CREAT;
+ } else {
+ return ret;
+ }
+ }
+
+ ret = open_cloexec(name, flags, mode);
+
+ if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
+ int fd = open_cloexec(name, flags & ~O_DIRECT, mode);
+ if (fd >= 0) {
+ close(fd);
+ error_report("file system does not support O_DIRECT");
+ }
+ errno = EINVAL; /* in case it was clobbered */
}
-#endif
return ret;
}
--
1.8.3.1
prev parent reply other threads:[~2013-08-22 8:37 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-22 8:34 [Qemu-devel] [RFC 0/2] osdep: warn if opening a file O_DIRECT on tmpfs fails Stefan Hajnoczi
2013-08-22 8:34 ` [Qemu-devel] [RFC 1/2] libcacard: link against qemu-error.o for error_report() Stefan Hajnoczi
2013-08-22 8:34 ` Stefan Hajnoczi [this message]
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=1377160499-11323-3-git-send-email-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=armbru@redhat.com \
--cc=deepakcs@linux.vnet.ibm.com \
--cc=kwolf@redhat.com \
--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 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).