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