f45498
From 596e74a63aec8cbb416d7421d4f6810ee4ae084b Mon Sep 17 00:00:00 2001
f45498
From: Jon Maloy <jmaloy@redhat.com>
f45498
Date: Wed, 10 Feb 2021 00:02:15 -0500
f45498
Subject: [PATCH 1/3] virtiofsd: extract lo_do_open() from lo_open()
f45498
f45498
RH-Author: Jon Maloy <jmaloy@redhat.com>
f45498
Message-id: <20210210000217.1561306-2-jmaloy@redhat.com>
f45498
Patchwork-id: 101025
f45498
O-Subject: [RHEL-8.3.0.z qemu-kvm PATCH 1/3] virtiofsd: extract lo_do_open() from lo_open()
f45498
Bugzilla: 1919109
f45498
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
f45498
RH-Acked-by: Greg Kurz <gkurz@redhat.com>
f45498
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
f45498
f45498
From: Stefan Hajnoczi <stefanha@redhat.com>
f45498
f45498
Both lo_open() and lo_create() have similar code to open a file. Extract
f45498
a common lo_do_open() function from lo_open() that will be used by
f45498
lo_create() in a later commit.
f45498
f45498
Since lo_do_open() does not otherwise need fuse_req_t req, convert
f45498
lo_add_fd_mapping() to use struct lo_data *lo instead.
f45498
f45498
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
f45498
Message-Id: <20210204150208.367837-2-stefanha@redhat.com>
f45498
Reviewed-by: Greg Kurz <groug@kaod.org>
f45498
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
f45498
f45498
(cherry-picked from commit 8afaaee976965b7fb90ec225a51d60f35c5f173c)
f45498
f45498
Conflict: update_open_flags() takes fewer arguments in this version
f45498
          than in upstream. Instead of applying commit e12a0edafeb
f45498
          ("virtiofsd: Add -o allow_direct_io|no_allow_direct_io
f45498
          options") we keep the old signature, since this seems to
f45498
          be an unrelated change.
f45498
f45498
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
f45498
Signed-off-by: Jon Maloy <jmaloy.redhat.com>
f45498
---
f45498
 tools/virtiofsd/passthrough_ll.c | 73 ++++++++++++++++++++------------
f45498
 1 file changed, 46 insertions(+), 27 deletions(-)
f45498
f45498
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
f45498
index f41a6b07c8..518ba11c47 100644
f45498
--- a/tools/virtiofsd/passthrough_ll.c
f45498
+++ b/tools/virtiofsd/passthrough_ll.c
f45498
@@ -439,17 +439,17 @@ static void lo_map_remove(struct lo_map *map, size_t key)
f45498
 }
f45498
 
f45498
 /* Assumes lo->mutex is held */
f45498
-static ssize_t lo_add_fd_mapping(fuse_req_t req, int fd)
f45498
+static ssize_t lo_add_fd_mapping(struct lo_data *lo, int fd)
f45498
 {
f45498
     struct lo_map_elem *elem;
f45498
 
f45498
-    elem = lo_map_alloc_elem(&lo_data(req)->fd_map);
f45498
+    elem = lo_map_alloc_elem(&lo->fd_map);
f45498
     if (!elem) {
f45498
         return -1;
f45498
     }
f45498
 
f45498
     elem->fd = fd;
f45498
-    return elem - lo_data(req)->fd_map.elems;
f45498
+    return elem - lo->fd_map.elems;
f45498
 }
f45498
 
f45498
 /* Assumes lo->mutex is held */
f45498
@@ -1712,6 +1712,38 @@ static void update_open_flags(int writeback, struct fuse_file_info *fi)
f45498
     fi->flags &= ~O_DIRECT;
f45498
 }
f45498
 
f45498
+static int lo_do_open(struct lo_data *lo, struct lo_inode *inode,
f45498
+                      struct fuse_file_info *fi)
f45498
+{
f45498
+    char buf[64];
f45498
+    ssize_t fh;
f45498
+    int fd;
f45498
+
f45498
+    update_open_flags(lo->writeback, fi);
f45498
+
f45498
+    sprintf(buf, "%i", inode->fd);
f45498
+    fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
f45498
+    if (fd == -1) {
f45498
+        return errno;
f45498
+    }
f45498
+
f45498
+    pthread_mutex_lock(&lo->mutex);
f45498
+    fh = lo_add_fd_mapping(lo, fd);
f45498
+    pthread_mutex_unlock(&lo->mutex);
f45498
+    if (fh == -1) {
f45498
+        close(fd);
f45498
+        return ENOMEM;
f45498
+    }
f45498
+
f45498
+    fi->fh = fh;
f45498
+    if (lo->cache == CACHE_NONE) {
f45498
+        fi->direct_io = 1;
f45498
+    } else if (lo->cache == CACHE_ALWAYS) {
f45498
+        fi->keep_cache = 1;
f45498
+    }
f45498
+    return 0;
f45498
+}
f45498
+
f45498
 static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
f45498
                       mode_t mode, struct fuse_file_info *fi)
f45498
 {
f45498
@@ -1752,7 +1784,7 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
f45498
         ssize_t fh;
f45498
 
f45498
         pthread_mutex_lock(&lo->mutex);
f45498
-        fh = lo_add_fd_mapping(req, fd);
f45498
+        fh = lo_add_fd_mapping(lo, fd);
f45498
         pthread_mutex_unlock(&lo->mutex);
f45498
         if (fh == -1) {
f45498
             close(fd);
f45498
@@ -1943,38 +1975,25 @@ static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
f45498
 
f45498
 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
f45498
 {
f45498
-    int fd;
f45498
-    ssize_t fh;
f45498
-    char buf[64];
f45498
     struct lo_data *lo = lo_data(req);
f45498
+    struct lo_inode *inode = lo_inode(req, ino);
f45498
+    int err;
f45498
 
f45498
     fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
f45498
              fi->flags);
f45498
 
f45498
-    update_open_flags(lo->writeback, fi);
f45498
-
f45498
-    sprintf(buf, "%i", lo_fd(req, ino));
f45498
-    fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
f45498
-    if (fd == -1) {
f45498
-        return (void)fuse_reply_err(req, errno);
f45498
-    }
f45498
-
f45498
-    pthread_mutex_lock(&lo->mutex);
f45498
-    fh = lo_add_fd_mapping(req, fd);
f45498
-    pthread_mutex_unlock(&lo->mutex);
f45498
-    if (fh == -1) {
f45498
-        close(fd);
f45498
-        fuse_reply_err(req, ENOMEM);
f45498
+    if (!inode) {
f45498
+        fuse_reply_err(req, EBADF);
f45498
         return;
f45498
     }
f45498
 
f45498
-    fi->fh = fh;
f45498
-    if (lo->cache == CACHE_NONE) {
f45498
-        fi->direct_io = 1;
f45498
-    } else if (lo->cache == CACHE_ALWAYS) {
f45498
-        fi->keep_cache = 1;
f45498
+    err = lo_do_open(lo, inode, fi);
f45498
+    lo_inode_put(lo, &inode;;
f45498
+    if (err) {
f45498
+        fuse_reply_err(req, err);
f45498
+    } else {
f45498
+        fuse_reply_open(req, fi);
f45498
     }
f45498
-    fuse_reply_open(req, fi);
f45498
 }
f45498
 
f45498
 static void lo_release(fuse_req_t req, fuse_ino_t ino,
f45498
-- 
f45498
2.18.2
f45498