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