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

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