<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="margin:0px;font-size:12pt;caret-color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">I can change to doing this if it preferred,</span>
<div style="margin:0px;font-size:12pt;caret-color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">
<br>
</div>
<span style="margin:0px;font-size:12pt;caret-color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">Simon</span><br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Michal Prívozník <mprivozn@redhat.com><br>
<b>Sent:</b> 20 August 2021 16:09<br>
<b>To:</b> Simon Rowe <simon.rowe@nutanix.com>; libvir-list@redhat.com <libvir-list@redhat.com><br>
<b>Subject:</b> Re: [PATCH 1/2] iohelper: skip lseek() and ftruncate() on block devices</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">On 8/20/21 10:39 AM, Simon Rowe wrote:<br>
> Signed-off-by: Simon Rowe <simon.rowe@nutanix.com><br>
> ---<br>
>  src/util/iohelper.c | 10 ++++++++--<br>
>  1 file changed, 8 insertions(+), 2 deletions(-)<br>
> <br>
> diff --git a/src/util/iohelper.c b/src/util/iohelper.c<br>
> index b8810d16d3..e6eb178fde 100644<br>
> --- a/src/util/iohelper.c<br>
> +++ b/src/util/iohelper.c<br>
> @@ -28,6 +28,8 @@<br>
>  #include <unistd.h><br>
>  #include <fcntl.h><br>
>  #include <stdlib.h><br>
> +#include <sys/types.h><br>
> +#include <sys/stat.h><br>
>  <br>
>  #include "virthread.h"<br>
>  #include "virfile.h"<br>
> @@ -56,6 +58,8 @@ runIO(const char *path, int fd, int oflags)<br>
>      unsigned long long total = 0;<br>
>      bool direct = O_DIRECT && ((oflags & O_DIRECT) != 0);<br>
>      off_t end = 0;<br>
> +    struct stat sb;<br>
> +    bool isBlockDev = false;<br>
>  <br>
>  #if WITH_POSIX_MEMALIGN<br>
>      if (posix_memalign(&base, alignMask + 1, buflen))<br>
> @@ -86,9 +90,11 @@ runIO(const char *path, int fd, int oflags)<br>
>          fdinname = "stdin";<br>
>          fdout = fd;<br>
>          fdoutname = path;<br>
> +        if (fstat(fd, &sb) == 0)<br>
> +            isBlockDev = S_ISBLK(sb.st_mode);<br>
>          /* To make the implementation simpler, we give up on any<br>
>           * attempt to use O_DIRECT in a non-trivial manner.  */<br>
> -        if (direct && (end = lseek(fd, 0, SEEK_END)) != 0) {<br>
> +        if (!isBlockDev && direct && (end = lseek(fd, 0, SEEK_END)) != 0) {<br>
>              virReportSystemError(end < 0 ? errno : EINVAL, "%s",<br>
>                                   _("O_DIRECT write needs empty seekable file"));<br>
>              goto cleanup;<br>
> @@ -140,7 +146,7 @@ runIO(const char *path, int fd, int oflags)<br>
>                  goto cleanup;<br>
>              }<br>
>  <br>
> -            if (ftruncate(fd, total) < 0) {<br>
> +            if (!isBlockDev && ftruncate(fd, total) < 0) {<br>
>                  virReportSystemError(errno, _("Unable to truncate %s"), fdoutname);<br>
>                  goto cleanup;<br>
>              }<br>
> <br>
<br>
IIUC, O_DIRECT is no good for block devices. I wonder whether the caller<br>
(doCoreDump()) should learn whether the path is a block device and don't<br>
set O_DIRECT flag instead of changing iohelper.<br>
<br>
Michal<br>
<br>
</div>
</span></font></div>
</body>
</html>