902636
From f91a9bdc171142174110e9ff1716b611f6fb0039 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:07 +0100
902636
Subject: [PATCH 036/116] virtiofsd: add --fd=FDNUM fd passing option
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-33-dgilbert@redhat.com>
902636
Patchwork-id: 93487
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 032/112] virtiofsd: add --fd=FDNUM fd passing option
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: Stefan Hajnoczi <stefanha@redhat.com>
902636
902636
Although --socket-path=PATH is useful for manual invocations, management
902636
tools typically create the UNIX domain socket themselves and pass it to
902636
the vhost-user device backend.  This way QEMU can be launched
902636
immediately with a valid socket.  No waiting for the vhost-user device
902636
backend is required when fd passing is used.
902636
902636
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit cee8e35d4386e34bf79c3ca2aab7f7b1bb48cf8d)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/fuse_i.h        |  1 +
902636
 tools/virtiofsd/fuse_lowlevel.c | 16 ++++++++++++----
902636
 tools/virtiofsd/fuse_virtio.c   | 31 +++++++++++++++++++++++++------
902636
 3 files changed, 38 insertions(+), 10 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/fuse_i.h b/tools/virtiofsd/fuse_i.h
902636
index 1126723..45995f3 100644
902636
--- a/tools/virtiofsd/fuse_i.h
902636
+++ b/tools/virtiofsd/fuse_i.h
902636
@@ -68,6 +68,7 @@ struct fuse_session {
902636
     size_t bufsize;
902636
     int error;
902636
     char *vu_socket_path;
902636
+    int   vu_listen_fd;
902636
     int   vu_socketfd;
902636
     struct fv_VuDev *virtio_dev;
902636
 };
902636
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
902636
index 4f4684d..95f4db8 100644
902636
--- a/tools/virtiofsd/fuse_lowlevel.c
902636
+++ b/tools/virtiofsd/fuse_lowlevel.c
902636
@@ -2130,6 +2130,7 @@ static const struct fuse_opt fuse_ll_opts[] = {
902636
     LL_OPTION("--debug", debug, 1),
902636
     LL_OPTION("allow_root", deny_others, 1),
902636
     LL_OPTION("--socket-path=%s", vu_socket_path, 0),
902636
+    LL_OPTION("--fd=%d", vu_listen_fd, 0),
902636
     FUSE_OPT_END
902636
 };
902636
 
902636
@@ -2147,7 +2148,8 @@ void fuse_lowlevel_help(void)
902636
      */
902636
     printf(
902636
         "    -o allow_root              allow access by root\n"
902636
-        "    --socket-path=PATH         path for the vhost-user socket\n");
902636
+        "    --socket-path=PATH         path for the vhost-user socket\n"
902636
+        "    --fd=FDNUM                 fd number of vhost-user socket\n");
902636
 }
902636
 
902636
 void fuse_session_destroy(struct fuse_session *se)
902636
@@ -2191,6 +2193,7 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
902636
         goto out1;
902636
     }
902636
     se->fd = -1;
902636
+    se->vu_listen_fd = -1;
902636
     se->conn.max_write = UINT_MAX;
902636
     se->conn.max_readahead = UINT_MAX;
902636
 
902636
@@ -2212,8 +2215,13 @@ 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
+    if (!se->vu_socket_path && se->vu_listen_fd < 0) {
902636
+        fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n");
902636
+        goto out4;
902636
+    }
902636
+    if (se->vu_socket_path && se->vu_listen_fd >= 0) {
902636
+        fuse_log(FUSE_LOG_ERR,
902636
+                 "fuse: --socket-path and --fd cannot be given together\n");
902636
         goto out4;
902636
     }
902636
 
902636
@@ -2253,7 +2261,7 @@ void fuse_session_unmount(struct fuse_session *se)
902636
 
902636
 int fuse_lowlevel_is_virtio(struct fuse_session *se)
902636
 {
902636
-    return se->vu_socket_path != NULL;
902636
+    return !!se->virtio_dev;
902636
 }
902636
 
902636
 #ifdef linux
902636
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
902636
index 7e2711b..635f877 100644
902636
--- a/tools/virtiofsd/fuse_virtio.c
902636
+++ b/tools/virtiofsd/fuse_virtio.c
902636
@@ -638,18 +638,21 @@ int virtio_loop(struct fuse_session *se)
902636
     return 0;
902636
 }
902636
 
902636
-int virtio_session_mount(struct fuse_session *se)
902636
+static int fv_create_listen_socket(struct fuse_session *se)
902636
 {
902636
     struct sockaddr_un un;
902636
     mode_t old_umask;
902636
 
902636
+    /* Nothing to do if fd is already initialized */
902636
+    if (se->vu_listen_fd >= 0) {
902636
+        return 0;
902636
+    }
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
@@ -682,15 +685,31 @@ int virtio_session_mount(struct fuse_session *se)
902636
         return -1;
902636
     }
902636
 
902636
+    se->vu_listen_fd = listen_sock;
902636
+    return 0;
902636
+}
902636
+
902636
+int virtio_session_mount(struct fuse_session *se)
902636
+{
902636
+    int ret;
902636
+
902636
+    ret = fv_create_listen_socket(se);
902636
+    if (ret < 0) {
902636
+        return ret;
902636
+    }
902636
+
902636
+    se->fd = -1;
902636
+
902636
     fuse_log(FUSE_LOG_INFO, "%s: Waiting for vhost-user socket connection...\n",
902636
              __func__);
902636
-    int data_sock = accept(listen_sock, NULL, NULL);
902636
+    int data_sock = accept(se->vu_listen_fd, NULL, NULL);
902636
     if (data_sock == -1) {
902636
         fuse_log(FUSE_LOG_ERR, "vhost socket accept: %m\n");
902636
-        close(listen_sock);
902636
+        close(se->vu_listen_fd);
902636
         return -1;
902636
     }
902636
-    close(listen_sock);
902636
+    close(se->vu_listen_fd);
902636
+    se->vu_listen_fd = -1;
902636
     fuse_log(FUSE_LOG_INFO, "%s: Received vhost-user socket connection\n",
902636
              __func__);
902636
 
902636
-- 
902636
1.8.3.1
902636