Whamcloud - gitweb
LU-9511 utils: fix parallel_grouplock test timeout
[fs/lustre-release.git] / lustre / tests / mpi / parallel_grouplock.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  *
30  * lustre/tests/parallel_grouplock.c
31  *
32  * Author: You Feng <youfeng@clusterfs.com>
33  */
34
35 #include <limits.h>
36 #include <mpi.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <asm/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <sys/ioctl.h>
45 #include <unistd.h>
46 #include <time.h>
47 #include <errno.h>
48 #include <lustre/lustre_user.h>
49 #include "lp_utils.h"
50
51 #define LPGL_BUF_LEN 8192
52 #define LPGL_TEST_ITEMS 12
53
54 #define MIN_GLHOST 5
55
56 #define MAX_WAIT_TRIES            10
57 #define WAIT_TIME                  1  /* secs */
58 #define ONE_MB               1048576  /*   1 MB */
59 #define MIN_LGBUF_SIZE     536870912  /* 512 MB */
60 #define MAX_LGBUF_SIZE     536870912  /* 512 MB */
61 // #define MAX_LGBUF_SIZE    1073741824  /*   1 GB */
62
63 #define READ    1
64 #define WRITE   2
65 #define IOCTL   3
66 #define CLOSE   4
67
68 int rank = 0;
69 int size = 0;
70
71 char *testdir = NULL;
72 int   only_test;
73
74 char  buf[LPGL_BUF_LEN];
75 char *lgbuf;
76 int   lgbuf_size;
77 char  filename[MAX_FILENAME_LEN];
78 char  errmsg[MAX_FILENAME_LEN+20];
79
80 static void
81 alloc_lgbuf()
82 {
83
84         if (lgbuf)
85                 return;
86
87         lgbuf_size = MAX_LGBUF_SIZE;
88         for (; lgbuf_size >= MIN_LGBUF_SIZE; lgbuf_size -= ONE_MB)
89                 if ((lgbuf = (char *)malloc(lgbuf_size)) != NULL)
90                         return;
91
92         FAIL("malloc of large buffer failed.\n");
93 }
94
95 static inline void
96 read_buf(int fd)
97 {
98         int pos, rc;
99
100         rc = read(fd, buf, sizeof(buf));
101         if (rc == -1) {
102                 pos = lseek(fd, 0, SEEK_CUR);
103                 sprintf(errmsg, "read of file %s at pos %d for %zu bytes "
104                         "returned %d: (%d) %s.\n",
105                         filename, pos, sizeof(buf), rc, errno, strerror(errno));
106                 FAIL(errmsg);
107         } else if (rc != sizeof(buf)) {
108                 pos = lseek(fd, 0, SEEK_CUR);
109                 sprintf(errmsg, "read of file %s at pos %d for %zu bytes "
110                         "returned %d.\n",
111                         filename, pos, sizeof(buf), rc);
112                 FAIL(errmsg);
113         }
114 }
115
116 static inline void
117 write_buf(int fd, int index)
118 {
119         int pos = index * sizeof(buf);
120         int rc;
121
122         memset(buf, index, sizeof(buf));
123         lseek(fd, pos, SEEK_SET);
124         rc = write(fd, buf, sizeof(buf));
125         if (rc == -1) {
126                 sprintf(errmsg, "write of file %s at pos %d for %zu bytes "
127                         "returned %d: (%d) %s.\n",
128                         filename, pos, sizeof(buf), rc, errno, strerror(errno));
129                 FAIL(errmsg);
130         } else if (rc != sizeof(buf)) {
131                 sprintf(errmsg, "write of file %s at pos %d for %zu bytes "
132                         "returned %d.\n",
133                         filename, pos, sizeof(buf), rc);
134                 FAIL(errmsg);
135         }
136 }
137
138 /*
139  * task0 attempts GR(gid=1) -- granted immediately
140  * task1 attempts PR|PW -- blocked, goes on waiting list
141  * task2 attempts GR(gid=1) -> should be granted
142  * task2 writes to file and releases GR(gid=1)
143  * task0 waits for task2 to complete its processing
144  * task0 writes to file and releases GR(gid=1)
145  * task1 PR|PW should be granted and reads the file
146  */
147 void grouplock_test1(char *filename, int fd, int blocking_op, int unlock_op)
148 {
149         MPI_Request req1, req2;
150         int iter, flag1, flag2, temp1, temp2;
151         int i, rc, gid = 1;
152
153         if (rank == 0) {
154                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
155                         sprintf(errmsg,
156                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
157                                 filename, errno, strerror(errno));
158                         FAIL(errmsg);
159                 }
160         }
161
162         MPI_Barrier(MPI_COMM_WORLD);
163
164         switch (rank) {
165         case 1:
166                 if (blocking_op == WRITE) {
167                         write_buf(fd, rank);
168                         lseek(fd, 0, SEEK_SET);
169                 }
170
171                 for (i = 0; i <= 2; i++)
172                         read_buf(fd);
173
174                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
175                 break;
176         case 2:
177                 /* Wait for task1 to progress. This could be racey. */
178                 sleep(WAIT_TIME);
179
180                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
181                         sprintf(errmsg,
182                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
183                                 filename, errno, strerror(errno));
184                         FAIL(errmsg);
185                 }
186
187                 write_buf(fd, rank);
188
189                 if (unlock_op == CLOSE)
190                         rc = close(fd);
191                 else
192                         rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
193
194                 if (rc == -1) {
195                         sprintf(errmsg,
196                                 "%s release GROUP_LOCK of file %s: (%d) %s.\n",
197                                 (unlock_op == CLOSE) ? "close" : "ioctl",
198                                 filename, errno, strerror(errno));
199                         FAIL(errmsg);
200                 }
201                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
202                 break;
203         case 0:
204                 /* PR|PW task will tell us when it completes */
205                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
206                 /* 2nd locking task will tell us when it completes */
207                 MPI_Irecv(&temp2, 1, MPI_INT, 2, 1, MPI_COMM_WORLD, &req2);
208
209                 /* Wait for task2 to complete. */
210                 iter = MAX_WAIT_TRIES;
211                 do {
212                         iter--;
213                         if (!iter) {
214                                 FAIL("2nd locking task is not progressing\n");
215                         }
216
217                         sleep(WAIT_TIME);
218
219                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
220                         if (flag1) {
221                                 FAIL("PR|PW task progressed even though GROUP "
222                                      "lock is held\n");
223                         }
224
225                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
226                 } while (!flag2);
227
228                 /* Make sure task1 is still waiting. */
229                 iter = MAX_WAIT_TRIES;
230                 do {
231                         iter--;
232                         sleep(WAIT_TIME);
233                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
234                         if (flag1) {
235                                 FAIL("PR|PW task progressed even though "
236                                      "GROUP lock is held\n");
237                         }
238                 } while (iter);
239
240                 write_buf(fd, rank);
241
242                 /* Now we need to release the lock */
243                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
244                         sprintf(errmsg,
245                                 "ioctl GROUP_UNLOCK of file %s: (%d) %s.\n",
246                                 filename, errno, strerror(errno));
247                         FAIL(errmsg);
248                 }
249
250                 /* Wait for task1 to complete. */
251                 iter = MAX_WAIT_TRIES;
252                 do {
253                         iter--;
254                         if (!iter) {
255                                 FAIL("PR|PW task is not progressing even "
256                                      "though GROUP lock was released\n");
257                                 break;
258                         }
259                         sleep(WAIT_TIME);
260                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
261                 } while (!flag1);
262
263                 break;
264         }
265 }
266
267 /*
268  * task0 attempts GR(gid=1) -- granted immediately
269  * task1 attempts GR(gid=2) -- blocked
270  * task2 attempts PR|PW -- blocked
271  * task3 attempts GR(gid=2) -- blocked
272  * task4 attempts GR(gid=1) -- should be granted
273  * task0,4 writes to file and releases GR(gid=1) --
274  *       this allows task2 & 3's GR locks to be granted; task4 remains blocked.
275  * task1 & 3 write to file and release GR(gid=2)
276  * task2 PR|PW should be granted and reads the file.
277  */
278 void grouplock_test2(char *filename, int fd, int blocking_op, int unlock_op)
279 {
280         int i, iter, rc, gid = 1;
281         int flag1, flag2, flag3, flag4;
282         int temp1, temp2, temp3, temp4;
283         MPI_Request req1, req2, req3, req4;
284
285         if (rank == 0) {
286                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
287                         sprintf(errmsg,
288                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
289                                 filename, errno, strerror(errno));
290                         FAIL(errmsg);
291                 }
292         }
293
294         MPI_Barrier(MPI_COMM_WORLD);
295
296         switch (rank) {
297         case 3:
298                 /* Wait for task2 to issue its read request. */
299                 sleep(2*WAIT_TIME);
300         case 1:
301                 gid = 2;
302                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
303                         sprintf(errmsg,
304                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
305                                 filename, errno, strerror(errno));
306                         FAIL(errmsg);
307                 }
308
309                 write_buf(fd, rank);
310
311                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
312
313                 /* Do not release the locks until task 0 is ready to watch
314                    for reading task only */
315                 MPI_Recv(&temp1, 1, MPI_INT, 0, 1, MPI_COMM_WORLD,
316                          MPI_STATUS_IGNORE);
317
318                 if (unlock_op == CLOSE)
319                         rc = close(fd);
320                 else
321                         rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
322                 if (rc == -1) {
323                         sprintf(errmsg,
324                                 "%s release GROUP_LOCK of file %s: (%d) %s.\n",
325                                 (unlock_op == CLOSE) ? "close" : "ioctl",
326                                 filename, errno, strerror(errno));
327                         FAIL(errmsg);
328                 }
329                 break;
330         case 2:
331                 /* Give task1 a chance to request its GR lock. */
332                 sleep(WAIT_TIME);
333
334                 if (blocking_op == WRITE) {
335                         write_buf(fd, rank);
336                         lseek(fd, 0, SEEK_SET);
337                 }
338
339                 for (i = 0; i <= 3; i++)
340                         read_buf(fd);
341
342                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
343                 break;
344         case 4:
345                 /* Give task1 & 3 a chance to queue their GR locks. */
346                 sleep(3*WAIT_TIME);
347
348                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
349                         sprintf(errmsg,
350                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
351                                 filename, errno, strerror(errno));
352                         FAIL(errmsg);
353                 }
354
355                 write_buf(fd, rank);
356
357                 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
358                 if (rc == -1) {
359                         sprintf(errmsg,
360                                 "%s release GROUP_LOCK of file %s: (%d) %s.\n",
361                                 (unlock_op == CLOSE) ? "close" : "ioctl",
362                                 filename, errno, strerror(errno));
363                         FAIL(errmsg);
364                 }
365
366                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
367                 break;
368         case 0:
369                 /* locking tasks will tell us when they complete */
370                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
371                 MPI_Irecv(&temp2, 1, MPI_INT, 2, 1, MPI_COMM_WORLD, &req2);
372                 MPI_Irecv(&temp3, 1, MPI_INT, 3, 1, MPI_COMM_WORLD, &req3);
373                 MPI_Irecv(&temp4, 1, MPI_INT, 4, 1, MPI_COMM_WORLD, &req4);
374
375                 /* Make sure all tasks that should be blocked are waiting. */
376                 iter = MAX_WAIT_TRIES;
377                 do {
378                         iter--;
379                         sleep(WAIT_TIME);
380                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
381                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
382                         MPI_Test(&req3, &flag3, MPI_STATUS_IGNORE);
383                         if (flag1 || flag3) {
384                                 FAIL("GROUP (gid=2) task progressed even though"
385                                      " GROUP (gid=1) lock is held.\n");
386                         }
387                         if (flag2) {
388                                 FAIL("PR|PW task progressed even though "
389                                      "GROUP (gid=1) lock is still held\n");
390                         }
391                 } while (iter);
392
393                 /* Wait for task4 to signal it has completed. */
394                 iter = MAX_WAIT_TRIES;
395                 do {
396                         iter--;
397                         if (!iter) {
398                                 FAIL("2nd task GROUP(gid=1) not progressing\n");
399                         }
400                         sleep(WAIT_TIME);
401                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
402                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
403                         MPI_Test(&req3, &flag3, MPI_STATUS_IGNORE);
404                         MPI_Test(&req4, &flag4, MPI_STATUS_IGNORE);
405                         if (flag1 || flag3) {
406                                 FAIL("GROUP (gid=2) task progressed even though"
407                                      " GROUP (gid=1) lock is held.\n");
408                         }
409                         if (flag2) {
410                                 FAIL("PR|PW task progressed even though "
411                                      "GROUP (gid=1) lock is still held\n");
412                         }
413                 } while (!flag4);
414
415                 write_buf(fd, rank);
416
417                 /* Now let's release first lock */
418                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
419                         sprintf(errmsg, "ioctl GROUP_UNLOCK of file %s "
420                                 "returned %d", filename, rc);
421                         FAIL(errmsg);
422                 }
423
424                 /* Wait for task1 & 3 to signal they have their lock. */
425                 iter = MAX_WAIT_TRIES;
426                 do {
427                         iter--;
428                         if (!iter) {
429                                 FAIL("GROUP(gid=2) tasks not progressing\n");
430                         }
431                         sleep(WAIT_TIME);
432                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
433                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
434                         MPI_Test(&req3, &flag3, MPI_STATUS_IGNORE);
435                         if (flag2) {
436                                 fprintf(stderr, "task2 %d\n", flag2);
437                                 FAIL("PR task progressed even though GROUP lock"
438                                      " was on the queue task\n");
439                         }
440                 } while (!(flag1 && flag3));
441
442                 /* Make sure task2 is still waiting. */
443                 iter = MAX_WAIT_TRIES;
444                 do {
445                         iter--;
446                         sleep(WAIT_TIME);
447                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
448                         if (flag2) {
449                                 FAIL("PR task progressed even though GR(gid=2) "
450                                      "lock was active.\n");
451                         }
452                 } while (iter);
453
454                 /* Tell task1 & 3 to release their GR(gid=2) lock. */
455                 MPI_Send(&gid, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
456                 MPI_Send(&gid, 1, MPI_INT, 3, 1, MPI_COMM_WORLD);
457
458                 /* Wait for task2 (PR) to complete. */
459                 iter = MAX_WAIT_TRIES;
460                 do {
461                         iter--;
462                         if (!iter) {
463                                 FAIL("reading task is not progressing even "
464                                      "though GROUP locks are released\n");
465                                 break;
466                         }
467                         sleep(WAIT_TIME);
468                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
469                 } while (!flag3);
470                 break;
471         }
472 }
473
474 /*
475  * Tests a bug that once existed in the group lock code;
476  * i.e. that a GR lock request on a O_NONBLOCK fd could fail even though
477  * there is no blocking GROUP lock ahead of it on the waitq.
478  *
479  * task0 starts a large write (PW). this test could be racey if this
480  *       write finishes too quickly.
481  * task1 attempts GR(gid=1) -- blocked
482  * task2 attempts GR(gid=2) with a O_NONBLOCK fs. should not fail.
483  */
484 void grouplock_test3(char *filename, int fd)
485 {
486         MPI_Request req1, req2;
487         int iter, flag1, flag2, temp1, temp2;
488         int rc, gid = 1;
489
490         if (rank == 0) {
491                 alloc_lgbuf();
492         } else if (rank == 2) {
493                 rc = fcntl(fd, F_SETFL, O_NONBLOCK);
494                 if (rc == -1) {
495                         sprintf(errmsg, "fcntl(O_NONBLOCK) failed: (%d) %s.\n",
496                                 errno, strerror(errno));
497                         FAIL(errmsg);
498                 }
499         }
500
501         MPI_Barrier(MPI_COMM_WORLD);
502
503         switch (rank) {
504         case 2:
505                 gid = 2;
506                 usleep(10000);
507                 usleep(10000);
508         case 1:
509                 /* Racey, we have to sleep just long enough for
510                  * task0's write to start. */
511                 usleep(10000);
512
513                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
514                         sprintf(errmsg,
515                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
516                                 filename, errno, strerror(errno));
517                         FAIL(errmsg);
518                 }
519
520                 /* tell task0 we have the lock. */
521                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
522
523                 /* the close of fd will release the lock. */
524                 break;
525         case 0:
526                 rc = write(fd, lgbuf, lgbuf_size);
527                 if (rc == -1) {
528                         sprintf(errmsg, "write of file %s for %d bytes "
529                                 "returned %d: (%d) %s.\n",
530                                 filename, lgbuf_size,
531                                 rc, errno, strerror(errno));
532                         FAIL(errmsg);
533                 } else if (rc != lgbuf_size) {
534                         sprintf(errmsg, "write of file %s for %d bytes "
535                                 "returned %d.\n",
536                                 filename, lgbuf_size, rc);
537                         FAIL(errmsg);
538                 }
539
540                 /* GR tasks will tell us when they complete */
541                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
542                 MPI_Irecv(&temp2, 1, MPI_INT, 2, 1, MPI_COMM_WORLD, &req2);
543
544                 /* Wait for task1 & 2 to complete. */
545                 iter = MAX_WAIT_TRIES;
546                 do {
547                         iter--;
548                         if (!iter) {
549                                 FAIL("GR(gid=1) tasks are not progressing even "
550                                      "no conflicting locks exist.\n");
551                                 break;
552                         }
553                         sleep(WAIT_TIME);
554                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
555                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
556                 } while (!(flag1 && flag2));
557                 break;
558         }
559 }
560
561 /*
562  * Tests a bug that once existed in the group lock code;
563  * i.e. extent locks without O_NONBLOCK that go on the waitq before a group
564  * lock request came in and was granted. The extent lock would timed out and
565  * produce an error.
566  *
567  * task0 starts a large write (PW). this test could be racey if this
568  *       write finishes too quickly.
569  * task1 attempts PR -- blocked
570  * task2 attempts GR(gid=1) -- blocked
571  * task0 completes write
572  * task1 should wakeup and complete its read
573  * task2 should wakeup and after task1 complete.
574  */
575 void grouplock_test4(char *filename, int fd)
576 {
577         MPI_Request req1;
578         int iter, flag1, temp1;
579         int rc, gid = 1;
580
581         if (rank == 0)
582                 alloc_lgbuf();
583
584         MPI_Barrier(MPI_COMM_WORLD);
585
586         switch (rank) {
587         case 1:
588                 /* Racey, we have to sleep just long enough for
589                  * task0's write to start. */
590                 MPI_Recv(&temp1, 1, MPI_INT, 0, 1, MPI_COMM_WORLD,
591                          MPI_STATUS_IGNORE);
592
593                 /* tell task2 to go. */
594                 MPI_Send(&gid, 1, MPI_INT, 2, 1, MPI_COMM_WORLD);
595                 sleep(WAIT_TIME);
596
597                 read_buf(fd);
598                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
599                 break;
600         case 2:
601                 /* Give task0 & 1 a chance to start. */
602                 MPI_Recv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD,
603                          MPI_STATUS_IGNORE);
604                 sleep(2 * WAIT_TIME);
605
606                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
607                         sprintf(errmsg,
608                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
609                                 filename, errno, strerror(errno));
610                         FAIL(errmsg);
611                 }
612
613                 /* tell task0 we have the lock. */
614                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
615
616                 /* Do not release the locks until task 0 tells us too.
617                    for reading task only */
618                 MPI_Recv(&temp1, 1, MPI_INT, 0, 1, MPI_COMM_WORLD,
619                          MPI_STATUS_IGNORE);
620
621                 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
622                 if (rc == -1) {
623                         sprintf(errmsg,
624                                 "ioctl GROUP_UNLOCK of file %s: (%d) %s.\n",
625                                 filename, errno, strerror(errno));
626                         FAIL(errmsg);
627                 }
628                 break;
629         case 0:
630                 /* tell task1 to go to avoid race */
631                 MPI_Send(&gid, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
632                 rc = write(fd, lgbuf, lgbuf_size);
633                 if (rc == -1) {
634                         sprintf(errmsg, "write of file %s for %d bytes "
635                                 "returned %d: (%d) %s.\n",
636                                 filename, lgbuf_size,
637                                 rc, errno, strerror(errno));
638                         FAIL(errmsg);
639                 } else if (rc != lgbuf_size) {
640                         sprintf(errmsg, "write of file %s for %d bytes "
641                                 "returned %d.\n",
642                                 filename, lgbuf_size, rc);
643                         FAIL(errmsg);
644                 }
645
646                 /* wait for task2 to get its lock. */
647                 MPI_Recv(&temp1, 1, MPI_INT, 2, 1, MPI_COMM_WORLD,
648                          MPI_STATUS_IGNORE);
649
650                 /* Tell task2 it's ok to release its GR(gid=1) lock. */
651                 MPI_Send(&gid, 1, MPI_INT, 2, 1, MPI_COMM_WORLD);
652
653                 /* wait a really long time. */
654                 sleep(180 * WAIT_TIME);
655
656                 /* PR task will tell us when it completes */
657                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
658
659                 /* Make sure the PR task is successful and doesn't hang.
660                  *
661                  * XXX - To test properly we need to make sure the read
662                  *       gets queued before task2's group lock request.
663                  *       You may need to increase lgbuf_size.
664                  */
665                 iter = MAX_WAIT_TRIES;
666                 do {
667                         iter--;
668                         if (!iter) {
669                                 FAIL("PR task is hung !\n");
670                                 break;
671                         }
672                         sleep(WAIT_TIME);
673                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
674                 } while (!flag1);
675
676                 break;
677         }
678 }
679
680 /*
681  * task0 attempts GR(gid=1) -- granted
682  * task1 attempts PR on non-blocking fd -> should return -EWOULDBLOCK
683  * task2 attempts PW on non-blocking fd -> should return -EWOULDBLOCK
684  * task3 attempts GR(gid=2) on non-blocking fd -> should return -EWOULDBLOCK
685  */
686 void grouplock_nonblock_test(char *filename, int fd)
687 {
688         MPI_Request req1, req2, req3;
689         int iter, flag1, flag2, flag3, temp1, temp2, temp3;
690         int rc, gid = 1;
691
692         if (rank == 0) {
693                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
694                         sprintf(errmsg,
695                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
696                                 filename, errno, strerror(errno));
697                         FAIL(errmsg);
698                 }
699         }
700
701         rc = fcntl(fd, F_SETFL, O_NONBLOCK);
702         if (rc == -1) {
703                 sprintf(errmsg, "fcntl(O_NONBLOCK) failed: (%d) %s.\n",
704                         errno, strerror(errno));
705                 FAIL(errmsg);
706         }
707
708         MPI_Barrier(MPI_COMM_WORLD);
709
710         switch (rank) {
711         case 1:
712                 rc = read(fd, buf, sizeof(buf));
713                 if ((rc != -1) || (errno != EWOULDBLOCK)) {
714                         FAIL("PR lock succeeded while incompatible "
715                              "GROUP LOCK (gid=1) is still held\n");
716                 }
717
718                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
719                 break;
720         case 2:
721                 rc = write(fd, buf, sizeof(buf));
722                 if ((rc != -1) || (errno != EWOULDBLOCK)) {
723                         FAIL("PW lock succeeded while incompatible "
724                              "GROUP LOCK (gid=1) is still held\n");
725                 }
726
727                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
728                 break;
729         case 3:
730                 gid = 2;
731                 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
732                 if ((rc != -1) || (errno != EWOULDBLOCK)) {
733                         FAIL("GROUP_LOCK (gid=2) succeeded while incompatible "
734                              "GROUP LOCK (gid=1) is still held.\n");
735                 }
736
737                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
738                 break;
739         case 0:
740                 /* reading task will tell us when it completes */
741                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
742                 /* writing task will tell us when it completes */
743                 MPI_Irecv(&temp2, 1, MPI_INT, 2, 1, MPI_COMM_WORLD, &req2);
744                 /* 2nd locking task will tell us when it completes */
745                 MPI_Irecv(&temp3, 1, MPI_INT, 3, 1, MPI_COMM_WORLD, &req3);
746
747                 iter = MAX_WAIT_TRIES;
748                 do {
749                         iter--;
750                         if (!iter) {
751                                 FAIL("non-blocking tasks are not progressing\n");
752                         }
753                         sleep(WAIT_TIME);
754                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
755                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
756                         MPI_Test(&req3, &flag3, MPI_STATUS_IGNORE);
757                 } while (!(flag1 && flag2 && flag3));
758
759                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
760                         sprintf(errmsg, "ioctl GROUP_UNLOCK of file %s",
761                                 filename);
762                         FAIL(errmsg);
763                 }
764                 break;
765         }
766 }
767
768 /* Just test some error paths with invalid requests */
769 void grouplock_errorstest(char *filename, int fd)
770 {
771         int rc, gid = 1;
772
773         MPI_Barrier(MPI_COMM_WORLD);
774
775         switch (rank) {
776         case 0:
777                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
778                         sprintf(errmsg,
779                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
780                                 filename, errno, strerror(errno));
781                         FAIL(errmsg);
782                 }
783
784                 /* second group lock on same fd, same gid */
785                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
786                         if (errno != EINVAL) {
787                                 sprintf(errmsg, "Double GROUP lock failed "
788                                         "with errno %d instead of EINVAL\n",
789                                         errno);
790                                 FAIL(errmsg);
791                         }
792                 } else {
793                         FAIL("Taking second GROUP lock on same fd succeed\n");
794                 }
795
796                 /* second group lock on same fd, different gid */
797                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid + 1)) == -1) {
798                         if (errno != EINVAL) {
799                                 sprintf(errmsg, "Double GROUP lock with "
800                                         "different gid failed with errno %d "
801                                         "instead of EINVAL\n", errno);
802                                 FAIL(errmsg);
803                         }
804                 } else {
805                         FAIL("Taking second GROUP lock on same fd, with "
806                              "different gid, succeeded.\n");
807                 }
808
809                 /* GROUP unlock with wrong gid */
810                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid + 1)) == -1) {
811                         if (errno != EINVAL) {
812                                 sprintf(errmsg, "GROUP_UNLOCK with wrong gid "
813                                         "failed with errno %d instead of "
814                                         "EINVAL\n", errno);
815                                 FAIL(errmsg);
816                         }
817                 } else {
818                         FAIL("GROUP unlock with wrong gid succeed\n");
819                 }
820
821                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
822                         sprintf(errmsg, "ioctl GROUP_UNLOCK of file %s "
823                                 "returned %d.", filename, rc);
824                         FAIL(errmsg);
825                 }
826                 break;
827
828         case 1:
829                 /* unlock of never locked fd */
830                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
831                         if (errno != EINVAL) {
832                                 sprintf(errmsg, "GROUP_UNLOCK on never locked "
833                                         "fd failed with errno %d instead of "
834                                         "EINVAL.\n", errno);
835                                 FAIL(errmsg);
836                         }
837                 } else {
838                         FAIL("GROUP unlock on never locked fd succeed\n");
839                 }
840                 break;
841         }
842 }
843
844 void grouplock_file(char *name, int subtest)
845 {
846         int fd;
847         int flags = O_CREAT|O_RDWR|O_SYNC|O_TRUNC;
848         int mode = 0666;
849
850         sprintf(filename, "%s/%s", testdir, name);
851
852         if ((fd = open(filename, flags, mode)) == -1) {
853                 sprintf(errmsg, "open of file %s: (%d) %s.\n",
854                         filename, errno, strerror(errno));
855                 FAIL(errmsg);
856         }
857
858         MPI_Barrier(MPI_COMM_WORLD);
859
860         switch (subtest) {
861         case 1:
862                 grouplock_test1(filename, fd, READ, IOCTL);
863                 break;
864         case 2:
865                 grouplock_test1(filename, fd, READ, CLOSE);
866                 break;
867         case 3:
868                 grouplock_test1(filename, fd, WRITE, IOCTL);
869                 break;
870         case 4:
871                 grouplock_test1(filename, fd, WRITE, CLOSE);
872                 break;
873         case 5:
874                 grouplock_test2(filename, fd, READ, IOCTL);
875                 break;
876         case 6:
877                 grouplock_test2(filename, fd, READ, CLOSE);
878                 break;
879         case 7:
880                 grouplock_test2(filename, fd, WRITE, IOCTL);
881                 break;
882         case 8:
883                 grouplock_test2(filename, fd, WRITE, CLOSE);
884                 break;
885         case 9:
886                 grouplock_nonblock_test(filename, fd);
887                 break;
888         case 10:
889                 grouplock_errorstest(filename, fd);
890                 break;
891         case 11:
892                 grouplock_test3(filename, fd);
893                 break;
894         case 12:
895                 grouplock_test4(filename, fd);
896                 break;
897         default:
898                 sprintf(errmsg, "wrong subtest number %d (should be <= %d)",
899                         subtest, LPGL_TEST_ITEMS);
900                 FAIL(errmsg);
901         }
902
903         close(fd);
904
905         if (rank == 0)
906                 unlink(filename);
907
908         MPI_Barrier(MPI_COMM_WORLD);
909 }
910
911 void parallel_grouplock(void)
912 {
913         char teststr[16];
914         int i;
915
916         if (only_test) {
917                 sprintf(teststr, "subtest %d", only_test);
918                 begin(teststr);
919                 grouplock_file("parallel_grouplock", only_test);
920                 end(teststr);
921         } else {
922                 for (i = 1; i <= LPGL_TEST_ITEMS; i++) {
923                         sprintf(teststr, "subtest %d", i);
924                         begin(teststr);
925                         grouplock_file("parallel_grouplock", i);
926                         end(teststr);
927                 }
928         }
929 }
930
931 void usage(char *proc)
932 {
933         int i;
934
935         if (rank == 0) {
936                 printf("Usage: %s [-h] -d <testdir> [-n <num>]\n", proc);
937                 printf("           [-t <num>] [-v] [-V #] [-g]\n");
938                 printf("\t-h: prints this help message\n");
939                 printf("\t-d: the directory in which the tests will run\n");
940                 printf("\t-n: repeat test # times\n");
941                 printf("\t-t: run a particular test #\n");
942                 printf("\t-v: increase the verbositly level by 1\n");
943                 printf("\t-V: select a specific verbosity level\n");
944                 printf("\t-g: debug mode\n");
945         }
946
947         MPI_Initialized(&i);
948         if (i) MPI_Finalize();
949         exit(0);
950 }
951
952 int main(int argc, char *argv[])
953 {
954         int i, iterations = 1, c;
955
956         setbuf(stdout, 0);
957         setbuf(stderr, 0);
958
959         /* Check for -h parameter before MPI_Init so the binary can be
960            called directly, without, for instance, mpirun */
961         for (i = 1; i < argc; ++i) {
962                 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
963                         usage(argv[0]);
964         }
965
966         MPI_Init(&argc, &argv);
967         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
968         MPI_Comm_size(MPI_COMM_WORLD, &size);
969
970         /* Parse command line options */
971         while (1) {
972                 c = getopt(argc, argv, "d:ghn:t:vV:");
973                 if (c == -1)
974                         break;
975
976                 switch (c) {
977                 case 'd':
978                         testdir = optarg;
979                         break;
980                 case 'g':
981                         debug = 1;
982                         break;
983                 case 'h':
984                         usage(argv[0]);
985                         break;
986                 case 'n':
987                         iterations = atoi(optarg);
988                         break;
989                 case 't':
990                         only_test = atoi(optarg);
991                         break;
992                 case 'v':
993                         verbose += 1;
994                         break;
995                 case 'V':
996                         verbose = atoi(optarg);
997                         break;
998                 }
999         }
1000
1001         if (rank == 0)
1002                 printf("%s is running with %d task(es) %s\n",
1003                        argv[0], size, debug ? "in DEBUG mode" : "\b\b");
1004
1005         if (size < MIN_GLHOST) {
1006                 fprintf(stderr, "Error: "
1007                         "%d tasks run, but should be at least %d tasks to run "
1008                         "the test!\n", size, MIN_GLHOST);
1009                 MPI_Abort(MPI_COMM_WORLD, 2);
1010         }
1011
1012         if (testdir == NULL && rank == 0) {
1013                 fprintf(stderr, "Please specify a test directory! "
1014                         "(\"%s -h\" for help)\n",
1015                        argv[0]);
1016                 MPI_Abort(MPI_COMM_WORLD, 2);
1017         }
1018
1019         lp_gethostname();
1020
1021         for (i = 0; i < iterations; ++i) {
1022                 if (rank == 0)
1023                         printf("%s: Running test #%s(iter %d)\n",
1024                                timestamp(), argv[0], i);
1025
1026                 parallel_grouplock();
1027                 MPI_Barrier(MPI_COMM_WORLD);
1028         }
1029
1030         if (rank == 0) {
1031                 printf("%s: All tests passed!\n", timestamp());
1032         }
1033         MPI_Finalize();
1034         return 0;
1035 }