7c2869
From dacb230663c9f6deeebc62139e675a594ec778f0 Mon Sep 17 00:00:00 2001
7c2869
From: Soumya Koduri <skoduri@redhat.com>
7c2869
Date: Tue, 17 Oct 2017 16:12:06 +0530
7c2869
Subject: [PATCH 642/642] gfapi: set lkowner in glfd
7c2869
7c2869
We need a provision to be able to set lkowner (which is
7c2869
used to distinguish locks maintained by server) in gfapi.
7c2869
Since the same lk_owner need to be used to be able to
7c2869
flush the lock while closing the fd, store the lkowner
7c2869
in the glfd structure itself.
7c2869
7c2869
A new API has been added to be able to set lkowner in glfd.
7c2869
7c2869
Upstream reference :
7c2869
1.) This is backport of below mainline fix -
7c2869
 https://review.gluster.org/#/c/18429
7c2869
 https://review.gluster.org/#/c/18522/
7c2869
7c2869
2.) 3.12 branch
7c2869
https://review.gluster.org/#/c/18524/
7c2869
7c2869
>Change-Id: I67591d6b9a89c20b9617d52616513ff9e6c06b47
7c2869
>BUG: 1501956
7c2869
>Signed-off-by: Soumya Koduri <skoduri@redhat.com>
7c2869
7c2869
Change-Id: I67591d6b9a89c20b9617d52616513ff9e6c06b47
7c2869
BUG: 1526378
7c2869
Signed-off-by: Jiffin Tony Thottan <jthottan@redhat.com>
7c2869
Reviewed-on: https://code.engineering.redhat.com/gerrit/126039
7c2869
Tested-by: RHGS Build Bot <nigelb@redhat.com>
7c2869
Reviewed-by: Poornima Gurusiddaiah <pgurusid@redhat.com>
7c2869
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
7c2869
---
7c2869
 api/src/gfapi.aliases            |   1 +
7c2869
 api/src/gfapi.map                |   7 +-
7c2869
 api/src/glfs-fops.c              |  51 ++++++++++
7c2869
 api/src/glfs-internal.h          |   1 +
7c2869
 api/src/glfs.h                   |  28 ++++++
7c2869
 tests/basic/gfapi/glfd-lkowner.c | 212 +++++++++++++++++++++++++++++++++++++++
7c2869
 tests/basic/gfapi/glfd-lkowner.t |  27 +++++
7c2869
 7 files changed, 326 insertions(+), 1 deletion(-)
7c2869
 create mode 100644 tests/basic/gfapi/glfd-lkowner.c
7c2869
 create mode 100755 tests/basic/gfapi/glfd-lkowner.t
7c2869
7c2869
diff --git a/api/src/gfapi.aliases b/api/src/gfapi.aliases
7c2869
index c69d650..bb60aa3 100644
7c2869
--- a/api/src/gfapi.aliases
7c2869
+++ b/api/src/gfapi.aliases
7c2869
@@ -158,5 +158,6 @@ _pub_glfs_upcall_inode_get_oldpstat _glfs_upcall_inode_get_oldpstat$GFAPI_3.7.16
7c2869
 _pub_glfs_realpath _glfs_realpath$GFAPI_3.7.17
7c2869
 
7c2869
 _pub_glfs_sysrq _glfs_sysrq$GFAPI_3.10.0
7c2869
+_pub_glfs_fd_set_lkowner _glfs_fd_set_lkowner$GFAPI_3.10.7
7c2869
 
7c2869
 _pub_glfs_ipc _glfs_ipc$GFAPI_4.0.0
7c2869
diff --git a/api/src/gfapi.map b/api/src/gfapi.map
7c2869
index 3bf2ffe..a8c686f 100644
7c2869
--- a/api/src/gfapi.map
7c2869
+++ b/api/src/gfapi.map
7c2869
@@ -198,7 +198,12 @@ GFAPI_3.10.0 {
7c2869
 		glfs_sysrq;
7c2869
 } GFAPI_3.7.17;
7c2869
 
7c2869
+GFAPI_3.10.7 {
7c2869
+	global:
7c2869
+		glfs_fd_set_lkowner;
7c2869
+} GFAPI_3.10.0;
7c2869
+
7c2869
 GFAPI_4.0.0 {
7c2869
         global:
7c2869
                 glfs_ipc;
7c2869
-} GFAPI_3.10.0;
7c2869
+} GFAPI_3.10.7;
7c2869
diff --git a/api/src/glfs-fops.c b/api/src/glfs-fops.c
7c2869
index 3bba7d6..9a5e7b5 100644
7c2869
--- a/api/src/glfs-fops.c
7c2869
+++ b/api/src/glfs-fops.c
7c2869
@@ -267,6 +267,12 @@ pub_glfs_close (struct glfs_fd *glfd)
7c2869
 		goto out;
7c2869
 	}
7c2869
 
7c2869
+        if (glfd->lk_owner.len != 0) {
7c2869
+                ret = syncopctx_setfslkowner (&glfd->lk_owner);
7c2869
+                if (ret)
7c2869
+                        goto out;
7c2869
+        }
7c2869
+
7c2869
 	ret = syncop_flush (subvol, fd, NULL, NULL);
7c2869
         DECODE_SYNCOP_ERR (ret);
7c2869
 out:
7c2869
@@ -4242,6 +4248,14 @@ pub_glfs_posix_lock (struct glfs_fd *glfd, int cmd, struct flock *flock)
7c2869
 
7c2869
 	gf_flock_from_flock (&gf_flock, flock);
7c2869
 	gf_flock_from_flock (&saved_flock, flock);
7c2869
+
7c2869
+        if (glfd->lk_owner.len != 0) {
7c2869
+                ret = syncopctx_setfslkowner (&glfd->lk_owner);
7c2869
+
7c2869
+                if (ret)
7c2869
+                        goto out;
7c2869
+        }
7c2869
+
7c2869
 	ret = syncop_lk (subvol, fd, cmd, &gf_flock, NULL, NULL);
7c2869
         DECODE_SYNCOP_ERR (ret);
7c2869
 	gf_flock_to_flock (&gf_flock, flock);
7c2869
@@ -4264,6 +4278,43 @@ invalid_fs:
7c2869
 
7c2869
 GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_posix_lock, 3.4.0);
7c2869
 
7c2869
+int
7c2869
+pub_glfs_fd_set_lkowner (glfs_fd_t *glfd, void *data, int len)
7c2869
+{
7c2869
+        int ret = -1;
7c2869
+
7c2869
+        DECLARE_OLD_THIS;
7c2869
+        __GLFS_ENTRY_VALIDATE_FD (glfd, invalid_fs);
7c2869
+
7c2869
+        if (!GF_REF_GET (glfd)) {
7c2869
+                goto invalid_fs;
7c2869
+        }
7c2869
+
7c2869
+        GF_VALIDATE_OR_GOTO (THIS->name, data, out);
7c2869
+
7c2869
+        if ((len <= 0) || (len > GFAPI_MAX_LOCK_OWNER_LEN)) {
7c2869
+                errno = EINVAL;
7c2869
+                gf_msg (THIS->name, GF_LOG_ERROR, errno,
7c2869
+                        LG_MSG_INVALID_ARG,
7c2869
+                        "Invalid lk_owner len (%d)", len);
7c2869
+                goto out;
7c2869
+        }
7c2869
+
7c2869
+        glfd->lk_owner.len = len;
7c2869
+
7c2869
+        memcpy (glfd->lk_owner.data, data, len);
7c2869
+
7c2869
+        ret = 0;
7c2869
+out:
7c2869
+        if (glfd)
7c2869
+                GF_REF_PUT (glfd);
7c2869
+
7c2869
+        __GLFS_EXIT_FS;
7c2869
+
7c2869
+invalid_fs:
7c2869
+        return ret;
7c2869
+}
7c2869
+GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_fd_set_lkowner, 3.10.7);
7c2869
 
7c2869
 struct glfs_fd *
7c2869
 pub_glfs_dup (struct glfs_fd *glfd)
7c2869
diff --git a/api/src/glfs-internal.h b/api/src/glfs-internal.h
7c2869
index 3f4ed71..f17a854 100644
7c2869
--- a/api/src/glfs-internal.h
7c2869
+++ b/api/src/glfs-internal.h
7c2869
@@ -215,6 +215,7 @@ struct glfs_fd {
7c2869
 	struct list_head   entries;
7c2869
 	gf_dirent_t       *next;
7c2869
 	struct dirent     *readdirbuf;
7c2869
+        gf_lkowner_t       lk_owner;
7c2869
 };
7c2869
 
7c2869
 /* glfs object handle introduced for the alternate gfapi implementation based
7c2869
diff --git a/api/src/glfs.h b/api/src/glfs.h
7c2869
index 9780b52..770d138 100644
7c2869
--- a/api/src/glfs.h
7c2869
+++ b/api/src/glfs.h
7c2869
@@ -798,6 +798,34 @@ int glfs_sysrq (glfs_t *fs, char sysrq) __THROW
7c2869
 int glfs_ipc (glfs_fd_t *fd, int cmd) __THROW
7c2869
         GFAPI_PUBLIC(glfs_ipc, 3.7.0);
7c2869
 
7c2869
+#define GFAPI_MAX_LOCK_OWNER_LEN 255
7c2869
+
7c2869
+/*
7c2869
+ *
7c2869
+ * DESCRIPTION
7c2869
+ *
7c2869
+ * This API allows application to set lk_owner on a fd.
7c2869
+ * A glfd can be associated with only single lk_owner. In case if there
7c2869
+ * is need to set another lk_owner, applications can make use of
7c2869
+ * 'glfs_dup' to get duplicate glfd and set new lk_owner on that second
7c2869
+ * glfd.
7c2869
+ *
7c2869
+ * Also its not recommended to override or clear lk_owner value as the
7c2869
+ * same shall be used to flush any outstanding locks while closing the fd.
7c2869
+ *
7c2869
+ * PARAMETERS
7c2869
+ *
7c2869
+ * INPUT:
7c2869
+ * @glfd: GFAPI file descriptor
7c2869
+ * @len: Size of lk_owner buffer. Max value can be GFAPI_MAX_LOCK_OWNER_LEN
7c2869
+ * @data: lk_owner data buffer.
7c2869
+ *
7c2869
+ * OUTPUT:
7c2869
+ * 0: SUCCESS
7c2869
+ * -1: FAILURE
7c2869
+ */
7c2869
+int glfs_fd_set_lkowner (glfs_fd_t *glfd, void *data, int len);
7c2869
+        GFAPI_PUBLIC(glfs_fd_set_lkowner, 3.10.7);
7c2869
 __END_DECLS
7c2869
 
7c2869
 #endif /* !_GLFS_H */
7c2869
diff --git a/tests/basic/gfapi/glfd-lkowner.c b/tests/basic/gfapi/glfd-lkowner.c
7c2869
new file mode 100644
7c2869
index 0000000..031a076
7c2869
--- /dev/null
7c2869
+++ b/tests/basic/gfapi/glfd-lkowner.c
7c2869
@@ -0,0 +1,212 @@
7c2869
+#include <fcntl.h>
7c2869
+#include <unistd.h>
7c2869
+#include <time.h>
7c2869
+#include <limits.h>
7c2869
+#include <string.h>
7c2869
+#include <stdio.h>
7c2869
+#include <stdlib.h>
7c2869
+#include <errno.h>
7c2869
+#include <glusterfs/api/glfs.h>
7c2869
+#include <glusterfs/api/glfs-handles.h>
7c2869
+#include <sys/wait.h>
7c2869
+#include <stdbool.h>
7c2869
+
7c2869
+int gfapi = 1;
7c2869
+
7c2869
+#define LOG_ERR(func, ret) do { \
7c2869
+        if (ret != 0) {            \
7c2869
+                fprintf (stderr, "%s : returned error %d (%s)\n", \
7c2869
+                         func, ret, strerror (errno)); \
7c2869
+                goto out; \
7c2869
+        } else { \
7c2869
+               fprintf (stderr, "%s : returned %d\n", func, ret); \
7c2869
+        } \
7c2869
+        } while (0)
7c2869
+
7c2869
+char lownera[8] = "lownera", lownerb[8] = "lownerb";
7c2869
+char lownerc[8] = "lownerc";
7c2869
+
7c2869
+int lock_test (glfs_fd_t *glfd1, glfs_fd_t *glfd2, bool should_fail,
7c2869
+               int l1_start, int l1_len, char *l1_owner, int lo1_len,
7c2869
+               int l2_start, int l2_len, char *l2_owner, int lo2_len)
7c2869
+{
7c2869
+        int ret = -1, f_ret = -1;
7c2869
+        struct flock lock1 = {0, }, lock2 = {0, };
7c2869
+
7c2869
+lock1:
7c2869
+        if (!glfd1)
7c2869
+                goto lock2;
7c2869
+
7c2869
+        /* lock on glfd1 */
7c2869
+        lock1.l_type = F_WRLCK;
7c2869
+        lock1.l_whence = SEEK_SET;
7c2869
+        lock1.l_start = l1_start;
7c2869
+        lock1.l_len = l1_len;
7c2869
+
7c2869
+        ret = glfs_fd_set_lkowner (glfd1, l1_owner, lo1_len);
7c2869
+        LOG_ERR ("glfs_fd_set_lkowner on glfd1", ret);
7c2869
+
7c2869
+        ret = glfs_posix_lock (glfd1, F_SETLK, &lock1);
7c2869
+        LOG_ERR ("glfs_posix_lock on glfd1", ret);
7c2869
+
7c2869
+lock2:
7c2869
+        if (!glfd2)
7c2869
+                goto out;
7c2869
+
7c2869
+        /* lock on glfd2 */
7c2869
+        lock2.l_type = F_WRLCK;
7c2869
+        lock2.l_whence = SEEK_SET;
7c2869
+        lock2.l_start = l2_start;
7c2869
+        lock2.l_len = l2_len;
7c2869
+
7c2869
+        ret = glfs_fd_set_lkowner (glfd2, l2_owner, lo2_len);
7c2869
+        LOG_ERR ("glfs_fd_set_lkowner on glfd2", ret);
7c2869
+
7c2869
+        ret = glfs_posix_lock (glfd2, F_SETLK, &lock2);
7c2869
+
7c2869
+        if (should_fail && ret) {
7c2869
+                f_ret = 0;
7c2869
+        } else if (!ret && !should_fail) {
7c2869
+                f_ret = 0;
7c2869
+        } else {
7c2869
+                f_ret = -1;
7c2869
+        }
7c2869
+out:
7c2869
+        fprintf (stderr, "Lock test on glfd1 (start(%d), len(%d),"
7c2869
+                         " lk_owner(%s)) and glfd2 (start(%d), len(%d), "
7c2869
+                         "lk_owner(%s)) - expected(%s) - result(%s)\n",
7c2869
+                         l1_start, l1_len, l1_owner, l2_start, l2_len, l2_owner,
7c2869
+                         (should_fail ? "FAIL" : "SUCCESS"),
7c2869
+                         (ret ? "FAIL" : "SUCCESS"));
7c2869
+        return f_ret;
7c2869
+}
7c2869
+
7c2869
+int
7c2869
+main (int argc, char *argv[])
7c2869
+{
7c2869
+        glfs_t    *fs = NULL;
7c2869
+        int        ret = 0, i, status = 0;
7c2869
+        glfs_fd_t *fd1 = NULL;
7c2869
+        glfs_fd_t *fd2 = NULL;
7c2869
+        glfs_fd_t *fd3 = NULL;
7c2869
+        char      *filename = "file_tmp";
7c2869
+        char      *volname = NULL;
7c2869
+        char      *logfile = NULL;
7c2869
+        char      *hostname = NULL;
7c2869
+
7c2869
+        if (argc != 4) {
7c2869
+                fprintf (stderr, "Invalid argument\n");
7c2869
+                exit(1);
7c2869
+        }
7c2869
+
7c2869
+        hostname = argv[1];
7c2869
+        volname = argv[2];
7c2869
+        logfile = argv[3];
7c2869
+
7c2869
+        fs = glfs_new (volname);
7c2869
+        if (!fs) {
7c2869
+                fprintf (stderr, "glfs_new: returned NULL\n");
7c2869
+                return -1;
7c2869
+        }
7c2869
+
7c2869
+        ret = glfs_set_volfile_server (fs, "tcp", hostname, 24007);
7c2869
+        LOG_ERR("glfs_set_volfile_server", ret);
7c2869
+
7c2869
+        ret = glfs_set_logging (fs, logfile, 7);
7c2869
+        LOG_ERR("glfs_set_logging", ret);
7c2869
+
7c2869
+        ret = glfs_init (fs);
7c2869
+        LOG_ERR("glfs_init", ret);
7c2869
+
7c2869
+        fd1 = glfs_creat(fs, filename, O_RDWR|O_SYNC, 0644);
7c2869
+        if (fd1 <= 0) {
7c2869
+                ret = -1;
7c2869
+                LOG_ERR ("glfs_creat", ret);
7c2869
+        }
7c2869
+        fprintf (stderr, "glfs-create fd1 - %d\n", fd1);
7c2869
+
7c2869
+        fd2 = glfs_dup(fd1);
7c2869
+        fprintf (stderr, "glfs-dup fd2 - %d\n", fd2);
7c2869
+
7c2869
+        fd3 = glfs_open(fs, filename, O_RDWR|O_SYNC);
7c2869
+        if (fd2 <= 0) {
7c2869
+                ret = -1;
7c2869
+                LOG_ERR ("glfs_open", ret);
7c2869
+        }
7c2869
+        fprintf (stderr, "glfs-open fd3 - %d\n", fd3);
7c2869
+
7c2869
+        /* TEST 1: Conflicting ranges, same lk_owner
7c2869
+         * lock1 (0, 10, lownera)
7c2869
+         * lock2 (5, 10, lownera)
7c2869
+         * Expected: should not fail but get merged
7c2869
+         */
7c2869
+        ret = lock_test (fd1, fd2, false, 0, 10, lownera, 8,
7c2869
+                         5, 10, lownera, 8);
7c2869
+        LOG_ERR ("==== glfs_lock_test_1", ret);
7c2869
+
7c2869
+        /* TEST 2: Conflicting ranges, different lk_owner
7c2869
+         * lock1 (0, 10, lownera) - already taken
7c2869
+         * lock2 (5, 10, lownerb)
7c2869
+         * Expected: should fail and not get merged
7c2869
+         */
7c2869
+        ret = lock_test (NULL, fd2, true, 0, 10, lownera, 8,
7c2869
+                         5, 10, lownerb, 8);
7c2869
+        LOG_ERR ("==== glfs_lock_test_2", ret);
7c2869
+
7c2869
+        /* TEST 3: Different ranges, same lk_owner
7c2869
+         * lock1 (0, 10, lownera) - already taken
7c2869
+         * lock2 (30, 10, lownera)
7c2869
+         * Expected: should not fail
7c2869
+         */
7c2869
+        ret = lock_test (NULL, fd2, false, 0, 10, lownera, 8,
7c2869
+                         30, 10, lownera, 8);
7c2869
+        LOG_ERR ("==== glfs_lock_test_3", ret);
7c2869
+
7c2869
+        /* TEST 4: Conflicting ranges, different lk_owner
7c2869
+         * lock1 (0, 10, lownera) - already taken
7c2869
+         * lock2 (50, 10, lownerb)
7c2869
+         * Expected: should not fail
7c2869
+         */
7c2869
+        ret = lock_test (NULL, fd2, false, 0, 10, lownera, 8,
7c2869
+                         50, 10, lownerb, 8);
7c2869
+        LOG_ERR ("==== glfs_lock_test_4", ret);
7c2869
+
7c2869
+        /* TEST 5: Close fd1 & retry TEST2
7c2869
+         * lock1 (not applicable)
7c2869
+         * lock2 (5, 10, lownerb)
7c2869
+         * Expected: should succeed now
7c2869
+         */
7c2869
+        ret = glfs_close(fd1);
7c2869
+        LOG_ERR ("glfs_close", ret);
7c2869
+
7c2869
+        ret = lock_test (NULL, fd2, false, 0, 10, lownera, 8,
7c2869
+                         5, 10, lownerb, 8);
7c2869
+        LOG_ERR ("==== glfs_lock_test_5", ret);
7c2869
+
7c2869
+        /* TEST 6: Check closing fd1 doesn't flush fd2 locks
7c2869
+         * retry TEST 4 but with fd2 and fd3.
7c2869
+         * lock1 (50, 10, lownerb) - already taken
7c2869
+         * lock2 (55, 10, lownerc)
7c2869
+         * Expected: should fail
7c2869
+         */
7c2869
+        ret = lock_test (NULL, fd3, true, 50, 10, lownerb, 8,
7c2869
+                         55, 10, lownerc, 8);
7c2869
+        LOG_ERR ("==== glfs_lock_test_6", ret);
7c2869
+
7c2869
+err:
7c2869
+        ret = glfs_close(fd2);
7c2869
+        LOG_ERR ("glfs_close", ret);
7c2869
+
7c2869
+        ret = glfs_close(fd3);
7c2869
+        LOG_ERR ("glfs_close", ret);
7c2869
+
7c2869
+out:
7c2869
+        if (fs) {
7c2869
+                ret = glfs_fini(fs);
7c2869
+                fprintf (stderr, "glfs_fini(fs) returned %d\n", ret);
7c2869
+        }
7c2869
+
7c2869
+        if (ret)
7c2869
+                exit(1);
7c2869
+        exit(0);
7c2869
+}
7c2869
diff --git a/tests/basic/gfapi/glfd-lkowner.t b/tests/basic/gfapi/glfd-lkowner.t
7c2869
new file mode 100755
7c2869
index 0000000..ad7b026
7c2869
--- /dev/null
7c2869
+++ b/tests/basic/gfapi/glfd-lkowner.t
7c2869
@@ -0,0 +1,27 @@
7c2869
+#!/bin/bash
7c2869
+
7c2869
+. $(dirname $0)/../../include.rc
7c2869
+. $(dirname $0)/../../volume.rc
7c2869
+
7c2869
+cleanup;
7c2869
+
7c2869
+TEST glusterd
7c2869
+
7c2869
+TEST $CLI volume create $V0 $H0:$B0/brick1;
7c2869
+EXPECT 'Created' volinfo_field $V0 'Status';
7c2869
+
7c2869
+TEST $CLI volume start $V0;
7c2869
+EXPECT 'Started' volinfo_field $V0 'Status';
7c2869
+
7c2869
+logdir=`gluster --print-logdir`
7c2869
+
7c2869
+TEST build_tester $(dirname $0)/glfd-lkowner.c -lgfapi
7c2869
+
7c2869
+TEST ./$(dirname $0)/glfd-lkowner $H0 $V0  $logdir/glfd-lkowner.log
7c2869
+
7c2869
+cleanup_tester $(dirname $0)/glfd-lkowner
7c2869
+
7c2869
+TEST $CLI volume stop $V0
7c2869
+TEST $CLI volume delete $V0
7c2869
+
7c2869
+cleanup;
7c2869
-- 
7c2869
2.9.3
7c2869