Blame 0038-virtiofsd-add-fd-FDNUM-fd-passing-option.patch

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