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