]> xenbits.xensource.com Git - libvirt.git/commitdiff
safezero: fall back to writing zeroes even when resizing
authorJán Tomko <jtomko@redhat.com>
Wed, 7 Jan 2015 15:49:00 +0000 (16:49 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 9 Jan 2015 12:48:23 +0000 (13:48 +0100)
Remove the resize flag and use the same code path for all callers.
This flag was added by commit 18f0316 to allow virStorageFileResize
use 'safezero' while preserving the behavior.

Explicitly return -2 when a fallback to a different method should
be done, to make the code path more obvious.

Fail immediately when ftruncate fails in the mmap method,
as we did before commit 18f0316.

src/locking/lock_driver_sanlock.c
src/storage/storage_backend.c
src/util/virfile.c
src/util/virfile.h
src/util/virstoragefile.c

index 9fc97db1d82e8804c8a888b03fb173c2368162b7..60f305c43bbba94834f430aa6365f884d50b99a1 100644 (file)
@@ -281,7 +281,7 @@ static int virLockManagerSanlockSetupLockspace(void)
             /*
              * Pre allocate enough data for 1 block of leases at preferred alignment
              */
-            if (safezero(fd, 0, rv, false) < 0) {
+            if (safezero(fd, 0, rv) < 0) {
                 virReportSystemError(errno,
                                      _("Unable to allocate lockspace %s"),
                                      path);
@@ -690,7 +690,7 @@ static int virLockManagerSanlockCreateLease(struct sanlk_resource *res)
             /*
              * Pre allocate enough data for 1 block of leases at preferred alignment
              */
-            if (safezero(fd, 0, rv, false) < 0) {
+            if (safezero(fd, 0, rv) < 0) {
                 virReportSystemError(errno,
                                      _("Unable to allocate lease %s"),
                                      res->disks[0].path);
index 472cec6b3dde6d5a5cbb3de6e7cbe78689a0bc8a..b990a82957dbf32e87afe00d0cb5117d0b3084e6 100644 (file)
@@ -399,7 +399,7 @@ createRawFile(int fd, virStorageVolDefPtr vol,
     }
 
     if (remain && need_alloc) {
-        if (safezero(fd, vol->target.allocation - remain, remain, false) < 0) {
+        if (safezero(fd, vol->target.allocation - remain, remain) < 0) {
             ret = -errno;
             virReportSystemError(errno, _("cannot fill file '%s'"),
                                  vol->target.path);
index a2bf008570f9a2a6a94f9e5aca5f8578f418da04..5f56005ea1c57a79618fa4ad8239332c2f34a869 100644 (file)
@@ -1053,7 +1053,7 @@ safezero_posix_fallocate(int fd ATTRIBUTE_UNUSED,
                          off_t offset ATTRIBUTE_UNUSED,
                          off_t len ATTRIBUTE_UNUSED)
 {
-    return -1;
+    return -2;
 }
 #endif /* !HAVE_POSIX_FALLOCATE */
 
@@ -1063,9 +1063,7 @@ safezero_sys_fallocate(int fd,
                        off_t offset,
                        off_t len)
 {
-    int rc = -1;
-    rc = syscall(SYS_fallocate, fd, 0, offset, len);
-    return rc;
+    return syscall(SYS_fallocate, fd, 0, offset, len);
 }
 #else /* !HAVE_SYS_SYSCALL_H || !defined(SYS_fallocate) */
 static int
@@ -1073,9 +1071,7 @@ safezero_sys_fallocate(int fd ATTRIBUTE_UNUSED,
                        off_t offset ATTRIBUTE_UNUSED,
                        off_t len ATTRIBUTE_UNUSED)
 {
-    int rc = -1;
-    errno = ENOSYS;
-    return rc;
+    return -2;
 }
 #endif /* !HAVE_SYS_SYSCALL_H || !defined(SYS_fallocate) */
 
@@ -1111,7 +1107,7 @@ safezero_mmap(int fd, off_t offset, off_t len)
 
     /* fall back to writing zeroes using safewrite if mmap fails (for
      * example because of virtual memory limits) */
-    return -1;
+    return -2;
 }
 #else /* !HAVE_MMAP */
 static int
@@ -1119,7 +1115,7 @@ safezero_mmap(int fd ATTRIBUTE_UNUSED,
               off_t offset ATTRIBUTE_UNUSED,
               off_t len ATTRIBUTE_UNUSED)
 {
-    return -1;
+    return -2;
 }
 #endif /* !HAVE_MMAP */
 
@@ -1160,26 +1156,20 @@ safezero_slow(int fd, off_t offset, off_t len)
     return 0;
 }
 
-int safezero(int fd, off_t offset, off_t len, bool resize)
+int safezero(int fd, off_t offset, off_t len)
 {
     int ret;
 
-    /* posix_fallocate returns 0 on success or error number on failure,
-     * but errno is not set so use that to our advantage since we set
-     * errno to the returned value if we make the call. If we don't make
-     * the call because it doesn't exist, then errno won't change and
-     * we can try other methods.
-     */
-    errno = 0;
     ret = safezero_posix_fallocate(fd, offset, len);
-    if (ret == 0 || errno != 0)
+    if (ret != -2)
         return ret;
 
-    if (resize)
-        return safezero_sys_fallocate(fd, offset, len);
-
-    if (safezero_mmap(fd, offset, len) == 0)
+    if (safezero_sys_fallocate(fd, offset, len) == 0)
         return 0;
+
+    ret = safezero_mmap(fd, offset, len);
+    if (ret != -2)
+        return ret;
     return safezero_slow(fd, offset, len);
 }
 
index b8e30c3abb5a477c6bc30c18fa36678e9c3726d7..403d0ba7c329a0215f60acdf79824bf0adb8b37f 100644 (file)
@@ -41,7 +41,7 @@ typedef enum {
 ssize_t saferead(int fd, void *buf, size_t count) ATTRIBUTE_RETURN_CHECK;
 ssize_t safewrite(int fd, const void *buf, size_t count)
     ATTRIBUTE_RETURN_CHECK;
-int safezero(int fd, off_t offset, off_t len, bool resize)
+int safezero(int fd, off_t offset, off_t len)
     ATTRIBUTE_RETURN_CHECK;
 
 /* Don't call these directly - use the macros below */
index 2be6c34e8784794cadaee87d1b63c1f4590d091f..7a4f9a0b3915c91ce1089d2720e33f8a838e7ae8 100644 (file)
@@ -1117,15 +1117,10 @@ virStorageFileResize(const char *path,
     }
 
     if (pre_allocate) {
-        if (safezero(fd, offset, len, true) != 0) {
-            if (errno == ENOSYS)
-                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);
+        if (safezero(fd, offset, len) != 0) {
+            virReportSystemError(errno,
+                                 _("Failed to pre-allocate space for "
+                                   "file '%s'"), path);
             goto cleanup;
         }
     } else {