dcavalca / rpms / qemu

Forked from rpms/qemu a year ago
Clone

Blame 0027-virtiofsd-Open-vhost-connection-instead-of-mounting.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:00:56 +0000
1d442b
Subject: [PATCH] virtiofsd: Open vhost connection instead of mounting
1d442b
MIME-Version: 1.0
1d442b
Content-Type: text/plain; charset=UTF-8
1d442b
Content-Transfer-Encoding: 8bit
1d442b
1d442b
When run with vhost-user options we conect to the QEMU instead
1d442b
via a socket.  Start this off by creating the socket.
1d442b
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
1d442b
Reviewed-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit d14bf584dd965821e80d14c16d9292a464b1ab85)
1d442b
---
1d442b
 tools/virtiofsd/fuse_i.h        |  7 ++-
1d442b
 tools/virtiofsd/fuse_lowlevel.c | 55 +++--------------------
1d442b
 tools/virtiofsd/fuse_virtio.c   | 79 +++++++++++++++++++++++++++++++++
1d442b
 tools/virtiofsd/fuse_virtio.h   | 23 ++++++++++
1d442b
 4 files changed, 114 insertions(+), 50 deletions(-)
1d442b
 create mode 100644 tools/virtiofsd/fuse_virtio.c
1d442b
 create mode 100644 tools/virtiofsd/fuse_virtio.h
1d442b
1d442b
diff --git a/tools/virtiofsd/fuse_i.h b/tools/virtiofsd/fuse_i.h
1d442b
index 26b1a7da88..82d6ac7115 100644
1d442b
--- a/tools/virtiofsd/fuse_i.h
1d442b
+++ b/tools/virtiofsd/fuse_i.h
1d442b
@@ -6,9 +6,10 @@
1d442b
  * See the file COPYING.LIB
1d442b
  */
1d442b
 
1d442b
-#define FUSE_USE_VERSION 31
1d442b
-
1d442b
+#ifndef FUSE_I_H
1d442b
+#define FUSE_I_H
1d442b
 
1d442b
+#define FUSE_USE_VERSION 31
1d442b
 #include "fuse.h"
1d442b
 #include "fuse_lowlevel.h"
1d442b
 
1d442b
@@ -101,3 +102,5 @@ void fuse_session_process_buf_int(struct fuse_session *se,
1d442b
 
1d442b
 /* room needed in buffer to accommodate header */
1d442b
 #define FUSE_BUFFER_HEADER_SIZE 0x1000
1d442b
+
1d442b
+#endif
1d442b
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
1d442b
index 17e8718283..5df124e64b 100644
1d442b
--- a/tools/virtiofsd/fuse_lowlevel.c
1d442b
+++ b/tools/virtiofsd/fuse_lowlevel.c
1d442b
@@ -14,6 +14,7 @@
1d442b
 #include "standard-headers/linux/fuse.h"
1d442b
 #include "fuse_misc.h"
1d442b
 #include "fuse_opt.h"
1d442b
+#include "fuse_virtio.h"
1d442b
 
1d442b
 #include <assert.h>
1d442b
 #include <errno.h>
1d442b
@@ -2202,6 +2203,11 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
1d442b
         goto out4;
1d442b
     }
1d442b
 
1d442b
+    if (!se->vu_socket_path) {
1d442b
+        fprintf(stderr, "fuse: missing -o vhost_user_socket option\n");
1d442b
+        goto out4;
1d442b
+    }
1d442b
+
1d442b
     se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
1d442b
 
1d442b
     list_init_req(&se->list);
1d442b
@@ -2224,54 +2230,7 @@ out1:
1d442b
 
1d442b
 int fuse_session_mount(struct fuse_session *se)
1d442b
 {
1d442b
-    int fd;
1d442b
-
1d442b
-    /*
1d442b
-     * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
1d442b
-     * would ensue.
1d442b
-     */
1d442b
-    do {
1d442b
-        fd = open("/dev/null", O_RDWR);
1d442b
-        if (fd > 2) {
1d442b
-            close(fd);
1d442b
-        }
1d442b
-    } while (fd >= 0 && fd <= 2);
1d442b
-
1d442b
-    /*
1d442b
-     * To allow FUSE daemons to run without privileges, the caller may open
1d442b
-     * /dev/fuse before launching the file system and pass on the file
1d442b
-     * descriptor by specifying /dev/fd/N as the mount point. Note that the
1d442b
-     * parent process takes care of performing the mount in this case.
1d442b
-     */
1d442b
-    fd = fuse_mnt_parse_fuse_fd(mountpoint);
1d442b
-    if (fd != -1) {
1d442b
-        if (fcntl(fd, F_GETFD) == -1) {
1d442b
-            fuse_log(FUSE_LOG_ERR, "fuse: Invalid file descriptor /dev/fd/%u\n",
1d442b
-                     fd);
1d442b
-            return -1;
1d442b
-        }
1d442b
-        se->fd = fd;
1d442b
-        return 0;
1d442b
-    }
1d442b
-
1d442b
-    /* Open channel */
1d442b
-    fd = fuse_kern_mount(mountpoint, se->mo);
1d442b
-    if (fd == -1) {
1d442b
-        return -1;
1d442b
-    }
1d442b
-    se->fd = fd;
1d442b
-
1d442b
-    /* Save mountpoint */
1d442b
-    se->mountpoint = strdup(mountpoint);
1d442b
-    if (se->mountpoint == NULL) {
1d442b
-        goto error_out;
1d442b
-    }
1d442b
-
1d442b
-    return 0;
1d442b
-
1d442b
-error_out:
1d442b
-    fuse_kern_unmount(mountpoint, fd);
1d442b
-    return -1;
1d442b
+    return virtio_session_mount(se);
1d442b
 }
1d442b
 
1d442b
 int fuse_session_fd(struct fuse_session *se)
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
1d442b
new file mode 100644
1d442b
index 0000000000..cbef6ffdda
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/fuse_virtio.c
1d442b
@@ -0,0 +1,79 @@
1d442b
+/*
1d442b
+ * virtio-fs glue for FUSE
1d442b
+ * Copyright (C) 2018 Red Hat, Inc. and/or its affiliates
1d442b
+ *
1d442b
+ * Authors:
1d442b
+ *   Dave Gilbert  <dgilbert@redhat.com>
1d442b
+ *
1d442b
+ * Implements the glue between libfuse and libvhost-user
1d442b
+ *
1d442b
+ * This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+ * See the file COPYING.LIB
1d442b
+ */
1d442b
+
1d442b
+#include "fuse_i.h"
1d442b
+#include "standard-headers/linux/fuse.h"
1d442b
+#include "fuse_misc.h"
1d442b
+#include "fuse_opt.h"
1d442b
+#include "fuse_virtio.h"
1d442b
+
1d442b
+#include <stdint.h>
1d442b
+#include <stdio.h>
1d442b
+#include <string.h>
1d442b
+#include <sys/socket.h>
1d442b
+#include <sys/types.h>
1d442b
+#include <sys/un.h>
1d442b
+#include <unistd.h>
1d442b
+
1d442b
+/* From spec */
1d442b
+struct virtio_fs_config {
1d442b
+    char tag[36];
1d442b
+    uint32_t num_queues;
1d442b
+};
1d442b
+
1d442b
+int virtio_session_mount(struct fuse_session *se)
1d442b
+{
1d442b
+    struct sockaddr_un un;
1d442b
+    mode_t old_umask;
1d442b
+
1d442b
+    if (strlen(se->vu_socket_path) >= sizeof(un.sun_path)) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "Socket path too long\n");
1d442b
+        return -1;
1d442b
+    }
1d442b
+
1d442b
+    se->fd = -1;
1d442b
+
1d442b
+    /*
1d442b
+     * Create the Unix socket to communicate with qemu
1d442b
+     * based on QEMU's vhost-user-bridge
1d442b
+     */
1d442b
+    unlink(se->vu_socket_path);
1d442b
+    strcpy(un.sun_path, se->vu_socket_path);
1d442b
+    size_t addr_len = sizeof(un);
1d442b
+
1d442b
+    int listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
1d442b
+    if (listen_sock == -1) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "vhost socket creation: %m\n");
1d442b
+        return -1;
1d442b
+    }
1d442b
+    un.sun_family = AF_UNIX;
1d442b
+
1d442b
+    /*
1d442b
+     * Unfortunately bind doesn't let you set the mask on the socket,
1d442b
+     * so set umask to 077 and restore it later.
1d442b
+     */
1d442b
+    old_umask = umask(0077);
1d442b
+    if (bind(listen_sock, (struct sockaddr *)&un, addr_len) == -1) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "vhost socket bind: %m\n");
1d442b
+        umask(old_umask);
1d442b
+        return -1;
1d442b
+    }
1d442b
+    umask(old_umask);
1d442b
+
1d442b
+    if (listen(listen_sock, 1) == -1) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "vhost socket listen: %m\n");
1d442b
+        return -1;
1d442b
+    }
1d442b
+
1d442b
+    return -1;
1d442b
+}
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.h b/tools/virtiofsd/fuse_virtio.h
1d442b
new file mode 100644
1d442b
index 0000000000..8f2edb69ca
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/fuse_virtio.h
1d442b
@@ -0,0 +1,23 @@
1d442b
+/*
1d442b
+ * virtio-fs glue for FUSE
1d442b
+ * Copyright (C) 2018 Red Hat, Inc. and/or its affiliates
1d442b
+ *
1d442b
+ * Authors:
1d442b
+ *   Dave Gilbert  <dgilbert@redhat.com>
1d442b
+ *
1d442b
+ * Implements the glue between libfuse and libvhost-user
1d442b
+ *
1d442b
+ * This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+ *  See the file COPYING.LIB
1d442b
+ */
1d442b
+
1d442b
+#ifndef FUSE_VIRTIO_H
1d442b
+#define FUSE_VIRTIO_H
1d442b
+
1d442b
+#include "fuse_i.h"
1d442b
+
1d442b
+struct fuse_session;
1d442b
+
1d442b
+int virtio_session_mount(struct fuse_session *se);
1d442b
+
1d442b
+#endif