[libvirt] [PATCH 1/3] use virFileAllocate in virStorageFileResize

Ján Tomko jtomko at redhat.com
Mon Sep 25 15:46:08 UTC 2017


Introduce a new function virFileAllocate that will call the
non-destructive variants of safezero, essentially reverting
my commit 1390c268
    safezero: fall back to writing zeroes even when resizing
back to the state as of commit 18f0316
    virstoragefile: Have virStorageFileResize use safezero

This means that _ALLOCATE flag will no longer work on platforms
without the allocate syscalls, but it will not overwrite data
either.
---
 src/util/virfile.c        | 11 +++++++++++
 src/util/virfile.h        |  2 ++
 src/util/virstoragefile.c | 15 ++++++++++-----
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index a03a23fab..7ca60052d 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1216,6 +1216,17 @@ int safezero(int fd, off_t offset, off_t len)
     return safezero_slow(fd, offset, len);
 }
 
+int virFileAllocate(int fd, off_t offset, off_t len)
+{
+    int ret;
+
+    ret = safezero_posix_fallocate(fd, offset, len);
+    if (ret != -2)
+        return ret;
+
+    return safezero_sys_fallocate(fd, offset, len);
+}
+
 #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
 /* search /proc/mounts for mount point of *type; return pointer to
  * malloc'ed string of the path if found, otherwise return NULL
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 57ceb8072..21fb41b70 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -44,6 +44,8 @@ ssize_t safewrite(int fd, const void *buf, size_t count)
     ATTRIBUTE_RETURN_CHECK;
 int safezero(int fd, off_t offset, off_t len)
     ATTRIBUTE_RETURN_CHECK;
+int virFileAllocate(int fd, off_t offset, off_t len)
+    ATTRIBUTE_RETURN_CHECK;
 
 /* Don't call these directly - use the macros below */
 int virFileClose(int *fdptr, virFileCloseFlags flags)
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 484a5c806..b3da0a452 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -1320,7 +1320,7 @@ virStorageFileResize(const char *path,
 {
     int fd = -1;
     int ret = -1;
-    int rc ATTRIBUTE_UNUSED;
+    int rc;
     off_t offset ATTRIBUTE_UNUSED;
     off_t len ATTRIBUTE_UNUSED;
 
@@ -1333,10 +1333,15 @@ virStorageFileResize(const char *path,
     }
 
     if (pre_allocate) {
-        if (safezero(fd, offset, len) != 0) {
-            virReportSystemError(errno,
-                                 _("Failed to pre-allocate space for "
-                                   "file '%s'"), path);
+        if ((rc = virFileAllocate(fd, offset, len)) != 0) {
+            if (rc == -2) {
+                virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                               _("preallocate is not supported on this platform"));
+            } else {
+                virReportSystemError(errno,
+                                     _("Failed to pre-allocate space for "
+                                       "file '%s'"), path);
+            }
             goto cleanup;
         }
     } else {
-- 
2.13.0




More information about the libvir-list mailing list