提交 18f03166 编写于 作者: J John Ferlan

virstoragefile: Have virStorageFileResize use safezero

Currently virStorageFileResize() function uses build conditionals to
choose either the posix_fallocate() or syscall(SYS_fallocate) with no
fallback in order to preallocate the space in the newly resized file.

Since the safezero code has a similar set of conditionals modify the
resize and safezero code in order to allow the resize logic to make use
of safezero to unify the look/feel of the code paths.

Add a new boolean (resize) to safezero() to make the optional decision
whether to try syscall(SYS_fallocate) if the posix_fallocate fails because
HAVE_POSIX_FALLOCATE is not defined (eg, return -1 and errno == 0).

Create a local safezero_sys_fallocate in order to handle the resize
code paths that support that.  If not present, the set errno = ENOSYS
in order to allow the caller to handle the failure scenarios.
Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
上级 214c687b
...@@ -281,7 +281,7 @@ static int virLockManagerSanlockSetupLockspace(void) ...@@ -281,7 +281,7 @@ static int virLockManagerSanlockSetupLockspace(void)
/* /*
* Pre allocate enough data for 1 block of leases at preferred alignment * Pre allocate enough data for 1 block of leases at preferred alignment
*/ */
if (safezero(fd, 0, rv) < 0) { if (safezero(fd, 0, rv, false) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to allocate lockspace %s"), _("Unable to allocate lockspace %s"),
path); path);
...@@ -690,7 +690,7 @@ static int virLockManagerSanlockCreateLease(struct sanlk_resource *res) ...@@ -690,7 +690,7 @@ static int virLockManagerSanlockCreateLease(struct sanlk_resource *res)
/* /*
* Pre allocate enough data for 1 block of leases at preferred alignment * Pre allocate enough data for 1 block of leases at preferred alignment
*/ */
if (safezero(fd, 0, rv) < 0) { if (safezero(fd, 0, rv, false) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to allocate lease %s"), _("Unable to allocate lease %s"),
res->disks[0].path); res->disks[0].path);
......
...@@ -399,7 +399,7 @@ createRawFile(int fd, virStorageVolDefPtr vol, ...@@ -399,7 +399,7 @@ createRawFile(int fd, virStorageVolDefPtr vol,
} }
if (remain && need_alloc) { if (remain && need_alloc) {
if (safezero(fd, vol->target.allocation - remain, remain) < 0) { if (safezero(fd, vol->target.allocation - remain, remain, false) < 0) {
ret = -errno; ret = -errno;
virReportSystemError(errno, _("cannot fill file '%s'"), virReportSystemError(errno, _("cannot fill file '%s'"),
vol->target.path); vol->target.path);
......
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
#if HAVE_MMAP #if HAVE_MMAP
# include <sys/mman.h> # include <sys/mman.h>
#endif #endif
#if HAVE_SYS_SYSCALL_H
# include <sys/syscall.h>
#endif
#ifdef __linux__ #ifdef __linux__
# if HAVE_LINUX_MAGIC_H # if HAVE_LINUX_MAGIC_H
...@@ -1046,6 +1049,20 @@ safezero_posix_fallocate(int fd, off_t offset, off_t len) ...@@ -1046,6 +1049,20 @@ safezero_posix_fallocate(int fd, off_t offset, off_t len)
return -1; return -1;
} }
static int
safezero_sys_fallocate(int fd,
off_t offset,
off_t len)
{
int rc = -1;
#if HAVE_SYS_SYSCALL_H && defined(SYS_fallocate)
rc = syscall(SYS_fallocate, fd, 0, offset, len);
#else
errno = ENOSYS;
#endif
return rc;
}
static int static int
safezero_mmap(int fd, off_t offset, off_t len) safezero_mmap(int fd, off_t offset, off_t len)
{ {
...@@ -1119,7 +1136,7 @@ safezero_slow(int fd, off_t offset, off_t len) ...@@ -1119,7 +1136,7 @@ safezero_slow(int fd, off_t offset, off_t len)
return 0; return 0;
} }
int safezero(int fd, off_t offset, off_t len) int safezero(int fd, off_t offset, off_t len, bool resize)
{ {
int ret; int ret;
...@@ -1134,6 +1151,9 @@ int safezero(int fd, off_t offset, off_t len) ...@@ -1134,6 +1151,9 @@ int safezero(int fd, off_t offset, off_t len)
if (ret == 0 || errno != 0) if (ret == 0 || errno != 0)
return ret; return ret;
if (resize)
return safezero_sys_fallocate(fd, offset, len);
if (safezero_mmap(fd, offset, len) == 0) if (safezero_mmap(fd, offset, len) == 0)
return 0; return 0;
return safezero_slow(fd, offset, len); return safezero_slow(fd, offset, len);
......
...@@ -41,7 +41,7 @@ typedef enum { ...@@ -41,7 +41,7 @@ typedef enum {
ssize_t saferead(int fd, void *buf, size_t count) ATTRIBUTE_RETURN_CHECK; ssize_t saferead(int fd, void *buf, size_t count) ATTRIBUTE_RETURN_CHECK;
ssize_t safewrite(int fd, const void *buf, size_t count) ssize_t safewrite(int fd, const void *buf, size_t count)
ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_RETURN_CHECK;
int safezero(int fd, off_t offset, off_t len) int safezero(int fd, off_t offset, off_t len, bool resize)
ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_RETURN_CHECK;
/* Don't call these directly - use the macros below */ /* Don't call these directly - use the macros below */
......
...@@ -43,9 +43,6 @@ ...@@ -43,9 +43,6 @@
#include "viruri.h" #include "viruri.h"
#include "dirname.h" #include "dirname.h"
#include "virbuffer.h" #include "virbuffer.h"
#if HAVE_SYS_SYSCALL_H
# include <sys/syscall.h>
#endif
#define VIR_FROM_THIS VIR_FROM_STORAGE #define VIR_FROM_THIS VIR_FROM_STORAGE
...@@ -1120,25 +1117,17 @@ virStorageFileResize(const char *path, ...@@ -1120,25 +1117,17 @@ virStorageFileResize(const char *path,
} }
if (pre_allocate) { if (pre_allocate) {
#if HAVE_POSIX_FALLOCATE if (safezero(fd, offset, len, true) != 0) {
if ((rc = posix_fallocate(fd, offset, len)) != 0) { if (errno == ENOSYS)
virReportSystemError(rc, virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
_("Failed to pre-allocate space for " _("preallocate is not supported on this "
"file '%s'"), path); "platform"));
goto cleanup; else
} virReportSystemError(errno,
#elif HAVE_SYS_SYSCALL_H && defined(SYS_fallocate) _("Failed to pre-allocate space for "
if (syscall(SYS_fallocate, fd, 0, offset, len) != 0) { "file '%s'"), path);
virReportSystemError(errno,
_("Failed to pre-allocate space for "
"file '%s'"), path);
goto cleanup; goto cleanup;
} }
#else
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
_("preallocate is not supported on this platform"));
goto cleanup;
#endif
} else { } else {
if (ftruncate(fd, capacity) < 0) { if (ftruncate(fd, capacity) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册