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

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