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