Blame SOURCES/kvm-virtiofsd-passthrough_ll-add-ino_map-to-hide-lo_inod.patch

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