Blame 0034-virtiofsd-Send-replies-to-messages.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:01:03 +0000
1d442b
Subject: [PATCH] virtiofsd: Send replies to messages
1d442b
MIME-Version: 1.0
1d442b
Content-Type: text/plain; charset=UTF-8
1d442b
Content-Transfer-Encoding: 8bit
1d442b
1d442b
Route fuse out messages back through the same queue elements
1d442b
that had the command that triggered the request.
1d442b
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit df57ba919ec3edef9cc208d35685095e6e92713e)
1d442b
---
1d442b
 tools/virtiofsd/fuse_lowlevel.c |   4 ++
1d442b
 tools/virtiofsd/fuse_virtio.c   | 107 ++++++++++++++++++++++++++++++--
1d442b
 tools/virtiofsd/fuse_virtio.h   |   4 ++
1d442b
 3 files changed, 111 insertions(+), 4 deletions(-)
1d442b
1d442b
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
1d442b
index af09fa2b94..380d93bd01 100644
1d442b
--- a/tools/virtiofsd/fuse_lowlevel.c
1d442b
+++ b/tools/virtiofsd/fuse_lowlevel.c
1d442b
@@ -171,6 +171,10 @@ static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
1d442b
         }
1d442b
     }
1d442b
 
1d442b
+    if (fuse_lowlevel_is_virtio(se)) {
1d442b
+        return virtio_send_msg(se, ch, iov, count);
1d442b
+    }
1d442b
+
1d442b
     abort(); /* virtio should have taken it before here */
1d442b
     return 0;
1d442b
 }
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
1d442b
index 3841b20129..05d0e29f12 100644
1d442b
--- a/tools/virtiofsd/fuse_virtio.c
1d442b
+++ b/tools/virtiofsd/fuse_virtio.c
1d442b
@@ -41,6 +41,9 @@ struct fv_QueueInfo {
1d442b
     /* Our queue index, corresponds to array position */
1d442b
     int qidx;
1d442b
     int kick_fd;
1d442b
+
1d442b
+    /* The element for the command currently being processed */
1d442b
+    VuVirtqElement *qe;
1d442b
 };
1d442b
 
1d442b
 /*
1d442b
@@ -121,6 +124,105 @@ static void copy_from_iov(struct fuse_buf *buf, size_t out_num,
1d442b
     }
1d442b
 }
1d442b
 
1d442b
+/*
1d442b
+ * Copy from one iov to another, the given number of bytes
1d442b
+ * The caller must have checked sizes.
1d442b
+ */
1d442b
+static void copy_iov(struct iovec *src_iov, int src_count,
1d442b
+                     struct iovec *dst_iov, int dst_count, size_t to_copy)
1d442b
+{
1d442b
+    size_t dst_offset = 0;
1d442b
+    /* Outer loop copies 'src' elements */
1d442b
+    while (to_copy) {
1d442b
+        assert(src_count);
1d442b
+        size_t src_len = src_iov[0].iov_len;
1d442b
+        size_t src_offset = 0;
1d442b
+
1d442b
+        if (src_len > to_copy) {
1d442b
+            src_len = to_copy;
1d442b
+        }
1d442b
+        /* Inner loop copies contents of one 'src' to maybe multiple dst. */
1d442b
+        while (src_len) {
1d442b
+            assert(dst_count);
1d442b
+            size_t dst_len = dst_iov[0].iov_len - dst_offset;
1d442b
+            if (dst_len > src_len) {
1d442b
+                dst_len = src_len;
1d442b
+            }
1d442b
+
1d442b
+            memcpy(dst_iov[0].iov_base + dst_offset,
1d442b
+                   src_iov[0].iov_base + src_offset, dst_len);
1d442b
+            src_len -= dst_len;
1d442b
+            to_copy -= dst_len;
1d442b
+            src_offset += dst_len;
1d442b
+            dst_offset += dst_len;
1d442b
+
1d442b
+            assert(dst_offset <= dst_iov[0].iov_len);
1d442b
+            if (dst_offset == dst_iov[0].iov_len) {
1d442b
+                dst_offset = 0;
1d442b
+                dst_iov++;
1d442b
+                dst_count--;
1d442b
+            }
1d442b
+        }
1d442b
+        src_iov++;
1d442b
+        src_count--;
1d442b
+    }
1d442b
+}
1d442b
+
1d442b
+/*
1d442b
+ * Called back by ll whenever it wants to send a reply/message back
1d442b
+ * The 1st element of the iov starts with the fuse_out_header
1d442b
+ * 'unique'==0 means it's a notify message.
1d442b
+ */
1d442b
+int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch,
1d442b
+                    struct iovec *iov, int count)
1d442b
+{
1d442b
+    VuVirtqElement *elem;
1d442b
+    VuVirtq *q;
1d442b
+
1d442b
+    assert(count >= 1);
1d442b
+    assert(iov[0].iov_len >= sizeof(struct fuse_out_header));
1d442b
+
1d442b
+    struct fuse_out_header *out = iov[0].iov_base;
1d442b
+    /* TODO: Endianness! */
1d442b
+
1d442b
+    size_t tosend_len = iov_size(iov, count);
1d442b
+
1d442b
+    /* unique == 0 is notification, which we don't support */
1d442b
+    assert(out->unique);
1d442b
+    /* For virtio we always have ch */
1d442b
+    assert(ch);
1d442b
+    elem = ch->qi->qe;
1d442b
+    q = &ch->qi->virtio_dev->dev.vq[ch->qi->qidx];
1d442b
+
1d442b
+    /* The 'in' part of the elem is to qemu */
1d442b
+    unsigned int in_num = elem->in_num;
1d442b
+    struct iovec *in_sg = elem->in_sg;
1d442b
+    size_t in_len = iov_size(in_sg, in_num);
1d442b
+    fuse_log(FUSE_LOG_DEBUG, "%s: elem %d: with %d in desc of length %zd\n",
1d442b
+             __func__, elem->index, in_num, in_len);
1d442b
+
1d442b
+    /*
1d442b
+     * The elem should have room for a 'fuse_out_header' (out from fuse)
1d442b
+     * plus the data based on the len in the header.
1d442b
+     */
1d442b
+    if (in_len < sizeof(struct fuse_out_header)) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "%s: elem %d too short for out_header\n",
1d442b
+                 __func__, elem->index);
1d442b
+        return -E2BIG;
1d442b
+    }
1d442b
+    if (in_len < tosend_len) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "%s: elem %d too small for data len %zd\n",
1d442b
+                 __func__, elem->index, tosend_len);
1d442b
+        return -E2BIG;
1d442b
+    }
1d442b
+
1d442b
+    copy_iov(iov, count, in_sg, in_num, tosend_len);
1d442b
+    vu_queue_push(&se->virtio_dev->dev, q, elem, tosend_len);
1d442b
+    vu_queue_notify(&se->virtio_dev->dev, q);
1d442b
+
1d442b
+    return 0;
1d442b
+}
1d442b
+
1d442b
 /* Thread function for individual queues, created when a queue is 'started' */
1d442b
 static void *fv_queue_thread(void *opaque)
1d442b
 {
1d442b
@@ -226,13 +328,10 @@ static void *fv_queue_thread(void *opaque)
1d442b
 
1d442b
             /* TODO! Endianness of header */
1d442b
 
1d442b
-            /* TODO: Fixup fuse_send_msg */
1d442b
             /* TODO: Add checks for fuse_session_exited */
1d442b
             fuse_session_process_buf_int(se, &fbuf, &ch);
1d442b
 
1d442b
-            /* TODO: vu_queue_push(dev, q, elem, qi->write_count); */
1d442b
-            vu_queue_notify(dev, q);
1d442b
-
1d442b
+            qi->qe = NULL;
1d442b
             free(elem);
1d442b
             elem = NULL;
1d442b
         }
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.h b/tools/virtiofsd/fuse_virtio.h
1d442b
index 23026d6e4c..135a14875a 100644
1d442b
--- a/tools/virtiofsd/fuse_virtio.h
1d442b
+++ b/tools/virtiofsd/fuse_virtio.h
1d442b
@@ -22,4 +22,8 @@ int virtio_session_mount(struct fuse_session *se);
1d442b
 
1d442b
 int virtio_loop(struct fuse_session *se);
1d442b
 
1d442b
+
1d442b
+int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch,
1d442b
+                    struct iovec *iov, int count);
1d442b
+
1d442b
 #endif