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