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