xfstests updates - rework build to be like other xfs packages, revive some old fs...
[xfstests-dev.git] / ltp / fsx.c
1 /*
2  * Copyright (C) 1991, NeXT Computer, Inc.  All Rights Reserverd.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  * 
6  * The contents of this file constitute Original Code as defined in and
7  * are subject to the Apple Public Source License Version 1.1 (the
8  * "License").  You may not use this file except in compliance with the
9  * License.  Please obtain a copy of the License at
10  * http://www.apple.com/publicsource and read it before using this file.
11  * 
12  * This Original Code and all software distributed under the License are
13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17  * License for the specific language governing rights and limitations
18  * under the License.
19  * 
20  * @APPLE_LICENSE_HEADER_END@
21  *
22  *
23  *      File:   fsx.c
24  *      Author: Avadis Tevanian, Jr.
25  *
26  *      File system exerciser. 
27  *
28  *      Rewritten 8/98 by Conrad Minshall.
29  *
30  *      Small changes to work under Linux -- davej.
31  *
32  */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <limits.h>
38 #include <time.h>
39 #include <strings.h>
40 #include <sys/file.h>
41 #include <sys/mman.h>
42 #include <limits.h>
43 #include <err.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <stdarg.h>
50 #include <errno.h>
51
52 #ifndef MAP_FILE
53 # define MAP_FILE 0
54 #endif
55
56 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
57
58 /*
59  *      A log entry is an operation and a bunch of arguments.
60  */
61
62 struct log_entry {
63         int     operation;
64         int     args[3];
65 };
66
67 #define LOGSIZE 1000
68
69 struct log_entry        oplog[LOGSIZE]; /* the log */
70 int                     logptr = 0;     /* current position in log */
71 int                     logcount = 0;   /* total ops */
72
73 /*
74  *      Define operations
75  */
76
77 #define OP_READ         1
78 #define OP_WRITE        2
79 #define OP_TRUNCATE     3
80 #define OP_CLOSEOPEN    4
81 #define OP_MAPREAD      5
82 #define OP_MAPWRITE     6
83 #define OP_SKIPPED      7
84
85 #ifndef PAGE_SIZE
86 #define PAGE_SIZE       getpagesize()
87 #endif
88 #define PAGE_MASK       (PAGE_SIZE - 1)
89
90 char    *original_buf;                  /* a pointer to the original data */
91 char    *good_buf;                      /* a pointer to the correct data */
92 char    *temp_buf;                      /* a pointer to the current data */
93 char    *fname;                         /* name of our test file */
94 int     fd;                             /* fd for our test file */
95
96 off_t           file_size = 0;
97 off_t           biggest = 0;
98 char            state[256];
99 unsigned long   testcalls = 0;          /* calls to function "test" */
100
101 unsigned long   simulatedopcount = 0;   /* -b flag */
102 int     closeprob = 0;                  /* -c flag */
103 int     debug = 0;                      /* -d flag */
104 unsigned long   debugstart = 0;         /* -D flag */
105 unsigned long   maxfilelen = 256 * 1024;        /* -l flag */
106 int     sizechecks = 1;                 /* -n flag disables them */
107 int     maxoplen = 64 * 1024;           /* -o flag */
108 int     quiet = 0;                      /* -q flag */
109 unsigned long progressinterval = 0;     /* -p flag */
110 int     readbdy = 1;                    /* -r flag */
111 int     style = 0;                      /* -s flag */
112 int     truncbdy = 1;                   /* -t flag */
113 int     writebdy = 1;                   /* -w flag */
114 long    monitorstart = -1;              /* -m flag */
115 long    monitorend = -1;                /* -m flag */
116 int     lite = 0;                       /* -L flag */
117 long    numops = -1;                    /* -N flag */
118 int     randomoplen = 1;                /* -O flag disables it */
119 int     seed = 1;                       /* -S flag */
120 int     mapped_writes = 1;              /* -W flag disables */
121 int     mapped_reads = 1;               /* -R flag disables it */
122 int     fsxgoodfd = 0;
123 FILE *  fsxlogf = NULL;
124 int badoff = -1;
125 int closeopen = 0;
126
127
128 void
129 prt(char *fmt, ...)
130 {
131         va_list args;
132
133         va_start(args, fmt);
134         vfprintf(stdout, fmt, args);
135         if (fsxlogf)
136                 vfprintf(fsxlogf, fmt, args);
137         va_end(args);
138 }
139
140 void
141 prterr(char *prefix)
142 {
143         prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
144 }
145
146
147 void
148 log4(int operation, int arg0, int arg1, int arg2)
149 {
150         struct log_entry *le;
151
152         le = &oplog[logptr];
153         le->operation = operation;
154         if (closeopen)
155                 le->operation = ~ le->operation;
156         le->args[0] = arg0;
157         le->args[1] = arg1;
158         le->args[2] = arg2;
159         logptr++;
160         logcount++;
161         if (logptr >= LOGSIZE)
162                 logptr = 0;
163 }
164
165
166 void
167 logdump(void)
168 {
169         int     i, count, down;
170         struct log_entry        *lp;
171
172         prt("LOG DUMP (%d total operations):\n", logcount);
173         if (logcount < LOGSIZE) {
174                 i = 0;
175                 count = logcount;
176         } else {
177                 i = logptr;
178                 count = LOGSIZE;
179         }
180         for ( ; count > 0; count--) {
181                 int opnum;
182
183                 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
184                 prt("%d(%d mod 256): ", opnum, opnum%256);
185                 lp = &oplog[i];
186                 if ((closeopen = lp->operation < 0))
187                         lp->operation = ~ lp->operation;
188                         
189                 switch (lp->operation) {
190                 case OP_MAPREAD:
191                         prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
192                             lp->args[0], lp->args[0] + lp->args[1] - 1,
193                             lp->args[1]);
194                         if (badoff >= lp->args[0] && badoff <
195                                                      lp->args[0] + lp->args[1])
196                                 prt("\t***RRRR***");
197                         break;
198                 case OP_MAPWRITE:
199                         prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
200                             lp->args[0], lp->args[0] + lp->args[1] - 1,
201                             lp->args[1]);
202                         if (badoff >= lp->args[0] && badoff <
203                                                      lp->args[0] + lp->args[1])
204                                 prt("\t******WWWW");
205                         break;
206                 case OP_READ:
207                         prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
208                             lp->args[0], lp->args[0] + lp->args[1] - 1,
209                             lp->args[1]);
210                         if (badoff >= lp->args[0] &&
211                             badoff < lp->args[0] + lp->args[1])
212                                 prt("\t***RRRR***");
213                         break;
214                 case OP_WRITE:
215                         prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
216                             lp->args[0], lp->args[0] + lp->args[1] - 1,
217                             lp->args[1]);
218                         if (lp->args[0] > lp->args[2])
219                                 prt(" HOLE");
220                         else if (lp->args[0] + lp->args[1] > lp->args[2])
221                                 prt(" EXTEND");
222                         if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
223                             badoff < lp->args[0] + lp->args[1])
224                                 prt("\t***WWWW");
225                         break;
226                 case OP_TRUNCATE:
227                         down = lp->args[0] < lp->args[1];
228                         prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
229                             down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
230                         if (badoff >= lp->args[!down] &&
231                             badoff < lp->args[!!down])
232                                 prt("\t******WWWW");
233                         break;
234                 case OP_SKIPPED:
235                         prt("SKIPPED (no operation)");
236                         break;
237                 default:
238                         prt("BOGUS LOG ENTRY (operation code = %d)!",
239                             lp->operation);
240                 }
241                 if (closeopen)
242                         prt("\n\t\tCLOSE/OPEN");
243                 prt("\n");
244                 i++;
245                 if (i == LOGSIZE)
246                         i = 0;
247         }
248 }
249
250
251 void
252 save_buffer(char *buffer, off_t bufferlength, int fd)
253 {
254         off_t ret;
255         ssize_t byteswritten;
256
257         if (fd <= 0 || bufferlength == 0)
258                 return;
259
260         if (bufferlength > SSIZE_MAX) {
261                 prt("fsx flaw: overflow in save_buffer\n");
262                 exit(67);
263         }
264         if (lite) {
265                 off_t size_by_seek = lseek(fd, (off_t)0, L_XTND);
266                 if (size_by_seek == (off_t)-1)
267                         prterr("save_buffer: lseek eof");
268                 else if (bufferlength > size_by_seek) {
269                                                  fprintf(stderr, "save_buffer: .fsxgood file too short... will save 0x%qx bytes instead of 0x%qx\n", (unsigned long long)size_by_seek,
270                              (unsigned long long)bufferlength);
271                         bufferlength = size_by_seek;
272                 }
273         }
274
275         ret = lseek(fd, (off_t)0, SEEK_SET);
276         if (ret == (off_t)-1)
277                 prterr("save_buffer: lseek 0");
278         
279         byteswritten = write(fd, buffer, (size_t)bufferlength);
280         if (byteswritten != bufferlength) {
281                 if (byteswritten == -1)
282                         prterr("save_buffer write");
283                 else
284                                                  fprintf(stderr, "save_buffer: short write, 0x%x bytes instead of 0x%qx\n",
285                              (unsigned)byteswritten,
286                              (unsigned long long)bufferlength);
287         }
288 }
289
290
291 void
292 report_failure(int status)
293 {
294         logdump();
295         
296         if (fsxgoodfd) {
297                 if (good_buf) {
298                         save_buffer(good_buf, file_size, fsxgoodfd);
299                         prt("Correct content saved for comparison\n");
300                         prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
301                             fname, fname);
302                 }
303                 close(fsxgoodfd);
304         }
305         exit(status);
306 }
307
308
309 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
310                                         *(((unsigned char *)(cp)) + 1)))
311
312 void
313 check_buffers(unsigned offset, unsigned size)
314 {
315         unsigned char c, t;
316         unsigned i = 0;
317         unsigned n = 0;
318         unsigned op = 0;
319         unsigned bad = 0;
320
321         if (bcmp(good_buf + offset, temp_buf, size) != 0) {
322                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
323                     offset, size);
324                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
325                 while (size > 0) {
326                         c = good_buf[offset];
327                         t = temp_buf[i];
328                         if (c != t) {
329                                 if (n == 0) {
330                                         bad = short_at(&temp_buf[i]);
331                                         prt("0x%5x\t0x%04x\t0x%04x", offset,
332                                             short_at(&good_buf[offset]), bad);
333                                         op = temp_buf[offset & 1 ? i+1 : i];
334                                 }
335                                 n++;
336                                 badoff = offset;
337                         }
338                         offset++;
339                         i++;
340                         size--;
341                 }
342                 if (n) {
343                         prt("\t0x%5x\n", n);
344                         if (bad)
345                                 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
346                         else
347                                 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
348                 } else
349                         prt("????????????????\n");
350                 report_failure(110);
351         }
352 }
353
354
355 void
356 check_size(void)
357 {
358         struct stat     statbuf;
359         off_t   size_by_seek;
360
361         if (fstat(fd, &statbuf)) {
362                 prterr("check_size: fstat");
363                 statbuf.st_size = -1;
364         }
365         size_by_seek = lseek(fd, (off_t)0, L_XTND);
366         if (file_size != statbuf.st_size || file_size != size_by_seek) {
367                 prt("Size error: expected 0x%qx stat 0x%qx seek 0x%qx\n",
368                     (unsigned long long)file_size,
369                     (unsigned long long)statbuf.st_size,
370                     (unsigned long long)size_by_seek);
371                 report_failure(120);
372         }
373 }
374
375
376 void
377 check_trunc_hack(void)
378 {
379         struct stat statbuf;
380
381         ftruncate(fd, (off_t)0);
382         ftruncate(fd, (off_t)100000);
383         fstat(fd, &statbuf);
384         if (statbuf.st_size != (off_t)100000) {
385                 prt("no extend on truncate! not posix!\n");
386                 exit(130);
387         }
388         ftruncate(fd, 0);
389 }
390
391
392 void
393 doread(unsigned offset, unsigned size)
394 {
395         off_t ret;
396         unsigned iret;
397
398         offset -= offset % readbdy;
399         if (size == 0) {
400                 if (!quiet && testcalls > simulatedopcount)
401                         prt("skipping zero size read\n");
402                 log4(OP_SKIPPED, OP_READ, offset, size);
403                 return;
404         }
405         if (size + offset > file_size) {
406                 if (!quiet && testcalls > simulatedopcount)
407                         prt("skipping seek/read past end of file\n");
408                 log4(OP_SKIPPED, OP_READ, offset, size);
409                 return;
410         }
411
412         log4(OP_READ, offset, size, 0);
413
414         if (testcalls <= simulatedopcount)
415                 return;
416
417         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
418                        (debug &&
419                         (monitorstart == -1 ||
420                          (offset + size > monitorstart &&
421                           (monitorend == -1 || offset <= monitorend))))))
422                 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
423                     offset, offset + size - 1, size);
424         ret = lseek(fd, (off_t)offset, SEEK_SET);
425         if (ret == (off_t)-1) {
426                 prterr("doread: lseek");
427                 report_failure(140);
428         }
429         iret = read(fd, temp_buf, size);
430         if (iret != size) {
431                 if (iret == -1)
432                         prterr("doread: read");
433                 else
434                         prt("short read: 0x%x bytes instead of 0x%x\n",
435                             iret, size);
436                 report_failure(141);
437         }
438         check_buffers(offset, size);
439 }
440
441
442 void
443 domapread(unsigned offset, unsigned size)
444 {
445         unsigned pg_offset;
446         unsigned map_size;
447         char    *p;
448
449         offset -= offset % readbdy;
450         if (size == 0) {
451                 if (!quiet && testcalls > simulatedopcount)
452                         prt("skipping zero size read\n");
453                 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
454                 return;
455         }
456         if (size + offset > file_size) {
457                 if (!quiet && testcalls > simulatedopcount)
458                         prt("skipping seek/read past end of file\n");
459                 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
460                 return;
461         }
462
463         log4(OP_MAPREAD, offset, size, 0);
464
465         if (testcalls <= simulatedopcount)
466                 return;
467
468         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
469                        (debug &&
470                         (monitorstart == -1 ||
471                          (offset + size > monitorstart &&
472                           ((monitorend == -1 || offset <= monitorend)))))))
473                 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
474                     offset, offset + size - 1, size);
475
476         pg_offset = offset & PAGE_MASK;
477         map_size  = pg_offset + size;
478
479         if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
480                               (off_t)(offset - pg_offset))) == (char *)-1) {
481                 prterr("domapread: mmap");
482                 report_failure(190);
483         }
484         memcpy(temp_buf, p + pg_offset, size);
485         if (munmap(p, map_size) != 0) {
486                 prterr("domapread: munmap");
487                 report_failure(191);
488         }
489
490         check_buffers(offset, size);
491 }
492
493
494 void
495 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
496 {
497         while (size--) {
498                 good_buf[offset] = testcalls % 256; 
499                 if (offset % 2)
500                         good_buf[offset] += original_buf[offset];
501                 offset++;
502         }
503 }
504
505
506 void
507 dowrite(unsigned offset, unsigned size)
508 {
509         off_t ret;
510         unsigned iret;
511
512         offset -= offset % writebdy;
513         if (size == 0) {
514                 if (!quiet && testcalls > simulatedopcount)
515                         prt("skipping zero size write\n");
516                 log4(OP_SKIPPED, OP_WRITE, offset, size);
517                 return;
518         }
519
520         log4(OP_WRITE, offset, size, file_size);
521
522         gendata(original_buf, good_buf, offset, size);
523         if (file_size < offset + size) {
524                 if (file_size < offset)
525                         bzero(good_buf + file_size, offset - file_size);
526                 file_size = offset + size;
527                 if (lite) {
528                                                  fprintf(stderr, "Lite file size bug in fsx!");
529                         report_failure(149);
530                 }
531         }
532
533         if (testcalls <= simulatedopcount)
534                 return;
535         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
536                        (debug &&
537                         (monitorstart == -1 ||
538                          (offset + size > monitorstart &&
539                           ((monitorend == -1 || offset <= monitorend)))))))
540                 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
541                     offset, offset + size - 1, size);
542         ret = lseek(fd, (off_t)offset, SEEK_SET);
543         if (ret == (off_t)-1) {
544                 prterr("dowrite: lseek");
545                 report_failure(150);
546         }
547         iret = write(fd, good_buf + offset, size);
548         if (iret != size) {
549                 if (iret == -1)
550                         prterr("dowrite: write");
551                 else
552                         prt("short write: 0x%x bytes instead of 0x%x\n",
553                             iret, size);
554                 report_failure(151);
555         }
556 }
557
558
559 void
560 domapwrite(unsigned offset, unsigned size)
561 {
562         unsigned pg_offset;
563         unsigned map_size;
564         off_t    cur_filesize;
565         char    *p;
566
567         offset -= offset % writebdy;
568         if (size == 0) {
569                 if (!quiet && testcalls > simulatedopcount)
570                         prt("skipping zero size write\n");
571                 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
572                 return;
573         }
574         cur_filesize = file_size;
575
576         log4(OP_MAPWRITE, offset, size, 0);
577
578         gendata(original_buf, good_buf, offset, size);
579         if (file_size < offset + size) {
580                 if (file_size < offset)
581                         bzero(good_buf + file_size, offset - file_size);
582                 file_size = offset + size;
583                 if (lite) {
584                                                  fprintf(stderr, "Lite file size bug in fsx!");
585                         report_failure(200);
586                 }
587         }
588
589         if (testcalls <= simulatedopcount)
590                 return;
591
592         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
593                        (debug &&
594                         (monitorstart == -1 ||
595                          (offset + size > monitorstart &&
596                           (monitorend == -1 || offset <= monitorend))))))
597                 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
598                     offset, offset + size - 1, size);
599
600         if (file_size > cur_filesize) {
601                 if (ftruncate(fd, file_size) == -1) {
602                         prterr("domapwrite: ftruncate");
603                         exit(201);
604                 }
605         }
606         pg_offset = offset & PAGE_MASK;
607         map_size  = pg_offset + size;
608
609         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
610                               MAP_FILE | MAP_SHARED, fd,
611                               (off_t)(offset - pg_offset))) == (char *)-1) {
612                 prterr("domapwrite: mmap");
613                 report_failure(202);
614         }
615         memcpy(p + pg_offset, good_buf + offset, size);
616         if (msync(p, map_size, 0) != 0) {
617                 prterr("domapwrite: msync");
618                 report_failure(203);
619         }
620         if (munmap(p, map_size) != 0) {
621                 prterr("domapwrite: munmap");
622                 report_failure(204);
623         }
624 }
625
626
627 void
628 dotruncate(unsigned size)
629 {
630         int oldsize = file_size;
631
632         size -= size % truncbdy;
633         if (size > biggest) {
634                 biggest = size;
635                 if (!quiet && testcalls > simulatedopcount)
636                         prt("truncating to largest ever: 0x%x\n", size);
637         }
638
639         log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
640
641         if (size > file_size)
642                 bzero(good_buf + file_size, size - file_size);
643         file_size = size;
644
645         if (testcalls <= simulatedopcount)
646                 return;
647         
648         if ((progressinterval && !(testcalls % progressinterval)) ||
649             (debug && (monitorstart == -1 || monitorend == -1 ||
650                       size <= monitorend)))
651                 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
652         if (ftruncate(fd, (off_t)size) == -1) {
653                 prt("ftruncate1: %x\n", size);
654                 prterr("dotruncate: ftruncate");
655                 report_failure(160);
656         }
657 }
658
659
660 void
661 writefileimage()
662 {
663         ssize_t iret;
664
665         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
666                 prterr("writefileimage: lseek");
667                 report_failure(171);
668         }
669         iret = write(fd, good_buf, file_size);
670         if ((off_t)iret != file_size) {
671                 if (iret == -1)
672                         prterr("writefileimage: write");
673                 else
674                         prt("short write: 0x%x bytes instead of 0x%qx\n",
675                             iret, (unsigned long long)file_size);
676                 report_failure(172);
677         }
678         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
679                 prt("ftruncate2: %qx\n", (unsigned long long)file_size);
680                 prterr("writefileimage: ftruncate");
681                 report_failure(173);
682         }
683 }
684
685
686 void
687 docloseopen(void)
688
689         if (testcalls <= simulatedopcount)
690                 return;
691
692         if (debug)
693                 prt("%lu close/open\n", testcalls);
694         if (close(fd)) {
695                 prterr("docloseopen: close");
696                 report_failure(180);
697         }
698         fd = open(fname, O_RDWR, 0);
699         if (fd < 0) {
700                 prterr("docloseopen: open");
701                 report_failure(181);
702         }
703 }
704
705
706 void
707 test(void)
708 {
709         unsigned long   offset;
710         unsigned long   size = maxoplen;
711         unsigned long   rv = random();
712         unsigned long   op = rv % (3 + !lite + mapped_writes);
713
714         /* turn off the map read if necessary */
715
716         if (op == 2 && !mapped_reads)
717             op = 0;
718
719         if (simulatedopcount > 0 && testcalls == simulatedopcount)
720                 writefileimage();
721
722         testcalls++;
723
724         if (closeprob)
725                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
726
727         if (debugstart > 0 && testcalls >= debugstart)
728                 debug = 1;
729
730         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
731                 prt("%lu...\n", testcalls);
732
733         /*
734          * READ:        op = 0
735          * WRITE:       op = 1
736          * MAPREAD:     op = 2
737          * TRUNCATE:    op = 3
738          * MAPWRITE:    op = 3 or 4
739          */
740         if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
741                 dotruncate(random() % maxfilelen);
742         else {
743                 if (randomoplen)
744                         size = random() % (maxoplen+1);
745                 if (lite ? 0 : op == 3)
746                         dotruncate(size);
747                 else {
748                         offset = random();
749                         if (op == 1 || op == (lite ? 3 : 4)) {
750                                 offset %= maxfilelen;
751                                 if (offset + size > maxfilelen)
752                                         size = maxfilelen - offset;
753                                 if (op != 1)
754                                         domapwrite(offset, size);
755                                 else
756                                         dowrite(offset, size);
757                         } else {
758                                 if (file_size)
759                                         offset %= file_size;
760                                 else
761                                         offset = 0;
762                                 if (offset + size > file_size)
763                                         size = file_size - offset;
764                                 if (op != 0)
765                                         domapread(offset, size);
766                                 else
767                                         doread(offset, size);
768                         }
769                 }
770         }
771         if (sizechecks && testcalls > simulatedopcount)
772                 check_size();
773         if (closeopen)
774                 docloseopen();
775 }
776
777
778 void
779 cleanup(sig)
780         int     sig;
781 {
782         if (sig)
783                 prt("signal %d\n", sig);
784         prt("testcalls = %lu\n", testcalls);
785         exit(sig);
786 }
787
788
789 void
790 usage(void)
791 {
792         fprintf(stdout, "usage: %s",
793                 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
794         -b opnum: beginning operation number (default 1)\n\
795         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
796         -d: debug output for all operations\n\
797         -l flen: the upper bound on file size (default 262144)\n\
798         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
799         -n: no verifications of file size\n\
800         -o oplen: the upper bound on operation size (default 65536)\n\
801         -p progressinterval: debug output at specified operation interval\n\
802         -q: quieter operation\n\
803         -r readbdy: 4096 would make reads page aligned (default 1)\n\
804         -s style: 1 gives smaller truncates (default 0)\n\
805         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
806         -w writebdy: 4096 would make writes page aligned (default 1)\n\
807         -D startingop: debug output starting at specified operation\n\
808         -L: fsxLite - no file creations & no file size changes\n\
809         -N numops: total # operations to do (default infinity)\n\
810         -O: use oplen (see -o flag) for every op (default random)\n\
811         -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
812         -S seed: for random # generator (default 1) 0 gets timestamp\n\
813         -W: mapped write operations DISabled\n\
814         -R: read() system calls only (mapped reads disabled)\n\
815         fname: this filename is REQUIRED (no default)\n");
816         exit(90);
817 }
818
819
820 int
821 getnum(char *s, char **e)
822 {
823         int ret = -1;
824
825         *e = (char *) 0;
826         ret = strtol(s, e, 0);
827         if (*e)
828                 switch (**e) {
829                 case 'b':
830                 case 'B':
831                         ret *= 512;
832                         *e = *e + 1;
833                         break;
834                 case 'k':
835                 case 'K':
836                         ret *= 1024;
837                         *e = *e + 1;
838                         break;
839                 case 'm':
840                 case 'M':
841                         ret *= 1024*1024;
842                         *e = *e + 1;
843                         break;
844                 case 'w':
845                 case 'W':
846                         ret *= 4;
847                         *e = *e + 1;
848                         break;
849                 }
850         return (ret);
851 }
852
853
854 int
855 main(int argc, char **argv)
856 {
857         int     i, style, ch;
858         char    *endp;
859         char goodfile[1024];
860         char logfile[1024];
861
862         goodfile[0] = 0;
863         logfile[0] = 0;
864
865         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
866
867         while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
868                != EOF)
869                 switch (ch) {
870                 case 'b':
871                         simulatedopcount = getnum(optarg, &endp);
872                         if (!quiet)
873                                 fprintf(stdout, "Will begin at operation %ld\n",
874                                         simulatedopcount);
875                         if (simulatedopcount == 0)
876                                 usage();
877                         simulatedopcount -= 1;
878                         break;
879                 case 'c':
880                         closeprob = getnum(optarg, &endp);
881                         if (!quiet)
882                                 fprintf(stdout,
883                                         "Chance of close/open is 1 in %d\n",
884                                         closeprob);
885                         if (closeprob <= 0)
886                                 usage();
887                         break;
888                 case 'd':
889                         debug = 1;
890                         break;
891                 case 'l':
892                         maxfilelen = getnum(optarg, &endp);
893                         if (maxfilelen <= 0)
894                                 usage();
895                         break;
896                 case 'm':
897                         monitorstart = getnum(optarg, &endp);
898                         if (monitorstart < 0)
899                                 usage();
900                         if (!endp || *endp++ != ':')
901                                 usage();
902                         monitorend = getnum(endp, &endp);
903                         if (monitorend < 0)
904                                 usage();
905                         if (monitorend == 0)
906                                 monitorend = -1; /* aka infinity */
907                         debug = 1;
908                 case 'n':
909                         sizechecks = 0;
910                         break;
911                 case 'o':
912                         maxoplen = getnum(optarg, &endp);
913                         if (maxoplen <= 0)
914                                 usage();
915                         break;
916                 case 'p':
917                         progressinterval = getnum(optarg, &endp);
918                         if (progressinterval < 0)
919                                 usage();
920                         break;
921                 case 'q':
922                         quiet = 1;
923                         break;
924                 case 'r':
925                         readbdy = getnum(optarg, &endp);
926                         if (readbdy <= 0)
927                                 usage();
928                         break;
929                 case 's':
930                         style = getnum(optarg, &endp);
931                         if (style < 0 || style > 1)
932                                 usage();
933                         break;
934                 case 't':
935                         truncbdy = getnum(optarg, &endp);
936                         if (truncbdy <= 0)
937                                 usage();
938                         break;
939                 case 'w':
940                         writebdy = getnum(optarg, &endp);
941                         if (writebdy <= 0)
942                                 usage();
943                         break;
944                 case 'D':
945                         debugstart = getnum(optarg, &endp);
946                         if (debugstart < 1)
947                                 usage();
948                         break;
949                 case 'L':
950                         lite = 1;
951                         break;
952                 case 'N':
953                         numops = getnum(optarg, &endp);
954                         if (numops < 0)
955                                 usage();
956                         break;
957                 case 'O':
958                         randomoplen = 0;
959                         break;
960                 case 'P':
961                         strncpy(goodfile, optarg, sizeof(goodfile));
962                         strcat(goodfile, "/");
963                         strncpy(logfile, optarg, sizeof(logfile));
964                         strcat(logfile, "/");
965                         break;
966                 case 'R':
967                         mapped_reads = 0;
968                         break;
969                 case 'S':
970                         seed = getnum(optarg, &endp);
971                         if (seed == 0)
972                                 seed = time(0) % 10000;
973                         if (!quiet)
974                                 fprintf(stdout, "Seed set to %d\n", seed);
975                         if (seed < 0)
976                                 usage();
977                         break;
978                 case 'W':
979                         mapped_writes = 0;
980                         if (!quiet)
981                                 fprintf(stdout, "mapped writes DISABLED\n");
982                         break;
983               
984                 default:
985                         usage();
986                         /* NOTREACHED */
987                 }
988         argc -= optind;
989         argv += optind;
990         if (argc != 1)
991                 usage();
992         fname = argv[0];
993
994
995         signal(SIGHUP,  cleanup);
996         signal(SIGINT,  cleanup);
997         signal(SIGPIPE, cleanup);
998         signal(SIGALRM, cleanup);
999         signal(SIGTERM, cleanup);
1000         signal(SIGXCPU, cleanup);
1001         signal(SIGXFSZ, cleanup);
1002         signal(SIGVTALRM,       cleanup);
1003         signal(SIGUSR1, cleanup);
1004         signal(SIGUSR2, cleanup);
1005
1006         initstate(seed, state, 256);
1007         setstate(state);
1008         fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1009         if (fd < 0) {
1010                 prterr(fname);
1011                 exit(91);
1012         }
1013         strncat(goodfile, fname, 256);
1014         strcat (goodfile, ".fsxgood");
1015         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1016         if (fsxgoodfd < 0) {
1017                 prterr(goodfile);
1018                 exit(92);
1019         }
1020         strncat(logfile, fname, 256);
1021         strcat (logfile, ".fsxlog");
1022         fsxlogf = fopen(logfile, "w");
1023         if (fsxlogf == NULL) {
1024                 prterr(logfile);
1025                 exit(93);
1026         }
1027         if (lite) {
1028                 off_t ret;
1029                 file_size = maxfilelen = lseek(fd, (off_t)0, L_XTND);
1030                 if (file_size == (off_t)-1) {
1031                         prterr(fname);
1032                                                  fprintf(stderr, "main: lseek eof");
1033                         exit(94);
1034                 }
1035                 ret = lseek(fd, (off_t)0, SEEK_SET);
1036                 if (ret == (off_t)-1) {
1037                         prterr(fname);
1038                                                  fprintf(stderr, "main: lseek 0");
1039                         exit(95);
1040                 }
1041         }
1042         original_buf = (char *) malloc(maxfilelen);
1043         for (i = 0; i < maxfilelen; i++)
1044                 original_buf[i] = random() % 256;
1045         good_buf = (char *) malloc(maxfilelen);
1046         bzero(good_buf, maxfilelen);
1047         temp_buf = (char *) malloc(maxoplen);
1048         bzero(temp_buf, maxoplen);
1049         if (lite) {     /* zero entire existing file */
1050                 ssize_t written;
1051
1052                 written = write(fd, good_buf, (size_t)maxfilelen);
1053                 if (written != maxfilelen) {
1054                         if (written == -1) {
1055                                 prterr(fname);
1056                                 fprintf(stderr, "main: error on write");
1057                         } else
1058                                 fprintf(stderr, "main: short write, 0x%x bytes instead of 0x%lx\n",
1059                                      (unsigned)written, maxfilelen);
1060                         exit(98);
1061                 }
1062         } else 
1063                 check_trunc_hack();
1064
1065         while (numops == -1 || numops--)
1066                 test();
1067
1068         if (close(fd)) {
1069                 prterr("close");
1070                 report_failure(99);
1071         }
1072         prt("All operations completed A-OK!\n");
1073
1074         exit(0);
1075         return 0;
1076 }