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 | 4 ++--
src/storage/storage_backend.c | 2 +-
src/util/virfile.c | 34 ++++++++++++----------------------
src/util/virfile.h | 2 +-
src/util/virstoragefile.c | 13 ++++---------
5 files changed, 20 insertions(+), 35 deletions(-)
diff --git a/src/locking/lock_driver_sanlock.c b/src/locking/lock_driver_sanlock.c
index 9fc97db..60f305c 100644
--- a/src/locking/lock_driver_sanlock.c
+++ b/src/locking/lock_driver_sanlock.c
@@ -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);
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index 472cec6..b990a82 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -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);
diff --git a/src/util/virfile.c b/src/util/virfile.c
index a2bf008..5f56005 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -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);
}
diff --git a/src/util/virfile.h b/src/util/virfile.h
index b8e30c3..403d0ba 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -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 */
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 2be6c34..7a4f9a0 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -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 {
--
2.0.4