[libvirt] [PATCH] iohelper: use saferead if later write with O_DIRECT

Nikolay Shirokovskiy posted 1 patch 6 years, 6 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/1506582407-834084-1-git-send-email-nshirokovskiy@virtuozzo.com
src/util/iohelper.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
[libvirt] [PATCH] iohelper: use saferead if later write with O_DIRECT
Posted by Nikolay Shirokovskiy 6 years, 6 months ago
One of the usecases of iohelper is to read from pipe and write
to file with O_DIRECT. As we read from pipe we can have partial
read and then we fail to write this data because output file
is open with O_DIRECT and buffer size is not aligned.
---
 src/util/iohelper.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/util/iohelper.c b/src/util/iohelper.c
index 5416d45..bb8a8dd 100644
--- a/src/util/iohelper.c
+++ b/src/util/iohelper.c
@@ -109,9 +109,21 @@ runIO(const char *path, int fd, int oflags)
     while (1) {
         ssize_t got;
 
-        if ((got = read(fdin, buf, buflen)) < 0) {
-            if (errno == EINTR)
+        /* If we read with O_DIRECT from file we can't use saferead as
+         * it can lead to unaligned read after reading last bytes.
+         * If we write with O_DIRECT use should use saferead so that
+         * writes will be aligned.
+         * In other cases using saferead reduces number of syscalls.
+         */
+        if (fdin == fd && direct) {
+            if ((got = read(fdin, buf, buflen)) < 0 &&
+                errno == EINTR)
                 continue;
+        } else {
+            got = saferead(fdin, buf, buflen);
+        }
+
+        if (got < 0) {
             virReportSystemError(errno, _("Unable to read %s"), fdinname);
             goto cleanup;
         }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] iohelper: use saferead if later write with O_DIRECT
Posted by Nikolay Shirokovskiy 6 years, 5 months ago
ping

On 28.09.2017 10:06, Nikolay Shirokovskiy wrote:
> One of the usecases of iohelper is to read from pipe and write
> to file with O_DIRECT. As we read from pipe we can have partial
> read and then we fail to write this data because output file
> is open with O_DIRECT and buffer size is not aligned.
> ---
>  src/util/iohelper.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/iohelper.c b/src/util/iohelper.c
> index 5416d45..bb8a8dd 100644
> --- a/src/util/iohelper.c
> +++ b/src/util/iohelper.c
> @@ -109,9 +109,21 @@ runIO(const char *path, int fd, int oflags)
>      while (1) {
>          ssize_t got;
>  
> -        if ((got = read(fdin, buf, buflen)) < 0) {
> -            if (errno == EINTR)
> +        /* If we read with O_DIRECT from file we can't use saferead as
> +         * it can lead to unaligned read after reading last bytes.
> +         * If we write with O_DIRECT use should use saferead so that
> +         * writes will be aligned.
> +         * In other cases using saferead reduces number of syscalls.
> +         */
> +        if (fdin == fd && direct) {
> +            if ((got = read(fdin, buf, buflen)) < 0 &&
> +                errno == EINTR)
>                  continue;
> +        } else {
> +            got = saferead(fdin, buf, buflen);
> +        }
> +
> +        if (got < 0) {
>              virReportSystemError(errno, _("Unable to read %s"), fdinname);
>              goto cleanup;
>          }
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] iohelper: use saferead if later write with O_DIRECT
Posted by Jiri Denemark 6 years, 5 months ago
On Thu, Sep 28, 2017 at 10:06:47 +0300, Nikolay Shirokovskiy wrote:
> One of the usecases of iohelper is to read from pipe and write
> to file with O_DIRECT. As we read from pipe we can have partial
> read and then we fail to write this data because output file
> is open with O_DIRECT and buffer size is not aligned.
> ---
>  src/util/iohelper.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)

ACK and pushed, thanks.

Jirka

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list