yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-passthrough_ll-add-dirp_map-to-hide-lo_dir.patch

ddf19c
From 474d0adafed4d73720d6413b2903d6c4b529e5e6 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:15 +0100
ddf19c
Subject: [PATCH 044/116] virtiofsd: passthrough_ll: add dirp_map to hide
ddf19c
 lo_dirp pointers
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-41-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93495
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 040/112] virtiofsd: passthrough_ll: add dirp_map to hide lo_dirp pointers
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
Do not expose lo_dirp pointers to clients.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit b39bce121bfad8757eec0ee41f14607b883935d3)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/passthrough_ll.c | 103 +++++++++++++++++++++++++++++----------
ddf19c
 1 file changed, 76 insertions(+), 27 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index a3ebf74..5f5a72f 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -56,27 +56,10 @@
ddf19c
 
ddf19c
 #include "passthrough_helpers.h"
ddf19c
 
ddf19c
-/*
ddf19c
- * We are re-using pointers to our `struct lo_inode`
ddf19c
- * elements as inodes. This means that we must be able to
ddf19c
- * store uintptr_t values in a fuse_ino_t variable. The following
ddf19c
- * incantation checks this condition at compile time.
ddf19c
- */
ddf19c
-#if defined(__GNUC__) &&                                      \
ddf19c
-    (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && \
ddf19c
-    !defined __cplusplus
ddf19c
-_Static_assert(sizeof(fuse_ino_t) >= sizeof(uintptr_t),
ddf19c
-               "fuse_ino_t too small to hold uintptr_t values!");
ddf19c
-#else
ddf19c
-struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct {
ddf19c
-    unsigned _uintptr_to_must_hold_fuse_ino_t
ddf19c
-        : ((sizeof(fuse_ino_t) >= sizeof(uintptr_t)) ? 1 : -1);
ddf19c
-};
ddf19c
-#endif
ddf19c
-
ddf19c
 struct lo_map_elem {
ddf19c
     union {
ddf19c
         struct lo_inode *inode;
ddf19c
+        struct lo_dirp *dirp;
ddf19c
         ssize_t freelist;
ddf19c
     };
ddf19c
     bool in_use;
ddf19c
@@ -123,6 +106,7 @@ struct lo_data {
ddf19c
     int timeout_set;
ddf19c
     struct lo_inode root; /* protected by lo->mutex */
ddf19c
     struct lo_map ino_map; /* protected by lo->mutex */
ddf19c
+    struct lo_map dirp_map; /* protected by lo->mutex */
ddf19c
 };
ddf19c
 
ddf19c
 static const struct fuse_opt lo_opts[] = {
ddf19c
@@ -253,6 +237,20 @@ static void lo_map_remove(struct lo_map *map, size_t key)
ddf19c
 }
ddf19c
 
ddf19c
 /* Assumes lo->mutex is held */
ddf19c
+static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp)
ddf19c
+{
ddf19c
+    struct lo_map_elem *elem;
ddf19c
+
ddf19c
+    elem = lo_map_alloc_elem(&lo_data(req)->dirp_map);
ddf19c
+    if (!elem) {
ddf19c
+        return -1;
ddf19c
+    }
ddf19c
+
ddf19c
+    elem->dirp = dirp;
ddf19c
+    return elem - lo_data(req)->dirp_map.elems;
ddf19c
+}
ddf19c
+
ddf19c
+/* Assumes lo->mutex is held */
ddf19c
 static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode)
ddf19c
 {
ddf19c
     struct lo_map_elem *elem;
ddf19c
@@ -861,9 +859,19 @@ struct lo_dirp {
ddf19c
     off_t offset;
ddf19c
 };
ddf19c
 
ddf19c
-static struct lo_dirp *lo_dirp(struct fuse_file_info *fi)
ddf19c
+static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi)
ddf19c
 {
ddf19c
-    return (struct lo_dirp *)(uintptr_t)fi->fh;
ddf19c
+    struct lo_data *lo = lo_data(req);
ddf19c
+    struct lo_map_elem *elem;
ddf19c
+
ddf19c
+    pthread_mutex_lock(&lo->mutex);
ddf19c
+    elem = lo_map_get(&lo->dirp_map, fi->fh);
ddf19c
+    pthread_mutex_unlock(&lo->mutex);
ddf19c
+    if (!elem) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    return elem->dirp;
ddf19c
 }
ddf19c
 
ddf19c
 static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
ddf19c
@@ -873,6 +881,7 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
     struct lo_dirp *d;
ddf19c
     int fd;
ddf19c
+    ssize_t fh;
ddf19c
 
ddf19c
     d = calloc(1, sizeof(struct lo_dirp));
ddf19c
     if (d == NULL) {
ddf19c
@@ -892,7 +901,14 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
ddf19c
     d->offset = 0;
ddf19c
     d->entry = NULL;
ddf19c
 
ddf19c
-    fi->fh = (uintptr_t)d;
ddf19c
+    pthread_mutex_lock(&lo->mutex);
ddf19c
+    fh = lo_add_dirp_mapping(req, d);
ddf19c
+    pthread_mutex_unlock(&lo->mutex);
ddf19c
+    if (fh == -1) {
ddf19c
+        goto out_err;
ddf19c
+    }
ddf19c
+
ddf19c
+    fi->fh = fh;
ddf19c
     if (lo->cache == CACHE_ALWAYS) {
ddf19c
         fi->keep_cache = 1;
ddf19c
     }
ddf19c
@@ -903,6 +919,9 @@ out_errno:
ddf19c
     error = errno;
ddf19c
 out_err:
ddf19c
     if (d) {
ddf19c
+        if (d->dp) {
ddf19c
+            closedir(d->dp);
ddf19c
+        }
ddf19c
         if (fd != -1) {
ddf19c
             close(fd);
ddf19c
         }
ddf19c
@@ -920,17 +939,21 @@ static int is_dot_or_dotdot(const char *name)
ddf19c
 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
ddf19c
                           off_t offset, struct fuse_file_info *fi, int plus)
ddf19c
 {
ddf19c
-    struct lo_dirp *d = lo_dirp(fi);
ddf19c
-    char *buf;
ddf19c
+    struct lo_dirp *d;
ddf19c
+    char *buf = NULL;
ddf19c
     char *p;
ddf19c
     size_t rem = size;
ddf19c
-    int err;
ddf19c
+    int err = ENOMEM;
ddf19c
 
ddf19c
     (void)ino;
ddf19c
 
ddf19c
+    d = lo_dirp(req, fi);
ddf19c
+    if (!d) {
ddf19c
+        goto error;
ddf19c
+    }
ddf19c
+
ddf19c
     buf = calloc(1, size);
ddf19c
     if (!buf) {
ddf19c
-        err = ENOMEM;
ddf19c
         goto error;
ddf19c
     }
ddf19c
     p = buf;
ddf19c
@@ -1028,8 +1051,21 @@ static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
ddf19c
 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
ddf19c
                           struct fuse_file_info *fi)
ddf19c
 {
ddf19c
-    struct lo_dirp *d = lo_dirp(fi);
ddf19c
+    struct lo_data *lo = lo_data(req);
ddf19c
+    struct lo_dirp *d;
ddf19c
+
ddf19c
     (void)ino;
ddf19c
+
ddf19c
+    d = lo_dirp(req, fi);
ddf19c
+    if (!d) {
ddf19c
+        fuse_reply_err(req, EBADF);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    pthread_mutex_lock(&lo->mutex);
ddf19c
+    lo_map_remove(&lo->dirp_map, fi->fh);
ddf19c
+    pthread_mutex_unlock(&lo->mutex);
ddf19c
+
ddf19c
     closedir(d->dp);
ddf19c
     free(d);
ddf19c
     fuse_reply_err(req, 0);
ddf19c
@@ -1081,8 +1117,18 @@ static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
ddf19c
                         struct fuse_file_info *fi)
ddf19c
 {
ddf19c
     int res;
ddf19c
-    int fd = dirfd(lo_dirp(fi)->dp);
ddf19c
+    struct lo_dirp *d;
ddf19c
+    int fd;
ddf19c
+
ddf19c
     (void)ino;
ddf19c
+
ddf19c
+    d = lo_dirp(req, fi);
ddf19c
+    if (!d) {
ddf19c
+        fuse_reply_err(req, EBADF);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    fd = dirfd(d->dp);
ddf19c
     if (datasync) {
ddf19c
         res = fdatasync(fd);
ddf19c
     } else {
ddf19c
@@ -1614,6 +1660,8 @@ int main(int argc, char *argv[])
ddf19c
     root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
ddf19c
     root_elem->inode = &lo.root;
ddf19c
 
ddf19c
+    lo_map_init(&lo.dirp_map);
ddf19c
+
ddf19c
     if (fuse_parse_cmdline(&args, &opts) != 0) {
ddf19c
         return 1;
ddf19c
     }
ddf19c
@@ -1710,6 +1758,7 @@ err_out2:
ddf19c
 err_out1:
ddf19c
     fuse_opt_free_args(&args);
ddf19c
 
ddf19c
+    lo_map_destroy(&lo.dirp_map);
ddf19c
     lo_map_destroy(&lo.ino_map);
ddf19c
 
ddf19c
     if (lo.root.fd >= 0) {
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c