d84cf8
From 51090a4b3cb000d601083f12d1875547819fc03f Mon Sep 17 00:00:00 2001
d84cf8
From: Mohit Agrawal <moagrawal@redhat.com>
d84cf8
Date: Wed, 4 Mar 2020 09:17:26 +0530
d84cf8
Subject: [PATCH 447/449] core[brick_mux]: brick crashed when creating and
d84cf8
 deleting volumes over time
d84cf8
d84cf8
Problem: In brick_mux environment, while volumes are created/stopped in a loop
d84cf8
         after running a long time the main brick is crashed.The brick is crashed
d84cf8
         because the main brick process was not cleaned up memory for all objects
d84cf8
         at the time of detaching a volume.
d84cf8
         Below are the objects that are missed at the time of detaching a volume
d84cf8
         1) xlator object for a brick graph
d84cf8
         2) local_pool for posix_lock xlator
d84cf8
         3) rpc object cleanup at quota xlator
d84cf8
         4) inode leak at brick xlator
d84cf8
d84cf8
Solution: To avoid the crash resolve all leak at the time of detaching a brick
d84cf8
> Change-Id: Ibb6e46c5fba22b9441a88cbaf6b3278823235913
d84cf8
> updates: #977
d84cf8
> Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
d84cf8
> (Cherry pick from commit e589d8de66d3325da8fbbbe44d1a5bd6335e08ab)
d84cf8
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/24209/)
d84cf8
d84cf8
BUG: 1790336
d84cf8
Change-Id: Ibb6e46c5fba22b9441a88cbaf6b3278823235913
d84cf8
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
d84cf8
Reviewed-on: https://code.engineering.redhat.com/gerrit/202782
d84cf8
Tested-by: RHGS Build Bot <nigelb@redhat.com>
d84cf8
Reviewed-by: Xavi Hernandez Juan <xhernandez@redhat.com>
d84cf8
---
d84cf8
 libglusterfs/src/glusterfs/glusterfs.h             |  1 +
d84cf8
 libglusterfs/src/graph.c                           |  1 +
d84cf8
 libglusterfs/src/graph.y                           |  2 +-
d84cf8
 libglusterfs/src/xlator.c                          | 29 ++++++++----
d84cf8
 xlators/features/changelog/src/changelog.c         |  1 +
d84cf8
 xlators/features/locks/src/posix.c                 |  4 ++
d84cf8
 xlators/features/quota/src/quota-enforcer-client.c | 14 +++++-
d84cf8
 xlators/features/quota/src/quota.c                 | 54 ++++++++++++++++++++--
d84cf8
 xlators/features/quota/src/quota.h                 |  3 ++
d84cf8
 xlators/protocol/server/src/server.c               | 12 +++--
d84cf8
 10 files changed, 103 insertions(+), 18 deletions(-)
d84cf8
d84cf8
diff --git a/libglusterfs/src/glusterfs/glusterfs.h b/libglusterfs/src/glusterfs/glusterfs.h
d84cf8
index 177a020..584846e 100644
d84cf8
--- a/libglusterfs/src/glusterfs/glusterfs.h
d84cf8
+++ b/libglusterfs/src/glusterfs/glusterfs.h
d84cf8
@@ -603,6 +603,7 @@ struct _glusterfs_graph {
d84cf8
     int used; /* Should be set when fuse gets
d84cf8
                         first CHILD_UP */
d84cf8
     uint32_t volfile_checksum;
d84cf8
+    pthread_mutex_t mutex;
d84cf8
 };
d84cf8
 typedef struct _glusterfs_graph glusterfs_graph_t;
d84cf8
 
d84cf8
diff --git a/libglusterfs/src/graph.c b/libglusterfs/src/graph.c
d84cf8
index bb5e67a..1cd92db 100644
d84cf8
--- a/libglusterfs/src/graph.c
d84cf8
+++ b/libglusterfs/src/graph.c
d84cf8
@@ -1092,6 +1092,7 @@ glusterfs_graph_destroy_residual(glusterfs_graph_t *graph)
d84cf8
     ret = xlator_tree_free_memacct(graph->first);
d84cf8
 
d84cf8
     list_del_init(&graph->list);
d84cf8
+    pthread_mutex_destroy(&graph->mutex);
d84cf8
     GF_FREE(graph);
d84cf8
 
d84cf8
     return ret;
d84cf8
diff --git a/libglusterfs/src/graph.y b/libglusterfs/src/graph.y
d84cf8
index 5b92985..5733515 100644
d84cf8
--- a/libglusterfs/src/graph.y
d84cf8
+++ b/libglusterfs/src/graph.y
d84cf8
@@ -541,7 +541,7 @@ glusterfs_graph_new ()
d84cf8
                 return NULL;
d84cf8
 
d84cf8
         INIT_LIST_HEAD (&graph->list);
d84cf8
-
d84cf8
+        pthread_mutex_init(&graph->mutex, NULL);
d84cf8
         gettimeofday (&graph->dob, NULL);
d84cf8
 
d84cf8
         return graph;
d84cf8
diff --git a/libglusterfs/src/xlator.c b/libglusterfs/src/xlator.c
d84cf8
index 108b96a..36cc32c 100644
d84cf8
--- a/libglusterfs/src/xlator.c
d84cf8
+++ b/libglusterfs/src/xlator.c
d84cf8
@@ -938,6 +938,8 @@ xlator_mem_cleanup(xlator_t *this)
d84cf8
     xlator_list_t **trav_p = NULL;
d84cf8
     xlator_t *top = NULL;
d84cf8
     xlator_t *victim = NULL;
d84cf8
+    glusterfs_graph_t *graph = NULL;
d84cf8
+    gf_boolean_t graph_cleanup = _gf_false;
d84cf8
 
d84cf8
     if (this->call_cleanup || !this->ctx)
d84cf8
         return;
d84cf8
@@ -945,6 +947,12 @@ xlator_mem_cleanup(xlator_t *this)
d84cf8
     this->call_cleanup = 1;
d84cf8
     ctx = this->ctx;
d84cf8
 
d84cf8
+    inode_table = this->itable;
d84cf8
+    if (inode_table) {
d84cf8
+        inode_table_destroy(inode_table);
d84cf8
+        this->itable = NULL;
d84cf8
+    }
d84cf8
+
d84cf8
     xlator_call_fini(trav);
d84cf8
 
d84cf8
     while (prev) {
d84cf8
@@ -953,12 +961,6 @@ xlator_mem_cleanup(xlator_t *this)
d84cf8
         prev = trav;
d84cf8
     }
d84cf8
 
d84cf8
-    inode_table = this->itable;
d84cf8
-    if (inode_table) {
d84cf8
-        inode_table_destroy(inode_table);
d84cf8
-        this->itable = NULL;
d84cf8
-    }
d84cf8
-
d84cf8
     if (this->fini) {
d84cf8
         this->fini(this);
d84cf8
     }
d84cf8
@@ -968,17 +970,28 @@ xlator_mem_cleanup(xlator_t *this)
d84cf8
     if (ctx->active) {
d84cf8
         top = ctx->active->first;
d84cf8
         LOCK(&ctx->volfile_lock);
d84cf8
-        /* TODO here we have leak for xlator node in a graph */
d84cf8
-        /* Need to move only top xlator from a graph */
d84cf8
         for (trav_p = &top->children; *trav_p; trav_p = &(*trav_p)->next) {
d84cf8
             victim = (*trav_p)->xlator;
d84cf8
             if (victim->call_cleanup && !strcmp(victim->name, this->name)) {
d84cf8
+                graph_cleanup = _gf_true;
d84cf8
                 (*trav_p) = (*trav_p)->next;
d84cf8
                 break;
d84cf8
             }
d84cf8
         }
d84cf8
         UNLOCK(&ctx->volfile_lock);
d84cf8
     }
d84cf8
+
d84cf8
+    if (graph_cleanup) {
d84cf8
+        prev = this;
d84cf8
+        graph = ctx->active;
d84cf8
+        pthread_mutex_lock(&graph->mutex);
d84cf8
+        while (prev) {
d84cf8
+            trav = prev->next;
d84cf8
+            GF_FREE(prev);
d84cf8
+            prev = trav;
d84cf8
+        }
d84cf8
+        pthread_mutex_unlock(&graph->mutex);
d84cf8
+    }
d84cf8
 }
d84cf8
 
d84cf8
 void
d84cf8
diff --git a/xlators/features/changelog/src/changelog.c b/xlators/features/changelog/src/changelog.c
d84cf8
index ff06c09..b54112c 100644
d84cf8
--- a/xlators/features/changelog/src/changelog.c
d84cf8
+++ b/xlators/features/changelog/src/changelog.c
d84cf8
@@ -2872,6 +2872,7 @@ fini(xlator_t *this)
d84cf8
         if (priv->active || priv->rpc_active) {
d84cf8
             /* terminate RPC server/threads */
d84cf8
             changelog_cleanup_rpc(this, priv);
d84cf8
+            GF_FREE(priv->ev_dispatcher);
d84cf8
         }
d84cf8
         /* call barrier_disable to cancel timer */
d84cf8
         if (priv->barrier_enabled)
d84cf8
diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c
d84cf8
index 9a14c64..50f1265 100644
d84cf8
--- a/xlators/features/locks/src/posix.c
d84cf8
+++ b/xlators/features/locks/src/posix.c
d84cf8
@@ -4102,6 +4102,10 @@ fini(xlator_t *this)
d84cf8
     if (!priv)
d84cf8
         return;
d84cf8
     this->private = NULL;
d84cf8
+    if (this->local_pool) {
d84cf8
+        mem_pool_destroy(this->local_pool);
d84cf8
+        this->local_pool = NULL;
d84cf8
+    }
d84cf8
     GF_FREE(priv->brickname);
d84cf8
     GF_FREE(priv);
d84cf8
 
d84cf8
diff --git a/xlators/features/quota/src/quota-enforcer-client.c b/xlators/features/quota/src/quota-enforcer-client.c
d84cf8
index 1a4c2e3..097439d 100644
d84cf8
--- a/xlators/features/quota/src/quota-enforcer-client.c
d84cf8
+++ b/xlators/features/quota/src/quota-enforcer-client.c
d84cf8
@@ -362,16 +362,28 @@ quota_enforcer_notify(struct rpc_clnt *rpc, void *mydata,
d84cf8
 {
d84cf8
     xlator_t *this = NULL;
d84cf8
     int ret = 0;
d84cf8
+    quota_priv_t *priv = NULL;
d84cf8
 
d84cf8
     this = mydata;
d84cf8
-
d84cf8
+    priv = this->private;
d84cf8
     switch (event) {
d84cf8
         case RPC_CLNT_CONNECT: {
d84cf8
+            pthread_mutex_lock(&priv->conn_mutex);
d84cf8
+            {
d84cf8
+                priv->conn_status = _gf_true;
d84cf8
+            }
d84cf8
+            pthread_mutex_unlock(&priv->conn_mutex);
d84cf8
             gf_msg_trace(this->name, 0, "got RPC_CLNT_CONNECT");
d84cf8
             break;
d84cf8
         }
d84cf8
 
d84cf8
         case RPC_CLNT_DISCONNECT: {
d84cf8
+            pthread_mutex_lock(&priv->conn_mutex);
d84cf8
+            {
d84cf8
+                priv->conn_status = _gf_false;
d84cf8
+                pthread_cond_signal(&priv->conn_cond);
d84cf8
+            }
d84cf8
+            pthread_mutex_unlock(&priv->conn_mutex);
d84cf8
             gf_msg_trace(this->name, 0, "got RPC_CLNT_DISCONNECT");
d84cf8
             break;
d84cf8
         }
d84cf8
diff --git a/xlators/features/quota/src/quota.c b/xlators/features/quota/src/quota.c
d84cf8
index a0c236d..d1123ce 100644
d84cf8
--- a/xlators/features/quota/src/quota.c
d84cf8
+++ b/xlators/features/quota/src/quota.c
d84cf8
@@ -5014,6 +5014,43 @@ quota_forget(xlator_t *this, inode_t *inode)
d84cf8
     return 0;
d84cf8
 }
d84cf8
 
d84cf8
+int
d84cf8
+notify(xlator_t *this, int event, void *data, ...)
d84cf8
+{
d84cf8
+    quota_priv_t *priv = NULL;
d84cf8
+    int ret = 0;
d84cf8
+    rpc_clnt_t *rpc = NULL;
d84cf8
+    gf_boolean_t conn_status = _gf_true;
d84cf8
+    xlator_t *victim = data;
d84cf8
+
d84cf8
+    priv = this->private;
d84cf8
+    if (!priv || !priv->is_quota_on)
d84cf8
+        goto out;
d84cf8
+
d84cf8
+    if (event == GF_EVENT_PARENT_DOWN) {
d84cf8
+        rpc = priv->rpc_clnt;
d84cf8
+        if (rpc) {
d84cf8
+            rpc_clnt_disable(rpc);
d84cf8
+            pthread_mutex_lock(&priv->conn_mutex);
d84cf8
+            {
d84cf8
+                conn_status = priv->conn_status;
d84cf8
+                while (conn_status) {
d84cf8
+                    (void)pthread_cond_wait(&priv->conn_cond,
d84cf8
+                                            &priv->conn_mutex);
d84cf8
+                    conn_status = priv->conn_status;
d84cf8
+                }
d84cf8
+            }
d84cf8
+            pthread_mutex_unlock(&priv->conn_mutex);
d84cf8
+            gf_log(this->name, GF_LOG_INFO,
d84cf8
+                   "Notify GF_EVENT_PARENT_DOWN for brick %s", victim->name);
d84cf8
+        }
d84cf8
+    }
d84cf8
+
d84cf8
+out:
d84cf8
+    ret = default_notify(this, event, data);
d84cf8
+    return ret;
d84cf8
+}
d84cf8
+
d84cf8
 int32_t
d84cf8
 init(xlator_t *this)
d84cf8
 {
d84cf8
@@ -5056,6 +5093,10 @@ init(xlator_t *this)
d84cf8
         goto err;
d84cf8
     }
d84cf8
 
d84cf8
+    pthread_mutex_init(&priv->conn_mutex, NULL);
d84cf8
+    pthread_cond_init(&priv->conn_cond, NULL);
d84cf8
+    priv->conn_status = _gf_false;
d84cf8
+
d84cf8
     if (priv->is_quota_on) {
d84cf8
         rpc = quota_enforcer_init(this, this->options);
d84cf8
         if (rpc == NULL) {
d84cf8
@@ -5169,20 +5210,22 @@ fini(xlator_t *this)
d84cf8
 {
d84cf8
     quota_priv_t *priv = NULL;
d84cf8
     rpc_clnt_t *rpc = NULL;
d84cf8
-    int i = 0, cnt = 0;
d84cf8
 
d84cf8
     priv = this->private;
d84cf8
     if (!priv)
d84cf8
         return;
d84cf8
     rpc = priv->rpc_clnt;
d84cf8
     priv->rpc_clnt = NULL;
d84cf8
-    this->private = NULL;
d84cf8
     if (rpc) {
d84cf8
-        cnt = GF_ATOMIC_GET(rpc->refcount);
d84cf8
-        for (i = 0; i < cnt; i++)
d84cf8
-            rpc_clnt_unref(rpc);
d84cf8
+        rpc_clnt_connection_cleanup(&rpc->conn);
d84cf8
+        rpc_clnt_unref(rpc);
d84cf8
     }
d84cf8
+
d84cf8
+    this->private = NULL;
d84cf8
     LOCK_DESTROY(&priv->lock);
d84cf8
+    pthread_mutex_destroy(&priv->conn_mutex);
d84cf8
+    pthread_cond_destroy(&priv->conn_cond);
d84cf8
+
d84cf8
     GF_FREE(priv);
d84cf8
     if (this->local_pool) {
d84cf8
         mem_pool_destroy(this->local_pool);
d84cf8
@@ -5314,6 +5357,7 @@ struct volume_options options[] = {
d84cf8
 xlator_api_t xlator_api = {
d84cf8
     .init = init,
d84cf8
     .fini = fini,
d84cf8
+    .notify = notify,
d84cf8
     .reconfigure = reconfigure,
d84cf8
     .mem_acct_init = mem_acct_init,
d84cf8
     .op_version = {1}, /* Present from the initial version */
d84cf8
diff --git a/xlators/features/quota/src/quota.h b/xlators/features/quota/src/quota.h
d84cf8
index a5a99ca..e51ffd4 100644
d84cf8
--- a/xlators/features/quota/src/quota.h
d84cf8
+++ b/xlators/features/quota/src/quota.h
d84cf8
@@ -217,6 +217,9 @@ struct quota_priv {
d84cf8
     char *volume_uuid;
d84cf8
     uint64_t validation_count;
d84cf8
     int32_t quotad_conn_status;
d84cf8
+    pthread_mutex_t conn_mutex;
d84cf8
+    pthread_cond_t conn_cond;
d84cf8
+    gf_boolean_t conn_status;
d84cf8
 };
d84cf8
 typedef struct quota_priv quota_priv_t;
d84cf8
 
d84cf8
diff --git a/xlators/protocol/server/src/server.c b/xlators/protocol/server/src/server.c
d84cf8
index a5f09fe..54d9c0f 100644
d84cf8
--- a/xlators/protocol/server/src/server.c
d84cf8
+++ b/xlators/protocol/server/src/server.c
d84cf8
@@ -409,7 +409,13 @@ server_call_xlator_mem_cleanup(xlator_t *this, char *victim_name)
d84cf8
 
d84cf8
     arg = calloc(1, sizeof(*arg));
d84cf8
     arg->this = this;
d84cf8
-    arg->victim_name = gf_strdup(victim_name);
d84cf8
+    arg->victim_name = strdup(victim_name);
d84cf8
+    if (!arg->victim_name) {
d84cf8
+        gf_smsg(this->name, GF_LOG_CRITICAL, ENOMEM, LG_MSG_NO_MEMORY,
d84cf8
+                "Memory allocation is failed");
d84cf8
+        return;
d84cf8
+    }
d84cf8
+
d84cf8
     th_ret = gf_thread_create_detached(&th_id, server_graph_janitor_threads,
d84cf8
                                        arg, "graphjanitor");
d84cf8
     if (th_ret) {
d84cf8
@@ -417,7 +423,7 @@ server_call_xlator_mem_cleanup(xlator_t *this, char *victim_name)
d84cf8
                "graph janitor Thread"
d84cf8
                " creation is failed for brick %s",
d84cf8
                victim_name);
d84cf8
-        GF_FREE(arg->victim_name);
d84cf8
+        free(arg->victim_name);
d84cf8
         free(arg);
d84cf8
     }
d84cf8
 }
d84cf8
@@ -628,7 +634,7 @@ server_graph_janitor_threads(void *data)
d84cf8
     }
d84cf8
 
d84cf8
 out:
d84cf8
-    GF_FREE(arg->victim_name);
d84cf8
+    free(arg->victim_name);
d84cf8
     free(arg);
d84cf8
     return NULL;
d84cf8
 }
d84cf8
-- 
d84cf8
1.8.3.1
d84cf8