yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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