Blame 0046-virtiofsd-passthrough_ll-add-dirp_map-to-hide-lo_dir.patch

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