Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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                 usleep(10000);
593                 usleep(10000);
594
595                 /* tell task2 to go. */
596                 MPI_Send(&gid, 1, MPI_INT, 2, 1, MPI_COMM_WORLD);
597
598
599                 read_buf(fd);
600                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
601                 break;
602         case 2:
603                 /* Give task0 & 1 a chance to start. */
604                 MPI_Recv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD,
605                          MPI_STATUS_IGNORE);
606                 usleep(25000);
607                 usleep(25000);
608
609                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
610                         sprintf(errmsg,
611                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
612                                 filename, errno, strerror(errno));
613                         FAIL(errmsg);
614                 }
615
616                 /* tell task0 we have the lock. */
617                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
618
619                 /* Do not release the locks until task 0 tells us too.
620                    for reading task only */
621                 MPI_Recv(&temp1, 1, MPI_INT, 0, 1, MPI_COMM_WORLD,
622                          MPI_STATUS_IGNORE);
623
624                 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
625                 if (rc == -1) {
626                         sprintf(errmsg,
627                                 "ioctl GROUP_UNLOCK of file %s: (%d) %s.\n",
628                                 filename, errno, strerror(errno));
629                         FAIL(errmsg);
630                 }
631                 break;
632         case 0:
633                 /* tell task1 to go to avoid race */
634                 MPI_Send(&gid, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
635                 rc = write(fd, lgbuf, lgbuf_size);
636                 if (rc == -1) {
637                         sprintf(errmsg, "write of file %s for %d bytes "
638                                 "returned %d: (%d) %s.\n",
639                                 filename, lgbuf_size,
640                                 rc, errno, strerror(errno));
641                         FAIL(errmsg);
642                 } else if (rc != lgbuf_size) {
643                         sprintf(errmsg, "write of file %s for %d bytes "
644                                 "returned %d.\n",
645                                 filename, lgbuf_size, rc);
646                         FAIL(errmsg);
647                 }
648
649                 /* wait for task2 to get its lock. */
650                 MPI_Recv(&temp1, 1, MPI_INT, 2, 1, MPI_COMM_WORLD,
651                          MPI_STATUS_IGNORE);
652
653                 /* Tell task2 it's ok to release its GR(gid=1) lock. */
654                 MPI_Send(&gid, 1, MPI_INT, 2, 1, MPI_COMM_WORLD);
655
656                 /* wait a really long time. */
657                 sleep(180 * WAIT_TIME);
658
659                 /* PR task will tell us when it completes */
660                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
661
662                 /* Make sure the PR task is successful and doesn't hang.
663                  *
664                  * XXX - To test properly we need to make sure the read
665                  *       gets queued before task2's group lock request.
666                  *       You may need to increase lgbuf_size.
667                  */
668                 iter = MAX_WAIT_TRIES;
669                 do {
670                         iter--;
671                         if (!iter) {
672                                 FAIL("PR task is hung !\n");
673                                 break;
674                         }
675                         sleep(WAIT_TIME);
676                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
677                 } while (!flag1);
678
679                 break;
680         }
681 }
682
683 /*
684  * task0 attempts GR(gid=1) -- granted
685  * task1 attempts PR on non-blocking fd -> should return -EWOULDBLOCK
686  * task2 attempts PW on non-blocking fd -> should return -EWOULDBLOCK
687  * task3 attempts GR(gid=2) on non-blocking fd -> should return -EWOULDBLOCK
688  */
689 void grouplock_nonblock_test(char *filename, int fd)
690 {
691         MPI_Request req1, req2, req3;
692         int iter, flag1, flag2, flag3, temp1, temp2, temp3;
693         int rc, gid = 1;
694
695         if (rank == 0) {
696                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
697                         sprintf(errmsg,
698                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
699                                 filename, errno, strerror(errno));
700                         FAIL(errmsg);
701                 }
702         }
703
704         rc = fcntl(fd, F_SETFL, O_NONBLOCK);
705         if (rc == -1) {
706                 sprintf(errmsg, "fcntl(O_NONBLOCK) failed: (%d) %s.\n",
707                         errno, strerror(errno));
708                 FAIL(errmsg);
709         }
710
711         MPI_Barrier(MPI_COMM_WORLD);
712
713         switch (rank) {
714         case 1:
715                 rc = read(fd, buf, sizeof(buf));
716                 if ((rc != -1) || (errno != EWOULDBLOCK)) {
717                         FAIL("PR lock succeeded while incompatible "
718                              "GROUP LOCK (gid=1) is still held\n");
719                 }
720
721                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
722                 break;
723         case 2:
724                 rc = write(fd, buf, sizeof(buf));
725                 if ((rc != -1) || (errno != EWOULDBLOCK)) {
726                         FAIL("PW lock succeeded while incompatible "
727                              "GROUP LOCK (gid=1) is still held\n");
728                 }
729
730                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
731                 break;
732         case 3:
733                 gid = 2;
734                 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
735                 if ((rc != -1) || (errno != EWOULDBLOCK)) {
736                         FAIL("GROUP_LOCK (gid=2) succeeded while incompatible "
737                              "GROUP LOCK (gid=1) is still held.\n");
738                 }
739
740                 MPI_Send(&gid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
741                 break;
742         case 0:
743                 /* reading task will tell us when it completes */
744                 MPI_Irecv(&temp1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &req1);
745                 /* writing task will tell us when it completes */
746                 MPI_Irecv(&temp2, 1, MPI_INT, 2, 1, MPI_COMM_WORLD, &req2);
747                 /* 2nd locking task will tell us when it completes */
748                 MPI_Irecv(&temp3, 1, MPI_INT, 3, 1, MPI_COMM_WORLD, &req3);
749
750                 iter = MAX_WAIT_TRIES;
751                 do {
752                         iter--;
753                         if (!iter) {
754                                 FAIL("non-blocking tasks are not progressing\n");
755                         }
756                         sleep(WAIT_TIME);
757                         MPI_Test(&req1, &flag1, MPI_STATUS_IGNORE);
758                         MPI_Test(&req2, &flag2, MPI_STATUS_IGNORE);
759                         MPI_Test(&req3, &flag3, MPI_STATUS_IGNORE);
760                 } while (!(flag1 && flag2 && flag3));
761
762                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
763                         sprintf(errmsg, "ioctl GROUP_UNLOCK of file %s",
764                                 filename);
765                         FAIL(errmsg);
766                 }
767                 break;
768         }
769 }
770
771 /* Just test some error paths with invalid requests */
772 void grouplock_errorstest(char *filename, int fd)
773 {
774         int rc, gid = 1;
775
776         MPI_Barrier(MPI_COMM_WORLD);
777
778         switch (rank) {
779         case 0:
780                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
781                         sprintf(errmsg,
782                                 "ioctl GROUP_LOCK of file %s: (%d) %s.\n",
783                                 filename, errno, strerror(errno));
784                         FAIL(errmsg);
785                 }
786
787                 /* second group lock on same fd, same gid */
788                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid)) == -1) {
789                         if (errno != EINVAL) {
790                                 sprintf(errmsg, "Double GROUP lock failed "
791                                         "with errno %d instead of EINVAL\n",
792                                         errno);
793                                 FAIL(errmsg);
794                         }
795                 } else {
796                         FAIL("Taking second GROUP lock on same fd succeed\n");
797                 }
798
799                 /* second group lock on same fd, different gid */
800                 if ((rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid + 1)) == -1) {
801                         if (errno != EINVAL) {
802                                 sprintf(errmsg, "Double GROUP lock with "
803                                         "different gid failed with errno %d "
804                                         "instead of EINVAL\n", errno);
805                                 FAIL(errmsg);
806                         }
807                 } else {
808                         FAIL("Taking second GROUP lock on same fd, with "
809                              "different gid, succeeded.\n");
810                 }
811
812                 /* GROUP unlock with wrong gid */
813                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid + 1)) == -1) {
814                         if (errno != EINVAL) {
815                                 sprintf(errmsg, "GROUP_UNLOCK with wrong gid "
816                                         "failed with errno %d instead of "
817                                         "EINVAL\n", errno);
818                                 FAIL(errmsg);
819                         }
820                 } else {
821                         FAIL("GROUP unlock with wrong gid succeed\n");
822                 }
823
824                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
825                         sprintf(errmsg, "ioctl GROUP_UNLOCK of file %s "
826                                 "returned %d.", filename, rc);
827                         FAIL(errmsg);
828                 }
829                 break;
830
831         case 1:
832                 /* unlock of never locked fd */
833                 if ((rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid)) == -1) {
834                         if (errno != EINVAL) {
835                                 sprintf(errmsg, "GROUP_UNLOCK on never locked "
836                                         "fd failed with errno %d instead of "
837                                         "EINVAL.\n", errno);
838                                 FAIL(errmsg);
839                         }
840                 } else {
841                         FAIL("GROUP unlock on never locked fd succeed\n");
842                 }
843                 break;
844         }
845 }
846
847 void grouplock_file(char *name, int subtest)
848 {
849         int fd;
850         int flags = O_CREAT|O_RDWR|O_SYNC|O_TRUNC;
851         int mode = 0666;
852
853         sprintf(filename, "%s/%s", testdir, name);
854
855         if ((fd = open(filename, flags, mode)) == -1) {
856                 sprintf(errmsg, "open of file %s: (%d) %s.\n",
857                         filename, errno, strerror(errno));
858                 FAIL(errmsg);
859         }
860
861         MPI_Barrier(MPI_COMM_WORLD);
862
863         switch (subtest) {
864         case 1:
865                 grouplock_test1(filename, fd, READ, IOCTL);
866                 break;
867         case 2:
868                 grouplock_test1(filename, fd, READ, CLOSE);
869                 break;
870         case 3:
871                 grouplock_test1(filename, fd, WRITE, IOCTL);
872                 break;
873         case 4:
874                 grouplock_test1(filename, fd, WRITE, CLOSE);
875                 break;
876         case 5:
877                 grouplock_test2(filename, fd, READ, IOCTL);
878                 break;
879         case 6:
880                 grouplock_test2(filename, fd, READ, CLOSE);
881                 break;
882         case 7:
883                 grouplock_test2(filename, fd, WRITE, IOCTL);
884                 break;
885         case 8:
886                 grouplock_test2(filename, fd, WRITE, CLOSE);
887                 break;
888         case 9:
889                 grouplock_nonblock_test(filename, fd);
890                 break;
891         case 10:
892                 grouplock_errorstest(filename, fd);
893                 break;
894         case 11:
895                 grouplock_test3(filename, fd);
896                 break;
897         case 12:
898                 grouplock_test4(filename, fd);
899                 break;
900         default:
901                 sprintf(errmsg, "wrong subtest number %d (should be <= %d)",
902                         subtest, LPGL_TEST_ITEMS);
903                 FAIL(errmsg);
904         }
905
906         close(fd);
907
908         if (rank == 0)
909                 unlink(filename);
910
911         MPI_Barrier(MPI_COMM_WORLD);
912 }
913
914 void parallel_grouplock(void)
915 {
916         char teststr[16];
917         int i;
918
919         if (only_test) {
920                 sprintf(teststr, "subtest %d", only_test);
921                 begin(teststr);
922                 grouplock_file("parallel_grouplock", only_test);
923                 end(teststr);
924         } else {
925                 for (i = 1; i <= LPGL_TEST_ITEMS; i++) {
926                         sprintf(teststr, "subtest %d", i);
927                         begin(teststr);
928                         grouplock_file("parallel_grouplock", i);
929                         end(teststr);
930                 }
931         }
932 }
933
934 void usage(char *proc)
935 {
936         int i;
937
938         if (rank == 0) {
939                 printf("Usage: %s [-h] -d <testdir> [-n <num>]\n", proc);
940                 printf("           [-t <num>] [-v] [-V #] [-g]\n");
941                 printf("\t-h: prints this help message\n");
942                 printf("\t-d: the directory in which the tests will run\n");
943                 printf("\t-n: repeat test # times\n");
944                 printf("\t-t: run a particular test #\n");
945                 printf("\t-v: increase the verbositly level by 1\n");
946                 printf("\t-V: select a specific verbosity level\n");
947                 printf("\t-g: debug mode\n");
948         }
949
950         MPI_Initialized(&i);
951         if (i) MPI_Finalize();
952         exit(0);
953 }
954
955 int main(int argc, char *argv[])
956 {
957         int i, iterations = 1, c;
958
959         setbuf(stdout, 0);
960         setbuf(stderr, 0);
961
962         /* Check for -h parameter before MPI_Init so the binary can be
963            called directly, without, for instance, mpirun */
964         for (i = 1; i < argc; ++i) {
965                 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
966                         usage(argv[0]);
967         }
968
969         MPI_Init(&argc, &argv);
970         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
971         MPI_Comm_size(MPI_COMM_WORLD, &size);
972
973         /* Parse command line options */
974         while (1) {
975                 c = getopt(argc, argv, "d:ghn:t:vV:");
976                 if (c == -1)
977                         break;
978
979                 switch (c) {
980                 case 'd':
981                         testdir = optarg;
982                         break;
983                 case 'g':
984                         debug = 1;
985                         break;
986                 case 'h':
987                         usage(argv[0]);
988                         break;
989                 case 'n':
990                         iterations = atoi(optarg);
991                         break;
992                 case 't':
993                         only_test = atoi(optarg);
994                         break;
995                 case 'v':
996                         verbose += 1;
997                         break;
998                 case 'V':
999                         verbose = atoi(optarg);
1000                         break;
1001                 }
1002         }
1003
1004         if (rank == 0)
1005                 printf("%s is running with %d task(es) %s\n",
1006                        argv[0], size, debug ? "in DEBUG mode" : "\b\b");
1007
1008         if (size < MIN_GLHOST) {
1009                 fprintf(stderr, "Error: "
1010                         "%d tasks run, but should be at least %d tasks to run "
1011                         "the test!\n", size, MIN_GLHOST);
1012                 MPI_Abort(MPI_COMM_WORLD, 2);
1013         }
1014
1015         if (testdir == NULL && rank == 0) {
1016                 fprintf(stderr, "Please specify a test directory! "
1017                         "(\"%s -h\" for help)\n",
1018                        argv[0]);
1019                 MPI_Abort(MPI_COMM_WORLD, 2);
1020         }
1021
1022         lp_gethostname();
1023
1024         for (i = 0; i < iterations; ++i) {
1025                 if (rank == 0)
1026                         printf("%s: Running test #%s(iter %d)\n",
1027                                timestamp(), argv[0], i);
1028
1029                 parallel_grouplock();
1030                 MPI_Barrier(MPI_COMM_WORLD);
1031         }
1032
1033         if (rank == 0) {
1034                 printf("%s: All tests passed!\n", timestamp());
1035         }
1036         MPI_Finalize();
1037         return 0;
1038 }