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