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