[libvirt] [PATCH v2 2/3] virfile: Check returned size from virFileFdPosixFallocate

John Ferlan jferlan at redhat.com
Mon Aug 11 20:30:20 UTC 2014


https://bugzilla.redhat.com/show_bug.cgi?id=1077068

Add an fstat check to virFileFdPosixFallocate and fail if the
allocated size doesn't match the expected value. Since the failure
will fall back to using safewrite(), just leave a VIR_WARN message
indicating the failure and some entrails for determining why the
failure occurred. It seems for an NFS file at least the allocation
may not work properly depending on the NFS configuration.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/util/virfile.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 7f03cbf..3b87d9f 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1038,9 +1038,28 @@ int
 virFileFdPosixFallocate(int fd, off_t offset, off_t len)
 {
 #ifdef HAVE_POSIX_FALLOCATE
+    struct stat sb;
+    off_t newlen;
     int ret = posix_fallocate(fd, offset, len);
-    if (ret == 0)
+    if (ret == 0) {
+        /* Let's make sure the new size matches our expectations */
+        if (fstat(fd, &sb) < 0)
+            return -1;
+# ifndef WIN32
+        newlen = (unsigned long long)sb.st_blocks *
+                 (unsigned long long)DEV_BSIZE;
+# else
+        newlen = sb.st_size;
+# endif
+        if (newlen != len) {
+            VIR_WARN("posix_fallocate only allocated '%lu' bytes, expected "
+                     "'%lu' byte file (blocks=%lu bsize=%d).",
+                     newlen, len, sb.st_blocks, DEV_BSIZE);
+            return -1;
+        }
+
         return 0;
+    }
     VIR_WARN("Failed to pre-allocate '%lu' bytes with return value '%d'",
              len, ret);
     errno = ret;
-- 
1.9.3




More information about the libvir-list mailing list