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