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