summaryrefslogtreecommitdiffstats
path: root/tests/basic/gfapi/glfd-lkowner.c
blob: 4f2b0fc065006f26689d45603ef3c2c4764d3041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>
#include <sys/wait.h>
#include <stdbool.h>

int gfapi = 1;

#define LOG_ERR(func, ret) do { \
        if (ret != 0) {            \
                fprintf (stderr, "%s : returned error %d (%s)\n", \
                         func, ret, strerror (errno)); \
                goto out; \
        } else { \
               fprintf (stderr, "%s : returned %d\n", func, ret); \
        } \
        } while (0)

char lownera[8] = "lownera", lownerb[8] = "lownerb";
char lownerc[8] = "lownerc";

int lock_test (glfs_fd_t *glfd1, glfs_fd_t *glfd2, bool should_fail,
               int l1_start, int l1_len, char *l1_owner, int lo1_len,
               int l2_start, int l2_len, char *l2_owner, int lo2_len)
{
        int ret = -1, f_ret = -1;
        struct flock lock1 = {0, }, lock2 = {0, };

lock1:
        if (!glfd1)
                goto lock2;

        /* lock on glfd1 */
        lock1.l_type = F_WRLCK;
        lock1.l_whence = SEEK_SET;
        lock1.l_start = l1_start;
        lock1.l_len = l1_len;

        ret = glfd_set_lk_owner (glfd1, l1_owner, lo1_len);
        LOG_ERR ("glfd_set_lk_owner on glfd1", ret);

        ret = glfs_posix_lock (glfd1, F_SETLK, &lock1);
        LOG_ERR ("glfs_posix_lock on glfd1", ret);

lock2:
        if (!glfd2)
                goto out;

        /* lock on glfd2 */
        lock2.l_type = F_WRLCK;
        lock2.l_whence = SEEK_SET;
        lock2.l_start = l2_start;
        lock2.l_len = l2_len;

        ret = glfd_set_lk_owner (glfd2, l2_owner, lo2_len);
        LOG_ERR ("glfd_set_lk_owner on glfd2", ret);

        ret = glfs_posix_lock (glfd2, F_SETLK, &lock2);

        if (should_fail && ret) {
                f_ret = 0;
        } else if (!ret && !should_fail) {
                f_ret = 0;
        } else {
                f_ret = -1;
        }
out:
        fprintf (stderr, "Lock test on glfd1 (start(%d), len(%d),"
                         " lk_owner(%s)) and glfd2 (start(%d), len(%d), "
                         "lk_owner(%s)) - expected(%s) - result(%s)\n",
                         l1_start, l1_len, l1_owner, l2_start, l2_len, l2_owner,
                         (should_fail ? "FAIL" : "SUCCESS"),
                         (ret ? "FAIL" : "SUCCESS"));
        return f_ret;
}

int
main (int argc, char *argv[])
{
        glfs_t    *fs = NULL;
        int        ret = 0, i, status = 0;
        glfs_fd_t *fd1 = NULL;
        glfs_fd_t *fd2 = NULL;
        glfs_fd_t *fd3 = NULL;
        char      *filename = "file_tmp";
        char      *volname = NULL;
        char      *logfile = NULL;
        char      *hostname = NULL;

        if (argc != 4) {
                fprintf (stderr, "Invalid argument\n");
                exit(1);
        }

        hostname = argv[1];
        volname = argv[2];
        logfile = argv[3];

        fs = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                return -1;
        }

        ret = glfs_set_volfile_server (fs, "tcp", hostname, 24007);
        LOG_ERR("glfs_set_volfile_server", ret);

        ret = glfs_set_logging (fs, logfile, 7);
        LOG_ERR("glfs_set_logging", ret);

        ret = glfs_init (fs);
        LOG_ERR("glfs_init", ret);

        fd1 = glfs_creat(fs, filename, O_RDWR|O_SYNC, 0644);
        if (fd1 <= 0) {
                ret = -1;
                LOG_ERR ("glfs_creat", ret);
        }
        fprintf (stderr, "glfs-create fd1 - %d\n", fd1);

        fd2 = glfs_dup(fd1);
        fprintf (stderr, "glfs-dup fd2 - %d\n", fd2);

        fd3 = glfs_open(fs, filename, O_RDWR|O_SYNC);
        if (fd2 <= 0) {
                ret = -1;
                LOG_ERR ("glfs_open", ret);
        }
        fprintf (stderr, "glfs-open fd3 - %d\n", fd3);

        /* TEST 1: Conflicting ranges, same lk_owner
         * lock1 (0, 10, lownera)
         * lock2 (5, 10, lownera)
         * Expected: should not fail but get merged
         */
        ret = lock_test (fd1, fd2, false, 0, 10, lownera, 8,
                         5, 10, lownera, 8);
        LOG_ERR ("==== glfs_lock_test_1", ret);

        /* TEST 2: Conflicting ranges, different lk_owner
         * lock1 (0, 10, lownera) - already taken
         * lock2 (5, 10, lownerb)
         * Expected: should fail and not get merged
         */
        ret = lock_test (NULL, fd2, true, 0, 10, lownera, 8,
                         5, 10, lownerb, 8);
        LOG_ERR ("==== glfs_lock_test_2", ret);

        /* TEST 3: Different ranges, same lk_owner
         * lock1 (0, 10, lownera) - already taken
         * lock2 (30, 10, lownera)
         * Expected: should not fail
         */
        ret = lock_test (NULL, fd2, false, 0, 10, lownera, 8,
                         30, 10, lownera, 8);
        LOG_ERR ("==== glfs_lock_test_3", ret);

        /* TEST 4: Conflicting ranges, different lk_owner
         * lock1 (0, 10, lownera) - already taken
         * lock2 (50, 10, lownerb)
         * Expected: should not fail
         */
        ret = lock_test (NULL, fd2, false, 0, 10, lownera, 8,
                         50, 10, lownerb, 8);
        LOG_ERR ("==== glfs_lock_test_4", ret);

        /* TEST 5: Close fd1 & retry TEST2
         * lock1 (not applicable)
         * lock2 (5, 10, lownerb)
         * Expected: should succeed now
         */
        ret = glfs_close(fd1);
        LOG_ERR ("glfs_close", ret);

        ret = lock_test (NULL, fd2, false, 0, 10, lownera, 8,
                         5, 10, lownerb, 8);
        LOG_ERR ("==== glfs_lock_test_5", ret);

        /* TEST 6: Check closing fd1 doesn't flush fd2 locks
         * retry TEST 4 but with fd2 and fd3.
         * lock1 (50, 10, lownerb) - already taken
         * lock2 (55, 10, lownerc)
         * Expected: should fail
         */
        ret = lock_test (NULL, fd3, true, 50, 10, lownerb, 8,
                         55, 10, lownerc, 8);
        LOG_ERR ("==== glfs_lock_test_6", ret);

err:
        ret = glfs_close(fd2);
        LOG_ERR ("glfs_close", ret);

        ret = glfs_close(fd3);
        LOG_ERR ("glfs_close", ret);

out:
        if (fs) {
                ret = glfs_fini(fs);
                fprintf (stderr, "glfs_fini(fs) returned %d\n", ret);
        }

        if (ret)
                exit(1);
        exit(0);
}