74b1de
From 1029c27982d2f91cb2d3c4fcc19aa5171111dfb9 Mon Sep 17 00:00:00 2001
74b1de
From: Jiffin Tony Thottan <jthottan@redhat.com>
74b1de
Date: Mon, 16 Oct 2017 14:24:29 +0530
74b1de
Subject: [PATCH 054/124] Revert "glusterd: (storhaug) remove ganesha"
74b1de
74b1de
This reverts commit 843e1b04b554ab887ec656ae7b468bb93ee4e2f7.
74b1de
74b1de
Label: DOWNSTREAM ONLY
74b1de
74b1de
Change-Id: I06b5450344c33f26da3d94b6f67051d41dfbba17
74b1de
Signed-off-by: Jiffin Tony Thottan <jthottan@redhat.com>
74b1de
Reviewed-on: https://code.engineering.redhat.com/gerrit/167103
74b1de
Reviewed-by: Soumya Koduri <skoduri@redhat.com>
74b1de
Tested-by: RHGS Build Bot <nigelb@redhat.com>
74b1de
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
74b1de
---
74b1de
 cli/src/cli-cmd-global.c                           |  57 ++
74b1de
 cli/src/cli-cmd-parser.c                           | 122 ++-
74b1de
 cli/src/cli-cmd.c                                  |   3 +-
74b1de
 cli/src/cli-rpc-ops.c                              |  82 ++
74b1de
 cli/src/cli.h                                      |   4 +
74b1de
 xlators/mgmt/glusterd/src/Makefile.am              |   4 +-
74b1de
 xlators/mgmt/glusterd/src/glusterd-errno.h         |   2 +-
74b1de
 xlators/mgmt/glusterd/src/glusterd-ganesha.c       | 915 +++++++++++++++++++++
74b1de
 xlators/mgmt/glusterd/src/glusterd-handler.c       |  79 ++
74b1de
 xlators/mgmt/glusterd/src/glusterd-messages.h      |   2 +-
74b1de
 xlators/mgmt/glusterd/src/glusterd-op-sm.c         |  45 +-
74b1de
 .../mgmt/glusterd/src/glusterd-snapshot-utils.c    | 196 +++++
74b1de
 xlators/mgmt/glusterd/src/glusterd-store.h         |   2 +
74b1de
 xlators/mgmt/glusterd/src/glusterd-volume-ops.c    |  37 +
74b1de
 xlators/mgmt/glusterd/src/glusterd-volume-set.c    |   7 +
74b1de
 xlators/mgmt/glusterd/src/glusterd.h               |  22 +
74b1de
 16 files changed, 1568 insertions(+), 11 deletions(-)
74b1de
 create mode 100644 xlators/mgmt/glusterd/src/glusterd-ganesha.c
74b1de
74b1de
diff --git a/cli/src/cli-cmd-global.c b/cli/src/cli-cmd-global.c
74b1de
index d0729ac..270b76f 100644
74b1de
--- a/cli/src/cli-cmd-global.c
74b1de
+++ b/cli/src/cli-cmd-global.c
74b1de
@@ -36,6 +36,10 @@ int
74b1de
 cli_cmd_get_state_cbk(struct cli_state *state, struct cli_cmd_word *word,
74b1de
                       const char **words, int wordcount);
74b1de
 
74b1de
+int
74b1de
+cli_cmd_ganesha_cbk(struct cli_state *state, struct cli_cmd_word *word,
74b1de
+                    const char **words, int wordcount);
74b1de
+
74b1de
 struct cli_cmd global_cmds[] = {
74b1de
     {
74b1de
         "global help",
74b1de
@@ -48,6 +52,11 @@ struct cli_cmd global_cmds[] = {
74b1de
         cli_cmd_get_state_cbk,
74b1de
         "Get local state representation of mentioned daemon",
74b1de
     },
74b1de
+    {
74b1de
+        "nfs-ganesha {enable| disable} ",
74b1de
+        cli_cmd_ganesha_cbk,
74b1de
+        "Enable/disable NFS-Ganesha support",
74b1de
+    },
74b1de
     {NULL, NULL, NULL}};
74b1de
 
74b1de
 int
74b1de
@@ -89,6 +98,54 @@ out:
74b1de
 }
74b1de
 
74b1de
 int
74b1de
+cli_cmd_ganesha_cbk(struct cli_state *state, struct cli_cmd_word *word,
74b1de
+                    const char **words, int wordcount)
74b1de
+
74b1de
+{
74b1de
+    int sent = 0;
74b1de
+    int parse_error = 0;
74b1de
+    int ret = -1;
74b1de
+    rpc_clnt_procedure_t *proc = NULL;
74b1de
+    call_frame_t *frame = NULL;
74b1de
+    dict_t *options = NULL;
74b1de
+    cli_local_t *local = NULL;
74b1de
+    char *op_errstr = NULL;
74b1de
+
74b1de
+    proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GANESHA];
74b1de
+
74b1de
+    frame = create_frame(THIS, THIS->ctx->pool);
74b1de
+    if (!frame)
74b1de
+        goto out;
74b1de
+
74b1de
+    ret = cli_cmd_ganesha_parse(state, words, wordcount, &options, &op_errstr);
74b1de
+    if (ret) {
74b1de
+        if (op_errstr) {
74b1de
+            cli_err("%s", op_errstr);
74b1de
+            GF_FREE(op_errstr);
74b1de
+        } else
74b1de
+            cli_usage_out(word->pattern);
74b1de
+        parse_error = 1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    CLI_LOCAL_INIT(local, words, frame, options);
74b1de
+
74b1de
+    if (proc->fn) {
74b1de
+        ret = proc->fn(frame, THIS, options);
74b1de
+    }
74b1de
+
74b1de
+out:
74b1de
+    if (ret) {
74b1de
+        cli_cmd_sent_status_get(&sent);
74b1de
+        if ((sent == 0) && (parse_error == 0))
74b1de
+            cli_out("Setting global option failed");
74b1de
+    }
74b1de
+
74b1de
+    CLI_STACK_DESTROY(frame);
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
 cli_cmd_get_state_cbk(struct cli_state *state, struct cli_cmd_word *word,
74b1de
                       const char **words, int wordcount)
74b1de
 {
74b1de
diff --git a/cli/src/cli-cmd-parser.c b/cli/src/cli-cmd-parser.c
74b1de
index d9ccba1..cd9c445 100644
74b1de
--- a/cli/src/cli-cmd-parser.c
74b1de
+++ b/cli/src/cli-cmd-parser.c
74b1de
@@ -1694,7 +1694,7 @@ cli_cmd_volume_set_parse(struct cli_state *state, const char **words,
74b1de
             }
74b1de
         }
74b1de
 
74b1de
-        if ((strcmp (key, "cluster.brick-multiplex") == 0)) {
74b1de
+        if ((strcmp(key, "cluster.brick-multiplex") == 0)) {
74b1de
             question =
74b1de
                 "Brick-multiplexing is supported only for "
74b1de
                 "OCS converged or independent mode. Also it is "
74b1de
@@ -1703,11 +1703,12 @@ cli_cmd_volume_set_parse(struct cli_state *state, const char **words,
74b1de
                 "are running before this option is modified."
74b1de
                 "Do you still want to continue?";
74b1de
 
74b1de
-            answer = cli_cmd_get_confirmation (state, question);
74b1de
+            answer = cli_cmd_get_confirmation(state, question);
74b1de
             if (GF_ANSWER_NO == answer) {
74b1de
-                gf_log ("cli", GF_LOG_ERROR, "Operation "
74b1de
-                        "cancelled, exiting");
74b1de
-                *op_errstr = gf_strdup ("Aborted by user.");
74b1de
+                gf_log("cli", GF_LOG_ERROR,
74b1de
+                       "Operation "
74b1de
+                       "cancelled, exiting");
74b1de
+                *op_errstr = gf_strdup("Aborted by user.");
74b1de
                 ret = -1;
74b1de
                 goto out;
74b1de
             }
74b1de
@@ -5848,3 +5849,114 @@ out:
74b1de
 
74b1de
     return ret;
74b1de
 }
74b1de
+
74b1de
+/* Parsing global option for NFS-Ganesha config
74b1de
+ *  gluster nfs-ganesha enable/disable */
74b1de
+
74b1de
+int32_t
74b1de
+cli_cmd_ganesha_parse(struct cli_state *state, const char **words,
74b1de
+                      int wordcount, dict_t **options, char **op_errstr)
74b1de
+{
74b1de
+    dict_t *dict = NULL;
74b1de
+    int ret = -1;
74b1de
+    char *key = NULL;
74b1de
+    char *value = NULL;
74b1de
+    char *w = NULL;
74b1de
+    char *opwords[] = {"enable", "disable", NULL};
74b1de
+    const char *question = NULL;
74b1de
+    gf_answer_t answer = GF_ANSWER_NO;
74b1de
+
74b1de
+    GF_ASSERT(words);
74b1de
+    GF_ASSERT(options);
74b1de
+
74b1de
+    dict = dict_new();
74b1de
+
74b1de
+    if (!dict)
74b1de
+        goto out;
74b1de
+
74b1de
+    if (wordcount != 2)
74b1de
+        goto out;
74b1de
+
74b1de
+    key = (char *)words[0];
74b1de
+    value = (char *)words[1];
74b1de
+
74b1de
+    if (!key || !value) {
74b1de
+        cli_out("Usage : nfs-ganesha <enable/disable>");
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = gf_strip_whitespace(value, strlen(value));
74b1de
+    if (ret == -1)
74b1de
+        goto out;
74b1de
+
74b1de
+    if (strcmp(key, "nfs-ganesha")) {
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "Global option: error: ' %s '"
74b1de
+                    "is not a valid global option.",
74b1de
+                    key);
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    w = str_getunamb(value, opwords);
74b1de
+    if (!w) {
74b1de
+        cli_out(
74b1de
+            "Invalid global option \n"
74b1de
+            "Usage : nfs-ganesha <enable/disable>");
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    question =
74b1de
+        "Enabling NFS-Ganesha requires Gluster-NFS to be"
74b1de
+        " disabled across the trusted pool. Do you "
74b1de
+        "still want to continue?\n";
74b1de
+
74b1de
+    if (strcmp(value, "enable") == 0) {
74b1de
+        answer = cli_cmd_get_confirmation(state, question);
74b1de
+        if (GF_ANSWER_NO == answer) {
74b1de
+            gf_log("cli", GF_LOG_ERROR,
74b1de
+                   "Global operation "
74b1de
+                   "cancelled, exiting");
74b1de
+            ret = -1;
74b1de
+            goto out;
74b1de
+        }
74b1de
+    }
74b1de
+    cli_out("This will take a few minutes to complete. Please wait ..");
74b1de
+
74b1de
+    ret = dict_set_str(dict, "key", key);
74b1de
+    if (ret) {
74b1de
+        gf_log(THIS->name, GF_LOG_ERROR, "dict set on key failed");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = dict_set_str(dict, "value", value);
74b1de
+    if (ret) {
74b1de
+        gf_log(THIS->name, GF_LOG_ERROR, "dict set on value failed");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = dict_set_str(dict, "globalname", "All");
74b1de
+    if (ret) {
74b1de
+        gf_log(THIS->name, GF_LOG_ERROR,
74b1de
+               "dict set on global"
74b1de
+               " key failed.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = dict_set_int32(dict, "hold_global_locks", _gf_true);
74b1de
+    if (ret) {
74b1de
+        gf_log(THIS->name, GF_LOG_ERROR,
74b1de
+               "dict set on global key "
74b1de
+               "failed.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    *options = dict;
74b1de
+out:
74b1de
+    if (ret)
74b1de
+        dict_unref(dict);
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
diff --git a/cli/src/cli-cmd.c b/cli/src/cli-cmd.c
74b1de
index 2ee8b1b..8c06905 100644
74b1de
--- a/cli/src/cli-cmd.c
74b1de
+++ b/cli/src/cli-cmd.c
74b1de
@@ -366,7 +366,8 @@ cli_cmd_submit(struct rpc_clnt *rpc, void *req, call_frame_t *frame,
74b1de
     unsigned timeout = 0;
74b1de
 
74b1de
     if ((GLUSTER_CLI_PROFILE_VOLUME == procnum) ||
74b1de
-        (GLUSTER_CLI_HEAL_VOLUME == procnum))
74b1de
+        (GLUSTER_CLI_HEAL_VOLUME == procnum) ||
74b1de
+        (GLUSTER_CLI_GANESHA == procnum))
74b1de
         timeout = cli_ten_minutes_timeout;
74b1de
     else
74b1de
         timeout = cli_default_conn_timeout;
74b1de
diff --git a/cli/src/cli-rpc-ops.c b/cli/src/cli-rpc-ops.c
74b1de
index 12e7fcc..736cd18 100644
74b1de
--- a/cli/src/cli-rpc-ops.c
74b1de
+++ b/cli/src/cli-rpc-ops.c
74b1de
@@ -2207,6 +2207,62 @@ out:
74b1de
     return ret;
74b1de
 }
74b1de
 
74b1de
+int
74b1de
+gf_cli_ganesha_cbk(struct rpc_req *req, struct iovec *iov, int count,
74b1de
+                   void *myframe)
74b1de
+{
74b1de
+    gf_cli_rsp rsp = {
74b1de
+        0,
74b1de
+    };
74b1de
+    int ret = -1;
74b1de
+    dict_t *dict = NULL;
74b1de
+
74b1de
+    GF_ASSERT(myframe);
74b1de
+
74b1de
+    if (-1 == req->rpc_status) {
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp);
74b1de
+    if (ret < 0) {
74b1de
+        gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR,
74b1de
+               "Failed to decode xdr response");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    gf_log("cli", GF_LOG_DEBUG, "Received resp to ganesha");
74b1de
+
74b1de
+    dict = dict_new();
74b1de
+
74b1de
+    if (!dict) {
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict);
74b1de
+    if (ret)
74b1de
+        goto out;
74b1de
+
74b1de
+    if (rsp.op_ret) {
74b1de
+        if (strcmp(rsp.op_errstr, ""))
74b1de
+            cli_err("nfs-ganesha: failed: %s", rsp.op_errstr);
74b1de
+        else
74b1de
+            cli_err("nfs-ganesha: failed");
74b1de
+    }
74b1de
+
74b1de
+    else {
74b1de
+        cli_out("nfs-ganesha : success ");
74b1de
+    }
74b1de
+
74b1de
+    ret = rsp.op_ret;
74b1de
+
74b1de
+out:
74b1de
+    if (dict)
74b1de
+        dict_unref(dict);
74b1de
+    cli_cmd_broadcast_response(ret);
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
 char *
74b1de
 is_server_debug_xlator(void *myframe)
74b1de
 {
74b1de
@@ -4880,6 +4936,31 @@ out:
74b1de
 }
74b1de
 
74b1de
 int32_t
74b1de
+gf_cli_ganesha(call_frame_t *frame, xlator_t *this, void *data)
74b1de
+{
74b1de
+    gf_cli_req req = {{
74b1de
+        0,
74b1de
+    }};
74b1de
+    int ret = 0;
74b1de
+    dict_t *dict = NULL;
74b1de
+
74b1de
+    if (!frame || !this || !data) {
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    dict = data;
74b1de
+
74b1de
+    ret = cli_to_glusterd(&req, frame, gf_cli_ganesha_cbk,
74b1de
+                          (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_GANESHA,
74b1de
+                          this, cli_rpc_prog, NULL);
74b1de
+out:
74b1de
+    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int32_t
74b1de
 gf_cli_set_volume(call_frame_t *frame, xlator_t *this, void *data)
74b1de
 {
74b1de
     gf_cli_req req = {{
74b1de
@@ -12214,6 +12295,7 @@ struct rpc_clnt_procedure gluster_cli_actors[GLUSTER_CLI_MAXVALUE] = {
74b1de
     [GLUSTER_CLI_SYS_EXEC] = {"SYS_EXEC", gf_cli_sys_exec},
74b1de
     [GLUSTER_CLI_SNAP] = {"SNAP", gf_cli_snapshot},
74b1de
     [GLUSTER_CLI_BARRIER_VOLUME] = {"BARRIER VOLUME", gf_cli_barrier_volume},
74b1de
+    [GLUSTER_CLI_GANESHA] = {"GANESHA", gf_cli_ganesha},
74b1de
     [GLUSTER_CLI_GET_VOL_OPT] = {"GET_VOL_OPT", gf_cli_get_vol_opt},
74b1de
     [GLUSTER_CLI_BITROT] = {"BITROT", gf_cli_bitrot},
74b1de
     [GLUSTER_CLI_ATTACH_TIER] = {"ATTACH_TIER", gf_cli_attach_tier},
74b1de
diff --git a/cli/src/cli.h b/cli/src/cli.h
74b1de
index b79a0a2..37e4d9d 100644
74b1de
--- a/cli/src/cli.h
74b1de
+++ b/cli/src/cli.h
74b1de
@@ -282,6 +282,10 @@ cli_cmd_volume_set_parse(struct cli_state *state, const char **words,
74b1de
                          int wordcount, dict_t **options, char **op_errstr);
74b1de
 
74b1de
 int32_t
74b1de
+cli_cmd_ganesha_parse(struct cli_state *state, const char **words,
74b1de
+                      int wordcount, dict_t **options, char **op_errstr);
74b1de
+
74b1de
+int32_t
74b1de
 cli_cmd_get_state_parse(struct cli_state *state, const char **words,
74b1de
                         int wordcount, dict_t **options, char **op_errstr);
74b1de
 
74b1de
diff --git a/xlators/mgmt/glusterd/src/Makefile.am b/xlators/mgmt/glusterd/src/Makefile.am
74b1de
index c8dd8e3..5fe5156 100644
74b1de
--- a/xlators/mgmt/glusterd/src/Makefile.am
74b1de
+++ b/xlators/mgmt/glusterd/src/Makefile.am
74b1de
@@ -10,7 +10,7 @@ glusterd_la_LDFLAGS = -module $(GF_XLATOR_DEFAULT_LDFLAGS) $(LIB_DL)
74b1de
 glusterd_la_SOURCES = glusterd.c glusterd-handler.c glusterd-sm.c \
74b1de
 	glusterd-op-sm.c glusterd-utils.c glusterd-rpc-ops.c \
74b1de
 	glusterd-store.c glusterd-handshake.c glusterd-pmap.c \
74b1de
-	glusterd-volgen.c glusterd-rebalance.c \
74b1de
+	glusterd-volgen.c glusterd-rebalance.c glusterd-ganesha.c \
74b1de
 	glusterd-quota.c glusterd-bitrot.c glusterd-geo-rep.c \
74b1de
 	glusterd-replace-brick.c glusterd-log-ops.c glusterd-tier.c \
74b1de
 	glusterd-volume-ops.c glusterd-brick-ops.c glusterd-mountbroker.c \
74b1de
@@ -52,6 +52,8 @@ AM_CPPFLAGS = $(GF_CPPFLAGS) -I$(top_srcdir)/libglusterfs/src \
74b1de
 	-I$(CONTRIBDIR)/mount -I$(CONTRIBDIR)/userspace-rcu \
74b1de
 	-DSBIN_DIR=\"$(sbindir)\" -DDATADIR=\"$(localstatedir)\" \
74b1de
 	-DGSYNCD_PREFIX=\"$(GLUSTERFS_LIBEXECDIR)\" \
74b1de
+	-DCONFDIR=\"$(localstatedir)/run/gluster/shared_storage/nfs-ganesha\" \
74b1de
+	-DGANESHA_PREFIX=\"$(libexecdir)/ganesha\" \
74b1de
 	-DSYNCDAEMON_COMPILE=$(SYNCDAEMON_COMPILE)
74b1de
 
74b1de
 
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-errno.h b/xlators/mgmt/glusterd/src/glusterd-errno.h
74b1de
index 7e1575b..c74070e 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-errno.h
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-errno.h
74b1de
@@ -27,7 +27,7 @@ enum glusterd_op_errno {
74b1de
     EG_ISSNAP = 30813,    /* Volume is a snap volume           */
74b1de
     EG_GEOREPRUN = 30814, /* Geo-Replication is running        */
74b1de
     EG_NOTTHINP = 30815,  /* Bricks are not thinly provisioned */
74b1de
-    EG_NOGANESHA = 30816, /* obsolete ganesha is not enabled   */
74b1de
+    EG_NOGANESHA = 30816, /* Global ganesha is not enabled   */
74b1de
 };
74b1de
 
74b1de
 #endif
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-ganesha.c b/xlators/mgmt/glusterd/src/glusterd-ganesha.c
74b1de
new file mode 100644
74b1de
index 0000000..fac16e6
74b1de
--- /dev/null
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-ganesha.c
74b1de
@@ -0,0 +1,915 @@
74b1de
+/*
74b1de
+   Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
74b1de
+   This file is part of GlusterFS.
74b1de
+
74b1de
+   This file is licensed to you under your choice of the GNU Lesser
74b1de
+   General Public License, version 3 or any later version (LGPLv3 or
74b1de
+   later), or the GNU General Public License, version 2 (GPLv2), in all
74b1de
+   cases as published by the Free Software Foundation.
74b1de
+*/
74b1de
+
74b1de
+#include <glusterfs/common-utils.h>
74b1de
+#include "glusterd.h"
74b1de
+#include "glusterd-op-sm.h"
74b1de
+#include "glusterd-store.h"
74b1de
+#include "glusterd-utils.h"
74b1de
+#include "glusterd-nfs-svc.h"
74b1de
+#include "glusterd-volgen.h"
74b1de
+#include "glusterd-messages.h"
74b1de
+#include <glusterfs/syscall.h>
74b1de
+
74b1de
+#include <ctype.h>
74b1de
+
74b1de
+int
74b1de
+start_ganesha(char **op_errstr);
74b1de
+
74b1de
+typedef struct service_command {
74b1de
+    char *binary;
74b1de
+    char *service;
74b1de
+    int (*action)(struct service_command *, char *);
74b1de
+} service_command;
74b1de
+
74b1de
+/* parsing_ganesha_ha_conf will allocate the returned string
74b1de
+ * to be freed (GF_FREE) by the caller
74b1de
+ * return NULL if error or not found */
74b1de
+static char *
74b1de
+parsing_ganesha_ha_conf(const char *key)
74b1de
+{
74b1de
+#define MAX_LINE 1024
74b1de
+    char scratch[MAX_LINE * 2] = {
74b1de
+        0,
74b1de
+    };
74b1de
+    char *value = NULL, *pointer = NULL, *end_pointer = NULL;
74b1de
+    FILE *fp;
74b1de
+
74b1de
+    fp = fopen(GANESHA_HA_CONF, "r");
74b1de
+    if (fp == NULL) {
74b1de
+        gf_msg(THIS->name, GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
74b1de
+               "couldn't open the file %s", GANESHA_HA_CONF);
74b1de
+        goto end_ret;
74b1de
+    }
74b1de
+    while ((pointer = fgets(scratch, MAX_LINE, fp)) != NULL) {
74b1de
+        /* Read config file until we get matching "^[[:space:]]*key" */
74b1de
+        if (*pointer == '#') {
74b1de
+            continue;
74b1de
+        }
74b1de
+        while (isblank(*pointer)) {
74b1de
+            pointer++;
74b1de
+        }
74b1de
+        if (strncmp(pointer, key, strlen(key))) {
74b1de
+            continue;
74b1de
+        }
74b1de
+        pointer += strlen(key);
74b1de
+        /* key found : if we fail to parse, we'll return an error
74b1de
+         * rather than trying next one
74b1de
+         * - supposition : conf file is bash compatible : no space
74b1de
+         *   around the '=' */
74b1de
+        if (*pointer != '=') {
74b1de
+            gf_msg(THIS->name, GF_LOG_ERROR, errno,
74b1de
+                   GD_MSG_GET_CONFIG_INFO_FAILED, "Parsing %s failed at key %s",
74b1de
+                   GANESHA_HA_CONF, key);
74b1de
+            goto end_close;
74b1de
+        }
74b1de
+        pointer++; /* jump the '=' */
74b1de
+
74b1de
+        if (*pointer == '"' || *pointer == '\'') {
74b1de
+            /* dont get the quote */
74b1de
+            pointer++;
74b1de
+        }
74b1de
+        end_pointer = pointer;
74b1de
+        /* stop at the next closing quote or  blank/newline */
74b1de
+        do {
74b1de
+            end_pointer++;
74b1de
+        } while (!(*end_pointer == '\'' || *end_pointer == '"' ||
74b1de
+                   isspace(*end_pointer) || *end_pointer == '\0'));
74b1de
+        *end_pointer = '\0';
74b1de
+
74b1de
+        /* got it. copy it and return */
74b1de
+        value = gf_strdup(pointer);
74b1de
+        break;
74b1de
+    }
74b1de
+
74b1de
+end_close:
74b1de
+    fclose(fp);
74b1de
+end_ret:
74b1de
+    return value;
74b1de
+}
74b1de
+
74b1de
+static int
74b1de
+sc_systemctl_action(struct service_command *sc, char *command)
74b1de
+{
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    runinit(&runner);
74b1de
+    runner_add_args(&runner, sc->binary, command, sc->service, NULL);
74b1de
+    return runner_run(&runner);
74b1de
+}
74b1de
+
74b1de
+static int
74b1de
+sc_service_action(struct service_command *sc, char *command)
74b1de
+{
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    runinit(&runner);
74b1de
+    runner_add_args(&runner, sc->binary, sc->service, command, NULL);
74b1de
+    return runner_run(&runner);
74b1de
+}
74b1de
+
74b1de
+static int
74b1de
+manage_service(char *action)
74b1de
+{
74b1de
+    struct stat stbuf = {
74b1de
+        0,
74b1de
+    };
74b1de
+    int i = 0;
74b1de
+    int ret = 0;
74b1de
+    struct service_command sc_list[] = {{.binary = "/usr/bin/systemctl",
74b1de
+                                         .service = "nfs-ganesha",
74b1de
+                                         .action = sc_systemctl_action},
74b1de
+                                        {.binary = "/sbin/invoke-rc.d",
74b1de
+                                         .service = "nfs-ganesha",
74b1de
+                                         .action = sc_service_action},
74b1de
+                                        {.binary = "/sbin/service",
74b1de
+                                         .service = "nfs-ganesha",
74b1de
+                                         .action = sc_service_action},
74b1de
+                                        {.binary = NULL}};
74b1de
+
74b1de
+    while (sc_list[i].binary != NULL) {
74b1de
+        ret = sys_stat(sc_list[i].binary, &stbuf);
74b1de
+        if (ret == 0) {
74b1de
+            gf_msg_debug(THIS->name, 0, "%s found.", sc_list[i].binary);
74b1de
+            if (strcmp(sc_list[i].binary, "/usr/bin/systemctl") == 0)
74b1de
+                ret = sc_systemctl_action(&sc_list[i], action);
74b1de
+            else
74b1de
+                ret = sc_service_action(&sc_list[i], action);
74b1de
+
74b1de
+            return ret;
74b1de
+        }
74b1de
+        i++;
74b1de
+    }
74b1de
+    gf_msg(THIS->name, GF_LOG_ERROR, 0, GD_MSG_UNRECOGNIZED_SVC_MNGR,
74b1de
+           "Could not %s NFS-Ganesha.Service manager for distro"
74b1de
+           " not recognized.",
74b1de
+           action);
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+/*
74b1de
+ * Check if the cluster is a ganesha cluster or not *
74b1de
+ */
74b1de
+gf_boolean_t
74b1de
+glusterd_is_ganesha_cluster()
74b1de
+{
74b1de
+    int ret = -1;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    xlator_t *this = NULL;
74b1de
+    gf_boolean_t ret_bool = _gf_false;
74b1de
+
74b1de
+    this = THIS;
74b1de
+    GF_VALIDATE_OR_GOTO("ganesha", this, out);
74b1de
+    priv = this->private;
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, priv, out);
74b1de
+
74b1de
+    ret = dict_get_str_boolean(priv->opts, GLUSTERD_STORE_KEY_GANESHA_GLOBAL,
74b1de
+                               _gf_false);
74b1de
+    if (ret == _gf_true) {
74b1de
+        ret_bool = _gf_true;
74b1de
+        gf_msg_debug(this->name, 0, "nfs-ganesha is enabled for the cluster");
74b1de
+    } else
74b1de
+        gf_msg_debug(this->name, 0, "nfs-ganesha is disabled for the cluster");
74b1de
+
74b1de
+out:
74b1de
+    return ret_bool;
74b1de
+}
74b1de
+
74b1de
+/* Check if ganesha.enable is set to 'on', that checks if
74b1de
+ * a  particular volume is exported via NFS-Ganesha */
74b1de
+gf_boolean_t
74b1de
+glusterd_check_ganesha_export(glusterd_volinfo_t *volinfo)
74b1de
+{
74b1de
+    char *value = NULL;
74b1de
+    gf_boolean_t is_exported = _gf_false;
74b1de
+    int ret = 0;
74b1de
+
74b1de
+    ret = glusterd_volinfo_get(volinfo, "ganesha.enable", &value);
74b1de
+    if ((ret == 0) && value) {
74b1de
+        if (strcmp(value, "on") == 0) {
74b1de
+            gf_msg_debug(THIS->name, 0,
74b1de
+                         "ganesha.enable set"
74b1de
+                         " to %s",
74b1de
+                         value);
74b1de
+            is_exported = _gf_true;
74b1de
+        }
74b1de
+    }
74b1de
+    return is_exported;
74b1de
+}
74b1de
+
74b1de
+/* *
74b1de
+ * The below function is called as part of commit phase for volume set option
74b1de
+ * "ganesha.enable". If the value is "on", it creates export configuration file
74b1de
+ * and then export the volume via dbus command. Incase of "off", the volume
74b1de
+ * will be already unexported during stage phase, so it will remove the conf
74b1de
+ * file from shared storage
74b1de
+ */
74b1de
+int
74b1de
+glusterd_check_ganesha_cmd(char *key, char *value, char **errstr, dict_t *dict)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    char *volname = NULL;
74b1de
+
74b1de
+    GF_ASSERT(key);
74b1de
+    GF_ASSERT(value);
74b1de
+    GF_ASSERT(dict);
74b1de
+
74b1de
+    if ((strcmp(key, "ganesha.enable") == 0)) {
74b1de
+        if ((strcmp(value, "on")) && (strcmp(value, "off"))) {
74b1de
+            gf_asprintf(errstr,
74b1de
+                        "Invalid value"
74b1de
+                        " for volume set command. Use on/off only.");
74b1de
+            ret = -1;
74b1de
+            goto out;
74b1de
+        }
74b1de
+        if (strcmp(value, "on") == 0) {
74b1de
+            ret = glusterd_handle_ganesha_op(dict, errstr, key, value);
74b1de
+
74b1de
+        } else if (is_origin_glusterd(dict)) {
74b1de
+            ret = dict_get_str(dict, "volname", &volname);
74b1de
+            if (ret) {
74b1de
+                gf_msg("glusterd-ganesha", GF_LOG_ERROR, errno,
74b1de
+                       GD_MSG_DICT_GET_FAILED, "Unable to get volume name");
74b1de
+                goto out;
74b1de
+            }
74b1de
+            ret = manage_export_config(volname, "off", errstr);
74b1de
+        }
74b1de
+    }
74b1de
+out:
74b1de
+    if (ret) {
74b1de
+        gf_msg("glusterd-ganesha", GF_LOG_ERROR, 0,
74b1de
+               GD_MSG_NFS_GNS_OP_HANDLE_FAIL,
74b1de
+               "Handling NFS-Ganesha"
74b1de
+               " op failed.");
74b1de
+    }
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+glusterd_op_stage_set_ganesha(dict_t *dict, char **op_errstr)
74b1de
+{
74b1de
+    int ret = -1;
74b1de
+    int value = -1;
74b1de
+    gf_boolean_t option = _gf_false;
74b1de
+    char *str = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    xlator_t *this = NULL;
74b1de
+
74b1de
+    GF_ASSERT(dict);
74b1de
+    this = THIS;
74b1de
+    GF_ASSERT(this);
74b1de
+    priv = this->private;
74b1de
+    GF_ASSERT(priv);
74b1de
+
74b1de
+    value = dict_get_str_boolean(dict, "value", _gf_false);
74b1de
+    if (value == -1) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_DICT_GET_FAILED,
74b1de
+               "value not present.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+    /* This dict_get will fail if the user had never set the key before */
74b1de
+    /*Ignoring the ret value and proceeding */
74b1de
+    ret = dict_get_str(priv->opts, GLUSTERD_STORE_KEY_GANESHA_GLOBAL, &str);
74b1de
+    if (ret == -1) {
74b1de
+        gf_msg(this->name, GF_LOG_WARNING, errno, GD_MSG_DICT_GET_FAILED,
74b1de
+               "Global dict not present.");
74b1de
+        ret = 0;
74b1de
+        goto out;
74b1de
+    }
74b1de
+    /* Validity of the value is already checked */
74b1de
+    ret = gf_string2boolean(str, &option);
74b1de
+    /* Check if the feature is already enabled, fail in that case */
74b1de
+    if (value == option) {
74b1de
+        gf_asprintf(op_errstr, "nfs-ganesha is already %sd.", str);
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    if (value) {
74b1de
+        ret = start_ganesha(op_errstr);
74b1de
+        if (ret) {
74b1de
+            gf_msg(THIS->name, GF_LOG_ERROR, 0, GD_MSG_NFS_GNS_START_FAIL,
74b1de
+                   "Could not start NFS-Ganesha");
74b1de
+        }
74b1de
+    } else {
74b1de
+        ret = stop_ganesha(op_errstr);
74b1de
+        if (ret)
74b1de
+            gf_msg_debug(THIS->name, 0,
74b1de
+                         "Could not stop "
74b1de
+                         "NFS-Ganesha.");
74b1de
+    }
74b1de
+
74b1de
+out:
74b1de
+
74b1de
+    if (ret) {
74b1de
+        if (!(*op_errstr)) {
74b1de
+            *op_errstr = gf_strdup("Error, Validation Failed");
74b1de
+            gf_msg_debug(this->name, 0, "Error, Cannot Validate option :%s",
74b1de
+                         GLUSTERD_STORE_KEY_GANESHA_GLOBAL);
74b1de
+        } else {
74b1de
+            gf_msg_debug(this->name, 0, "Error, Cannot Validate option");
74b1de
+        }
74b1de
+    }
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+glusterd_op_set_ganesha(dict_t *dict, char **errstr)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    xlator_t *this = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    char *key = NULL;
74b1de
+    char *value = NULL;
74b1de
+    char *next_version = NULL;
74b1de
+
74b1de
+    this = THIS;
74b1de
+    GF_ASSERT(this);
74b1de
+    GF_ASSERT(dict);
74b1de
+
74b1de
+    priv = this->private;
74b1de
+    GF_ASSERT(priv);
74b1de
+
74b1de
+    ret = dict_get_str(dict, "key", &key);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_DICT_GET_FAILED,
74b1de
+               "Couldn't get key in global option set");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = dict_get_str(dict, "value", &value);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_DICT_GET_FAILED,
74b1de
+               "Couldn't get value in global option set");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = glusterd_handle_ganesha_op(dict, errstr, key, value);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_NFS_GNS_SETUP_FAIL,
74b1de
+               "Initial NFS-Ganesha set up failed");
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+    ret = dict_set_dynstr_with_alloc(priv->opts,
74b1de
+                                     GLUSTERD_STORE_KEY_GANESHA_GLOBAL, value);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_WARNING, errno, GD_MSG_DICT_SET_FAILED,
74b1de
+               "Failed to set"
74b1de
+               " nfs-ganesha in dict.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+    ret = glusterd_get_next_global_opt_version_str(priv->opts, &next_version);
74b1de
+    if (ret) {
74b1de
+        gf_msg_debug(THIS->name, 0,
74b1de
+                     "Could not fetch "
74b1de
+                     " global op version");
74b1de
+        goto out;
74b1de
+    }
74b1de
+    ret = dict_set_str(priv->opts, GLUSTERD_GLOBAL_OPT_VERSION, next_version);
74b1de
+    if (ret)
74b1de
+        goto out;
74b1de
+
74b1de
+    ret = glusterd_store_options(this, priv->opts);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_STORE_FAIL,
74b1de
+               "Failed to store options");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+out:
74b1de
+    gf_msg_debug(this->name, 0, "returning %d", ret);
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+/* Following function parse GANESHA_HA_CONF
74b1de
+ * The sample file looks like below,
74b1de
+ * HA_NAME="ganesha-ha-360"
74b1de
+ * HA_VOL_NAME="ha-state"
74b1de
+ * HA_CLUSTER_NODES="server1,server2"
74b1de
+ * VIP_rhs_1="10.x.x.x"
74b1de
+ * VIP_rhs_2="10.x.x.x." */
74b1de
+
74b1de
+/* Check if the localhost is listed as one of nfs-ganesha nodes */
74b1de
+gf_boolean_t
74b1de
+check_host_list(void)
74b1de
+{
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    char *hostname, *hostlist;
74b1de
+    gf_boolean_t ret = _gf_false;
74b1de
+    xlator_t *this = NULL;
74b1de
+
74b1de
+    this = THIS;
74b1de
+    priv = THIS->private;
74b1de
+    GF_ASSERT(priv);
74b1de
+
74b1de
+    hostlist = parsing_ganesha_ha_conf("HA_CLUSTER_NODES");
74b1de
+    if (hostlist == NULL) {
74b1de
+        gf_msg(this->name, GF_LOG_INFO, errno, GD_MSG_GET_CONFIG_INFO_FAILED,
74b1de
+               "couldn't get HA_CLUSTER_NODES from file %s", GANESHA_HA_CONF);
74b1de
+        return _gf_false;
74b1de
+    }
74b1de
+
74b1de
+    /* Hostlist is a comma separated list now */
74b1de
+    hostname = strtok(hostlist, ",");
74b1de
+    while (hostname != NULL) {
74b1de
+        ret = gf_is_local_addr(hostname);
74b1de
+        if (ret) {
74b1de
+            gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_NFS_GNS_HOST_FOUND,
74b1de
+                   "ganesha host found "
74b1de
+                   "Hostname is %s",
74b1de
+                   hostname);
74b1de
+            break;
74b1de
+        }
74b1de
+        hostname = strtok(NULL, ",");
74b1de
+    }
74b1de
+
74b1de
+    GF_FREE(hostlist);
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+manage_export_config(char *volname, char *value, char **op_errstr)
74b1de
+{
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+    int ret = -1;
74b1de
+
74b1de
+    GF_ASSERT(volname);
74b1de
+    runinit(&runner);
74b1de
+    runner_add_args(&runner, "sh", GANESHA_PREFIX "/create-export-ganesha.sh",
74b1de
+                    CONFDIR, value, volname, NULL);
74b1de
+    ret = runner_run(&runner);
74b1de
+
74b1de
+    if (ret)
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "Failed to create"
74b1de
+                    " NFS-Ganesha export config file.");
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+/* Exports and unexports a particular volume via NFS-Ganesha */
74b1de
+int
74b1de
+ganesha_manage_export(dict_t *dict, char *value, char **op_errstr)
74b1de
+{
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+    int ret = -1;
74b1de
+    glusterd_volinfo_t *volinfo = NULL;
74b1de
+    dict_t *vol_opts = NULL;
74b1de
+    char *volname = NULL;
74b1de
+    xlator_t *this = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    gf_boolean_t option = _gf_false;
74b1de
+
74b1de
+    runinit(&runner);
74b1de
+    this = THIS;
74b1de
+    GF_ASSERT(this);
74b1de
+    priv = this->private;
74b1de
+
74b1de
+    GF_ASSERT(value);
74b1de
+    GF_ASSERT(dict);
74b1de
+    GF_ASSERT(priv);
74b1de
+
74b1de
+    ret = dict_get_str(dict, "volname", &volname);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_DICT_GET_FAILED,
74b1de
+               "Unable to get volume name");
74b1de
+        goto out;
74b1de
+    }
74b1de
+    ret = gf_string2boolean(value, &option);
74b1de
+    if (ret == -1) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_INVALID_ENTRY,
74b1de
+               "invalid value.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = glusterd_volinfo_find(volname, &volinfo);
74b1de
+    if (ret) {
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_VOL_NOT_FOUND,
74b1de
+               FMTSTR_CHECK_VOL_EXISTS, volname);
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = glusterd_check_ganesha_export(volinfo);
74b1de
+    if (ret && option) {
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "ganesha.enable "
74b1de
+                    "is already 'on'.");
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+
74b1de
+    } else if (!option && !ret) {
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "ganesha.enable "
74b1de
+                    "is already 'off'.");
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    /* Check if global option is enabled, proceed only then */
74b1de
+    ret = dict_get_str_boolean(priv->opts, GLUSTERD_STORE_KEY_GANESHA_GLOBAL,
74b1de
+                               _gf_false);
74b1de
+    if (ret == -1) {
74b1de
+        gf_msg_debug(this->name, 0,
74b1de
+                     "Failed to get "
74b1de
+                     "global option dict.");
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "The option "
74b1de
+                    "nfs-ganesha should be "
74b1de
+                    "enabled before setting ganesha.enable.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+    if (!ret) {
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "The option "
74b1de
+                    "nfs-ganesha should be "
74b1de
+                    "enabled before setting ganesha.enable.");
74b1de
+        ret = -1;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    /* *
74b1de
+     * Create the export file from the node where ganesha.enable "on"
74b1de
+     * is executed
74b1de
+     * */
74b1de
+    if (option) {
74b1de
+        ret = manage_export_config(volname, "on", op_errstr);
74b1de
+        if (ret) {
74b1de
+            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_EXPORT_FILE_CREATE_FAIL,
74b1de
+                   "Failed to create"
74b1de
+                   "export file for NFS-Ganesha\n");
74b1de
+            goto out;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+    if (check_host_list()) {
74b1de
+        runner_add_args(&runner, "sh", GANESHA_PREFIX "/dbus-send.sh", CONFDIR,
74b1de
+                        value, volname, NULL);
74b1de
+        ret = runner_run(&runner);
74b1de
+        if (ret) {
74b1de
+            gf_asprintf(op_errstr,
74b1de
+                        "Dynamic export"
74b1de
+                        " addition/deletion failed."
74b1de
+                        " Please see log file for details");
74b1de
+            goto out;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+    vol_opts = volinfo->dict;
74b1de
+    ret = dict_set_dynstr_with_alloc(vol_opts, "features.cache-invalidation",
74b1de
+                                     value);
74b1de
+    if (ret)
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "Cache-invalidation could not"
74b1de
+                    " be set to %s.",
74b1de
+                    value);
74b1de
+    ret = glusterd_store_volinfo(volinfo, GLUSTERD_VOLINFO_VER_AC_INCREMENT);
74b1de
+    if (ret)
74b1de
+        gf_asprintf(op_errstr, "failed to store volinfo for %s",
74b1de
+                    volinfo->volname);
74b1de
+
74b1de
+out:
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+tear_down_cluster(gf_boolean_t run_teardown)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+    struct stat st = {
74b1de
+        0,
74b1de
+    };
74b1de
+    DIR *dir = NULL;
74b1de
+    struct dirent *entry = NULL;
74b1de
+    struct dirent scratch[2] = {
74b1de
+        {
74b1de
+            0,
74b1de
+        },
74b1de
+    };
74b1de
+    char path[PATH_MAX] = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    if (run_teardown) {
74b1de
+        runinit(&runner);
74b1de
+        runner_add_args(&runner, "sh", GANESHA_PREFIX "/ganesha-ha.sh",
74b1de
+                        "teardown", CONFDIR, NULL);
74b1de
+        ret = runner_run(&runner);
74b1de
+        /* *
74b1de
+         * Remove all the entries in CONFDIR expect ganesha.conf and
74b1de
+         * ganesha-ha.conf
74b1de
+         */
74b1de
+        dir = sys_opendir(CONFDIR);
74b1de
+        if (!dir) {
74b1de
+            gf_msg_debug(THIS->name, 0,
74b1de
+                         "Failed to open directory %s. "
74b1de
+                         "Reason : %s",
74b1de
+                         CONFDIR, strerror(errno));
74b1de
+            ret = 0;
74b1de
+            goto out;
74b1de
+        }
74b1de
+
74b1de
+        GF_SKIP_IRRELEVANT_ENTRIES(entry, dir, scratch);
74b1de
+        while (entry) {
74b1de
+            snprintf(path, PATH_MAX, "%s/%s", CONFDIR, entry->d_name);
74b1de
+            ret = sys_lstat(path, &st);
74b1de
+            if (ret == -1) {
74b1de
+                gf_msg_debug(THIS->name, 0,
74b1de
+                             "Failed to stat entry %s :"
74b1de
+                             " %s",
74b1de
+                             path, strerror(errno));
74b1de
+                goto out;
74b1de
+            }
74b1de
+
74b1de
+            if (strcmp(entry->d_name, "ganesha.conf") == 0 ||
74b1de
+                strcmp(entry->d_name, "ganesha-ha.conf") == 0)
74b1de
+                gf_msg_debug(THIS->name, 0,
74b1de
+                             " %s is not required"
74b1de
+                             " to remove",
74b1de
+                             path);
74b1de
+            else if (S_ISDIR(st.st_mode))
74b1de
+                ret = recursive_rmdir(path);
74b1de
+            else
74b1de
+                ret = sys_unlink(path);
74b1de
+
74b1de
+            if (ret) {
74b1de
+                gf_msg_debug(THIS->name, 0,
74b1de
+                             " Failed to remove %s. "
74b1de
+                             "Reason : %s",
74b1de
+                             path, strerror(errno));
74b1de
+            }
74b1de
+
74b1de
+            gf_msg_debug(THIS->name, 0, "%s %s",
74b1de
+                         ret ? "Failed to remove" : "Removed", entry->d_name);
74b1de
+            GF_SKIP_IRRELEVANT_ENTRIES(entry, dir, scratch);
74b1de
+        }
74b1de
+
74b1de
+        ret = sys_closedir(dir);
74b1de
+        if (ret) {
74b1de
+            gf_msg_debug(THIS->name, 0,
74b1de
+                         "Failed to close dir %s. Reason :"
74b1de
+                         " %s",
74b1de
+                         CONFDIR, strerror(errno));
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+out:
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+setup_cluster(gf_boolean_t run_setup)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    if (run_setup) {
74b1de
+        runinit(&runner);
74b1de
+        runner_add_args(&runner, "sh", GANESHA_PREFIX "/ganesha-ha.sh", "setup",
74b1de
+                        CONFDIR, NULL);
74b1de
+        ret = runner_run(&runner);
74b1de
+    }
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+static int
74b1de
+teardown(gf_boolean_t run_teardown, char **op_errstr)
74b1de
+{
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+    int ret = 1;
74b1de
+    glusterd_volinfo_t *volinfo = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    dict_t *vol_opts = NULL;
74b1de
+
74b1de
+    priv = THIS->private;
74b1de
+
74b1de
+    ret = tear_down_cluster(run_teardown);
74b1de
+    if (ret == -1) {
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "Cleanup of NFS-Ganesha"
74b1de
+                    " HA config failed.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    runinit(&runner);
74b1de
+    runner_add_args(&runner, "sh", GANESHA_PREFIX "/ganesha-ha.sh", "cleanup",
74b1de
+                    CONFDIR, NULL);
74b1de
+    ret = runner_run(&runner);
74b1de
+    if (ret)
74b1de
+        gf_msg_debug(THIS->name, 0,
74b1de
+                     "Could not clean up"
74b1de
+                     " NFS-Ganesha related config");
74b1de
+
74b1de
+    cds_list_for_each_entry(volinfo, &priv->volumes, vol_list)
74b1de
+    {
74b1de
+        vol_opts = volinfo->dict;
74b1de
+        /* All the volumes exported via NFS-Ganesha will be
74b1de
+        unexported, hence setting the appropriate keys */
74b1de
+        ret = dict_set_str(vol_opts, "features.cache-invalidation", "off");
74b1de
+        if (ret)
74b1de
+            gf_msg(THIS->name, GF_LOG_WARNING, errno, GD_MSG_DICT_SET_FAILED,
74b1de
+                   "Could not set features.cache-invalidation "
74b1de
+                   "to off for %s",
74b1de
+                   volinfo->volname);
74b1de
+
74b1de
+        ret = dict_set_str(vol_opts, "ganesha.enable", "off");
74b1de
+        if (ret)
74b1de
+            gf_msg(THIS->name, GF_LOG_WARNING, errno, GD_MSG_DICT_SET_FAILED,
74b1de
+                   "Could not set ganesha.enable to off for %s",
74b1de
+                   volinfo->volname);
74b1de
+
74b1de
+        ret = glusterd_store_volinfo(volinfo,
74b1de
+                                     GLUSTERD_VOLINFO_VER_AC_INCREMENT);
74b1de
+        if (ret)
74b1de
+            gf_msg(THIS->name, GF_LOG_WARNING, 0, GD_MSG_VOLINFO_SET_FAIL,
74b1de
+                   "failed to store volinfo for %s", volinfo->volname);
74b1de
+    }
74b1de
+out:
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+stop_ganesha(char **op_errstr)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    runinit(&runner);
74b1de
+    runner_add_args(&runner, "sh", GANESHA_PREFIX "/ganesha-ha.sh",
74b1de
+                    "--setup-ganesha-conf-files", CONFDIR, "no", NULL);
74b1de
+    ret = runner_run(&runner);
74b1de
+    if (ret) {
74b1de
+        gf_asprintf(op_errstr,
74b1de
+                    "removal of symlink ganesha.conf "
74b1de
+                    "in /etc/ganesha failed");
74b1de
+    }
74b1de
+
74b1de
+    if (check_host_list()) {
74b1de
+        ret = manage_service("stop");
74b1de
+        if (ret)
74b1de
+            gf_asprintf(op_errstr,
74b1de
+                        "NFS-Ganesha service could not"
74b1de
+                        "be stopped.");
74b1de
+    }
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+start_ganesha(char **op_errstr)
74b1de
+{
74b1de
+    int ret = -1;
74b1de
+    dict_t *vol_opts = NULL;
74b1de
+    glusterd_volinfo_t *volinfo = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    runner_t runner = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    priv = THIS->private;
74b1de
+    GF_ASSERT(priv);
74b1de
+
74b1de
+    cds_list_for_each_entry(volinfo, &priv->volumes, vol_list)
74b1de
+    {
74b1de
+        vol_opts = volinfo->dict;
74b1de
+        /* Gluster-nfs has to be disabled across the trusted pool */
74b1de
+        /* before attempting to start nfs-ganesha */
74b1de
+        ret = dict_set_str(vol_opts, NFS_DISABLE_MAP_KEY, "on");
74b1de
+        if (ret)
74b1de
+            goto out;
74b1de
+
74b1de
+        ret = glusterd_store_volinfo(volinfo,
74b1de
+                                     GLUSTERD_VOLINFO_VER_AC_INCREMENT);
74b1de
+        if (ret) {
74b1de
+            *op_errstr = gf_strdup(
74b1de
+                "Failed to store the "
74b1de
+                "Volume information");
74b1de
+            goto out;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+    /* If the nfs svc is not initialized it means that the service is not
74b1de
+     * running, hence we can skip the process of stopping gluster-nfs
74b1de
+     * service
74b1de
+     */
74b1de
+    if (priv->nfs_svc.inited) {
74b1de
+        ret = priv->nfs_svc.stop(&(priv->nfs_svc), SIGKILL);
74b1de
+        if (ret) {
74b1de
+            ret = -1;
74b1de
+            gf_asprintf(op_errstr,
74b1de
+                        "Gluster-NFS service could"
74b1de
+                        "not be stopped, exiting.");
74b1de
+            goto out;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+    if (check_host_list()) {
74b1de
+        runinit(&runner);
74b1de
+        runner_add_args(&runner, "sh", GANESHA_PREFIX "/ganesha-ha.sh",
74b1de
+                        "--setup-ganesha-conf-files", CONFDIR, "yes", NULL);
74b1de
+        ret = runner_run(&runner);
74b1de
+        if (ret) {
74b1de
+            gf_asprintf(op_errstr,
74b1de
+                        "creation of symlink ganesha.conf "
74b1de
+                        "in /etc/ganesha failed");
74b1de
+            goto out;
74b1de
+        }
74b1de
+        ret = manage_service("start");
74b1de
+        if (ret)
74b1de
+            gf_asprintf(op_errstr,
74b1de
+                        "NFS-Ganesha failed to start."
74b1de
+                        "Please see log file for details");
74b1de
+    }
74b1de
+
74b1de
+out:
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+static int
74b1de
+pre_setup(gf_boolean_t run_setup, char **op_errstr)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+
74b1de
+    ret = check_host_list();
74b1de
+
74b1de
+    if (ret) {
74b1de
+        ret = setup_cluster(run_setup);
74b1de
+        if (ret == -1)
74b1de
+            gf_asprintf(op_errstr,
74b1de
+                        "Failed to set up HA "
74b1de
+                        "config for NFS-Ganesha. "
74b1de
+                        "Please check the log file for details");
74b1de
+    }
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+glusterd_handle_ganesha_op(dict_t *dict, char **op_errstr, char *key,
74b1de
+                           char *value)
74b1de
+{
74b1de
+    int32_t ret = -1;
74b1de
+    gf_boolean_t option = _gf_false;
74b1de
+
74b1de
+    GF_ASSERT(dict);
74b1de
+    GF_ASSERT(op_errstr);
74b1de
+    GF_ASSERT(key);
74b1de
+    GF_ASSERT(value);
74b1de
+
74b1de
+    if (strcmp(key, "ganesha.enable") == 0) {
74b1de
+        ret = ganesha_manage_export(dict, value, op_errstr);
74b1de
+        if (ret < 0)
74b1de
+            goto out;
74b1de
+    }
74b1de
+
74b1de
+    /* It is possible that the key might not be set */
74b1de
+    ret = gf_string2boolean(value, &option);
74b1de
+    if (ret == -1) {
74b1de
+        gf_asprintf(op_errstr, "Invalid value in key-value pair.");
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    if (strcmp(key, GLUSTERD_STORE_KEY_GANESHA_GLOBAL) == 0) {
74b1de
+        /* *
74b1de
+         * The set up/teardown of pcs cluster should be performed only
74b1de
+         * once. This will done on the node in which the cli command
74b1de
+         * 'gluster nfs-ganesha <enable/disable>' got executed. So that
74b1de
+         * node should part of ganesha HA cluster
74b1de
+         */
74b1de
+        if (option) {
74b1de
+            ret = pre_setup(is_origin_glusterd(dict), op_errstr);
74b1de
+            if (ret < 0)
74b1de
+                goto out;
74b1de
+        } else {
74b1de
+            ret = teardown(is_origin_glusterd(dict), op_errstr);
74b1de
+            if (ret < 0)
74b1de
+                goto out;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+out:
74b1de
+    return ret;
74b1de
+}
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-handler.c b/xlators/mgmt/glusterd/src/glusterd-handler.c
74b1de
index de44af7..528993c 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-handler.c
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-handler.c
74b1de
@@ -1911,6 +1911,83 @@ glusterd_op_begin(rpcsvc_request_t *req, glusterd_op_t op, void *ctx,
74b1de
     return ret;
74b1de
 }
74b1de
 
74b1de
+int
74b1de
+__glusterd_handle_ganesha_cmd(rpcsvc_request_t *req)
74b1de
+{
74b1de
+    int32_t ret = -1;
74b1de
+    gf_cli_req cli_req = {{
74b1de
+        0,
74b1de
+    }};
74b1de
+    dict_t *dict = NULL;
74b1de
+    glusterd_op_t cli_op = GD_OP_GANESHA;
74b1de
+    char *op_errstr = NULL;
74b1de
+    char err_str[2048] = {
74b1de
+        0,
74b1de
+    };
74b1de
+    xlator_t *this = NULL;
74b1de
+
74b1de
+    this = THIS;
74b1de
+    GF_ASSERT(this);
74b1de
+
74b1de
+    GF_ASSERT(req);
74b1de
+
74b1de
+    ret = xdr_to_generic(req->msg[0], &cli_req, (xdrproc_t)xdr_gf_cli_req);
74b1de
+    if (ret < 0) {
74b1de
+        snprintf(err_str, sizeof(err_str),
74b1de
+                 "Failed to decode "
74b1de
+                 "request received from cli");
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_REQ_DECODE_FAIL, "%s",
74b1de
+               err_str);
74b1de
+        req->rpc_err = GARBAGE_ARGS;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    if (cli_req.dict.dict_len) {
74b1de
+        /* Unserialize the dictionary */
74b1de
+        dict = dict_new();
74b1de
+        if (!dict) {
74b1de
+            ret = -1;
74b1de
+            goto out;
74b1de
+        }
74b1de
+
74b1de
+        ret = dict_unserialize(cli_req.dict.dict_val, cli_req.dict.dict_len,
74b1de
+                               &dict);
74b1de
+        if (ret < 0) {
74b1de
+            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_UNSERIALIZE_FAIL,
74b1de
+                   "failed to "
74b1de
+                   "unserialize req-buffer to dictionary");
74b1de
+            snprintf(err_str, sizeof(err_str),
74b1de
+                     "Unable to decode "
74b1de
+                     "the command");
74b1de
+            goto out;
74b1de
+        } else {
74b1de
+            dict->extra_stdfree = cli_req.dict.dict_val;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+    gf_msg_trace(this->name, 0, "Received global option request");
74b1de
+
74b1de
+    ret = glusterd_op_begin_synctask(req, GD_OP_GANESHA, dict);
74b1de
+out:
74b1de
+    if (ret) {
74b1de
+        if (err_str[0] == '\0')
74b1de
+            snprintf(err_str, sizeof(err_str), "Operation failed");
74b1de
+        ret = glusterd_op_send_cli_response(cli_op, ret, 0, req, dict, err_str);
74b1de
+    }
74b1de
+    if (op_errstr)
74b1de
+        GF_FREE(op_errstr);
74b1de
+    if (dict)
74b1de
+        dict_unref(dict);
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
+int
74b1de
+glusterd_handle_ganesha_cmd(rpcsvc_request_t *req)
74b1de
+{
74b1de
+    return glusterd_big_locked_handler(req, __glusterd_handle_ganesha_cmd);
74b1de
+}
74b1de
+
74b1de
 static int
74b1de
 __glusterd_handle_reset_volume(rpcsvc_request_t *req)
74b1de
 {
74b1de
@@ -6644,6 +6721,8 @@ rpcsvc_actor_t gd_svc_cli_actors[GLUSTER_CLI_MAXVALUE] = {
74b1de
     [GLUSTER_CLI_BARRIER_VOLUME] = {"BARRIER_VOLUME",
74b1de
                                     GLUSTER_CLI_BARRIER_VOLUME,
74b1de
                                     glusterd_handle_barrier, NULL, 0, DRC_NA},
74b1de
+    [GLUSTER_CLI_GANESHA] = {"GANESHA", GLUSTER_CLI_GANESHA,
74b1de
+                             glusterd_handle_ganesha_cmd, NULL, 0, DRC_NA},
74b1de
     [GLUSTER_CLI_GET_VOL_OPT] = {"GET_VOL_OPT", GLUSTER_CLI_GET_VOL_OPT,
74b1de
                                  glusterd_handle_get_vol_opt, NULL, 0, DRC_NA},
74b1de
     [GLUSTER_CLI_BITROT] = {"BITROT", GLUSTER_CLI_BITROT,
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-messages.h b/xlators/mgmt/glusterd/src/glusterd-messages.h
74b1de
index 1a4bd54..9558480 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-messages.h
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-messages.h
74b1de
@@ -297,7 +297,7 @@ GLFS_MSGID(
74b1de
     GD_MSG_LOCALTIME_LOGGING_VOL_OPT_VALIDATE_FAIL,
74b1de
     GD_MSG_LOCALTIME_LOGGING_ENABLE, GD_MSG_LOCALTIME_LOGGING_DISABLE,
74b1de
     GD_MSG_PORTS_EXHAUSTED, GD_MSG_CHANGELOG_GET_FAIL,
74b1de
-    GD_MSG_MANAGER_FUNCTION_FAILED,
74b1de
+    GD_MSG_MANAGER_FUNCTION_FAILED, GD_MSG_NFS_GANESHA_DISABLED,
74b1de
     GD_MSG_DAEMON_LOG_LEVEL_VOL_OPT_VALIDATE_FAIL);
74b1de
 
74b1de
 #endif /* !_GLUSTERD_MESSAGES_H_ */
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-op-sm.c b/xlators/mgmt/glusterd/src/glusterd-op-sm.c
74b1de
index 12d857a..a630c48 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-op-sm.c
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-op-sm.c
74b1de
@@ -1176,6 +1176,13 @@ glusterd_op_stage_set_volume(dict_t *dict, char **op_errstr)
74b1de
         if (ret)
74b1de
             goto out;
74b1de
 
74b1de
+        if ((strcmp(key, "ganesha.enable") == 0) &&
74b1de
+            (strcmp(value, "off") == 0)) {
74b1de
+            ret = ganesha_manage_export(dict, "off", op_errstr);
74b1de
+            if (ret)
74b1de
+                goto out;
74b1de
+        }
74b1de
+
74b1de
         ret = glusterd_check_quota_cmd(key, value, errstr, sizeof(errstr));
74b1de
         if (ret)
74b1de
             goto out;
74b1de
@@ -1677,6 +1684,20 @@ glusterd_op_stage_reset_volume(dict_t *dict, char **op_errstr)
74b1de
         goto out;
74b1de
     }
74b1de
 
74b1de
+    /* *
74b1de
+     * If key ganesha.enable is set, then volume should be unexported from
74b1de
+     * ganesha server. Also it is a volume-level option, perform only when
74b1de
+     * volume name not equal to "all"(in other words if volinfo != NULL)
74b1de
+     */
74b1de
+    if (volinfo && (!strcmp(key, "all") || !strcmp(key, "ganesha.enable"))) {
74b1de
+        if (glusterd_check_ganesha_export(volinfo)) {
74b1de
+            ret = ganesha_manage_export(dict, "off", op_errstr);
74b1de
+            if (ret)
74b1de
+                gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_NFS_GNS_RESET_FAIL,
74b1de
+                       "Could not reset ganesha.enable key");
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
     if (strcmp(key, "all")) {
74b1de
         exists = glusterd_check_option_exists(key, &key_fixed);
74b1de
         if (exists == -1) {
74b1de
@@ -2393,6 +2414,15 @@ glusterd_op_reset_volume(dict_t *dict, char **op_rspstr)
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    if (!strcmp(key, "ganesha.enable") || !strcmp(key, "all")) {
74b1de
+        if (glusterd_check_ganesha_export(volinfo)) {
74b1de
+            ret = manage_export_config(volname, "off", op_rspstr);
74b1de
+            if (ret)
74b1de
+                gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_NFS_GNS_RESET_FAIL,
74b1de
+                       "Could not reset ganesha.enable key");
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
 out:
74b1de
     GF_FREE(key_fixed);
74b1de
     if (quorum_action)
74b1de
@@ -2964,6 +2994,10 @@ glusterd_op_set_volume(dict_t *dict, char **errstr)
74b1de
             }
74b1de
         }
74b1de
 
74b1de
+        ret = glusterd_check_ganesha_cmd(key, value, errstr, dict);
74b1de
+        if (ret == -1)
74b1de
+            goto out;
74b1de
+
74b1de
         if (!is_key_glusterd_hooks_friendly(key)) {
74b1de
             ret = glusterd_check_option_exists(key, &key_fixed);
74b1de
             GF_ASSERT(ret);
74b1de
@@ -4494,7 +4528,8 @@ glusterd_op_build_payload(dict_t **req, char **op_errstr, dict_t *op_ctx)
74b1de
 
74b1de
         case GD_OP_SYNC_VOLUME:
74b1de
         case GD_OP_COPY_FILE:
74b1de
-        case GD_OP_SYS_EXEC: {
74b1de
+        case GD_OP_SYS_EXEC:
74b1de
+        case GD_OP_GANESHA: {
74b1de
             dict_copy(dict, req_dict);
74b1de
         } break;
74b1de
 
74b1de
@@ -5944,6 +5979,10 @@ glusterd_op_stage_validate(glusterd_op_t op, dict_t *dict, char **op_errstr,
74b1de
             ret = glusterd_op_stage_set_volume(dict, op_errstr);
74b1de
             break;
74b1de
 
74b1de
+        case GD_OP_GANESHA:
74b1de
+            ret = glusterd_op_stage_set_ganesha(dict, op_errstr);
74b1de
+            break;
74b1de
+
74b1de
         case GD_OP_RESET_VOLUME:
74b1de
             ret = glusterd_op_stage_reset_volume(dict, op_errstr);
74b1de
             break;
74b1de
@@ -6074,7 +6113,9 @@ glusterd_op_commit_perform(glusterd_op_t op, dict_t *dict, char **op_errstr,
74b1de
         case GD_OP_SET_VOLUME:
74b1de
             ret = glusterd_op_set_volume(dict, op_errstr);
74b1de
             break;
74b1de
-
74b1de
+        case GD_OP_GANESHA:
74b1de
+            ret = glusterd_op_set_ganesha(dict, op_errstr);
74b1de
+            break;
74b1de
         case GD_OP_RESET_VOLUME:
74b1de
             ret = glusterd_op_reset_volume(dict, op_errstr);
74b1de
             break;
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c b/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c
74b1de
index 2958443..041946d 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-snapshot-utils.c
74b1de
@@ -3788,6 +3788,148 @@ out:
74b1de
     return ret;
74b1de
 }
74b1de
 
74b1de
+/* *
74b1de
+ * Here there are two possibilities, either destination is snaphot or
74b1de
+ * clone. In the case of snapshot nfs_ganesha export file will be copied
74b1de
+ * to snapdir. If it is clone , then new export file will be created for
74b1de
+ * the clone in the GANESHA_EXPORT_DIRECTORY, replacing occurences of
74b1de
+ * volname with clonename
74b1de
+ */
74b1de
+int
74b1de
+glusterd_copy_nfs_ganesha_file(glusterd_volinfo_t *src_vol,
74b1de
+                               glusterd_volinfo_t *dest_vol)
74b1de
+{
74b1de
+    int32_t ret = -1;
74b1de
+    char snap_dir[PATH_MAX] = {
74b1de
+        0,
74b1de
+    };
74b1de
+    char src_path[PATH_MAX] = {
74b1de
+        0,
74b1de
+    };
74b1de
+    char dest_path[PATH_MAX] = {
74b1de
+        0,
74b1de
+    };
74b1de
+    char buffer[BUFSIZ] = {
74b1de
+        0,
74b1de
+    };
74b1de
+    char *find_ptr = NULL;
74b1de
+    char *buff_ptr = NULL;
74b1de
+    char *tmp_ptr = NULL;
74b1de
+    xlator_t *this = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    struct stat stbuf = {
74b1de
+        0,
74b1de
+    };
74b1de
+    FILE *src = NULL;
74b1de
+    FILE *dest = NULL;
74b1de
+
74b1de
+    this = THIS;
74b1de
+    GF_VALIDATE_OR_GOTO("snapshot", this, out);
74b1de
+    priv = this->private;
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, priv, out);
74b1de
+
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, src_vol, out);
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, dest_vol, out);
74b1de
+
74b1de
+    if (glusterd_check_ganesha_export(src_vol) == _gf_false) {
74b1de
+        gf_msg_debug(this->name, 0,
74b1de
+                     "%s is not exported via "
74b1de
+                     "NFS-Ganesha. Skipping copy of export conf.",
74b1de
+                     src_vol->volname);
74b1de
+        ret = 0;
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    if (src_vol->is_snap_volume) {
74b1de
+        GLUSTERD_GET_SNAP_DIR(snap_dir, src_vol->snapshot, priv);
74b1de
+        ret = snprintf(src_path, PATH_MAX, "%s/export.%s.conf", snap_dir,
74b1de
+                       src_vol->snapshot->snapname);
74b1de
+    } else {
74b1de
+        ret = snprintf(src_path, PATH_MAX, "%s/export.%s.conf",
74b1de
+                       GANESHA_EXPORT_DIRECTORY, src_vol->volname);
74b1de
+    }
74b1de
+    if (ret < 0 || ret >= PATH_MAX)
74b1de
+        goto out;
74b1de
+
74b1de
+    ret = sys_lstat(src_path, &stbuf);
74b1de
+    if (ret) {
74b1de
+        /*
74b1de
+         * This code path is hit, only when the src_vol is being *
74b1de
+         * exported via NFS-Ganesha. So if the conf file is not  *
74b1de
+         * available, we fail the snapshot operation.            *
74b1de
+         */
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
74b1de
+               "Stat on %s failed with %s", src_path, strerror(errno));
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    if (dest_vol->is_snap_volume) {
74b1de
+        memset(snap_dir, 0, PATH_MAX);
74b1de
+        GLUSTERD_GET_SNAP_DIR(snap_dir, dest_vol->snapshot, priv);
74b1de
+        ret = snprintf(dest_path, sizeof(dest_path), "%s/export.%s.conf",
74b1de
+                       snap_dir, dest_vol->snapshot->snapname);
74b1de
+        if (ret < 0)
74b1de
+            goto out;
74b1de
+
74b1de
+        ret = glusterd_copy_file(src_path, dest_path);
74b1de
+        if (ret) {
74b1de
+            gf_msg(this->name, GF_LOG_ERROR, ENOMEM, GD_MSG_NO_MEMORY,
74b1de
+                   "Failed to copy %s in %s", src_path, dest_path);
74b1de
+            goto out;
74b1de
+        }
74b1de
+
74b1de
+    } else {
74b1de
+        ret = snprintf(dest_path, sizeof(dest_path), "%s/export.%s.conf",
74b1de
+                       GANESHA_EXPORT_DIRECTORY, dest_vol->volname);
74b1de
+        if (ret < 0)
74b1de
+            goto out;
74b1de
+
74b1de
+        src = fopen(src_path, "r");
74b1de
+        dest = fopen(dest_path, "w");
74b1de
+
74b1de
+        if (!src || !dest) {
74b1de
+            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_FILE_OP_FAILED,
74b1de
+                   "Failed to open %s", dest ? src_path : dest_path);
74b1de
+            ret = -1;
74b1de
+            goto out;
74b1de
+        }
74b1de
+
74b1de
+        /* *
74b1de
+         * if the source volume is snapshot, the export conf file
74b1de
+         * consists of orginal volname
74b1de
+         */
74b1de
+        if (src_vol->is_snap_volume)
74b1de
+            find_ptr = gf_strdup(src_vol->parent_volname);
74b1de
+        else
74b1de
+            find_ptr = gf_strdup(src_vol->volname);
74b1de
+
74b1de
+        if (!find_ptr)
74b1de
+            goto out;
74b1de
+
74b1de
+        /* Replacing volname with clonename */
74b1de
+        while (fgets(buffer, BUFSIZ, src)) {
74b1de
+            buff_ptr = buffer;
74b1de
+            while ((tmp_ptr = strstr(buff_ptr, find_ptr))) {
74b1de
+                while (buff_ptr < tmp_ptr)
74b1de
+                    fputc((int)*buff_ptr++, dest);
74b1de
+                fputs(dest_vol->volname, dest);
74b1de
+                buff_ptr += strlen(find_ptr);
74b1de
+            }
74b1de
+            fputs(buff_ptr, dest);
74b1de
+            memset(buffer, 0, BUFSIZ);
74b1de
+        }
74b1de
+    }
74b1de
+out:
74b1de
+    if (src)
74b1de
+        fclose(src);
74b1de
+    if (dest)
74b1de
+        fclose(dest);
74b1de
+    if (find_ptr)
74b1de
+        GF_FREE(find_ptr);
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
 int32_t
74b1de
 glusterd_restore_geo_rep_files(glusterd_volinfo_t *snap_vol)
74b1de
 {
74b1de
@@ -3876,6 +4018,60 @@ out:
74b1de
     return ret;
74b1de
 }
74b1de
 
74b1de
+int
74b1de
+glusterd_restore_nfs_ganesha_file(glusterd_volinfo_t *src_vol,
74b1de
+                                  glusterd_snap_t *snap)
74b1de
+{
74b1de
+    int32_t ret = -1;
74b1de
+    char snap_dir[PATH_MAX] = "";
74b1de
+    char src_path[PATH_MAX] = "";
74b1de
+    char dest_path[PATH_MAX] = "";
74b1de
+    xlator_t *this = NULL;
74b1de
+    glusterd_conf_t *priv = NULL;
74b1de
+    struct stat stbuf = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    this = THIS;
74b1de
+    GF_VALIDATE_OR_GOTO("snapshot", this, out);
74b1de
+    priv = this->private;
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, priv, out);
74b1de
+
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, src_vol, out);
74b1de
+    GF_VALIDATE_OR_GOTO(this->name, snap, out);
74b1de
+
74b1de
+    GLUSTERD_GET_SNAP_DIR(snap_dir, snap, priv);
74b1de
+
74b1de
+    ret = snprintf(src_path, sizeof(src_path), "%s/export.%s.conf", snap_dir,
74b1de
+                   snap->snapname);
74b1de
+    if (ret < 0)
74b1de
+        goto out;
74b1de
+
74b1de
+    ret = sys_lstat(src_path, &stbuf);
74b1de
+    if (ret) {
74b1de
+        if (errno == ENOENT) {
74b1de
+            ret = 0;
74b1de
+            gf_msg_debug(this->name, 0, "%s not found", src_path);
74b1de
+        } else
74b1de
+            gf_msg(this->name, GF_LOG_WARNING, errno, GD_MSG_FILE_OP_FAILED,
74b1de
+                   "Stat on %s failed with %s", src_path, strerror(errno));
74b1de
+        goto out;
74b1de
+    }
74b1de
+
74b1de
+    ret = snprintf(dest_path, sizeof(dest_path), "%s/export.%s.conf",
74b1de
+                   GANESHA_EXPORT_DIRECTORY, src_vol->volname);
74b1de
+    if (ret < 0)
74b1de
+        goto out;
74b1de
+
74b1de
+    ret = glusterd_copy_file(src_path, dest_path);
74b1de
+    if (ret)
74b1de
+        gf_msg(this->name, GF_LOG_ERROR, ENOMEM, GD_MSG_NO_MEMORY,
74b1de
+               "Failed to copy %s in %s", src_path, dest_path);
74b1de
+
74b1de
+out:
74b1de
+    return ret;
74b1de
+}
74b1de
+
74b1de
 /* Snapd functions */
74b1de
 int
74b1de
 glusterd_is_snapd_enabled(glusterd_volinfo_t *volinfo)
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.h b/xlators/mgmt/glusterd/src/glusterd-store.h
74b1de
index e60be6e..41d0001 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-store.h
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-store.h
74b1de
@@ -118,6 +118,8 @@ typedef enum glusterd_store_ver_ac_ {
74b1de
 #define GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SKIPPED "migration-skipped"
74b1de
 #define GLUSTERD_STORE_KEY_VOL_MIGRATION_RUN_TIME "migration-run-time"
74b1de
 
74b1de
+#define GLUSTERD_STORE_KEY_GANESHA_GLOBAL "nfs-ganesha"
74b1de
+
74b1de
 int32_t
74b1de
 glusterd_store_volinfo(glusterd_volinfo_t *volinfo,
74b1de
                        glusterd_volinfo_ver_ac_t ac);
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-ops.c b/xlators/mgmt/glusterd/src/glusterd-volume-ops.c
74b1de
index 86ef470..a0417ca 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-volume-ops.c
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-volume-ops.c
74b1de
@@ -1823,6 +1823,18 @@ glusterd_op_stage_stop_volume(dict_t *dict, char **op_errstr)
74b1de
         goto out;
74b1de
     }
74b1de
 
74b1de
+    ret = glusterd_check_ganesha_export(volinfo);
74b1de
+    if (ret) {
74b1de
+        ret = ganesha_manage_export(dict, "off", op_errstr);
74b1de
+        if (ret) {
74b1de
+            gf_msg(THIS->name, GF_LOG_WARNING, 0,
74b1de
+                   GD_MSG_NFS_GNS_UNEXPRT_VOL_FAIL,
74b1de
+                   "Could not "
74b1de
+                   "unexport volume via NFS-Ganesha");
74b1de
+            ret = 0;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
     if (glusterd_is_defrag_on(volinfo)) {
74b1de
         snprintf(msg, sizeof(msg),
74b1de
                  "rebalance session is "
74b1de
@@ -2674,6 +2686,8 @@ glusterd_op_start_volume(dict_t *dict, char **op_errstr)
74b1de
     xlator_t *this = NULL;
74b1de
     glusterd_conf_t *conf = NULL;
74b1de
     glusterd_svc_t *svc = NULL;
74b1de
+    char *str = NULL;
74b1de
+    gf_boolean_t option = _gf_false;
74b1de
 
74b1de
     this = THIS;
74b1de
     GF_ASSERT(this);
74b1de
@@ -2731,6 +2745,29 @@ glusterd_op_start_volume(dict_t *dict, char **op_errstr)
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    ret = dict_get_str(conf->opts, GLUSTERD_STORE_KEY_GANESHA_GLOBAL, &str);
74b1de
+    if (ret != 0) {
74b1de
+        gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_DICT_GET_FAILED,
74b1de
+               "Global dict not present.");
74b1de
+        ret = 0;
74b1de
+
74b1de
+    } else {
74b1de
+        ret = gf_string2boolean(str, &option);
74b1de
+        /* Check if the feature is enabled and set nfs-disable to true */
74b1de
+        if (option) {
74b1de
+            gf_msg_debug(this->name, 0, "NFS-Ganesha is enabled");
74b1de
+            /* Gluster-nfs should not start when NFS-Ganesha is enabled*/
74b1de
+            ret = dict_set_str(volinfo->dict, NFS_DISABLE_MAP_KEY, "on");
74b1de
+            if (ret) {
74b1de
+                gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED,
74b1de
+                       "Failed to set nfs.disable for"
74b1de
+                       "volume %s",
74b1de
+                       volname);
74b1de
+                goto out;
74b1de
+            }
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
     ret = glusterd_start_volume(volinfo, flags, _gf_true);
74b1de
     if (ret)
74b1de
         goto out;
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-set.c b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
74b1de
index d1244e4..13f423a 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd-volume-set.c
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
74b1de
@@ -2597,6 +2597,13 @@ struct volopt_map_entry glusterd_volopt_map[] = {
74b1de
         .voltype = "features/upcall",
74b1de
         .op_version = GD_OP_VERSION_3_7_0,
74b1de
     },
74b1de
+    {
74b1de
+        .key = "ganesha.enable",
74b1de
+        .voltype = "features/ganesha",
74b1de
+        .value = "off",
74b1de
+        .option = "ganesha.enable",
74b1de
+        .op_version = GD_OP_VERSION_3_7_0,
74b1de
+    },
74b1de
     /* Lease translator options */
74b1de
     {
74b1de
         .key = "features.leases",
74b1de
diff --git a/xlators/mgmt/glusterd/src/glusterd.h b/xlators/mgmt/glusterd/src/glusterd.h
74b1de
index 67867f8..5135181 100644
74b1de
--- a/xlators/mgmt/glusterd/src/glusterd.h
74b1de
+++ b/xlators/mgmt/glusterd/src/glusterd.h
74b1de
@@ -61,6 +61,9 @@
74b1de
 #define GLUSTERD_LOCALTIME_LOGGING_KEY "cluster.localtime-logging"
74b1de
 #define GLUSTERD_DAEMON_LOG_LEVEL_KEY "cluster.daemon-log-level"
74b1de
 
74b1de
+#define GANESHA_HA_CONF CONFDIR "/ganesha-ha.conf"
74b1de
+#define GANESHA_EXPORT_DIRECTORY CONFDIR "/exports"
74b1de
+
74b1de
 #define GLUSTERD_SNAPS_MAX_HARD_LIMIT 256
74b1de
 #define GLUSTERD_SNAPS_DEF_SOFT_LIMIT_PERCENT 90
74b1de
 #define GLUSTERD_SNAPS_MAX_SOFT_LIMIT_PERCENT 100
74b1de
@@ -1356,6 +1359,25 @@ glusterd_op_stop_volume(dict_t *dict);
74b1de
 int
74b1de
 glusterd_op_delete_volume(dict_t *dict);
74b1de
 int
74b1de
+glusterd_handle_ganesha_op(dict_t *dict, char **op_errstr, char *key,
74b1de
+                           char *value);
74b1de
+int
74b1de
+glusterd_check_ganesha_cmd(char *key, char *value, char **errstr, dict_t *dict);
74b1de
+int
74b1de
+glusterd_op_stage_set_ganesha(dict_t *dict, char **op_errstr);
74b1de
+int
74b1de
+glusterd_op_set_ganesha(dict_t *dict, char **errstr);
74b1de
+int
74b1de
+ganesha_manage_export(dict_t *dict, char *value, char **op_errstr);
74b1de
+gf_boolean_t
74b1de
+glusterd_is_ganesha_cluster();
74b1de
+gf_boolean_t
74b1de
+glusterd_check_ganesha_export(glusterd_volinfo_t *volinfo);
74b1de
+int
74b1de
+stop_ganesha(char **op_errstr);
74b1de
+int
74b1de
+tear_down_cluster(gf_boolean_t run_teardown);
74b1de
+int
74b1de
 manage_export_config(char *volname, char *value, char **op_errstr);
74b1de
 
74b1de
 int
74b1de
-- 
74b1de
1.8.3.1
74b1de