From 5646b7c5e14400920ac16f417ba201587ee0ce6c Mon Sep 17 00:00:00 2001 From: Simon Rowe Date: Mon, 23 Aug 2021 15:40:47 +0000 Subject: [PATCH] iohelper: skip lseek() and ftruncate() on block devices Signed-off-by: Simon Rowe Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- src/util/iohelper.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/util/iohelper.c b/src/util/iohelper.c index b8810d16d3..2c91bf4f93 100644 --- a/src/util/iohelper.c +++ b/src/util/iohelper.c @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include "virthread.h" #include "virfile.h" @@ -56,6 +58,8 @@ runIO(const char *path, int fd, int oflags) unsigned long long total = 0; bool direct = O_DIRECT && ((oflags & O_DIRECT) != 0); off_t end = 0; + struct stat sb; + bool isBlockDev = false; #if WITH_POSIX_MEMALIGN if (posix_memalign(&base, alignMask + 1, buflen)) @@ -67,6 +71,14 @@ runIO(const char *path, int fd, int oflags) buf = (char *) (((intptr_t) base + alignMask) & ~alignMask); #endif + if (fstat(fd, &sb) < 0) { + virReportSystemError(errno, + _("Unable to access file descriptor %d path %s"), + fd, path); + goto cleanup; + } + isBlockDev = S_ISBLK(sb.st_mode); + switch (oflags & O_ACCMODE) { case O_RDONLY: fdin = fd; @@ -75,7 +87,7 @@ runIO(const char *path, int fd, int oflags) fdoutname = "stdout"; /* To make the implementation simpler, we give up on any * attempt to use O_DIRECT in a non-trivial manner. */ - if (direct && ((end = lseek(fd, 0, SEEK_CUR)) != 0)) { + if (!isBlockDev && direct && ((end = lseek(fd, 0, SEEK_CUR)) != 0)) { virReportSystemError(end < 0 ? errno : EINVAL, "%s", _("O_DIRECT read needs entire seekable file")); goto cleanup; @@ -88,7 +100,7 @@ runIO(const char *path, int fd, int oflags) fdoutname = path; /* To make the implementation simpler, we give up on any * attempt to use O_DIRECT in a non-trivial manner. */ - if (direct && (end = lseek(fd, 0, SEEK_END)) != 0) { + if (!isBlockDev && direct && (end = lseek(fd, 0, SEEK_END)) != 0) { virReportSystemError(end < 0 ? errno : EINVAL, "%s", _("O_DIRECT write needs empty seekable file")); goto cleanup; @@ -140,7 +152,7 @@ runIO(const char *path, int fd, int oflags) goto cleanup; } - if (ftruncate(fd, total) < 0) { + if (!isBlockDev && ftruncate(fd, total) < 0) { virReportSystemError(errno, _("Unable to truncate %s"), fdoutname); goto cleanup; }