cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
902636
From 44f4434b1305f6ff47b4f63fafcf39bcea9e4ceb Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:52 +0100
902636
Subject: [PATCH 081/116] virtiofsd: passthrough_ll: use hashtable
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-78-dgilbert@redhat.com>
902636
Patchwork-id: 93528
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 077/112] virtiofsd: passthrough_ll: use hashtable
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: Miklos Szeredi <mszeredi@redhat.com>
902636
902636
Improve performance of inode lookup by using a hash table.
902636
902636
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Signed-off-by: Liu Bo <bo.liu@linux.alibaba.com>
902636
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit bfc50a6e06b10b2f9dbaf6c1a89dd523322e016f)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/passthrough_ll.c | 81 ++++++++++++++++++++++------------------
902636
 1 file changed, 45 insertions(+), 36 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index b40f287..b176a31 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -84,13 +84,15 @@ struct lo_map {
902636
     ssize_t freelist;
902636
 };
902636
 
902636
+struct lo_key {
902636
+    ino_t ino;
902636
+    dev_t dev;
902636
+};
902636
+
902636
 struct lo_inode {
902636
-    struct lo_inode *next; /* protected by lo->mutex */
902636
-    struct lo_inode *prev; /* protected by lo->mutex */
902636
     int fd;
902636
     bool is_symlink;
902636
-    ino_t ino;
902636
-    dev_t dev;
902636
+    struct lo_key key;
902636
     uint64_t refcount; /* protected by lo->mutex */
902636
     fuse_ino_t fuse_ino;
902636
 };
902636
@@ -119,7 +121,8 @@ struct lo_data {
902636
     int timeout_set;
902636
     int readdirplus_set;
902636
     int readdirplus_clear;
902636
-    struct lo_inode root; /* protected by lo->mutex */
902636
+    struct lo_inode root;
902636
+    GHashTable *inodes; /* protected by lo->mutex */
902636
     struct lo_map ino_map; /* protected by lo->mutex */
902636
     struct lo_map dirp_map; /* protected by lo->mutex */
902636
     struct lo_map fd_map; /* protected by lo->mutex */
902636
@@ -573,7 +576,7 @@ retry:
902636
         }
902636
         goto fail_unref;
902636
     }
902636
-    if (stat.st_dev != inode->dev || stat.st_ino != inode->ino) {
902636
+    if (stat.st_dev != inode->key.dev || stat.st_ino != inode->key.ino) {
902636
         if (!retries) {
902636
             fuse_log(FUSE_LOG_WARNING,
902636
                      "%s: failed to match last\n", __func__);
902636
@@ -753,19 +756,20 @@ out_err:
902636
 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
902636
 {
902636
     struct lo_inode *p;
902636
-    struct lo_inode *ret = NULL;
902636
+    struct lo_key key = {
902636
+        .ino = st->st_ino,
902636
+        .dev = st->st_dev,
902636
+    };
902636
 
902636
     pthread_mutex_lock(&lo->mutex);
902636
-    for (p = lo->root.next; p != &lo->root; p = p->next) {
902636
-        if (p->ino == st->st_ino && p->dev == st->st_dev) {
902636
-            assert(p->refcount > 0);
902636
-            ret = p;
902636
-            ret->refcount++;
902636
-            break;
902636
-        }
902636
+    p = g_hash_table_lookup(lo->inodes, &key);
902636
+    if (p) {
902636
+        assert(p->refcount > 0);
902636
+        p->refcount++;
902636
     }
902636
     pthread_mutex_unlock(&lo->mutex);
902636
-    return ret;
902636
+
902636
+    return p;
902636
 }
902636
 
902636
 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
@@ -810,8 +814,6 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
         close(newfd);
902636
         newfd = -1;
902636
     } else {
902636
-        struct lo_inode *prev, *next;
902636
-
902636
         saverr = ENOMEM;
902636
         inode = calloc(1, sizeof(struct lo_inode));
902636
         if (!inode) {
902636
@@ -822,17 +824,12 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
         inode->refcount = 1;
902636
         inode->fd = newfd;
902636
         newfd = -1;
902636
-        inode->ino = e->attr.st_ino;
902636
-        inode->dev = e->attr.st_dev;
902636
+        inode->key.ino = e->attr.st_ino;
902636
+        inode->key.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
-        inode->next = next;
902636
-        inode->prev = prev;
902636
-        prev->next = inode;
902636
+        g_hash_table_insert(lo->inodes, &inode->key, inode);
902636
         pthread_mutex_unlock(&lo->mutex);
902636
     }
902636
     e->ino = inode->fuse_ino;
902636
@@ -1162,14 +1159,8 @@ static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
902636
     assert(inode->refcount >= n);
902636
     inode->refcount -= n;
902636
     if (!inode->refcount) {
902636
-        struct lo_inode *prev, *next;
902636
-
902636
-        prev = inode->prev;
902636
-        next = inode->next;
902636
-        next->prev = prev;
902636
-        prev->next = next;
902636
-
902636
         lo_map_remove(&lo->ino_map, inode->fuse_ino);
902636
+        g_hash_table_remove(lo->inodes, &inode->key);
902636
         pthread_mutex_unlock(&lo->mutex);
902636
         close(inode->fd);
902636
         free(inode);
902636
@@ -1369,7 +1360,7 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
902636
 
902636
         /* Hide root's parent directory */
902636
         if (dinode == &lo->root && strcmp(name, "..") == 0) {
902636
-            e.attr.st_ino = lo->root.ino;
902636
+            e.attr.st_ino = lo->root.key.ino;
902636
             e.attr.st_mode = DT_DIR << 12;
902636
         }
902636
 
902636
@@ -2370,11 +2361,26 @@ static void setup_root(struct lo_data *lo, struct lo_inode *root)
902636
 
902636
     root->is_symlink = false;
902636
     root->fd = fd;
902636
-    root->ino = stat.st_ino;
902636
-    root->dev = stat.st_dev;
902636
+    root->key.ino = stat.st_ino;
902636
+    root->key.dev = stat.st_dev;
902636
     root->refcount = 2;
902636
 }
902636
 
902636
+static guint lo_key_hash(gconstpointer key)
902636
+{
902636
+    const struct lo_key *lkey = key;
902636
+
902636
+    return (guint)lkey->ino + (guint)lkey->dev;
902636
+}
902636
+
902636
+static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
902636
+{
902636
+    const struct lo_key *la = a;
902636
+    const struct lo_key *lb = b;
902636
+
902636
+    return la->ino == lb->ino && la->dev == lb->dev;
902636
+}
902636
+
902636
 int main(int argc, char *argv[])
902636
 {
902636
     struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
902636
@@ -2392,7 +2398,7 @@ int main(int argc, char *argv[])
902636
     umask(0);
902636
 
902636
     pthread_mutex_init(&lo.mutex, NULL);
902636
-    lo.root.next = lo.root.prev = &lo.root;
902636
+    lo.inodes = g_hash_table_new(lo_key_hash, lo_key_equal);
902636
     lo.root.fd = -1;
902636
     lo.root.fuse_ino = FUSE_ROOT_ID;
902636
     lo.cache = CACHE_AUTO;
902636
@@ -2522,6 +2528,9 @@ err_out2:
902636
 err_out1:
902636
     fuse_opt_free_args(&args);
902636
 
902636
+    if (lo.inodes) {
902636
+        g_hash_table_destroy(lo.inodes);
902636
+    }
902636
     lo_map_destroy(&lo.fd_map);
902636
     lo_map_destroy(&lo.dirp_map);
902636
     lo_map_destroy(&lo.ino_map);
902636
-- 
902636
1.8.3.1
902636