14f8ab
From 4d95e271a9042bf2d789a4d900ad263b6ea47681 Mon Sep 17 00:00:00 2001
14f8ab
From: Mohammed Rafi KC <rkavunga@redhat.com>
14f8ab
Date: Wed, 23 Jan 2019 21:55:01 +0530
14f8ab
Subject: [PATCH 100/124] clnt/rpc: ref leak during disconnect.
14f8ab
14f8ab
During disconnect cleanup, we are not cancelling reconnect
14f8ab
timer, which causes a ref leak each time when a disconnect
14f8ab
happen.
14f8ab
14f8ab
Backport of: https://review.gluster.org/#/c/glusterfs/+/22087/
14f8ab
14f8ab
>Change-Id: I9d05d1f368d080e04836bf6a0bb018bf8f7b5b8a
14f8ab
>updates: bz#1659708
14f8ab
>Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
14f8ab
14f8ab
Change-Id: I5a2dbb17e663a4809bb4c435cacadbf0ab694a76
14f8ab
BUG: 1471742
14f8ab
Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/167844
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
14f8ab
---
14f8ab
 libglusterfs/src/timer.c                           | 16 +++++++----
14f8ab
 rpc/rpc-lib/src/rpc-clnt.c                         | 11 +++++++-
14f8ab
 .../mgmt/glusterd/src/glusterd-snapshot-utils.c    | 32 ++++++++++++++++++----
14f8ab
 3 files changed, 47 insertions(+), 12 deletions(-)
14f8ab
14f8ab
diff --git a/libglusterfs/src/timer.c b/libglusterfs/src/timer.c
14f8ab
index d882543..2643c07 100644
14f8ab
--- a/libglusterfs/src/timer.c
14f8ab
+++ b/libglusterfs/src/timer.c
14f8ab
@@ -75,13 +75,13 @@ gf_timer_call_cancel(glusterfs_ctx_t *ctx, gf_timer_t *event)
14f8ab
     if (ctx == NULL || event == NULL) {
14f8ab
         gf_msg_callingfn("timer", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
14f8ab
                          "invalid argument");
14f8ab
-        return 0;
14f8ab
+        return -1;
14f8ab
     }
14f8ab
 
14f8ab
     if (ctx->cleanup_started) {
14f8ab
         gf_msg_callingfn("timer", GF_LOG_INFO, 0, LG_MSG_CTX_CLEANUP_STARTED,
14f8ab
                          "ctx cleanup started");
14f8ab
-        return 0;
14f8ab
+        return -1;
14f8ab
     }
14f8ab
 
14f8ab
     LOCK(&ctx->lock);
14f8ab
@@ -93,10 +93,9 @@ gf_timer_call_cancel(glusterfs_ctx_t *ctx, gf_timer_t *event)
14f8ab
     if (!reg) {
14f8ab
         /* This can happen when cleanup may have just started and
14f8ab
          * gf_timer_registry_destroy() sets ctx->timer to NULL.
14f8ab
-         * Just bail out as success as gf_timer_proc() takes
14f8ab
-         * care of cleaning up the events.
14f8ab
+         * gf_timer_proc() takes care of cleaning up the events.
14f8ab
          */
14f8ab
-        return 0;
14f8ab
+        return -1;
14f8ab
     }
14f8ab
 
14f8ab
     LOCK(&reg->lock);
14f8ab
@@ -203,6 +202,13 @@ gf_timer_proc(void *data)
14f8ab
         list_for_each_entry_safe(event, tmp, &reg->active, list)
14f8ab
         {
14f8ab
             list_del(&event->list);
14f8ab
+            /* TODO Possible resource leak
14f8ab
+             * Before freeing the event, we need to call the respective
14f8ab
+             * event functions and free any resources.
14f8ab
+             * For example, In case of rpc_clnt_reconnect, we need to
14f8ab
+             * unref rpc object which was taken when added to timer
14f8ab
+             * wheel.
14f8ab
+             */
14f8ab
             GF_FREE(event);
14f8ab
         }
14f8ab
     }
14f8ab
diff --git a/rpc/rpc-lib/src/rpc-clnt.c b/rpc/rpc-lib/src/rpc-clnt.c
14f8ab
index 3f7bb3c..6f47515 100644
14f8ab
--- a/rpc/rpc-lib/src/rpc-clnt.c
14f8ab
+++ b/rpc/rpc-lib/src/rpc-clnt.c
14f8ab
@@ -495,6 +495,7 @@ rpc_clnt_connection_cleanup(rpc_clnt_connection_t *conn)
14f8ab
     int unref = 0;
14f8ab
     int ret = 0;
14f8ab
     gf_boolean_t timer_unref = _gf_false;
14f8ab
+    gf_boolean_t reconnect_unref = _gf_false;
14f8ab
 
14f8ab
     if (!conn) {
14f8ab
         goto out;
14f8ab
@@ -514,6 +515,12 @@ rpc_clnt_connection_cleanup(rpc_clnt_connection_t *conn)
14f8ab
                 timer_unref = _gf_true;
14f8ab
             conn->timer = NULL;
14f8ab
         }
14f8ab
+        if (conn->reconnect) {
14f8ab
+            ret = gf_timer_call_cancel(clnt->ctx, conn->reconnect);
14f8ab
+            if (!ret)
14f8ab
+                reconnect_unref = _gf_true;
14f8ab
+            conn->reconnect = NULL;
14f8ab
+        }
14f8ab
 
14f8ab
         conn->connected = 0;
14f8ab
         conn->disconnected = 1;
14f8ab
@@ -533,6 +540,8 @@ rpc_clnt_connection_cleanup(rpc_clnt_connection_t *conn)
14f8ab
     if (timer_unref)
14f8ab
         rpc_clnt_unref(clnt);
14f8ab
 
14f8ab
+    if (reconnect_unref)
14f8ab
+        rpc_clnt_unref(clnt);
14f8ab
 out:
14f8ab
     return 0;
14f8ab
 }
14f8ab
@@ -830,7 +839,7 @@ rpc_clnt_handle_disconnect(struct rpc_clnt *clnt, rpc_clnt_connection_t *conn)
14f8ab
     pthread_mutex_lock(&conn->lock);
14f8ab
     {
14f8ab
         if (!conn->rpc_clnt->disabled && (conn->reconnect == NULL)) {
14f8ab
-            ts.tv_sec = 10;
14f8ab
+            ts.tv_sec = 3;
14f8ab
             ts.tv_nsec = 0;
14f8ab
 
14f8ab
             rpc_clnt_ref(clnt);
14f8ab
diff --git a/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c b/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c
14f8ab
index 041946d..b3c4158 100644
14f8ab
--- a/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c
14f8ab
+++ b/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c
14f8ab
@@ -3364,6 +3364,25 @@ out:
14f8ab
     return ret;
14f8ab
 }
14f8ab
 
14f8ab
+int
14f8ab
+glusterd_is_path_mounted(const char *path)
14f8ab
+{
14f8ab
+    FILE *mtab = NULL;
14f8ab
+    struct mntent *part = NULL;
14f8ab
+    int is_mounted = 0;
14f8ab
+
14f8ab
+    if ((mtab = setmntent("/etc/mtab", "r")) != NULL) {
14f8ab
+        while ((part = getmntent(mtab)) != NULL) {
14f8ab
+            if ((part->mnt_fsname != NULL) &&
14f8ab
+                (strcmp(part->mnt_dir, path)) == 0) {
14f8ab
+                is_mounted = 1;
14f8ab
+                break;
14f8ab
+            }
14f8ab
+        }
14f8ab
+        endmntent(mtab);
14f8ab
+    }
14f8ab
+    return is_mounted;
14f8ab
+}
14f8ab
 /* This function will do unmount for snaps.
14f8ab
  */
14f8ab
 int32_t
14f8ab
@@ -3388,14 +3407,11 @@ glusterd_snap_unmount(xlator_t *this, glusterd_volinfo_t *volinfo)
14f8ab
             continue;
14f8ab
         }
14f8ab
 
14f8ab
-        /* Fetch the brick mount path from the brickinfo->path */
14f8ab
-        ret = glusterd_get_brick_root(brickinfo->path, &brick_mount_path);
14f8ab
+        ret = glusterd_find_brick_mount_path(brickinfo->path,
14f8ab
+                                             &brick_mount_path);
14f8ab
         if (ret) {
14f8ab
-            gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_BRICK_PATH_UNMOUNTED,
14f8ab
+            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_BRK_MNTPATH_GET_FAIL,
14f8ab
                    "Failed to find brick_mount_path for %s", brickinfo->path);
14f8ab
-            /* There is chance that brick path is already
14f8ab
-             * unmounted. */
14f8ab
-            ret = 0;
14f8ab
             goto out;
14f8ab
         }
14f8ab
         /* unmount cannot be done when the brick process is still in
14f8ab
@@ -3440,6 +3456,10 @@ glusterd_umount(const char *path)
14f8ab
     GF_ASSERT(this);
14f8ab
     GF_ASSERT(path);
14f8ab
 
14f8ab
+    if (!glusterd_is_path_mounted(path)) {
14f8ab
+        return 0;
14f8ab
+    }
14f8ab
+
14f8ab
     runinit(&runner);
14f8ab
     snprintf(msg, sizeof(msg), "umount path %s", path);
14f8ab
     runner_add_args(&runner, _PATH_UMOUNT, "-f", path, NULL);
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab