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