peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0045-virtiofsd-passthrough_ll-add-ino_map-to-hide-lo_inod.patch

1d442b
From: Stefan Hajnoczi <stefanha@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:01:14 +0000
1d442b
Subject: [PATCH] virtiofsd: passthrough_ll: add ino_map to hide lo_inode
1d442b
 pointers
1d442b
1d442b
Do not expose lo_inode pointers to clients.
1d442b
1d442b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
1d442b
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit 92fb57b83cdbfc4bf53c0c46a3d0bcbc36e64126)
1d442b
---
1d442b
 tools/virtiofsd/passthrough_ll.c | 144 ++++++++++++++++++++++++-------
1d442b
 1 file changed, 114 insertions(+), 30 deletions(-)
1d442b
1d442b
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
1d442b
index e83a976587..a3ebf74eab 100644
1d442b
--- a/tools/virtiofsd/passthrough_ll.c
1d442b
+++ b/tools/virtiofsd/passthrough_ll.c
1d442b
@@ -57,8 +57,8 @@
1d442b
 #include "passthrough_helpers.h"
1d442b
 
1d442b
 /*
1d442b
- * We are re-using pointers to our `struct lo_inode` and `struct
1d442b
- * lo_dirp` elements as inodes. This means that we must be able to
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
@@ -76,7 +76,7 @@ struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct {
1d442b
 
1d442b
 struct lo_map_elem {
1d442b
     union {
1d442b
-        /* Element values will go here... */
1d442b
+        struct lo_inode *inode;
1d442b
         ssize_t freelist;
1d442b
     };
1d442b
     bool in_use;
1d442b
@@ -97,6 +97,7 @@ struct lo_inode {
1d442b
     ino_t ino;
1d442b
     dev_t dev;
1d442b
     uint64_t refcount; /* protected by lo->mutex */
1d442b
+    fuse_ino_t fuse_ino;
1d442b
 };
1d442b
 
1d442b
 struct lo_cred {
1d442b
@@ -121,6 +122,7 @@ struct lo_data {
1d442b
     int cache;
1d442b
     int timeout_set;
1d442b
     struct lo_inode root; /* protected by lo->mutex */
1d442b
+    struct lo_map ino_map; /* protected by lo->mutex */
1d442b
 };
1d442b
 
1d442b
 static const struct fuse_opt lo_opts[] = {
1d442b
@@ -145,14 +147,14 @@ static struct lo_data *lo_data(fuse_req_t req)
1d442b
     return (struct lo_data *)fuse_req_userdata(req);
1d442b
 }
1d442b
 
1d442b
-__attribute__((unused)) static void lo_map_init(struct lo_map *map)
1d442b
+static void lo_map_init(struct lo_map *map)
1d442b
 {
1d442b
     map->elems = NULL;
1d442b
     map->nelems = 0;
1d442b
     map->freelist = -1;
1d442b
 }
1d442b
 
1d442b
-__attribute__((unused)) static void lo_map_destroy(struct lo_map *map)
1d442b
+static void lo_map_destroy(struct lo_map *map)
1d442b
 {
1d442b
     free(map->elems);
1d442b
 }
1d442b
@@ -183,8 +185,7 @@ static int lo_map_grow(struct lo_map *map, size_t new_nelems)
1d442b
     return 1;
1d442b
 }
1d442b
 
1d442b
-__attribute__((unused)) static struct lo_map_elem *
1d442b
-lo_map_alloc_elem(struct lo_map *map)
1d442b
+static struct lo_map_elem *lo_map_alloc_elem(struct lo_map *map)
1d442b
 {
1d442b
     struct lo_map_elem *elem;
1d442b
 
1d442b
@@ -200,8 +201,7 @@ lo_map_alloc_elem(struct lo_map *map)
1d442b
     return elem;
1d442b
 }
1d442b
 
1d442b
-__attribute__((unused)) static struct lo_map_elem *
1d442b
-lo_map_reserve(struct lo_map *map, size_t key)
1d442b
+static struct lo_map_elem *lo_map_reserve(struct lo_map *map, size_t key)
1d442b
 {
1d442b
     ssize_t *prev;
1d442b
 
1d442b
@@ -222,8 +222,7 @@ lo_map_reserve(struct lo_map *map, size_t key)
1d442b
     return NULL;
1d442b
 }
1d442b
 
1d442b
-__attribute__((unused)) static struct lo_map_elem *
1d442b
-lo_map_get(struct lo_map *map, size_t key)
1d442b
+static struct lo_map_elem *lo_map_get(struct lo_map *map, size_t key)
1d442b
 {
1d442b
     if (key >= map->nelems) {
1d442b
         return NULL;
1d442b
@@ -234,8 +233,7 @@ lo_map_get(struct lo_map *map, size_t key)
1d442b
     return &map->elems[key];
1d442b
 }
1d442b
 
1d442b
-__attribute__((unused)) static void lo_map_remove(struct lo_map *map,
1d442b
-                                                  size_t key)
1d442b
+static void lo_map_remove(struct lo_map *map, size_t key)
1d442b
 {
1d442b
     struct lo_map_elem *elem;
1d442b
 
1d442b
@@ -254,18 +252,40 @@ __attribute__((unused)) static void lo_map_remove(struct lo_map *map,
1d442b
     map->freelist = key;
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
+    struct lo_map_elem *elem;
1d442b
+
1d442b
+    elem = lo_map_alloc_elem(&lo_data(req)->ino_map);
1d442b
+    if (!elem) {
1d442b
+        return -1;
1d442b
+    }
1d442b
+
1d442b
+    elem->inode = inode;
1d442b
+    return elem - lo_data(req)->ino_map.elems;
1d442b
+}
1d442b
+
1d442b
 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
1d442b
 {
1d442b
-    if (ino == FUSE_ROOT_ID) {
1d442b
-        return &lo_data(req)->root;
1d442b
-    } else {
1d442b
-        return (struct lo_inode *)(uintptr_t)ino;
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->ino_map, ino);
1d442b
+    pthread_mutex_unlock(&lo->mutex);
1d442b
+
1d442b
+    if (!elem) {
1d442b
+        return NULL;
1d442b
     }
1d442b
+
1d442b
+    return elem->inode;
1d442b
 }
1d442b
 
1d442b
 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
1d442b
 {
1d442b
-    return lo_inode(req, ino)->fd;
1d442b
+    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    return inode ? inode->fd : -1;
1d442b
 }
1d442b
 
1d442b
 static bool lo_debug(fuse_req_t req)
1d442b
@@ -337,10 +357,18 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
1d442b
 {
1d442b
     int saverr;
1d442b
     char procname[64];
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
-    int ifd = inode->fd;
1d442b
+    struct lo_inode *inode;
1d442b
+    int ifd;
1d442b
     int res;
1d442b
 
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
+    ifd = inode->fd;
1d442b
+
1d442b
     if (valid & FUSE_SET_ATTR_MODE) {
1d442b
         if (fi) {
1d442b
             res = fchmod(fi->fh, attr->st_mode);
1d442b
@@ -470,6 +498,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
1d442b
         inode->dev = e->attr.st_dev;
1d442b
 
1d442b
         pthread_mutex_lock(&lo->mutex);
1d442b
+        inode->fuse_ino = lo_add_inode_mapping(req, inode);
1d442b
         prev = &lo->root;
1d442b
         next = prev->next;
1d442b
         next->prev = inode;
1d442b
@@ -478,7 +507,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
1d442b
         prev->next = inode;
1d442b
         pthread_mutex_unlock(&lo->mutex);
1d442b
     }
1d442b
-    e->ino = (uintptr_t)inode;
1d442b
+    e->ino = inode->fuse_ino;
1d442b
 
1d442b
     if (lo_debug(req)) {
1d442b
         fuse_log(FUSE_LOG_DEBUG, "  %lli/%s -> %lli\n",
1d442b
@@ -582,10 +611,16 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
1d442b
 {
1d442b
     int res;
1d442b
     int saverr;
1d442b
-    struct lo_inode *dir = lo_inode(req, parent);
1d442b
+    struct lo_inode *dir;
1d442b
     struct fuse_entry_param e;
1d442b
     struct lo_cred old = {};
1d442b
 
1d442b
+    dir = lo_inode(req, parent);
1d442b
+    if (!dir) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
     saverr = ENOMEM;
1d442b
 
1d442b
     saverr = lo_change_cred(req, &old;;
1d442b
@@ -663,10 +698,16 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
1d442b
 {
1d442b
     int res;
1d442b
     struct lo_data *lo = lo_data(req);
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    struct lo_inode *inode;
1d442b
     struct fuse_entry_param e;
1d442b
     int saverr;
1d442b
 
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
     memset(&e, 0, sizeof(struct fuse_entry_param));
1d442b
     e.attr_timeout = lo->timeout;
1d442b
     e.entry_timeout = lo->timeout;
1d442b
@@ -684,7 +725,7 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
1d442b
     pthread_mutex_lock(&lo->mutex);
1d442b
     inode->refcount++;
1d442b
     pthread_mutex_unlock(&lo->mutex);
1d442b
-    e.ino = (uintptr_t)inode;
1d442b
+    e.ino = inode->fuse_ino;
1d442b
 
1d442b
     if (lo_debug(req)) {
1d442b
         fuse_log(FUSE_LOG_DEBUG, "  %lli/%s -> %lli\n",
1d442b
@@ -750,10 +791,10 @@ static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
1d442b
         next->prev = prev;
1d442b
         prev->next = next;
1d442b
 
1d442b
+        lo_map_remove(&lo->ino_map, inode->fuse_ino);
1d442b
         pthread_mutex_unlock(&lo->mutex);
1d442b
         close(inode->fd);
1d442b
         free(inode);
1d442b
-
1d442b
     } else {
1d442b
         pthread_mutex_unlock(&lo->mutex);
1d442b
     }
1d442b
@@ -762,7 +803,12 @@ static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
1d442b
 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1d442b
 {
1d442b
     struct lo_data *lo = lo_data(req);
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    struct lo_inode *inode;
1d442b
+
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        return;
1d442b
+    }
1d442b
 
1d442b
     if (lo_debug(req)) {
1d442b
         fuse_log(FUSE_LOG_DEBUG, "  forget %lli %lli -%lli\n",
1d442b
@@ -1244,10 +1290,16 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1d442b
 {
1d442b
     char *value = NULL;
1d442b
     char procname[64];
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    struct lo_inode *inode;
1d442b
     ssize_t ret;
1d442b
     int saverr;
1d442b
 
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
     saverr = ENOSYS;
1d442b
     if (!lo_data(req)->xattr) {
1d442b
         goto out;
1d442b
@@ -1306,10 +1358,16 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
1d442b
 {
1d442b
     char *value = NULL;
1d442b
     char procname[64];
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    struct lo_inode *inode;
1d442b
     ssize_t ret;
1d442b
     int saverr;
1d442b
 
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
     saverr = ENOSYS;
1d442b
     if (!lo_data(req)->xattr) {
1d442b
         goto out;
1d442b
@@ -1367,10 +1425,16 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1d442b
                         const char *value, size_t size, int flags)
1d442b
 {
1d442b
     char procname[64];
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    struct lo_inode *inode;
1d442b
     ssize_t ret;
1d442b
     int saverr;
1d442b
 
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
     saverr = ENOSYS;
1d442b
     if (!lo_data(req)->xattr) {
1d442b
         goto out;
1d442b
@@ -1400,10 +1464,16 @@ out:
1d442b
 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
1d442b
 {
1d442b
     char procname[64];
1d442b
-    struct lo_inode *inode = lo_inode(req, ino);
1d442b
+    struct lo_inode *inode;
1d442b
     ssize_t ret;
1d442b
     int saverr;
1d442b
 
1d442b
+    inode = lo_inode(req, ino);
1d442b
+    if (!inode) {
1d442b
+        fuse_reply_err(req, EBADF);
1d442b
+        return;
1d442b
+    }
1d442b
+
1d442b
     saverr = ENOSYS;
1d442b
     if (!lo_data(req)->xattr) {
1d442b
         goto out;
1d442b
@@ -1522,6 +1592,7 @@ int main(int argc, char *argv[])
1d442b
     struct fuse_session *se;
1d442b
     struct fuse_cmdline_opts opts;
1d442b
     struct lo_data lo = { .debug = 0, .writeback = 0 };
1d442b
+    struct lo_map_elem *root_elem;
1d442b
     int ret = -1;
1d442b
 
1d442b
     /* Don't mask creation mode, kernel already did that */
1d442b
@@ -1530,8 +1601,19 @@ int main(int argc, char *argv[])
1d442b
     pthread_mutex_init(&lo.mutex, NULL);
1d442b
     lo.root.next = lo.root.prev = &lo.root;
1d442b
     lo.root.fd = -1;
1d442b
+    lo.root.fuse_ino = FUSE_ROOT_ID;
1d442b
     lo.cache = CACHE_NORMAL;
1d442b
 
1d442b
+    /*
1d442b
+     * Set up the ino map like this:
1d442b
+     * [0] Reserved (will not be used)
1d442b
+     * [1] Root inode
1d442b
+     */
1d442b
+    lo_map_init(&lo.ino_map);
1d442b
+    lo_map_reserve(&lo.ino_map, 0)->in_use = false;
1d442b
+    root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
1d442b
+    root_elem->inode = &lo.root;
1d442b
+
1d442b
     if (fuse_parse_cmdline(&args, &opts) != 0) {
1d442b
         return 1;
1d442b
     }
1d442b
@@ -1628,6 +1710,8 @@ err_out2:
1d442b
 err_out1:
1d442b
     fuse_opt_free_args(&args);
1d442b
 
1d442b
+    lo_map_destroy(&lo.ino_map);
1d442b
+
1d442b
     if (lo.root.fd >= 0) {
1d442b
         close(lo.root.fd);
1d442b
     }