summaryrefslogtreecommitdiffstats
path: root/tools/setgfid2path/src
diff options
context:
space:
mode:
authorAravinda VK <avishwan@redhat.com>2017-07-20 17:08:12 +0530
committerShyamsundar Ranganathan <srangana@redhat.com>2017-07-28 14:53:20 +0000
commit1ae254ddcf397b101d291342272e13af25b0b1a1 (patch)
treea979684704b882f5717ce4e2f6a5c3c3c913ba70 /tools/setgfid2path/src
parentb5fa5ae05f73e03023db37e43fb203267b719160 (diff)
tools/setgfid2path: Tool to set GFID to Path xattr in brick backend
Once storage/gfid2path feature is enabled using `gluster volume set <volname> storage.gfid2path enable`, it starts recording the gfid2path xattr on each files. But this feature will not add xattr to the existing files. This tool accepts the file path as argument and sets the necessary xattr required for this feature. Change-Id: I75ad82c86ce482950645e687ff2e33b413fa53da Updates: #139 Signed-off-by: Aravinda VK <avishwan@redhat.com> Reviewed-on: https://review.gluster.org/17839 Smoke: Gluster Build System <jenkins@build.gluster.org> Reviewed-by: Niels de Vos <ndevos@redhat.com> Reviewed-by: Kotresh HR <khiremat@redhat.com> Tested-by: Kotresh HR <khiremat@redhat.com> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
Diffstat (limited to 'tools/setgfid2path/src')
-rw-r--r--tools/setgfid2path/src/Makefile.am14
-rw-r--r--tools/setgfid2path/src/main.c128
2 files changed, 142 insertions, 0 deletions
diff --git a/tools/setgfid2path/src/Makefile.am b/tools/setgfid2path/src/Makefile.am
new file mode 100644
index 00000000000..45520cadafb
--- /dev/null
+++ b/tools/setgfid2path/src/Makefile.am
@@ -0,0 +1,14 @@
+gluster_setgfid2pathdir = $(sbindir)
+
+gluster_setgfid2path_PROGRAMS = gluster-setgfid2path
+
+gluster_setgfid2path_SOURCES = main.c
+
+gluster_setgfid2path_LDADD = $(top_builddir)/libglusterfs/src/libglusterfs.la
+
+gluster_setgfid2path_LDFLAGS = $(GF_LDFLAGS)
+
+AM_CPPFLAGS = $(GF_CPPFLAGS) -I$(top_srcdir)/libglusterfs/src \
+ -I$(top_builddir)/rpc/xdr/src
+
+AM_CFLAGS = -Wall $(GF_CFLAGS)
diff --git a/tools/setgfid2path/src/main.c b/tools/setgfid2path/src/main.c
new file mode 100644
index 00000000000..07d7a48b15d
--- /dev/null
+++ b/tools/setgfid2path/src/main.c
@@ -0,0 +1,128 @@
+/*
+ Copyright (c) 2017 Red Hat, Inc. <http://www.redhat.com>
+ This file is part of GlusterFS.
+
+ This file is licensed to you under your choice of the GNU Lesser
+ General Public License, version 3 or any later version (LGPLv3 or
+ later), or the GNU General Public License, version 2 (GPLv2), in all
+ cases as published by the Free Software Foundation.
+ */
+#include <stdio.h>
+#include <libgen.h>
+
+#include "common-utils.h"
+#include "syscall.h"
+
+#define MAX_GFID2PATH_LINK_SUP 500
+#define GFID_SIZE 16
+#define GFID_XATTR_KEY "trusted.gfid"
+
+
+int main(int argc, char **argv)
+{
+ int ret = 0;
+ struct stat st;
+ char *dname = NULL;
+ char *bname = NULL;
+ ssize_t ret_size = 0;
+ uuid_t pgfid_raw = {0,};
+ char pgfid[36] = "";
+ char xxh64[GF_XXH64_DIGEST_LENGTH*2+1] = {0,};
+ char pgfid_bname[1024] = {0,};
+ char *key = NULL;
+ char *val = NULL;
+ size_t key_size = 0;
+ size_t val_size = 0;
+ const char *file_path = NULL;
+ char *file_path1 = NULL;
+ char *file_path2 = NULL;
+
+ if (argc != 2) {
+ fprintf (stderr, "Usage: setgfid2path <file-path>\n");
+ return -1;
+ }
+
+ ret = sys_lstat (argv[1], &st);
+ if (ret != 0) {
+ fprintf (stderr, "Invalid File Path\n");
+ return -1;
+ }
+
+ if (st.st_nlink >= MAX_GFID2PATH_LINK_SUP) {
+ fprintf (stderr,
+ "Number of Hardlink support exceeded. "
+ "max=%d\n", MAX_GFID2PATH_LINK_SUP);
+ return -1;
+ }
+
+ file_path = argv[1];
+ file_path1 = strdup (file_path);
+ file_path2 = strdup (file_path);
+
+ dname = dirname (file_path1);
+ bname = basename (file_path2);
+
+ /* Get GFID of Parent directory */
+ ret_size = sys_lgetxattr (dname, GFID_XATTR_KEY, pgfid_raw, GFID_SIZE);
+ if (ret_size != GFID_SIZE) {
+ fprintf (stderr,
+ "Failed to get GFID of parent directory. dir=%s\n",
+ dname);
+ ret = -1;
+ goto out;
+ }
+
+ /* Convert to UUID format */
+ if (uuid_utoa_r (pgfid_raw, pgfid) == NULL) {
+ fprintf (stderr,
+ "Failed to format GFID of parent directory. "
+ "dir=%s GFID=%s\n", dname, pgfid_raw);
+ ret = -1;
+ goto out;
+ }
+
+ /* Find xxhash for PGFID/BaseName */
+ snprintf (pgfid_bname, sizeof (pgfid_bname), "%s/%s", pgfid, bname);
+ gf_xxh64_wrapper (
+ (unsigned char *)pgfid_bname,
+ strlen (pgfid_bname),
+ GF_XXHSUM64_DEFAULT_SEED,
+ xxh64
+ );
+
+ key_size = strlen(GFID2PATH_XATTR_KEY_PREFIX) +
+ GF_XXH64_DIGEST_LENGTH*2+1;
+ key = alloca (key_size);
+ snprintf (key, key_size, GFID2PATH_XATTR_KEY_PREFIX"%s", xxh64);
+
+ val_size = UUID_CANONICAL_FORM_LEN + NAME_MAX + 2;
+ val = alloca (val_size);
+ snprintf (val, val_size, "%s/%s", pgfid, bname);
+
+ /* Set the Xattr, ignore if same key xattr already exists */
+ ret = sys_lsetxattr (file_path, key, val, strlen(val), XATTR_CREATE);
+ if (ret == -1) {
+ if (errno == EEXIST) {
+ printf ("Xattr already exists, ignoring..\n");
+ ret = 0;
+ goto out;
+ }
+
+ fprintf (stderr,
+ "Failed to set gfid2path xattr. errno=%d\n error=%s",
+ errno, strerror(errno));
+ ret = -1;
+ goto out;
+ }
+
+ printf ("Success. file=%s key=%s value=%s\n", file_path, key, val);
+
+out:
+ if (file_path1 != NULL)
+ free (file_path1);
+
+ if (file_path2 != NULL)
+ free (file_path2);
+
+ return ret;
+}