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