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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
/* Few rules:
* 1. A client may have multiple lease keys, but a lease key cannot be shared by
* multiple clients.
* 2. Lease key can be set before open, or in glfs_lease request. A lease key
* set like this is valid for the lifetime of the fd, i.e. a fd cannot have
* multiple lease key. But a lease key can be shared across multiple fds.
*/
glfs_t *client1 = NULL, *client2 = NULL;
glfs_fd_t *fd1 = NULL;
FILE *log_file = NULL;
char lid1[GLFS_LEASE_ID_SIZE] = "lid1-clnt1",
lid2[GLFS_LEASE_ID_SIZE] = "lid2-clnt2";
char lid3[GLFS_LEASE_ID_SIZE] = "lid3-clnt2", lid4[GLFS_LEASE_ID_SIZE] = {
0,
};
char *volname = NULL, *glfs_log_file = NULL;
int upcall_recv = 0;
#define MAX_CLIENTS 4
#define MAX_FDS 4
#define TEST_FILE "/test/lease"
#define SHUD_PASS 0
#define SHUD_FAIL -1
#define NONE 0
static void
recall_cbk(struct glfs_lease lease, void *data);
static int
set_read_lease(glfs_fd_t *fd, char ld[])
{
struct glfs_lease lease = {
0,
};
int ret = 0;
memset(&lease, 0, sizeof(lease));
lease.cmd = GLFS_SET_LEASE;
lease.lease_type = GLFS_RD_LEASE;
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
ret = glfs_lease(fd, &lease, &recall_cbk, fd);
if (ret < 0) {
fprintf(log_file, "\n RD_LEASE failed with ret: %d (%s)", ret,
strerror(errno));
return -1;
}
fprintf(log_file, "\n Took RD_LEASE");
return ret;
}
static int
set_write_lease(glfs_fd_t *fd, char ld[])
{
struct glfs_lease lease = {
0,
};
int ret = 0;
memset(&lease, 0, sizeof(lease));
lease.cmd = GLFS_SET_LEASE;
lease.lease_type = GLFS_RW_LEASE;
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
if (ret < 0) {
fprintf(log_file, "\n RW_LEASE failed with ret: %d (%s)", ret,
strerror(errno));
return -1;
}
fprintf(log_file, "\n Took RW_LEASE");
return ret;
}
static int
get_lease(glfs_fd_t *fd, char ld[])
{
struct glfs_lease lease = {
0,
};
int ret = 0;
memset(&lease, 0, sizeof(lease));
lease.cmd = GLFS_GET_LEASE;
lease.lease_type = -1;
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
if (ret < 0) {
fprintf(log_file, "\n GET_LEASE failed with ret: %d (%s)", ret,
strerror(errno));
return -1;
}
if (lease.lease_type == GLFS_RD_LEASE)
fprintf(log_file, "\n Esisting Lease: RD_LEASE");
else if (lease.lease_type == GLFS_RW_LEASE)
fprintf(log_file, "\n Esisting Lease: RW_LEASE");
else if (lease.lease_type == 3)
fprintf(log_file, "\n Esisting Lease: RD_LEASE|RW_LEASE");
else if (lease.lease_type == 0)
fprintf(log_file, "\n Esisting Lease: NONE");
else
fprintf(log_file, "\n Existing lease type:%d", lease.lease_type);
return lease.lease_type;
}
static int
unlk_write_lease(glfs_fd_t *fd, char ld[])
{
struct glfs_lease lease = {
0,
};
int ret = 0;
memset(&lease, 0, sizeof(lease));
lease.cmd = GLFS_UNLK_LEASE;
lease.lease_type = GLFS_RW_LEASE;
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
if (ret < 0) {
fprintf(log_file, "\n Unlock RW_LESAE failed with ret: %d (%s)", ret,
strerror(errno));
return -1;
}
fprintf(log_file, "\n Unlocked RW_LEASE");
return ret;
}
static int
unlk_read_lease(glfs_fd_t *fd, char ld[])
{
struct glfs_lease lease = {
0,
};
int ret = 0;
memset(&lease, 0, sizeof(lease));
lease.cmd = GLFS_UNLK_LEASE;
lease.lease_type = GLFS_RD_LEASE;
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
if (ret < 0) {
fprintf(log_file, "\n Unlock RD_LEASE failed with ret: %d (%s)", ret,
strerror(errno));
return -1;
}
fprintf(log_file, "\n Unlocked RD_LEASE");
return ret;
}
void
up_async_lease_recall(struct glfs_upcall *up_arg, void *data)
{
struct glfs_upcall_lease *in_arg = NULL;
enum glfs_upcall_reason reason = 0;
struct glfs_object *object = NULL;
uint64_t flags = 0;
uint64_t expire = 0;
if (!up_arg)
return;
reason = glfs_upcall_get_reason(up_arg);
/* Expect 'GLFS_UPCALL_RECALL_LEASE' upcall event. */
if (reason == GLFS_UPCALL_RECALL_LEASE) {
in_arg = glfs_upcall_get_event(up_arg);
object = glfs_upcall_lease_get_object(in_arg);
fprintf(log_file,
" upcall event type - %d,"
" object(%p)\n",
reason, object);
upcall_recv = 1;
}
glfs_free(up_arg);
return;
}
glfs_t *
setup_new_client(char *volname, char *log_fileile)
{
int ret = 0;
glfs_t *fs = NULL;
int up_events = GLFS_EVENT_ANY;
fs = glfs_new(volname);
if (!fs) {
fprintf(log_file, "\nglfs_new: returned NULL (%s)\n", strerror(errno));
goto error;
}
ret = glfs_set_volfile_server(fs, "tcp", "localhost", 24007);
if (ret < 0) {
fprintf(log_file, "\nglfs_set_volfile_server failed ret:%d (%s)\n", ret,
strerror(errno));
goto error;
}
ret = glfs_set_logging(fs, log_fileile, 7);
if (ret < 0) {
fprintf(log_file, "\nglfs_set_logging failed with ret: %d (%s)\n", ret,
strerror(errno));
goto error;
}
ret = glfs_init(fs);
if (ret < 0) {
fprintf(log_file, "\nglfs_init failed with ret: %d (%s)\n", ret,
strerror(errno));
goto error;
}
/* Register Upcalls */
ret = glfs_upcall_register(fs, up_events, up_async_lease_recall, NULL);
/* Check if the return mask contains the event */
if ((ret < 0) || !(ret & GLFS_EVENT_RECALL_LEASE)) {
fprintf(stderr,
"glfs_upcall_register return doesn't contain"
" upcall event - GLFS_EVENT_RECALL_LEASE\n");
goto error;
}
return fs;
error:
if (fs)
glfs_fini(fs);
return NULL;
}
#define OPEN(client, flags, fd, lease_id) \
do { \
int ret_val = 0; \
ret_val = glfs_setfsleaseid(lease_id); \
if (ret_val) { \
fprintf(log_file, \
"\nglfs_setfsleaseid failed with ret: %d (%s)\n", ret, \
strerror(errno)); \
return -1; \
} \
fd = glfs_open(client, TEST_FILE, flags); \
if (fd == NULL) { \
fprintf(log_file, "\nglfs_open failed with ret: %d (%s)\n", ret, \
strerror(errno)); \
return -1; \
} \
} while (0)
#define VERIFY_RESULT(test_case, ret, value) \
do { \
if (ret != value) { \
fprintf(log_file, \
"\n Testcase %d failed, ret = %d, value=%d\n", \
test_case, ret, value); \
goto error; /*test unsuccessful*/ \
} \
fprintf(log_file, "\n Testcase %d Succeeded\n", test_case); \
} while (0)
static void
recall_cbk(struct glfs_lease lease, void *data)
{
int ret = -1;
char ld[GLFS_LEASE_ID_SIZE] = "";
fprintf(log_file, "\nRECALL received on lease_id:(%s)", lease.lease_id);
memcpy(ld, lease.lease_id, GLFS_LEASE_ID_SIZE);
ret = unlk_write_lease((glfs_fd_t *)data, ld);
VERIFY_RESULT(500, ret, SHUD_PASS);
error:
return;
}
static int
testcase_recall_conflict_lease()
{
struct glfs_object *obj = NULL;
glfs_fd_t *fd1 = NULL;
int ret = 0;
struct glfs_lease lease = {
0,
};
fprintf(log_file,
"\n Basic test case for conflicting lease causing recall");
memset(&lease, 0, sizeof(lease));
lease.cmd = GLFS_SET_LEASE;
lease.lease_type = GLFS_RD_LEASE;
memcpy(&lease.lease_id, lid2, GLFS_LEASE_ID_SIZE);
/* Open fd on client 1 in RD mode */
OPEN(client1, O_RDWR, fd1, lid1);
ret = set_write_lease(fd1, lid1);
VERIFY_RESULT(1, ret, SHUD_PASS);
/* reset counter */
upcall_recv = 0;
obj = glfs_h_lookupat(client2, NULL, TEST_FILE, NULL, 0);
ret = glfs_h_lease(client2, obj, &lease);
VERIFY_RESULT(2, ret, SHUD_FAIL);
sleep(3);
/* should recv upcall */
VERIFY_RESULT(6, !upcall_recv, SHUD_PASS);
ret = unlk_write_lease(fd1, lid1);
VERIFY_RESULT(5, ret, SHUD_PASS);
ret = glfs_h_close(obj);
VERIFY_RESULT(3, ret, SHUD_PASS);
ret = glfs_close(fd1);
VERIFY_RESULT(4, ret, SHUD_PASS);
return 0;
error:
return -1;
}
int
main(int argc, char *argv[])
{
int ret = 0;
int i = 0;
glfs_fd_t *fd = NULL;
glfs_fd_t *fd1 = NULL;
char *topdir = "topdir", *filename = "file1";
char *buf = NULL;
int x = 0;
ssize_t xattr_size = -1;
if (argc != 4) {
fprintf(stderr,
"Expect following args %s <Vol> <glfs client log file> "
"<testcase log file>\n",
argv[0]);
return -1;
}
log_file = fopen(argv[3], "w");
if (!log_file)
goto error;
volname = argv[1];
glfs_log_file = argv[2];
/* Setup 2 clients */
client1 = setup_new_client(volname, glfs_log_file);
client2 = setup_new_client(volname, glfs_log_file);
ret = testcase_recall_conflict_lease();
VERIFY_RESULT(101, ret, SHUD_PASS);
glfs_fini(client1);
glfs_fini(client2);
fclose(log_file);
return 0;
error:
return -1;
}
|