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