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