Blame SOURCES/kvm-virtiofsd-Open-vhost-connection-instead-of-mounting.patch

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