[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] v3: Use posix_fallocate() on supported systems to allocate diskspace



On (Thu) Mar 19 2009 [20:17:52], Amit Shah wrote:
> This patchset makes use of the posix_fallocate() call to allocate
> chunks of files whenever needed if it's available.
> 
> We fallback to using safewrite() if it's not available.
> 
> mmap() could be used instead of safewrite too; I have a patch in case
> someone is interested in seeing it.

Something like this:

>From d33b843b381ea6a25c6e8efb6b248965a40e5f84 Mon Sep 17 00:00:00 2001
From: Amit Shah <amit shah redhat com>
Date: Thu, 19 Mar 2009 21:43:50 +0530
Subject: [PATCH] Use mmap() and memset() for safezero

If available, use mmap to allocate zeroed chunks for files. This
should be faster than allocating small chunks using safewrite.

Signed-off-by: Amit Shah <amit shah redhat com>
---
 configure.in |    2 +-
 src/util.c   |   32 +++++++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/configure.in b/configure.in
index edce040..6b2bb5e 100644
--- a/configure.in
+++ b/configure.in
@@ -72,7 +72,7 @@ dnl Use --disable-largefile if you don't want this.
 AC_SYS_LARGEFILE
 
 dnl Availability of various common functions (non-fatal if missing).
-AC_CHECK_FUNCS([cfmakeraw regexec uname sched_getaffinity getuid getgid posix_fallocate])
+AC_CHECK_FUNCS([cfmakeraw regexec uname sched_getaffinity getuid getgid posix_fallocate mmap])
 
 dnl Availability of various not common threadsafe functions
 AC_CHECK_FUNCS([strerror_r strtok_r getmntent_r getgrnam_r getpwuid_r])
diff --git a/src/util.c b/src/util.c
index 955c4e5..93d2937 100644
--- a/src/util.c
+++ b/src/util.c
@@ -39,6 +39,9 @@
 #if HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
+#if HAVE_MMAP
+#include <sys/mman.h>
+#endif
 #include <string.h>
 #include <signal.h>
 #if HAVE_TERMIOS_H
@@ -123,6 +126,32 @@ int safezero(int fd, int flags, off_t offset, off_t len)
     return posix_fallocate(fd, offset, len);
 }
 #else
+
+#ifdef HAVE_MMAP
+int safezero(int fd, int flags, off_t offset, off_t len)
+{
+    int r;
+    char *buf;
+
+    /* memset wants the mmap'ed file to be present on disk so create a
+     * sparse file
+     */
+    r = ftruncate(fd, len);
+    if (r < 0)
+        return -errno;
+
+    buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
+    if (buf == MAP_FAILED)
+        return -errno;
+
+    memset(buf, 0, len);
+    munmap(buf, len);
+
+    return 0;
+}
+
+#else /* HAVE_MMAP */
+
 int safezero(int fd, int flags, off_t offset, off_t len)
 {
     int r;
@@ -153,7 +182,8 @@ int safezero(int fd, int flags, off_t offset, off_t len)
     VIR_FREE(buf);
     return 0;
 }
-#endif
+#endif /* HAVE_MMAP */
+#endif /* HAVE_POSIX_FALLOCATE */
 
 #ifndef PROXY
 
-- 
1.6.0.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]