<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);">
That's fine with me, thanks</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Simon</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> 24 August 2021 14:37<br>
<b>To:</b> Simon Rowe <simon.rowe@nutanix.com>; libvir-list@redhat.com <libvir-list@redhat.com><br>
<b>Subject:</b> Re: [PATCHv2 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/23/21 5:40 PM, 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>
<br>
I think we can check for this before this switch() and report error if<br>
fstat() fails. That way we can ..<br>
<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>
<br>
.. do this change for O_RDONLY case too. IOW, I suggest squashing this<br>
in:<br>
<br>
<br>
diff --git i/src/util/iohelper.c w/src/util/iohelper.c<br>
index e6eb178fde..2c91bf4f93 100644<br>
--- i/src/util/iohelper.c<br>
+++ w/src/util/iohelper.c<br>
@@ -71,6 +71,14 @@ runIO(const char *path, int fd, int oflags)<br>
     buf = (char *) (((intptr_t) base + alignMask) & ~alignMask);<br>
 #endif<br>
 <br>
+    if (fstat(fd, &sb) < 0) {<br>
+        virReportSystemError(errno,<br>
+                             _("Unable to access file descriptor %d path %s"),<br>
+                             fd, path);<br>
+        goto cleanup;<br>
+    }<br>
+    isBlockDev = S_ISBLK(sb.st_mode);<br>
+<br>
     switch (oflags & O_ACCMODE) {<br>
     case O_RDONLY:<br>
         fdin = fd;<br>
@@ -79,7 +87,7 @@ runIO(const char *path, int fd, int oflags)<br>
         fdoutname = "stdout";<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_CUR)) != 0)) {<br>
+        if (!isBlockDev && direct && ((end = lseek(fd, 0, SEEK_CUR)) != 0)) {<br>
             virReportSystemError(end < 0 ? errno : EINVAL, "%s",<br>
                                  _("O_DIRECT read needs entire seekable file"));<br>
             goto cleanup;<br>
@@ -90,8 +98,6 @@ 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 (!isBlockDev && direct && (end = lseek(fd, 0, SEEK_END)) != 0) {<br>
<br>
<br>
No need to resend, just let me know if you're okay with the suggested<br>
change and I'll squash it in before pushing.<br>
<br>
Michal<br>
<br>
</div>
</span></font></div>
</body>
</html>