yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-Pass-write-iov-s-all-the-way-through.patch

902636
From d5986c804f05070a07dfe702f7c66357daaa1ab6 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:20 +0100
902636
Subject: [PATCH 049/116] virtiofsd: Pass write iov's all the way through
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-46-dgilbert@redhat.com>
902636
Patchwork-id: 93497
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 045/112] virtiofsd: Pass write iov's all the way through
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
902636
Pass the write iov pointing to guest RAM all the way through rather
902636
than copying the data.
902636
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Reviewed-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit e17f7a580e2c599330ad3a6946be615ca2fe97d9)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/fuse_virtio.c | 79 +++++++++++++++++++++++++++++++++++++++----
902636
 1 file changed, 73 insertions(+), 6 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
902636
index fd588a4..872968f 100644
902636
--- a/tools/virtiofsd/fuse_virtio.c
902636
+++ b/tools/virtiofsd/fuse_virtio.c
902636
@@ -454,6 +454,10 @@ static void *fv_queue_thread(void *opaque)
902636
                  __func__, qi->qidx, (size_t)evalue, in_bytes, out_bytes);
902636
 
902636
         while (1) {
902636
+            bool allocated_bufv = false;
902636
+            struct fuse_bufvec bufv;
902636
+            struct fuse_bufvec *pbufv;
902636
+
902636
             /*
902636
              * An element contains one request and the space to send our
902636
              * response They're spread over multiple descriptors in a
902636
@@ -495,14 +499,76 @@ static void *fv_queue_thread(void *opaque)
902636
                          __func__, elem->index);
902636
                 assert(0); /* TODO */
902636
             }
902636
-            copy_from_iov(&fbuf, out_num, out_sg);
902636
-            fbuf.size = out_len;
902636
+            /* Copy just the first element and look at it */
902636
+            copy_from_iov(&fbuf, 1, out_sg);
902636
+
902636
+            if (out_num > 2 &&
902636
+                out_sg[0].iov_len == sizeof(struct fuse_in_header) &&
902636
+                ((struct fuse_in_header *)fbuf.mem)->opcode == FUSE_WRITE &&
902636
+                out_sg[1].iov_len == sizeof(struct fuse_write_in)) {
902636
+                /*
902636
+                 * For a write we don't actually need to copy the
902636
+                 * data, we can just do it straight out of guest memory
902636
+                 * but we must still copy the headers in case the guest
902636
+                 * was nasty and changed them while we were using them.
902636
+                 */
902636
+                fuse_log(FUSE_LOG_DEBUG, "%s: Write special case\n", __func__);
902636
+
902636
+                /* copy the fuse_write_in header after the fuse_in_header */
902636
+                fbuf.mem += out_sg->iov_len;
902636
+                copy_from_iov(&fbuf, 1, out_sg + 1);
902636
+                fbuf.mem -= out_sg->iov_len;
902636
+                fbuf.size = out_sg[0].iov_len + out_sg[1].iov_len;
902636
+
902636
+                /* Allocate the bufv, with space for the rest of the iov */
902636
+                allocated_bufv = true;
902636
+                pbufv = malloc(sizeof(struct fuse_bufvec) +
902636
+                               sizeof(struct fuse_buf) * (out_num - 2));
902636
+                if (!pbufv) {
902636
+                    vu_queue_unpop(dev, q, elem, 0);
902636
+                    free(elem);
902636
+                    fuse_log(FUSE_LOG_ERR, "%s: pbufv malloc failed\n",
902636
+                             __func__);
902636
+                    goto out;
902636
+                }
902636
+
902636
+                pbufv->count = 1;
902636
+                pbufv->buf[0] = fbuf;
902636
+
902636
+                size_t iovindex, pbufvindex;
902636
+                iovindex = 2; /* 2 headers, separate iovs */
902636
+                pbufvindex = 1; /* 2 headers, 1 fusebuf */
902636
+
902636
+                for (; iovindex < out_num; iovindex++, pbufvindex++) {
902636
+                    pbufv->count++;
902636
+                    pbufv->buf[pbufvindex].pos = ~0; /* Dummy */
902636
+                    pbufv->buf[pbufvindex].flags = 0;
902636
+                    pbufv->buf[pbufvindex].mem = out_sg[iovindex].iov_base;
902636
+                    pbufv->buf[pbufvindex].size = out_sg[iovindex].iov_len;
902636
+                }
902636
+            } else {
902636
+                /* Normal (non fast write) path */
902636
+
902636
+                /* Copy the rest of the buffer */
902636
+                fbuf.mem += out_sg->iov_len;
902636
+                copy_from_iov(&fbuf, out_num - 1, out_sg + 1);
902636
+                fbuf.mem -= out_sg->iov_len;
902636
+                fbuf.size = out_len;
902636
 
902636
-            /* TODO! Endianness of header */
902636
+                /* TODO! Endianness of header */
902636
 
902636
-            /* TODO: Add checks for fuse_session_exited */
902636
-            struct fuse_bufvec bufv = { .buf[0] = fbuf, .count = 1 };
902636
-            fuse_session_process_buf_int(se, &bufv, &ch);
902636
+                /* TODO: Add checks for fuse_session_exited */
902636
+                bufv.buf[0] = fbuf;
902636
+                bufv.count = 1;
902636
+                pbufv = &buf;;
902636
+            }
902636
+            pbufv->idx = 0;
902636
+            pbufv->off = 0;
902636
+            fuse_session_process_buf_int(se, pbufv, &ch);
902636
+
902636
+            if (allocated_bufv) {
902636
+                free(pbufv);
902636
+            }
902636
 
902636
             if (!qi->reply_sent) {
902636
                 fuse_log(FUSE_LOG_DEBUG, "%s: elem %d no reply sent\n",
902636
@@ -516,6 +582,7 @@ static void *fv_queue_thread(void *opaque)
902636
             elem = NULL;
902636
         }
902636
     }
902636
+out:
902636
     pthread_mutex_destroy(&ch.lock);
902636
     free(fbuf.mem);
902636
 
902636
-- 
902636
1.8.3.1
902636