Using posix_fallocate() to allocate disk space and fill it with zeros is faster than writing the zeros block-by-block. Also, for backing file systems that support extents and the fallocate() syscall, this operation will give us a big speed boost. This also brings us the advantage of very less fragmentation for the chunk being allocated. For systems that don't support posix_fallocate(), fall back to safewrite(). Signed-off-by: Amit Shah <amit.shah@xxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/util.c | 20 ++++++++++++++++++++ src/util.h | 1 + 3 files changed, 22 insertions(+), 0 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f0d8afa..a5f9f92 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -308,6 +308,7 @@ virStrToLong_ui; virFileLinkPointsTo; saferead; safewrite; +safezero; virMacAddrCompare; virEnumFromString; virEnumToString; diff --git a/src/util.c b/src/util.c index 66ad9a4..b69d33a 100644 --- a/src/util.c +++ b/src/util.c @@ -117,6 +117,26 @@ ssize_t safewrite(int fd, const void *buf, size_t count) return nwritten; } +#if 1 +int safezero(int fd, int flags, off_t offset, off_t len) +{ + return posix_fallocate(fd, offset, len); +} +#else +int safezero(int fd, int flags, off_t offset, off_t len) +{ + char *buf; + int r; + + buf = calloc(len, sizeof(char)); + if (buf == NULL) + return -ENOMEM; + + r = safewrite(fd, buf, len); + return r; +} +#endif + #ifndef PROXY int virFileStripSuffix(char *str, diff --git a/src/util.h b/src/util.h index 87cbf67..3fd5d25 100644 --- a/src/util.h +++ b/src/util.h @@ -31,6 +31,7 @@ int saferead(int fd, void *buf, size_t count); ssize_t safewrite(int fd, const void *buf, size_t count); +int safezero(int fd, int flags, off_t offset, off_t len); enum { VIR_EXEC_NONE = 0, -- 1.6.0.6 -- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list