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 1.2 (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.
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 * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.2 2003/04/23 23:42:23 jkh Exp $
38 * $DragonFly: src/test/stress/fsx/fsx.c,v 1.2 2005/05/02 19:31:56 dillon Exp $
42 #include <sys/types.h>
56 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
59 * A log entry is an operation and a bunch of arguments.
69 struct log_entry oplog
[LOGSIZE
]; /* the log */
70 int logptr
= 0; /* current position in log */
71 int logcount
= 0; /* total ops */
75 static void log4(int operation
, int arg0
, int arg1
, int arg2
);
76 static void vwarnc(int code
, const char *fmt
, va_list ap
);
77 static void warn(const char * fmt
, ...);
78 static void prt(const char *fmt
, ...);
79 static void prterr(const char *prefix
);
80 static void logdump(void);
81 static void save_buffer(char *buffer
, off_t bufferlength
, int fd
);
82 static void report_failure(int status
);
83 static void check_buffers(unsigned offset
, unsigned size
);
84 static void check_trunc_hack(void);
85 static void check_size(void);
86 static void doread(unsigned offset
, unsigned size
);
87 static void check_eofpage(const char *s
, unsigned offset
, char *p
, int size
);
88 static void domapread(unsigned offset
, unsigned size
);
89 static void gendata(char *original_buf
, char *good_buf
,
90 unsigned offset
, unsigned size
);
91 static void dowrite(unsigned offset
, unsigned size
);
92 static void domapwrite(unsigned offset
, unsigned size
);
93 static void dotruncate(unsigned size
);
94 static void docloseopen(void);
95 static void test(void);
96 static void segv(int sig
);
97 static void cleanup(int sig
);
98 static void usage(void);
99 static int getnum(char *s
, char **e
);
107 #define OP_TRUNCATE 3
108 #define OP_CLOSEOPEN 4
110 #define OP_MAPWRITE 6
116 char *original_buf
; /* a pointer to the original data */
117 char *good_buf
; /* a pointer to the correct data */
118 char *temp_buf
; /* a pointer to the current data */
119 char *fname
; /* name of our test file */
120 int fd
; /* fd for our test file */
125 unsigned long testcalls
= 0; /* calls to function "test" */
127 unsigned long simulatedopcount
= 0; /* -b flag */
128 int closeprob
= 0; /* -c flag */
129 int debug
= 0; /* -d flag */
130 unsigned long debugstart
= 0; /* -D flag */
131 unsigned long maxfilelen
= 256 * 1024; /* -l flag */
132 int sizechecks
= 1; /* -n flag disables them */
133 int maxoplen
= 64 * 1024; /* -o flag */
134 int quiet
= 0; /* -q flag */
135 unsigned long progressinterval
= 0; /* -p flag */
136 int readbdy
= 1; /* -r flag */
137 int style
= 0; /* -s flag */
138 int truncbdy
= 1; /* -t flag */
139 int writebdy
= 1; /* -w flag */
140 long monitorstart
= -1; /* -m flag */
141 long monitorend
= -1; /* -m flag */
142 int lite
= 0; /* -L flag */
143 long numops
= -1; /* -N flag */
144 int randomoplen
= 1; /* -O flag disables it */
145 int seed
= 1; /* -S flag */
146 int mapped_writes
= 1; /* -W flag disables */
147 int mapped_reads
= 1; /* -R flag disables it */
149 FILE * fsxlogf
= NULL
;
156 vwarnc(int code
, const char *fmt
, va_list ap
)
158 fprintf(stderr
, "fsx: ");
160 vfprintf(stderr
, fmt
, ap
);
161 fprintf(stderr
, ": ");
163 fprintf(stderr
, "%s\n", strerror(code
));
168 warn(const char * fmt
, ...)
172 vwarnc(errno
, fmt
, ap
);
179 prt(const char *fmt
, ...)
184 vfprintf(stdout
, fmt
, args
);
188 vfprintf(fsxlogf
, fmt
, args
);
194 prterr(const char *prefix
)
196 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(errno
));
200 log4(int operation
, int arg0
, int arg1
, int arg2
)
202 struct log_entry
*le
;
205 le
->operation
= operation
;
207 le
->operation
= ~ le
->operation
;
213 if (logptr
>= LOGSIZE
)
223 struct log_entry
*lp
;
225 prt("LOG DUMP (%d total operations):\n", logcount
);
226 if (logcount
< LOGSIZE
) {
233 for ( ; count
> 0; count
--) {
236 opnum
= i
+1 + (logcount
/LOGSIZE
)*LOGSIZE
;
237 prt("%d(%d mod 256): ", opnum
, opnum
%256);
239 if ((closeopen
= lp
->operation
< 0))
240 lp
->operation
= ~ lp
->operation
;
242 switch (lp
->operation
) {
244 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
245 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
247 if (badoff
>= lp
->args
[0] && badoff
<
248 lp
->args
[0] + lp
->args
[1])
252 prt("MAPWRITE\t0x%x thru 0x%x\t(0x%x bytes)",
253 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
255 if (badoff
>= lp
->args
[0] && badoff
<
256 lp
->args
[0] + lp
->args
[1])
260 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
261 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
263 if (badoff
>= lp
->args
[0] &&
264 badoff
< lp
->args
[0] + lp
->args
[1])
268 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
269 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
271 if (lp
->args
[0] > lp
->args
[2])
273 else if (lp
->args
[0] + lp
->args
[1] > lp
->args
[2])
275 if ((badoff
>= lp
->args
[0] || badoff
>=lp
->args
[2]) &&
276 badoff
< lp
->args
[0] + lp
->args
[1])
280 down
= lp
->args
[0] < lp
->args
[1];
281 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
282 down
? "DOWN" : "UP", lp
->args
[1], lp
->args
[0]);
283 if (badoff
>= lp
->args
[!down
] &&
284 badoff
< lp
->args
[!!down
])
288 prt("SKIPPED (no operation)");
291 prt("BOGUS LOG ENTRY (operation code = %d)!",
295 prt("\n\t\tCLOSE/OPEN");
305 save_buffer(char *buffer
, off_t bufferlength
, int xfd
)
308 ssize_t byteswritten
;
310 if (xfd
<= 0 || bufferlength
== 0)
313 if (bufferlength
> SSIZE_MAX
) {
314 prt("fsx flaw: overflow in save_buffer\n");
318 off_t size_by_seek
= lseek(xfd
, (off_t
)0, SEEK_END
);
319 if (size_by_seek
== (off_t
)-1)
320 prterr("save_buffer: lseek eof");
321 else if (bufferlength
> size_by_seek
) {
322 warn("save_buffer: .fsxgood file too short... "
323 "will save 0x%jx bytes instead of 0x%llx\n",
324 (intmax_t)size_by_seek
,
325 (intmax_t)bufferlength
);
326 bufferlength
= size_by_seek
;
330 ret
= lseek(xfd
, (off_t
)0, SEEK_SET
);
331 if (ret
== (off_t
)-1)
332 prterr("save_buffer: lseek 0");
334 byteswritten
= write(xfd
, buffer
, (size_t)bufferlength
);
335 if (byteswritten
!= bufferlength
) {
336 if (byteswritten
== -1)
337 prterr("save_buffer write");
339 warn("save_buffer: short write, "
340 "0x%x bytes instead of 0x%jx\n",
341 (unsigned)byteswritten
,
342 (intmax_t)bufferlength
);
349 report_failure(int status
)
355 save_buffer(good_buf
, file_size
, fsxgoodfd
);
356 prt("Correct content saved for comparison\n");
357 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
366 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
367 *(((unsigned char *)(cp)) + 1)))
371 check_buffers(unsigned offset
, unsigned size
)
379 if (memcmp(good_buf
+ offset
, temp_buf
, size
) != 0) {
380 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
382 prt("OFFSET\t\tGOOD\tBAD\tRANGE\n");
384 c
= good_buf
[offset
];
388 bad
= short_at(&temp_buf
[i
]);
389 prt("0x%08x\t0x%04x\t0x%04x", offset
,
390 short_at(&good_buf
[offset
]), bad
);
391 op
= temp_buf
[offset
& 1 ? i
+1 : i
];
403 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op
& 0xff));
405 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
407 prt("????????????????\n");
419 if (fstat(fd
, &statbuf
)) {
420 prterr("check_size: fstat");
421 statbuf
.st_size
= -1;
423 size_by_seek
= lseek(fd
, (off_t
)0, SEEK_END
);
424 if (file_size
!= statbuf
.st_size
|| file_size
!= size_by_seek
) {
425 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
426 (unsigned long long)file_size
,
427 (unsigned long long)statbuf
.st_size
,
428 (unsigned long long)size_by_seek
);
435 check_trunc_hack(void)
439 ftruncate(fd
, (off_t
)0);
440 ftruncate(fd
, (off_t
)100000);
442 if (statbuf
.st_size
!= (off_t
)100000) {
443 prt("no extend on truncate! not posix!\n");
446 ftruncate(fd
, (off_t
)0);
451 doread(unsigned offset
, unsigned size
)
456 offset
-= offset
% readbdy
;
458 if (!quiet
&& testcalls
> simulatedopcount
)
459 prt("skipping zero size read\n");
460 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
463 if (size
+ offset
> file_size
) {
464 if (!quiet
&& testcalls
> simulatedopcount
)
465 prt("skipping seek/read past end of file\n");
466 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
470 log4(OP_READ
, offset
, size
, 0);
472 if (testcalls
<= simulatedopcount
)
475 if (!quiet
&& ((progressinterval
&&
476 testcalls
% progressinterval
== 0) ||
478 (monitorstart
== -1 ||
479 (offset
+ size
> monitorstart
&&
480 (monitorend
== -1 || offset
<= monitorend
))))))
481 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
482 offset
, offset
+ size
- 1, size
);
483 ret
= lseek(fd
, (off_t
)offset
, SEEK_SET
);
484 if (ret
== (off_t
)-1) {
485 prterr("doread: lseek");
488 iret
= read(fd
, temp_buf
, size
);
491 prterr("doread: read");
493 prt("short read: 0x%x bytes instead of 0x%x\n",
497 check_buffers(offset
, size
);
502 check_eofpage(const char *s
, unsigned offset
, char *p
, int size
)
504 intptr_t last_page
, should_be_zero
;
506 if (offset
+ size
<= (file_size
& ~page_mask
))
509 * we landed in the last page of the file
510 * test to make sure the VM system provided 0's
511 * beyond the true end of the file mapping
512 * (as required by mmap def in 1996 posix 1003.1)
514 last_page
= ((intptr_t)p
+ (offset
& page_mask
) + size
) & ~(intptr_t)page_mask
;
516 for (should_be_zero
= last_page
+ (file_size
& page_mask
);
517 should_be_zero
< last_page
+ page_size
;
519 if (*(char *)should_be_zero
) {
520 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
521 s
, file_size
- 1, should_be_zero
& page_mask
,
522 *(char *)(should_be_zero
));
530 domapread(unsigned offset
, unsigned size
)
536 offset
-= offset
% readbdy
;
538 if (!quiet
&& testcalls
> simulatedopcount
)
539 prt("skipping zero size read\n");
540 log4(OP_SKIPPED
, OP_MAPREAD
, offset
, size
);
543 if (size
+ offset
> file_size
) {
544 if (!quiet
&& testcalls
> simulatedopcount
)
545 prt("skipping seek/read past end of file\n");
546 log4(OP_SKIPPED
, OP_MAPREAD
, offset
, size
);
550 log4(OP_MAPREAD
, offset
, size
, 0);
552 if (testcalls
<= simulatedopcount
)
555 if (!quiet
&& ((progressinterval
&&
556 testcalls
% progressinterval
== 0) ||
558 (monitorstart
== -1 ||
559 (offset
+ size
> monitorstart
&&
560 (monitorend
== -1 || offset
<= monitorend
))))))
561 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
562 offset
, offset
+ size
- 1, size
);
564 pg_offset
= offset
& page_mask
;
565 map_size
= pg_offset
+ size
;
567 if ((p
= (char *)mmap(0, map_size
, PROT_READ
, MAP_FILE
| MAP_SHARED
, fd
,
568 (off_t
)(offset
- pg_offset
))) == (char *)-1) {
569 prterr("domapread: mmap");
572 if (setjmp(jmpbuf
) == 0) {
574 memcpy(temp_buf
, p
+ pg_offset
, size
);
575 check_eofpage("Read", offset
, p
, size
);
578 report_failure(1901);
581 if (munmap(p
, map_size
) != 0) {
582 prterr("domapread: munmap");
586 check_buffers(offset
, size
);
591 gendata(char *obuf
, char *gbuf
, unsigned offset
, unsigned size
)
594 gbuf
[offset
] = testcalls
% 256;
596 gbuf
[offset
] += obuf
[offset
];
603 dowrite(unsigned offset
, unsigned size
)
608 offset
-= offset
% writebdy
;
610 if (!quiet
&& testcalls
> simulatedopcount
)
611 prt("skipping zero size write\n");
612 log4(OP_SKIPPED
, OP_WRITE
, offset
, size
);
616 log4(OP_WRITE
, offset
, size
, file_size
);
618 gendata(original_buf
, good_buf
, offset
, size
);
619 if (file_size
< offset
+ size
) {
620 if (file_size
< offset
)
621 memset(good_buf
+ file_size
, '\0', offset
- file_size
);
622 file_size
= offset
+ size
;
624 warn("Lite file size bug in fsx!");
629 if (testcalls
<= simulatedopcount
)
632 if (!quiet
&& ((progressinterval
&&
633 testcalls
% progressinterval
== 0) ||
635 (monitorstart
== -1 ||
636 (offset
+ size
> monitorstart
&&
637 (monitorend
== -1 || offset
<= monitorend
))))))
638 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
639 offset
, offset
+ size
- 1, size
);
640 ret
= lseek(fd
, (off_t
)offset
, SEEK_SET
);
641 if (ret
== (off_t
)-1) {
642 prterr("dowrite: lseek");
645 iret
= write(fd
, good_buf
+ offset
, size
);
648 prterr("dowrite: write");
650 prt("short write: 0x%x bytes instead of 0x%x\n",
658 domapwrite(unsigned offset
, unsigned size
)
665 offset
-= offset
% writebdy
;
667 if (!quiet
&& testcalls
> simulatedopcount
)
668 prt("skipping zero size write\n");
669 log4(OP_SKIPPED
, OP_MAPWRITE
, offset
, size
);
672 cur_filesize
= file_size
;
674 log4(OP_MAPWRITE
, offset
, size
, 0);
676 gendata(original_buf
, good_buf
, offset
, size
);
677 if (file_size
< offset
+ size
) {
678 if (file_size
< offset
)
679 memset(good_buf
+ file_size
, '\0', offset
- file_size
);
680 file_size
= offset
+ size
;
682 warn("Lite file size bug in fsx!");
687 if (testcalls
<= simulatedopcount
)
690 if (!quiet
&& ((progressinterval
&&
691 testcalls
% progressinterval
== 0) ||
693 (monitorstart
== -1 ||
694 (offset
+ size
> monitorstart
&&
695 (monitorend
== -1 || offset
<= monitorend
))))))
696 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
697 offset
, offset
+ size
- 1, size
);
699 if (file_size
> cur_filesize
) {
700 if (ftruncate(fd
, file_size
) == -1) {
701 prterr("domapwrite: ftruncate");
705 pg_offset
= offset
& page_mask
;
706 map_size
= pg_offset
+ size
;
708 if ((p
= (char *)mmap(0, map_size
, PROT_READ
| PROT_WRITE
,
709 MAP_FILE
| MAP_SHARED
, fd
,
710 (off_t
)(offset
- pg_offset
))) == (char *)-1) {
711 prterr("domapwrite: mmap");
714 if (setjmp(jmpbuf
) == 0) {
716 memcpy(p
+ pg_offset
, good_buf
+ offset
, size
);
717 if (msync(p
, map_size
, 0) != 0) {
718 prterr("domapwrite: msync");
721 check_eofpage("Write", offset
, p
, size
);
724 report_failure(2021);
727 if (munmap(p
, map_size
) != 0) {
728 prterr("domapwrite: munmap");
735 dotruncate(unsigned size
)
737 int oldsize
= file_size
;
739 size
-= size
% truncbdy
;
740 if (size
> biggest
) {
742 if (!quiet
&& testcalls
> simulatedopcount
)
743 prt("truncating to largest ever: 0x%x\n", size
);
746 log4(OP_TRUNCATE
, size
, (unsigned)file_size
, 0);
748 if (size
> file_size
)
749 memset(good_buf
+ file_size
, '\0', size
- file_size
);
752 if (testcalls
<= simulatedopcount
)
755 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
756 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
757 size
<= monitorend
)))
758 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls
, oldsize
, size
);
759 if (ftruncate(fd
, (off_t
)size
) == -1) {
760 prt("ftruncate1: %x\n", size
);
761 prterr("dotruncate: ftruncate");
772 if (lseek(fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1) {
773 prterr("writefileimage: lseek");
776 iret
= write(fd
, good_buf
, file_size
);
777 if ((off_t
)iret
!= file_size
) {
779 prterr("writefileimage: write");
781 prt("short write: 0x%x bytes instead of 0x%jx\n",
782 iret
, (intmax_t)file_size
);
785 if (lite
? 0 : ftruncate(fd
, file_size
) == -1) {
786 prt("ftruncate2: %jx\n", (intmax_t)file_size
);
787 prterr("writefileimage: ftruncate");
796 if (testcalls
<= simulatedopcount
)
800 prt("%lu close/open\n", testcalls
);
802 prterr("docloseopen: close");
805 fd
= open(fname
, O_RDWR
, 0);
807 prterr("docloseopen: open");
816 unsigned long offset
;
817 unsigned long size
= maxoplen
;
818 unsigned long rv
= random();
819 unsigned long op
= rv
% (3 + !lite
+ mapped_writes
);
821 /* turn off the map read if necessary */
823 if (op
== 2 && !mapped_reads
)
826 if (simulatedopcount
> 0 && testcalls
== simulatedopcount
)
832 closeopen
= (rv
>> 3) < (1 << 28) / closeprob
;
834 if (debugstart
> 0 && testcalls
>= debugstart
)
837 if (!quiet
&& testcalls
< simulatedopcount
&& testcalls
% 100000 == 0)
838 prt("%lu...\n", testcalls
);
845 * MAPWRITE: op = 3 or 4
847 if (lite
? 0 : op
== 3 && style
== 0) /* vanilla truncate? */
848 dotruncate(random() % maxfilelen
);
851 size
= random() % (maxoplen
+1);
852 if (lite
? 0 : op
== 3)
856 if (op
== 1 || op
== (lite
? 3 : 4)) {
857 offset
%= maxfilelen
;
858 if (offset
+ size
> maxfilelen
)
859 size
= maxfilelen
- offset
;
861 domapwrite(offset
, size
);
863 dowrite(offset
, size
);
869 if (offset
+ size
> file_size
)
870 size
= file_size
- offset
;
872 domapread(offset
, size
);
874 doread(offset
, size
);
878 if (sizechecks
&& testcalls
> simulatedopcount
)
892 report_failure(9999);
900 prt("signal %d\n", sig
);
901 prt("testcalls = %lu\n", testcalls
);
909 fprintf(stdout
, "usage: %s",
910 "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\
911 -b opnum: beginning operation number (default 1)\n\
912 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
913 -d: debug output for all operations\n\
914 -l flen: the upper bound on file size (default 262144)\n\
915 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
916 -n: no verifications of file size\n\
917 -o oplen: the upper bound on operation size (default 65536)\n\
918 -p progressinterval: debug output at specified operation interval\n\
919 -q: quieter operation\n\
920 -r readbdy: 4096 would make reads page aligned (default 1)\n\
921 -s style: 1 gives smaller truncates (default 0)\n\
922 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
923 -w writebdy: 4096 would make writes page aligned (default 1)\n\
924 -D startingop: debug output starting at specified operation\n\
925 -L: fsxLite - no file creations & no file size changes\n\
926 -N numops: total # operations to do (default infinity)\n\
927 -O: use oplen (see -o flag) for every op (default random)\n\
928 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
929 -S seed: for random # generator (default 1) 0 gets timestamp\n\
930 -W: mapped write operations DISabled\n\
931 -R: mapped read operations DISabled)\n\
932 fname: this filename is REQUIRED (no default)\n");
938 getnum(char *s
, char **e
)
943 ret
= strtol(s
, e
, 0);
972 main(int argc
, char **argv
)
982 page_size
= getpagesize();
983 page_mask
= page_size
- 1;
985 setvbuf(stdout
, NULL
, _IOLBF
, 0); /* line buffered stdout */
987 while ((ch
= getopt(argc
, argv
, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
991 simulatedopcount
= getnum(optarg
, &endp
);
993 fprintf(stdout
, "Will begin at operation %ld\n",
995 if (simulatedopcount
== 0)
997 simulatedopcount
-= 1;
1000 closeprob
= getnum(optarg
, &endp
);
1003 "Chance of close/open is 1 in %d\n",
1012 maxfilelen
= getnum(optarg
, &endp
);
1013 if (maxfilelen
<= 0)
1017 monitorstart
= getnum(optarg
, &endp
);
1018 if (monitorstart
< 0)
1020 if (!endp
|| *endp
++ != ':')
1022 monitorend
= getnum(endp
, &endp
);
1025 if (monitorend
== 0)
1026 monitorend
= -1; /* aka infinity */
1032 maxoplen
= getnum(optarg
, &endp
);
1037 progressinterval
= getnum(optarg
, &endp
);
1038 if ((int)progressinterval
< 0)
1045 readbdy
= getnum(optarg
, &endp
);
1050 style
= getnum(optarg
, &endp
);
1051 if (style
< 0 || style
> 1)
1055 truncbdy
= getnum(optarg
, &endp
);
1060 writebdy
= getnum(optarg
, &endp
);
1065 debugstart
= getnum(optarg
, &endp
);
1073 numops
= getnum(optarg
, &endp
);
1081 strncpy(goodfile
, optarg
, sizeof(goodfile
));
1082 strcat(goodfile
, "/");
1083 strncpy(logfile
, optarg
, sizeof(logfile
));
1084 strcat(logfile
, "/");
1090 seed
= getnum(optarg
, &endp
);
1092 seed
= time(0) % 10000;
1094 fprintf(stdout
, "Seed set to %d\n", seed
);
1101 fprintf(stdout
, "mapped writes DISABLED\n");
1114 signal(SIGHUP
, cleanup
);
1115 signal(SIGINT
, cleanup
);
1116 signal(SIGPIPE
, cleanup
);
1117 signal(SIGALRM
, cleanup
);
1118 signal(SIGTERM
, cleanup
);
1119 signal(SIGXCPU
, cleanup
);
1120 signal(SIGXFSZ
, cleanup
);
1121 signal(SIGVTALRM
, cleanup
);
1122 signal(SIGUSR1
, cleanup
);
1123 signal(SIGUSR2
, cleanup
);
1124 signal(SIGSEGV
, segv
);
1126 initstate(seed
, state
, 256);
1128 fd
= open(fname
, O_RDWR
|(lite
? 0 : O_CREAT
|O_TRUNC
), 0666);
1133 strncat(goodfile
, fname
, 256);
1134 strcat (goodfile
, ".fsxgood");
1135 fsxgoodfd
= open(goodfile
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
1136 if (fsxgoodfd
< 0) {
1140 strncat(logfile
, fname
, 256);
1141 strcat (logfile
, ".fsxlog");
1142 fsxlogf
= fopen(logfile
, "w");
1143 if (fsxlogf
== NULL
) {
1149 file_size
= maxfilelen
= lseek(fd
, (off_t
)0, SEEK_END
);
1150 if (file_size
== (off_t
)-1) {
1152 warn("main: lseek eof");
1155 ret
= lseek(fd
, (off_t
)0, SEEK_SET
);
1156 if (ret
== (off_t
)-1) {
1158 warn("main: lseek 0");
1162 original_buf
= (char *) malloc(maxfilelen
);
1163 for (i
= 0; i
< maxfilelen
; i
++)
1164 original_buf
[i
] = random() % 256;
1165 good_buf
= (char *) malloc(maxfilelen
);
1166 memset(good_buf
, '\0', maxfilelen
);
1167 temp_buf
= (char *) malloc(maxoplen
);
1168 memset(temp_buf
, '\0', maxoplen
);
1169 if (lite
) { /* zero entire existing file */
1172 written
= write(fd
, good_buf
, (size_t)maxfilelen
);
1173 if (written
!= maxfilelen
) {
1174 if (written
== -1) {
1176 warn("main: error on write");
1178 warn("main: short write, 0x%x bytes instead of 0x%x\n",
1179 (unsigned)written
, maxfilelen
);
1185 while (numops
== -1 || numops
--)
1192 prt("All operations completed A-OK!\n");