提交 1390c268 编写于 作者: J Ján Tomko 提交者: Michal Privoznik

safezero: fall back to writing zeroes even when resizing

Remove the resize flag and use the same code path for all callers.
This flag was added by commit 18f03166 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 18f03166.
上级 a4b00403
......@@ -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);
......
......@@ -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);
......
......@@ -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);
}
......
......@@ -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 */
......
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册