overlay/{069,071}: fix undefined variables
[xfstests-dev.git] / ltp / doio.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 /*
7  * doio -       a general purpose io initiator with system call and
8  *              write logging.  See doio.h for the structure which defines
9  *              what doio requests should look like.
10  *
11  * programming
12  * notes:
13  * -----------
14  *      messages should generally be printed using doio_fprintf().
15  *
16  */
17
18 #include "global.h"
19
20 #include <stdarg.h>
21 #include <sys/uio.h>    /* for struct iovec (readv)*/
22 #include <sys/mman.h>   /* for mmap(2) */
23 #include <sys/ipc.h>    /* for i/o buffer in shared memory */
24 #include <sys/shm.h>    /* for i/o buffer in shared memory */
25 #include <sys/wait.h>
26 #include <sys/time.h>   /* for delays */
27 #include <ctype.h>
28
29 struct io_req;
30 int do_xfsctl(struct io_req *);
31
32 #include "doio.h"
33 #include "pattern.h"
34 #include "write_log.h"
35 #include "random_range.h"
36 #include "string_to_tokens.h"
37
38 #ifndef O_SSD
39 #define O_SSD 0                /* so code compiles on a CRAY2 */
40 #endif
41
42 #define UINT64_T unsigned long long
43
44 #ifndef O_PARALLEL
45 #define O_PARALLEL 0    /* so O_PARALLEL may be used in expressions */
46 #endif
47
48 #define PPID_CHECK_INTERVAL 5           /* check ppid every <-- iterations */
49 #define MAX_AIO         256             /* maximum number of async I/O ops */
50 #define MPP_BUMP        0
51
52
53 #define SYSERR strerror(errno)
54
55 /*
56  * getopt() string of supported cmdline arguments.
57  */
58
59 #define OPTS    "aC:d:ehm:n:kr:w:vU:V:M:N:"
60
61 #define DEF_RELEASE_INTERVAL    0
62
63 /*
64  * Flags set in parse_cmdline() to indicate which options were selected
65  * on the cmdline.
66  */
67
68 int     a_opt = 0;          /* abort on data compare errors     */
69 int     e_opt = 0;          /* exec() after fork()'ing          */
70 int     C_opt = 0;          /* Data Check Type                  */
71 int     d_opt = 0;          /* delay between operations         */
72 int     k_opt = 0;          /* lock file regions during writes  */
73 int     m_opt = 0;          /* generate periodic messages       */
74 int     n_opt = 0;          /* nprocs                           */
75 int     r_opt = 0;          /* resource release interval        */
76 int     w_opt = 0;          /* file write log file              */
77 int     v_opt = 0;          /* verify writes if set             */
78 int     U_opt = 0;          /* upanic() on varios conditions    */
79 int     V_opt = 0;          /* over-ride default validation fd type */
80 int     M_opt = 0;          /* data buffer allocation types     */
81 char    TagName[40];        /* name of this doio (see Monster)  */
82
83
84 /*
85  * Misc globals initialized in parse_cmdline()
86  */
87
88 char    *Prog = NULL;       /* set up in parse_cmdline()                */
89 int     Upanic_Conditions;  /* set by args to -U                        */
90 int     Release_Interval;   /* arg to -r                                */
91 int     Nprocs;             /* arg to -n                                */
92 char    *Write_Log;         /* arg to -w                                */
93 char    *Infile;            /* input file (defaults to stdin)           */
94 int     *Children;          /* pids of child procs                      */
95 int     Nchildren = 0;
96 int     Nsiblings = 0;      /* tfork'ed siblings                        */
97 int     Execd = 0;
98 int     Message_Interval = 0;
99 int     Npes = 0;           /* non-zero if built as an mpp multi-pe app */
100 int     Vpe = -1;           /* Virtual pe number if Npes >= 0           */
101 int     Reqno = 1;          /* request # - used in some error messages  */
102 int     Reqskipcnt = 0;     /* count of I/O requests that are skipped   */
103 int     Validation_Flags;
104 char    *(*Data_Check)();   /* function to call for data checking       */
105 int     (*Data_Fill)();     /* function to call for data filling        */
106 int     Nmemalloc = 0;      /* number of memory allocation strategies   */
107 int     delayop = 0;        /* delay between operations - type of delay */
108 int     delaytime = 0;      /* delay between operations - how long      */
109
110 struct wlog_file        Wlog;
111
112 int     active_mmap_rw = 0; /* Indicates that mmapped I/O is occurring. */
113                             /* Used by sigbus_action() in the child doio. */
114 int     havesigint = 0;
115
116 #define SKIP_REQ        -2      /* skip I/O request */
117
118 #define NMEMALLOC       32
119 #define MEM_DATA        1       /* data space                           */
120 #define MEM_SHMEM       2       /* System V shared memory               */
121 #define MEM_T3ESHMEM    3       /* T3E Shared Memory                    */
122 #define MEM_MMAP        4       /* mmap(2)                              */
123
124 #define MEMF_PRIVATE    0001
125 #define MEMF_AUTORESRV  0002
126 #define MEMF_LOCAL      0004
127 #define MEMF_SHARED     0010
128
129 #define MEMF_FIXADDR    0100
130 #define MEMF_ADDR       0200
131 #define MEMF_AUTOGROW   0400
132 #define MEMF_FILE       01000   /* regular file -- unlink on close      */
133 #define MEMF_MPIN       010000  /* use mpin(2) to lock pages in memory */
134
135 struct memalloc {
136         int     memtype;
137         int     flags;
138         int     nblks;
139         char    *name;
140         void    *space;         /* memory address of allocated space */
141         int     fd;             /* FD open for mmaping */
142         int     size;
143 }       Memalloc[NMEMALLOC];
144
145 /*
146  * Global file descriptors
147  */
148
149 int     Wfd_Append;         /* for appending to the write-log       */
150 int     Wfd_Random;         /* for overlaying write-log entries     */
151
152 /*
153  * Structure for maintaining open file test descriptors.  Used by
154  * alloc_fd().
155  */
156
157 struct fd_cache {
158         char    c_file[MAX_FNAME_LENGTH+1];
159         int     c_oflags;
160         int     c_fd;
161         long    c_rtc;
162         int     c_memalign;     /* from xfsctl(XFS_IOC_DIOINFO) */
163         int     c_miniosz;
164         int     c_maxiosz;
165         void    *c_memaddr;     /* mmapped address */
166         int     c_memlen;       /* length of above region */
167 };
168
169 #define FD_ALLOC_INCR   32      /* allocate this many fd_map structs    */
170                                 /* at a time */
171
172 /*
173  * Globals for tracking Sds and Core usage
174  */
175
176 char    *Memptr;                /* ptr to core buffer space             */
177 int     Memsize;                /* # bytes pointed to by Memptr         */
178                                 /* maintained by alloc_mem()            */
179
180 int     Sdsptr;                 /* sds offset (always 0)                */
181 int     Sdssize;                /* # bytes of allocated sds space       */
182                                 /* Maintained by alloc_sds()            */
183 char    Host[16];
184 char    Pattern[128];
185 int     Pattern_Length;
186
187 /*
188  * Signal handlers, and related globals
189  */
190
191 void    sigint_handler();       /* Catch SIGINT in parent doio, propagate
192                                  * to children, does not die. */
193
194 void    die_handler();          /* Bad sig in child doios, exit 1. */
195 void    cleanup_handler();      /* Normal kill, exit 0. */
196
197 void    sigbus_handler();       /* Handle sigbus--check active_mmap_rw to
198                                    decide if this should be a normal exit. */
199
200 void    cb_handler();           /* Posix aio callback handler. */
201 void    noop_handler();         /* Delayop alarm, does nothing. */
202 char    *hms(time_t  t);
203 char    *format_rw();
204 char    *format_sds();
205 char    *format_listio();
206 char    *check_file(char *file, int offset, int length, char *pattern,
207                     int pattern_length, int patshift, int fsa);
208 int     doio_fprintf(FILE *stream, char *format, ...);
209 void    doio_upanic(int mask);
210 void    doio();
211 void    help(FILE *stream);
212 void    doio_delay();
213 int     alloc_fd( char *, int );
214 int     alloc_mem( int );
215 int     do_read( struct io_req * );
216 int     do_write( struct io_req * );
217 int     do_rw( struct io_req * );
218 int     do_sync( struct io_req * );
219 int     usage( FILE * );
220 int     aio_unregister( int );
221 int     parse_cmdline( int, char **, char * );
222 int     lock_file_region( char *, int, int, int, int );
223 struct  fd_cache *alloc_fdcache(char *, int);
224 int     aio_register( int, int, int );
225 #ifndef linux
226 int aio_wait(int);
227 #endif
228
229 /*
230  * Upanic conditions, and a map from symbolics to values
231  */
232
233 #define U_CORRUPTION    0001        /* upanic on data corruption    */
234 #define U_IOSW          0002        /* upanic on bad iosw           */
235 #define U_RVAL          0004        /* upanic on bad rval           */
236
237 #define U_ALL           (U_CORRUPTION | U_IOSW | U_RVAL)
238
239 /*
240  * Name-To-Value map
241  * Used to map cmdline arguments to values
242  */
243 struct smap {
244         char    *string;
245         int     value;
246 };
247
248 struct smap Upanic_Args[] = {
249         { "corruption", U_CORRUPTION    },
250         { "iosw",       U_IOSW          },
251         { "rval",       U_RVAL          },
252         { "all",        U_ALL           },
253         { NULL,         0               }
254 };
255
256 struct aio_info {
257         int                     busy;
258         int                     id;
259         int                     fd;
260         int                     strategy;
261         volatile int            done;
262         int                     sig;
263         int                     signalled;
264         struct sigaction        osa;
265 };
266
267 struct aio_info Aio_Info[MAX_AIO];
268
269 struct aio_info *aio_slot();
270 int     aio_done( struct aio_info * );
271
272 /* -C data-fill/check type */
273 #define C_DEFAULT       1
274 struct smap checkmap[] = {
275         { "default",    C_DEFAULT },
276         { NULL,         0 },
277 };
278
279 /* -d option delay types */
280 #define DELAY_SELECT    1
281 #define DELAY_SLEEP     2
282 #define DELAY_SGINAP    3
283 #define DELAY_ALARM     4
284 #define DELAY_ITIMER    5       /* POSIX timer                          */
285
286 struct smap delaymap[] = {
287         { "select",     DELAY_SELECT },
288         { "sleep",      DELAY_SLEEP },
289         { "alarm",      DELAY_ALARM },
290         { NULL, 0 },
291 };
292
293 /******
294 *
295 * strerror() does similar actions.
296
297 char *
298 syserrno(int err)
299 {
300     static char sys_errno[10];
301     sprintf(sys_errno, "%d", errno);
302     return(sys_errno);
303 }
304
305 ******/
306
307 int
308 main(argc, argv)
309 int     argc;
310 char    **argv;
311 {
312         int                     i, pid, stat, ex_stat;
313         struct sigaction        sa;
314         sigset_t                block_mask, old_mask;
315         umask(0);               /* force new file modes to known values */
316
317         TagName[0] = '\0';
318         parse_cmdline(argc, argv, OPTS);
319
320         random_range_seed(getpid());       /* initialize random number generator */
321
322         /*      
323          * If this is a re-exec of doio, jump directly into the doio function.
324          */
325
326         if (Execd) {
327                 doio();
328                 exit(E_SETUP);
329         }
330
331         /*
332          * Stop on all but a few signals...
333          */
334         sigemptyset(&sa.sa_mask);
335         sa.sa_handler = sigint_handler;
336         sa.sa_flags = SA_RESETHAND;     /* sigint is ignored after the */
337                                         /* first time */
338         for (i = 1; i <= NSIG; i++) {
339                 switch(i) {
340 #ifdef SIGRECOVERY
341                 case SIGRECOVERY:
342                         break;
343 #endif
344 #ifdef SIGCKPT
345                 case SIGCKPT:
346 #endif
347 #ifdef SIGRESTART
348                 case SIGRESTART:
349 #endif
350                 case SIGTSTP:
351                 case SIGSTOP:
352                 case SIGCONT:
353                 case SIGCHLD:
354                 case SIGBUS:
355                 case SIGSEGV:
356                 case SIGQUIT:
357                         break;
358                 default:
359                         sigaction(i, &sa, NULL);
360                 }
361         }
362
363         /*
364          * If we're logging write operations, make a dummy call to wlog_open
365          * to initialize the write history file.  This call must be done in
366          * the parent, to ensure that the history file exists and/or has
367          * been truncated before any children attempt to open it, as the doio
368          * children are not allowed to truncate the file.
369          */
370
371         if (w_opt) {
372                 strcpy(Wlog.w_file, Write_Log);
373
374                 if (wlog_open(&Wlog, 1, 0666) < 0) {
375                         doio_fprintf(stderr,
376                                      "Could not create/truncate write log %s\n",
377                                      Write_Log);
378                         exit(2);
379                 }
380
381                 wlog_close(&Wlog);
382         }
383
384         /*
385          * Malloc space for the children pid array.  Initialize all entries
386          * to -1.
387          */
388
389         Children = (int *)malloc(sizeof(int) * Nprocs);
390         for (i = 0; i < Nprocs; i++) {
391                 Children[i] = -1;
392         }
393
394         sigemptyset(&block_mask);
395         sigaddset(&block_mask, SIGCHLD);
396         sigprocmask(SIG_BLOCK, &block_mask, &old_mask);
397
398         /*
399          * Fork Nprocs.  This [parent] process is a watchdog, to notify the
400          * invoker of procs which exit abnormally, and to make sure that all
401          * child procs get cleaned up.  If the -e option was used, we will also
402          * re-exec.  This is mostly for unicos/mk on mpp's, to ensure that not
403          * all of the doio's don't end up in the same pe.
404          *
405          * Note - if Nprocs is 1, or this doio is a multi-pe app (Npes > 1),
406          * jump directly to doio().  multi-pe apps can't fork(), and there is
407          * no reason to fork() for 1 proc.
408          */
409
410         if (Nprocs == 1 || Npes > 1) {
411                 doio();
412                 exit(0);
413         } else {
414                 for (i = 0; i < Nprocs; i++) {
415                         if ((pid = fork()) == -1) {
416                                 doio_fprintf(stderr,
417                                              "(parent) Could not fork %d children:  %s (%d)\n",
418                                              i+1, SYSERR, errno);
419                                 exit(E_SETUP);
420                         }
421                         
422                         Children[Nchildren] = pid;
423                         Nchildren++;
424                         
425                         if (pid == 0) {
426                                 if (e_opt) {
427                                         char *exec_path;
428
429                                         exec_path = argv[0];
430                                         argv[0] = (char *)malloc(strlen(exec_path + 1));
431                                         sprintf(argv[0], "-%s", exec_path);
432
433                                         execvp(exec_path, argv);
434                                         doio_fprintf(stderr,
435                                                      "(parent) Could not execvp %s:  %s (%d)\n",
436                                                      exec_path, SYSERR, errno);
437                                         exit(E_SETUP);
438                                 } else {
439                                         doio();
440                                         exit(E_SETUP);
441                                 }
442                         }
443                 }
444
445                 /*
446                  * Parent spins on wait(), until all children exit.
447                  */
448                 
449                 ex_stat = E_NORMAL;
450                 
451                 while (Nprocs) {
452                         if ((pid = wait(&stat)) == -1) {
453                                 if (errno == EINTR)
454                                         continue;
455                         }
456                         
457                         for (i = 0; i < Nchildren; i++)
458                                 if (Children[i] == pid)
459                                         Children[i] = -1;
460                         
461                         Nprocs--;
462                         
463                         if (WIFEXITED(stat)) {
464                                 switch (WEXITSTATUS(stat)) {
465                                 case E_NORMAL:
466                                         /* noop */
467                                         break;
468
469                                 case E_INTERNAL:
470                                         doio_fprintf(stderr,
471                                                      "(parent) pid %d exited because of an internal error\n",
472                                                      pid);
473                                         ex_stat |= E_INTERNAL;
474                                         break;
475
476                                 case E_SETUP:
477                                         doio_fprintf(stderr,
478                                                      "(parent) pid %d exited because of a setup error\n",
479                                                      pid);
480                                         ex_stat |= E_SETUP;
481                                         break;
482
483                                 case E_COMPARE:
484                                         doio_fprintf(stderr,
485                                                      "(parent) pid %d exited because of data compare errors\n",
486                                                      pid);
487
488                                         ex_stat |= E_COMPARE;
489
490                                         if (a_opt)
491                                                 kill(0, SIGINT);
492
493                                         break;
494
495                                 case E_USAGE:
496                                         doio_fprintf(stderr,
497                                                      "(parent) pid %d exited because of a usage error\n",
498                                                      pid);
499
500                                         ex_stat |= E_USAGE;
501                                         break;
502
503                                 default:
504                                         doio_fprintf(stderr,
505                                                      "(parent) pid %d exited with unknown status %d\n",
506                                                      pid, WEXITSTATUS(stat));
507                                         ex_stat |= E_INTERNAL;
508                                         break;
509                                 }
510                         } else if (WIFSIGNALED(stat) && WTERMSIG(stat) != SIGINT) {
511                                 doio_fprintf(stderr,
512                                              "(parent) pid %d terminated by signal %d\n",
513                                              pid, WTERMSIG(stat));
514                                 
515                                 ex_stat |= E_SIGNAL;
516                         }
517                         
518                         fflush(NULL);
519                 }
520         }
521
522         exit(ex_stat);
523
524 }  /* main */
525
526 /*
527  * main doio function.  Each doio child starts here, and never returns.
528  */
529
530 void
531 doio()
532 {
533         int                     rval, i, infd, nbytes;
534         char                    *cp;
535         struct io_req           ioreq;
536         struct sigaction        sa, def_action, ignore_action, exit_action;
537         struct sigaction        sigbus_action;
538
539         Memsize = Sdssize = 0;
540
541         /*
542          * Initialize the Pattern - write-type syscalls will replace Pattern[1]
543          * with the pattern passed in the request.  Make sure that
544          * strlen(Pattern) is not mod 16 so that out of order words will be
545          * detected.
546          */
547
548         gethostname(Host, sizeof(Host));
549         if ((cp = strchr(Host, '.')) != NULL)
550                 *cp = '\0';
551
552         Pattern_Length = sprintf(Pattern, "-:%d:%s:%s*", (int)getpid(), Host, Prog);
553
554         if (!(Pattern_Length % 16)) {
555                 Pattern_Length = sprintf(Pattern, "-:%d:%s:%s**",
556                                          (int)getpid(), Host, Prog);
557         }
558
559         /*
560          * Open a couple of descriptors for the write-log file.  One descriptor
561          * is for appending, one for random access.  Write logging is done for
562          * file corruption detection.  The program doio_check is capable of
563          * doing corruption detection based on a doio write-log.
564          */
565
566         if (w_opt) {
567
568                 strcpy(Wlog.w_file, Write_Log);
569         
570                 if (wlog_open(&Wlog, 0, 0666) == -1) {
571                         doio_fprintf(stderr,
572                                      "Could not open write log file (%s): wlog_open() failed\n",
573                                      Write_Log);
574                         exit(E_SETUP);
575                 }
576         }
577
578         /*
579          * Open the input stream - either a file or stdin
580          */
581
582         if (Infile == NULL) {
583                 infd = 0;
584         } else {
585                 if ((infd = open(Infile, O_RDWR)) == -1) {
586                         doio_fprintf(stderr,
587                                      "Could not open input file (%s):  %s (%d)\n",
588                                      Infile, SYSERR, errno);
589                         exit(E_SETUP);
590                 }
591         }
592
593         /*
594          * Define a set of signals that should never be masked.  Receipt of
595          * these signals generally indicates a programming error, and we want
596          * a corefile at the point of error.  We put SIGQUIT in this list so
597          * that ^\ will force a user core dump.
598          *
599          * Note:  the handler for these should be SIG_DFL, all of them 
600          * produce a corefile as the default action.
601          */
602
603         ignore_action.sa_handler = SIG_IGN;
604         ignore_action.sa_flags = 0;
605         sigemptyset(&ignore_action.sa_mask);
606
607         def_action.sa_handler = SIG_DFL;
608         def_action.sa_flags = 0;
609         sigemptyset(&def_action.sa_mask);
610
611         exit_action.sa_handler = cleanup_handler;
612         exit_action.sa_flags = 0;
613         sigemptyset(&exit_action.sa_mask);
614
615         sa.sa_handler = die_handler;
616         sa.sa_flags = 0;
617         sigemptyset(&sa.sa_mask);
618
619         sigbus_action.sa_handler = sigbus_handler;
620         sigbus_action.sa_flags = 0;
621         sigemptyset(&sigbus_action.sa_mask);
622
623         for (i = 1; i <= NSIG; i++) {
624                 switch(i) {
625                         /* Signals to terminate program on */
626                 case SIGINT:
627                         sigaction(i, &exit_action, NULL);
628                         break;
629
630                         /* This depends on active_mmap_rw */
631                 case SIGBUS:
632                         sigaction(i, &sigbus_action, NULL);
633                         break;
634
635                     /* Signals to Ignore... */
636                 case SIGSTOP:
637                 case SIGCONT:
638 #ifdef SIGRECOVERY
639                 case SIGRECOVERY:
640 #endif
641                         sigaction(i, &ignore_action, NULL);
642                         break;
643
644                     /* Signals to trap & report & die */
645                 /*case SIGTRAP:*/
646                 /*case SIGABRT:*/
647 #ifdef SIGERR   /* cray only signals */
648                 case SIGERR:
649                 case SIGBUFIO:
650                 case SIGINFO:
651 #endif
652                 /*case SIGFPE:*/
653                 case SIGURG:
654                 case SIGHUP:
655                 case SIGTERM:
656                 case SIGPIPE:
657                 case SIGIO:
658                 case SIGUSR1:
659                 case SIGUSR2:
660                         sigaction(i, &sa, NULL);
661                         break;
662
663
664                     /* Default Action for all other signals */
665                 default:
666                         sigaction(i, &def_action, NULL);
667                         break;
668                 }
669         }
670
671         /*
672          * Main loop - each doio proc does this until the read returns eof (0).
673          * Call the appropriate io function based on the request type.
674          */
675
676         while ((nbytes = read(infd, (char *)&ioreq, sizeof(ioreq)))) {
677
678                 /*
679                  * Periodically check our ppid.  If it is 1, the child exits to
680                  * help clean up in the case that the main doio process was
681                  * killed.
682                  */
683
684                 if (Reqno && ((Reqno % PPID_CHECK_INTERVAL) == 0)) {
685                         if (getppid() == 1) {
686                                 doio_fprintf(stderr,
687                                              "Parent doio process has exited\n");
688                                 alloc_mem(-1);
689                                 exit(E_SETUP);
690                         }
691                 }
692
693                 if (nbytes == -1) {
694                         doio_fprintf(stderr,
695                                      "read of %d bytes from input failed:  %s (%d)\n",
696                                      sizeof(ioreq), SYSERR, errno);
697                         alloc_mem(-1);
698                         exit(E_SETUP);
699                 }
700
701                 if (nbytes != sizeof(ioreq)) {
702                         doio_fprintf(stderr,
703                                      "read wrong # bytes from input stream, expected %d, got %d\n",
704                                      sizeof(ioreq), nbytes);
705                         alloc_mem(-1);
706                         exit(E_SETUP);
707                 }
708
709                 if (ioreq.r_magic != DOIO_MAGIC) {
710                         doio_fprintf(stderr,
711                                      "got a bad magic # from input stream.  Expected 0%o, got 0%o\n",
712                                      DOIO_MAGIC, ioreq.r_magic);
713                         alloc_mem(-1);
714                         exit(E_SETUP);
715                 }
716
717                 /*
718                  * If we're on a Release_Interval multiple, relase all ssd and
719                  * core space, and close all fd's in Fd_Map[].
720                  */
721
722                 if (Reqno && Release_Interval && ! (Reqno%Release_Interval)) {
723                         if (Memsize) {
724 #ifdef NOTDEF
725                                 sbrk(-1 * Memsize);
726 #else
727                                 alloc_mem(-1);
728 #endif
729                         }
730                         alloc_fd(NULL, 0);
731                 }
732
733                 switch (ioreq.r_type) {
734                 case READ:
735                 case READA:
736                         rval = do_read(&ioreq);
737                         break;
738
739                 case WRITE:
740                 case WRITEA:
741                         rval = do_write(&ioreq);
742                         break;
743
744                 case READV:
745                 case AREAD:
746                 case PREAD:
747                 case LREAD:
748                 case LREADA:
749                 case LSREAD:
750                 case LSREADA:
751                 case WRITEV:
752                 case AWRITE:
753                 case PWRITE:
754                 case MMAPR:
755                 case MMAPW:
756                 case LWRITE:
757                 case LWRITEA:
758                 case LSWRITE:
759                 case LSWRITEA:
760                 case LEREAD:
761                 case LEREADA:
762                 case LEWRITE:
763                 case LEWRITEA:
764                         rval = do_rw(&ioreq);
765                         break;
766                 case RESVSP:
767                 case UNRESVSP:
768                         rval = do_xfsctl(&ioreq);
769                         break;
770                 case FSYNC2:
771                 case FDATASYNC:
772                         rval = do_sync(&ioreq);
773                         break;
774                 default:
775                         doio_fprintf(stderr,
776                                      "Don't know how to handle io request type %d\n",
777                                      ioreq.r_type);
778                         alloc_mem(-1);
779                         exit(E_SETUP);
780                 }
781
782                 if (rval == SKIP_REQ){
783                         Reqskipcnt++;
784                 }
785                 else if (rval != 0) {
786                         alloc_mem(-1);
787                         doio_fprintf(stderr,
788                                      "doio(): operation %d returned != 0\n",
789                                      ioreq.r_type);
790                         exit(E_SETUP);
791                 }
792
793                 if (Message_Interval && Reqno % Message_Interval == 0) {
794                         doio_fprintf(stderr, "Info:  %d requests done (%d skipped) by this process\n", Reqno, Reqskipcnt);
795                 }
796
797                 Reqno++;
798
799                 if(delayop != 0)
800                         doio_delay();
801         }
802
803         /*
804          * Child exits normally
805          */
806         alloc_mem(-1);
807         exit(E_NORMAL);
808
809 }  /* doio */
810
811 void
812 doio_delay()
813 {
814         struct timeval tv_delay;
815         struct sigaction sa_al, sa_old;
816         sigset_t al_mask;
817
818         switch(delayop) {
819         case DELAY_SELECT:
820                 tv_delay.tv_sec = delaytime / 1000000;
821                 tv_delay.tv_usec = delaytime % 1000000;
822                 /*doio_fprintf(stdout, "delay_select: %d %d\n", 
823                             tv_delay.tv_sec, tv_delay.tv_usec);*/
824                 select(0, NULL, NULL, NULL, &tv_delay);
825                 break;
826
827         case DELAY_SLEEP:
828                 sleep(delaytime);
829                 break;
830
831         case DELAY_ALARM:
832                 sa_al.sa_flags = 0;
833                 sa_al.sa_handler = noop_handler;
834                 sigemptyset(&sa_al.sa_mask);
835                 sigaction(SIGALRM, &sa_al, &sa_old);
836                 sigemptyset(&al_mask);
837                 alarm(delaytime);
838                 sigsuspend(&al_mask);
839                 sigaction(SIGALRM, &sa_old, 0);
840                 break;
841         }
842 }
843
844
845 /*
846  * Format IO requests, returning a pointer to the formatted text.
847  *
848  * format_strat - formats the async i/o completion strategy
849  * format_rw    - formats a read[a]/write[a] request
850  * format_sds   - formats a ssread/sswrite request
851  * format_listio- formats a listio request
852  *
853  * ioreq is the doio io request structure.
854  */
855
856 struct smap sysnames[] = {
857         { "READ",       READ            },
858         { "WRITE",      WRITE           },
859         { "READA",      READA           },
860         { "WRITEA",     WRITEA          },
861         { "SSREAD",     SSREAD          },
862         { "SSWRITE",    SSWRITE         },
863         { "LISTIO",     LISTIO          },
864         { "LREAD",      LREAD           },
865         { "LREADA",     LREADA          },
866         { "LWRITE",     LWRITE          },
867         { "LWRITEA",    LWRITEA         },
868         { "LSREAD",     LSREAD          },
869         { "LSREADA",    LSREADA         },
870         { "LSWRITE",    LSWRITE         },
871         { "LSWRITEA",   LSWRITEA        },
872
873         /* Irix System Calls */
874         { "PREAD",      PREAD           },
875         { "PWRITE",     PWRITE          },
876         { "AREAD",      AREAD           },
877         { "AWRITE",     AWRITE          },
878         { "LLREAD",     LLREAD          },
879         { "LLAREAD",    LLAREAD         },
880         { "LLWRITE",    LLWRITE         },
881         { "LLAWRITE",   LLAWRITE        },
882         { "RESVSP",     RESVSP          },
883         { "UNRESVSP",   UNRESVSP        },
884
885         /* Irix and Linux System Calls */
886         { "READV",      READV           },
887         { "WRITEV",     WRITEV          },
888         { "MMAPR",      MMAPR           },
889         { "MMAPW",      MMAPW           },
890         { "FSYNC2",     FSYNC2          },
891         { "FDATASYNC",  FDATASYNC       },
892
893         { "unknown",    -1              },
894 };      
895
896 struct smap aionames[] = {
897         { "poll",       A_POLL          },
898         { "signal",     A_SIGNAL        },
899         { "recall",     A_RECALL        },
900         { "recalla",    A_RECALLA       },
901         { "recalls",    A_RECALLS       },
902         { "suspend",    A_SUSPEND       },
903         { "callback",   A_CALLBACK      },
904         { "synch",      0               },
905         { "unknown",    -1              },
906 };
907
908 char *
909 format_oflags(int oflags)
910 {
911         char flags[255];
912
913
914         flags[0]='\0';
915         switch(oflags & 03) {
916         case O_RDONLY:          strcat(flags,"O_RDONLY,");      break;
917         case O_WRONLY:          strcat(flags,"O_WRONLY,");      break;
918         case O_RDWR:            strcat(flags,"O_RDWR,");        break;
919         default:                strcat(flags,"O_weird");        break;
920         }
921
922         if(oflags & O_EXCL)
923                 strcat(flags,"O_EXCL,");
924
925         if(oflags & O_SYNC)
926                 strcat(flags,"O_SYNC,");
927
928         if(oflags & O_DIRECT)
929                 strcat(flags,"O_DIRECT,");
930
931         return(strdup(flags));
932 }
933
934 char *
935 format_strat(int strategy)
936 {
937         char msg[64];
938         char *aio_strat;
939
940         switch (strategy) {
941         case A_POLL:            aio_strat = "POLL";     break;
942         case A_SIGNAL:          aio_strat = "SIGNAL";   break;
943         case A_RECALL:          aio_strat = "RECALL";   break;
944         case A_RECALLA:         aio_strat = "RECALLA";  break;
945         case A_RECALLS:         aio_strat = "RECALLS";  break;
946         case A_SUSPEND:         aio_strat = "SUSPEND";  break;
947         case A_CALLBACK:        aio_strat = "CALLBACK"; break;
948         case 0:                 aio_strat = "<zero>";   break;
949         default:
950                 sprintf(msg, "<error:%#o>", strategy);
951                 aio_strat = strdup(msg);
952                 break;
953         }
954
955         return(aio_strat);
956 }
957
958 char *
959 format_rw(
960         struct  io_req  *ioreq,
961         int             fd,
962         void            *buffer,
963         int             signo,
964         char            *pattern,
965         void            *iosw
966         )
967 {
968         static char             *errbuf=NULL;
969         char                    *aio_strat, *cp;
970         struct read_req         *readp = &ioreq->r_data.read;
971         struct write_req        *writep = &ioreq->r_data.write;
972         struct read_req         *readap = &ioreq->r_data.read;
973         struct write_req        *writeap = &ioreq->r_data.write;
974
975         if(errbuf == NULL)
976                 errbuf = (char *)malloc(32768);
977
978         cp = errbuf;
979         cp += sprintf(cp, "Request number %d\n", Reqno);
980
981         switch (ioreq->r_type) {
982         case READ:
983                 cp += sprintf(cp, "syscall:  read(%d, %#lo, %d)\n",
984                               fd, (unsigned long) buffer, readp->r_nbytes);
985                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
986                               fd, readp->r_file, readp->r_oflags);
987                 cp += sprintf(cp, "          read done at file offset %d\n",
988                               readp->r_offset);
989                 break;
990
991         case WRITE:
992                 cp += sprintf(cp, "syscall:  write(%d, %#lo, %d)\n",
993                               fd, (unsigned long) buffer, writep->r_nbytes);
994                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
995                               fd, writep->r_file, writep->r_oflags);
996                 cp += sprintf(cp, "          write done at file offset %d - pattern is %s\n",
997                               writep->r_offset, pattern);
998                 break;
999
1000         case READA:
1001                 aio_strat = format_strat(readap->r_aio_strat);
1002
1003                 cp += sprintf(cp, "syscall:  reada(%d, %#lo, %d, %#lo, %d)\n",
1004                               fd, (unsigned long) buffer, readap->r_nbytes,
1005                               (unsigned long) iosw, signo);
1006                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
1007                               fd, readap->r_file, readp->r_oflags);
1008                 cp += sprintf(cp, "          reada done at file offset %d\n",
1009                               readap->r_offset);
1010                 cp += sprintf(cp, "          async io completion strategy is %s\n",
1011                               aio_strat);
1012                 break;
1013
1014         case WRITEA:
1015                 aio_strat = format_strat(writeap->r_aio_strat);
1016
1017                 cp += sprintf(cp, "syscall:  writea(%d, %#lo, %d, %#lo, %d)\n",
1018                               fd, (unsigned long) buffer, writeap->r_nbytes,
1019                               (unsigned long) iosw, signo);
1020                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
1021                               fd, writeap->r_file, writeap->r_oflags);
1022                 cp += sprintf(cp, "          writea done at file offset %d - pattern is %s\n",
1023                               writeap->r_offset, pattern);
1024                 cp += sprintf(cp, "          async io completion strategy is %s\n",
1025                               aio_strat);
1026                 break;
1027
1028         }
1029
1030         return errbuf;
1031 }
1032
1033 /*
1034  * Perform the various sorts of disk reads
1035  */
1036
1037 int
1038 do_read(req)
1039 struct io_req   *req;
1040 {
1041         int                     fd, offset, nbytes, oflags, rval;
1042         char                    *addr, *file;
1043         struct fd_cache         *fdc;
1044
1045         /*
1046          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
1047          * r_nbytes are at the same offset in the read_req and reada_req
1048          * structures.
1049          */
1050
1051         file = req->r_data.read.r_file;
1052         oflags = req->r_data.read.r_oflags;
1053         offset = req->r_data.read.r_offset;
1054         nbytes = req->r_data.read.r_nbytes;
1055
1056         /*printf("read: %s, %#o, %d %d\n", file, oflags, offset, nbytes);*/
1057
1058         /*
1059          * Grab an open file descriptor
1060          * Note: must be done before memory allocation so that the direct i/o
1061          *      information is available in mem. allocate
1062          */
1063
1064         if ((fd = alloc_fd(file, oflags)) == -1)
1065                 return -1;
1066
1067         /*
1068          * Allocate core or sds - based on the O_SSD flag
1069          */
1070
1071 #ifndef wtob
1072 #define wtob(x) (x * sizeof(UINT64_T))
1073 #endif
1074
1075         /* get memory alignment for using DIRECT I/O */
1076         fdc = alloc_fdcache(file, oflags);
1077
1078         if ((rval = alloc_mem(nbytes + wtob(1) * 2 + fdc->c_memalign)) < 0) {
1079                 return rval;
1080         }
1081
1082         addr = Memptr;
1083
1084
1085         if( (req->r_data.read.r_uflags & F_WORD_ALIGNED) ) {
1086                 /*
1087                  * Force memory alignment for Direct I/O
1088                  */
1089                 if( (oflags & O_DIRECT) && ((long)addr % fdc->c_memalign != 0) ) {
1090                         addr += fdc->c_memalign - ((long)addr % fdc->c_memalign);
1091                 }
1092         } else {
1093                 addr += random_range(0, wtob(1) - 1, 1, NULL);
1094         }
1095
1096         switch (req->r_type) {
1097         case READ:
1098                 /* move to the desired file position. */
1099                 if (lseek(fd, offset, SEEK_SET) == -1) {
1100                         doio_fprintf(stderr,
1101                                      "lseek(%d, %d, SEEK_SET) failed:  %s (%d)\n",
1102                                      fd, offset, SYSERR, errno);
1103                         return -1;
1104                 }
1105
1106                 if ((rval = read(fd, addr, nbytes)) == -1) {
1107                         doio_fprintf(stderr,
1108                                      "read() request failed:  %s (%d)\n%s\n",
1109                                      SYSERR, errno,
1110                                      format_rw(req, fd, addr, -1, NULL, NULL));
1111                         doio_upanic(U_RVAL);
1112                         return -1;
1113                 } else if (rval != nbytes) {
1114                         doio_fprintf(stderr,
1115                                      "read() request returned wrong # of bytes - expected %d, got %d\n%s\n",
1116                                      nbytes, rval, 
1117                                      format_rw(req, fd, addr, -1, NULL, NULL));
1118                         doio_upanic(U_RVAL);
1119                         return -1;
1120                 }
1121                 break;
1122         }
1123
1124         return 0;               /* if we get here, everything went ok */
1125 }
1126
1127 /*
1128  * Perform the verious types of disk writes.
1129  */
1130
1131 int
1132 do_write(req)
1133 struct io_req   *req;
1134 {
1135         static int              pid = -1;
1136         int                     fd, nbytes, oflags;
1137         /* REFERENCED */
1138         int                     logged_write, rval, got_lock;
1139         long                    offset, woffset = 0;
1140         char                    *addr, pattern, *file, *msg;
1141         struct wlog_rec         wrec;
1142         struct fd_cache         *fdc;
1143
1144         /*
1145          * Misc variable setup
1146          */
1147
1148         nbytes  = req->r_data.write.r_nbytes;
1149         offset  = req->r_data.write.r_offset;
1150         pattern = req->r_data.write.r_pattern;
1151         file    = req->r_data.write.r_file;
1152         oflags  = req->r_data.write.r_oflags;
1153
1154         /*printf("pwrite: %s, %#o, %d %d\n", file, oflags, offset, nbytes);*/
1155
1156         /*
1157          * Allocate core memory and possibly sds space.  Initialize the data
1158          * to be written.
1159          */
1160
1161         Pattern[0] = pattern;
1162
1163
1164         /*
1165          * Get a descriptor to do the io on
1166          */
1167
1168         if ((fd = alloc_fd(file, oflags)) == -1)
1169                 return -1;
1170
1171         /*printf("write: %d, %s, %#o, %d %d\n",
1172                fd, file, oflags, offset, nbytes);*/
1173
1174         /*
1175          * Allocate SDS space for backdoor write if desired
1176          */
1177
1178         /* get memory alignment for using DIRECT I/O */
1179         fdc = alloc_fdcache(file, oflags);
1180
1181         if ((rval = alloc_mem(nbytes + wtob(1) * 2 + fdc->c_memalign)) < 0) {
1182                 return rval;
1183         }
1184
1185         addr = Memptr;
1186
1187         if( (req->r_data.write.r_uflags & F_WORD_ALIGNED) ) {
1188                 /*
1189                  * Force memory alignment for Direct I/O
1190                  */
1191                 if( (oflags & O_DIRECT) && ((long)addr % fdc->c_memalign != 0) ) {
1192                         addr += fdc->c_memalign - ((long)addr % fdc->c_memalign);
1193                 }
1194         } else {
1195                 addr += random_range(0, wtob(1) - 1, 1, NULL);
1196         }
1197
1198         (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
1199         if( addr != Memptr )
1200                 memmove( addr, Memptr, nbytes);
1201
1202         rval = -1;
1203         got_lock = 0;
1204         logged_write = 0;
1205
1206         if (k_opt) {
1207                 if (lock_file_region(file, fd, F_WRLCK, offset, nbytes) < 0) {
1208                         alloc_mem(-1);
1209                         exit(E_INTERNAL);
1210                 }
1211
1212                 got_lock = 1;
1213         }
1214
1215         /*
1216          * Write a preliminary write-log entry.  This is done so that
1217          * doio_check can do corruption detection across an interrupt/crash.
1218          * Note that w_done is set to 0.  If doio_check sees this, it
1219          * re-creates the file extents as if the write completed, but does not
1220          * do any checking - see comments in doio_check for more details.
1221          */
1222
1223         if (w_opt) {
1224                 if (pid == -1) {
1225                         pid = getpid();
1226                 }
1227                 wrec.w_async = (req->r_type == WRITEA) ? 1 : 0;
1228                 wrec.w_oflags = oflags;
1229                 wrec.w_pid = pid;
1230                 wrec.w_offset = offset;
1231                 wrec.w_nbytes = nbytes;
1232
1233                 wrec.w_pathlen = strlen(file);
1234                 memcpy(wrec.w_path, file, wrec.w_pathlen);
1235                 wrec.w_hostlen = strlen(Host);
1236                 memcpy(wrec.w_host, Host, wrec.w_hostlen);
1237                 wrec.w_patternlen = Pattern_Length;
1238                 memcpy(wrec.w_pattern, Pattern, wrec.w_patternlen);
1239
1240                 wrec.w_done = 0;
1241
1242                 if ((woffset = wlog_record_write(&Wlog, &wrec, -1)) == -1) {
1243                         doio_fprintf(stderr,
1244                                      "Could not append to write-log:  %s (%d)\n",
1245                                      SYSERR, errno);
1246                 } else {
1247                         logged_write = 1;
1248                 }
1249         }
1250
1251         switch (req->r_type ) {
1252         case WRITE:
1253                 /*
1254                  * sync write
1255                  */
1256
1257                 if (lseek(fd, offset, SEEK_SET) == -1) {
1258                         doio_fprintf(stderr,
1259                                      "lseek(%d, %d, SEEK_SET) failed:  %s (%d)\n",
1260                                      fd, offset, SYSERR, errno);
1261                         return -1;
1262                 }
1263
1264                 rval = write(fd, addr, nbytes);
1265
1266                 if (rval == -1) {
1267                         doio_fprintf(stderr,
1268                                      "write() failed:  %s (%d)\n%s\n",
1269                                      SYSERR, errno,
1270                                      format_rw(req, fd, addr, -1, Pattern, NULL));
1271                         doio_fprintf(stderr,
1272                                      "write() failed:  %s\n\twrite(%d, %#o, %d)\n\toffset %d, nbytes%%miniou(%d)=%d, oflags=%#o memalign=%d, addr%%memalign=%d\n",
1273                                      strerror(errno),
1274                                      fd, addr, nbytes,
1275                                      offset,
1276                                      fdc->c_miniosz, nbytes%fdc->c_miniosz,
1277                                      oflags, fdc->c_memalign, (long)addr%fdc->c_memalign);
1278                         doio_upanic(U_RVAL);
1279                 } else if (rval != nbytes) {
1280                         doio_fprintf(stderr,
1281                                      "write() returned wrong # bytes - expected %d, got %d\n%s\n",
1282                                      nbytes, rval,
1283                                      format_rw(req, fd, addr, -1, Pattern, NULL));
1284                         doio_upanic(U_RVAL);
1285                         rval = -1;
1286                 }
1287
1288                 break;
1289         }
1290
1291         /*
1292          * Verify that the data was written correctly - check_file() returns
1293          * a non-null pointer which contains an error message if there are
1294          * problems.
1295          */
1296
1297         if (v_opt) {
1298                 msg = check_file(file, offset, nbytes, Pattern, Pattern_Length,
1299                                  0, oflags & O_PARALLEL);
1300                 if (msg != NULL) {
1301                         doio_fprintf(stderr, "%s%s\n",
1302                                      msg,
1303                                      format_rw(req, fd, addr, -1, Pattern, NULL)
1304                                 );
1305                         doio_upanic(U_CORRUPTION);
1306                         exit(E_COMPARE);
1307
1308                 }
1309         }
1310
1311         /*
1312          * General cleanup ...
1313          *
1314          * Write extent information to the write-log, so that doio_check can do
1315          * corruption detection.  Note that w_done is set to 1, indicating that
1316          * the write has been verified as complete.  We don't need to write the
1317          * filename on the second logging.
1318          */
1319
1320         if (w_opt && logged_write) {
1321                 wrec.w_done = 1;
1322                 wlog_record_write(&Wlog, &wrec, woffset);
1323         }
1324
1325         /*
1326          * Unlock file region if necessary
1327          */
1328
1329         if (got_lock) {
1330                 if (lock_file_region(file, fd, F_UNLCK, offset, nbytes) < 0) {
1331                         alloc_mem(-1);
1332                         exit(E_INTERNAL);
1333                 }
1334         }
1335
1336         return( (rval == -1) ? -1 : 0);
1337 }
1338
1339
1340 /*
1341  * Simple routine to lock/unlock a file using fcntl()
1342  */
1343
1344 int
1345 lock_file_region(fname, fd, type, start, nbytes)
1346 char    *fname;
1347 int     fd;
1348 int     type;
1349 int     start;
1350 int     nbytes;
1351 {
1352         struct flock    flk;
1353
1354         flk.l_type = type;
1355         flk.l_whence = 0;
1356         flk.l_start = start;
1357         flk.l_len = nbytes;
1358
1359         if (fcntl(fd, F_SETLKW, &flk) < 0) {
1360                 doio_fprintf(stderr,
1361                              "fcntl(%d, %d, %#o) failed for file %s, lock type %d, offset %d, length %d:  %s (%d), open flags: %#o\n",
1362                              fd, F_SETLKW, &flk, fname, type,
1363                              start, nbytes, SYSERR, errno,
1364                              fcntl(fd, F_GETFL, 0));
1365                 return -1;
1366         }
1367
1368         return 0;
1369 }
1370
1371 int
1372 do_listio(req)
1373 struct io_req   *req;
1374 {
1375         return -1;
1376 }
1377
1378 /* ---------------------------------------------------------------------------
1379  *
1380  * A new paradigm of doing the r/w system call where there is a "stub"
1381  * function that builds the info for the system call, then does the system
1382  * call; this is called by code that is common to all system calls and does
1383  * the syscall return checking, async I/O wait, iosw check, etc.
1384  *
1385  * Flags:
1386  *      WRITE, ASYNC, SSD/SDS,
1387  *      FILE_LOCK, WRITE_LOG, VERIFY_DATA,
1388  */
1389
1390 struct  status {
1391         int     rval;           /* syscall return */
1392         int     err;            /* errno */
1393         int     *aioid;         /* list of async I/O structures */
1394 };
1395
1396 struct syscall_info {
1397         char            *sy_name;
1398         int             sy_type;
1399         struct status   *(*sy_syscall)();
1400         int             (*sy_buffer)();
1401         char            *(*sy_format)();
1402         int             sy_flags;
1403         int             sy_bits;
1404 };
1405
1406 #define SY_WRITE                00001
1407 #define SY_ASYNC                00010
1408 #define SY_IOSW                 00020
1409 #define SY_SDS                  00100
1410
1411 char *
1412 fmt_ioreq(struct io_req *ioreq, struct syscall_info *sy, int fd)
1413 {
1414         static char             *errbuf=NULL;
1415         char                    *cp;
1416         struct rw_req           *io;
1417         struct smap             *aname;
1418
1419         if(errbuf == NULL)
1420                 errbuf = (char *)malloc(32768);
1421
1422         io = &ioreq->r_data.io;
1423
1424         /*
1425          * Look up async I/O completion strategy
1426          */
1427         for(aname=aionames;
1428             aname->value != -1 && aname->value != io->r_aio_strat;
1429             aname++)
1430                 ;
1431
1432         cp = errbuf;
1433         cp += sprintf(cp, "Request number %d\n", Reqno);
1434
1435         cp += sprintf(cp, "          fd %d is file %s - open flags are %#o %s\n",
1436                       fd, io->r_file, io->r_oflags, format_oflags(io->r_oflags));
1437
1438         if(sy->sy_flags & SY_WRITE) {
1439                 cp += sprintf(cp, "          write done at file offset %d - pattern is %c (%#o)\n",
1440                               io->r_offset,
1441                               (io->r_pattern == '\0') ? '?' : io->r_pattern,
1442                               io->r_pattern);
1443         } else {
1444                 cp += sprintf(cp, "          read done at file offset %d\n",
1445                       io->r_offset);
1446         }
1447
1448         if(sy->sy_flags & SY_ASYNC) {
1449                 cp += sprintf(cp, "          async io completion strategy is %s\n",
1450                               aname->string);
1451         }
1452
1453         cp += sprintf(cp, "          number of requests is %d, strides per request is %d\n",
1454                       io->r_nent, io->r_nstrides);
1455
1456         cp += sprintf(cp, "          i/o byte count = %d\n",
1457                       io->r_nbytes);
1458
1459         cp += sprintf(cp, "          memory alignment is %s\n",
1460                       (io->r_uflags & F_WORD_ALIGNED) ? "aligned" : "unaligned");
1461
1462         if(io->r_oflags & O_DIRECT) {
1463                 char            *dio_env;
1464                 struct dioattr  finfo;
1465                 
1466                 if(xfsctl(io->r_file, fd, XFS_IOC_DIOINFO, &finfo) == -1) {
1467                         cp += sprintf(cp, "          Error %s (%d) getting direct I/O info\n",
1468                                       strerror(errno), errno);
1469                         finfo.d_mem = 1;
1470                         finfo.d_miniosz = 1;
1471                         finfo.d_maxiosz = 1;
1472                 }
1473
1474                 dio_env = getenv("XFS_DIO_MIN");
1475                 if (dio_env)
1476                         finfo.d_mem = finfo.d_miniosz = atoi(dio_env);
1477
1478                 cp += sprintf(cp, "          DIRECT I/O: offset %% %d = %d length %% %d = %d\n",
1479                               finfo.d_miniosz,
1480                               io->r_offset % finfo.d_miniosz,
1481                               io->r_nbytes,
1482                               io->r_nbytes % finfo.d_miniosz);
1483                 cp += sprintf(cp, "          mem alignment 0x%x xfer size: small: %d large: %d\n",
1484                               finfo.d_mem, finfo.d_miniosz, finfo.d_maxiosz);
1485         }
1486         return(errbuf);
1487 }
1488
1489 struct status *
1490 sy_pread(req, sysc, fd, addr)
1491 struct io_req   *req;
1492 struct syscall_info *sysc;
1493 int fd;
1494 char *addr;
1495 {
1496         int rc;
1497         struct status   *status;
1498
1499         rc = pread(fd, addr, req->r_data.io.r_nbytes,
1500                    req->r_data.io.r_offset);
1501
1502         status = (struct status *)malloc(sizeof(struct status));
1503         if( status == NULL ){
1504                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
1505                         __FILE__, __LINE__);
1506                 return NULL;
1507         }
1508         status->aioid = NULL;
1509         status->rval = rc;
1510         status->err = errno;
1511
1512         return(status);
1513 }
1514
1515 struct status *
1516 sy_pwrite(req, sysc, fd, addr)
1517 struct io_req   *req;
1518 struct syscall_info *sysc;
1519 int fd;
1520 char *addr;
1521 {
1522         int rc;
1523         struct status   *status;
1524
1525         rc = pwrite(fd, addr, req->r_data.io.r_nbytes,
1526                     req->r_data.io.r_offset);
1527
1528         status = (struct status *)malloc(sizeof(struct status));
1529         if( status == NULL ){
1530                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
1531                         __FILE__, __LINE__);
1532                 return NULL;
1533         }
1534         status->aioid = NULL;
1535         status->rval = rc;
1536         status->err = errno;
1537
1538         return(status);
1539 }
1540
1541 char *
1542 fmt_pread(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
1543 {
1544         static char     *errbuf = NULL;
1545         char            *cp;
1546
1547         if(errbuf == NULL){
1548                 errbuf = (char *)malloc(32768);
1549                 if( errbuf == NULL ){
1550                         doio_fprintf(stderr, "malloc failed, %s/%d\n",
1551                                 __FILE__, __LINE__);
1552                         return NULL;
1553                 }
1554         }
1555
1556         cp = errbuf;
1557         cp += sprintf(cp, "syscall:  %s(%d, 0x%p, %d)\n",
1558                       sy->sy_name, fd, addr, req->r_data.io.r_nbytes);
1559         return(errbuf);
1560 }
1561
1562 struct status *
1563 sy_rwv(req, sysc, fd, addr, rw)
1564 struct io_req   *req;
1565 struct syscall_info *sysc;
1566 int fd;
1567 char *addr;
1568 int rw;
1569 {
1570         int rc;
1571         struct status   *status;
1572         struct iovec    iov[2];
1573
1574         status = (struct status *)malloc(sizeof(struct status));
1575         if( status == NULL ){
1576                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
1577                         __FILE__, __LINE__);
1578                 return NULL;
1579         }
1580         status->aioid = NULL;
1581
1582         /* move to the desired file position. */
1583         if ((rc=lseek(fd, req->r_data.io.r_offset, SEEK_SET)) == -1) {
1584                 status->rval = rc;
1585                 status->err = errno;
1586                 return(status);
1587         }
1588
1589         iov[0].iov_base = addr;
1590         iov[0].iov_len = req->r_data.io.r_nbytes;
1591
1592         if(rw)
1593                 rc = writev(fd, iov, 1);
1594         else
1595                 rc = readv(fd, iov, 1);
1596         status->aioid = NULL;
1597         status->rval = rc;
1598         status->err = errno;
1599         return(status);
1600 }
1601
1602 struct status *
1603 sy_readv(req, sysc, fd, addr)
1604 struct io_req   *req;
1605 struct syscall_info *sysc;
1606 int fd;
1607 char *addr;
1608 {
1609         return sy_rwv(req, sysc, fd, addr, 0);
1610 }
1611
1612 struct status *
1613 sy_writev(req, sysc, fd, addr)
1614 struct io_req   *req;
1615 struct syscall_info *sysc;
1616 int fd;
1617 char *addr;
1618 {
1619         return sy_rwv(req, sysc, fd, addr, 1);
1620 }
1621
1622 char *
1623 fmt_readv(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
1624 {
1625         static char     errbuf[32768];
1626         char            *cp;
1627
1628         cp = errbuf;
1629         cp += sprintf(cp, "syscall:  %s(%d, (iov on stack), 1)\n",
1630                       sy->sy_name, fd);
1631         return(errbuf);
1632 }
1633
1634 struct status *
1635 sy_mmrw(req, sysc, fd, addr, rw)
1636 struct io_req *req;
1637 struct syscall_info *sysc;
1638 int fd;
1639 char *addr;
1640 int rw;
1641 {
1642         /*
1643          * mmap read/write
1644          * This version is oriented towards mmaping the file to memory
1645          * ONCE and keeping it mapped.
1646          */
1647         struct status           *status;
1648         void                    *mrc, *memaddr;
1649         struct fd_cache         *fdc;
1650         struct stat             sbuf;
1651
1652         status = (struct status *)malloc(sizeof(struct status));
1653         if( status == NULL ){
1654                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
1655                         __FILE__, __LINE__);
1656                 return NULL;
1657         }
1658         status->aioid = NULL;
1659         status->rval = -1;
1660
1661         fdc = alloc_fdcache(req->r_data.io.r_file, req->r_data.io.r_oflags);
1662
1663         if( fdc->c_memaddr == NULL ) {
1664                 if( fstat(fd, &sbuf) < 0 ){
1665                         doio_fprintf(stderr, "fstat failed, errno=%d\n",
1666                                      errno);
1667                         status->err = errno;
1668                         return(status);
1669                 }
1670
1671                 fdc->c_memlen = (int)sbuf.st_size;
1672                 mrc = mmap(NULL, (int)sbuf.st_size,
1673                      rw ? PROT_WRITE|PROT_READ : PROT_READ,
1674                      MAP_SHARED, fd, 0);
1675
1676                 if( mrc == MAP_FAILED ) {
1677                         doio_fprintf(stderr, "mmap() failed - 0x%lx %d\n",
1678                                 mrc, errno);
1679                         status->err = errno;
1680                         return(status);
1681                 }
1682
1683                 fdc->c_memaddr = mrc;
1684         }
1685
1686         memaddr = (void *)((char *)fdc->c_memaddr + req->r_data.io.r_offset);
1687
1688         active_mmap_rw = 1;
1689         if(rw)
1690                 memcpy(memaddr, addr, req->r_data.io.r_nbytes);
1691         else
1692                 memcpy(addr, memaddr, req->r_data.io.r_nbytes);
1693         active_mmap_rw = 0;
1694
1695         status->rval = req->r_data.io.r_nbytes;
1696         status->err = 0;
1697         return(status);
1698 }
1699
1700 struct status *
1701 sy_mmread(req, sysc, fd, addr)
1702 struct io_req *req;
1703 struct syscall_info *sysc;
1704 int fd;
1705 char *addr;
1706 {
1707         return sy_mmrw(req, sysc, fd, addr, 0);
1708 }
1709
1710 struct status *
1711 sy_mmwrite(req, sysc, fd, addr)
1712 struct io_req *req;
1713 struct syscall_info *sysc;
1714 int fd;
1715 char *addr;
1716 {
1717         return sy_mmrw(req, sysc, fd, addr, 1);
1718 }
1719
1720 char *
1721 fmt_mmrw(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
1722 {
1723         static char     errbuf[32768];
1724         char            *cp;
1725         struct fd_cache *fdc;
1726         void            *memaddr;
1727
1728         fdc = alloc_fdcache(req->r_data.io.r_file, req->r_data.io.r_oflags);
1729
1730         cp = errbuf;
1731         cp += sprintf(cp, "syscall:  %s(NULL, %d, %s, MAP_SHARED, %d, 0)\n",
1732                       sy->sy_name,
1733                       fdc->c_memlen,
1734                       (sy->sy_flags & SY_WRITE) ? "PROT_WRITE" : "PROT_READ",
1735                       fd);
1736
1737         cp += sprintf(cp, "\tfile is mmaped to: 0x%lx\n",
1738                       (unsigned long) fdc->c_memaddr);
1739
1740         memaddr = (void *)((char *)fdc->c_memaddr + req->r_data.io.r_offset);
1741
1742         cp += sprintf(cp, "\tfile-mem=0x%lx, length=%d, buffer=0x%lx\n",
1743                       (unsigned long) memaddr, req->r_data.io.r_nbytes,
1744                       (unsigned long) addr);
1745                       
1746         return(errbuf);
1747 }
1748
1749 struct syscall_info syscalls[] = {
1750         { "pread",                      PREAD,
1751           sy_pread,     NULL,           fmt_pread,
1752           0
1753         },
1754         { "pwrite",                     PWRITE,
1755           sy_pwrite,    NULL,           fmt_pread,
1756           SY_WRITE
1757         },
1758
1759         { "readv",                      READV,
1760           sy_readv,     NULL,           fmt_readv,
1761           0
1762         },
1763         { "writev",                     WRITEV,
1764           sy_writev,    NULL,           fmt_readv,
1765           SY_WRITE
1766         },
1767         { "mmap-read",                  MMAPR,
1768           sy_mmread,    NULL,           fmt_mmrw,
1769           0
1770         },
1771         { "mmap-write",                 MMAPW,
1772           sy_mmwrite,   NULL,           fmt_mmrw,
1773           SY_WRITE
1774         },
1775
1776         { NULL,                         0,
1777           0,            0,              0,
1778           0
1779         },
1780 };
1781
1782 int
1783 do_rw(req)
1784         struct io_req   *req;
1785 {
1786         static int              pid = -1;
1787         int                     fd, offset, nbytes, nstrides, nents, oflags;
1788         int                     rval, mem_needed, i;
1789         int                     logged_write, got_lock, woffset = 0, pattern;
1790         int                     min_byte, max_byte;
1791         char                    *addr, *file, *msg;
1792         struct status           *s;
1793         struct wlog_rec         wrec;
1794         struct syscall_info     *sy;
1795         struct fd_cache         *fdc;
1796
1797         /*
1798          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
1799          * r_nbytes are at the same offset in the read_req and reada_req
1800          * structures.
1801          */
1802         file    = req->r_data.io.r_file;
1803         oflags  = req->r_data.io.r_oflags;
1804         offset  = req->r_data.io.r_offset;
1805         nbytes  = req->r_data.io.r_nbytes;
1806         nstrides= req->r_data.io.r_nstrides;
1807         nents   = req->r_data.io.r_nent;
1808         pattern = req->r_data.io.r_pattern;
1809
1810         if( nents >= MAX_AIO ) {
1811                 doio_fprintf(stderr, "do_rw: too many list requests, %d.  Maximum is %d\n",
1812                              nents, MAX_AIO);
1813                 return(-1);
1814         }
1815
1816         /*
1817          * look up system call info
1818          */
1819         for(sy=syscalls; sy->sy_name != NULL && sy->sy_type != req->r_type; sy++)
1820                 ;
1821
1822         if(sy->sy_name == NULL) {
1823                 doio_fprintf(stderr, "do_rw: unknown r_type %d.\n",
1824                              req->r_type);
1825                 return(-1);
1826         }
1827
1828         /*
1829          * Get an open file descriptor
1830          * Note: must be done before memory allocation so that the direct i/o
1831          *      information is available in mem. allocate
1832          */
1833
1834         if ((fd = alloc_fd(file, oflags)) == -1)
1835                 return -1;
1836
1837         /*
1838          * Allocate core memory and possibly sds space.  Initialize the
1839          * data to be written.  Make sure we get enough, based on the
1840          * memstride.
1841          *
1842          * need:
1843          *      1 extra word for possible partial-word address "bump"
1844          *      1 extra word for dynamic pattern overrun
1845          *      MPP_BUMP extra words for T3E non-hw-aligned memory address.
1846          */
1847
1848         if( sy->sy_buffer != NULL ) {
1849                 mem_needed = (*sy->sy_buffer)(req, 0, 0, NULL, NULL);
1850         } else {
1851                 mem_needed = nbytes;
1852         }
1853
1854         /* get memory alignment for using DIRECT I/O */
1855         fdc = alloc_fdcache(file, oflags);
1856
1857         if ((rval = alloc_mem(mem_needed + wtob(1) * 2 + fdc->c_memalign)) < 0) {
1858                 return rval;
1859         }
1860
1861         Pattern[0] = pattern;
1862
1863         /*
1864          * Allocate SDS space for backdoor write if desired
1865          */
1866
1867         if (oflags & O_SSD) {
1868                 doio_fprintf(stderr, "Invalid O_SSD flag was generated for non-Cray system\n");
1869                 fflush(stderr);
1870                 return -1;
1871         } else {
1872                 addr = Memptr;
1873
1874                 /*
1875                  * if io is not raw, bump the offset by a random amount
1876                  * to generate non-word-aligned io.
1877                  *
1878                  * On MPP systems, raw I/O must start on an 0x80 byte boundary.
1879                  * For non-aligned I/O, bump the address from 1 to 8 words.
1880                  */
1881
1882                 if (! (req->r_data.io.r_uflags & F_WORD_ALIGNED)) {
1883                         addr += random_range(0, wtob(1) - 1, 1, NULL);
1884                 }
1885
1886                 /*
1887                  * Force memory alignment for Direct I/O
1888                  */
1889                 if( (oflags & O_DIRECT) && ((long)addr % fdc->c_memalign != 0) ) {
1890                         addr += fdc->c_memalign - ((long)addr % fdc->c_memalign);
1891                 }
1892
1893                 /*
1894                  * FILL must be done on a word-aligned buffer.
1895                  * Call the fill function with Memptr which is aligned,
1896                  * then memmove it to the right place.
1897                  */
1898                 if (sy->sy_flags & SY_WRITE) {
1899                         (*Data_Fill)(Memptr, mem_needed, Pattern, Pattern_Length, 0);
1900                         if( addr != Memptr )
1901                             memmove( addr, Memptr, mem_needed);
1902                 }
1903         }
1904
1905         rval = 0;
1906         got_lock = 0;
1907         logged_write = 0;
1908
1909         /*
1910          * Lock data if this is a write and locking option is set
1911          */
1912         if (sy->sy_flags & SY_WRITE && k_opt) {
1913                 if( sy->sy_buffer != NULL ) {
1914                         (*sy->sy_buffer)(req, offset, 0, &min_byte, &max_byte);
1915                 } else {
1916                         min_byte = offset;
1917                         max_byte = offset + (nbytes * nstrides * nents);
1918                 }
1919
1920                 if (lock_file_region(file, fd, F_WRLCK,
1921                                      min_byte, (max_byte-min_byte+1)) < 0) {
1922                     doio_fprintf(stderr, 
1923                                 "file lock failed:\n%s\n",
1924                                 fmt_ioreq(req, sy, fd));
1925                     doio_fprintf(stderr, 
1926                                 "          buffer(req, %d, 0, 0x%x, 0x%x)\n",
1927                                 offset, min_byte, max_byte);
1928                     alloc_mem(-1);
1929                     exit(E_INTERNAL);
1930                 }
1931
1932                 got_lock = 1;
1933         }
1934
1935         /*
1936          * Write a preliminary write-log entry.  This is done so that
1937          * doio_check can do corruption detection across an interrupt/crash.
1938          * Note that w_done is set to 0.  If doio_check sees this, it
1939          * re-creates the file extents as if the write completed, but does not
1940          * do any checking - see comments in doio_check for more details.
1941          */
1942
1943         if (sy->sy_flags & SY_WRITE && w_opt) {
1944                 if (pid == -1) {
1945                         pid = getpid();
1946                 }
1947
1948                 wrec.w_async = (sy->sy_flags & SY_ASYNC) ? 1 : 0;
1949                 wrec.w_oflags = oflags;
1950                 wrec.w_pid = pid;
1951                 wrec.w_offset = offset;
1952                 wrec.w_nbytes = nbytes; /* mem_needed -- total length */
1953
1954                 wrec.w_pathlen = strlen(file);
1955                 memcpy(wrec.w_path, file, wrec.w_pathlen);
1956                 wrec.w_hostlen = strlen(Host);
1957                 memcpy(wrec.w_host, Host, wrec.w_hostlen);
1958                 wrec.w_patternlen = Pattern_Length;
1959                 memcpy(wrec.w_pattern, Pattern, wrec.w_patternlen);
1960
1961                 wrec.w_done = 0;
1962
1963                 if ((woffset = wlog_record_write(&Wlog, &wrec, -1)) == -1) {
1964                         doio_fprintf(stderr,
1965                                      "Could not append to write-log:  %s (%d)\n",
1966                                      SYSERR, errno);
1967                 } else {
1968                         logged_write = 1;
1969                 }
1970         }
1971
1972         s = (*sy->sy_syscall)(req, sy, fd, addr);
1973
1974         if( s->rval == -1 ) {
1975                 doio_fprintf(stderr,
1976                              "%s() request failed:  %s (%d)\n%s\n%s\n",
1977                              sy->sy_name, SYSERR, errno,
1978                              fmt_ioreq(req, sy, fd),
1979                              (*sy->sy_format)(req, sy, fd, addr));
1980
1981                 doio_upanic(U_RVAL);
1982
1983                 for(i=0; i < nents; i++) {
1984                         if(s->aioid == NULL)
1985                                 break;
1986                         aio_unregister(s->aioid[i]);
1987                 }
1988                 rval = -1;
1989         } else {
1990                 /*
1991                  * If the syscall was async, wait for I/O to complete
1992                  */
1993 #ifndef linux
1994                 if(sy->sy_flags & SY_ASYNC) {
1995                         for(i=0; i < nents; i++) {
1996                                 aio_wait(s->aioid[i]);
1997                         }
1998                 }
1999 #endif
2000
2001                 /*
2002                  * Check the syscall how-much-data-written return.  Look
2003                  * for this in either the return value or the 'iosw'
2004                  * structure.
2005                  */
2006
2007                 if( !(sy->sy_flags & SY_IOSW) ) {
2008
2009                         if(s->rval != mem_needed) {
2010                                 doio_fprintf(stderr,
2011                                              "%s() request returned wrong # of bytes - expected %d, got %d\n%s\n%s\n",
2012                                              sy->sy_name, nbytes, s->rval,
2013                                              fmt_ioreq(req, sy, fd),
2014                                              (*sy->sy_format)(req, sy, fd, addr));
2015                                 rval = -1;
2016                                 doio_upanic(U_RVAL);
2017                         }
2018                 }
2019         }
2020
2021
2022         /*
2023          * Verify that the data was written correctly - check_file() returns
2024          * a non-null pointer which contains an error message if there are
2025          * problems.
2026          */
2027
2028         if ( rval == 0 && sy->sy_flags & SY_WRITE && v_opt) {
2029                 msg = check_file(file, offset, nbytes*nstrides*nents,
2030                                  Pattern, Pattern_Length, 0,
2031                                  oflags & O_PARALLEL);
2032                 if (msg != NULL) {
2033                         doio_fprintf(stderr, "%s\n%s\n%s\n",
2034                                      msg,
2035                                      fmt_ioreq(req, sy, fd),
2036                                      (*sy->sy_format)(req, sy, fd, addr));
2037                         doio_upanic(U_CORRUPTION);
2038                         exit(E_COMPARE);
2039                 }
2040         }
2041
2042         /*
2043          * General cleanup ...
2044          *
2045          * Write extent information to the write-log, so that doio_check can do
2046          * corruption detection.  Note that w_done is set to 1, indicating that
2047          * the write has been verified as complete.  We don't need to write the
2048          * filename on the second logging.
2049          */
2050
2051         if (w_opt && logged_write) {
2052                 wrec.w_done = 1;
2053                 wlog_record_write(&Wlog, &wrec, woffset);
2054         }
2055
2056         /*
2057          * Unlock file region if necessary
2058          */
2059
2060         if (got_lock) {
2061                 if (lock_file_region(file, fd, F_UNLCK,
2062                                      min_byte, (max_byte-min_byte+1)) < 0) {
2063                         alloc_mem(-1);
2064                         exit(E_INTERNAL);
2065                 }
2066         }
2067
2068         if(s->aioid != NULL)
2069                 free(s->aioid);
2070         free(s);
2071         return (rval == -1) ? -1 : 0;
2072 }
2073
2074
2075 /*
2076  * xfsctl-based requests
2077  *   - XFS_IOC_RESVSP
2078  *   - XFS_IOC_UNRESVSP
2079  */
2080 int
2081 do_xfsctl(req)
2082         struct io_req   *req;
2083 {
2084         int                     fd, oflags, offset, nbytes;
2085         int                     rval, op = 0;
2086         int                     got_lock;
2087         int                     min_byte = 0, max_byte = 0;
2088         char                    *file, *msg = NULL;
2089         struct xfs_flock64      flk;
2090
2091         /*
2092          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
2093          * r_nbytes are at the same offset in the read_req and reada_req
2094          * structures.
2095          */
2096         file    = req->r_data.io.r_file;
2097         oflags  = req->r_data.io.r_oflags;
2098         offset  = req->r_data.io.r_offset;
2099         nbytes  = req->r_data.io.r_nbytes;
2100
2101         flk.l_type=0;
2102         flk.l_whence=SEEK_SET;
2103         flk.l_start=offset;
2104         flk.l_len=nbytes;
2105
2106         /*
2107          * Get an open file descriptor
2108          */
2109
2110         if ((fd = alloc_fd(file, oflags)) == -1)
2111                 return -1;
2112
2113         rval = 0;
2114         got_lock = 0;
2115
2116         /*
2117          * Lock data if this is locking option is set
2118          */
2119         if (k_opt) {
2120                 min_byte = offset;
2121                 max_byte = offset + nbytes;
2122
2123                 if (lock_file_region(file, fd, F_WRLCK,
2124                                      min_byte, (nbytes+1)) < 0) {
2125                     doio_fprintf(stderr, 
2126                                 "file lock failed:\n");
2127                     doio_fprintf(stderr, 
2128                                 "          buffer(req, %d, 0, 0x%x, 0x%x)\n",
2129                                 offset, min_byte, max_byte);
2130                     alloc_mem(-1);
2131                     exit(E_INTERNAL);
2132                 }
2133
2134                 got_lock = 1;
2135         }
2136
2137         switch (req->r_type) {
2138         case RESVSP:    op=XFS_IOC_RESVSP;      msg="resvsp";   break;
2139         case UNRESVSP:  op=XFS_IOC_UNRESVSP;    msg="unresvsp"; break;
2140         }
2141
2142         rval = xfsctl(file, fd, op, &flk);
2143
2144         if( rval == -1 ) {
2145                 doio_fprintf(stderr,
2146 "xfsctl %s request failed: %s (%d)\n\txfsctl(%d, %s %d, {%d %lld ==> %lld}\n",
2147                              msg, SYSERR, errno,
2148                              fd, msg, op, flk.l_whence, 
2149                              (long long)flk.l_start, 
2150                              (long long)flk.l_len);
2151
2152                 doio_upanic(U_RVAL);
2153                 rval = -1;
2154         }
2155
2156         /*
2157          * Unlock file region if necessary
2158          */
2159
2160         if (got_lock) {
2161                 if (lock_file_region(file, fd, F_UNLCK,
2162                                      min_byte, (max_byte-min_byte+1)) < 0) {
2163                         alloc_mem(-1);
2164                         exit(E_INTERNAL);
2165                 }
2166         }
2167
2168         return (rval == -1) ? -1 : 0;
2169 }
2170
2171 /*
2172  *  fsync(2) and fdatasync(2)
2173  */
2174 int
2175 do_sync(req)
2176         struct io_req   *req;
2177 {
2178         int                     fd, oflags;
2179         int                     rval;
2180         char                    *file;
2181
2182         /*
2183          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
2184          * r_nbytes are at the same offset in the read_req and reada_req
2185          * structures.
2186          */
2187         file    = req->r_data.io.r_file;
2188         oflags  = req->r_data.io.r_oflags;
2189
2190         /*
2191          * Get an open file descriptor
2192          */
2193
2194         if ((fd = alloc_fd(file, oflags)) == -1)
2195                 return -1;
2196
2197         rval = 0;
2198         switch(req->r_type) {
2199         case FSYNC2:
2200                 rval = fsync(fd);
2201                 break;
2202         case FDATASYNC:
2203                 rval = fdatasync(fd);
2204                 break;
2205         default:
2206                 rval = -1;
2207         }
2208         return (rval == -1) ? -1 : 0;
2209 }
2210
2211 int
2212 doio_pat_fill(char *addr, int mem_needed, char *Pattern, int Pattern_Length,
2213               int shift)
2214 {
2215         return pattern_fill(addr, mem_needed, Pattern, Pattern_Length, 0);
2216 }
2217
2218 char *
2219 doio_pat_check(buf, offset, length, pattern, pattern_length, patshift)
2220 char    *buf;
2221 int     offset;
2222 int     length;
2223 char    *pattern;
2224 int     pattern_length;
2225 int     patshift;
2226 {
2227         static char     errbuf[4096];
2228         int             nb, i, pattern_index;
2229         char            *cp, *bufend, *ep;
2230         char            actual[33], expected[33];
2231
2232         if (pattern_check(buf, length, pattern, pattern_length, patshift) != 0) {
2233                 ep = errbuf;
2234                 ep += sprintf(ep, "Corrupt regions follow - unprintable chars are represented as '.'\n");
2235                 ep += sprintf(ep, "-----------------------------------------------------------------\n");
2236
2237                 pattern_index = patshift % pattern_length;;
2238                 cp = buf;
2239                 bufend = buf + length;
2240
2241                 while (cp < bufend) {
2242                         if (*cp != pattern[pattern_index]) {
2243                                 nb = bufend - cp;
2244                                 if (nb > sizeof(expected)-1) {
2245                                         nb = sizeof(expected)-1;
2246                                 }
2247                             
2248                                 ep += sprintf(ep, "corrupt bytes starting at file offset %d\n", offset + (int)(cp-buf));
2249
2250                                 /*
2251                                  * Fill in the expected and actual patterns
2252                                  */
2253                                 bzero(expected, sizeof(expected));
2254                                 bzero(actual, sizeof(actual));
2255
2256                                 for (i = 0; i < nb; i++) {
2257                                         expected[i] = pattern[(pattern_index + i) % pattern_length];
2258                                         if (! isprint((int)expected[i])) {
2259                                                 expected[i] = '.';
2260                                         }
2261
2262                                         actual[i] = cp[i];
2263                                         if (! isprint((int)actual[i])) {
2264                                                 actual[i] = '.';
2265                                         }
2266                                 }
2267
2268                                 ep += sprintf(ep, "    1st %2d expected bytes:  %s\n", nb, expected);
2269                                 ep += sprintf(ep, "    1st %2d actual bytes:    %s\n", nb, actual);
2270                                 fflush(stderr);
2271                                 return errbuf;
2272                         } else {
2273                                 cp++;
2274                                 pattern_index++;
2275
2276                                 if (pattern_index == pattern_length) {
2277                                         pattern_index = 0;
2278                                 }
2279                         }
2280                 }
2281                 return errbuf;
2282         }
2283
2284         return(NULL);
2285 }
2286
2287
2288 /*
2289  * Check the contents of a file beginning at offset, for length bytes.  It
2290  * is assumed that there is a string of pattern bytes in this area of the
2291  * file.  Use normal buffered reads to do the verification.
2292  *
2293  * If there is a data mismatch, write a detailed message into a static buffer
2294  * suitable for the caller to print.  Otherwise print NULL.
2295  *
2296  * The fsa flag is set to non-zero if the buffer should be read back through
2297  * the FSA (unicos/mk).  This implies the file will be opened
2298  * O_PARALLEL|O_RAW|O_WELLFORMED to do the validation.  We must do this because
2299  * FSA will not allow the file to be opened for buffered io if it was
2300  * previously opened for O_PARALLEL io.
2301  */
2302
2303 char *
2304 check_file(file, offset, length, pattern, pattern_length, patshift, fsa)
2305 char    *file;
2306 int     offset;
2307 int     length;
2308 char    *pattern;
2309 int     pattern_length;
2310 int     patshift;
2311 int     fsa;
2312 {
2313         static char     errbuf[4096];
2314         int             fd, nb, flags;
2315         char            *buf, *em, *ep;
2316         struct fd_cache *fdc;
2317
2318         buf = Memptr;
2319
2320         if (V_opt) {
2321                 flags = Validation_Flags | O_RDONLY;
2322         } else {
2323                 flags = O_RDONLY;
2324         }
2325
2326         if ((fd = alloc_fd(file, flags)) == -1) {
2327                 sprintf(errbuf,
2328                         "Could not open file %s with flags %#o (%s) for data comparison:  %s (%d)\n",
2329                         file, flags, format_oflags(flags),
2330                         SYSERR, errno);
2331                 return errbuf;
2332         }
2333
2334         if (lseek(fd, offset, SEEK_SET) == -1) {
2335                 sprintf(errbuf, 
2336                         "Could not lseek to offset %d in %s for verification:  %s (%d)\n",
2337                         offset, file, SYSERR, errno);
2338                 return errbuf;
2339         }
2340
2341         /* Guarantee a properly aligned address on Direct I/O */
2342         fdc = alloc_fdcache(file, flags);
2343         if( (flags & O_DIRECT) && ((long)buf % fdc->c_memalign != 0) ) {
2344                 buf += fdc->c_memalign - ((long)buf % fdc->c_memalign);
2345         }
2346
2347         if ((nb = read(fd, buf, length)) == -1) {
2348                 sprintf(errbuf,
2349                         "Could not read %d bytes from %s for verification:  %s (%d)\n\tread(%d, 0x%p, %d)\n\tbuf %% alignment(%d) = %ld\n",
2350                         length, file, SYSERR, errno,
2351                         fd, buf, length,
2352                         fdc->c_memalign, (long)buf % fdc->c_memalign);
2353                 return errbuf;
2354         }
2355
2356         if (nb != length) {
2357                 sprintf(errbuf,
2358                         "Read wrong # bytes from %s.  Expected %d, got %d\n",
2359                         file, length, nb);
2360                 return errbuf;
2361         }
2362     
2363         if( (em = (*Data_Check)(buf, offset, length, pattern, pattern_length, patshift)) != NULL ) {
2364                 ep = errbuf;
2365                 ep += sprintf(ep, "*** DATA COMPARISON ERROR ***\n");
2366                 ep += sprintf(ep, "check_file(%s, %d, %d, %s, %d, %d) failed\n\n",
2367                               file, offset, length, pattern, pattern_length, patshift);
2368                 ep += sprintf(ep, "Comparison fd is %d, with open flags %#o\n",
2369                               fd, flags);
2370                 strcpy(ep, em);
2371                 return(errbuf);
2372         }
2373         return NULL;
2374 }
2375
2376 /*
2377  * Function to single-thread stdio output.
2378  */
2379
2380 int
2381 doio_fprintf(FILE *stream, char *format, ...)
2382 {
2383         static int      pid = -1;
2384         char            *date;
2385         int             rval;
2386         struct flock    flk;
2387         va_list         arglist;
2388
2389         date = hms(time(0));
2390
2391         if (pid == -1) {
2392                 pid = getpid();
2393         }
2394
2395         flk.l_whence = flk.l_start = flk.l_len = 0;
2396         flk.l_type = F_WRLCK;
2397         fcntl(fileno(stream), F_SETLKW, &flk);
2398
2399         va_start(arglist, format);
2400         rval = fprintf(stream, "\n%s%s (%5d) %s\n", Prog, TagName, pid, date);
2401         rval += fprintf(stream, "---------------------\n");
2402         vfprintf(stream, format, arglist);
2403         va_end(arglist);
2404
2405         fflush(stream);
2406
2407         flk.l_type = F_UNLCK;
2408         fcntl(fileno(stream), F_SETLKW, &flk);
2409  
2410         return rval;
2411 }
2412
2413 /*
2414  * Simple function for allocating core memory.  Uses Memsize and Memptr to
2415  * keep track of the current amount allocated.
2416  */
2417
2418 int
2419 alloc_mem(nbytes)
2420 int nbytes;
2421 {
2422         char            *cp;
2423         void            *addr;
2424         int             me = 0, flags, key, shmid;
2425         static int      mturn = 0;      /* which memory type to use */
2426         struct memalloc *M;
2427         char            filename[255];
2428 #ifdef linux
2429         struct shmid_ds shm_ds;
2430         bzero( &shm_ds, sizeof(struct shmid_ds) );
2431 #endif
2432
2433         /* nbytes = -1 means "free all allocated memory" */
2434         if( nbytes == -1 ) {
2435
2436                 for(me=0; me < Nmemalloc; me++) {
2437                         if(Memalloc[me].space == NULL)
2438                                 continue;
2439
2440                         switch(Memalloc[me].memtype) {
2441                         case MEM_DATA:
2442                                 free(Memalloc[me].space);
2443                                 Memalloc[me].space = NULL;
2444                                 Memptr = NULL;
2445                                 Memsize = 0;
2446                                 break;
2447                         case MEM_SHMEM:
2448                                 shmdt(Memalloc[me].space);
2449                                 Memalloc[me].space = NULL;
2450                                 shmctl(Memalloc[me].fd, IPC_RMID, &shm_ds);
2451                                 break;
2452                         case MEM_MMAP:
2453                                 munmap(Memalloc[me].space, 
2454                                        Memalloc[me].size);
2455                                 close(Memalloc[me].fd);
2456                                 if(Memalloc[me].flags & MEMF_FILE) {
2457                                         unlink(Memalloc[me].name);
2458                                 }
2459                                 Memalloc[me].space = NULL;
2460                                 break;
2461                         default:
2462                                 doio_fprintf(stderr, "alloc_mem: HELP! Unknown memory space type %d index %d\n",
2463                                              Memalloc[me].memtype, me);
2464                                 break;
2465                         }
2466                 }
2467                 return 0;
2468         }
2469
2470         /*
2471          * Select a memory area (currently round-robbin)
2472          */
2473
2474         if(mturn >= Nmemalloc)
2475                 mturn=0;
2476
2477         M = &Memalloc[mturn];
2478
2479         switch(M->memtype) {
2480         case MEM_DATA:
2481                 if( nbytes > M->size ) {
2482                         if( M->space != NULL ){
2483                                 free(M->space);
2484                         }
2485                         M->space = NULL;
2486                         M->size = 0;
2487                 }
2488
2489                 if( M->space == NULL ) {
2490                         if( (cp = malloc( nbytes )) == NULL ) {
2491                                 doio_fprintf(stderr, "malloc(%d) failed:  %s (%d)\n",
2492                                              nbytes, SYSERR, errno);
2493                                 return -1;
2494                         }
2495                         M->space = (void *)cp;
2496                         M->size = nbytes;
2497                 }
2498                 break;
2499
2500         case MEM_MMAP:
2501                 if( nbytes > M->size ) {
2502                         if( M->space != NULL ) {
2503                                 munmap(M->space, M->size);
2504                                 close(M->fd);
2505                                 if( M->flags & MEMF_FILE )
2506                                         unlink( M->name );
2507                         }
2508                         M->space = NULL;
2509                         M->size = 0;
2510                 }
2511
2512                 if( M->space == NULL ) {
2513                         if(strchr(M->name, '%')) {
2514                                 sprintf(filename, M->name, getpid());
2515                                 M->name = strdup(filename);
2516                         }
2517
2518                         if( (M->fd = open(M->name, O_CREAT|O_RDWR, 0666)) == -1) {
2519                                 doio_fprintf(stderr, "alloc_mmap: error %d (%s) opening '%s'\n",
2520                                              errno, SYSERR, 
2521                                              M->name);
2522                                 return(-1);
2523                         }
2524
2525                         addr = NULL;
2526                         flags = 0;
2527                         M->size = nbytes * 4;
2528
2529                         /* bias addr if MEMF_ADDR | MEMF_FIXADDR */
2530                         /* >>> how to pick a memory address? */
2531
2532                         /* bias flags on MEMF_PRIVATE etc */
2533                         if(M->flags & MEMF_PRIVATE)
2534                                 flags |= MAP_PRIVATE;
2535                         if(M->flags & MEMF_SHARED)
2536                                 flags |= MAP_SHARED;
2537
2538 /*printf("alloc_mem, about to mmap, fd=%d, name=(%s)\n", M->fd, M->name);*/
2539                         if( (M->space = mmap(addr, M->size,
2540                                              PROT_READ|PROT_WRITE,
2541                                              flags, M->fd, 0))
2542                             == MAP_FAILED) {
2543                                 doio_fprintf(stderr, "alloc_mem: mmap error. errno %d (%s)\n\tmmap(addr 0x%x, size %d, read|write 0x%x, mmap flags 0x%x [%#o], fd %d, 0)\n\tfile %s\n",
2544                                              errno, SYSERR,
2545                                              addr, M->size,
2546                                              PROT_READ|PROT_WRITE,
2547                                              flags, M->flags, M->fd,
2548                                              M->name);
2549                                 doio_fprintf(stderr, "\t%s%s%s%s%s",
2550                                              (flags & MAP_PRIVATE) ? "private " : "",
2551                                              (flags & MAP_SHARED) ? "shared" : "");
2552                                 return(-1);
2553                         }
2554                 }
2555                 break;
2556                 
2557         case MEM_SHMEM:
2558                 if( nbytes > M->size ) {
2559                         if( M->space != NULL ) {
2560                                 shmctl( M->fd, IPC_RMID, &shm_ds );
2561                         }
2562                         M->space = NULL;
2563                         M->size = 0;
2564                 }
2565
2566                 if(M->space == NULL) {
2567                         if(!strcmp(M->name, "private")) {
2568                                 key = IPC_PRIVATE;
2569                         } else {
2570                                 sscanf(M->name, "%i", &key);
2571                         }
2572
2573                         M->size = M->nblks ? M->nblks * 512 : nbytes;
2574
2575                         if( nbytes > M->size ){
2576 #ifdef DEBUG
2577                                 doio_fprintf(stderr, "MEM_SHMEM: nblks(%d) too small:  nbytes=%d  Msize=%d, skipping this req.\n",
2578                                              M->nblks, nbytes, M->size );
2579 #endif
2580                                 return SKIP_REQ;
2581                         }
2582
2583                         shmid = shmget(key, M->size, IPC_CREAT|0666);
2584                         if( shmid == -1 ) {
2585                                 doio_fprintf(stderr, "shmget(0x%x, %d, CREAT) failed: %s (%d)\n",
2586                                              key, M->size, SYSERR, errno);
2587                                 return(-1);
2588                         }
2589                         M->fd = shmid;
2590                         M->space = shmat(shmid, NULL, SHM_RND);
2591                         if( M->space == (void *)-1 ) {
2592                                 doio_fprintf(stderr, "shmat(0x%x, NULL, SHM_RND) failed: %s (%d)\n", 
2593                                              shmid, SYSERR, errno);
2594                                 return(-1);
2595                         }
2596                 }
2597                 break;
2598
2599         default:
2600                 doio_fprintf(stderr, "alloc_mem: HELP! Unknown memory space type %d index %d\n",
2601                              Memalloc[me].memtype, mturn);
2602                 break;
2603         }
2604
2605         Memptr = M->space;
2606         Memsize = M->size;
2607
2608         mturn++;
2609         return 0;
2610 }
2611
2612 /*
2613  * Function to maintain a file descriptor cache, so that doio does not have
2614  * to do so many open() and close() calls.  Descriptors are stored in the
2615  * cache by file name, and open flags.  Each entry also has a _rtc value
2616  * associated with it which is used in aging.  If doio cannot open a file
2617  * because it already has too many open (ie. system limit hit) it will close
2618  * the one in the cache that has the oldest _rtc value.
2619  *
2620  * If alloc_fd() is called with a file of NULL, it will close all descriptors
2621  * in the cache, and free the memory in the cache.
2622  */
2623
2624 int
2625 alloc_fd(file, oflags)
2626 char    *file;
2627 int     oflags;
2628 {
2629         struct fd_cache *fdc;
2630         struct fd_cache *alloc_fdcache(char *file, int oflags);
2631
2632         fdc = alloc_fdcache(file, oflags);
2633         if(fdc != NULL)
2634                 return(fdc->c_fd);
2635         else
2636                 return(-1);
2637 }
2638
2639 struct fd_cache *
2640 alloc_fdcache(file, oflags)
2641 char    *file;
2642 int     oflags;
2643 {
2644         int                     fd;
2645         struct fd_cache         *free_slot, *oldest_slot, *cp;
2646         static int              cache_size = 0;
2647         static struct fd_cache  *cache = NULL;
2648         struct dioattr  finfo;
2649
2650         /*
2651          * If file is NULL, it means to free up the fd cache.
2652          */
2653
2654         if (file == NULL && cache != NULL) {
2655                 for (cp = cache; cp < &cache[cache_size]; cp++) {
2656                         if (cp->c_fd != -1) {
2657                                 close(cp->c_fd);
2658                         }
2659                         if (cp->c_memaddr != NULL) {
2660                                 munmap(cp->c_memaddr, cp->c_memlen);
2661                         }
2662                 }
2663
2664                 free(cache);
2665                 cache = NULL;
2666                 cache_size = 0;
2667                 return 0;
2668         }
2669
2670         free_slot = NULL;
2671         oldest_slot = NULL;
2672
2673         /*
2674          * Look for a fd in the cache.  If one is found, return it directly.
2675          * Otherwise, when this loop exits, oldest_slot will point to the
2676          * oldest fd slot in the cache, and free_slot will point to an
2677          * unoccupied slot if there are any.
2678          */
2679
2680         for (cp = cache; cp != NULL && cp < &cache[cache_size]; cp++) {
2681                 if (cp->c_fd != -1 &&
2682                     cp->c_oflags == oflags &&
2683                     strcmp(cp->c_file, file) == 0) {
2684                         cp->c_rtc = Reqno;
2685                         return cp;
2686                 }
2687
2688                 if (cp->c_fd == -1) {
2689                         if (free_slot == NULL) {
2690                                 free_slot = cp;
2691                         }
2692                 } else {
2693                         if (oldest_slot == NULL || 
2694                             cp->c_rtc < oldest_slot->c_rtc) {
2695                                 oldest_slot = cp;
2696                         }
2697                 }
2698         }
2699
2700         /*
2701          * No matching file/oflags pair was found in the cache.  Attempt to
2702          * open a new fd.
2703          */
2704
2705         if ((fd = open(file, oflags, 0666)) < 0) {
2706                 if (errno != EMFILE) {
2707                         doio_fprintf(stderr,
2708                                      "Could not open file %s with flags %#o (%s): %s (%d)\n",
2709                                      file, oflags, format_oflags(oflags),
2710                                      SYSERR, errno);
2711                         alloc_mem(-1);
2712                         exit(E_SETUP);
2713                 }
2714
2715                 /*
2716                  * If we get here, we have as many open fd's as we can have.
2717                  * Close the oldest one in the cache (pointed to by
2718                  * oldest_slot), and attempt to re-open.
2719                  */
2720
2721                 close(oldest_slot->c_fd);
2722                 oldest_slot->c_fd = -1;
2723                 free_slot = oldest_slot;
2724
2725                 if ((fd = open(file, oflags, 0666)) < 0) {
2726                         doio_fprintf(stderr,
2727                                      "Could not open file %s with flags %#o (%s):  %s (%d)\n",
2728                                      file, oflags, format_oflags(oflags),
2729                                      SYSERR, errno);
2730                         alloc_mem(-1);
2731                         exit(E_SETUP);
2732                 }
2733         }
2734
2735 /*printf("alloc_fd: new file %s flags %#o fd %d\n", file, oflags, fd);*/
2736
2737         /*
2738          * If we get here, fd is our open descriptor.  If free_slot is NULL,
2739          * we need to grow the cache, otherwise free_slot is the slot that
2740          * should hold the fd info.
2741          */
2742
2743         if (free_slot == NULL) {
2744                 cache = (struct fd_cache *)realloc(cache, sizeof(struct fd_cache) * (FD_ALLOC_INCR + cache_size));
2745                 if (cache == NULL) {
2746                         doio_fprintf(stderr, "Could not malloc() space for fd chace");
2747                         alloc_mem(-1);
2748                         exit(E_SETUP);
2749                 }
2750
2751                 cache_size += FD_ALLOC_INCR;
2752
2753                 for (cp = &cache[cache_size-FD_ALLOC_INCR];
2754                      cp < &cache[cache_size]; cp++) {
2755                         cp->c_fd = -1;
2756                 }
2757
2758                 free_slot = &cache[cache_size - FD_ALLOC_INCR];
2759         }
2760
2761         /*
2762          * finally, fill in the cache slot info
2763          */
2764
2765         free_slot->c_fd = fd;
2766         free_slot->c_oflags = oflags;
2767         strcpy(free_slot->c_file, file);
2768         free_slot->c_rtc = Reqno;
2769
2770         if (oflags & O_DIRECT) {
2771                 char *dio_env;
2772
2773                 if (xfsctl(file, fd, XFS_IOC_DIOINFO, &finfo) == -1) {
2774                         finfo.d_mem = 1;
2775                         finfo.d_miniosz = 1;
2776                         finfo.d_maxiosz = 1;
2777                 }
2778
2779                 dio_env = getenv("XFS_DIO_MIN");
2780                 if (dio_env)
2781                         finfo.d_mem = finfo.d_miniosz = atoi(dio_env);
2782
2783         } else {
2784                 finfo.d_mem = 1;
2785                 finfo.d_miniosz = 1;
2786                 finfo.d_maxiosz = 1;
2787         }
2788
2789         free_slot->c_memalign = finfo.d_mem;
2790         free_slot->c_miniosz = finfo.d_miniosz;
2791         free_slot->c_maxiosz = finfo.d_maxiosz;
2792         free_slot->c_memaddr = NULL;
2793         free_slot->c_memlen = 0;
2794
2795         return free_slot;
2796 }
2797
2798 /*
2799  *
2800  *                      Signal Handling Section
2801  *
2802  *
2803  */
2804
2805 void
2806 cleanup_handler()
2807 {
2808         havesigint=1; /* in case there's a followup signal */
2809         alloc_mem(-1);
2810         exit(0);
2811 }
2812
2813 void
2814 die_handler(sig)
2815 int sig;
2816 {
2817         doio_fprintf(stderr, "terminating on signal %d\n", sig);
2818         alloc_mem(-1);
2819         exit(1);
2820 }
2821
2822 void
2823 sigbus_handler(sig)
2824 int sig;
2825 {
2826         /* See sigbus_handler() in the 'ifdef sgi' case for details.  Here,
2827            we don't have the siginfo stuff so the guess is weaker but we'll
2828            do it anyway.
2829         */
2830
2831         if( active_mmap_rw && havesigint )
2832                 cleanup_handler();
2833         else
2834                 die_handler(sig);
2835 }
2836
2837 void
2838 noop_handler(sig)
2839 int sig;
2840 {
2841         return;
2842 }
2843
2844
2845 /*
2846  * SIGINT handler for the parent (original doio) process.  It simply sends
2847  * a SIGINT to all of the doio children.  Since they're all in the same
2848  * pgrp, this can be done with a single kill().
2849  */
2850
2851 void
2852 sigint_handler()
2853 {
2854         int     i;
2855
2856         for (i = 0; i < Nchildren; i++) {
2857                 if (Children[i] != -1) {
2858                         kill(Children[i], SIGINT);
2859                 }
2860         }
2861 }
2862
2863 /*
2864  * Signal handler used to inform a process when async io completes.  Referenced
2865  * in do_read() and do_write().  Note that the signal handler is not
2866  * re-registered.
2867  */
2868
2869 void
2870 aio_handler(sig)
2871 int     sig;
2872 {
2873         int             i;
2874         struct aio_info *aiop;
2875
2876         for (i = 0; i < sizeof(Aio_Info) / sizeof(Aio_Info[0]); i++) {
2877                 aiop = &Aio_Info[i];
2878
2879                 if (aiop->strategy == A_SIGNAL && aiop->sig == sig) {
2880                         aiop->signalled++;
2881
2882                         if (aio_done(aiop)) {
2883                                 aiop->done++;
2884                         }
2885                 }
2886         }
2887 }
2888
2889 /*
2890  * dump info on all open aio slots
2891  */
2892 void
2893 dump_aio()
2894 {
2895         int             i, count;
2896
2897         count=0;
2898         for (i = 0; i < sizeof(Aio_Info) / sizeof(Aio_Info[0]); i++) {
2899                 if (Aio_Info[i].busy) {
2900                         count++;
2901                         fprintf(stderr,
2902                                 "Aio_Info[%03d] id=%d fd=%d signal=%d signaled=%d\n",
2903                                 i, Aio_Info[i].id,
2904                                 Aio_Info[i].fd,
2905                                 Aio_Info[i].sig,
2906                                 Aio_Info[i].signalled);
2907                         fprintf(stderr, "\tstrategy=%s\n",
2908                                 format_strat(Aio_Info[i].strategy));
2909                 }
2910         }
2911         fprintf(stderr, "%d active async i/os\n", count);
2912 }
2913
2914 struct aio_info *
2915 aio_slot(aio_id)
2916 int     aio_id;
2917 {
2918         int             i;
2919         static int      id = 1;
2920         struct aio_info *aiop;
2921
2922         aiop = NULL;
2923
2924         for (i = 0; i < sizeof(Aio_Info) / sizeof(Aio_Info[0]); i++) {
2925                 if (aio_id == -1) {
2926                         if (! Aio_Info[i].busy) {
2927                                 aiop = &Aio_Info[i];
2928                                 aiop->busy = 1;
2929                                 aiop->id = id++;
2930                                 break;
2931                         }
2932                 } else {
2933                         if (Aio_Info[i].busy && Aio_Info[i].id == aio_id) {
2934                                 aiop = &Aio_Info[i];
2935                                 break;
2936                         }
2937                 }
2938         }
2939
2940         if( aiop == NULL ){
2941                 doio_fprintf(stderr,"aio_slot(%d) not found.  Request %d\n", 
2942                              aio_id, Reqno);
2943                 dump_aio();
2944                 alloc_mem(-1);
2945                 exit(E_INTERNAL);
2946         }
2947
2948         return aiop;
2949 }
2950
2951 int
2952 aio_register(fd, strategy, sig)
2953 int             fd;
2954 int             strategy;
2955 int             sig;
2956 {
2957         struct aio_info         *aiop;
2958         void                    aio_handler();
2959         struct sigaction        sa;
2960
2961         aiop = aio_slot(-1);
2962
2963         aiop->fd = fd;
2964         aiop->strategy = strategy;
2965         aiop->done = 0;
2966
2967         if (strategy == A_SIGNAL) {
2968                 aiop->sig = sig;
2969                 aiop->signalled = 0;
2970
2971                 sa.sa_handler = aio_handler;
2972                 sa.sa_flags = 0;
2973                 sigemptyset(&sa.sa_mask);
2974
2975                 sigaction(sig, &sa, &aiop->osa);
2976         } else {
2977                 aiop->sig = -1;
2978                 aiop->signalled = 0;
2979         }
2980
2981         return aiop->id;
2982 }
2983
2984 int
2985 aio_unregister(aio_id)
2986 int     aio_id;
2987 {
2988         struct aio_info *aiop;
2989
2990         aiop = aio_slot(aio_id);
2991
2992         if (aiop->strategy == A_SIGNAL) {
2993                 sigaction(aiop->sig, &aiop->osa, NULL);
2994         }
2995
2996         aiop->busy = 0;
2997         return 0;
2998 }
2999
3000 #ifndef linux
3001 int
3002 aio_wait(aio_id)
3003 int     aio_id;
3004 {
3005 #ifdef RECALL_SIZEOF
3006         long            mask[RECALL_SIZEOF];
3007 #endif
3008         sigset_t        sigset;
3009         struct aio_info *aiop;
3010
3011 /*printf("aio_wait: errno %d return %d\n", aiop->aio_errno, aiop->aio_ret);*/
3012
3013         return 0;
3014 }
3015 #endif /* !linux */
3016
3017 /*
3018  * Format specified time into HH:MM:SS format.  t is the time to format
3019  * in seconds (as returned from time(2)).
3020  */
3021
3022 char *
3023 hms(t)
3024 time_t  t;
3025 {
3026         static char     ascii_time[9];
3027         struct tm       *ltime;
3028
3029         ltime = localtime(&t);
3030         strftime(ascii_time, sizeof(ascii_time), "%H:%M:%S", ltime);
3031
3032         return ascii_time;
3033 }
3034
3035 /*
3036  * Simple routine to check if an async io request has completed.
3037  */
3038
3039 int
3040 aio_done(struct aio_info *ainfo)
3041 {
3042         return -1;   /* invalid */
3043 }
3044
3045 /*
3046  * Routine to handle upanic() - it first attempts to set the panic flag.  If
3047  * the flag cannot be set, an error message is issued.  A call to upanic
3048  * with PA_PANIC is then done unconditionally, in case the panic flag was set
3049  * from outside the program (as with the panic(8) program).
3050  *
3051  * Note - we only execute the upanic code if -U was used, and the passed in
3052  * mask is set in the Upanic_Conditions bitmask.
3053  */
3054
3055 void
3056 doio_upanic(mask)
3057 int     mask;
3058 {
3059         if (U_opt == 0 || (mask & Upanic_Conditions) == 0) {
3060                 return;
3061         }
3062         doio_fprintf(stderr, "WARNING - upanic() failed\n");
3063 }
3064
3065 /*
3066  * Parse cmdline options/arguments and set appropriate global variables.
3067  * If the cmdline is valid, return 0 to caller.  Otherwise exit with a status
3068  * of 1.
3069  */
3070
3071 int
3072 parse_cmdline(argc, argv, opts)
3073 int     argc;
3074 char    **argv;
3075 char    *opts;
3076 {
3077         int             c;
3078         char            cc, *cp, *tok = NULL;
3079         extern int      opterr;
3080         extern int      optind;
3081         extern char     *optarg;
3082         struct smap     *s;
3083         char            *memargs[NMEMALLOC];
3084         int             nmemargs, ma;
3085         void            parse_memalloc(char *arg);
3086         void            parse_delay(char *arg);
3087         void            dump_memalloc();
3088
3089         if (*argv[0] == '-') {
3090                 argv[0]++;
3091                 Execd = 1;
3092         }
3093         
3094         if ((Prog = strrchr(argv[0], '/')) == NULL) {
3095                 Prog = argv[0];
3096         } else {
3097                 Prog++;
3098         }
3099         
3100         opterr = 0;
3101         while ((c = getopt(argc, argv, opts)) != EOF) {
3102                 switch ((char)c) {
3103                 case 'a':
3104                         a_opt++;
3105                         break;
3106
3107                 case 'C':
3108                         C_opt++;
3109                         for(s=checkmap; s->string != NULL; s++)
3110                                 if(!strcmp(s->string, optarg))
3111                                         break;
3112                         if (s->string == NULL) {
3113                                 fprintf(stderr,
3114                                         "%s%s:  Illegal -C arg (%s).  Must be one of: ", 
3115                                         Prog, TagName, tok);
3116
3117                                 for (s = checkmap; s->string != NULL; s++)
3118                                         fprintf(stderr, "%s ", s->string);
3119                                 fprintf(stderr, "\n");
3120                                 exit(1);
3121                         }
3122
3123                         switch(s->value) {
3124                         case C_DEFAULT:
3125                                 Data_Fill = doio_pat_fill;
3126                                 Data_Check = doio_pat_check;
3127                                 break;
3128                         default:
3129                                 fprintf(stderr,
3130                                         "%s%s:  Unrecognised -C arg '%s' %d", 
3131                                         Prog, TagName, s->string, s->value);
3132                                 exit(1);
3133                         }
3134                         break;
3135
3136                 case 'd':       /* delay between i/o ops */
3137                         parse_delay(optarg);
3138                         break;
3139
3140                 case 'e':
3141                         if (Npes > 1 && Nprocs > 1) {
3142                                 fprintf(stderr, "%s%s:  Warning - Program is a multi-pe application - exec option is ignored.\n", Prog, TagName);
3143                         }
3144                         e_opt++;
3145                         break;
3146
3147                 case 'h':
3148                         help(stdout);
3149                         exit(0);
3150                         break;
3151
3152                 case 'k':
3153                         k_opt++;
3154                         break;
3155
3156                 case 'm':
3157                         Message_Interval = strtol(optarg, &cp, 10);
3158                         if (*cp != '\0' || Message_Interval < 0) {
3159                                 fprintf(stderr, "%s%s:  Illegal -m arg (%s):  Must be an integer >= 0\n", Prog, TagName, optarg);
3160                                 exit(1);
3161                         }
3162                         m_opt++;
3163                         break;
3164
3165                 case 'M':       /* memory allocation types */
3166                         nmemargs = string_to_tokens(optarg, memargs, 32, ",");
3167                         for(ma=0; ma < nmemargs; ma++) {
3168                                 parse_memalloc(memargs[ma]);
3169                         }
3170                         /*dump_memalloc();*/
3171                         M_opt++;
3172                         break;
3173
3174                 case 'N':
3175                         sprintf( TagName, "(%.37s)", optarg );
3176                         break;
3177
3178                 case 'n':
3179                         Nprocs = strtol(optarg, &cp, 10);
3180                         if (*cp != '\0' || Nprocs < 1) {
3181                                 fprintf(stderr,
3182                                         "%s%s:  Illegal -n arg (%s):  Must be integer > 0\n",
3183                                         Prog, TagName, optarg);
3184                                 exit(E_USAGE);
3185                         }
3186
3187                         if (Npes > 1 && Nprocs > 1) {
3188                                 fprintf(stderr, "%s%s:  Program has been built as a multi-pe app.  -n1 is the only nprocs value allowed\n", Prog, TagName);
3189                                 exit(E_SETUP);
3190                         }
3191                         n_opt++;
3192                         break;
3193
3194                 case 'r':
3195                         Release_Interval = strtol(optarg, &cp, 10);
3196                         if (*cp != '\0' || Release_Interval < 0) {
3197                                 fprintf(stderr,
3198                                         "%s%s:  Illegal -r arg (%s):  Must be integer >= 0\n",
3199                                         Prog, TagName, optarg);
3200                                 exit(E_USAGE);
3201                         }
3202
3203                         r_opt++;
3204                         break;
3205
3206                 case 'w':
3207                         Write_Log = optarg;
3208                         w_opt++;
3209                         break;
3210
3211                 case 'v':
3212                         v_opt++;
3213                         break;
3214
3215                 case 'V':
3216                         if (strcasecmp(optarg, "sync") == 0) {
3217                                 Validation_Flags = O_SYNC;
3218                         } else if (strcasecmp(optarg, "buffered") == 0) {
3219                                 Validation_Flags = 0;
3220                         } else if (strcasecmp(optarg, "direct") == 0) {
3221                                 Validation_Flags = O_DIRECT;
3222                         } else {
3223                                 if (sscanf(optarg, "%i%c", &Validation_Flags, &cc) != 1) {
3224                                         fprintf(stderr, "%s:  Invalid -V argument (%s) - must be a decimal, hex, or octal\n", Prog, optarg);
3225                                         fprintf(stderr, "    number, or one of the following strings:  'sync',\n");
3226                                         fprintf(stderr, "    'buffered', 'parallel', 'ldraw', or 'raw'\n");
3227                                         exit(E_USAGE);
3228                                 }
3229                         }
3230                         V_opt++;
3231                         break;
3232                 case 'U':
3233                         tok = strtok(optarg, ",");
3234                         while (tok != NULL) {
3235                                 for (s = Upanic_Args; s->string != NULL; s++)
3236                                         if (strcmp(s->string, tok) == 0)
3237                                                 break;
3238
3239                                 if (s->string == NULL) {
3240                                         fprintf(stderr,
3241                                                 "%s%s:  Illegal -U arg (%s).  Must be one of: ", 
3242                                                 Prog, TagName, tok);
3243
3244                                         for (s = Upanic_Args; s->string != NULL; s++)
3245                                                 fprintf(stderr, "%s ", s->string);
3246
3247                                         fprintf(stderr, "\n");
3248
3249                                         exit(1);
3250                                 }
3251
3252                                 Upanic_Conditions |= s->value;
3253                                 tok = strtok(NULL, ",");
3254                         }
3255
3256                         U_opt++;
3257                         break;
3258
3259                 case '?':
3260                         usage(stderr);
3261                         exit(E_USAGE);
3262                         break;
3263                 }
3264         }
3265         
3266         /*
3267          * Supply defaults
3268          */
3269         
3270         if (! C_opt) {
3271                 Data_Fill = doio_pat_fill;
3272                 Data_Check = doio_pat_check;
3273         }
3274
3275         if (! U_opt)
3276                 Upanic_Conditions = 0;
3277
3278         if (! n_opt)
3279                 Nprocs = 1;
3280         
3281         if (! r_opt)
3282                 Release_Interval = DEF_RELEASE_INTERVAL;
3283
3284         if (! M_opt) {
3285                 Memalloc[Nmemalloc].memtype = MEM_DATA;
3286                 Memalloc[Nmemalloc].flags = 0;
3287                 Memalloc[Nmemalloc].name = NULL;
3288                 Memalloc[Nmemalloc].space = NULL;
3289                 Nmemalloc++;
3290         }
3291
3292         /*
3293          * Initialize input stream
3294          */
3295
3296         if (argc == optind) {
3297                 Infile = NULL;
3298         } else {
3299                 Infile = argv[optind++];
3300         }
3301
3302         if (argc != optind) {
3303                 usage(stderr);
3304                 exit(E_USAGE);
3305         }
3306
3307         return 0;
3308 }       
3309
3310
3311
3312 /*
3313  * Parse memory allocation types
3314  *
3315  * Types are:
3316  *  Data
3317  *  T3E-shmem:blksize[:nblks]
3318  *  SysV-shmem:shmid:blksize:nblks
3319  *      if shmid is "private", use IPC_PRIVATE
3320  *      and nblks is not required
3321  *
3322  *  mmap:flags:filename:blksize[:nblks]
3323  *   flags are one of:
3324  *      p - private (MAP_PRIVATE)
3325  *      a - private, MAP_AUTORESRV
3326  *      l - local (MAP_LOCAL)
3327  *      s - shared (nblks required)
3328  *
3329  *   plus any of:
3330  *      f - fixed address (MAP_FIXED)
3331  *      A - use an address without MAP_FIXED
3332  *      a - autogrow (map once at startup)
3333  *
3334  *  mmap:flags:devzero
3335  *      mmap /dev/zero  (shared not allowd)
3336  *      maps the first 4096 bytes of /dev/zero
3337  *
3338  * - put a directory at the beginning of the shared
3339  *   regions saying what pid has what region.
3340  *      DIRMAGIC
3341  *      BLKSIZE
3342  *      NBLKS
3343  *      nblks worth of directories - 1 int pids
3344  */
3345 void
3346 parse_memalloc(char *arg)
3347 {
3348         char            *allocargs[NMEMALLOC];
3349         int             nalloc;
3350         struct memalloc *M;
3351
3352         if(Nmemalloc >= NMEMALLOC) {
3353                 doio_fprintf(stderr, "Error - too many memory types (%d).\n", 
3354                         Nmemalloc);
3355                 return;
3356         }
3357
3358         M = &Memalloc[Nmemalloc];
3359
3360         nalloc = string_to_tokens(arg, allocargs, 32, ":");
3361         if(!strcmp(allocargs[0], "data")) {
3362                 M->memtype = MEM_DATA;
3363                 M->flags = 0;
3364                 M->name = NULL;
3365                 M->space = NULL;
3366                 Nmemalloc++;
3367                 if(nalloc >= 2) {
3368                         if(strchr(allocargs[1], 'p'))
3369                                 M->flags |= MEMF_MPIN;
3370                 }
3371         } else if(!strcmp(allocargs[0], "mmap")) {
3372                 /* mmap:flags:filename[:size] */
3373                 M->memtype = MEM_MMAP;
3374                 M->flags = 0;
3375                 M->space = NULL;
3376                 if(nalloc >= 1) {
3377                         if(strchr(allocargs[1], 'p'))
3378                                 M->flags |= MEMF_PRIVATE;
3379                         if(strchr(allocargs[1], 'a'))
3380                                 M->flags |= MEMF_AUTORESRV;
3381                         if(strchr(allocargs[1], 'l'))
3382                                 M->flags |= MEMF_LOCAL;
3383                         if(strchr(allocargs[1], 's'))
3384                                 M->flags |= MEMF_SHARED;
3385
3386                         if(strchr(allocargs[1], 'f'))
3387                                 M->flags |= MEMF_FIXADDR;
3388                         if(strchr(allocargs[1], 'A'))
3389                                 M->flags |= MEMF_ADDR;
3390                         if(strchr(allocargs[1], 'G'))
3391                                 M->flags |= MEMF_AUTOGROW;
3392
3393                         if(strchr(allocargs[1], 'U'))
3394                                 M->flags |= MEMF_FILE;
3395                 } else {
3396                         M->flags |= MEMF_PRIVATE;
3397                 }
3398
3399                 if(nalloc > 2) {
3400                         if(!strcmp(allocargs[2], "devzero")) {
3401                                 M->name = "/dev/zero";
3402                                 if(M->flags & 
3403                                    ((MEMF_PRIVATE|MEMF_LOCAL) == 0))
3404                                         M->flags |= MEMF_PRIVATE;
3405                         } else {
3406                                 M->name = allocargs[2];
3407                         }
3408                 } else {
3409                         M->name = "/dev/zero";
3410                         if(M->flags & 
3411                            ((MEMF_PRIVATE|MEMF_LOCAL) == 0))
3412                                 M->flags |= MEMF_PRIVATE;
3413                 }
3414                 Nmemalloc++;
3415
3416         } else if(!strcmp(allocargs[0], "shmem")) {
3417                 /* shmem:shmid:size */
3418                 M->memtype = MEM_SHMEM;
3419                 M->flags = 0;
3420                 M->space = NULL;
3421                 if(nalloc >= 2) {
3422                         M->name = allocargs[1];
3423                 } else {
3424                         M->name = NULL;
3425                 }
3426                 if(nalloc >= 3) {
3427                         sscanf(allocargs[2], "%i", &M->nblks);
3428                 } else {
3429                         M->nblks = 0;
3430                 }
3431                 if(nalloc >= 4) {
3432                         if(strchr(allocargs[3], 'p'))
3433                                 M->flags |= MEMF_MPIN;
3434                 }
3435
3436                 Nmemalloc++;
3437         } else {
3438                 doio_fprintf(stderr, "Error - unknown memory type '%s'.\n",
3439                         allocargs[0]);
3440                 exit(1);
3441         }
3442 }
3443
3444 void
3445 dump_memalloc()
3446 {
3447         int     ma;
3448         char    *mt;
3449
3450         if(Nmemalloc == 0) {
3451                 printf("No memory allocation strategies devined\n");
3452                 return;
3453         }
3454
3455         for(ma=0; ma < Nmemalloc; ma++) {
3456                 switch(Memalloc[ma].memtype) {
3457                 case MEM_DATA:  mt = "data";    break;
3458                 case MEM_SHMEM: mt = "shmem";   break;
3459                 case MEM_MMAP:  mt = "mmap";    break;
3460                 default:        mt = "unknown"; break;
3461                 }
3462                 printf("mstrat[%d] = %d %s\n", ma, Memalloc[ma].memtype, mt);
3463                 printf("\tflags=%#o name='%s' nblks=%d\n",
3464                        Memalloc[ma].flags,
3465                        Memalloc[ma].name,
3466                        Memalloc[ma].nblks);
3467         }
3468 }
3469
3470 /*
3471  * -d <op>:<time> - doio inter-operation delay
3472  *      currently this permits ONE type of delay between operations.
3473  */
3474
3475 void
3476 parse_delay(char *arg)
3477 {
3478         char            *delayargs[NMEMALLOC];
3479         int             ndelay;
3480         struct smap     *s;
3481
3482         ndelay = string_to_tokens(arg, delayargs, 32, ":");
3483         if(ndelay < 2) {
3484                 doio_fprintf(stderr,
3485                         "Illegal delay arg (%s). Must be operation:time\n", arg);
3486                 exit(1);
3487         }
3488         for(s=delaymap; s->string != NULL; s++)
3489                 if(!strcmp(s->string, delayargs[0]))
3490                         break;
3491         if (s->string == NULL) {
3492                 fprintf(stderr,
3493                         "Illegal Delay arg (%s).  Must be one of: ", arg);
3494
3495                 for (s = delaymap; s->string != NULL; s++)
3496                         fprintf(stderr, "%s ", s->string);
3497                 fprintf(stderr, "\n");
3498                 exit(1);
3499         }
3500
3501         delayop = s->value;
3502
3503         sscanf(delayargs[1], "%i", &delaytime);
3504
3505         if(ndelay > 2) {
3506                 fprintf(stderr,
3507                         "Warning: extra delay arguments ignored.\n");
3508         }
3509 }
3510
3511
3512 /*
3513  * Usage clause - obvious
3514  */
3515
3516 int
3517 usage(stream)
3518 FILE    *stream;
3519 {
3520         /*
3521          * Only do this if we are on vpe 0, to avoid seeing it from every
3522          * process in the application.
3523          */
3524
3525         if (Npes > 1 && Vpe != 0) {
3526                 return 0;
3527         }
3528
3529         fprintf(stream, "usage%s:  %s [-aekv] [-m message_interval] [-n nprocs] [-r release_interval] [-w write_log] [-V validation_ftype] [-U upanic_cond] [infile]\n", TagName, Prog);
3530         return 0;
3531 }
3532
3533 void
3534 help(stream)
3535 FILE    *stream;
3536 {
3537         /*
3538          * Only the app running on vpe 0 gets to issue help - this prevents
3539          * everybody in the application from doing this.
3540          */
3541
3542         if (Npes > 1 && Vpe != 0) {
3543                 return;
3544         }
3545
3546         usage(stream);
3547         fprintf(stream, "\n");
3548         fprintf(stream, "\t-a                   abort - kill all doio processes on data compare\n");
3549         fprintf(stream, "\t                     errors.  Normally only the erroring process exits\n");
3550         fprintf(stream, "\t-C data-pattern-type \n");
3551         fprintf(stream, "\t                     Available data patterns are:\n");
3552         fprintf(stream, "\t                     default - repeating pattern\n");
3553         fprintf(stream, "\t-d Operation:Time    Inter-operation delay.\n");
3554         fprintf(stream, "\t                     Operations are:\n");
3555         fprintf(stream, "\t                         select:time (1 second=1000000)\n");
3556         fprintf(stream, "\t                         sleep:time (1 second=1)\n");
3557         fprintf(stream, "\t                         alarm:time (1 second=1)\n");
3558         fprintf(stream, "\t-e                   Re-exec children before entering the main\n");
3559         fprintf(stream, "\t                     loop.  This is useful for spreading\n");
3560         fprintf(stream, "\t                     procs around on multi-pe systems.\n");
3561         fprintf(stream, "\t-k                   Lock file regions during writes using fcntl()\n");
3562         fprintf(stream, "\t-v                   Verify writes - this is done by doing a buffered\n");
3563         fprintf(stream, "\t                     read() of the data if file io was done, or\n");
3564         fprintf(stream, "\t                     an ssread()of the data if sds io was done\n");
3565         fprintf(stream, "\t-M                   Data buffer allocation method\n");
3566         fprintf(stream, "\t                     alloc-type[,type]\n");
3567         fprintf(stream, "\t                         data\n");
3568         fprintf(stream, "\t                         shmem:shmid:size\n");
3569         fprintf(stream, "\t                         mmap:flags:filename\n");
3570         fprintf(stream, "\t                             p - private\n");
3571         fprintf(stream, "\t                             s - shared (shared file must exist\n"),
3572         fprintf(stream, "\t                                 and have needed length)\n");
3573         fprintf(stream, "\t                             f - fixed address (not used)\n");
3574         fprintf(stream, "\t                             a - specify address (not used)\n");
3575         fprintf(stream, "\t                             U - Unlink file when done\n");
3576         fprintf(stream, "\t                             The default flag is private\n");
3577         fprintf(stream, "\n");
3578         fprintf(stream, "\t-m message_interval  Generate a message every 'message_interval'\n");
3579         fprintf(stream, "\t                     requests.  An interval of 0 suppresses\n");
3580         fprintf(stream, "\t                     messages.  The default is 0.\n");
3581         fprintf(stream, "\t-N tagname           Tag name, for Monster.\n");
3582         fprintf(stream, "\t-n nprocs            # of processes to start up\n");
3583         fprintf(stream, "\t-r release_interval  Release all memory and close\n");
3584         fprintf(stream, "\t                     files every release_interval operations.\n");
3585         fprintf(stream, "\t                     By default procs never release memory\n");
3586         fprintf(stream, "\t                     or close fds unless they have to.\n");
3587         fprintf(stream, "\t-V validation_ftype  The type of file descriptor to use for doing data\n");
3588         fprintf(stream, "\t                     validation.  validation_ftype may be an octal,\n");
3589         fprintf(stream, "\t                     hex, or decimal number representing the open()\n");
3590         fprintf(stream, "\t                     flags, or may be one of the following strings:\n");
3591         fprintf(stream, "\t                     'buffered' - validate using bufferd read\n");
3592         fprintf(stream, "\t                     'sync'     - validate using O_SYNC read\n");
3593         fprintf(stream, "\t                     'direct    - validate using O_DIRECT read'\n");
3594         fprintf(stream, "\t                     By default, 'parallel'\n");
3595         fprintf(stream, "\t                     is used if the write was done with O_PARALLEL\n");
3596         fprintf(stream, "\t                     or 'buffered' for all other writes.\n");
3597         fprintf(stream, "\t-w write_log         File to log file writes to.  The doio_check\n");
3598         fprintf(stream, "\t                     program can reconstruct datafiles using the\n");
3599         fprintf(stream, "\t                     write_log, and detect if a file is corrupt\n");
3600         fprintf(stream, "\t                     after all procs have exited.\n");
3601         fprintf(stream, "\t-U upanic_cond       Comma separated list of conditions that will\n");
3602         fprintf(stream, "\t                     cause a call to upanic(PA_PANIC).\n");
3603         fprintf(stream, "\t                     'corruption' -> upanic on bad data comparisons\n");
3604         fprintf(stream, "\t                     'iosw'     ---> upanic on unexpected async iosw\n");
3605         fprintf(stream, "\t                     'rval'     ---> upanic on unexpected syscall rvals\n");
3606         fprintf(stream, "\t                     'all'      ---> all of the above\n");
3607         fprintf(stream, "\n");
3608         fprintf(stream, "\tinfile               Input stream - default is stdin - must be a list\n");
3609         fprintf(stream, "\t                     of io_req structures (see doio.h).  Currently\n");
3610         fprintf(stream, "\t                     only the iogen program generates the proper\n");
3611         fprintf(stream, "\t                     format\n");
3612 }       
3613