17b94a
From 462e3988936761317975fd811dd355b81328b60a Mon Sep 17 00:00:00 2001
17b94a
From: Amar Tumballi <amarts@redhat.com>
17b94a
Date: Thu, 14 Mar 2019 10:04:28 +0530
17b94a
Subject: [PATCH 185/192] gfapi: provide an api for setting statedump path
17b94a
17b94a
Currently for an application using glfsapi to use glusterfs, when a
17b94a
statedump is taken, it uses /var/run/gluster dir to dump info.
17b94a
17b94a
There can be concerns as this directory may be owned by some other
17b94a
user, and hence it may fail taking statedump. Such applications
17b94a
should have an option to use different path.
17b94a
17b94a
This patch provides an API to do so.
17b94a
17b94a
Upstream details:
17b94a
> Updates: bz#1689097
17b94a
> Change-Id: I8918e002bc823d83614c972b6c738baa04681b23
17b94a
> URL: https://review.gluster.org/22364
17b94a
17b94a
BUG: 1720461
17b94a
Change-Id: I6079c8d799f35eaf76e62d259b51573bf561ba5b
17b94a
Signed-off-by: Amar Tumballi <amarts@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/173451
17b94a
Tested-by: RHGS Build Bot <nigelb@redhat.com>
17b94a
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
17b94a
---
17b94a
 api/src/gfapi.aliases |  2 ++
17b94a
 api/src/gfapi.map     |  5 ++++
17b94a
 api/src/glfs.c        | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
17b94a
 api/src/glfs.h        | 28 +++++++++++++++++++++++
17b94a
 4 files changed, 98 insertions(+)
17b94a
17b94a
diff --git a/api/src/gfapi.aliases b/api/src/gfapi.aliases
17b94a
index 09c0fd8..8fdf734 100644
17b94a
--- a/api/src/gfapi.aliases
17b94a
+++ b/api/src/gfapi.aliases
17b94a
@@ -195,3 +195,5 @@ _pub_glfs_zerofill_async _glfs_zerofill_async$GFAPI_6.0
17b94a
 _pub_glfs_copy_file_range _glfs_copy_file_range$GFAPI_6.0
17b94a
 _pub_glfs_fsetattr _glfs_fsetattr$GFAPI_6.0
17b94a
 _pub_glfs_setattr _glfs_setattr$GFAPI_6.0
17b94a
+
17b94a
+_pub_glfs_set_statedump_path _glfs_set_statedump_path@GFAPI_future
17b94a
diff --git a/api/src/gfapi.map b/api/src/gfapi.map
17b94a
index b97a614..cf118e8 100644
17b94a
--- a/api/src/gfapi.map
17b94a
+++ b/api/src/gfapi.map
17b94a
@@ -271,3 +271,8 @@ GFAPI_PRIVATE_6.1 {
17b94a
 	global:
17b94a
 		glfs_setfspid;
17b94a
 } GFAPI_6.0;
17b94a
+
17b94a
+GFAPI_future {
17b94a
+	global:
17b94a
+		glfs_set_statedump_path;
17b94a
+} GFAPI_PRIVATE_6.1;
17b94a
diff --git a/api/src/glfs.c b/api/src/glfs.c
17b94a
index f4a8e08..ba513e6 100644
17b94a
--- a/api/src/glfs.c
17b94a
+++ b/api/src/glfs.c
17b94a
@@ -1212,6 +1212,7 @@ glusterfs_ctx_destroy(glusterfs_ctx_t *ctx)
17b94a
         glusterfs_graph_destroy_residual(trav_graph);
17b94a
     }
17b94a
 
17b94a
+    GF_FREE(ctx->statedump_path);
17b94a
     FREE(ctx);
17b94a
 
17b94a
     return ret;
17b94a
@@ -1738,3 +1739,65 @@ invalid_fs:
17b94a
 }
17b94a
 
17b94a
 GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_upcall_unregister, 3.13.0);
17b94a
+
17b94a
+int
17b94a
+pub_glfs_set_statedump_path(struct glfs *fs, const char *path)
17b94a
+{
17b94a
+    struct stat st;
17b94a
+    int ret;
17b94a
+    DECLARE_OLD_THIS;
17b94a
+    __GLFS_ENTRY_VALIDATE_FS(fs, invalid_fs);
17b94a
+
17b94a
+    if (!path) {
17b94a
+        gf_log("glfs", GF_LOG_ERROR, "path is NULL");
17b94a
+        errno = EINVAL;
17b94a
+        goto err;
17b94a
+    }
17b94a
+
17b94a
+    /* If path is not present OR, if it is directory AND has enough permission
17b94a
+     * to create files, then proceed */
17b94a
+    ret = sys_stat(path, &st);
17b94a
+    if (ret && errno != ENOENT) {
17b94a
+        gf_log("glfs", GF_LOG_ERROR, "%s: not a valid path (%s)", path,
17b94a
+               strerror(errno));
17b94a
+        errno = EINVAL;
17b94a
+        goto err;
17b94a
+    }
17b94a
+
17b94a
+    if (!ret) {
17b94a
+        /* file is present, now check other things */
17b94a
+        if (!S_ISDIR(st.st_mode)) {
17b94a
+            gf_log("glfs", GF_LOG_ERROR, "%s: path is not directory", path);
17b94a
+            errno = EINVAL;
17b94a
+            goto err;
17b94a
+        }
17b94a
+        if (sys_access(path, W_OK | X_OK) < 0) {
17b94a
+            gf_log("glfs", GF_LOG_ERROR,
17b94a
+                   "%s: path doesn't have write permission", path);
17b94a
+            errno = EPERM;
17b94a
+            goto err;
17b94a
+        }
17b94a
+    }
17b94a
+
17b94a
+    /* If set, it needs to be freed, so we don't have leak */
17b94a
+    GF_FREE(fs->ctx->statedump_path);
17b94a
+
17b94a
+    fs->ctx->statedump_path = gf_strdup(path);
17b94a
+    if (!fs->ctx->statedump_path) {
17b94a
+        gf_log("glfs", GF_LOG_ERROR,
17b94a
+               "%s: failed to set statedump path, no memory", path);
17b94a
+        errno = ENOMEM;
17b94a
+        goto err;
17b94a
+    }
17b94a
+
17b94a
+    __GLFS_EXIT_FS;
17b94a
+
17b94a
+    return 0;
17b94a
+err:
17b94a
+    __GLFS_EXIT_FS;
17b94a
+
17b94a
+invalid_fs:
17b94a
+    return -1;
17b94a
+}
17b94a
+
17b94a
+GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_set_statedump_path, future);
17b94a
diff --git a/api/src/glfs.h b/api/src/glfs.h
17b94a
index 6714782..a6c12e1 100644
17b94a
--- a/api/src/glfs.h
17b94a
+++ b/api/src/glfs.h
17b94a
@@ -1453,5 +1453,33 @@ int
17b94a
 glfs_setattr(struct glfs *fs, const char *path, struct glfs_stat *stat,
17b94a
              int follow) __THROW GFAPI_PUBLIC(glfs_setattr, 6.0);
17b94a
 
17b94a
+/*
17b94a
+  SYNOPSIS
17b94a
+
17b94a
+  glfs_set_statedump_path: Function to set statedump path.
17b94a
+
17b94a
+  DESCRIPTION
17b94a
+
17b94a
+  This function is used to set statedump directory
17b94a
+
17b94a
+  PARAMETERS
17b94a
+
17b94a
+  @fs: The 'virtual mount' object to be configured with the volume
17b94a
+       specification file.
17b94a
+
17b94a
+  @path: statedump path. Should be a directory. But the API won't fail if the
17b94a
+  directory doesn't exist yet, as one may create it later.
17b94a
+
17b94a
+  RETURN VALUES
17b94a
+
17b94a
+   0 : Success.
17b94a
+  -1 : Failure. @errno will be set with the type of failure.
17b94a
+
17b94a
+ */
17b94a
+
17b94a
+int
17b94a
+glfs_set_statedump_path(struct glfs *fs, const char *path) __THROW
17b94a
+    GFAPI_PUBLIC(glfs_set_statedump_path, future);
17b94a
+
17b94a
 __END_DECLS
17b94a
 #endif /* !_GLFS_H */
17b94a
-- 
17b94a
1.8.3.1
17b94a