]> git.proxmox.com Git - qemu.git/commitdiff
block: use fdatasync instead of fsync if possible
authorChristoph Hellwig <hch@lst.de>
Fri, 4 Sep 2009 17:01:32 +0000 (19:01 +0200)
committerAnthony Liguori <aliguori@us.ibm.com>
Fri, 11 Sep 2009 15:19:46 +0000 (10:19 -0500)
If we are flushing the caches for our image files we only care about the
data (including the metadata required for accessing it) but not things
like timestamp updates.  So try to use fdatasync instead of fsync to
implement the flush operations.

Unfortunately many operating systems still do not support fdatasync,
so we add a qemu_fdatasync wrapper that uses fdatasync if available
as per the _POSIX_SYNCHRONIZED_IO feature macro or fsync otherwise.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
block/cow.c
block/raw-posix.c
cutils.c
qemu-common.h

index 84818f10348b3f124552e8e7a2973c5d18b53a53..a70854e631bbea31d01d092ad506cf6dc8da6ca6 100644 (file)
@@ -258,7 +258,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
 static void cow_flush(BlockDriverState *bs)
 {
     BDRVCowState *s = bs->opaque;
-    fsync(s->fd);
+    qemu_fdatasync(s->fd);
 }
 
 static QEMUOptionParameter cow_create_options[] = {
index 2125d67a5f71cf5c304a5a06cedc3eb47bfe373c..2ebc88f00469e1f2c634fbcd17d9bc1c8942fe76 100644 (file)
@@ -723,7 +723,7 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)
 static void raw_flush(BlockDriverState *bs)
 {
     BDRVRawState *s = bs->opaque;
-    fsync(s->fd);
+    qemu_fdatasync(s->fd);
 }
 
 
index ffe5c717e45d36eb43d671f574c1473a68b2f615..7a2234646a0f019ebfe3fd15af7f07461639e772 100644 (file)
--- a/cutils.c
+++ b/cutils.c
@@ -115,6 +115,22 @@ int qemu_fls(int i)
     return 32 - clz32(i);
 }
 
+/*
+ * Make sure data goes on disk, but if possible do not bother to
+ * write out the inode just for timestamp updates.
+ *
+ * Unfortunately even in 2009 many operating systems do not support
+ * fdatasync and have to fall back to fsync.
+ */
+int qemu_fdatasync(int fd)
+{
+#ifdef _POSIX_SYNCHRONIZED_IO
+    return fdatasync(fd);
+#else
+    return fsync(fd);
+#endif
+}
+
 /* io vectors */
 
 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
index f3cfb683eac6a7d865ffd48d23df14d8f53557f2..12e7dd0ada3fcc260d8d62f8ff7976fa24f44252 100644 (file)
@@ -114,6 +114,7 @@ int stristart(const char *str, const char *val, const char **ptr);
 int qemu_strnlen(const char *s, int max_len);
 time_t mktimegm(struct tm *tm);
 int qemu_fls(int i);
+int qemu_fdatasync(int fd);
 
 /* path.c */
 void init_paths(const char *prefix);