summaryrefslogtreecommitdiffstats
path: root/tests/features
diff options
context:
space:
mode:
authorAnoop C S <anoopcs@redhat.com>2015-02-04 10:34:33 +0530
committerPranith Kumar Karampuri <pkarampu@redhat.com>2016-05-02 04:18:44 -0700
commit4517bf8dd6de310950cc5a612955aa3a2fddb57e (patch)
tree2cdf4e8ec5c9362a325d5a48e07778ea44e1fdfe /tests/features
parent78c1c6002f0b11afa997a14f8378c04f257ea1c5 (diff)
features/locks: Implement mandatory locks
Initial change to fix/enable the mandatory locking support in GlusterFS as per the following design: https://review.gluster.org/#/c/12014/ Accordingly 'locks.mandatory-locking' option is available as part of this change which will accept one among the following values: * off * file * forced * optimal See design doc for more details Change-Id: I14c489b3f8af5ebcbfa155a03f0c175e9558ac46 BUG: 762184 Signed-off-by: Anoop C S <anoopcs@redhat.com> Reviewed-on: http://review.gluster.org/9768 Smoke: Gluster Build System <jenkins@build.gluster.com> CentOS-regression: Gluster Build System <jenkins@build.gluster.com> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org> Reviewed-by: Poornima G <pgurusid@redhat.com> Reviewed-by: Raghavendra Talur <rtalur@redhat.com> Reviewed-by: Rajesh Joseph <rjoseph@redhat.com> Reviewed-by: Pranith Kumar Karampuri <pkarampu@redhat.com>
Diffstat (limited to 'tests/features')
-rw-r--r--tests/features/mandatory-lock-forced.c138
-rw-r--r--tests/features/mandatory-lock-forced.t80
2 files changed, 218 insertions, 0 deletions
diff --git a/tests/features/mandatory-lock-forced.c b/tests/features/mandatory-lock-forced.c
new file mode 100644
index 00000000000..f37206845f1
--- /dev/null
+++ b/tests/features/mandatory-lock-forced.c
@@ -0,0 +1,138 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+
+#define LOG_ERR(func, err) do { \
+ fprintf (stderr, "%s : returned error (%s)\n", func, strerror(err)); \
+ exit (err); \
+} while (0)
+
+int fd;
+struct flock lock;
+char *buf = "ten bytes!";
+char *fname = "/mnt/glusterfs/0/mand.lock";
+int open_flags, child, err, status, blocked = 0;
+
+int do_child (char *argv[]) {
+ /* Initialize file open flags */
+ if (strcmp (argv[2], "BLOCK") == 0)
+ open_flags = O_RDWR;
+ else if (strcmp (argv[2], "TRUNC") == 0)
+ open_flags = O_RDWR | O_TRUNC | O_NONBLOCK;
+ else if (strcmp (argv[2], "NONE") == 0)
+ open_flags = O_RDWR | O_NONBLOCK;
+ else
+ LOG_ERR ("Invalid option:", EINVAL);
+
+ /* Open the file */
+ fd = open (fname, open_flags);
+ if (fd == -1)
+ LOG_ERR ("Child open", errno);
+
+ /* Perform the file operation*/
+ if (strcmp (argv[3], "READ") == 0) {
+ buf = NULL;
+ err = read (fd, buf, 10);
+ if (err == -1)
+ LOG_ERR ("Child read", errno);
+ } else if (strcmp (argv[3], "WRITE") == 0) {
+ err = write (fd, buf, 10);
+ if (err == -1)
+ LOG_ERR ("Child write", errno);
+ } else if (strcmp (argv[3], "FTRUNCATE") == 0) {
+ err = ftruncate (fd, 5);
+ if (err)
+ LOG_ERR ("Child ftruncate", errno);
+ } else
+ LOG_ERR ("Invalid operation:", EINVAL);
+
+ /* Close child fd */
+ err = close (fd);
+ if (err)
+ LOG_ERR ("Child close", errno);
+
+ /* Exit success */
+ exit (0);
+}
+
+int main (int argc, char *argv[]) {
+ if (argc < 4) {
+ fprintf (stderr, "Wrong usage: Use as ./mandatory-lock "
+ "<RD_LCK/WR_LCK> <BLOCK/TRUNC/NONE> "
+ "<READ/WRITE/FTRUNCATE\n");
+ exit(EINVAL);
+ }
+ /* Create an empty lock file */
+ fd = open (fname, O_CREAT | O_RDWR, 0755);
+ if (fd == -1)
+ LOG_ERR ("Parent create", errno);
+
+ /* Determine the type of lock */
+ if (strcmp (argv[1], "RD_LCK") == 0)
+ lock.l_type = F_RDLCK;
+ else if (strcmp (argv[1], "WR_LCK") == 0)
+ lock.l_type = F_WRLCK;
+ else
+ LOG_ERR ("Parent lock type", EINVAL);
+
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0L;
+ lock.l_len = 0L;
+
+ /* Let parent acquire the initial lock */
+ err = fcntl (fd, F_SETLK, &lock);
+ if (err)
+ LOG_ERR ("Parent lock", errno);
+
+ /* Now fork a child */
+ child = fork ();
+ if (child == 0)
+ /* Perform the child operations */
+ do_child (argv);
+ else {
+ /* If blocking mode, then sleep for 2 seconds
+ * and wait for the child */
+ if (strcmp (argv[2], "NONE") != 0) {
+ sleep (2);
+ if (waitpid (child, &status, WNOHANG) == 0)
+ blocked = 1;
+ /* Release the parent lock so that the
+ * child can terminate */
+ lock.l_type = F_UNLCK;
+ err = fcntl (fd, F_SETLK, &lock);
+ if (err)
+ LOG_ERR ("Parent unlock", errno);
+ }
+
+ /* Wait for child to finish */
+ waitpid (child, &status, 0);
+
+ /* Close the parent fd */
+ err = close (fd);
+ if (err)
+ LOG_ERR ("Parent close", errno);
+
+ /* Remove the lock file*/
+ err = unlink (fname);
+ if (err)
+ LOG_ERR ("Parent unlink", errno);
+
+ /* If not blocked, exit with child exit status*/
+ errno = WEXITSTATUS(status);
+
+ /* If blocked, exit with corresponding
+ * error code */
+ if (blocked)
+ errno = EWOULDBLOCK;
+
+ if (errno != 0)
+ printf ("%s\n", strerror(errno));
+
+ exit (errno);
+
+ }
+}
diff --git a/tests/features/mandatory-lock-forced.t b/tests/features/mandatory-lock-forced.t
new file mode 100644
index 00000000000..563669c6774
--- /dev/null
+++ b/tests/features/mandatory-lock-forced.t
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+. $(dirname $0)/../include.rc
+. $(dirname $0)/../volume.rc
+
+cleanup;
+# Start glusterd [1]
+TEST glusterd
+
+# Create and verify the volume information [2-4]
+TEST $CLI volume create $V0 $H0:$B0/${V0}{1,2};
+EXPECT "$V0" volinfo_field $V0 'Volume Name';
+EXPECT 'Created' volinfo_field $V0 'Status';
+
+# Turn off the performance translators [5-9]
+TEST $CLI volume set $V0 performance.open-behind off
+TEST $CLI volume set $V0 performance.write-behind off
+TEST $CLI volume set $V0 performance.quick-read off
+TEST $CLI volume set $V0 performance.io-cache off
+TEST $CLI volume set $V0 performance.read-ahead off
+
+# Start and mount the volume [10-11]
+TEST $CLI volume start $V0;
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0
+
+build_tester $(dirname $0)/mandatory-lock-forced.c -o $(dirname $0)/mandatory-lock
+
+# Various read/write tests without enabling mandatory-locking [12-18]
+$(dirname $0)/mandatory-lock RD_LCK NONE READ
+TEST [ $? -eq 0 ]
+
+$(dirname $0)/mandatory-lock RD_LCK NONE WRITE
+TEST [ $? -eq 0 ]
+
+$(dirname $0)/mandatory-lock WR_LCK NONE READ
+TEST [ $? -eq 0 ]
+
+$(dirname $0)/mandatory-lock WR_LCK NONE WRITE
+TEST [ $? -eq 0 ]
+
+# Specifies O_TRUNC during open
+$(dirname $0)/mandatory-lock RD_LCK TRUNC READ
+TEST [ $? -eq 0 ]
+
+$(dirname $0)/mandatory-lock RD_LCK NONE FTRUNCATE
+TEST [ $? -eq 0 ]
+
+$(dirname $0)/mandatory-lock RD_LCK BLOCK WRITE
+TEST [ $? -eq 0 ]
+
+# Enable mandatory-locking [19]
+TEST $CLI volume set $V0 mandatory-locking forced
+
+# Restart the volume to take the change into effect [20-23]
+TEST umount $M0
+TEST $CLI volume stop $V0
+TEST $CLI volume start $V0
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0
+
+# Repeat the above tests with mandatory-locking [24-30]
+$(dirname $0)/mandatory-lock RD_LCK NONE READ
+TEST [ $? -eq 0 ]
+
+EXPECT "Resource temporarily unavailable" $(dirname $0)/mandatory-lock RD_LCK NONE WRITE
+
+EXPECT "Resource temporarily unavailable" $(dirname $0)/mandatory-lock WR_LCK NONE READ
+
+EXPECT "Resource temporarily unavailable" $(dirname $0)/mandatory-lock WR_LCK NONE WRITE
+
+EXPECT "Resource temporarily unavailable" $(dirname $0)/mandatory-lock RD_LCK TRUNC READ
+
+EXPECT "Resource temporarily unavailable" $(dirname $0)/mandatory-lock RD_LCK NONE FTRUNCATE
+
+EXPECT "Resource temporarily unavailable" $(dirname $0)/mandatory-lock RD_LCK BLOCK WRITE
+
+rm -rf $(dirname $0)/mandatory-lock
+
+cleanup
+
+#G_TESTDEF_TEST_STATUS_NETBSD7=KNOWN_ISSUE,BUG=1326464