Blame 0051-virtiofsd-Pass-write-iov-s-all-the-way-through.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:01:20 +0000
1d442b
Subject: [PATCH] virtiofsd: Pass write iov's all the way through
1d442b
1d442b
Pass the write iov pointing to guest RAM all the way through rather
1d442b
than copying the data.
1d442b
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
Reviewed-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit e17f7a580e2c599330ad3a6946be615ca2fe97d9)
1d442b
---
1d442b
 tools/virtiofsd/fuse_virtio.c | 79 ++++++++++++++++++++++++++++++++---
1d442b
 1 file changed, 73 insertions(+), 6 deletions(-)
1d442b
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
1d442b
index fd588a4829..872968f2c8 100644
1d442b
--- a/tools/virtiofsd/fuse_virtio.c
1d442b
+++ b/tools/virtiofsd/fuse_virtio.c
1d442b
@@ -454,6 +454,10 @@ static void *fv_queue_thread(void *opaque)
1d442b
                  __func__, qi->qidx, (size_t)evalue, in_bytes, out_bytes);
1d442b
 
1d442b
         while (1) {
1d442b
+            bool allocated_bufv = false;
1d442b
+            struct fuse_bufvec bufv;
1d442b
+            struct fuse_bufvec *pbufv;
1d442b
+
1d442b
             /*
1d442b
              * An element contains one request and the space to send our
1d442b
              * response They're spread over multiple descriptors in a
1d442b
@@ -495,14 +499,76 @@ static void *fv_queue_thread(void *opaque)
1d442b
                          __func__, elem->index);
1d442b
                 assert(0); /* TODO */
1d442b
             }
1d442b
-            copy_from_iov(&fbuf, out_num, out_sg);
1d442b
-            fbuf.size = out_len;
1d442b
+            /* Copy just the first element and look at it */
1d442b
+            copy_from_iov(&fbuf, 1, out_sg);
1d442b
+
1d442b
+            if (out_num > 2 &&
1d442b
+                out_sg[0].iov_len == sizeof(struct fuse_in_header) &&
1d442b
+                ((struct fuse_in_header *)fbuf.mem)->opcode == FUSE_WRITE &&
1d442b
+                out_sg[1].iov_len == sizeof(struct fuse_write_in)) {
1d442b
+                /*
1d442b
+                 * For a write we don't actually need to copy the
1d442b
+                 * data, we can just do it straight out of guest memory
1d442b
+                 * but we must still copy the headers in case the guest
1d442b
+                 * was nasty and changed them while we were using them.
1d442b
+                 */
1d442b
+                fuse_log(FUSE_LOG_DEBUG, "%s: Write special case\n", __func__);
1d442b
+
1d442b
+                /* copy the fuse_write_in header after the fuse_in_header */
1d442b
+                fbuf.mem += out_sg->iov_len;
1d442b
+                copy_from_iov(&fbuf, 1, out_sg + 1);
1d442b
+                fbuf.mem -= out_sg->iov_len;
1d442b
+                fbuf.size = out_sg[0].iov_len + out_sg[1].iov_len;
1d442b
+
1d442b
+                /* Allocate the bufv, with space for the rest of the iov */
1d442b
+                allocated_bufv = true;
1d442b
+                pbufv = malloc(sizeof(struct fuse_bufvec) +
1d442b
+                               sizeof(struct fuse_buf) * (out_num - 2));
1d442b
+                if (!pbufv) {
1d442b
+                    vu_queue_unpop(dev, q, elem, 0);
1d442b
+                    free(elem);
1d442b
+                    fuse_log(FUSE_LOG_ERR, "%s: pbufv malloc failed\n",
1d442b
+                             __func__);
1d442b
+                    goto out;
1d442b
+                }
1d442b
+
1d442b
+                pbufv->count = 1;
1d442b
+                pbufv->buf[0] = fbuf;
1d442b
+
1d442b
+                size_t iovindex, pbufvindex;
1d442b
+                iovindex = 2; /* 2 headers, separate iovs */
1d442b
+                pbufvindex = 1; /* 2 headers, 1 fusebuf */
1d442b
+
1d442b
+                for (; iovindex < out_num; iovindex++, pbufvindex++) {
1d442b
+                    pbufv->count++;
1d442b
+                    pbufv->buf[pbufvindex].pos = ~0; /* Dummy */
1d442b
+                    pbufv->buf[pbufvindex].flags = 0;
1d442b
+                    pbufv->buf[pbufvindex].mem = out_sg[iovindex].iov_base;
1d442b
+                    pbufv->buf[pbufvindex].size = out_sg[iovindex].iov_len;
1d442b
+                }
1d442b
+            } else {
1d442b
+                /* Normal (non fast write) path */
1d442b
+
1d442b
+                /* Copy the rest of the buffer */
1d442b
+                fbuf.mem += out_sg->iov_len;
1d442b
+                copy_from_iov(&fbuf, out_num - 1, out_sg + 1);
1d442b
+                fbuf.mem -= out_sg->iov_len;
1d442b
+                fbuf.size = out_len;
1d442b
 
1d442b
-            /* TODO! Endianness of header */
1d442b
+                /* TODO! Endianness of header */
1d442b
 
1d442b
-            /* TODO: Add checks for fuse_session_exited */
1d442b
-            struct fuse_bufvec bufv = { .buf[0] = fbuf, .count = 1 };
1d442b
-            fuse_session_process_buf_int(se, &bufv, &ch);
1d442b
+                /* TODO: Add checks for fuse_session_exited */
1d442b
+                bufv.buf[0] = fbuf;
1d442b
+                bufv.count = 1;
1d442b
+                pbufv = &buf;;
1d442b
+            }
1d442b
+            pbufv->idx = 0;
1d442b
+            pbufv->off = 0;
1d442b
+            fuse_session_process_buf_int(se, pbufv, &ch);
1d442b
+
1d442b
+            if (allocated_bufv) {
1d442b
+                free(pbufv);
1d442b
+            }
1d442b
 
1d442b
             if (!qi->reply_sent) {
1d442b
                 fuse_log(FUSE_LOG_DEBUG, "%s: elem %d no reply sent\n",
1d442b
@@ -516,6 +582,7 @@ static void *fv_queue_thread(void *opaque)
1d442b
             elem = NULL;
1d442b
         }
1d442b
     }
1d442b
+out:
1d442b
     pthread_mutex_destroy(&ch.lock);
1d442b
     free(fbuf.mem);
1d442b