Update copyright annotations and license boilerplates to correspond with SGI Legals...
[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 #ifdef sgi
33 #include <aio.h>        /* for aio_read,write */
34 #include <inttypes.h>   /* for uint64_t type */
35 #include <siginfo.h>    /* signal handlers & SA_SIGINFO */
36 #endif
37
38 #include <sys/uio.h>    /* for struct iovec (readv)*/
39 #include <sys/mman.h>   /* for mmap(2) */
40 #include <sys/ipc.h>    /* for i/o buffer in shared memory */
41 #include <sys/shm.h>    /* for i/o buffer in shared memory */
42 #include <sys/wait.h>
43 #include <sys/time.h>   /* for delays */
44 #include <ctype.h>
45
46 #ifndef NO_XFS
47 struct io_req;
48 int do_xfsctl(struct io_req *);
49 #endif
50
51 #include "doio.h"
52 #include "pattern.h"
53 #include "write_log.h"
54 #include "random_range.h"
55 #include "string_to_tokens.h"
56
57 #ifndef O_SSD
58 #define O_SSD 0         /* so code compiles on a CRAY2 */
59 #endif
60
61 #define UINT64_T unsigned long long
62
63 #ifndef O_PARALLEL
64 #define O_PARALLEL 0    /* so O_PARALLEL may be used in expressions */
65 #endif
66
67 #define PPID_CHECK_INTERVAL 5           /* check ppid every <-- iterations */
68 #define MAX_AIO         256             /* maximum number of async I/O ops */
69 #ifdef _CRAYMPP
70 #define MPP_BUMP        16              /* page un-alignment for MPP */
71 #else
72 #define MPP_BUMP        0
73 #endif
74
75
76 #define SYSERR strerror(errno)
77
78 /*
79  * getopt() string of supported cmdline arguments.
80  */
81
82 #define OPTS    "aC:d:ehm:n:kr:w:vU:V:M:N:"
83
84 #define DEF_RELEASE_INTERVAL    0
85
86 /*
87  * Flags set in parse_cmdline() to indicate which options were selected
88  * on the cmdline.
89  */
90
91 int     a_opt = 0;          /* abort on data compare errors     */
92 int     e_opt = 0;          /* exec() after fork()'ing          */
93 int     C_opt = 0;          /* Data Check Type                  */
94 int     d_opt = 0;          /* delay between operations         */
95 int     k_opt = 0;          /* lock file regions during writes  */
96 int     m_opt = 0;          /* generate periodic messages       */
97 int     n_opt = 0;          /* nprocs                           */
98 int     r_opt = 0;          /* resource release interval        */
99 int     w_opt = 0;          /* file write log file              */
100 int     v_opt = 0;          /* verify writes if set             */
101 int     U_opt = 0;          /* upanic() on varios conditions    */
102 int     V_opt = 0;          /* over-ride default validation fd type */
103 int     M_opt = 0;          /* data buffer allocation types     */
104 char    TagName[40];        /* name of this doio (see Monster)  */
105
106
107 /*
108  * Misc globals initialized in parse_cmdline()
109  */
110
111 char    *Prog = NULL;       /* set up in parse_cmdline()                */
112 int     Upanic_Conditions;  /* set by args to -U                        */
113 int     Release_Interval;   /* arg to -r                                */
114 int     Nprocs;             /* arg to -n                                */
115 char    *Write_Log;         /* arg to -w                                */
116 char    *Infile;            /* input file (defaults to stdin)           */
117 int     *Children;          /* pids of child procs                      */
118 int     Nchildren = 0;
119 int     Nsiblings = 0;      /* tfork'ed siblings                        */
120 int     Execd = 0;
121 int     Message_Interval = 0;
122 int     Npes = 0;           /* non-zero if built as an mpp multi-pe app */
123 int     Vpe = -1;           /* Virtual pe number if Npes >= 0           */
124 int     Reqno = 1;          /* request # - used in some error messages  */
125 int     Reqskipcnt = 0;     /* count of I/O requests that are skipped   */
126 int     Validation_Flags;
127 char    *(*Data_Check)();   /* function to call for data checking       */
128 int     (*Data_Fill)();     /* function to call for data filling        */
129 int     Nmemalloc = 0;      /* number of memory allocation strategies   */
130 int     delayop = 0;        /* delay between operations - type of delay */
131 int     delaytime = 0;      /* delay between operations - how long      */
132
133 struct wlog_file        Wlog;
134
135 int     active_mmap_rw = 0; /* Indicates that mmapped I/O is occurring. */
136                             /* Used by sigbus_action() in the child doio. */
137 int     havesigint = 0;
138
139 #define SKIP_REQ        -2      /* skip I/O request */
140
141 #define NMEMALLOC       32
142 #define MEM_DATA        1       /* data space                           */
143 #define MEM_SHMEM       2       /* System V shared memory               */
144 #define MEM_T3ESHMEM    3       /* T3E Shared Memory                    */
145 #define MEM_MMAP        4       /* mmap(2)                              */
146
147 #define MEMF_PRIVATE    0001
148 #define MEMF_AUTORESRV  0002
149 #define MEMF_LOCAL      0004
150 #define MEMF_SHARED     0010
151
152 #define MEMF_FIXADDR    0100
153 #define MEMF_ADDR       0200
154 #define MEMF_AUTOGROW   0400
155 #define MEMF_FILE       01000   /* regular file -- unlink on close      */
156 #define MEMF_MPIN       010000  /* use mpin(2) to lock pages in memory */
157
158 struct memalloc {
159         int     memtype;
160         int     flags;
161         int     nblks;
162         char    *name;
163         void    *space;         /* memory address of allocated space */
164         int     fd;             /* FD open for mmaping */
165         int     size;
166 }       Memalloc[NMEMALLOC];
167
168 /*
169  * Global file descriptors
170  */
171
172 int     Wfd_Append;         /* for appending to the write-log       */
173 int     Wfd_Random;         /* for overlaying write-log entries     */
174
175 /*
176  * Structure for maintaining open file test descriptors.  Used by
177  * alloc_fd().
178  */
179
180 struct fd_cache {
181         char    c_file[MAX_FNAME_LENGTH+1];
182         int     c_oflags;
183         int     c_fd;
184         long    c_rtc;
185 #ifndef NO_XFS
186         int     c_memalign;     /* from xfsctl(XFS_IOC_DIOINFO) */
187         int     c_miniosz;
188         int     c_maxiosz;
189 #endif
190 #ifndef CRAY
191         void    *c_memaddr;     /* mmapped address */
192         int     c_memlen;       /* length of above region */
193 #endif
194 };
195
196 #define FD_ALLOC_INCR   32      /* allocate this many fd_map structs    */
197                                 /* at a time */
198
199 /*
200  * Globals for tracking Sds and Core usage
201  */
202
203 char    *Memptr;                /* ptr to core buffer space             */
204 int     Memsize;                /* # bytes pointed to by Memptr         */
205                                 /* maintained by alloc_mem()            */
206
207 int     Sdsptr;                 /* sds offset (always 0)                */
208 int     Sdssize;                /* # bytes of allocated sds space       */
209                                 /* Maintained by alloc_sds()            */
210 char    Host[16];
211 char    Pattern[128];
212 int     Pattern_Length;
213
214 /*
215  * Signal handlers, and related globals
216  */
217
218 void    sigint_handler();       /* Catch SIGINT in parent doio, propagate
219                                  * to children, does not die. */
220
221 void    die_handler();          /* Bad sig in child doios, exit 1. */
222 void    cleanup_handler();      /* Normal kill, exit 0. */
223
224 #ifndef CRAY
225 void    sigbus_handler();       /* Handle sigbus--check active_mmap_rw to
226                                    decide if this should be a normal exit. */
227 #endif
228
229 void    cb_handler();           /* Posix aio callback handler. */
230 void    noop_handler();         /* Delayop alarm, does nothing. */
231 char    *hms();
232 char    *format_rw();
233 char    *format_sds();
234 char    *format_listio();
235 char    *check_file();
236 int     doio_fprintf(FILE *stream, char *format, ...);
237 void    doio_upanic();
238 void    doio();
239 void    help();
240 void    doio_delay();
241 int     alloc_fd( char *, int );
242 int     alloc_mem( int );
243 int     do_read( struct io_req * );
244 int     do_write( struct io_req * );
245 int     do_rw( struct io_req * );
246 int     do_sync( struct io_req * );
247 int     usage( FILE * );
248 int     aio_unregister( int );
249 int     parse_cmdline( int, char **, char * );
250 int     lock_file_region( char *, int, int, int, int );
251 struct  fd_cache *alloc_fdcache(char *, int);
252 int     aio_register( int, int, int );
253 #ifndef linux
254 int aio_wait(int);
255 #endif
256
257 /*
258  * Upanic conditions, and a map from symbolics to values
259  */
260
261 #define U_CORRUPTION    0001        /* upanic on data corruption    */
262 #define U_IOSW          0002        /* upanic on bad iosw           */
263 #define U_RVAL          0004        /* upanic on bad rval           */
264
265 #define U_ALL           (U_CORRUPTION | U_IOSW | U_RVAL)
266
267 /*
268  * Name-To-Value map
269  * Used to map cmdline arguments to values
270  */
271 struct smap {
272         char    *string;
273         int     value;
274 };
275
276 struct smap Upanic_Args[] = {
277         { "corruption", U_CORRUPTION    },
278         { "iosw",       U_IOSW          },
279         { "rval",       U_RVAL          },
280         { "all",        U_ALL           },
281         { NULL,         0               }
282 };
283
284 struct aio_info {
285         int                     busy;
286         int                     id;
287         int                     fd;
288         int                     strategy;
289         volatile int            done;
290 #ifdef CRAY
291         struct iosw             iosw;
292 #endif
293 #ifdef sgi
294         aiocb_t                 aiocb;
295         int                     aio_ret;        /* from aio_return */
296         int                     aio_errno;      /* from aio_error */
297 #endif
298         int                     sig;
299         int                     signalled;
300         struct sigaction        osa;
301 };
302
303 struct aio_info Aio_Info[MAX_AIO];
304
305 struct aio_info *aio_slot();
306 int     aio_done( struct aio_info * );
307
308 /* -C data-fill/check type */
309 #define C_DEFAULT       1
310 struct smap checkmap[] = {
311         { "default",    C_DEFAULT },
312         { NULL,         0 },
313 };
314
315 /* -d option delay types */
316 #define DELAY_SELECT    1
317 #define DELAY_SLEEP     2
318 #define DELAY_SGINAP    3
319 #define DELAY_ALARM     4
320 #define DELAY_ITIMER    5       /* POSIX timer                          */
321
322 struct smap delaymap[] = {
323         { "select",     DELAY_SELECT },
324         { "sleep",      DELAY_SLEEP },
325 #ifdef sgi
326         { "sginap",     DELAY_SGINAP },
327 #endif
328         { "alarm",      DELAY_ALARM },
329         { NULL, 0 },
330 };
331
332 /******
333 *
334 * strerror() does similar actions.
335
336 char *
337 syserrno(int err)
338 {
339     static char sys_errno[10];
340     sprintf(sys_errno, "%d", errno);
341     return(sys_errno);
342 }
343
344 ******/
345
346 int
347 main(argc, argv)
348 int     argc;
349 char    **argv;
350 {
351         int                     i, pid, stat, ex_stat;
352         struct sigaction        sa;
353         int omask;
354         umask(0);               /* force new file modes to known values */
355 #if _CRAYMPP
356         Npes = sysconf(_SC_CRAY_NPES);  /* must do this before parse_cmdline */
357         Vpe = sysconf(_SC_CRAY_VPE);
358 #endif
359
360         TagName[0] = '\0';
361         parse_cmdline(argc, argv, OPTS);
362
363         random_range_seed(getpid());       /* initialize random number generator */
364
365         /*      
366          * If this is a re-exec of doio, jump directly into the doio function.
367          */
368
369         if (Execd) {
370                 doio();
371                 exit(E_SETUP);
372         }
373
374         /*
375          * Stop on all but a few signals...
376          */
377         sigemptyset(&sa.sa_mask);
378         sa.sa_handler = sigint_handler;
379         sa.sa_flags = SA_RESETHAND;     /* sigint is ignored after the */
380                                         /* first time */
381         for (i = 1; i <= NSIG; i++) {
382                 switch(i) {
383 #ifdef SIGRECOVERY
384                 case SIGRECOVERY:
385                         break;
386 #endif
387 #ifdef SIGCKPT
388                 case SIGCKPT:
389 #endif
390 #ifdef SIGRESTART
391                 case SIGRESTART:
392 #endif
393                 case SIGTSTP:
394                 case SIGSTOP:
395                 case SIGCONT:
396                 case SIGCLD:
397                 case SIGBUS:
398                 case SIGSEGV:
399                 case SIGQUIT:
400                         break;
401                 default:
402                         sigaction(i, &sa, NULL);
403                 }
404         }
405
406         /*
407          * If we're logging write operations, make a dummy call to wlog_open
408          * to initialize the write history file.  This call must be done in
409          * the parent, to ensure that the history file exists and/or has
410          * been truncated before any children attempt to open it, as the doio
411          * children are not allowed to truncate the file.
412          */
413
414         if (w_opt) {
415                 strcpy(Wlog.w_file, Write_Log);
416
417                 if (wlog_open(&Wlog, 1, 0666) < 0) {
418                         doio_fprintf(stderr,
419                                      "Could not create/truncate write log %s\n",
420                                      Write_Log);
421                         exit(2);
422                 }
423
424                 wlog_close(&Wlog);
425         }
426
427         /*
428          * Malloc space for the children pid array.  Initialize all entries
429          * to -1.
430          */
431
432         Children = (int *)malloc(sizeof(int) * Nprocs);
433         for (i = 0; i < Nprocs; i++) {
434                 Children[i] = -1;
435         }
436
437         omask = sigblock(sigmask(SIGCLD));
438
439         /*
440          * Fork Nprocs.  This [parent] process is a watchdog, to notify the
441          * invoker of procs which exit abnormally, and to make sure that all
442          * child procs get cleaned up.  If the -e option was used, we will also
443          * re-exec.  This is mostly for unicos/mk on mpp's, to ensure that not
444          * all of the doio's don't end up in the same pe.
445          *
446          * Note - if Nprocs is 1, or this doio is a multi-pe app (Npes > 1),
447          * jump directly to doio().  multi-pe apps can't fork(), and there is
448          * no reason to fork() for 1 proc.
449          */
450
451         if (Nprocs == 1 || Npes > 1) {
452                 doio();
453                 exit(0);
454         } else {
455                 for (i = 0; i < Nprocs; i++) {
456                         if ((pid = fork()) == -1) {
457                                 doio_fprintf(stderr,
458                                              "(parent) Could not fork %d children:  %s (%d)\n",
459                                              i+1, SYSERR, errno);
460                                 exit(E_SETUP);
461                         }
462                         
463                         Children[Nchildren] = pid;
464                         Nchildren++;
465                         
466                         if (pid == 0) {
467                                 if (e_opt) {
468                                         char *exec_path;
469
470                                         exec_path = argv[0];
471                                         argv[0] = (char *)malloc(strlen(exec_path + 1));
472                                         sprintf(argv[0], "-%s", exec_path);
473
474                                         execvp(exec_path, argv);
475                                         doio_fprintf(stderr,
476                                                      "(parent) Could not execvp %s:  %s (%d)\n",
477                                                      exec_path, SYSERR, errno);
478                                         exit(E_SETUP);
479                                 } else {
480                                         doio();
481                                         exit(E_SETUP);
482                                 }
483                         }
484                 }
485
486                 /*
487                  * Parent spins on wait(), until all children exit.
488                  */
489                 
490                 ex_stat = E_NORMAL;
491                 
492                 while (Nprocs) {
493                         if ((pid = wait(&stat)) == -1) {
494                                 if (errno == EINTR)
495                                         continue;
496                         }
497                         
498                         for (i = 0; i < Nchildren; i++)
499                                 if (Children[i] == pid)
500                                         Children[i] = -1;
501                         
502                         Nprocs--;
503                         
504                         if (WIFEXITED(stat)) {
505                                 switch (WEXITSTATUS(stat)) {
506                                 case E_NORMAL:
507                                         /* noop */
508                                         break;
509
510                                 case E_INTERNAL:
511                                         doio_fprintf(stderr,
512                                                      "(parent) pid %d exited because of an internal error\n",
513                                                      pid);
514                                         ex_stat |= E_INTERNAL;
515                                         break;
516
517                                 case E_SETUP:
518                                         doio_fprintf(stderr,
519                                                      "(parent) pid %d exited because of a setup error\n",
520                                                      pid);
521                                         ex_stat |= E_SETUP;
522                                         break;
523
524                                 case E_COMPARE:
525                                         doio_fprintf(stderr,
526                                                      "(parent) pid %d exited because of data compare errors\n",
527                                                      pid);
528
529                                         ex_stat |= E_COMPARE;
530
531                                         if (a_opt)
532                                                 kill(0, SIGINT);
533
534                                         break;
535
536                                 case E_USAGE:
537                                         doio_fprintf(stderr,
538                                                      "(parent) pid %d exited because of a usage error\n",
539                                                      pid);
540
541                                         ex_stat |= E_USAGE;
542                                         break;
543
544                                 default:
545                                         doio_fprintf(stderr,
546                                                      "(parent) pid %d exited with unknown status %d\n",
547                                                      pid, WEXITSTATUS(stat));
548                                         ex_stat |= E_INTERNAL;
549                                         break;
550                                 }
551                         } else if (WIFSIGNALED(stat) && WTERMSIG(stat) != SIGINT) {
552                                 doio_fprintf(stderr,
553                                              "(parent) pid %d terminated by signal %d\n",
554                                              pid, WTERMSIG(stat));
555                                 
556                                 ex_stat |= E_SIGNAL;
557                         }
558                         
559                         fflush(NULL);
560                 }
561         }
562
563         exit(ex_stat);
564
565 }  /* main */
566
567 /*
568  * main doio function.  Each doio child starts here, and never returns.
569  */
570
571 void
572 doio()
573 {
574         int                     rval, i, infd, nbytes;
575         char                    *cp;
576         struct io_req           ioreq;
577         struct sigaction        sa, def_action, ignore_action, exit_action;
578 #ifndef CRAY
579         struct sigaction        sigbus_action;
580 #endif
581
582         Memsize = Sdssize = 0;
583
584         /*
585          * Initialize the Pattern - write-type syscalls will replace Pattern[1]
586          * with the pattern passed in the request.  Make sure that
587          * strlen(Pattern) is not mod 16 so that out of order words will be
588          * detected.
589          */
590
591         gethostname(Host, sizeof(Host));
592         if ((cp = strchr(Host, '.')) != NULL)
593                 *cp = '\0';
594
595         Pattern_Length = sprintf(Pattern, "-:%d:%s:%s*", (int)getpid(), Host, Prog);
596
597         if (!(Pattern_Length % 16)) {
598                 Pattern_Length = sprintf(Pattern, "-:%d:%s:%s**",
599                                          (int)getpid(), Host, Prog);
600         }
601
602         /*
603          * Open a couple of descriptors for the write-log file.  One descriptor
604          * is for appending, one for random access.  Write logging is done for
605          * file corruption detection.  The program doio_check is capable of
606          * doing corruption detection based on a doio write-log.
607          */
608
609         if (w_opt) {
610
611                 strcpy(Wlog.w_file, Write_Log);
612         
613                 if (wlog_open(&Wlog, 0, 0666) == -1) {
614                         doio_fprintf(stderr,
615                                      "Could not open write log file (%s): wlog_open() failed\n",
616                                      Write_Log);
617                         exit(E_SETUP);
618                 }
619         }
620
621         /*
622          * Open the input stream - either a file or stdin
623          */
624
625         if (Infile == NULL) {
626                 infd = 0;
627         } else {
628                 if ((infd = open(Infile, O_RDWR)) == -1) {
629                         doio_fprintf(stderr,
630                                      "Could not open input file (%s):  %s (%d)\n",
631                                      Infile, SYSERR, errno);
632                         exit(E_SETUP);
633                 }
634         }
635
636         /*
637          * Define a set of signals that should never be masked.  Receipt of
638          * these signals generally indicates a programming error, and we want
639          * a corefile at the point of error.  We put SIGQUIT in this list so
640          * that ^\ will force a user core dump.
641          *
642          * Note:  the handler for these should be SIG_DFL, all of them 
643          * produce a corefile as the default action.
644          */
645
646         ignore_action.sa_handler = SIG_IGN;
647         ignore_action.sa_flags = 0;
648         sigemptyset(&ignore_action.sa_mask);
649
650         def_action.sa_handler = SIG_DFL;
651         def_action.sa_flags = 0;
652         sigemptyset(&def_action.sa_mask);
653
654 #ifdef sgi
655         exit_action.sa_sigaction = cleanup_handler;
656         exit_action.sa_flags = SA_SIGINFO;
657         sigemptyset(&exit_action.sa_mask);
658
659         sa.sa_sigaction = die_handler;
660         sa.sa_flags = SA_SIGINFO;
661         sigemptyset(&sa.sa_mask);
662
663         sigbus_action.sa_sigaction = sigbus_handler;
664         sigbus_action.sa_flags = SA_SIGINFO;
665         sigemptyset(&sigbus_action.sa_mask);
666 #else
667         exit_action.sa_handler = cleanup_handler;
668         exit_action.sa_flags = 0;
669         sigemptyset(&exit_action.sa_mask);
670
671         sa.sa_handler = die_handler;
672         sa.sa_flags = 0;
673         sigemptyset(&sa.sa_mask);
674
675 #ifndef CRAY
676         sigbus_action.sa_handler = sigbus_handler;
677         sigbus_action.sa_flags = 0;
678         sigemptyset(&sigbus_action.sa_mask);
679 #endif
680 #endif
681
682         for (i = 1; i <= NSIG; i++) {
683                 switch(i) {
684                         /* Signals to terminate program on */
685                 case SIGINT:
686                         sigaction(i, &exit_action, NULL);
687                         break;
688
689 #ifndef CRAY
690                         /* This depends on active_mmap_rw */
691                 case SIGBUS:
692                         sigaction(i, &sigbus_action, NULL);
693                         break;
694 #endif
695
696                     /* Signals to Ignore... */
697                 case SIGSTOP:
698                 case SIGCONT:
699 #ifdef SIGRECOVERY
700                 case SIGRECOVERY:
701 #endif
702                         sigaction(i, &ignore_action, NULL);
703                         break;
704
705                     /* Signals to trap & report & die */
706                 /*case SIGTRAP:*/
707                 /*case SIGABRT:*/
708 #ifdef SIGERR   /* cray only signals */
709                 case SIGERR:
710                 case SIGBUFIO:
711                 case SIGINFO:
712 #endif
713                 /*case SIGFPE:*/
714                 case SIGURG:
715                 case SIGHUP:
716                 case SIGTERM:
717                 case SIGPIPE:
718                 case SIGIO:
719                 case SIGUSR1:
720                 case SIGUSR2:
721                         sigaction(i, &sa, NULL);
722                         break;
723
724
725                     /* Default Action for all other signals */
726                 default:
727                         sigaction(i, &def_action, NULL);
728                         break;
729                 }
730         }
731
732         /*
733          * Main loop - each doio proc does this until the read returns eof (0).
734          * Call the appropriate io function based on the request type.
735          */
736
737         while ((nbytes = read(infd, (char *)&ioreq, sizeof(ioreq)))) {
738
739                 /*
740                  * Periodically check our ppid.  If it is 1, the child exits to
741                  * help clean up in the case that the main doio process was
742                  * killed.
743                  */
744
745                 if (Reqno && ((Reqno % PPID_CHECK_INTERVAL) == 0)) {
746                         if (getppid() == 1) {
747                                 doio_fprintf(stderr,
748                                              "Parent doio process has exited\n");
749                                 alloc_mem(-1);
750                                 exit(E_SETUP);
751                         }
752                 }
753
754                 if (nbytes == -1) {
755                         doio_fprintf(stderr,
756                                      "read of %d bytes from input failed:  %s (%d)\n",
757                                      sizeof(ioreq), SYSERR, errno);
758                         alloc_mem(-1);
759                         exit(E_SETUP);
760                 }
761
762                 if (nbytes != sizeof(ioreq)) {
763                         doio_fprintf(stderr,
764                                      "read wrong # bytes from input stream, expected %d, got %d\n",
765                                      sizeof(ioreq), nbytes);
766                         alloc_mem(-1);
767                         exit(E_SETUP);
768                 }
769
770                 if (ioreq.r_magic != DOIO_MAGIC) {
771                         doio_fprintf(stderr,
772                                      "got a bad magic # from input stream.  Expected 0%o, got 0%o\n",
773                                      DOIO_MAGIC, ioreq.r_magic);
774                         alloc_mem(-1);
775                         exit(E_SETUP);
776                 }
777
778                 /*
779                  * If we're on a Release_Interval multiple, relase all ssd and
780                  * core space, and close all fd's in Fd_Map[].
781                  */
782
783                 if (Reqno && Release_Interval && ! (Reqno%Release_Interval)) {
784                         if (Memsize) {
785 #ifdef NOTDEF
786                                 sbrk(-1 * Memsize);
787 #else
788                                 alloc_mem(-1);
789 #endif
790                         }
791
792 #ifdef _CRAY1
793                         if (Sdssize) {
794                                 ssbreak(-1 * btoc(Sdssize));
795                                 Sdsptr = 0;
796                                 Sdssize = 0;
797                         }
798 #endif /* _CRAY1 */
799
800                         alloc_fd(NULL, 0);
801                 }
802
803                 switch (ioreq.r_type) {
804                 case READ:
805                 case READA:
806                         rval = do_read(&ioreq);
807                         break;
808
809                 case WRITE:
810                 case WRITEA:
811                         rval = do_write(&ioreq);
812                         break;
813
814                 case READV:
815                 case AREAD:
816                 case PREAD:
817                 case LREAD:
818                 case LREADA:
819                 case LSREAD:
820                 case LSREADA:
821                 case WRITEV:
822                 case AWRITE:
823                 case PWRITE:
824                 case MMAPR:
825                 case MMAPW:
826                 case LWRITE:
827                 case LWRITEA:
828                 case LSWRITE:
829                 case LSWRITEA:
830                 case LEREAD:
831                 case LEREADA:
832                 case LEWRITE:
833                 case LEWRITEA:
834                         rval = do_rw(&ioreq);
835                         break;
836
837 #ifdef CRAY
838                 case SSREAD:
839                 case SSWRITE:
840                         rval = do_ssdio(&ioreq);
841                         break;
842
843                 case LISTIO:
844                         rval = do_listio(&ioreq);
845                         break;
846 #endif
847
848 #ifndef NO_XFS
849                 case RESVSP:
850                 case UNRESVSP:
851                         rval = do_xfsctl(&ioreq);
852                         break;
853 #endif
854
855 #ifndef CRAY
856                 case FSYNC2:
857                 case FDATASYNC:
858                         rval = do_sync(&ioreq);
859                         break;
860 #endif
861                 default:
862                         doio_fprintf(stderr,
863                                      "Don't know how to handle io request type %d\n",
864                                      ioreq.r_type);
865                         alloc_mem(-1);
866                         exit(E_SETUP);
867                 }
868
869                 if (rval == SKIP_REQ){
870                         Reqskipcnt++;
871                 }
872                 else if (rval != 0) {
873                         alloc_mem(-1);
874                         doio_fprintf(stderr,
875                                      "doio(): operation %d returned != 0\n",
876                                      ioreq.r_type);
877                         exit(E_SETUP);
878                 }
879
880                 if (Message_Interval && Reqno % Message_Interval == 0) {
881                         doio_fprintf(stderr, "Info:  %d requests done (%d skipped) by this process\n", Reqno, Reqskipcnt);
882                 }
883
884                 Reqno++;
885
886                 if(delayop != 0)
887                         doio_delay();
888         }
889
890         /*
891          * Child exits normally
892          */
893         alloc_mem(-1);
894         exit(E_NORMAL);
895
896 }  /* doio */
897
898 void
899 doio_delay()
900 {
901         struct timeval tv_delay;
902         struct sigaction sa_al, sa_old;
903         sigset_t al_mask;
904
905         switch(delayop) {
906         case DELAY_SELECT:
907                 tv_delay.tv_sec = delaytime / 1000000;
908                 tv_delay.tv_usec = delaytime % 1000000;
909                 /*doio_fprintf(stdout, "delay_select: %d %d\n", 
910                             tv_delay.tv_sec, tv_delay.tv_usec);*/
911                 select(0, NULL, NULL, NULL, &tv_delay);
912                 break;
913
914         case DELAY_SLEEP:
915                 sleep(delaytime);
916                 break;
917
918 #ifdef sgi
919         case DELAY_SGINAP:
920                 sginap(delaytime);
921                 break;
922 #endif
923
924         case DELAY_ALARM:
925                 sa_al.sa_flags = 0;
926                 sa_al.sa_handler = noop_handler;
927                 sigemptyset(&sa_al.sa_mask);
928                 sigaction(SIGALRM, &sa_al, &sa_old);
929                 sigemptyset(&al_mask);
930                 alarm(delaytime);
931                 sigsuspend(&al_mask);
932                 sigaction(SIGALRM, &sa_old, 0);
933                 break;
934         }
935 }
936
937
938 /*
939  * Format IO requests, returning a pointer to the formatted text.
940  *
941  * format_strat - formats the async i/o completion strategy
942  * format_rw    - formats a read[a]/write[a] request
943  * format_sds   - formats a ssread/sswrite request
944  * format_listio- formats a listio request
945  *
946  * ioreq is the doio io request structure.
947  */
948
949 struct smap sysnames[] = {
950         { "READ",       READ            },
951         { "WRITE",      WRITE           },
952         { "READA",      READA           },
953         { "WRITEA",     WRITEA          },
954         { "SSREAD",     SSREAD          },
955         { "SSWRITE",    SSWRITE         },
956         { "LISTIO",     LISTIO          },
957         { "LREAD",      LREAD           },
958         { "LREADA",     LREADA          },
959         { "LWRITE",     LWRITE          },
960         { "LWRITEA",    LWRITEA         },
961         { "LSREAD",     LSREAD          },
962         { "LSREADA",    LSREADA         },
963         { "LSWRITE",    LSWRITE         },
964         { "LSWRITEA",   LSWRITEA        },
965
966         /* Irix System Calls */
967         { "PREAD",      PREAD           },
968         { "PWRITE",     PWRITE          },
969         { "AREAD",      AREAD           },
970         { "AWRITE",     AWRITE          },
971         { "LLREAD",     LLREAD          },
972         { "LLAREAD",    LLAREAD         },
973         { "LLWRITE",    LLWRITE         },
974         { "LLAWRITE",   LLAWRITE        },
975         { "RESVSP",     RESVSP          },
976         { "UNRESVSP",   UNRESVSP        },
977
978         /* Irix and Linux System Calls */
979         { "READV",      READV           },
980         { "WRITEV",     WRITEV          },
981         { "MMAPR",      MMAPR           },
982         { "MMAPW",      MMAPW           },
983         { "FSYNC2",     FSYNC2          },
984         { "FDATASYNC",  FDATASYNC       },
985
986         { "unknown",    -1              },
987 };      
988
989 struct smap aionames[] = {
990         { "poll",       A_POLL          },
991         { "signal",     A_SIGNAL        },
992         { "recall",     A_RECALL        },
993         { "recalla",    A_RECALLA       },
994         { "recalls",    A_RECALLS       },
995         { "suspend",    A_SUSPEND       },
996         { "callback",   A_CALLBACK      },
997         { "synch",      0               },
998         { "unknown",    -1              },
999 };
1000
1001 char *
1002 format_oflags(int oflags)
1003 {
1004         char flags[255];
1005
1006
1007         flags[0]='\0';
1008         switch(oflags & 03) {
1009         case O_RDONLY:          strcat(flags,"O_RDONLY,");      break;
1010         case O_WRONLY:          strcat(flags,"O_WRONLY,");      break;
1011         case O_RDWR:            strcat(flags,"O_RDWR,");        break;
1012         default:                strcat(flags,"O_weird");        break;
1013         }
1014
1015         if(oflags & O_EXCL)
1016                 strcat(flags,"O_EXCL,");
1017
1018         if(oflags & O_SYNC)
1019                 strcat(flags,"O_SYNC,");
1020 #ifdef CRAY
1021         if(oflags & O_RAW)
1022                 strcat(flags,"O_RAW,");
1023         if(oflags & O_WELLFORMED)
1024                 strcat(flags,"O_WELLFORMED,");
1025 #ifdef O_SSD
1026         if(oflags & O_SSD)
1027                 strcat(flags,"O_SSD,");
1028 #endif
1029         if(oflags & O_LDRAW)
1030                 strcat(flags,"O_LDRAW,");
1031         if(oflags & O_PARALLEL)
1032                 strcat(flags,"O_PARALLEL,");
1033         if(oflags & O_BIG)
1034                 strcat(flags,"O_BIG,");
1035         if(oflags & O_PLACE)
1036                 strcat(flags,"O_PLACE,");
1037         if(oflags & O_ASYNC)
1038                 strcat(flags,"O_ASYNC,");
1039 #endif
1040
1041         if(oflags & O_DIRECT)
1042                 strcat(flags,"O_DIRECT,");
1043 #ifdef sgi
1044         if(oflags & O_DSYNC)
1045                 strcat(flags,"O_DSYNC,");
1046         if(oflags & O_RSYNC)
1047                 strcat(flags,"O_RSYNC,");
1048 #endif
1049
1050         return(strdup(flags));
1051 }
1052
1053 char *
1054 format_strat(int strategy)
1055 {
1056         char msg[64];
1057         char *aio_strat;
1058
1059         switch (strategy) {
1060         case A_POLL:            aio_strat = "POLL";     break;
1061         case A_SIGNAL:          aio_strat = "SIGNAL";   break;
1062         case A_RECALL:          aio_strat = "RECALL";   break;
1063         case A_RECALLA:         aio_strat = "RECALLA";  break;
1064         case A_RECALLS:         aio_strat = "RECALLS";  break;
1065         case A_SUSPEND:         aio_strat = "SUSPEND";  break;
1066         case A_CALLBACK:        aio_strat = "CALLBACK"; break;
1067         case 0:                 aio_strat = "<zero>";   break;
1068         default:
1069                 sprintf(msg, "<error:%#o>", strategy);
1070                 aio_strat = strdup(msg);
1071                 break;
1072         }
1073
1074         return(aio_strat);
1075 }
1076
1077 char *
1078 format_rw(
1079         struct  io_req  *ioreq,
1080         int             fd,
1081         void            *buffer,
1082         int             signo,
1083         char            *pattern,
1084 #ifdef CRAY
1085         struct  iosw    *iosw
1086 #else
1087         void            *iosw
1088 #endif
1089         )
1090 {
1091         static char             *errbuf=NULL;
1092         char                    *aio_strat, *cp;
1093         struct read_req         *readp = &ioreq->r_data.read;
1094         struct write_req        *writep = &ioreq->r_data.write;
1095         struct read_req         *readap = &ioreq->r_data.read;
1096         struct write_req        *writeap = &ioreq->r_data.write;
1097
1098         if(errbuf == NULL)
1099                 errbuf = (char *)malloc(32768);
1100
1101         cp = errbuf;
1102         cp += sprintf(cp, "Request number %d\n", Reqno);
1103
1104         switch (ioreq->r_type) {
1105         case READ:
1106                 cp += sprintf(cp, "syscall:  read(%d, %#lo, %d)\n",
1107                               fd, (unsigned long) buffer, readp->r_nbytes);
1108                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
1109                               fd, readp->r_file, readp->r_oflags);
1110                 cp += sprintf(cp, "          read done at file offset %d\n",
1111                               readp->r_offset);
1112                 break;
1113
1114         case WRITE:
1115                 cp += sprintf(cp, "syscall:  write(%d, %#lo, %d)\n",
1116                               fd, (unsigned long) buffer, writep->r_nbytes);
1117                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
1118                               fd, writep->r_file, writep->r_oflags);
1119                 cp += sprintf(cp, "          write done at file offset %d - pattern is %s\n",
1120                               writep->r_offset, pattern);
1121                 break;
1122
1123         case READA:
1124                 aio_strat = format_strat(readap->r_aio_strat);
1125
1126                 cp += sprintf(cp, "syscall:  reada(%d, %#lo, %d, %#lo, %d)\n",
1127                               fd, (unsigned long) buffer, readap->r_nbytes,
1128                               (unsigned long) iosw, signo);
1129                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
1130                               fd, readap->r_file, readp->r_oflags);
1131                 cp += sprintf(cp, "          reada done at file offset %d\n",
1132                               readap->r_offset);
1133                 cp += sprintf(cp, "          async io completion strategy is %s\n",
1134                               aio_strat);
1135                 break;
1136
1137         case WRITEA:
1138                 aio_strat = format_strat(writeap->r_aio_strat);
1139
1140                 cp += sprintf(cp, "syscall:  writea(%d, %#lo, %d, %#lo, %d)\n",
1141                               fd, (unsigned long) buffer, writeap->r_nbytes,
1142                               (unsigned long) iosw, signo);
1143                 cp += sprintf(cp, "          fd %d is file %s - open flags are %#o\n",
1144                               fd, writeap->r_file, writeap->r_oflags);
1145                 cp += sprintf(cp, "          writea done at file offset %d - pattern is %s\n",
1146                               writeap->r_offset, pattern);
1147                 cp += sprintf(cp, "          async io completion strategy is %s\n",
1148                               aio_strat);
1149                 break;
1150
1151         }
1152
1153         return errbuf;
1154 }
1155
1156 #ifdef CRAY
1157 char *
1158 format_sds(
1159         struct  io_req  *ioreq,
1160         void            *buffer,
1161         int             sds,
1162         char            *pattern
1163         )
1164 {
1165         int                     i;
1166         static char             *errbuf=NULL;
1167         char                    *cp;
1168
1169         struct ssread_req       *ssreadp = &ioreq->r_data.ssread;
1170         struct sswrite_req      *sswritep = &ioreq->r_data.sswrite;
1171
1172         if(errbuf == NULL)
1173                 errbuf = (char *)malloc(32768);
1174
1175         cp = errbuf;
1176         cp += sprintf(cp, "Request number %d\n", Reqno);
1177
1178
1179         switch (ioreq->r_type) {
1180         case SSREAD:
1181                 cp += sprintf(cp, "syscall:  ssread(%#o, %#o, %d)\n",
1182                               buffer, sds, ssreadp->r_nbytes);
1183                 break;
1184
1185         case SSWRITE:
1186                 cp += sprintf(cp, "syscall:  sswrite(%#o, %#o, %d) - pattern was %s\n",
1187                               buffer, sds, sswritep->r_nbytes, pattern);
1188                 break;
1189         }
1190         return errbuf;
1191 }
1192 #endif /* CRAY */
1193
1194 /*
1195  * Perform the various sorts of disk reads
1196  */
1197
1198 int
1199 do_read(req)
1200 struct io_req   *req;
1201 {
1202         int                     fd, offset, nbytes, oflags, rval;
1203         char                    *addr, *file;
1204 #ifdef CRAY
1205         struct aio_info         *aiop;
1206         int                     aio_id, aio_strat, signo;
1207 #endif
1208 #ifndef NO_XFS
1209         struct fd_cache         *fdc;
1210 #endif
1211
1212         /*
1213          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
1214          * r_nbytes are at the same offset in the read_req and reada_req
1215          * structures.
1216          */
1217
1218         file = req->r_data.read.r_file;
1219         oflags = req->r_data.read.r_oflags;
1220         offset = req->r_data.read.r_offset;
1221         nbytes = req->r_data.read.r_nbytes;
1222
1223         /*printf("read: %s, %#o, %d %d\n", file, oflags, offset, nbytes);*/
1224
1225         /*
1226          * Grab an open file descriptor
1227          * Note: must be done before memory allocation so that the direct i/o
1228          *      information is available in mem. allocate
1229          */
1230
1231         if ((fd = alloc_fd(file, oflags)) == -1)
1232                 return -1;
1233
1234         /*
1235          * Allocate core or sds - based on the O_SSD flag
1236          */
1237
1238 #ifndef wtob
1239 #define wtob(x) (x * sizeof(UINT64_T))
1240 #endif
1241
1242 #ifdef CRAY
1243         if (oflags & O_SSD) {
1244                 if (alloc_sds(nbytes) == -1)
1245                         return -1;
1246
1247                 addr = (char *)Sdsptr;
1248         } else {
1249                 if ((rval = alloc_mem(nbytes + wtob(1) * 2 + MPP_BUMP * sizeof(UINT64_T))) < 0) {
1250                         return rval;
1251                 }
1252
1253                 addr = Memptr;
1254
1255                 /*
1256                  * if io is not raw, bump the offset by a random amount
1257                  * to generate non-word-aligned io.
1258                  */
1259                 if (! (req->r_data.read.r_uflags & F_WORD_ALIGNED)) {
1260                         addr += random_range(0, wtob(1) - 1, 1, NULL);
1261                 }
1262         }
1263 #else
1264 #ifndef NO_XFS
1265         /* get memory alignment for using DIRECT I/O */
1266         fdc = alloc_fdcache(file, oflags);
1267
1268         if ((rval = alloc_mem(nbytes + wtob(1) * 2 + fdc->c_memalign)) < 0) {
1269                 return rval;
1270         }
1271
1272         addr = Memptr;
1273
1274
1275         if( (req->r_data.read.r_uflags & F_WORD_ALIGNED) ) {
1276                 /*
1277                  * Force memory alignment for Direct I/O
1278                  */
1279                 if( (oflags & O_DIRECT) && ((long)addr % fdc->c_memalign != 0) ) {
1280                         addr += fdc->c_memalign - ((long)addr % fdc->c_memalign);
1281                 }
1282         } else {
1283                 addr += random_range(0, wtob(1) - 1, 1, NULL);
1284         }
1285 #else
1286         if ((rval = alloc_mem(nbytes + wtob(1) * 2)) < 0) {
1287                 return rval;
1288         }
1289
1290         addr = Memptr;
1291 #endif  /* !CRAY && sgi */
1292 #endif  /* CRAY */
1293
1294
1295         switch (req->r_type) {
1296         case READ:
1297                 /* move to the desired file position. */
1298                 if (lseek(fd, offset, SEEK_SET) == -1) {
1299                         doio_fprintf(stderr,
1300                                      "lseek(%d, %d, SEEK_SET) failed:  %s (%d)\n",
1301                                      fd, offset, SYSERR, errno);
1302                         return -1;
1303                 }
1304
1305                 if ((rval = read(fd, addr, nbytes)) == -1) {
1306                         doio_fprintf(stderr,
1307                                      "read() request failed:  %s (%d)\n%s\n",
1308                                      SYSERR, errno,
1309                                      format_rw(req, fd, addr, -1, NULL, NULL));
1310                         doio_upanic(U_RVAL);
1311                         return -1;
1312                 } else if (rval != nbytes) {
1313                         doio_fprintf(stderr,
1314                                      "read() request returned wrong # of bytes - expected %d, got %d\n%s\n",
1315                                      nbytes, rval, 
1316                                      format_rw(req, fd, addr, -1, NULL, NULL));
1317                         doio_upanic(U_RVAL);
1318                         return -1;
1319                 }
1320                 break;
1321
1322 #ifdef CRAY
1323         case READA:
1324                 /*
1325                  * Async read
1326                  */
1327
1328                 /* move to the desired file position. */
1329                 if (lseek(fd, offset, SEEK_SET) == -1) {
1330                         doio_fprintf(stderr,
1331                                      "lseek(%d, %d, SEEK_SET) failed:  %s (%d)\n",
1332                                      fd, offset, SYSERR, errno);
1333                         return -1;
1334                 }
1335
1336                 aio_strat = req->r_data.read.r_aio_strat;
1337                 signo = (aio_strat == A_SIGNAL) ? SIGUSR1 : 0;
1338
1339                 aio_id = aio_register(fd, aio_strat, signo);
1340                 aiop = aio_slot(aio_id);
1341
1342                 if (reada(fd, addr, nbytes, &aiop->iosw, signo) == -1) {
1343                         doio_fprintf(stderr, "reada() failed: %s (%d)\n%s\n",
1344                                      SYSERR, errno,
1345                                      format_rw(req, fd, addr, signo, NULL, &aiop->iosw));
1346                         aio_unregister(aio_id);
1347                         doio_upanic(U_RVAL);
1348                         rval = -1;
1349                 } else {
1350                         /*
1351                          * Wait for io to complete
1352                          */
1353
1354                         aio_wait(aio_id);
1355
1356                         /*
1357                          * make sure the io completed without error
1358                          */
1359
1360                         if (aiop->iosw.sw_count != nbytes) {
1361                                 doio_fprintf(stderr,
1362                                              "Bad iosw from reada()\nExpected (%d,%d,%d), got (%d,%d,%d)\n%s\n",
1363                                              1, 0, nbytes,
1364                                              aiop->iosw.sw_flag,
1365                                              aiop->iosw.sw_error,
1366                                              aiop->iosw.sw_count,
1367                                      format_rw(req, fd, addr, signo, NULL, &aiop->iosw));
1368                                 aio_unregister(aio_id);
1369                                 doio_upanic(U_IOSW);
1370                                 rval = -1;
1371                         } else {
1372                                 aio_unregister(aio_id);
1373                                 rval = 0;
1374                         }
1375                 }
1376
1377                 if (rval == -1)
1378                         return rval;
1379                 break;
1380 #endif  /* CRAY */
1381         }
1382
1383         return 0;               /* if we get here, everything went ok */
1384 }
1385
1386 /*
1387  * Perform the verious types of disk writes.
1388  */
1389
1390 int
1391 do_write(req)
1392 struct io_req   *req;
1393 {
1394         static int              pid = -1;
1395         int                     fd, nbytes, oflags;
1396         /* REFERENCED */
1397         int                     signo;
1398         int                     logged_write, rval, got_lock;
1399         long                    offset, woffset = 0;
1400         char                    *addr, pattern, *file, *msg;
1401         struct wlog_rec         wrec;
1402 #ifdef CRAY
1403         int                     aio_strat, aio_id;
1404         struct aio_info         *aiop;
1405 #endif
1406 #ifndef NO_XFS
1407         struct fd_cache         *fdc;
1408 #endif
1409
1410         /*
1411          * Misc variable setup
1412          */
1413
1414         signo   = 0;
1415         nbytes  = req->r_data.write.r_nbytes;
1416         offset  = req->r_data.write.r_offset;
1417         pattern = req->r_data.write.r_pattern;
1418         file    = req->r_data.write.r_file;
1419         oflags  = req->r_data.write.r_oflags;
1420
1421         /*printf("pwrite: %s, %#o, %d %d\n", file, oflags, offset, nbytes);*/
1422
1423         /*
1424          * Allocate core memory and possibly sds space.  Initialize the data
1425          * to be written.
1426          */
1427
1428         Pattern[0] = pattern;
1429
1430
1431         /*
1432          * Get a descriptor to do the io on
1433          */
1434
1435         if ((fd = alloc_fd(file, oflags)) == -1)
1436                 return -1;
1437
1438         /*printf("write: %d, %s, %#o, %d %d\n",
1439                fd, file, oflags, offset, nbytes);*/
1440
1441         /*
1442          * Allocate SDS space for backdoor write if desired
1443          */
1444
1445 #ifdef CRAY
1446         if (oflags & O_SSD) {
1447 #ifndef _CRAYMPP
1448                 if ((rval = alloc_mem(nbytes + wtob(1))) < 0) {
1449                         return rval;
1450                 }
1451
1452                 (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
1453                 /*pattern_fill(Memptr, nbytes, Pattern, Pattern_Length, 0);*/
1454
1455                 if (alloc_sds(nbytes) == -1)
1456                         return -1;
1457
1458                 if (sswrite((long)Memptr, Sdsptr, btoc(nbytes)) == -1) {
1459                         doio_fprintf(stderr, "sswrite(%d, %d, %d) failed:  %s (%d)\n",
1460                                      (long)Memptr, Sdsptr, btoc(nbytes), 
1461                                      SYSERR, errno);
1462                         fflush(stderr);
1463                         return -1;
1464                 }
1465
1466                 addr = (char *)Sdsptr;
1467 #else
1468                 doio_fprintf(stderr, "Invalid O_SSD flag was generated for MPP system\n");
1469                 fflush(stderr);
1470                 return -1;
1471 #endif /* !CRAYMPP */
1472         } else {
1473                 if ((rval = alloc_mem(nbytes + wtob(1)) < 0)) {
1474                         return rval;
1475                 }
1476
1477                 addr = Memptr;
1478
1479                 /*
1480                  * if io is not raw, bump the offset by a random amount
1481                  * to generate non-word-aligned io.
1482                  */
1483
1484                 if (! (req->r_data.write.r_uflags & F_WORD_ALIGNED)) {
1485                         addr += random_range(0, wtob(1) - 1, 1, NULL);
1486                 }
1487
1488                 (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
1489                 if( addr != Memptr )
1490                         memmove( addr, Memptr, nbytes);
1491         }
1492 #else /* CRAY */
1493 #ifndef NO_XFS
1494         /* get memory alignment for using DIRECT I/O */
1495         fdc = alloc_fdcache(file, oflags);
1496
1497         if ((rval = alloc_mem(nbytes + wtob(1) * 2 + fdc->c_memalign)) < 0) {
1498                 return rval;
1499         }
1500
1501         addr = Memptr;
1502
1503         if( (req->r_data.write.r_uflags & F_WORD_ALIGNED) ) {
1504                 /*
1505                  * Force memory alignment for Direct I/O
1506                  */
1507                 if( (oflags & O_DIRECT) && ((long)addr % fdc->c_memalign != 0) ) {
1508                         addr += fdc->c_memalign - ((long)addr % fdc->c_memalign);
1509                 }
1510         } else {
1511                 addr += random_range(0, wtob(1) - 1, 1, NULL);
1512         }
1513
1514         (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
1515         if( addr != Memptr )
1516                 memmove( addr, Memptr, nbytes);
1517
1518 #else /* sgi */
1519         if ((rval = alloc_mem(nbytes + wtob(1) * 2)) < 0) {
1520                 return rval;
1521         }
1522
1523         addr = Memptr;
1524
1525         (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
1526         if( addr != Memptr )
1527                 memmove( addr, Memptr, nbytes);
1528 #endif /* sgi */
1529 #endif /* CRAY */
1530
1531         rval = -1;
1532         got_lock = 0;
1533         logged_write = 0;
1534
1535         if (k_opt) {
1536                 if (lock_file_region(file, fd, F_WRLCK, offset, nbytes) < 0) {
1537                         alloc_mem(-1);
1538                         exit(E_INTERNAL);
1539                 }
1540
1541                 got_lock = 1;
1542         }
1543
1544         /*
1545          * Write a preliminary write-log entry.  This is done so that
1546          * doio_check can do corruption detection across an interrupt/crash.
1547          * Note that w_done is set to 0.  If doio_check sees this, it
1548          * re-creates the file extents as if the write completed, but does not
1549          * do any checking - see comments in doio_check for more details.
1550          */
1551
1552         if (w_opt) {
1553                 if (pid == -1) {
1554                         pid = getpid();
1555                 }
1556                 wrec.w_async = (req->r_type == WRITEA) ? 1 : 0;
1557                 wrec.w_oflags = oflags;
1558                 wrec.w_pid = pid;
1559                 wrec.w_offset = offset;
1560                 wrec.w_nbytes = nbytes;
1561
1562                 wrec.w_pathlen = strlen(file);
1563                 memcpy(wrec.w_path, file, wrec.w_pathlen);
1564                 wrec.w_hostlen = strlen(Host);
1565                 memcpy(wrec.w_host, Host, wrec.w_hostlen);
1566                 wrec.w_patternlen = Pattern_Length;
1567                 memcpy(wrec.w_pattern, Pattern, wrec.w_patternlen);
1568
1569                 wrec.w_done = 0;
1570
1571                 if ((woffset = wlog_record_write(&Wlog, &wrec, -1)) == -1) {
1572                         doio_fprintf(stderr,
1573                                      "Could not append to write-log:  %s (%d)\n",
1574                                      SYSERR, errno);
1575                 } else {
1576                         logged_write = 1;
1577                 }
1578         }
1579
1580         switch (req->r_type ) {
1581         case WRITE:
1582                 /*
1583                  * sync write
1584                  */
1585
1586                 if (lseek(fd, offset, SEEK_SET) == -1) {
1587                         doio_fprintf(stderr,
1588                                      "lseek(%d, %d, SEEK_SET) failed:  %s (%d)\n",
1589                                      fd, offset, SYSERR, errno);
1590                         return -1;
1591                 }
1592
1593                 rval = write(fd, addr, nbytes);
1594
1595                 if (rval == -1) {
1596                         doio_fprintf(stderr,
1597                                      "write() failed:  %s (%d)\n%s\n",
1598                                      SYSERR, errno,
1599                                      format_rw(req, fd, addr, -1, Pattern, NULL));
1600 #ifndef NO_XFS
1601                         doio_fprintf(stderr,
1602                                      "write() failed:  %s\n\twrite(%d, %#o, %d)\n\toffset %d, nbytes%%miniou(%d)=%d, oflags=%#o memalign=%d, addr%%memalign=%d\n",
1603                                      strerror(errno),
1604                                      fd, addr, nbytes,
1605                                      offset,
1606                                      fdc->c_miniosz, nbytes%fdc->c_miniosz,
1607                                      oflags, fdc->c_memalign, (long)addr%fdc->c_memalign);
1608 #else
1609                         doio_fprintf(stderr,
1610                                      "write() failed:  %s\n\twrite(%d, %#o, %d)\n\toffset %d, nbytes%%1B=%d, oflags=%#o\n",
1611                                      strerror(errno),
1612                                      fd, addr, nbytes,
1613                                      offset, nbytes%4096, oflags);
1614 #endif
1615                         doio_upanic(U_RVAL);
1616                 } else if (rval != nbytes) {
1617                         doio_fprintf(stderr,
1618                                      "write() returned wrong # bytes - expected %d, got %d\n%s\n",
1619                                      nbytes, rval,
1620                                      format_rw(req, fd, addr, -1, Pattern, NULL));
1621                         doio_upanic(U_RVAL);
1622                         rval = -1;
1623                 }
1624
1625                 break;
1626
1627 #ifdef CRAY
1628         case WRITEA:
1629                 /*
1630                  * async write
1631                  */
1632                 if (lseek(fd, offset, SEEK_SET) == -1) {
1633                         doio_fprintf(stderr,
1634                                      "lseek(%d, %d, SEEK_SET) failed:  %s (%d)\n",
1635                                      fd, offset, SYSERR, errno);
1636                         return -1;
1637                 }
1638
1639                 aio_strat = req->r_data.write.r_aio_strat;
1640                 signo = (aio_strat == A_SIGNAL) ? SIGUSR1 : 0;
1641
1642                 aio_id = aio_register(fd, aio_strat, signo);
1643                 aiop = aio_slot(aio_id);
1644
1645                 /*
1646                  * init iosw and do the async write
1647                  */
1648
1649                 if (writea(fd, addr, nbytes, &aiop->iosw, signo) == -1) {
1650                         doio_fprintf(stderr,
1651                                      "writea() failed: %s (%d)\n%s\n",
1652                                      SYSERR, errno,
1653                                      format_rw(req, fd, addr, -1, Pattern, NULL));
1654                         doio_upanic(U_RVAL);
1655                         aio_unregister(aio_id);
1656                         rval = -1;
1657                 } else {
1658
1659                         /*
1660                          * Wait for io to complete
1661                          */
1662
1663                         aio_wait(aio_id);
1664
1665                         /*
1666                          * check that iosw is ok
1667                          */
1668
1669                         if (aiop->iosw.sw_count != nbytes) {
1670                                 doio_fprintf(stderr,
1671                                              "Bad iosw from writea()\nExpected (%d,%d,%d), got (%d,%d,%d)\n%s\n",
1672                                              1, 0, nbytes,
1673                                              aiop->iosw.sw_flag,
1674                                              aiop->iosw.sw_error,
1675                                              aiop->iosw.sw_count,
1676                                              format_rw(req, fd, addr, -1, Pattern, &aiop->iosw));
1677                                 aio_unregister(aio_id);
1678                                 doio_upanic(U_IOSW);
1679                                 rval = -1;
1680                         } else {
1681                                 aio_unregister(aio_id);
1682                                 rval = 0;
1683                         }
1684                 }
1685                 break;
1686
1687 #endif /* CRAY */
1688         }
1689
1690         /*
1691          * Verify that the data was written correctly - check_file() returns
1692          * a non-null pointer which contains an error message if there are
1693          * problems.
1694          */
1695
1696         if (v_opt) {
1697                 msg = check_file(file, offset, nbytes, Pattern, Pattern_Length,
1698                                  0, oflags & O_PARALLEL);
1699                 if (msg != NULL) {
1700                         doio_fprintf(stderr, "%s%s\n",
1701                                      msg,
1702 #ifdef CRAY
1703                                      format_rw(req, fd, addr, -1, Pattern, &aiop->iosw)
1704 #else
1705                                      format_rw(req, fd, addr, -1, Pattern, NULL)
1706 #endif
1707                                 );
1708                         doio_upanic(U_CORRUPTION);
1709                         exit(E_COMPARE);
1710
1711                 }
1712         }
1713
1714         /*
1715          * General cleanup ...
1716          *
1717          * Write extent information to the write-log, so that doio_check can do
1718          * corruption detection.  Note that w_done is set to 1, indicating that
1719          * the write has been verified as complete.  We don't need to write the
1720          * filename on the second logging.
1721          */
1722
1723         if (w_opt && logged_write) {
1724                 wrec.w_done = 1;
1725                 wlog_record_write(&Wlog, &wrec, woffset);
1726         }
1727
1728         /*
1729          * Unlock file region if necessary
1730          */
1731
1732         if (got_lock) {
1733                 if (lock_file_region(file, fd, F_UNLCK, offset, nbytes) < 0) {
1734                         alloc_mem(-1);
1735                         exit(E_INTERNAL);
1736                 }
1737         }
1738
1739         return( (rval == -1) ? -1 : 0);
1740 }
1741
1742
1743 /*
1744  * Simple routine to lock/unlock a file using fcntl()
1745  */
1746
1747 int
1748 lock_file_region(fname, fd, type, start, nbytes)
1749 char    *fname;
1750 int     fd;
1751 int     type;
1752 int     start;
1753 int     nbytes;
1754 {
1755         struct flock    flk;
1756
1757         flk.l_type = type;
1758         flk.l_whence = 0;
1759         flk.l_start = start;
1760         flk.l_len = nbytes;
1761
1762         if (fcntl(fd, F_SETLKW, &flk) < 0) {
1763                 doio_fprintf(stderr,
1764                              "fcntl(%d, %d, %#o) failed for file %s, lock type %d, offset %d, length %d:  %s (%d), open flags: %#o\n",
1765                              fd, F_SETLKW, &flk, fname, type,
1766                              start, nbytes, SYSERR, errno,
1767                              fcntl(fd, F_GETFL, 0));
1768                 return -1;
1769         }
1770
1771         return 0;
1772 }
1773
1774 /*
1775  * Perform a listio request.
1776  */
1777
1778 #ifdef CRAY
1779 char *
1780 format_listio(
1781         struct  io_req  *ioreq,
1782         int             lcmd,
1783         struct listreq  *list,
1784         int             nent,
1785         int             fd,
1786         char            *pattern
1787         )
1788 {
1789         static  char            *errbuf=NULL;
1790         struct  listio_req      *liop = &ioreq->r_data.listio;
1791         struct  listreq         *listreq;
1792         char                    *cp, *cmd, *opcode, *aio_strat;
1793         int                     i;
1794
1795         switch (lcmd) {
1796         case LC_START:  cmd = "LC_START";       break;
1797         case LC_WAIT:   cmd = "LC_WAIT";        break;
1798         default:        cmd = "???";            break;
1799         }
1800
1801         if(errbuf == NULL)
1802                 errbuf = (char *)malloc(32768);
1803
1804         cp = errbuf;
1805         cp += sprintf(cp, "Request number %d\n", Reqno);
1806
1807         cp += sprintf(cp, "syscall:  listio(%s, %#o, %d)\n\n",
1808                       cmd, list, nent);
1809
1810         aio_strat = format_strat(liop->r_aio_strat);
1811
1812         for (i = 0; i < nent; i++) {
1813                 cp += sprintf(cp, "struct lioreq for request element %d\n", i);
1814                 cp += sprintf(cp, "----------------------------------------\n");
1815
1816                 listreq = list + i;
1817
1818                 switch (listreq->li_opcode) {
1819                 case LO_READ:   opcode = "LO_READ";     break;
1820                 case LO_WRITE:  opcode = "LO_WRITE";    break;
1821                 default:        opcode = "???";         break;
1822                 }
1823                         
1824                 cp += sprintf(cp, "          li_opcode =    %s\n", opcode);
1825                 cp += sprintf(cp, "          li_drvr =      %#o\n", listreq->li_drvr);
1826                 cp += sprintf(cp, "          li_flags =     %#o\n", listreq->li_flags);
1827                 cp += sprintf(cp, "          li_offset =    %d\n", listreq->li_offset);
1828                 cp += sprintf(cp, "          li_fildes =    %d\n", listreq->li_fildes);
1829                 cp += sprintf(cp, "          li_buf =       %#o\n", listreq->li_buf);
1830                 cp += sprintf(cp, "          li_nbyte =     %d\n", listreq->li_nbyte);
1831                 cp += sprintf(cp, "          li_status =    %#o (%d, %d, %d)\n", listreq->li_status, listreq->li_status->sw_flag, listreq->li_status->sw_error, listreq->li_status->sw_count);
1832                 cp += sprintf(cp, "          li_signo =     %d\n", listreq->li_signo);
1833                 cp += sprintf(cp, "          li_nstride =   %d\n", listreq->li_nstride);
1834                 cp += sprintf(cp, "          li_filstride = %d\n", listreq->li_filstride);
1835                 cp += sprintf(cp, "          li_memstride = %d\n", listreq->li_memstride);
1836                 cp += sprintf(cp, "          io completion strategy is %s\n", aio_strat);
1837         }
1838         return errbuf;
1839 }
1840 #endif /* CRAY */
1841
1842 int
1843 do_listio(req)
1844 struct io_req   *req;
1845 {
1846 #ifdef CRAY
1847         struct listio_req       *lio;
1848         int                     fd, oflags, signo, nb, i;
1849         int                     logged_write, rval, got_lock;
1850         int                     aio_strat, aio_id;
1851         int                     min_byte, max_byte;
1852         int                     mem_needed;
1853         int                     foffset, fstride, mstride, nstrides;
1854         char                    *moffset;
1855         long                    offset, woffset;
1856         char                    *addr, *msg;
1857         sigset_t                block_mask, omask;
1858         struct wlog_rec         wrec;
1859         struct aio_info         *aiop;
1860         struct listreq          lio_req;
1861
1862         lio = &req->r_data.listio;
1863
1864         /*
1865          * If bytes per stride is less than the stride size, drop the request
1866          * since it will cause overlapping strides, and we cannot predict
1867          * the order they will complete in.
1868          */
1869
1870         if (lio->r_filestride && abs(lio->r_filestride) < lio->r_nbytes) {
1871                 doio_fprintf(stderr, "do_listio():  Bogus listio request - abs(filestride) [%d] < nbytes [%d]\n",
1872                              abs(lio->r_filestride), lio->r_nbytes);
1873                 return -1;
1874         }
1875
1876         /*
1877          * Allocate core memory.  Initialize the data to be written.  Make
1878          * sure we get enough, based on the memstride.
1879          */
1880
1881         mem_needed = 
1882                 stride_bounds(0, lio->r_memstride, lio->r_nstrides,
1883                               lio->r_nbytes, NULL, NULL);
1884
1885         if ((rval = alloc_mem(mem_needed + wtob(1))) < 0) {
1886                 return rval;
1887         }
1888
1889         /*
1890          * Set the memory address pointer.  If the io is not raw, adjust
1891          * addr by a random amount, so that non-raw io is not necessarily
1892          * word aligned.
1893          */
1894
1895         addr = Memptr;
1896
1897         if (! (lio->r_uflags & F_WORD_ALIGNED)) {
1898                 addr += random_range(0, wtob(1) - 1, 1, NULL);
1899         }
1900
1901         if (lio->r_opcode == LO_WRITE) {
1902                 Pattern[0] = lio->r_pattern;
1903                 (*Data_Fill)(Memptr, mem_needed, Pattern, Pattern_Length, 0);
1904                 if( addr != Memptr )
1905                         memmove( addr, Memptr, mem_needed);
1906         }
1907
1908         /*
1909          * Get a descriptor to do the io on.  No need to do an lseek, as this
1910          * is encoded in the listio request.
1911          */
1912
1913         if ((fd = alloc_fd(lio->r_file, lio->r_oflags)) == -1) {
1914                 return -1;
1915         }
1916
1917         rval = -1;
1918         got_lock = 0;
1919         logged_write = 0;
1920
1921         /*
1922          * If the opcode is LO_WRITE, lock all regions of the file that
1923          * are touched by this listio request.  Currently, we use
1924          * stride_bounds() to figure out the min and max bytes affected, and
1925          * lock the entire region, regardless of the file stride.
1926          */
1927
1928         if (lio->r_opcode == LO_WRITE && k_opt) {
1929                 stride_bounds(lio->r_offset,
1930                               lio->r_filestride, lio->r_nstrides,
1931                               lio->r_nbytes, &min_byte, &max_byte);
1932
1933                 if (lock_file_region(lio->r_file, fd, F_WRLCK,
1934                                      min_byte, (max_byte-min_byte+1)) < 0) {
1935                         doio_fprintf(stderr, "stride_bounds(%d, %d, %d, %d, ..., ...) set min_byte to %d, max_byte to %d\n",
1936                                      lio->r_offset, lio->r_filestride,
1937                                      lio->r_nstrides, lio->r_nbytes, min_byte,
1938                                      max_byte);
1939                         return -1;
1940                 } else {
1941                         got_lock = 1;
1942                 }
1943         }
1944
1945         /*
1946          * async write
1947          */
1948
1949         aio_strat = lio->r_aio_strat;
1950         signo = (aio_strat == A_SIGNAL) ? SIGUSR1 : 0;
1951
1952         aio_id = aio_register(fd, aio_strat, signo);
1953         aiop = aio_slot(aio_id);
1954
1955         /*
1956          * Form the listio request, and make the call.
1957          */
1958
1959         lio_req.li_opcode = lio->r_opcode;
1960         lio_req.li_drvr = 0;
1961         lio_req.li_flags = LF_LSEEK;
1962         lio_req.li_offset = lio->r_offset;
1963         lio_req.li_fildes = fd;
1964
1965         if (lio->r_memstride >= 0 || lio->r_nstrides <= 1) {
1966                 lio_req.li_buf = addr;
1967         } else {
1968                 lio_req.li_buf = addr + mem_needed - lio->r_nbytes;
1969         }
1970
1971         lio_req.li_nbyte = lio->r_nbytes;
1972         lio_req.li_status = &aiop->iosw;
1973         lio_req.li_signo = signo;
1974         lio_req.li_nstride = lio->r_nstrides;
1975         lio_req.li_filstride = lio->r_filestride;
1976         lio_req.li_memstride = lio->r_memstride;
1977
1978         /*
1979          * If signo != 0, block signo while we're in the system call, so that
1980          * we don't get interrupted syscall failures.
1981          */
1982
1983         if (signo) {
1984                 sigemptyset(&block_mask);
1985                 sigaddset(&block_mask, signo);
1986                 sigprocmask(SIG_BLOCK, &block_mask, &omask);
1987         }
1988
1989         if (listio(lio->r_cmd, &lio_req, 1) < 0) {
1990                 doio_fprintf(stderr,
1991                              "listio() failed: %s (%d)\n%s\n",
1992                              SYSERR, errno,
1993                              format_listio(req, lio->r_cmd, &lio_req, 1, fd, Pattern));
1994                 aio_unregister(aio_id);
1995                 doio_upanic(U_RVAL);
1996                 goto lio_done;
1997         }
1998
1999         if (signo) {
2000                 sigprocmask(SIG_SETMASK, &omask, NULL);
2001         }
2002
2003         /*
2004          * Wait for io to complete
2005          */
2006
2007         aio_wait(aio_id);
2008
2009         nstrides = lio->r_nstrides ? lio->r_nstrides : 1;
2010         if (aiop->iosw.sw_count != lio->r_nbytes * nstrides) {
2011                 doio_fprintf(stderr,
2012                              "Bad iosw from listio()\nExpected (%d,%d,%d), got (%d,%d,%d)\n%s\n",
2013                              1, 0, lio->r_nbytes * lio->r_nstrides,
2014                              aiop->iosw.sw_flag,
2015                              aiop->iosw.sw_error, aiop->iosw.sw_count,
2016                              format_listio(req, lio->r_cmd, &lio_req, 1, fd, Pattern));
2017                 aio_unregister(aio_id);
2018                 doio_upanic(U_IOSW);
2019                 goto lio_done;
2020         } 
2021
2022         aio_unregister(aio_id);
2023
2024         /*
2025          * Verify that the data was written correctly - check_file() returns
2026          * a non-null pointer which contains an error message if there are
2027          * problems.
2028          *
2029          * For listio, we basically have to make 1 call to check_file for each
2030          * stride.
2031          */
2032
2033         if (v_opt && lio_req.li_opcode == LO_WRITE) {
2034                 fstride = lio->r_filestride ? lio->r_filestride : lio->r_nbytes;
2035                 mstride = lio->r_memstride ? lio->r_memstride : lio->r_nbytes;
2036                 foffset = lio->r_offset;
2037
2038                 if (mstride> 0 || lio->r_nstrides <= 1) {
2039                         moffset = addr;
2040                 } else {
2041                         moffset = addr + mem_needed - lio->r_nbytes;
2042                 }
2043
2044                 for (i = 0; i < lio_req.li_nstride; i++) {
2045                         msg = check_file(lio->r_file,
2046                                          foffset, lio->r_nbytes,
2047                                          Pattern, Pattern_Length,
2048                                          moffset - addr,
2049                                          lio->r_oflags & O_PARALLEL);
2050
2051                         if (msg != NULL) {
2052                                 doio_fprintf(stderr, "%s\n%s\n",
2053                                              msg,
2054                              format_listio(req, lio->r_cmd, &lio_req, 1, fd, Pattern));
2055                                 doio_upanic(U_CORRUPTION);
2056                                 exit(E_COMPARE);
2057                         }
2058
2059                         moffset += mstride;
2060                         foffset += fstride;
2061                 }
2062
2063         }
2064
2065         rval = 0;
2066
2067  lio_done:
2068
2069         /*
2070          * General cleanup ...
2071          *
2072          */
2073
2074         /*
2075          * Release file locks if necessary
2076          */
2077
2078         if (got_lock) {
2079                 if (lock_file_region(lio->r_file, fd, F_UNLCK,
2080                                      min_byte, (max_byte-min_byte+1)) < 0) {
2081                         return -1;
2082                 }
2083         }
2084
2085         return rval;
2086 #else
2087         return -1;
2088 #endif
2089 }
2090
2091 /*
2092  * perform ssread/sswrite operations
2093  */
2094
2095 #ifdef _CRAY1
2096
2097 int
2098 do_ssdio(req)
2099 struct io_req   *req;
2100 {
2101         int         nbytes, nb;
2102         char    errbuf[BSIZE];
2103
2104         nbytes = req->r_data.ssread.r_nbytes;
2105
2106         /*
2107          * Grab core and sds space
2108          */
2109
2110         if ((nb = alloc_mem(nbytes)) < 0)
2111                 return nb;
2112
2113         if (alloc_sds(nbytes) == -1)
2114                 return -1;
2115
2116         if (req->r_type == SSWRITE) {
2117
2118                 /*
2119                  * Init data and ship it to the ssd
2120                  */
2121
2122                 Pattern[0] = req->r_data.sswrite.r_pattern;
2123                 /*pattern_fill(Memptr, nbytes, Pattern, Pattern_Length, 0);*/
2124                 (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
2125
2126                 if (sswrite((long)Memptr, (long)Sdsptr, btoc(nbytes)) == -1) {
2127                         doio_fprintf(stderr, "sswrite() failed:  %s (%d)\n%s\n",
2128                                      SYSERR, errno,
2129                                      format_sds(req, Memptr, Sdsptr, Pattern));
2130                         doio_upanic(U_RVAL);
2131                         return -1;
2132                 }
2133         } else {
2134                 /*
2135                  * read from sds
2136                  */
2137
2138                 if (ssread((long)Memptr, (long)Sdsptr, btoc(nbytes)) == -1) {
2139                         doio_fprintf(stderr, "ssread() failed: %s (%d)\n%s\n",
2140                                      SYSERR, errno,
2141                                      format_sds(req, Memptr, Sdsptr, Pattern));
2142
2143                         doio_upanic(U_RVAL);
2144                         return -1;
2145                 }
2146         }
2147
2148         /*
2149          * Verify data if SSWRITE and v_opt
2150          */
2151
2152         if (v_opt && req->r_type == SSWRITE) {
2153                 ssread((long)Memptr, (long)Sdsptr, btoc(nbytes));
2154
2155                 if (pattern_check(Memptr, nbytes, Pattern, Pattern_Length, 0) == -1) {
2156                         doio_fprintf(stderr,
2157                                      "sds DATA COMPARE ERROR - ABORTING\n%s\n",
2158                                      format_sds(req, Memptr, Sdsptr, Pattern));
2159
2160                         doio_upanic(U_CORRUPTION);
2161                         exit(E_COMPARE);
2162                 }
2163         }
2164 }
2165
2166 #else
2167
2168 #ifdef CRAY
2169
2170 int
2171 do_ssdio(req)
2172 struct io_req   *req;
2173 {
2174         doio_fprintf(stderr,
2175                      "Internal Error - do_ssdio() called on a non-cray1 system\n");
2176         alloc_mem(-1);
2177         exit(E_INTERNAL);
2178 }
2179
2180 #endif
2181
2182 #endif /* _CRAY1 */
2183
2184 \f
2185 /* ---------------------------------------------------------------------------
2186  * 
2187  * A new paradigm of doing the r/w system call where there is a "stub"
2188  * function that builds the info for the system call, then does the system
2189  * call; this is called by code that is common to all system calls and does
2190  * the syscall return checking, async I/O wait, iosw check, etc.
2191  *
2192  * Flags:
2193  *      WRITE, ASYNC, SSD/SDS, 
2194  *      FILE_LOCK, WRITE_LOG, VERIFY_DATA,
2195  */
2196
2197 struct  status {
2198         int     rval;           /* syscall return */
2199         int     err;            /* errno */
2200         int     *aioid;         /* list of async I/O structures */
2201 };
2202
2203 struct syscall_info {
2204         char            *sy_name;
2205         int             sy_type;
2206         struct status   *(*sy_syscall)();
2207         int             (*sy_buffer)();
2208         char            *(*sy_format)();
2209         int             sy_flags;
2210         int             sy_bits;
2211 };
2212
2213 #define SY_WRITE                00001
2214 #define SY_ASYNC                00010
2215 #define SY_IOSW                 00020
2216 #define SY_SDS                  00100
2217
2218 char *
2219 fmt_ioreq(struct io_req *ioreq, struct syscall_info *sy, int fd)
2220 {
2221         static char             *errbuf=NULL;
2222         char                    *cp;
2223         struct rw_req           *io;
2224         struct smap             *aname;
2225 #ifdef CRAY
2226         struct stat             sbuf;
2227 #endif
2228
2229         if(errbuf == NULL)
2230                 errbuf = (char *)malloc(32768);
2231
2232         io = &ioreq->r_data.io;
2233
2234         /*
2235          * Look up async I/O completion strategy
2236          */
2237         for(aname=aionames;
2238             aname->value != -1 && aname->value != io->r_aio_strat;
2239             aname++)
2240                 ;
2241
2242         cp = errbuf;
2243         cp += sprintf(cp, "Request number %d\n", Reqno);
2244
2245         cp += sprintf(cp, "          fd %d is file %s - open flags are %#o %s\n",
2246                       fd, io->r_file, io->r_oflags, format_oflags(io->r_oflags));
2247
2248         if(sy->sy_flags & SY_WRITE) {
2249                 cp += sprintf(cp, "          write done at file offset %d - pattern is %c (%#o)\n",
2250                               io->r_offset,
2251                               (io->r_pattern == '\0') ? '?' : io->r_pattern, 
2252                               io->r_pattern);
2253         } else {
2254                 cp += sprintf(cp, "          read done at file offset %d\n",
2255                       io->r_offset);
2256         }
2257
2258         if(sy->sy_flags & SY_ASYNC) {
2259                 cp += sprintf(cp, "          async io completion strategy is %s\n",
2260                               aname->string);
2261         }
2262
2263         cp += sprintf(cp, "          number of requests is %d, strides per request is %d\n",
2264                       io->r_nent, io->r_nstrides);
2265
2266         cp += sprintf(cp, "          i/o byte count = %d\n",
2267                       io->r_nbytes);
2268
2269         cp += sprintf(cp, "          memory alignment is %s\n",
2270                       (io->r_uflags & F_WORD_ALIGNED) ? "aligned" : "unaligned");
2271
2272 #ifdef CRAY
2273         if(io->r_oflags & O_RAW) {
2274                 cp += sprintf(cp, "          RAW I/O: offset %% 4096 = %d length %% 4096 = %d\n",
2275                               io->r_offset % 4096, io->r_nbytes % 4096);
2276                 fstat(fd, &sbuf);
2277                 cp += sprintf(cp, "          optimal file xfer size: small: %d large: %d\n",
2278                               sbuf.st_blksize, sbuf.st_oblksize);
2279                 cp += sprintf(cp, "          cblks %d cbits %#o\n",
2280                               sbuf.st_cblks, sbuf.st_cbits);
2281         }
2282 #endif
2283 #ifndef NO_XFS
2284         if(io->r_oflags & O_DIRECT) {
2285                 struct dioattr  finfo;
2286                 
2287                 if(xfsctl(io->r_file, fd, XFS_IOC_DIOINFO, &finfo) == -1) {
2288                         cp += sprintf(cp, "          Error %s (%d) getting direct I/O info\n",
2289                                       strerror(errno), errno);
2290                         finfo.d_mem = 1;
2291                         finfo.d_miniosz = 1;
2292                         finfo.d_maxiosz = 1;
2293                 }
2294
2295                 cp += sprintf(cp, "          DIRECT I/O: offset %% %d = %d length %% %d = %d\n",
2296                               finfo.d_miniosz,
2297                               io->r_offset % finfo.d_miniosz,
2298                               io->r_nbytes,
2299                               io->r_nbytes % finfo.d_miniosz);
2300                 cp += sprintf(cp, "          mem alignment 0x%x xfer size: small: %d large: %d\n",
2301                               finfo.d_mem, finfo.d_miniosz, finfo.d_maxiosz);
2302         }
2303 #endif
2304
2305         return(errbuf);
2306 }
2307
2308 /*
2309  * Issue listio requests
2310  */
2311 #ifdef CRAY
2312 struct status *
2313 sy_listio(req, sysc, fd, addr)
2314 struct io_req   *req;
2315 struct syscall_info *sysc;
2316 int fd;
2317 char *addr;
2318 {
2319         int             offset, nbytes, nstrides, nents, aio_strat;
2320         int             aio_id, signo, o, i, lc;
2321         char            *a;
2322         struct listreq  *lio_req, *l;
2323         struct aio_info *aiop;
2324         struct status   *status;
2325
2326         /*
2327          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
2328          * r_nbytes are at the same offset in the read_req and reada_req
2329          * structures.
2330          */
2331         offset    = req->r_data.io.r_offset;
2332         nbytes    = req->r_data.io.r_nbytes;
2333         nstrides  = req->r_data.io.r_nstrides;
2334         nents     = req->r_data.io.r_nent;
2335         aio_strat = req->r_data.io.r_aio_strat;
2336
2337         lc = (sysc->sy_flags & SY_ASYNC) ? LC_START : LC_WAIT;
2338
2339         status = (struct status *)malloc(sizeof(struct status));
2340         if( status == NULL ){
2341                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2342                         __FILE__, __LINE__);
2343                 return NULL;
2344         }
2345         status->aioid = (int *)malloc( (nents+1) * sizeof(int) );
2346         if( status->aioid == NULL ){
2347                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2348                         __FILE__, __LINE__);
2349                 return NULL;
2350         }
2351
2352         signo = (aio_strat == A_SIGNAL) ? SIGUSR1 : 0;
2353
2354         lio_req = (struct listreq *)malloc(nents * sizeof(struct listreq));
2355         if( lio_req == NULL ){
2356                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2357                         __FILE__, __LINE__);
2358                 return NULL;
2359         }
2360         for(l=lio_req,a=addr,o=offset,i=0;
2361             i < nents;
2362             l++, a+=nbytes, o+=nbytes, i++) {
2363
2364                 aio_id = aio_register(fd, aio_strat, signo);
2365                 aiop = aio_slot(aio_id);
2366                 status->aioid[i] = aio_id;
2367
2368                 l->li_opcode    = (sysc->sy_flags & SY_WRITE) ? LO_WRITE : LO_READ;
2369                 l->li_offset    = o;
2370                 l->li_fildes    = fd;
2371                 l->li_buf       = a;
2372                 l->li_nbyte     = nbytes;
2373                 l->li_status    = &aiop->iosw;
2374                 l->li_signo     = signo;
2375                 l->li_nstride   = nstrides;
2376                 l->li_filstride = 0;
2377                 l->li_memstride = 0;
2378                 l->li_drvr      = 0;
2379                 l->li_flags     = LF_LSEEK;
2380         }
2381
2382         status->aioid[nents] = -1;              /* end sentinel */
2383
2384         if( (status->rval = listio(lc, lio_req, nents)) == -1) {
2385                 status->err = errno;
2386         }
2387
2388         free(lio_req);
2389         return(status);
2390 }
2391
2392 /*
2393  * Calculate the size of a request in bytes and min/max boundaries
2394  *
2395  * This assumes filestride & memstride = 0.
2396  */
2397 int
2398 listio_mem(struct io_req *req, int offset, int fmstride,
2399            int *min, int *max)
2400 {
2401         int     i, size;
2402
2403         size = stride_bounds(offset, fmstride,
2404                              req->r_data.io.r_nstrides*req->r_data.io.r_nent,
2405                              req->r_data.io.r_nbytes, min, max);
2406         return(size);
2407 }
2408
2409 char *
2410 fmt_listio(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
2411 {
2412         static char     *errbuf = NULL;
2413         char            *cp;
2414         char            *c, *opcode;
2415         int             i;
2416
2417         if(errbuf == NULL){
2418                 errbuf = (char *)malloc(32768);
2419                 if( errbuf == NULL ){
2420                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2421                         __FILE__, __LINE__);
2422                         return NULL;
2423                 }
2424         }
2425
2426         c = (sy->sy_flags & SY_ASYNC) ? "lc_wait" : "lc_start";
2427
2428         cp = errbuf;
2429         cp += sprintf(cp, "syscall:  listio(%s, (?), %d)\n",
2430                       c, req->r_data.io.r_nent);
2431
2432         cp += sprintf(cp, "          data buffer at %#o\n", addr);
2433
2434         return(errbuf);
2435 }
2436 #endif /* CRAY */
2437
2438 struct status *
2439 sy_pread(req, sysc, fd, addr)
2440 struct io_req   *req;
2441 struct syscall_info *sysc;
2442 int fd;
2443 char *addr;
2444 {
2445         int rc;
2446         struct status   *status;
2447
2448         rc = pread(fd, addr, req->r_data.io.r_nbytes,
2449                    req->r_data.io.r_offset);
2450
2451         status = (struct status *)malloc(sizeof(struct status));
2452         if( status == NULL ){
2453                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2454                         __FILE__, __LINE__);
2455                 return NULL;
2456         }
2457         status->aioid = NULL;
2458         status->rval = rc;
2459         status->err = errno;
2460
2461         return(status);
2462 }
2463
2464 struct status *
2465 sy_pwrite(req, sysc, fd, addr)
2466 struct io_req   *req;
2467 struct syscall_info *sysc;
2468 int fd;
2469 char *addr;
2470 {
2471         int rc;
2472         struct status   *status;
2473
2474         rc = pwrite(fd, addr, req->r_data.io.r_nbytes,
2475                     req->r_data.io.r_offset);
2476
2477         status = (struct status *)malloc(sizeof(struct status));
2478         if( status == NULL ){
2479                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2480                         __FILE__, __LINE__);
2481                 return NULL;
2482         }
2483         status->aioid = NULL;
2484         status->rval = rc;
2485         status->err = errno;
2486
2487         return(status);
2488 }
2489
2490 char *
2491 fmt_pread(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
2492 {
2493         static char     *errbuf = NULL;
2494         char            *cp;
2495
2496         if(errbuf == NULL){
2497                 errbuf = (char *)malloc(32768);
2498                 if( errbuf == NULL ){
2499                         doio_fprintf(stderr, "malloc failed, %s/%d\n",
2500                                 __FILE__, __LINE__);
2501                         return NULL;
2502                 }
2503         }
2504
2505         cp = errbuf;
2506         cp += sprintf(cp, "syscall:  %s(%d, 0x%p, %d)\n",
2507                       sy->sy_name, fd, addr, req->r_data.io.r_nbytes);
2508         return(errbuf);
2509 }
2510
2511 #ifndef CRAY
2512 struct status *
2513 sy_readv(req, sysc, fd, addr)
2514 struct io_req   *req;
2515 struct syscall_info *sysc;
2516 int fd;
2517 char *addr;
2518 {
2519         struct status *sy_rwv();
2520         return sy_rwv(req, sysc, fd, addr, 0);
2521 }
2522
2523 struct status *
2524 sy_writev(req, sysc, fd, addr)
2525 struct io_req   *req;
2526 struct syscall_info *sysc;
2527 int fd;
2528 char *addr;
2529 {
2530         struct status *sy_rwv();
2531         return sy_rwv(req, sysc, fd, addr, 1);
2532 }
2533
2534 struct status *
2535 sy_rwv(req, sysc, fd, addr, rw)
2536 struct io_req   *req;
2537 struct syscall_info *sysc;
2538 int fd;
2539 char *addr;
2540 int rw;
2541 {
2542         int rc;
2543         struct status   *status;
2544         struct iovec    iov[2];
2545
2546         status = (struct status *)malloc(sizeof(struct status));
2547         if( status == NULL ){
2548                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2549                         __FILE__, __LINE__);
2550                 return NULL;
2551         }
2552         status->aioid = NULL;
2553
2554         /* move to the desired file position. */
2555         if ((rc=lseek(fd, req->r_data.io.r_offset, SEEK_SET)) == -1) {
2556                 status->rval = rc;
2557                 status->err = errno;
2558                 return(status);
2559         }
2560
2561         iov[0].iov_base = addr;
2562         iov[0].iov_len = req->r_data.io.r_nbytes;
2563
2564         if(rw)
2565                 rc = writev(fd, iov, 1);
2566         else
2567                 rc = readv(fd, iov, 1);
2568         status->aioid = NULL;
2569         status->rval = rc;
2570         status->err = errno;
2571         return(status);
2572 }
2573
2574 char *
2575 fmt_readv(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
2576 {
2577         static char     errbuf[32768];
2578         char            *cp;
2579
2580         cp = errbuf;
2581         cp += sprintf(cp, "syscall:  %s(%d, (iov on stack), 1)\n",
2582                       sy->sy_name, fd);
2583         return(errbuf);
2584 }
2585 #endif /* !CRAY */
2586
2587 #ifdef sgi
2588 struct status *
2589 sy_aread(req, sysc, fd, addr)
2590 struct io_req *req;
2591 struct syscall_info *sysc;
2592 int fd;
2593 char *addr;
2594 {
2595         struct status *sy_arw();
2596         return sy_arw(req, sysc, fd, addr, 0);
2597 }
2598
2599 struct status *
2600 sy_awrite(req, sysc, fd, addr)
2601 struct io_req *req;
2602 struct syscall_info *sysc;
2603 int fd;
2604 char *addr;
2605 {
2606         struct status *sy_arw();
2607         return sy_arw(req, sysc, fd, addr, 1);
2608 }
2609
2610 /*
2611   #define sy_aread(A, B, C, D)  sy_arw(A, B, C, D, 0)
2612   #define sy_awrite(A, B, C, D) sy_arw(A, B, C, D, 1)
2613  */
2614
2615 struct status *
2616 sy_arw(req, sysc, fd, addr, rw)
2617 struct io_req *req;
2618 struct syscall_info *sysc;
2619 int fd;
2620 char *addr;
2621 int rw;
2622 {
2623         /* POSIX 1003.1b-1993 Async read */
2624         struct status           *status;
2625         int                     rc;
2626         int                     aio_id, aio_strat, signo;
2627         struct aio_info         *aiop;
2628
2629         status = (struct status *)malloc(sizeof(struct status));
2630         if( status == NULL ){
2631                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2632                         __FILE__, __LINE__);
2633                 return NULL;
2634         }
2635         aio_strat = req->r_data.io.r_aio_strat;
2636         signo = (aio_strat == A_SIGNAL) ? SIGUSR1 : 0;
2637
2638         aio_id = aio_register(fd, aio_strat, signo);
2639         aiop = aio_slot(aio_id);
2640
2641         memset( (void *)&aiop->aiocb, 0, sizeof(aiocb_t));
2642
2643         aiop->aiocb.aio_fildes = fd;
2644         aiop->aiocb.aio_nbytes = req->r_data.io.r_nbytes;
2645         aiop->aiocb.aio_offset = req->r_data.io.r_offset;
2646         aiop->aiocb.aio_buf = addr;
2647         aiop->aiocb.aio_reqprio = 0;    /* must be 0 */
2648         aiop->aiocb.aio_lio_opcode = 0;
2649
2650         if(aio_strat == A_SIGNAL) {     /* siginfo(2) stuff */
2651                 aiop->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
2652                 aiop->aiocb.aio_sigevent.sigev_signo = signo;
2653         } else if(aio_strat == A_CALLBACK) {
2654                 aiop->aiocb.aio_sigevent.sigev_signo = 0;
2655                 aiop->aiocb.aio_sigevent.sigev_notify = SIGEV_CALLBACK;
2656                 aiop->aiocb.aio_sigevent.sigev_func = cb_handler;
2657                 aiop->aiocb.aio_sigevent.sigev_value.sival_int = aio_id;
2658         } else {
2659                 aiop->aiocb.aio_sigevent.sigev_notify = SIGEV_NONE;
2660                 aiop->aiocb.aio_sigevent.sigev_signo = 0;
2661         }
2662
2663         if(rw)
2664                 rc = aio_write(&aiop->aiocb);
2665         else
2666                 rc = aio_read(&aiop->aiocb);
2667
2668         status->aioid = (int *)malloc( 2 * sizeof(int) );
2669         if( status->aioid == NULL ){
2670                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2671                         __FILE__, __LINE__);
2672                 return NULL;
2673         }
2674         status->aioid[0] = aio_id;
2675         status->aioid[1] = -1;
2676         status->rval = rc;
2677         status->err = errno;
2678         return(status);
2679 }
2680
2681 char *
2682 fmt_aread(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
2683 {
2684         static char     errbuf[32768];
2685         char            *cp;
2686
2687         cp = errbuf;
2688         cp += sprintf(cp, "syscall:  %s(&aiop->aiocb)\n",
2689                       sy->sy_name);
2690         return(errbuf);
2691 }
2692 #endif /* sgi */
2693
2694 #ifndef CRAY
2695
2696 struct status *
2697 sy_mmread(req, sysc, fd, addr)
2698 struct io_req *req;
2699 struct syscall_info *sysc;
2700 int fd;
2701 char *addr;
2702 {
2703         struct status *sy_mmrw();
2704         return sy_mmrw(req, sysc, fd, addr, 0);
2705 }
2706
2707 struct status *
2708 sy_mmwrite(req, sysc, fd, addr)
2709 struct io_req *req;
2710 struct syscall_info *sysc;
2711 int fd;
2712 char *addr;
2713 {
2714         struct status *sy_mmrw();
2715         return sy_mmrw(req, sysc, fd, addr, 1);
2716 }
2717
2718 struct status *
2719 sy_mmrw(req, sysc, fd, addr, rw)
2720 struct io_req *req;
2721 struct syscall_info *sysc;
2722 int fd;
2723 char *addr;
2724 int rw;
2725 {
2726         /*
2727          * mmap read/write
2728          * This version is oriented towards mmaping the file to memory
2729          * ONCE and keeping it mapped.
2730          */
2731         struct status           *status;
2732         void                    *mrc, *memaddr;
2733         struct fd_cache         *fdc;
2734         struct stat             sbuf;
2735
2736         status = (struct status *)malloc(sizeof(struct status));
2737         if( status == NULL ){
2738                 doio_fprintf(stderr, "malloc failed, %s/%d\n",
2739                         __FILE__, __LINE__);
2740                 return NULL;
2741         }
2742         status->aioid = NULL;
2743         status->rval = -1;
2744
2745         fdc = alloc_fdcache(req->r_data.io.r_file, req->r_data.io.r_oflags);
2746
2747         if( fdc->c_memaddr == NULL ) {
2748                 if( fstat(fd, &sbuf) < 0 ){
2749                         doio_fprintf(stderr, "fstat failed, errno=%d\n",
2750                                      errno);
2751                         status->err = errno;
2752                         return(status);
2753                 }
2754
2755                 fdc->c_memlen = (int)sbuf.st_size;
2756                 mrc = mmap(NULL, (int)sbuf.st_size,
2757                      rw ? PROT_WRITE|PROT_READ : PROT_READ,
2758                      MAP_SHARED, fd, 0);
2759
2760                 if( mrc == MAP_FAILED ) {
2761                         doio_fprintf(stderr, "mmap() failed - 0x%lx %d\n",
2762                                 mrc, errno);
2763                         status->err = errno;
2764                         return(status);
2765                 }
2766
2767                 fdc->c_memaddr = mrc;
2768         }
2769
2770         memaddr = (void *)((char *)fdc->c_memaddr + req->r_data.io.r_offset);
2771
2772         active_mmap_rw = 1;
2773         if(rw)
2774                 memcpy(memaddr, addr, req->r_data.io.r_nbytes);
2775         else
2776                 memcpy(addr, memaddr, req->r_data.io.r_nbytes);
2777         active_mmap_rw = 0;
2778
2779         status->rval = req->r_data.io.r_nbytes;
2780         status->err = 0;
2781         return(status);
2782 }
2783
2784 char *
2785 fmt_mmrw(struct io_req *req, struct syscall_info *sy, int fd, char *addr)
2786 {
2787         static char     errbuf[32768];
2788         char            *cp;
2789         struct fd_cache *fdc;
2790         void            *memaddr;
2791
2792         fdc = alloc_fdcache(req->r_data.io.r_file, req->r_data.io.r_oflags);
2793
2794         cp = errbuf;
2795         cp += sprintf(cp, "syscall:  %s(NULL, %d, %s, MAP_SHARED, %d, 0)\n",
2796                       sy->sy_name,
2797                       fdc->c_memlen,
2798                       (sy->sy_flags & SY_WRITE) ? "PROT_WRITE" : "PROT_READ",
2799                       fd);
2800
2801         cp += sprintf(cp, "\tfile is mmaped to: 0x%lx\n",
2802                       (unsigned long) fdc->c_memaddr);
2803
2804         memaddr = (void *)((char *)fdc->c_memaddr + req->r_data.io.r_offset);
2805
2806         cp += sprintf(cp, "\tfile-mem=0x%lx, length=%d, buffer=0x%lx\n",
2807                       (unsigned long) memaddr, req->r_data.io.r_nbytes,
2808                       (unsigned long) addr);
2809                       
2810         return(errbuf);
2811 }
2812 #endif /* !CRAY */
2813
2814 struct syscall_info syscalls[] = {
2815 #ifdef CRAY
2816         { "listio-read-sync",           LREAD,
2817           sy_listio,    NULL,           fmt_listio,
2818           SY_IOSW
2819         },
2820         { "listio-read-strides-sync",   LSREAD,
2821           sy_listio,    listio_mem,     fmt_listio,
2822           SY_IOSW
2823         },
2824         { "listio-read-reqs-sync",      LEREAD,
2825           sy_listio,    listio_mem,     fmt_listio,
2826           SY_IOSW
2827         },
2828         { "listio-read-async",          LREADA,
2829           sy_listio,    NULL,           fmt_listio,
2830           SY_IOSW | SY_ASYNC
2831         },
2832         { "listio-read-strides-async",  LSREADA,
2833           sy_listio,    listio_mem,     fmt_listio,
2834           SY_IOSW | SY_ASYNC
2835         },
2836         { "listio-read-reqs-async",     LEREADA,
2837           sy_listio,    listio_mem,     fmt_listio,
2838           SY_IOSW | SY_ASYNC
2839         },
2840         { "listio-write-sync",          LWRITE,
2841           sy_listio,    listio_mem,     fmt_listio,
2842           SY_IOSW | SY_WRITE
2843         },
2844         { "listio-write-strides-sync",  LSWRITE,
2845           sy_listio,    listio_mem,     fmt_listio,
2846           SY_IOSW | SY_WRITE
2847         },
2848         { "listio-write-reqs-sync",     LEWRITE,
2849           sy_listio,    listio_mem,     fmt_listio,
2850           SY_IOSW | SY_WRITE
2851         },
2852         { "listio-write-async",         LWRITEA,
2853           sy_listio,    listio_mem,     fmt_listio,
2854           SY_IOSW | SY_WRITE | SY_ASYNC
2855         },
2856         { "listio-write-strides-async", LSWRITEA,
2857           sy_listio,    listio_mem,     fmt_listio,
2858           SY_IOSW | SY_WRITE | SY_ASYNC
2859         },
2860         { "listio-write-reqs-async",    LEWRITEA,
2861           sy_listio,    listio_mem,     fmt_listio,
2862           SY_IOSW | SY_WRITE | SY_ASYNC
2863         },
2864 #endif
2865
2866 #ifdef sgi
2867         { "aread",                      AREAD,
2868           sy_aread,     NULL,           fmt_aread,
2869           SY_IOSW | SY_ASYNC
2870         },
2871         { "awrite",                     AWRITE,
2872           sy_awrite,    NULL,           fmt_aread,
2873           SY_IOSW | SY_WRITE | SY_ASYNC
2874         },
2875 #endif
2876         { "pread",                      PREAD,
2877           sy_pread,     NULL,           fmt_pread,
2878           0
2879         },
2880         { "pwrite",                     PWRITE,
2881           sy_pwrite,    NULL,           fmt_pread,
2882           SY_WRITE
2883         },
2884
2885 #ifndef CRAY
2886         { "readv",                      READV,
2887           sy_readv,     NULL,           fmt_readv,
2888           0
2889         },
2890         { "writev",                     WRITEV,
2891           sy_writev,    NULL,           fmt_readv,
2892           SY_WRITE
2893         },
2894         { "mmap-read",                  MMAPR,
2895           sy_mmread,    NULL,           fmt_mmrw,
2896           0
2897         },
2898         { "mmap-write",                 MMAPW,
2899           sy_mmwrite,   NULL,           fmt_mmrw,
2900           SY_WRITE
2901         },
2902 #endif
2903
2904         { NULL,                         0,
2905           0,            0,              0,
2906           0
2907         },
2908 };
2909
2910 int
2911 do_rw(req)
2912         struct io_req   *req;
2913 {
2914         static int              pid = -1;
2915         int                     fd, offset, nbytes, nstrides, nents, oflags;
2916         int                     rval, mem_needed, i;
2917         int                     logged_write, got_lock, woffset = 0, pattern;
2918         int                     min_byte, max_byte;
2919         char                    *addr, *file, *msg;
2920         struct status           *s;
2921         struct wlog_rec         wrec;
2922         struct syscall_info     *sy;
2923 #ifdef sgi
2924         struct aio_info         *aiop;
2925 #endif
2926 #ifdef CRAY
2927         /* REFERENCED */
2928         struct iosw             *iosw;
2929 #endif
2930 #ifndef NO_XFS
2931         struct fd_cache         *fdc;
2932 #endif
2933
2934         /*
2935          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
2936          * r_nbytes are at the same offset in the read_req and reada_req
2937          * structures.
2938          */
2939         file    = req->r_data.io.r_file;
2940         oflags  = req->r_data.io.r_oflags;
2941         offset  = req->r_data.io.r_offset;
2942         nbytes  = req->r_data.io.r_nbytes;
2943         nstrides= req->r_data.io.r_nstrides;
2944         nents   = req->r_data.io.r_nent;
2945         pattern = req->r_data.io.r_pattern;
2946
2947         if( nents >= MAX_AIO ) {
2948                 doio_fprintf(stderr, "do_rw: too many list requests, %d.  Maximum is %d\n",
2949                              nents, MAX_AIO);
2950                 return(-1);
2951         }
2952
2953         /*
2954          * look up system call info
2955          */
2956         for(sy=syscalls; sy->sy_name != NULL && sy->sy_type != req->r_type; sy++)
2957                 ;
2958
2959         if(sy->sy_name == NULL) {
2960                 doio_fprintf(stderr, "do_rw: unknown r_type %d.\n",
2961                              req->r_type);
2962                 return(-1);
2963         }
2964
2965         /*
2966          * Get an open file descriptor
2967          * Note: must be done before memory allocation so that the direct i/o
2968          *      information is available in mem. allocate
2969          */
2970
2971         if ((fd = alloc_fd(file, oflags)) == -1)
2972                 return -1;
2973
2974         /*
2975          * Allocate core memory and possibly sds space.  Initialize the
2976          * data to be written.  Make sure we get enough, based on the
2977          * memstride.
2978          *
2979          * need:
2980          *      1 extra word for possible partial-word address "bump"
2981          *      1 extra word for dynamic pattern overrun
2982          *      MPP_BUMP extra words for T3E non-hw-aligned memory address.
2983          */
2984
2985         if( sy->sy_buffer != NULL ) {
2986                 mem_needed = (*sy->sy_buffer)(req, 0, 0, NULL, NULL);
2987         } else {
2988                 mem_needed = nbytes;
2989         }
2990
2991 #ifdef CRAY
2992         if ((rval = alloc_mem(mem_needed + wtob(1) * 2 + MPP_BUMP * sizeof(UINT64_T))) < 0) {
2993                 return rval;
2994         }
2995 #else
2996 #ifndef NO_XFS
2997         /* get memory alignment for using DIRECT I/O */
2998         fdc = alloc_fdcache(file, oflags);
2999
3000         if ((rval = alloc_mem(mem_needed + wtob(1) * 2 + fdc->c_memalign)) < 0) {
3001                 return rval;
3002         }
3003 #else
3004         if ((rval = alloc_mem(mem_needed + wtob(1) * 2)) < 0) {
3005                 return rval;
3006         }
3007 #endif
3008 #endif /* CRAY */
3009
3010         Pattern[0] = pattern;
3011
3012         /*
3013          * Allocate SDS space for backdoor write if desired
3014          */
3015
3016         if (oflags & O_SSD) {
3017 #ifdef CRAY
3018 #ifndef _CRAYMPP
3019                 if (alloc_sds(nbytes) == -1)
3020                         return -1;
3021
3022                 if( sy->sy_flags & SY_WRITE ) {
3023                         /*pattern_fill(Memptr, mem_needed, Pattern, Pattern_Length, 0);*/
3024                         (*Data_Fill)(Memptr, nbytes, Pattern, Pattern_Length, 0);
3025
3026                         if (sswrite((long)Memptr, Sdsptr, btoc(mem_needed)) == -1) {
3027                                 doio_fprintf(stderr, "sswrite(%d, %d, %d) failed:  %s (%d)\n",
3028                                              (long)Memptr, Sdsptr, 
3029                                              btoc(mem_needed), SYSERR, errno);
3030                                 fflush(stderr);
3031                                 return -1;
3032                         }
3033                 }
3034
3035                 addr = (char *)Sdsptr;
3036 #else
3037                 doio_fprintf(stderr, "Invalid O_SSD flag was generated for MPP system\n");
3038                 fflush(stderr);
3039                 return -1;
3040 #endif /* _CRAYMPP */
3041 #else   /* CRAY */
3042                 doio_fprintf(stderr, "Invalid O_SSD flag was generated for non-Cray system\n");
3043                 fflush(stderr);
3044                 return -1;
3045 #endif  /* CRAY */
3046         } else {
3047                 addr = Memptr;
3048
3049                 /*
3050                  * if io is not raw, bump the offset by a random amount
3051                  * to generate non-word-aligned io.
3052                  *
3053                  * On MPP systems, raw I/O must start on an 0x80 byte boundary.
3054                  * For non-aligned I/O, bump the address from 1 to 8 words.
3055                  */
3056
3057                 if (! (req->r_data.io.r_uflags & F_WORD_ALIGNED)) {
3058 #ifdef _CRAYMPP
3059                         addr += random_range(0, MPP_BUMP, 1, NULL) * sizeof(int);
3060 #endif
3061                         addr += random_range(0, wtob(1) - 1, 1, NULL);
3062                 }
3063
3064 #ifndef NO_XFS
3065                 /*
3066                  * Force memory alignment for Direct I/O
3067                  */
3068                 if( (oflags & O_DIRECT) && ((long)addr % fdc->c_memalign != 0) ) {
3069                         addr += fdc->c_memalign - ((long)addr % fdc->c_memalign);
3070                 }
3071 #endif
3072
3073                 /*
3074                  * FILL must be done on a word-aligned buffer.
3075                  * Call the fill function with Memptr which is aligned,
3076                  * then memmove it to the right place.
3077                  */
3078                 if (sy->sy_flags & SY_WRITE) {
3079                         (*Data_Fill)(Memptr, mem_needed, Pattern, Pattern_Length, 0);
3080                         if( addr != Memptr )
3081                             memmove( addr, Memptr, mem_needed);
3082                 }
3083         }
3084
3085         rval = 0;
3086         got_lock = 0;
3087         logged_write = 0;
3088
3089         /*
3090          * Lock data if this is a write and locking option is set
3091          */
3092         if (sy->sy_flags & SY_WRITE && k_opt) {
3093                 if( sy->sy_buffer != NULL ) {
3094                         (*sy->sy_buffer)(req, offset, 0, &min_byte, &max_byte);
3095                 } else {
3096                         min_byte = offset;
3097                         max_byte = offset + (nbytes * nstrides * nents);
3098                 }
3099
3100                 if (lock_file_region(file, fd, F_WRLCK,
3101                                      min_byte, (max_byte-min_byte+1)) < 0) {
3102                     doio_fprintf(stderr, 
3103                                 "file lock failed:\n%s\n",
3104                                 fmt_ioreq(req, sy, fd));
3105                     doio_fprintf(stderr, 
3106                                 "          buffer(req, %d, 0, 0x%x, 0x%x)\n",
3107                                 offset, min_byte, max_byte);
3108                     alloc_mem(-1);
3109                     exit(E_INTERNAL);
3110                 }
3111
3112                 got_lock = 1;
3113         }
3114
3115         /*
3116          * Write a preliminary write-log entry.  This is done so that
3117          * doio_check can do corruption detection across an interrupt/crash.
3118          * Note that w_done is set to 0.  If doio_check sees this, it
3119          * re-creates the file extents as if the write completed, but does not
3120          * do any checking - see comments in doio_check for more details.
3121          */
3122
3123         if (sy->sy_flags & SY_WRITE && w_opt) {
3124                 if (pid == -1) {
3125                         pid = getpid();
3126                 }
3127
3128                 wrec.w_async = (sy->sy_flags & SY_ASYNC) ? 1 : 0;
3129                 wrec.w_oflags = oflags;
3130                 wrec.w_pid = pid;
3131                 wrec.w_offset = offset;
3132                 wrec.w_nbytes = nbytes; /* mem_needed -- total length */
3133
3134                 wrec.w_pathlen = strlen(file);
3135                 memcpy(wrec.w_path, file, wrec.w_pathlen);
3136                 wrec.w_hostlen = strlen(Host);
3137                 memcpy(wrec.w_host, Host, wrec.w_hostlen);
3138                 wrec.w_patternlen = Pattern_Length;
3139                 memcpy(wrec.w_pattern, Pattern, wrec.w_patternlen);
3140
3141                 wrec.w_done = 0;
3142
3143                 if ((woffset = wlog_record_write(&Wlog, &wrec, -1)) == -1) {
3144                         doio_fprintf(stderr,
3145                                      "Could not append to write-log:  %s (%d)\n",
3146                                      SYSERR, errno);
3147                 } else {
3148                         logged_write = 1;
3149                 }
3150         }
3151
3152         s = (*sy->sy_syscall)(req, sy, fd, addr);
3153
3154         if( s->rval == -1 ) {
3155                 doio_fprintf(stderr,
3156                              "%s() request failed:  %s (%d)\n%s\n%s\n",
3157                              sy->sy_name, SYSERR, errno,
3158                              fmt_ioreq(req, sy, fd),
3159                              (*sy->sy_format)(req, sy, fd, addr));
3160
3161                 doio_upanic(U_RVAL);
3162
3163                 for(i=0; i < nents; i++) {
3164                         if(s->aioid == NULL)
3165                                 break;
3166                         aio_unregister(s->aioid[i]);
3167                 }
3168                 rval = -1;
3169         } else {
3170                 /*
3171                  * If the syscall was async, wait for I/O to complete
3172                  */
3173 #ifndef linux
3174                 if(sy->sy_flags & SY_ASYNC) {
3175                         for(i=0; i < nents; i++) {
3176                                 aio_wait(s->aioid[i]);
3177                         }
3178                 }
3179 #endif
3180
3181                 /*
3182                  * Check the syscall how-much-data-written return.  Look
3183                  * for this in either the return value or the 'iosw'
3184                  * structure.
3185                  */
3186
3187                 if( sy->sy_flags & SY_IOSW ) {
3188 #ifdef CRAY
3189                         for( i=0; i < nents; i++ ) {
3190                                 if(s->aioid == NULL)
3191                                         break; /* >>> error condition? */
3192                                 aiop = aio_slot(s->aioid[i]);
3193                                 iosw = &aiop->iosw;
3194                                 if(iosw->sw_error != 0) {
3195                                         doio_fprintf(stderr,
3196                                                      "%s() iosw error set: %s\n%s\n%s\n",
3197                                                      sy->sy_name,
3198                                                      strerror(iosw->sw_error),
3199                                                      fmt_ioreq(req, sy, fd),
3200                                                      (*sy->sy_format)(req, sy, fd, addr));
3201                                         doio_upanic(U_IOSW);
3202                                         rval = -1;
3203                                 } else if(iosw->sw_count != nbytes*nstrides) {
3204                                         doio_fprintf(stderr,
3205                                                      "Bad iosw from %s() #%d\nExpected (%d,%d,%d), got (%d,%d,%d)\n%s\n%s\n",
3206                                                      sy->sy_name, i,
3207                                                      1, 0, nbytes*nstrides,
3208                                                      iosw->sw_flag,
3209                                                      iosw->sw_error,
3210                                                      iosw->sw_count,
3211                                                      fmt_ioreq(req, sy, fd),
3212                                                      (*sy->sy_format)(req, sy, fd, addr));
3213                                         doio_upanic(U_IOSW);
3214                                         rval = -1;
3215                                 }
3216
3217                                 aio_unregister(s->aioid[i]);
3218                         }
3219 #endif /* CRAY */
3220 #ifdef sgi
3221                         for( i=0; s->aioid[i] != -1; i++ ) {
3222                                 if(s->aioid == NULL) {
3223                                         doio_fprintf(stderr,
3224                                                      "aioid == NULL!\n");
3225                                         break;
3226                                 }
3227                                 aiop = aio_slot(s->aioid[i]);
3228
3229                                 /*
3230                                  * make sure the io completed without error
3231                                  */
3232                                 if (aiop->aio_errno != 0) {
3233                                         doio_fprintf(stderr,
3234                                                      "%s() aio error set: %s (%d)\n%s\n%s\n",
3235                                                      sy->sy_name,
3236                                                      strerror(aiop->aio_errno),
3237                                                      aiop->aio_errno,
3238                                                      fmt_ioreq(req, sy, fd),
3239                                                      (*sy->sy_format)(req, sy, fd, addr));
3240                                         doio_upanic(U_IOSW);
3241                                         rval = -1;
3242                                 } else if (aiop->aio_ret != nbytes) {
3243                                         doio_fprintf(stderr,
3244                                                      "Bad aio return from %s() #%d\nExpected (%d,%d), got (%d,%d)\n%s\n%s\n",
3245                                                      sy->sy_name, i,
3246                                                      0, nbytes,
3247                                                      aiop->aio_errno,
3248                                                      aiop->aio_ret,
3249                                                      fmt_ioreq(req, sy, fd),
3250                                                      (*sy->sy_format)(req, sy, fd, addr));
3251                                         aio_unregister(s->aioid[i]);
3252                                         doio_upanic(U_IOSW);
3253                                         return -1;
3254                                 } else {
3255                                         aio_unregister(s->aioid[i]);
3256                                         rval = 0;
3257                                 }
3258                         }
3259 #endif /* sgi */
3260                 } else {
3261
3262                         if(s->rval != mem_needed) {
3263                                 doio_fprintf(stderr,
3264                                              "%s() request returned wrong # of bytes - expected %d, got %d\n%s\n%s\n",
3265                                              sy->sy_name, nbytes, s->rval,
3266                                              fmt_ioreq(req, sy, fd),
3267                                              (*sy->sy_format)(req, sy, fd, addr));
3268                                 rval = -1;
3269                                 doio_upanic(U_RVAL);
3270                         }
3271                 }
3272         }
3273
3274
3275         /*
3276          * Verify that the data was written correctly - check_file() returns
3277          * a non-null pointer which contains an error message if there are
3278          * problems.
3279          */
3280
3281         if ( rval == 0 && sy->sy_flags & SY_WRITE && v_opt) {
3282                 msg = check_file(file, offset, nbytes*nstrides*nents,
3283                                  Pattern, Pattern_Length, 0,
3284                                  oflags & O_PARALLEL);
3285                 if (msg != NULL) {
3286                         doio_fprintf(stderr, "%s\n%s\n%s\n",
3287                                      msg,
3288                                      fmt_ioreq(req, sy, fd),
3289                                      (*sy->sy_format)(req, sy, fd, addr));
3290                         doio_upanic(U_CORRUPTION);
3291                         exit(E_COMPARE);
3292                 }
3293         }
3294
3295         /*
3296          * General cleanup ...
3297          *
3298          * Write extent information to the write-log, so that doio_check can do
3299          * corruption detection.  Note that w_done is set to 1, indicating that
3300          * the write has been verified as complete.  We don't need to write the
3301          * filename on the second logging.
3302          */
3303
3304         if (w_opt && logged_write) {
3305                 wrec.w_done = 1;
3306                 wlog_record_write(&Wlog, &wrec, woffset);
3307         }
3308
3309         /*
3310          * Unlock file region if necessary
3311          */
3312
3313         if (got_lock) {
3314                 if (lock_file_region(file, fd, F_UNLCK,
3315                                      min_byte, (max_byte-min_byte+1)) < 0) {
3316                         alloc_mem(-1);
3317                         exit(E_INTERNAL);
3318                 }
3319         }
3320
3321         if(s->aioid != NULL)
3322                 free(s->aioid);
3323         free(s);
3324         return (rval == -1) ? -1 : 0;
3325 }
3326
3327
3328 /*
3329  * xfsctl-based requests
3330  *   - XFS_IOC_RESVSP
3331  *   - XFS_IOC_UNRESVSP
3332  */
3333 #ifndef NO_XFS
3334 int
3335 do_xfsctl(req)
3336         struct io_req   *req;
3337 {
3338         int                     fd, oflags, offset, nbytes;
3339         int                     rval, op = 0;
3340         int                     got_lock;
3341         int                     min_byte = 0, max_byte = 0;
3342         char                    *file, *msg = NULL;
3343         struct xfs_flock64      flk;
3344
3345         /*
3346          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
3347          * r_nbytes are at the same offset in the read_req and reada_req
3348          * structures.
3349          */
3350         file    = req->r_data.io.r_file;
3351         oflags  = req->r_data.io.r_oflags;
3352         offset  = req->r_data.io.r_offset;
3353         nbytes  = req->r_data.io.r_nbytes;
3354
3355         flk.l_type=0;
3356         flk.l_whence=SEEK_SET;
3357         flk.l_start=offset;
3358         flk.l_len=nbytes;
3359
3360         /*
3361          * Get an open file descriptor
3362          */
3363
3364         if ((fd = alloc_fd(file, oflags)) == -1)
3365                 return -1;
3366
3367         rval = 0;
3368         got_lock = 0;
3369
3370         /*
3371          * Lock data if this is locking option is set
3372          */
3373         if (k_opt) {
3374                 min_byte = offset;
3375                 max_byte = offset + nbytes;
3376
3377                 if (lock_file_region(file, fd, F_WRLCK,
3378                                      min_byte, (nbytes+1)) < 0) {
3379                     doio_fprintf(stderr, 
3380                                 "file lock failed:\n");
3381                     doio_fprintf(stderr, 
3382                                 "          buffer(req, %d, 0, 0x%x, 0x%x)\n",
3383                                 offset, min_byte, max_byte);
3384                     alloc_mem(-1);
3385                     exit(E_INTERNAL);
3386                 }
3387
3388                 got_lock = 1;
3389         }
3390
3391         switch (req->r_type) {
3392         case RESVSP:    op=XFS_IOC_RESVSP;      msg="resvsp";   break;
3393         case UNRESVSP:  op=XFS_IOC_UNRESVSP;    msg="unresvsp"; break;
3394         }
3395
3396         rval = xfsctl(file, fd, op, &flk);
3397
3398         if( rval == -1 ) {
3399                 doio_fprintf(stderr,
3400 "xfsctl %s request failed: %s (%d)\n\txfsctl(%d, %s %d, {%d %lld ==> %lld}\n",
3401                              msg, SYSERR, errno,
3402                              fd, msg, op, flk.l_whence, 
3403                              (long long)flk.l_start, 
3404                              (long long)flk.l_len);
3405
3406                 doio_upanic(U_RVAL);
3407                 rval = -1;
3408         }
3409
3410         /*
3411          * Unlock file region if necessary
3412          */
3413
3414         if (got_lock) {
3415                 if (lock_file_region(file, fd, F_UNLCK,
3416                                      min_byte, (max_byte-min_byte+1)) < 0) {
3417                         alloc_mem(-1);
3418                         exit(E_INTERNAL);
3419                 }
3420         }
3421
3422         return (rval == -1) ? -1 : 0;
3423 }
3424 #endif
3425
3426 /*
3427  *  fsync(2) and fdatasync(2)
3428  */
3429 #ifndef CRAY
3430 int
3431 do_sync(req)
3432         struct io_req   *req;
3433 {
3434         int                     fd, oflags;
3435         int                     rval;
3436         char                    *file;
3437
3438         /*
3439          * Initialize common fields - assumes r_oflags, r_file, r_offset, and
3440          * r_nbytes are at the same offset in the read_req and reada_req
3441          * structures.
3442          */
3443         file    = req->r_data.io.r_file;
3444         oflags  = req->r_data.io.r_oflags;
3445
3446         /*
3447          * Get an open file descriptor
3448          */
3449
3450         if ((fd = alloc_fd(file, oflags)) == -1)
3451                 return -1;
3452
3453         rval = 0;
3454         switch(req->r_type) {
3455         case FSYNC2:
3456                 rval = fsync(fd);
3457                 break;
3458         case FDATASYNC:
3459                 rval = fdatasync(fd);
3460                 break;
3461         default:
3462                 rval = -1;
3463         }
3464         return (rval == -1) ? -1 : 0;
3465 }
3466 #endif
3467
3468
3469 int
3470 doio_pat_fill(char *addr, int mem_needed, char *Pattern, int Pattern_Length,
3471               int shift)
3472 {
3473         return pattern_fill(addr, mem_needed, Pattern, Pattern_Length, 0);
3474 }
3475
3476 char *
3477 doio_pat_check(buf, offset, length, pattern, pattern_length, patshift)
3478 char    *buf;
3479 int     offset;
3480 int     length;
3481 char    *pattern;
3482 int     pattern_length;
3483 int     patshift;
3484 {
3485         static char     errbuf[4096];
3486         int             nb, i, pattern_index;
3487         char            *cp, *bufend, *ep;
3488         char            actual[33], expected[33];
3489
3490         if (pattern_check(buf, length, pattern, pattern_length, patshift) != 0) {
3491                 ep = errbuf;
3492                 ep += sprintf(ep, "Corrupt regions follow - unprintable chars are represented as '.'\n");
3493                 ep += sprintf(ep, "-----------------------------------------------------------------\n");
3494
3495                 pattern_index = patshift % pattern_length;;
3496                 cp = buf;
3497                 bufend = buf + length;
3498
3499                 while (cp < bufend) {
3500                         if (*cp != pattern[pattern_index]) {
3501                                 nb = bufend - cp;
3502                                 if (nb > sizeof(expected)-1) {
3503                                         nb = sizeof(expected)-1;
3504                                 }
3505                             
3506                                 ep += sprintf(ep, "corrupt bytes starting at file offset %d\n", offset + (int)(cp-buf));
3507
3508                                 /*
3509                                  * Fill in the expected and actual patterns
3510                                  */
3511                                 bzero(expected, sizeof(expected));
3512                                 bzero(actual, sizeof(actual));
3513
3514                                 for (i = 0; i < nb; i++) {
3515                                         expected[i] = pattern[(pattern_index + i) % pattern_length];
3516                                         if (! isprint((int)expected[i])) {
3517                                                 expected[i] = '.';
3518                                         }
3519
3520                                         actual[i] = cp[i];
3521                                         if (! isprint((int)actual[i])) {
3522                                                 actual[i] = '.';
3523                                         }
3524                                 }
3525
3526                                 ep += sprintf(ep, "    1st %2d expected bytes:  %s\n", nb, expected);
3527                                 ep += sprintf(ep, "    1st %2d actual bytes:    %s\n", nb, actual);
3528                                 fflush(stderr);
3529                                 return errbuf;
3530                         } else {
3531                                 cp++;
3532                                 pattern_index++;
3533
3534                                 if (pattern_index == pattern_length) {
3535                                         pattern_index = 0;
3536                                 }
3537                         }
3538                 }
3539                 return errbuf;
3540         }
3541
3542         return(NULL);
3543 }
3544
3545
3546 /*
3547  * Check the contents of a file beginning at offset, for length bytes.  It
3548  * is assumed that there is a string of pattern bytes in this area of the
3549  * file.  Use normal buffered reads to do the verification.
3550  *
3551  * If there is a data mismatch, write a detailed message into a static buffer
3552  * suitable for the caller to print.  Otherwise print NULL.
3553  *
3554  * The fsa flag is set to non-zero if the buffer should be read back through
3555  * the FSA (unicos/mk).  This implies the file will be opened
3556  * O_PARALLEL|O_RAW|O_WELLFORMED to do the validation.  We must do this because
3557  * FSA will not allow the file to be opened for buffered io if it was
3558  * previously opened for O_PARALLEL io.
3559  */
3560
3561 char *
3562 check_file(file, offset, length, pattern, pattern_length, patshift, fsa)
3563 char    *file;
3564 int     offset;
3565 int     length;
3566 char    *pattern;
3567 int     pattern_length;
3568 int     patshift;
3569 int     fsa;
3570 {
3571         static char     errbuf[4096];
3572         int             fd, nb, flags;
3573         char            *buf, *em, *ep;
3574 #ifndef NO_XFS
3575         struct fd_cache *fdc;
3576 #endif
3577
3578         buf = Memptr;
3579
3580         if (V_opt) {
3581                 flags = Validation_Flags | O_RDONLY;
3582         } else {
3583                 flags = O_RDONLY;
3584                 if (fsa) {
3585 #ifdef CRAY
3586                         flags |= O_PARALLEL | O_RAW | O_WELLFORMED;
3587 #endif
3588                 }
3589         }
3590
3591         if ((fd = alloc_fd(file, flags)) == -1) {
3592                 sprintf(errbuf,
3593                         "Could not open file %s with flags %#o (%s) for data comparison:  %s (%d)\n",
3594                         file, flags, format_oflags(flags),
3595                         SYSERR, errno);
3596                 return errbuf;
3597         }
3598
3599         if (lseek(fd, offset, SEEK_SET) == -1) {
3600                 sprintf(errbuf, 
3601                         "Could not lseek to offset %d in %s for verification:  %s (%d)\n",
3602                         offset, file, SYSERR, errno);
3603                 return errbuf;
3604         }
3605
3606 #ifndef NO_XFS
3607         /* Guarantee a properly aligned address on Direct I/O */
3608         fdc = alloc_fdcache(file, flags);
3609         if( (flags & O_DIRECT) && ((long)buf % fdc->c_memalign != 0) ) {
3610                 buf += fdc->c_memalign - ((long)buf % fdc->c_memalign);
3611         }
3612 #endif
3613
3614         if ((nb = read(fd, buf, length)) == -1) {
3615 #ifndef NO_XFS
3616                 sprintf(errbuf,
3617                         "Could not read %d bytes from %s for verification:  %s (%d)\n\tread(%d, 0x%p, %d)\n\tbuf %% alignment(%d) = %ld\n",
3618                         length, file, SYSERR, errno,
3619                         fd, buf, length,
3620                         fdc->c_memalign, (long)buf % fdc->c_memalign);
3621 #else
3622                 sprintf(errbuf,
3623                         "Could not read %d bytes from %s for verification:  %s (%d)\n",
3624                         length, file, SYSERR, errno);
3625
3626 #endif
3627                 return errbuf;
3628         }
3629
3630         if (nb != length) {
3631                 sprintf(errbuf,
3632                         "Read wrong # bytes from %s.  Expected %d, got %d\n",
3633                         file, length, nb);
3634                 return errbuf;
3635         }
3636     
3637         if( (em = (*Data_Check)(buf, offset, length, pattern, pattern_length, patshift)) != NULL ) {
3638                 ep = errbuf;
3639                 ep += sprintf(ep, "*** DATA COMPARISON ERROR ***\n");
3640                 ep += sprintf(ep, "check_file(%s, %d, %d, %s, %d, %d) failed\n\n",
3641                               file, offset, length, pattern, pattern_length, patshift);
3642                 ep += sprintf(ep, "Comparison fd is %d, with open flags %#o\n",
3643                               fd, flags);
3644                 strcpy(ep, em);
3645                 return(errbuf);
3646         }
3647         return NULL;
3648 }
3649
3650 /*
3651  * Function to single-thread stdio output.
3652  */
3653
3654 int
3655 doio_fprintf(FILE *stream, char *format, ...)
3656 {
3657         static int      pid = -1;
3658         char            *date;
3659         int             rval;
3660         struct flock    flk;
3661         va_list         arglist;
3662
3663         date = hms(time(0));
3664
3665         if (pid == -1) {
3666                 pid = getpid();
3667         }
3668
3669         flk.l_whence = flk.l_start = flk.l_len = 0;
3670         flk.l_type = F_WRLCK;
3671         fcntl(fileno(stream), F_SETLKW, &flk);
3672
3673         va_start(arglist, format);
3674         rval = fprintf(stream, "\n%s%s (%5d) %s\n", Prog, TagName, pid, date);
3675         rval += fprintf(stream, "---------------------\n");
3676         vfprintf(stream, format, arglist);
3677         va_end(arglist);
3678
3679         fflush(stream);
3680
3681         flk.l_type = F_UNLCK;
3682         fcntl(fileno(stream), F_SETLKW, &flk);
3683  
3684         return rval;
3685 }
3686
3687 /*
3688  * Simple function for allocating core memory.  Uses Memsize and Memptr to
3689  * keep track of the current amount allocated.
3690  */
3691 #ifndef CRAY
3692 int
3693 alloc_mem(nbytes)
3694 int nbytes;
3695 {
3696         char            *cp;
3697         void            *addr;
3698         int             me = 0, flags, key, shmid;
3699         static int      mturn = 0;      /* which memory type to use */
3700         struct memalloc *M;
3701         char            filename[255];
3702 #ifdef linux
3703         struct shmid_ds shm_ds;
3704 #endif
3705
3706 #ifdef linux
3707         bzero( &shm_ds, sizeof(struct shmid_ds) );
3708 #endif
3709
3710         /* nbytes = -1 means "free all allocated memory" */
3711         if( nbytes == -1 ) {
3712
3713                 for(me=0; me < Nmemalloc; me++) {
3714                         if(Memalloc[me].space == NULL)
3715                                 continue;
3716
3717                         switch(Memalloc[me].memtype) {
3718                         case MEM_DATA:
3719 #ifdef sgi
3720                                 if(Memalloc[me].flags & MEMF_MPIN)
3721                                         munpin(Memalloc[me].space,
3722                                                Memalloc[me].size);
3723 #endif
3724                                 free(Memalloc[me].space);
3725                                 Memalloc[me].space = NULL;
3726                                 Memptr = NULL;
3727                                 Memsize = 0;
3728                                 break;
3729                         case MEM_SHMEM:
3730 #ifdef sgi
3731                                 if(Memalloc[me].flags & MEMF_MPIN)
3732                                         munpin(Memalloc[me].space,
3733                                                Memalloc[me].size);
3734 #endif
3735                                 shmdt(Memalloc[me].space);
3736                                 Memalloc[me].space = NULL;
3737 #ifdef sgi
3738                                 shmctl(Memalloc[me].fd, IPC_RMID);
3739 #else
3740                                 shmctl(Memalloc[me].fd, IPC_RMID, &shm_ds);
3741 #endif
3742                                 break;
3743                         case MEM_MMAP:
3744 #ifdef sgi
3745                                 if(Memalloc[me].flags & MEMF_MPIN)
3746                                         munpin(Memalloc[me].space,
3747                                                Memalloc[me].size);
3748 #endif
3749                                 munmap(Memalloc[me].space, 
3750                                        Memalloc[me].size);
3751                                 close(Memalloc[me].fd);
3752                                 if(Memalloc[me].flags & MEMF_FILE) {
3753                                         unlink(Memalloc[me].name);
3754                                 }
3755                                 Memalloc[me].space = NULL;
3756                                 break;
3757                         default:
3758                                 doio_fprintf(stderr, "alloc_mem: HELP! Unknown memory space type %d index %d\n",
3759                                              Memalloc[me].memtype, me);
3760                                 break;
3761                         }
3762                 }
3763                 return 0;
3764         }
3765
3766         /*
3767          * Select a memory area (currently round-robbin)
3768          */
3769
3770         if(mturn >= Nmemalloc)
3771                 mturn=0;
3772
3773         M = &Memalloc[mturn];
3774
3775         switch(M->memtype) {
3776         case MEM_DATA:
3777                 if( nbytes > M->size ) {
3778                         if( M->space != NULL ){
3779 #ifdef sgi
3780                                 if( M->flags & MEMF_MPIN )
3781                                         munpin( M->space, M->size );
3782 #endif
3783                                 free(M->space);
3784                         }
3785                         M->space = NULL;
3786                         M->size = 0;
3787                 }
3788
3789                 if( M->space == NULL ) {
3790                         if( (cp = malloc( nbytes )) == NULL ) {
3791                                 doio_fprintf(stderr, "malloc(%d) failed:  %s (%d)\n",
3792                                              nbytes, SYSERR, errno);
3793                                 return -1;
3794                         }
3795 #ifdef sgi
3796                         if(M->flags & MEMF_MPIN) {
3797                                 if( mpin(cp, nbytes) == -1 ) {
3798                                         doio_fprintf(stderr, "mpin(0x%lx, %d) failed:  %s (%d)\n",
3799                                              cp, nbytes, SYSERR, errno);
3800                                 }
3801                         }
3802 #endif
3803                         M->space = (void *)cp;
3804                         M->size = nbytes;
3805                 }
3806                 break;
3807
3808         case MEM_MMAP:
3809                 if( nbytes > M->size ) {
3810                         if( M->space != NULL ) {
3811 #ifdef sgi
3812                                 if( M->flags & MEMF_MPIN )
3813                                         munpin(M->space, M->size);
3814 #endif
3815                                 munmap(M->space, M->size);
3816                                 close(M->fd);
3817                                 if( M->flags & MEMF_FILE )
3818                                         unlink( M->name );
3819                         }
3820                         M->space = NULL;
3821                         M->size = 0;
3822                 }
3823
3824                 if( M->space == NULL ) {
3825                         if(strchr(M->name, '%')) {
3826                                 sprintf(filename, M->name, getpid());
3827                                 M->name = strdup(filename);
3828                         }
3829
3830                         if( (M->fd = open(M->name, O_CREAT|O_RDWR, 0666)) == -1) {
3831                                 doio_fprintf(stderr, "alloc_mmap: error %d (%s) opening '%s'\n",
3832                                              errno, SYSERR, 
3833                                              M->name);
3834                                 return(-1);
3835                         }
3836
3837                         addr = NULL;
3838                         flags = 0;
3839                         M->size = nbytes * 4;
3840
3841                         /* bias addr if MEMF_ADDR | MEMF_FIXADDR */
3842                         /* >>> how to pick a memory address? */
3843
3844                         /* bias flags on MEMF_PRIVATE etc */
3845                         if(M->flags & MEMF_PRIVATE)
3846                                 flags |= MAP_PRIVATE;
3847 #ifdef sgi
3848                         if(M->flags & MEMF_LOCAL)
3849                                 flags |= MAP_LOCAL;
3850                         if(M->flags & MEMF_AUTORESRV)
3851                                 flags |= MAP_AUTORESRV;
3852                         if(M->flags & MEMF_AUTOGROW)
3853                                 flags |= MAP_AUTOGROW;
3854 #endif
3855                         if(M->flags & MEMF_SHARED)
3856                                 flags |= MAP_SHARED;
3857
3858 /*printf("alloc_mem, about to mmap, fd=%d, name=(%s)\n", M->fd, M->name);*/
3859                         if( (M->space = mmap(addr, M->size,
3860                                              PROT_READ|PROT_WRITE,
3861                                              flags, M->fd, 0))
3862                             == MAP_FAILED) {
3863                                 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",
3864                                              errno, SYSERR,
3865                                              addr, M->size,
3866                                              PROT_READ|PROT_WRITE,
3867                                              flags, M->flags, M->fd,
3868                                              M->name);
3869                                 doio_fprintf(stderr, "\t%s%s%s%s%s",
3870                                              (flags & MAP_PRIVATE) ? "private " : "",
3871 #ifdef sgi
3872                                              (flags & MAP_LOCAL) ? "local " : "",
3873                                              (flags & MAP_AUTORESRV) ? "autoresrv " : "",
3874                                              (flags & MAP_AUTOGROW) ? "autogrow " : "",
3875 #endif
3876                                              (flags & MAP_SHARED) ? "shared" : "");
3877                                 return(-1);
3878                         }
3879                 }
3880                 break;
3881                 
3882         case MEM_SHMEM:
3883                 if( nbytes > M->size ) {
3884                         if( M->space != NULL ) {
3885 #ifdef sgi
3886                                 if( M->flags & MEMF_MPIN )
3887                                         munpin(M->space, M->size);
3888 #endif
3889                                 shmdt( M->space );
3890 #ifdef sgi
3891                                 shmctl( M->fd, IPC_RMID );
3892 #else
3893                                 shmctl( M->fd, IPC_RMID, &shm_ds );
3894 #endif
3895                         }
3896                         M->space = NULL;
3897                         M->size = 0;
3898                 }
3899
3900                 if(M->space == NULL) {
3901                         if(!strcmp(M->name, "private")) {
3902                                 key = IPC_PRIVATE;
3903                         } else {
3904                                 sscanf(M->name, "%i", &key);
3905                         }
3906
3907                         M->size = M->nblks ? M->nblks * 512 : nbytes;
3908
3909                         if( nbytes > M->size ){
3910 #ifdef DEBUG
3911                                 doio_fprintf(stderr, "MEM_SHMEM: nblks(%d) too small:  nbytes=%d  Msize=%d, skipping this req.\n",
3912                                              M->nblks, nbytes, M->size );
3913 #endif
3914                                 return SKIP_REQ;
3915                         }
3916
3917                         shmid = shmget(key, M->size, IPC_CREAT|0666);
3918                         if( shmid == -1 ) {
3919                                 doio_fprintf(stderr, "shmget(0x%x, %d, CREAT) failed: %s (%d)\n",
3920                                              key, M->size, SYSERR, errno);
3921                                 return(-1);
3922                         }
3923                         M->fd = shmid;
3924                         M->space = shmat(shmid, NULL, SHM_RND);
3925                         if( M->space == (void *)-1 ) {
3926                                 doio_fprintf(stderr, "shmat(0x%x, NULL, SHM_RND) failed: %s (%d)\n", 
3927                                              shmid, SYSERR, errno);
3928                                 return(-1);
3929                         }
3930 #ifdef sgi
3931                         if(M->flags & MEMF_MPIN) {
3932                                 if( mpin(M->space, M->size) == -1 ) {
3933                                         doio_fprintf(stderr, "mpin(0x%lx, %d) failed:  %s (%d)\n",
3934                                                      M->space, M->size, SYSERR, errno);
3935                             }
3936                         }
3937 #endif
3938                 }
3939                 break;
3940
3941         default:
3942                 doio_fprintf(stderr, "alloc_mem: HELP! Unknown memory space type %d index %d\n",
3943                              Memalloc[me].memtype, mturn);
3944                 break;
3945         }
3946
3947         Memptr = M->space;
3948         Memsize = M->size;
3949
3950         mturn++;
3951         return 0;
3952 }
3953 #endif /* !CRAY */
3954
3955 #ifdef CRAY
3956 int
3957 alloc_mem(nbytes)
3958 int nbytes;
3959 {
3960         char    *cp;
3961         int     ip;
3962         static  char    *malloc_space;
3963
3964         /*
3965          * The "unicos" version of this did some stuff with sbrk;
3966          * this caused problems with async I/O on irix, and now appears
3967          * to be causing problems with FSA I/O on unicos/mk.
3968          */
3969 #ifdef NOTDEF
3970         if (nbytes > Memsize) {
3971                 if ((cp = (char *)sbrk(nbytes - Memsize)) == (char *)-1) {
3972                         doio_fprintf(stderr, "sbrk(%d) failed:  %s (%d)\n",
3973                                      nbytes - Memsize, SYSERR, errno);
3974                         return -1;
3975                 }
3976
3977                 if (Memsize == 0)
3978                         Memptr = cp;
3979                 Memsize += nbytes - Memsize;
3980         }
3981 #else
3982
3983         /* nbytes = -1 means "free all allocated memory" */
3984         if( nbytes == -1 ) {
3985                 free( malloc_space );
3986                 Memptr = NULL;
3987                 Memsize = 0;
3988                 return 0;
3989         }
3990
3991         if( nbytes > Memsize ) {
3992             if( Memsize != 0 )
3993                 free( malloc_space );
3994
3995             if( (cp = malloc_space = malloc( nbytes )) == NULL ) {
3996                 doio_fprintf(stderr, "malloc(%d) failed:  %s (%d)\n",
3997                              nbytes, SYSERR, errno);
3998                 return -1;
3999             }
4000
4001 #ifdef _CRAYT3E
4002             /* T3E requires memory to be aligned on 0x40 word boundaries */
4003             ip = (int)cp;
4004             if( ip & 0x3F != 0 ) {
4005                 doio_fprintf(stderr, "malloc(%d) = 0x%x(0x%x) not aligned by 0x%x\n",
4006                              nbytes, cp, ip, ip & 0x3f);
4007
4008                 free(cp);
4009                 if( (cp = malloc_space = malloc( nbytes + 0x40 )) == NULL ) {
4010                     doio_fprintf(stderr, "malloc(%d) failed:  %s (%d)\n",
4011                                  nbytes, SYSERR, errno);
4012                     return -1;
4013                 }
4014                 ip = (int)cp;
4015                 cp += (0x40 - (ip & 0x3F));
4016             }
4017 #endif /* _CRAYT3E */
4018             Memptr = cp;
4019             Memsize = nbytes;
4020         }
4021 #endif /* NOTDEF */
4022         return 0;
4023 }
4024 #endif /* CRAY */
4025
4026 /*
4027  * Simple function for allocating sds space.  Uses Sdssize and Sdsptr to
4028  * keep track of location and size of currently allocated chunk.
4029  */
4030
4031 #ifdef _CRAY1
4032
4033 int
4034 alloc_sds(nbytes)
4035 int nbytes;
4036 {
4037         int nblks;
4038
4039         if (nbytes > Sdssize) {
4040                 if ((nblks = ssbreak(btoc(nbytes - Sdssize))) == -1) {
4041                         doio_fprintf(stderr, "ssbreak(%d) failed:  %s (%d)\n",
4042                                      btoc(nbytes - Sdssize), SYSERR, errno);
4043                         return -1;
4044                 }
4045
4046                 Sdssize = ctob(nblks);
4047                 Sdsptr = 0;
4048         }
4049
4050         return 0;
4051 }
4052
4053 #else
4054
4055 #ifdef CRAY
4056
4057 int
4058 alloc_sds(nbytes)
4059 int     nbytes;
4060 {
4061         doio_fprintf(stderr,
4062                      "Internal Error - alloc_sds() called on a CRAY2 system\n");
4063         alloc_mem(-1);
4064         exit(E_INTERNAL);
4065 }
4066
4067 #endif
4068
4069 #endif /* _CRAY1 */
4070
4071 /*
4072  * Function to maintain a file descriptor cache, so that doio does not have
4073  * to do so many open() and close() calls.  Descriptors are stored in the
4074  * cache by file name, and open flags.  Each entry also has a _rtc value
4075  * associated with it which is used in aging.  If doio cannot open a file
4076  * because it already has too many open (ie. system limit hit) it will close
4077  * the one in the cache that has the oldest _rtc value.
4078  *
4079  * If alloc_fd() is called with a file of NULL, it will close all descriptors
4080  * in the cache, and free the memory in the cache.
4081  */
4082
4083 int
4084 alloc_fd(file, oflags)
4085 char    *file;
4086 int     oflags;
4087 {
4088         struct fd_cache *fdc;
4089         struct fd_cache *alloc_fdcache(char *file, int oflags);
4090
4091         fdc = alloc_fdcache(file, oflags);
4092         if(fdc != NULL)
4093                 return(fdc->c_fd);
4094         else
4095                 return(-1);
4096 }
4097
4098 struct fd_cache *
4099 alloc_fdcache(file, oflags)
4100 char    *file;
4101 int     oflags;
4102 {
4103         int                     fd;
4104         struct fd_cache         *free_slot, *oldest_slot, *cp;
4105         static int              cache_size = 0;
4106         static struct fd_cache  *cache = NULL;
4107 #ifndef NO_XFS
4108         struct dioattr  finfo;
4109 #endif
4110
4111         /*
4112          * If file is NULL, it means to free up the fd cache.
4113          */
4114
4115         if (file == NULL && cache != NULL) {
4116                 for (cp = cache; cp < &cache[cache_size]; cp++) {
4117                         if (cp->c_fd != -1) {
4118                                 close(cp->c_fd);
4119                         }
4120 #ifndef CRAY
4121                         if (cp->c_memaddr != NULL) {
4122                                 munmap(cp->c_memaddr, cp->c_memlen);
4123                         }
4124 #endif
4125                 }
4126
4127                 free(cache);
4128                 cache = NULL;
4129                 cache_size = 0;
4130                 return 0;
4131         }
4132
4133         free_slot = NULL;
4134         oldest_slot = NULL;
4135
4136         /*
4137          * Look for a fd in the cache.  If one is found, return it directly.
4138          * Otherwise, when this loop exits, oldest_slot will point to the
4139          * oldest fd slot in the cache, and free_slot will point to an
4140          * unoccupied slot if there are any.
4141          */
4142
4143         for (cp = cache; cp != NULL && cp < &cache[cache_size]; cp++) {
4144                 if (cp->c_fd != -1 &&
4145                     cp->c_oflags == oflags &&
4146                     strcmp(cp->c_file, file) == 0) {
4147 #ifdef CRAY
4148                         cp->c_rtc = _rtc();
4149 #else
4150                         cp->c_rtc = Reqno;
4151 #endif
4152                         return cp;
4153                 }
4154
4155                 if (cp->c_fd == -1) {
4156                         if (free_slot == NULL) {
4157                                 free_slot = cp;
4158                         }
4159                 } else {
4160                         if (oldest_slot == NULL || 
4161                             cp->c_rtc < oldest_slot->c_rtc) {
4162                                 oldest_slot = cp;
4163                         }
4164                 }
4165         }
4166
4167         /*
4168          * No matching file/oflags pair was found in the cache.  Attempt to
4169          * open a new fd.
4170          */
4171
4172         if ((fd = open(file, oflags, 0666)) < 0) {
4173                 if (errno != EMFILE) {
4174                         doio_fprintf(stderr,
4175                                      "Could not open file %s with flags %#o (%s): %s (%d)\n",
4176                                      file, oflags, format_oflags(oflags),
4177                                      SYSERR, errno);
4178                         alloc_mem(-1);
4179                         exit(E_SETUP);
4180                 }
4181
4182                 /*
4183                  * If we get here, we have as many open fd's as we can have.
4184                  * Close the oldest one in the cache (pointed to by
4185                  * oldest_slot), and attempt to re-open.
4186                  */
4187
4188                 close(oldest_slot->c_fd);
4189                 oldest_slot->c_fd = -1;
4190                 free_slot = oldest_slot;
4191
4192                 if ((fd = open(file, oflags, 0666)) < 0) {
4193                         doio_fprintf(stderr,
4194                                      "Could not open file %s with flags %#o (%s):  %s (%d)\n",
4195                                      file, oflags, format_oflags(oflags),
4196                                      SYSERR, errno);
4197                         alloc_mem(-1);
4198                         exit(E_SETUP);
4199                 }
4200         }
4201
4202 /*printf("alloc_fd: new file %s flags %#o fd %d\n", file, oflags, fd);*/
4203
4204         /*
4205          * If we get here, fd is our open descriptor.  If free_slot is NULL,
4206          * we need to grow the cache, otherwise free_slot is the slot that
4207          * should hold the fd info.
4208          */
4209
4210         if (free_slot == NULL) {
4211                 cache = (struct fd_cache *)realloc(cache, sizeof(struct fd_cache) * (FD_ALLOC_INCR + cache_size));
4212                 if (cache == NULL) {
4213                         doio_fprintf(stderr, "Could not malloc() space for fd chace");
4214                         alloc_mem(-1);
4215                         exit(E_SETUP);
4216                 }
4217
4218                 cache_size += FD_ALLOC_INCR;
4219
4220                 for (cp = &cache[cache_size-FD_ALLOC_INCR];
4221                      cp < &cache[cache_size]; cp++) {
4222                         cp->c_fd = -1;
4223                 }
4224
4225                 free_slot = &cache[cache_size - FD_ALLOC_INCR];
4226         }
4227
4228         /*
4229          * finally, fill in the cache slot info
4230          */
4231
4232         free_slot->c_fd = fd;
4233         free_slot->c_oflags = oflags;
4234         strcpy(free_slot->c_file, file);
4235 #ifdef CRAY
4236         free_slot->c_rtc = _rtc();
4237 #else
4238         free_slot->c_rtc = Reqno;
4239 #endif
4240
4241 #ifndef NO_XFS
4242         if (oflags & O_DIRECT) {
4243                 if (xfsctl(file, fd, XFS_IOC_DIOINFO, &finfo) == -1) {
4244                         finfo.d_mem = 1;
4245                         finfo.d_miniosz = 1;
4246                         finfo.d_maxiosz = 1;
4247                 }
4248         } else {
4249                 finfo.d_mem = 1;
4250                 finfo.d_miniosz = 1;
4251                 finfo.d_maxiosz = 1;
4252         }
4253
4254         free_slot->c_memalign = finfo.d_mem;
4255         free_slot->c_miniosz = finfo.d_miniosz;
4256         free_slot->c_maxiosz = finfo.d_maxiosz;
4257 #endif
4258 #ifndef CRAY
4259         free_slot->c_memaddr = NULL;
4260         free_slot->c_memlen = 0;
4261 #endif
4262
4263         return free_slot;
4264 }
4265
4266 /*
4267  *
4268  *                      Signal Handling Section
4269  *
4270  *
4271  */
4272
4273 #ifdef sgi
4274 /*
4275  * "caller-id" for signals
4276  */
4277 void
4278 signal_info(int sig, siginfo_t *info, void *v)
4279 {
4280         int haveit = 0;
4281
4282         if(info != NULL) {
4283                 switch(info->si_code) {
4284                 case SI_USER:
4285                         doio_fprintf(stderr,
4286                                      "signal_info: si_signo %d si_errno %d si_code SI_USER pid %d uid %d\n",
4287                                      info->si_signo, info->si_errno, 
4288                                      info->si_pid, info->si_uid);
4289                         haveit = 1;
4290                         break;
4291
4292                 case SI_QUEUE:
4293                         doio_fprintf(stderr, "signal_info  si_signo %d si_code = SI_QUEUE\n",
4294                                      info->si_signo);
4295                         haveit = 1;
4296                         break;
4297                 }
4298
4299                 if( ! haveit ){
4300                         if( (info->si_signo == SIGSEGV) ||
4301                            (info->si_signo == SIGBUS) ){
4302                                 doio_fprintf(stderr, "signal_info  si_signo %d si_errno %d si_code = %d  si_addr=%p  active_mmap_rw=%d havesigint=%d\n",
4303                                              info->si_signo, info->si_errno,
4304                                              info->si_code, info->si_addr,
4305                                              active_mmap_rw,
4306                                              havesigint);
4307                                 haveit = 1;
4308                            }
4309                 }
4310
4311                 if( !haveit ){
4312                         doio_fprintf(stderr, "signal_info: si_signo %d si_errno %d unknown code %d\n",
4313                                      info->si_signo, info->si_errno,
4314                                      info->si_code);
4315                 }
4316         } else {
4317                 doio_fprintf(stderr, "signal_info: sig %d\n", sig);
4318         }
4319 }
4320 #endif
4321
4322 #ifdef sgi
4323 void
4324 cleanup_handler(int sig, siginfo_t *info, void *v)
4325 {
4326         havesigint=1; /* in case there's a followup signal */
4327         /*signal_info(sig, info, v);*/  /* be quiet on "normal" kill */
4328         alloc_mem(-1);
4329         exit(0);
4330 }
4331
4332
4333 void
4334 die_handler(int sig, siginfo_t *info, void *v)
4335 {
4336         doio_fprintf(stderr, "terminating on signal %d\n", sig);
4337         signal_info(sig, info, v);
4338         alloc_mem(-1);
4339         exit(1);
4340 }
4341
4342 void
4343 sigbus_handler(int sig, siginfo_t *info, void *v)
4344 {
4345         /* While we are doing a memcpy to/from an mmapped region we can
4346            get a SIGBUS for a variety of reasons--and not all of them
4347            should be considered failures.
4348
4349            Under normal conditions if we get a SIGINT it means we've been
4350            told to shutdown.  However, if we're currently doing the above-
4351            mentioned memcopy then the kernel will follow that SIGINT with
4352            a SIGBUS.  We can guess that we're in this situation by seeing
4353            that the si_errno field in the siginfo structure has EINTR as
4354            an errno.  (We might make the guess stronger by looking at the
4355            si_addr field to see that it's not faulting off the end of the
4356            mmapped region, but it seems that in such a case havesigint
4357            would not have been set so maybe that doesn't make the guess
4358            stronger.)
4359          */
4360
4361         
4362         if( active_mmap_rw && havesigint && (info->si_errno == EINTR) ){
4363                 cleanup_handler( sig, info, v );
4364         }
4365         else{
4366                 die_handler( sig, info, v );
4367         }
4368 }
4369 #else
4370
4371 void
4372 cleanup_handler()
4373 {
4374         havesigint=1; /* in case there's a followup signal */
4375         alloc_mem(-1);
4376         exit(0);
4377 }
4378
4379 void
4380 die_handler(sig)
4381 int sig;
4382 {
4383         doio_fprintf(stderr, "terminating on signal %d\n", sig);
4384         alloc_mem(-1);
4385         exit(1);
4386 }
4387
4388 #ifndef CRAY
4389 void
4390 sigbus_handler(sig)
4391 int sig;
4392 {
4393         /* See sigbus_handler() in the 'ifdef sgi' case for details.  Here,
4394            we don't have the siginfo stuff so the guess is weaker but we'll
4395            do it anyway.
4396         */
4397
4398         if( active_mmap_rw && havesigint )
4399                 cleanup_handler();
4400         else
4401                 die_handler(sig);
4402 }
4403 #endif /* !CRAY */
4404 #endif /* sgi */
4405
4406
4407 void
4408 noop_handler(sig)
4409 int sig;
4410 {
4411         return;
4412 }
4413
4414
4415 /*
4416  * SIGINT handler for the parent (original doio) process.  It simply sends
4417  * a SIGINT to all of the doio children.  Since they're all in the same
4418  * pgrp, this can be done with a single kill().
4419  */
4420
4421 void
4422 sigint_handler()
4423 {
4424         int     i;
4425
4426         for (i = 0; i < Nchildren; i++) {
4427                 if (Children[i] != -1) {
4428                         kill(Children[i], SIGINT);
4429                 }
4430         }
4431 }
4432
4433 /*
4434  * Signal handler used to inform a process when async io completes.  Referenced
4435  * in do_read() and do_write().  Note that the signal handler is not
4436  * re-registered.
4437  */
4438
4439 void
4440 aio_handler(sig)
4441 int     sig;
4442 {
4443         int             i;
4444         struct aio_info *aiop;
4445
4446         for (i = 0; i < sizeof(Aio_Info) / sizeof(Aio_Info[0]); i++) {
4447                 aiop = &Aio_Info[i];
4448
4449                 if (aiop->strategy == A_SIGNAL && aiop->sig == sig) {
4450                         aiop->signalled++;
4451
4452                         if (aio_done(aiop)) {
4453                                 aiop->done++;
4454                         }
4455                 }
4456         }
4457 }
4458
4459 /*
4460  * dump info on all open aio slots
4461  */
4462 void
4463 dump_aio()
4464 {
4465         int             i, count;
4466
4467         count=0;
4468         for (i = 0; i < sizeof(Aio_Info) / sizeof(Aio_Info[0]); i++) {
4469                 if (Aio_Info[i].busy) {
4470                         count++;
4471                         fprintf(stderr,
4472                                 "Aio_Info[%03d] id=%d fd=%d signal=%d signaled=%d\n",
4473                                 i, Aio_Info[i].id,
4474                                 Aio_Info[i].fd,
4475                                 Aio_Info[i].sig,
4476                                 Aio_Info[i].signalled);
4477                         fprintf(stderr, "\tstrategy=%s\n",
4478                                 format_strat(Aio_Info[i].strategy));
4479                 }
4480         }
4481         fprintf(stderr, "%d active async i/os\n", count);
4482 }
4483
4484
4485 #ifdef sgi
4486 /*
4487  * Signal handler called as a callback, not as a signal.
4488  * 'val' is the value from sigev_value and is assumed to be the
4489  * Aio_Info[] index.
4490  */
4491 void
4492 cb_handler(val)
4493 sigval_t val;
4494 {
4495         struct aio_info *aiop;
4496
4497 /*printf("cb_handler requesting slot %d\n", val.sival_int);*/
4498         aiop = aio_slot( val.sival_int );
4499 /*printf("cb_handler, aiop=%p\n", aiop);*/
4500
4501 /*printf("%d in cb_handler\n", getpid() );*/
4502         if (aiop->strategy == A_CALLBACK) {
4503                 aiop->signalled++;
4504
4505                 if (aio_done(aiop)) {
4506                         aiop->done++;
4507                 }
4508         }
4509 }
4510 #endif
4511
4512 struct aio_info *
4513 aio_slot(aio_id)
4514 int     aio_id;
4515 {
4516         int             i;
4517         static int      id = 1;
4518         struct aio_info *aiop;
4519
4520         aiop = NULL;
4521
4522         for (i = 0; i < sizeof(Aio_Info) / sizeof(Aio_Info[0]); i++) {
4523                 if (aio_id == -1) {
4524                         if (! Aio_Info[i].busy) {
4525                                 aiop = &Aio_Info[i];
4526                                 aiop->busy = 1;
4527                                 aiop->id = id++;
4528                                 break;
4529                         }
4530                 } else {
4531                         if (Aio_Info[i].busy && Aio_Info[i].id == aio_id) {
4532                                 aiop = &Aio_Info[i];
4533                                 break;
4534                         }
4535                 }
4536         }
4537
4538         if( aiop == NULL ){
4539                 doio_fprintf(stderr,"aio_slot(%d) not found.  Request %d\n", 
4540                              aio_id, Reqno);
4541                 dump_aio();
4542                 alloc_mem(-1);
4543                 exit(E_INTERNAL);
4544         }
4545
4546         return aiop;
4547 }
4548
4549 int
4550 aio_register(fd, strategy, sig)
4551 int             fd;
4552 int             strategy;
4553 int             sig;
4554 {
4555         struct aio_info         *aiop;
4556         void                    aio_handler();
4557         struct sigaction        sa;
4558
4559         aiop = aio_slot(-1);
4560
4561         aiop->fd = fd;
4562         aiop->strategy = strategy;
4563         aiop->done = 0;
4564 #ifdef CRAY
4565         bzero((char *)&aiop->iosw, sizeof(aiop->iosw));
4566 #endif
4567
4568         if (strategy == A_SIGNAL) {
4569                 aiop->sig = sig;
4570                 aiop->signalled = 0;
4571
4572                 sa.sa_handler = aio_handler;
4573                 sa.sa_flags = 0;
4574                 sigemptyset(&sa.sa_mask);
4575
4576                 sigaction(sig, &sa, &aiop->osa);
4577         } else {
4578                 aiop->sig = -1;
4579                 aiop->signalled = 0;
4580         }
4581
4582         return aiop->id;
4583 }
4584
4585 int
4586 aio_unregister(aio_id)
4587 int     aio_id;
4588 {
4589         struct aio_info *aiop;
4590
4591         aiop = aio_slot(aio_id);
4592
4593         if (aiop->strategy == A_SIGNAL) {
4594                 sigaction(aiop->sig, &aiop->osa, NULL);
4595         }
4596
4597         aiop->busy = 0;
4598         return 0;
4599 }
4600
4601 #ifndef linux
4602 int
4603 aio_wait(aio_id)
4604 int     aio_id;
4605 {
4606 #ifdef RECALL_SIZEOF
4607         long            mask[RECALL_SIZEOF];
4608 #endif
4609         sigset_t        sigset;
4610         struct aio_info *aiop;
4611 #ifdef CRAY
4612         struct iosw     *ioswlist[1];
4613 #endif
4614 #ifdef sgi
4615         const aiocb_t   *aioary[1];
4616 #endif
4617         int r, cnt;
4618
4619
4620         aiop = aio_slot(aio_id);
4621 /*printf("%d aiop B =%p\n", getpid(), aiop);*/
4622
4623         switch (aiop->strategy) {
4624         case A_POLL:
4625                 while (! aio_done(aiop))
4626                         ;
4627                 break;
4628
4629         case A_SIGNAL:
4630                 sigemptyset(&sigset);
4631                 sighold( aiop->sig );
4632
4633                 while ( !aiop->signalled || !aiop->done ) {
4634                         sigsuspend(&sigset);
4635                         sighold( aiop->sig );
4636                 }
4637                 break;
4638
4639 #ifdef CRAY
4640         case A_RECALL:
4641                 ioswlist[0] = &aiop->iosw;
4642                 if (recall(aiop->fd, 1, ioswlist) < 0) {
4643                         doio_fprintf(stderr, "recall() failed:  %s (%d)\n",
4644                                      SYSERR, errno);
4645                         exit(E_SETUP);
4646                 }
4647                 break;
4648
4649 #ifdef RECALL_SIZEOF
4650
4651         case A_RECALLA:
4652                 RECALL_INIT(mask);
4653                 RECALL_SET(mask, aiop->fd);
4654                 if (recalla(mask) < 0) {
4655                         doio_fprintf(stderr, "recalla() failed:  %s (%d)\n",
4656                                      SYSERR, errno);
4657                         exit(E_SETUP);
4658                 }
4659
4660                 RECALL_CLR(mask, aiop->fd);
4661                 break;
4662 #endif
4663
4664         case A_RECALLS:
4665                 ioswlist[0] = &aiop->iosw;
4666                 if (recalls(1, ioswlist) < 0) {
4667                         doio_fprintf(stderr, "recalls failed:  %s (%d)\n",
4668                                 SYSERR, errno);
4669                         exit(E_SETUP);
4670                 }
4671                 break;
4672 #endif  /* CRAY */
4673
4674 #ifdef sgi
4675         case A_CALLBACK:
4676                 aioary[0] = &aiop->aiocb;
4677                 cnt=0;
4678                 do {
4679                         r = aio_suspend(aioary, 1, NULL);
4680                         if( r == -1 ){
4681                                 doio_fprintf(stderr, "aio_suspend failed: %s (%d)\n",
4682                                              SYSERR, errno );
4683                                 exit(E_SETUP);
4684                         }
4685                         cnt++;
4686                 } while(aiop->done == 0);
4687
4688 #if 0
4689                 /*
4690                  * after having this set for a while, I've decided that
4691                  * it's too noisy
4692                  */
4693                 if(cnt > 1)
4694                         doio_fprintf(stderr, "aio_wait: callback wait took %d tries\n", cnt);
4695 #endif
4696
4697                 /* 
4698                  * Note: cb_handler already calls aio_done
4699                  */
4700                 break;
4701
4702
4703         case A_SUSPEND:
4704                 aioary[0] = &aiop->aiocb;
4705                 r = aio_suspend(aioary, 1, NULL);
4706                 if( r == -1 ){
4707                         doio_fprintf(stderr, "aio_suspend failed: %s (%d)\n",
4708                                      SYSERR, errno );
4709                         exit(E_SETUP);
4710                 }
4711
4712                 aio_done(aiop);
4713                 break;
4714 #endif
4715         }
4716
4717 /*printf("aio_wait: errno %d return %d\n", aiop->aio_errno, aiop->aio_ret);*/
4718
4719         return 0;
4720 }
4721 #endif /* !linux */
4722
4723 /*
4724  * Format specified time into HH:MM:SS format.  t is the time to format
4725  * in seconds (as returned from time(2)).
4726  */
4727
4728 char *
4729 hms(t)
4730 time_t  t;
4731 {
4732         static char     ascii_time[9];
4733         struct tm       *ltime;
4734
4735         ltime = localtime(&t);
4736         strftime(ascii_time, sizeof(ascii_time), "%H:%M:%S", ltime);
4737
4738         return ascii_time;
4739 }
4740
4741 /*
4742  * Simple routine to check if an async io request has completed.
4743  */
4744
4745 int
4746 aio_done(struct aio_info *ainfo)
4747 {
4748 #ifdef CRAY
4749         return ainfo->iosw.sw_flag;
4750 #endif
4751
4752 #ifdef sgi
4753         if( (ainfo->aio_errno = aio_error(&ainfo->aiocb)) == -1 ){
4754                 doio_fprintf(stderr, "aio_done: aio_error failed: %s (%d)\n",
4755                              SYSERR, errno );
4756                 exit(E_SETUP);
4757         }
4758         /*printf("%d aio_done aio_errno=%d\n", getpid(), ainfo->aio_errno);*/
4759         if( ainfo->aio_errno != EINPROGRESS ){
4760                 if( (ainfo->aio_ret = aio_return(&ainfo->aiocb)) == -1 ){
4761                         doio_fprintf(stderr, "aio_done: aio_return failed: %s (%d)\n",
4762                                      SYSERR, errno );
4763                         exit(E_SETUP);
4764                 }
4765         }
4766
4767         return (ainfo->aio_errno != EINPROGRESS);
4768 #else
4769         return -1;   /* invalid */
4770 #endif
4771 }
4772
4773 /*
4774  * Routine to handle upanic() - it first attempts to set the panic flag.  If
4775  * the flag cannot be set, an error message is issued.  A call to upanic
4776  * with PA_PANIC is then done unconditionally, in case the panic flag was set
4777  * from outside the program (as with the panic(8) program).
4778  *
4779  * Note - we only execute the upanic code if -U was used, and the passed in
4780  * mask is set in the Upanic_Conditions bitmask.
4781  */
4782
4783 void
4784 doio_upanic(mask)
4785 int     mask;
4786 {
4787         if (U_opt == 0 || (mask & Upanic_Conditions) == 0) {
4788                 return;
4789         }
4790
4791 #ifdef CRAY
4792         if (upanic(PA_SET) < 0) {
4793                 doio_fprintf(stderr, "WARNING - Could not set the panic flag - upanic(PA_SET) failed:  %s (%d)\n",
4794                              SYSERR, errno);
4795         }
4796
4797         upanic(PA_PANIC);
4798 #endif
4799 #ifdef sgi
4800         syssgi(1005);   /* syssgi test panic - DEBUG kernels only */
4801 #endif
4802         doio_fprintf(stderr, "WARNING - upanic() failed\n");
4803 }
4804
4805 /*
4806  * Parse cmdline options/arguments and set appropriate global variables.
4807  * If the cmdline is valid, return 0 to caller.  Otherwise exit with a status
4808  * of 1.
4809  */
4810
4811 int
4812 parse_cmdline(argc, argv, opts)
4813 int     argc;
4814 char    **argv;
4815 char    *opts;
4816 {
4817         int             c;
4818         char            cc, *cp, *tok = NULL;
4819         extern int      opterr;
4820         extern int      optind;
4821         extern char     *optarg;
4822         struct smap     *s;
4823         char            *memargs[NMEMALLOC];
4824         int             nmemargs, ma;
4825         void            parse_memalloc(char *arg);
4826         void            parse_delay(char *arg);
4827         void            dump_memalloc();
4828
4829         if (*argv[0] == '-') {
4830                 argv[0]++;
4831                 Execd = 1;
4832         }
4833         
4834         if ((Prog = strrchr(argv[0], '/')) == NULL) {
4835                 Prog = argv[0];
4836         } else {
4837                 Prog++;
4838         }
4839         
4840         opterr = 0;
4841         while ((c = getopt(argc, argv, opts)) != EOF) {
4842                 switch ((char)c) {
4843                 case 'a':
4844                         a_opt++;
4845                         break;
4846
4847                 case 'C':
4848                         C_opt++;
4849                         for(s=checkmap; s->string != NULL; s++)
4850                                 if(!strcmp(s->string, optarg))
4851                                         break;
4852                         if (s->string == NULL) {
4853                                 fprintf(stderr,
4854                                         "%s%s:  Illegal -C arg (%s).  Must be one of: ", 
4855                                         Prog, TagName, tok);
4856
4857                                 for (s = checkmap; s->string != NULL; s++)
4858                                         fprintf(stderr, "%s ", s->string);
4859                                 fprintf(stderr, "\n");
4860                                 exit(1);
4861                         }
4862
4863                         switch(s->value) {
4864                         case C_DEFAULT:
4865                                 Data_Fill = doio_pat_fill;
4866                                 Data_Check = doio_pat_check;
4867                                 break;
4868                         default:
4869                                 fprintf(stderr,
4870                                         "%s%s:  Unrecognised -C arg '%s' %d", 
4871                                         Prog, TagName, s->string, s->value);
4872                                 exit(1);
4873                         }
4874                         break;
4875
4876                 case 'd':       /* delay between i/o ops */
4877                         parse_delay(optarg);
4878                         break;
4879
4880                 case 'e':
4881                         if (Npes > 1 && Nprocs > 1) {
4882                                 fprintf(stderr, "%s%s:  Warning - Program is a multi-pe application - exec option is ignored.\n", Prog, TagName);
4883                         }
4884                         e_opt++;
4885                         break;
4886
4887                 case 'h':
4888                         help(stdout);
4889                         exit(0);
4890                         break;
4891
4892                 case 'k':
4893                         k_opt++;
4894                         break;
4895
4896                 case 'm':
4897                         Message_Interval = strtol(optarg, &cp, 10);
4898                         if (*cp != '\0' || Message_Interval < 0) {
4899                                 fprintf(stderr, "%s%s:  Illegal -m arg (%s):  Must be an integer >= 0\n", Prog, TagName, optarg);
4900                                 exit(1);
4901                         }
4902                         m_opt++;
4903                         break;
4904
4905                 case 'M':       /* memory allocation types */
4906 #ifndef CRAY
4907                         nmemargs = string_to_tokens(optarg, memargs, 32, ",");
4908                         for(ma=0; ma < nmemargs; ma++) {
4909                                 parse_memalloc(memargs[ma]);
4910                         }
4911                         /*dump_memalloc();*/
4912 #else
4913                         fprintf(stderr, "%s%s: Error: -M isn't supported on this platform\n", Prog, TagName);
4914                         exit(1);
4915 #endif
4916                         M_opt++;
4917                         break;
4918
4919                 case 'N':
4920                         sprintf( TagName, "(%.39s)", optarg );
4921                         break;
4922
4923                 case 'n':
4924                         Nprocs = strtol(optarg, &cp, 10);
4925                         if (*cp != '\0' || Nprocs < 1) {
4926                                 fprintf(stderr,
4927                                         "%s%s:  Illegal -n arg (%s):  Must be integer > 0\n",
4928                                         Prog, TagName, optarg);
4929                                 exit(E_USAGE);
4930                         }
4931
4932                         if (Npes > 1 && Nprocs > 1) {
4933                                 fprintf(stderr, "%s%s:  Program has been built as a multi-pe app.  -n1 is the only nprocs value allowed\n", Prog, TagName);
4934                                 exit(E_SETUP);
4935                         }
4936                         n_opt++;
4937                         break;
4938
4939                 case 'r':
4940                         Release_Interval = strtol(optarg, &cp, 10);
4941                         if (*cp != '\0' || Release_Interval < 0) {
4942                                 fprintf(stderr,
4943                                         "%s%s:  Illegal -r arg (%s):  Must be integer >= 0\n",
4944                                         Prog, TagName, optarg);
4945                                 exit(E_USAGE);
4946                         }
4947
4948                         r_opt++;
4949                         break;
4950
4951                 case 'w':
4952                         Write_Log = optarg;
4953                         w_opt++;
4954                         break;
4955
4956                 case 'v':
4957                         v_opt++;
4958                         break;
4959
4960                 case 'V':
4961                         if (strcasecmp(optarg, "sync") == 0) {
4962                                 Validation_Flags = O_SYNC;
4963                         } else if (strcasecmp(optarg, "buffered") == 0) {
4964                                 Validation_Flags = 0;
4965 #ifdef CRAY
4966                         } else if (strcasecmp(optarg, "parallel") == 0) {
4967                                 Validation_Flags = O_PARALLEL;
4968                         } else if (strcasecmp(optarg, "ldraw") == 0) {
4969                                 Validation_Flags = O_LDRAW;
4970                         } else if (strcasecmp(optarg, "raw") == 0) {
4971                                 Validation_Flags = O_RAW;
4972 #endif
4973                         } else if (strcasecmp(optarg, "direct") == 0) {
4974                                 Validation_Flags = O_DIRECT;
4975                         } else {
4976                                 if (sscanf(optarg, "%i%c", &Validation_Flags, &cc) != 1) {
4977                                         fprintf(stderr, "%s:  Invalid -V argument (%s) - must be a decimal, hex, or octal\n", Prog, optarg);
4978                                         fprintf(stderr, "    number, or one of the following strings:  'sync',\n");
4979                                         fprintf(stderr, "    'buffered', 'parallel', 'ldraw', or 'raw'\n");
4980                                         exit(E_USAGE);
4981                                 }
4982                         }
4983                         V_opt++;
4984                         break;
4985                 case 'U':
4986                         tok = strtok(optarg, ",");
4987                         while (tok != NULL) {
4988                                 for (s = Upanic_Args; s->string != NULL; s++)
4989                                         if (strcmp(s->string, tok) == 0)
4990                                                 break;
4991
4992                                 if (s->string == NULL) {
4993                                         fprintf(stderr,
4994                                                 "%s%s:  Illegal -U arg (%s).  Must be one of: ", 
4995                                                 Prog, TagName, tok);
4996
4997                                         for (s = Upanic_Args; s->string != NULL; s++)
4998                                                 fprintf(stderr, "%s ", s->string);
4999
5000                                         fprintf(stderr, "\n");
5001
5002                                         exit(1);
5003                                 }
5004
5005                                 Upanic_Conditions |= s->value;
5006                                 tok = strtok(NULL, ",");
5007                         }
5008
5009                         U_opt++;
5010                         break;
5011
5012                 case '?':
5013                         usage(stderr);
5014                         exit(E_USAGE);
5015                         break;
5016                 }
5017         }
5018         
5019         /*
5020          * Supply defaults
5021          */
5022         
5023         if (! C_opt) {
5024                 Data_Fill = doio_pat_fill;
5025                 Data_Check = doio_pat_check;
5026         }
5027
5028         if (! U_opt)
5029                 Upanic_Conditions = 0;
5030
5031         if (! n_opt)
5032                 Nprocs = 1;
5033         
5034         if (! r_opt)
5035                 Release_Interval = DEF_RELEASE_INTERVAL;
5036
5037         if (! M_opt) {
5038                 Memalloc[Nmemalloc].memtype = MEM_DATA;
5039                 Memalloc[Nmemalloc].flags = 0;
5040                 Memalloc[Nmemalloc].name = NULL;
5041                 Memalloc[Nmemalloc].space = NULL;
5042                 Nmemalloc++;
5043         }
5044
5045         /*
5046          * Initialize input stream
5047          */
5048
5049         if (argc == optind) {
5050                 Infile = NULL;
5051         } else {
5052                 Infile = argv[optind++];
5053         }
5054
5055         if (argc != optind) {
5056                 usage(stderr);
5057                 exit(E_USAGE);
5058         }
5059
5060         return 0;
5061 }       
5062
5063
5064
5065 /*
5066  * Parse memory allocation types
5067  *
5068  * Types are:
5069  *  Data
5070  *  T3E-shmem:blksize[:nblks]
5071  *  SysV-shmem:shmid:blksize:nblks
5072  *      if shmid is "private", use IPC_PRIVATE
5073  *      and nblks is not required
5074  *
5075  *  mmap:flags:filename:blksize[:nblks]
5076  *   flags are one of:
5077  *      p - private (MAP_PRIVATE)
5078  *      a - private, MAP_AUTORESRV
5079  *      l - local (MAP_LOCAL)
5080  *      s - shared (nblks required)
5081  *
5082  *   plus any of:
5083  *      f - fixed address (MAP_FIXED)
5084  *      A - use an address without MAP_FIXED
5085  *      a - autogrow (map once at startup)
5086  *
5087  *  mmap:flags:devzero
5088  *      mmap /dev/zero  (shared not allowd)
5089  *      maps the first 4096 bytes of /dev/zero
5090  *
5091  * - put a directory at the beginning of the shared
5092  *   regions saying what pid has what region.
5093  *      DIRMAGIC
5094  *      BLKSIZE
5095  *      NBLKS
5096  *      nblks worth of directories - 1 int pids
5097  */
5098 #ifndef CRAY
5099 void
5100 parse_memalloc(char *arg)
5101 {
5102         char            *allocargs[NMEMALLOC];
5103         int             nalloc;
5104         struct memalloc *M;
5105
5106         if(Nmemalloc >= NMEMALLOC) {
5107                 doio_fprintf(stderr, "Error - too many memory types (%d).\n", 
5108                         Nmemalloc);
5109                 return;
5110         }
5111
5112         M = &Memalloc[Nmemalloc];
5113
5114         nalloc = string_to_tokens(arg, allocargs, 32, ":");
5115         if(!strcmp(allocargs[0], "data")) {
5116                 M->memtype = MEM_DATA;
5117                 M->flags = 0;
5118                 M->name = NULL;
5119                 M->space = NULL;
5120                 Nmemalloc++;
5121                 if(nalloc >= 2) {
5122                         if(strchr(allocargs[1], 'p'))
5123                                 M->flags |= MEMF_MPIN;
5124                 }
5125         } else if(!strcmp(allocargs[0], "mmap")) {
5126                 /* mmap:flags:filename[:size] */
5127                 M->memtype = MEM_MMAP;
5128                 M->flags = 0;
5129                 M->space = NULL;
5130                 if(nalloc >= 1) {
5131                         if(strchr(allocargs[1], 'p'))
5132                                 M->flags |= MEMF_PRIVATE;
5133                         if(strchr(allocargs[1], 'a'))
5134                                 M->flags |= MEMF_AUTORESRV;
5135                         if(strchr(allocargs[1], 'l'))
5136                                 M->flags |= MEMF_LOCAL;
5137                         if(strchr(allocargs[1], 's'))
5138                                 M->flags |= MEMF_SHARED;
5139
5140                         if(strchr(allocargs[1], 'f'))
5141                                 M->flags |= MEMF_FIXADDR;
5142                         if(strchr(allocargs[1], 'A'))
5143                                 M->flags |= MEMF_ADDR;
5144                         if(strchr(allocargs[1], 'G'))
5145                                 M->flags |= MEMF_AUTOGROW;
5146
5147                         if(strchr(allocargs[1], 'U'))
5148                                 M->flags |= MEMF_FILE;
5149                 } else {
5150                         M->flags |= MEMF_PRIVATE;
5151                 }
5152
5153                 if(nalloc > 2) {
5154                         if(!strcmp(allocargs[2], "devzero")) {
5155                                 M->name = "/dev/zero";
5156                                 if(M->flags & 
5157                                    ((MEMF_PRIVATE|MEMF_LOCAL) == 0))
5158                                         M->flags |= MEMF_PRIVATE;
5159                         } else {
5160                                 M->name = allocargs[2];
5161                         }
5162                 } else {
5163                         M->name = "/dev/zero";
5164                         if(M->flags & 
5165                            ((MEMF_PRIVATE|MEMF_LOCAL) == 0))
5166                                 M->flags |= MEMF_PRIVATE;
5167                 }
5168                 Nmemalloc++;
5169
5170         } else if(!strcmp(allocargs[0], "shmem")) {
5171                 /* shmem:shmid:size */
5172                 M->memtype = MEM_SHMEM;
5173                 M->flags = 0;
5174                 M->space = NULL;
5175                 if(nalloc >= 2) {
5176                         M->name = allocargs[1];
5177                 } else {
5178                         M->name = NULL;
5179                 }
5180                 if(nalloc >= 3) {
5181                         sscanf(allocargs[2], "%i", &M->nblks);
5182                 } else {
5183                         M->nblks = 0;
5184                 }
5185                 if(nalloc >= 4) {
5186                         if(strchr(allocargs[3], 'p'))
5187                                 M->flags |= MEMF_MPIN;
5188                 }
5189
5190                 Nmemalloc++;
5191         } else {
5192                 doio_fprintf(stderr, "Error - unknown memory type '%s'.\n",
5193                         allocargs[0]);
5194                 exit(1);
5195         }
5196 }
5197
5198 void
5199 dump_memalloc()
5200 {
5201         int     ma;
5202         char    *mt;
5203
5204         if(Nmemalloc == 0) {
5205                 printf("No memory allocation strategies devined\n");
5206                 return;
5207         }
5208
5209         for(ma=0; ma < Nmemalloc; ma++) {
5210                 switch(Memalloc[ma].memtype) {
5211                 case MEM_DATA:  mt = "data";    break;
5212                 case MEM_SHMEM: mt = "shmem";   break;
5213                 case MEM_MMAP:  mt = "mmap";    break;
5214                 default:        mt = "unknown"; break;
5215                 }
5216                 printf("mstrat[%d] = %d %s\n", ma, Memalloc[ma].memtype, mt);
5217                 printf("\tflags=%#o name='%s' nblks=%d\n",
5218                        Memalloc[ma].flags,
5219                        Memalloc[ma].name,
5220                        Memalloc[ma].nblks);
5221         }
5222 }
5223
5224 #endif /* !CRAY */
5225
5226 /*
5227  * -d <op>:<time> - doio inter-operation delay
5228  *      currently this permits ONE type of delay between operations.
5229  */
5230
5231 void
5232 parse_delay(char *arg)
5233 {
5234         char            *delayargs[NMEMALLOC];
5235         int             ndelay;
5236         struct smap     *s;
5237
5238         ndelay = string_to_tokens(arg, delayargs, 32, ":");
5239         if(ndelay < 2) {
5240                 doio_fprintf(stderr,
5241                         "Illegal delay arg (%s). Must be operation:time\n", arg);
5242                 exit(1);
5243         }
5244         for(s=delaymap; s->string != NULL; s++)
5245                 if(!strcmp(s->string, delayargs[0]))
5246                         break;
5247         if (s->string == NULL) {
5248                 fprintf(stderr,
5249                         "Illegal Delay arg (%s).  Must be one of: ", arg);
5250
5251                 for (s = delaymap; s->string != NULL; s++)
5252                         fprintf(stderr, "%s ", s->string);
5253                 fprintf(stderr, "\n");
5254                 exit(1);
5255         }
5256
5257         delayop = s->value;
5258
5259         sscanf(delayargs[1], "%i", &delaytime);
5260
5261         if(ndelay > 2) {
5262                 fprintf(stderr,
5263                         "Warning: extra delay arguments ignored.\n");
5264         }
5265 }
5266
5267
5268 /*
5269  * Usage clause - obvious
5270  */
5271
5272 int
5273 usage(stream)
5274 FILE    *stream;
5275 {
5276         /*
5277          * Only do this if we are on vpe 0, to avoid seeing it from every
5278          * process in the application.
5279          */
5280
5281         if (Npes > 1 && Vpe != 0) {
5282                 return 0;
5283         }
5284
5285         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);
5286         return 0;
5287 }
5288
5289 void
5290 help(stream)
5291 FILE    *stream;
5292 {
5293         /*
5294          * Only the app running on vpe 0 gets to issue help - this prevents
5295          * everybody in the application from doing this.
5296          */
5297
5298         if (Npes > 1 && Vpe != 0) {
5299                 return;
5300         }
5301
5302         usage(stream);
5303         fprintf(stream, "\n");
5304         fprintf(stream, "\t-a                   abort - kill all doio processes on data compare\n");
5305         fprintf(stream, "\t                     errors.  Normally only the erroring process exits\n");
5306         fprintf(stream, "\t-C data-pattern-type \n");
5307         fprintf(stream, "\t                     Available data patterns are:\n");
5308         fprintf(stream, "\t                     default - repeating pattern\n");
5309         fprintf(stream, "\t-d Operation:Time    Inter-operation delay.\n");
5310         fprintf(stream, "\t                     Operations are:\n");
5311         fprintf(stream, "\t                         select:time (1 second=1000000)\n");
5312         fprintf(stream, "\t                         sleep:time (1 second=1)\n");
5313 #ifdef sgi
5314         fprintf(stream, "\t                         sginap:time (1 second=CLK_TCK=100)\n");
5315 #endif
5316         fprintf(stream, "\t                         alarm:time (1 second=1)\n");
5317         fprintf(stream, "\t-e                   Re-exec children before entering the main\n");
5318         fprintf(stream, "\t                     loop.  This is useful for spreading\n");
5319         fprintf(stream, "\t                     procs around on multi-pe systems.\n");
5320         fprintf(stream, "\t-k                   Lock file regions during writes using fcntl()\n");
5321         fprintf(stream, "\t-v                   Verify writes - this is done by doing a buffered\n");
5322         fprintf(stream, "\t                     read() of the data if file io was done, or\n");
5323         fprintf(stream, "\t                     an ssread()of the data if sds io was done\n");
5324 #ifndef CRAY
5325         fprintf(stream, "\t-M                   Data buffer allocation method\n");
5326         fprintf(stream, "\t                     alloc-type[,type]\n");
5327 #ifdef sgi
5328         fprintf(stream, "\t                         data:flags\n");
5329         fprintf(stream, "\t                             p - mpin buffer\n");
5330         fprintf(stream, "\t                         shmem:shmid:size:flags\n");
5331         fprintf(stream, "\t                             p - mpin buffer\n");
5332 #else
5333         fprintf(stream, "\t                         data\n");
5334         fprintf(stream, "\t                         shmem:shmid:size\n");
5335 #endif /* sgi */
5336         fprintf(stream, "\t                         mmap:flags:filename\n");
5337         fprintf(stream, "\t                             p - private\n");
5338 #ifdef sgi
5339         fprintf(stream, "\t                             s - shared\n");
5340         fprintf(stream, "\t                             l - local\n");
5341         fprintf(stream, "\t                             a - autoresrv\n");
5342         fprintf(stream, "\t                             G - autogrow\n");
5343 #else
5344         fprintf(stream, "\t                             s - shared (shared file must exist\n"),
5345         fprintf(stream, "\t                                 and have needed length)\n");
5346 #endif
5347         fprintf(stream, "\t                             f - fixed address (not used)\n");
5348         fprintf(stream, "\t                             a - specify address (not used)\n");
5349         fprintf(stream, "\t                             U - Unlink file when done\n");
5350         fprintf(stream, "\t                             The default flag is private\n");
5351         fprintf(stream, "\n");
5352 #endif /* !CRAY */
5353         fprintf(stream, "\t-m message_interval  Generate a message every 'message_interval'\n");
5354         fprintf(stream, "\t                     requests.  An interval of 0 suppresses\n");
5355         fprintf(stream, "\t                     messages.  The default is 0.\n");
5356         fprintf(stream, "\t-N tagname           Tag name, for Monster.\n");
5357         fprintf(stream, "\t-n nprocs            # of processes to start up\n");
5358         fprintf(stream, "\t-r release_interval  Release all memory and close\n");
5359         fprintf(stream, "\t                     files every release_interval operations.\n");
5360         fprintf(stream, "\t                     By default procs never release memory\n");
5361         fprintf(stream, "\t                     or close fds unless they have to.\n");
5362         fprintf(stream, "\t-V validation_ftype  The type of file descriptor to use for doing data\n");
5363         fprintf(stream, "\t                     validation.  validation_ftype may be an octal,\n");
5364         fprintf(stream, "\t                     hex, or decimal number representing the open()\n");
5365         fprintf(stream, "\t                     flags, or may be one of the following strings:\n");
5366         fprintf(stream, "\t                     'buffered' - validate using bufferd read\n");
5367         fprintf(stream, "\t                     'sync'     - validate using O_SYNC read\n");
5368         fprintf(stream, "\t                     'direct    - validate using O_DIRECT read'\n");
5369 #ifdef CRAY
5370         fprintf(stream, "\t                     'ldraw'    - validate using O_LDRAW read\n");
5371         fprintf(stream, "\t                     'parallel' - validate using O_PARALLEL read\n");
5372         fprintf(stream, "\t                     'raw'      - validate using O_RAW read\n");
5373 #endif
5374         fprintf(stream, "\t                     By default, 'parallel'\n");
5375         fprintf(stream, "\t                     is used if the write was done with O_PARALLEL\n");
5376         fprintf(stream, "\t                     or 'buffered' for all other writes.\n");
5377         fprintf(stream, "\t-w write_log         File to log file writes to.  The doio_check\n");
5378         fprintf(stream, "\t                     program can reconstruct datafiles using the\n");
5379         fprintf(stream, "\t                     write_log, and detect if a file is corrupt\n");
5380         fprintf(stream, "\t                     after all procs have exited.\n");
5381         fprintf(stream, "\t-U upanic_cond       Comma separated list of conditions that will\n");
5382         fprintf(stream, "\t                     cause a call to upanic(PA_PANIC).\n");
5383         fprintf(stream, "\t                     'corruption' -> upanic on bad data comparisons\n");
5384         fprintf(stream, "\t                     'iosw'     ---> upanic on unexpected async iosw\n");
5385         fprintf(stream, "\t                     'rval'     ---> upanic on unexpected syscall rvals\n");
5386         fprintf(stream, "\t                     'all'      ---> all of the above\n");
5387         fprintf(stream, "\n");
5388         fprintf(stream, "\tinfile               Input stream - default is stdin - must be a list\n");
5389         fprintf(stream, "\t                     of io_req structures (see doio.h).  Currently\n");
5390         fprintf(stream, "\t                     only the iogen program generates the proper\n");
5391         fprintf(stream, "\t                     format\n");
5392 }       
5393