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

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