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