2 * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 2.0 (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.opensource.apple.com/apsl/ and read it before using this file.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Author: Avadis Tevanian, Jr.
25 * File system exerciser.
27 * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
29 * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
31 * Small changes to work under Linux -- davej@suse.de
33 * Sundry porting patches from Guy Harris 12/2001
35 * Checks for mmap last-page zero fill.
37 * Updated license to APSL 2.0, 2004/7/27 - Jordan Hubbard
43 #include <sys/types.h>
46 # include <sys/param.h>
66 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
69 * A log entry is an operation and a bunch of arguments.
79 struct log_entry oplog[LOGSIZE]; /* the log */
80 int logptr = 0; /* current position in log */
81 int logcount = 0; /* total ops */
90 #define OP_CLOSEOPEN 4
94 #define OP_INVALIDATE 8
99 char *original_buf; /* a pointer to the original data */
100 char *good_buf; /* a pointer to the correct data */
101 char *temp_buf; /* a pointer to the current data */
102 char *fname; /* name of our test file */
103 int fd; /* fd for our test file */
108 unsigned long testcalls = 0; /* calls to function "test" */
110 unsigned long simulatedopcount = 0; /* -b flag */
111 int closeprob = 0; /* -c flag */
112 int invlprob = 0; /* -i flag */
113 int debug = 0; /* -d flag */
114 unsigned long debugstart = 0; /* -D flag */
115 unsigned long maxfilelen = 256 * 1024; /* -l flag */
116 int sizechecks = 1; /* -n flag disables them */
117 int maxoplen = 64 * 1024; /* -o flag */
118 int quiet = 0; /* -q flag */
119 unsigned long progressinterval = 0; /* -p flag */
120 int readbdy = 1; /* -r flag */
121 int style = 0; /* -s flag */
122 int truncbdy = 1; /* -t flag */
123 int writebdy = 1; /* -w flag */
124 long monitorstart = -1; /* -m flag */
125 long monitorend = -1; /* -m flag */
126 int lite = 0; /* -L flag */
127 long numops = -1; /* -N flag */
128 int randomoplen = 1; /* -O flag disables it */
129 int seed = 1; /* -S flag */
130 int mapped_writes = 1; /* -W flag disables */
131 int mapped_reads = 1; /* -R flag disables it */
132 int mapped_msync = 1; /* -U flag disables */
134 FILE * fsxlogf = NULL;
141 vwarnc(code, fmt, ap)
146 fprintf(stderr, "fsx: ");
148 vfprintf(stderr, fmt, ap);
149 fprintf(stderr, ": ");
151 fprintf(stderr, "%s\n", strerror(code));
156 warn(const char * fmt, ...)
160 vwarnc(errno, fmt, ap);
171 vfprintf(stdout, fmt, args);
176 vfprintf(fsxlogf, fmt, args);
184 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
189 do_log4(int operation, int arg0, int arg1, int arg2)
191 struct log_entry *le;
194 le->operation = operation;
200 if (logptr >= LOGSIZE)
206 log4(int operation, int arg0, int arg1, int arg2)
208 do_log4(operation, arg0, arg1, arg2);
210 do_log4(OP_CLOSEOPEN, 0, 0, 0);
212 do_log4(OP_INVALIDATE, 0, 0, 0);
219 struct log_entry *lp;
220 int i, count, down, opnum;
222 prt("LOG DUMP (%d total operations):\n", logcount);
223 if (logcount < LOGSIZE) {
231 opnum = i + 1 + (logcount/LOGSIZE)*LOGSIZE;
232 for ( ; count > 0; count--) {
235 if (lp->operation == OP_CLOSEOPEN ||
236 lp->operation == OP_INVALIDATE) {
237 switch (lp->operation) {
239 prt("\t\tCLOSE/OPEN\n");
242 prt("\t\tMS_INVALIDATE\n");
251 prt("%d(%d mod 256): ", opnum, opnum%256);
252 switch (lp->operation) {
254 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
255 lp->args[0], lp->args[0] + lp->args[1] - 1,
257 if (badoff >= lp->args[0] && badoff <
258 lp->args[0] + lp->args[1])
262 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
263 lp->args[0], lp->args[0] + lp->args[1] - 1,
265 if (badoff >= lp->args[0] && badoff <
266 lp->args[0] + lp->args[1])
270 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
271 lp->args[0], lp->args[0] + lp->args[1] - 1,
273 if (badoff >= lp->args[0] &&
274 badoff < lp->args[0] + lp->args[1])
279 int offset = lp->args[0];
280 int len = lp->args[1];
281 int oldlen = lp->args[2];
283 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
284 offset, offset + len - 1,
288 else if (offset + len > oldlen)
290 if ((badoff >= offset || badoff >=oldlen) &&
291 badoff < offset + len)
296 down = lp->args[0] < lp->args[1];
297 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
298 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
299 if (badoff >= lp->args[!down] &&
300 badoff < lp->args[!!down])
304 prt("SKIPPED (no operation)");
307 prt("BOGUS LOG ENTRY (operation code = %d)!",
320 save_buffer(char *buffer, off_t bufferlength, int fd)
323 ssize_t byteswritten;
325 if (fd <= 0 || bufferlength == 0)
328 if (bufferlength > SSIZE_MAX) {
329 prt("fsx flaw: overflow in save_buffer\n");
333 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
334 if (size_by_seek == (off_t)-1)
335 prterr("save_buffer: lseek eof");
336 else if (bufferlength > size_by_seek) {
337 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
338 (unsigned long long)bufferlength);
339 bufferlength = size_by_seek;
343 ret = lseek(fd, (off_t)0, SEEK_SET);
344 if (ret == (off_t)-1)
345 prterr("save_buffer: lseek 0");
347 byteswritten = write(fd, buffer, (size_t)bufferlength);
348 if (byteswritten != bufferlength) {
349 if (byteswritten == -1)
350 prterr("save_buffer write");
352 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
353 (unsigned)byteswritten,
354 (unsigned long long)bufferlength);
360 report_failure(int status)
366 save_buffer(good_buf, file_size, fsxgoodfd);
367 prt("Correct content saved for comparison\n");
368 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
377 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
378 *(((unsigned char *)(cp)) + 1)))
381 check_buffers(unsigned offset, unsigned size)
389 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
390 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
392 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
394 c = good_buf[offset];
398 bad = short_at(&temp_buf[i]);
399 prt("0x%5x\t0x%04x\t0x%04x", offset,
400 short_at(&good_buf[offset]), bad);
401 op = temp_buf[offset & 1 ? i+1 : i];
413 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
415 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
417 prt("????????????????\n");
429 if (fstat(fd, &statbuf)) {
430 prterr("check_size: fstat");
431 statbuf.st_size = -1;
433 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
434 if (file_size != statbuf.st_size || file_size != size_by_seek) {
435 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
436 (unsigned long long)file_size,
437 (unsigned long long)statbuf.st_size,
438 (unsigned long long)size_by_seek);
445 check_trunc_hack(void)
449 ftruncate(fd, (off_t)0);
450 ftruncate(fd, (off_t)100000);
452 if (statbuf.st_size != (off_t)100000) {
453 prt("no extend on truncate! not posix!\n");
456 ftruncate(fd, (off_t)0);
461 doread(unsigned offset, unsigned size)
466 offset -= offset % readbdy;
468 if (!quiet && testcalls > simulatedopcount)
469 prt("skipping zero size read\n");
470 log4(OP_SKIPPED, OP_READ, offset, size);
473 if (size + offset > file_size) {
474 if (!quiet && testcalls > simulatedopcount)
475 prt("skipping seek/read past end of file\n");
476 log4(OP_SKIPPED, OP_READ, offset, size);
480 log4(OP_READ, offset, size, 0);
482 if (testcalls <= simulatedopcount)
485 if (!quiet && ((progressinterval &&
486 testcalls % progressinterval == 0) ||
488 (monitorstart == -1 ||
489 (offset + size > monitorstart &&
490 (monitorend == -1 || offset <= monitorend))))))
491 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
492 offset, offset + size - 1, size);
493 ret = lseek(fd, (off_t)offset, SEEK_SET);
494 if (ret == (off_t)-1) {
495 prterr("doread: lseek");
498 iret = read(fd, temp_buf, size);
501 prterr("doread: read");
503 prt("short read: 0x%x bytes instead of 0x%x\n",
507 check_buffers(offset, size);
512 check_eofpage(char *s, unsigned offset, char *p, int size)
514 uintptr_t last_page, should_be_zero;
516 if (offset + size <= (file_size & ~page_mask))
519 * we landed in the last page of the file
520 * test to make sure the VM system provided 0's
521 * beyond the true end of the file mapping
522 * (as required by mmap def in 1996 posix 1003.1)
524 last_page = ((uintptr_t)p + (offset & page_mask) + size) & ~page_mask;
526 for (should_be_zero = last_page + (file_size & page_mask);
527 should_be_zero < last_page + page_size;
529 if (*(char *)should_be_zero) {
530 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
531 s, file_size - 1, should_be_zero & page_mask,
532 short_at(should_be_zero));
539 domapread(unsigned offset, unsigned size)
545 offset -= offset % readbdy;
547 if (!quiet && testcalls > simulatedopcount)
548 prt("skipping zero size read\n");
549 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
552 if (size + offset > file_size) {
553 if (!quiet && testcalls > simulatedopcount)
554 prt("skipping seek/read past end of file\n");
555 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
559 log4(OP_MAPREAD, offset, size, 0);
561 if (testcalls <= simulatedopcount)
564 if (!quiet && ((progressinterval &&
565 testcalls % progressinterval == 0) ||
567 (monitorstart == -1 ||
568 (offset + size > monitorstart &&
569 (monitorend == -1 || offset <= monitorend))))))
570 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
571 offset, offset + size - 1, size);
573 pg_offset = offset & page_mask;
574 map_size = pg_offset + size;
576 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
577 (off_t)(offset - pg_offset))) == (char *)-1) {
578 prterr("domapread: mmap");
581 memcpy(temp_buf, p + pg_offset, size);
583 check_eofpage("Read", offset, p, size);
585 if (munmap(p, map_size) != 0) {
586 prterr("domapread: munmap");
590 check_buffers(offset, size);
595 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
598 good_buf[offset] = testcalls % 256;
600 good_buf[offset] += original_buf[offset];
607 dowrite(unsigned offset, unsigned size)
612 offset -= offset % writebdy;
614 if (!quiet && testcalls > simulatedopcount)
615 prt("skipping zero size write\n");
616 log4(OP_SKIPPED, OP_WRITE, offset, size);
620 log4(OP_WRITE, offset, size, file_size);
622 gendata(original_buf, good_buf, offset, size);
623 if (file_size < offset + size) {
624 if (file_size < offset)
625 memset(good_buf + file_size, '\0', offset - file_size);
626 file_size = offset + size;
628 warn("Lite file size bug in fsx!");
633 if (testcalls <= simulatedopcount)
636 if (!quiet && ((progressinterval &&
637 testcalls % progressinterval == 0) ||
639 (monitorstart == -1 ||
640 (offset + size > monitorstart &&
641 (monitorend == -1 || offset <= monitorend))))))
642 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
643 offset, offset + size - 1, size);
644 ret = lseek(fd, (off_t)offset, SEEK_SET);
645 if (ret == (off_t)-1) {
646 prterr("dowrite: lseek");
649 iret = write(fd, good_buf + offset, size);
652 prterr("dowrite: write");
654 prt("short write: 0x%x bytes instead of 0x%x\n",
662 domapwrite(unsigned offset, unsigned size)
669 offset -= offset % writebdy;
671 if (!quiet && testcalls > simulatedopcount)
672 prt("skipping zero size write\n");
673 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
676 cur_filesize = file_size;
678 log4(OP_MAPWRITE, offset, size, 0);
680 gendata(original_buf, good_buf, offset, size);
681 if (file_size < offset + size) {
682 if (file_size < offset)
683 memset(good_buf + file_size, '\0', offset - file_size);
684 file_size = offset + size;
686 warn("Lite file size bug in fsx!");
691 if (testcalls <= simulatedopcount)
694 if (!quiet && ((progressinterval &&
695 testcalls % progressinterval == 0) ||
697 (monitorstart == -1 ||
698 (offset + size > monitorstart &&
699 (monitorend == -1 || offset <= monitorend))))))
700 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
701 offset, offset + size - 1, size);
703 if (file_size > cur_filesize) {
704 if (ftruncate(fd, file_size) == -1) {
705 prterr("domapwrite: ftruncate");
709 pg_offset = offset & page_mask;
710 map_size = pg_offset + size;
712 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
713 MAP_FILE | MAP_SHARED, fd,
714 (off_t)(offset - pg_offset))) == MAP_FAILED) {
715 prterr("domapwrite: mmap");
718 memcpy(p + pg_offset, good_buf + offset, size);
719 if (mapped_msync && msync(p, map_size, MS_SYNC) != 0) {
720 prterr("domapwrite: msync");
724 check_eofpage("Write", offset, p, size);
726 if (munmap(p, map_size) != 0) {
727 prterr("domapwrite: munmap");
734 dotruncate(unsigned size)
736 int oldsize = file_size;
738 size -= size % truncbdy;
739 if (size > biggest) {
741 if (!quiet && testcalls > simulatedopcount)
742 prt("truncating to largest ever: 0x%x\n", size);
745 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
747 if (size > file_size)
748 memset(good_buf + file_size, '\0', size - file_size);
751 if (testcalls <= simulatedopcount)
754 if ((progressinterval && testcalls % progressinterval == 0) ||
755 (debug && (monitorstart == -1 || monitorend == -1 ||
756 size <= monitorend)))
757 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
758 if (ftruncate(fd, (off_t)size) == -1) {
759 prt("ftruncate1: %x\n", size);
760 prterr("dotruncate: ftruncate");
771 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
772 prterr("writefileimage: lseek");
775 iret = write(fd, good_buf, file_size);
776 if ((off_t)iret != file_size) {
778 prterr("writefileimage: write");
780 prt("short write: 0x%x bytes instead of 0x%llx\n",
781 iret, (unsigned long long)file_size);
784 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
785 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
786 prterr("writefileimage: ftruncate");
795 if (testcalls <= simulatedopcount)
799 prt("%lu close/open\n", testcalls);
801 prterr("docloseopen: close");
804 fd = open(fname, O_RDWR, 0);
806 prterr("docloseopen: open");
819 if (testcalls <= simulatedopcount)
822 prt("%lu msync(MS_INVALIDATE)\n", testcalls);
824 if ((p = (char *)mmap(0, file_size, PROT_READ | PROT_WRITE,
825 MAP_FILE | MAP_SHARED, fd, 0)) == MAP_FAILED) {
826 prterr("doinvl: mmap");
830 if (msync(p, 0, MS_SYNC | MS_INVALIDATE) != 0) {
831 prterr("doinvl: msync");
835 if (munmap(p, file_size) != 0) {
836 prterr("doinvl: munmap");
845 unsigned long offset;
846 unsigned long size = maxoplen;
847 unsigned long rv = random();
848 unsigned long op = rv % (3 + !lite + mapped_writes);
850 /* turn off the map read if necessary */
852 if (op == 2 && !mapped_reads)
855 if (simulatedopcount > 0 && testcalls == simulatedopcount)
861 closeopen = (rv >> 3) < (1 << 28) / closeprob;
863 invl = (rv >> 3) < (1 << 28) / invlprob;
865 if (debugstart > 0 && testcalls >= debugstart)
868 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
869 prt("%lu...\n", testcalls);
876 * MAPWRITE: op = 3 or 4
878 if (lite ? 0 : op == 3 && style == 0) /* vanilla truncate? */
879 dotruncate(random() % maxfilelen);
882 size = random() % (maxoplen+1);
883 if (lite ? 0 : op == 3)
887 if (op == 1 || op == (lite ? 3 : 4)) {
888 offset %= maxfilelen;
889 if (offset + size > maxfilelen)
890 size = maxfilelen - offset;
892 domapwrite(offset, size);
894 dowrite(offset, size);
900 if (offset + size > file_size)
901 size = file_size - offset;
903 domapread(offset, size);
905 doread(offset, size);
909 if (sizechecks && testcalls > simulatedopcount)
923 prt("signal %d\n", sig);
924 prt("testcalls = %lu\n", testcalls);
932 fprintf(stdout, "usage: %s",
933 "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\
934 -b opnum: beginning operation number (default 1)\n\
935 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
936 -d: debug output for all operations\n\
937 -i P: 1 in P chance of calling msync(MS_INVALIDATE) (default infinity)\n\
938 -l flen: the upper bound on file size (default 262144)\n\
939 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
940 -n: no verifications of file size\n\
941 -o oplen: the upper bound on operation size (default 65536)\n\
942 -p progressinterval: debug output at specified operation interval\n\
943 -q: quieter operation\n\
944 -r readbdy: 4096 would make reads page aligned (default 1)\n\
945 -s style: 1 gives smaller truncates (default 0)\n\
946 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
947 -w writebdy: 4096 would make writes page aligned (default 1)\n\
948 -D startingop: debug output starting at specified operation\n\
949 -L: fsxLite - no file creations & no file size changes\n\
950 -N numops: total # operations to do (default infinity)\n\
951 -O: use oplen (see -o flag) for every op (default random)\n\
952 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
953 -S seed: for random # generator (default 1) 0 gets timestamp\n\
954 -W: mapped write operations DISabled\n\
955 -R: mapped read operations DISabled)\n\
956 -U: msync after mapped write operations DISabled\n\
957 fname: this filename is REQUIRED (no default)\n");
963 getnum(char *s, char **e)
968 ret = strtol(s, e, 0);
997 main(int argc, char **argv)
1001 char goodfile[1024];
1003 struct timespec now;
1008 page_size = getpagesize();
1009 page_mask = page_size - 1;
1011 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1013 while ((ch = getopt(argc, argv,
1014 "b:c:di:l:m:no:p:qr:s:t:w:D:LN:OP:RS:UW")) != -1)
1017 simulatedopcount = getnum(optarg, &endp);
1019 fprintf(stdout, "Will begin at operation %ld\n",
1021 if (simulatedopcount == 0)
1023 simulatedopcount -= 1;
1026 closeprob = getnum(optarg, &endp);
1029 "Chance of close/open is 1 in %d\n",
1038 invlprob = getnum(optarg, &endp);
1041 "Chance of MS_INVALIDATE is 1 in %d\n",
1047 maxfilelen = getnum(optarg, &endp);
1048 if (maxfilelen <= 0)
1052 monitorstart = getnum(optarg, &endp);
1053 if (monitorstart < 0)
1055 if (!endp || *endp++ != ':')
1057 monitorend = getnum(endp, &endp);
1060 if (monitorend == 0)
1061 monitorend = -1; /* aka infinity */
1067 maxoplen = getnum(optarg, &endp);
1072 progressinterval = getnum(optarg, &endp);
1073 if (progressinterval < 0)
1080 readbdy = getnum(optarg, &endp);
1085 style = getnum(optarg, &endp);
1086 if (style < 0 || style > 1)
1090 truncbdy = getnum(optarg, &endp);
1095 writebdy = getnum(optarg, &endp);
1100 debugstart = getnum(optarg, &endp);
1108 numops = getnum(optarg, &endp);
1116 strncpy(goodfile, optarg, sizeof(goodfile));
1117 strcat(goodfile, "/");
1118 strncpy(logfile, optarg, sizeof(logfile));
1119 strcat(logfile, "/");
1125 seed = getnum(optarg, &endp);
1127 if (clock_gettime(CLOCK_REALTIME, &now) != 0)
1128 err(1, "clock_gettime");
1129 seed = now.tv_nsec % 10000;
1132 fprintf(stdout, "Seed set to %d\n", seed);
1139 fprintf(stdout, "mapped writes DISABLED\n");
1144 fprintf(stdout, "mapped msync DISABLED\n");
1157 signal(SIGHUP, cleanup);
1158 signal(SIGINT, cleanup);
1159 signal(SIGPIPE, cleanup);
1160 signal(SIGALRM, cleanup);
1161 signal(SIGTERM, cleanup);
1162 signal(SIGXCPU, cleanup);
1163 signal(SIGXFSZ, cleanup);
1164 signal(SIGVTALRM, cleanup);
1165 signal(SIGUSR1, cleanup);
1166 signal(SIGUSR2, cleanup);
1168 initstate(seed, state, 256);
1170 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1175 strncat(goodfile, fname, 256);
1176 strcat (goodfile, ".fsxgood");
1177 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1178 if (fsxgoodfd < 0) {
1182 strncat(logfile, fname, 256);
1183 strcat (logfile, ".fsxlog");
1184 fsxlogf = fopen(logfile, "w");
1185 if (fsxlogf == NULL) {
1191 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1192 if (file_size == (off_t)-1) {
1194 warn("main: lseek eof");
1197 ret = lseek(fd, (off_t)0, SEEK_SET);
1198 if (ret == (off_t)-1) {
1200 warn("main: lseek 0");
1204 original_buf = (char *) malloc(maxfilelen);
1205 for (i = 0; i < maxfilelen; i++)
1206 original_buf[i] = random() % 256;
1207 good_buf = (char *) malloc(maxfilelen);
1208 memset(good_buf, '\0', maxfilelen);
1209 temp_buf = (char *) malloc(maxoplen);
1210 memset(temp_buf, '\0', maxoplen);
1211 if (lite) { /* zero entire existing file */
1214 written = write(fd, good_buf, (size_t)maxfilelen);
1215 if (written != maxfilelen) {
1216 if (written == -1) {
1218 warn("main: error on write");
1220 warn("main: short write, 0x%x bytes instead of 0x%lx\n",
1221 (unsigned)written, maxfilelen);
1227 while (numops == -1 || numops--)
1234 prt("All operations completed A-OK!\n");