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