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