Navigation Menu

Skip to content

Commit

Permalink
block: use fdatasync instead of fsync if possible
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Christoph Hellwig authored and Anthony Liguori committed Sep 11, 2009
1 parent e900a7b commit 6f1953c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion block/cow.c
Expand Up @@ -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[] = {
Expand Down
2 changes: 1 addition & 1 deletion block/raw-posix.c
Expand Up @@ -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);
}


Expand Down
16 changes: 16 additions & 0 deletions cutils.c
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions qemu-common.h
Expand Up @@ -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);
Expand Down

0 comments on commit 6f1953c

Please sign in to comment.