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

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