xref: /freebsd/tools/regression/fsx/fsx.c (revision b3512b30dbec579da28028e29d8b33ec7242af68)
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$
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 <err.h>
52 #include <fcntl.h>
53 #include <sys/mman.h>
54 #ifndef MAP_FILE
55 # define MAP_FILE 0
56 #endif
57 #include <limits.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <stdarg.h>
64 #include <errno.h>
65 
66 #define NUMPRINTCOLUMNS 32	/* # columns of data to print on each line */
67 
68 /*
69  *	A log entry is an operation and a bunch of arguments.
70  */
71 
72 struct log_entry {
73 	int	operation;
74 	int	args[3];
75 };
76 
77 #define	LOGSIZE	1000
78 
79 struct log_entry	oplog[LOGSIZE];	/* the log */
80 int			logptr = 0;	/* current position in log */
81 int			logcount = 0;	/* total ops */
82 
83 /*
84  *	Define operations
85  */
86 
87 #define	OP_READ		1
88 #define OP_WRITE	2
89 #define OP_TRUNCATE	3
90 #define OP_CLOSEOPEN	4
91 #define OP_MAPREAD	5
92 #define OP_MAPWRITE	6
93 #define OP_SKIPPED	7
94 #define OP_INVALIDATE	8
95 
96 int page_size;
97 int page_mask;
98 
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 */
104 
105 off_t		file_size = 0;
106 off_t		biggest = 0;
107 char		state[256];
108 unsigned long	testcalls = 0;		/* calls to function "test" */
109 
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 */
133 int	fsxgoodfd = 0;
134 FILE *	fsxlogf = NULL;
135 int badoff = -1;
136 int closeopen = 0;
137 int invl = 0;
138 
139 
140 void
141 vwarnc(code, fmt, ap)
142 	int code;
143 	const char *fmt;
144 	va_list ap;
145 {
146 	fprintf(stderr, "fsx: ");
147 	if (fmt != NULL) {
148 		vfprintf(stderr, fmt, ap);
149 		fprintf(stderr, ": ");
150 	}
151 	fprintf(stderr, "%s\n", strerror(code));
152 }
153 
154 
155 void
156 warn(const char * fmt, ...)
157 {
158 	va_list ap;
159 	va_start(ap, fmt);
160 	vwarnc(errno, fmt, ap);
161 	va_end(ap);
162 }
163 
164 
165 void
166 prt(char *fmt, ...)
167 {
168 	va_list args;
169 
170 	va_start(args, fmt);
171 	vfprintf(stdout, fmt, args);
172 	va_end(args);
173 
174 	if (fsxlogf) {
175 		va_start(args, fmt);
176 		vfprintf(fsxlogf, fmt, args);
177 		va_end(args);
178 	}
179 }
180 
181 void
182 prterr(char *prefix)
183 {
184 	prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
185 }
186 
187 
188 void
189 do_log4(int operation, int arg0, int arg1, int arg2)
190 {
191 	struct log_entry *le;
192 
193 	le = &oplog[logptr];
194 	le->operation = operation;
195 	le->args[0] = arg0;
196 	le->args[1] = arg1;
197 	le->args[2] = arg2;
198 	logptr++;
199 	logcount++;
200 	if (logptr >= LOGSIZE)
201 		logptr = 0;
202 }
203 
204 
205 void
206 log4(int operation, int arg0, int arg1, int arg2)
207 {
208 	do_log4(operation, arg0, arg1, arg2);
209 	if (closeopen)
210 		do_log4(OP_CLOSEOPEN, 0, 0, 0);
211 	if (invl)
212 		do_log4(OP_INVALIDATE, 0, 0, 0);
213 }
214 
215 
216 void
217 logdump(void)
218 {
219 	struct log_entry	*lp;
220 	int	i, count, down, opnum;
221 
222 	prt("LOG DUMP (%d total operations):\n", logcount);
223 	if (logcount < LOGSIZE) {
224 		i = 0;
225 		count = logcount;
226 	} else {
227 		i = logptr;
228 		count = LOGSIZE;
229 	}
230 
231 	opnum = i + 1 + (logcount/LOGSIZE)*LOGSIZE;
232 	for ( ; count > 0; count--) {
233 		lp = &oplog[i];
234 
235 		if (lp->operation == OP_CLOSEOPEN ||
236 		    lp->operation == OP_INVALIDATE) {
237 			switch (lp->operation) {
238 			case OP_CLOSEOPEN:
239 				prt("\t\tCLOSE/OPEN\n");
240 				break;
241 			case OP_INVALIDATE:
242 				prt("\t\tMS_INVALIDATE\n");
243 				break;
244 			}
245 			i++;
246 			if (i == LOGSIZE)
247 				i = 0;
248 			continue;
249 		}
250 
251 		prt("%d(%d mod 256): ", opnum, opnum%256);
252 		switch (lp->operation) {
253 		case OP_MAPREAD:
254 			prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
255 			    lp->args[0], lp->args[0] + lp->args[1] - 1,
256 			    lp->args[1]);
257 			if (badoff >= lp->args[0] && badoff <
258 						     lp->args[0] + lp->args[1])
259 				prt("\t***RRRR***");
260 			break;
261 		case OP_MAPWRITE:
262 			prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
263 			    lp->args[0], lp->args[0] + lp->args[1] - 1,
264 			    lp->args[1]);
265 			if (badoff >= lp->args[0] && badoff <
266 						     lp->args[0] + lp->args[1])
267 				prt("\t******WWWW");
268 			break;
269 		case OP_READ:
270 			prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
271 			    lp->args[0], lp->args[0] + lp->args[1] - 1,
272 			    lp->args[1]);
273 			if (badoff >= lp->args[0] &&
274 			    badoff < lp->args[0] + lp->args[1])
275 				prt("\t***RRRR***");
276 			break;
277 		case OP_WRITE:
278 			{
279 				int offset = lp->args[0];
280 				int len = lp->args[1];
281 				int oldlen = lp->args[2];
282 
283 				prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
284 				    offset, offset + len - 1,
285 				    len);
286 				if (offset > oldlen)
287 					prt(" HOLE");
288 				else if (offset + len > oldlen)
289 					prt(" EXTEND");
290 				if ((badoff >= offset || badoff >=oldlen) &&
291 				    badoff < offset + len)
292 					prt("\t***WWWW");
293 			}
294 			break;
295 		case OP_TRUNCATE:
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])
301 				prt("\t******WWWW");
302 			break;
303 		case OP_SKIPPED:
304 			prt("SKIPPED (no operation)");
305 			break;
306 		default:
307 			prt("BOGUS LOG ENTRY (operation code = %d)!",
308 			    lp->operation);
309 		}
310 		prt("\n");
311 		opnum++;
312 		i++;
313 		if (i == LOGSIZE)
314 			i = 0;
315 	}
316 }
317 
318 
319 void
320 save_buffer(char *buffer, off_t bufferlength, int fd)
321 {
322 	off_t ret;
323 	ssize_t byteswritten;
324 
325 	if (fd <= 0 || bufferlength == 0)
326 		return;
327 
328 	if (bufferlength > SSIZE_MAX) {
329 		prt("fsx flaw: overflow in save_buffer\n");
330 		exit(67);
331 	}
332 	if (lite) {
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;
340 		}
341 	}
342 
343 	ret = lseek(fd, (off_t)0, SEEK_SET);
344 	if (ret == (off_t)-1)
345 		prterr("save_buffer: lseek 0");
346 
347 	byteswritten = write(fd, buffer, (size_t)bufferlength);
348 	if (byteswritten != bufferlength) {
349 		if (byteswritten == -1)
350 			prterr("save_buffer write");
351 		else
352 			warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
353 			     (unsigned)byteswritten,
354 			     (unsigned long long)bufferlength);
355 	}
356 }
357 
358 
359 void
360 report_failure(int status)
361 {
362 	logdump();
363 
364 	if (fsxgoodfd) {
365 		if (good_buf) {
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",
369 			    fname, fname);
370 		}
371 		close(fsxgoodfd);
372 	}
373 	exit(status);
374 }
375 
376 
377 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
378 					*(((unsigned char *)(cp)) + 1)))
379 
380 void
381 check_buffers(unsigned offset, unsigned size)
382 {
383 	unsigned char c, t;
384 	unsigned i = 0;
385 	unsigned n = 0;
386 	unsigned op = 0;
387 	unsigned bad = 0;
388 
389 	if (memcmp(good_buf + offset, temp_buf, size) != 0) {
390 		prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
391 		    offset, size);
392 		prt("OFFSET\tGOOD\tBAD\tRANGE\n");
393 		while (size > 0) {
394 			c = good_buf[offset];
395 			t = temp_buf[i];
396 			if (c != t) {
397 				if (n == 0) {
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];
402 				}
403 				n++;
404 				badoff = offset;
405 			}
406 			offset++;
407 			i++;
408 			size--;
409 		}
410 		if (n) {
411 			prt("\t0x%5x\n", n);
412 			if (bad)
413 				prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
414 			else
415 				prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
416 		} else
417 			prt("????????????????\n");
418 		report_failure(110);
419 	}
420 }
421 
422 
423 void
424 check_size(void)
425 {
426 	struct stat	statbuf;
427 	off_t	size_by_seek;
428 
429 	if (fstat(fd, &statbuf)) {
430 		prterr("check_size: fstat");
431 		statbuf.st_size = -1;
432 	}
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);
439 		report_failure(120);
440 	}
441 }
442 
443 
444 void
445 check_trunc_hack(void)
446 {
447 	struct stat statbuf;
448 
449 	ftruncate(fd, (off_t)0);
450 	ftruncate(fd, (off_t)100000);
451 	fstat(fd, &statbuf);
452 	if (statbuf.st_size != (off_t)100000) {
453 		prt("no extend on truncate! not posix!\n");
454 		exit(130);
455 	}
456 	ftruncate(fd, (off_t)0);
457 }
458 
459 
460 void
461 doread(unsigned offset, unsigned size)
462 {
463 	off_t ret;
464 	unsigned iret;
465 
466 	offset -= offset % readbdy;
467 	if (size == 0) {
468 		if (!quiet && testcalls > simulatedopcount)
469 			prt("skipping zero size read\n");
470 		log4(OP_SKIPPED, OP_READ, offset, size);
471 		return;
472 	}
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);
477 		return;
478 	}
479 
480 	log4(OP_READ, offset, size, 0);
481 
482 	if (testcalls <= simulatedopcount)
483 		return;
484 
485 	if (!quiet && ((progressinterval &&
486 			testcalls % progressinterval == 0) ||
487 		       (debug &&
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");
496 		report_failure(140);
497 	}
498 	iret = read(fd, temp_buf, size);
499 	if (iret != size) {
500 		if (iret == -1)
501 			prterr("doread: read");
502 		else
503 			prt("short read: 0x%x bytes instead of 0x%x\n",
504 			    iret, size);
505 		report_failure(141);
506 	}
507 	check_buffers(offset, size);
508 }
509 
510 
511 void
512 check_eofpage(char *s, unsigned offset, char *p, int size)
513 {
514 	uintptr_t last_page, should_be_zero;
515 
516 	if (offset + size <= (file_size & ~page_mask))
517 		return;
518 	/*
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)
523 	 */
524 	last_page = ((uintptr_t)p + (offset & page_mask) + size) & ~page_mask;
525 
526 	for (should_be_zero = last_page + (file_size & page_mask);
527 	     should_be_zero < last_page + page_size;
528 	     should_be_zero++)
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));
533 			report_failure(205);
534 		}
535 }
536 
537 
538 void
539 domapread(unsigned offset, unsigned size)
540 {
541 	unsigned pg_offset;
542 	unsigned map_size;
543 	char    *p;
544 
545 	offset -= offset % readbdy;
546 	if (size == 0) {
547 		if (!quiet && testcalls > simulatedopcount)
548 			prt("skipping zero size read\n");
549 		log4(OP_SKIPPED, OP_MAPREAD, offset, size);
550 		return;
551 	}
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);
556 		return;
557 	}
558 
559 	log4(OP_MAPREAD, offset, size, 0);
560 
561 	if (testcalls <= simulatedopcount)
562 		return;
563 
564 	if (!quiet && ((progressinterval &&
565 			testcalls % progressinterval == 0) ||
566 		       (debug &&
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);
572 
573 	pg_offset = offset & page_mask;
574 	map_size  = pg_offset + size;
575 
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");
579 		report_failure(190);
580 	}
581 	memcpy(temp_buf, p + pg_offset, size);
582 
583 	check_eofpage("Read", offset, p, size);
584 
585 	if (munmap(p, map_size) != 0) {
586 		prterr("domapread: munmap");
587 		report_failure(191);
588 	}
589 
590 	check_buffers(offset, size);
591 }
592 
593 
594 void
595 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
596 {
597 	while (size--) {
598 		good_buf[offset] = testcalls % 256;
599 		if (offset % 2)
600 			good_buf[offset] += original_buf[offset];
601 		offset++;
602 	}
603 }
604 
605 
606 void
607 dowrite(unsigned offset, unsigned size)
608 {
609 	off_t ret;
610 	unsigned iret;
611 
612 	offset -= offset % writebdy;
613 	if (size == 0) {
614 		if (!quiet && testcalls > simulatedopcount)
615 			prt("skipping zero size write\n");
616 		log4(OP_SKIPPED, OP_WRITE, offset, size);
617 		return;
618 	}
619 
620 	log4(OP_WRITE, offset, size, file_size);
621 
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;
627 		if (lite) {
628 			warn("Lite file size bug in fsx!");
629 			report_failure(149);
630 		}
631 	}
632 
633 	if (testcalls <= simulatedopcount)
634 		return;
635 
636 	if (!quiet && ((progressinterval &&
637 			testcalls % progressinterval == 0) ||
638 		       (debug &&
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");
647 		report_failure(150);
648 	}
649 	iret = write(fd, good_buf + offset, size);
650 	if (iret != size) {
651 		if (iret == -1)
652 			prterr("dowrite: write");
653 		else
654 			prt("short write: 0x%x bytes instead of 0x%x\n",
655 			    iret, size);
656 		report_failure(151);
657 	}
658 }
659 
660 
661 void
662 domapwrite(unsigned offset, unsigned size)
663 {
664 	unsigned pg_offset;
665 	unsigned map_size;
666 	off_t    cur_filesize;
667 	char    *p;
668 
669 	offset -= offset % writebdy;
670 	if (size == 0) {
671 		if (!quiet && testcalls > simulatedopcount)
672 			prt("skipping zero size write\n");
673 		log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
674 		return;
675 	}
676 	cur_filesize = file_size;
677 
678 	log4(OP_MAPWRITE, offset, size, 0);
679 
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;
685 		if (lite) {
686 			warn("Lite file size bug in fsx!");
687 			report_failure(200);
688 		}
689 	}
690 
691 	if (testcalls <= simulatedopcount)
692 		return;
693 
694 	if (!quiet && ((progressinterval &&
695 			testcalls % progressinterval == 0) ||
696 		       (debug &&
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);
702 
703 	if (file_size > cur_filesize) {
704 		if (ftruncate(fd, file_size) == -1) {
705 			prterr("domapwrite: ftruncate");
706 			exit(201);
707 		}
708 	}
709 	pg_offset = offset & page_mask;
710 	map_size  = pg_offset + size;
711 
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");
716 		report_failure(202);
717 	}
718 	memcpy(p + pg_offset, good_buf + offset, size);
719 	if (mapped_msync && msync(p, map_size, MS_SYNC) != 0) {
720 		prterr("domapwrite: msync");
721 		report_failure(203);
722 	}
723 
724 	check_eofpage("Write", offset, p, size);
725 
726 	if (munmap(p, map_size) != 0) {
727 		prterr("domapwrite: munmap");
728 		report_failure(204);
729 	}
730 }
731 
732 
733 void
734 dotruncate(unsigned size)
735 {
736 	int oldsize = file_size;
737 
738 	size -= size % truncbdy;
739 	if (size > biggest) {
740 		biggest = size;
741 		if (!quiet && testcalls > simulatedopcount)
742 			prt("truncating to largest ever: 0x%x\n", size);
743 	}
744 
745 	log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
746 
747 	if (size > file_size)
748 		memset(good_buf + file_size, '\0', size - file_size);
749 	file_size = size;
750 
751 	if (testcalls <= simulatedopcount)
752 		return;
753 
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");
761 		report_failure(160);
762 	}
763 }
764 
765 
766 void
767 writefileimage()
768 {
769 	ssize_t iret;
770 
771 	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
772 		prterr("writefileimage: lseek");
773 		report_failure(171);
774 	}
775 	iret = write(fd, good_buf, file_size);
776 	if ((off_t)iret != file_size) {
777 		if (iret == -1)
778 			prterr("writefileimage: write");
779 		else
780 			prt("short write: 0x%x bytes instead of 0x%llx\n",
781 			    iret, (unsigned long long)file_size);
782 		report_failure(172);
783 	}
784 	if (lite ? 0 : ftruncate(fd, file_size) == -1) {
785 		prt("ftruncate2: %llx\n", (unsigned long long)file_size);
786 		prterr("writefileimage: ftruncate");
787 		report_failure(173);
788 	}
789 }
790 
791 
792 void
793 docloseopen(void)
794 {
795 	if (testcalls <= simulatedopcount)
796 		return;
797 
798 	if (debug)
799 		prt("%lu close/open\n", testcalls);
800 	if (close(fd)) {
801 		prterr("docloseopen: close");
802 		report_failure(180);
803 	}
804 	fd = open(fname, O_RDWR, 0);
805 	if (fd < 0) {
806 		prterr("docloseopen: open");
807 		report_failure(181);
808 	}
809 }
810 
811 
812 void
813 doinvl(void)
814 {
815 	char *p;
816 
817 	if (file_size == 0)
818 		return;
819 	if (testcalls <= simulatedopcount)
820 		return;
821 	if (debug)
822 		prt("%lu msync(MS_INVALIDATE)\n", testcalls);
823 
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");
827 		report_failure(205);
828 	}
829 
830 	if (msync(p, 0, MS_SYNC | MS_INVALIDATE) != 0) {
831 		prterr("doinvl: msync");
832 		report_failure(206);
833 	}
834 
835 	if (munmap(p, file_size) != 0) {
836 		prterr("doinvl: munmap");
837 		report_failure(207);
838 	}
839 }
840 
841 
842 void
843 test(void)
844 {
845 	unsigned long	offset;
846 	unsigned long	size = maxoplen;
847 	unsigned long	rv = random();
848 	unsigned long	op = rv % (3 + !lite + mapped_writes);
849 
850 	/* turn off the map read if necessary */
851 
852 	if (op == 2 && !mapped_reads)
853 	    op = 0;
854 
855 	if (simulatedopcount > 0 && testcalls == simulatedopcount)
856 		writefileimage();
857 
858 	testcalls++;
859 
860 	if (closeprob)
861 		closeopen = (rv >> 3) < (1 << 28) / closeprob;
862 	if (invlprob)
863 		invl = (rv >> 3) < (1 << 28) / invlprob;
864 
865 	if (debugstart > 0 && testcalls >= debugstart)
866 		debug = 1;
867 
868 	if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
869 		prt("%lu...\n", testcalls);
870 
871 	/*
872 	 * READ:	op = 0
873 	 * WRITE:	op = 1
874 	 * MAPREAD:     op = 2
875 	 * TRUNCATE:	op = 3
876 	 * MAPWRITE:    op = 3 or 4
877 	 */
878 	if (lite ? 0 : op == 3 && style == 0) /* vanilla truncate? */
879 		dotruncate(random() % maxfilelen);
880 	else {
881 		if (randomoplen)
882 			size = random() % (maxoplen+1);
883 		if (lite ? 0 : op == 3)
884 			dotruncate(size);
885 		else {
886 			offset = random();
887 			if (op == 1 || op == (lite ? 3 : 4)) {
888 				offset %= maxfilelen;
889 				if (offset + size > maxfilelen)
890 					size = maxfilelen - offset;
891 				if (op != 1)
892 					domapwrite(offset, size);
893 				else
894 					dowrite(offset, size);
895 			} else {
896 				if (file_size)
897 					offset %= file_size;
898 				else
899 					offset = 0;
900 				if (offset + size > file_size)
901 					size = file_size - offset;
902 				if (op != 0)
903 					domapread(offset, size);
904 				else
905 					doread(offset, size);
906 			}
907 		}
908 	}
909 	if (sizechecks && testcalls > simulatedopcount)
910 		check_size();
911 	if (invl)
912 		doinvl();
913 	if (closeopen)
914 		docloseopen();
915 }
916 
917 
918 void
919 cleanup(sig)
920 	int	sig;
921 {
922 	if (sig)
923 		prt("signal %d\n", sig);
924 	prt("testcalls = %lu\n", testcalls);
925 	exit(sig);
926 }
927 
928 
929 void
930 usage(void)
931 {
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");
958 	exit(90);
959 }
960 
961 
962 int
963 getnum(char *s, char **e)
964 {
965 	int ret = -1;
966 
967 	*e = (char *) 0;
968 	ret = strtol(s, e, 0);
969 	if (*e)
970 		switch (**e) {
971 		case 'b':
972 		case 'B':
973 			ret *= 512;
974 			*e = *e + 1;
975 			break;
976 		case 'k':
977 		case 'K':
978 			ret *= 1024;
979 			*e = *e + 1;
980 			break;
981 		case 'm':
982 		case 'M':
983 			ret *= 1024*1024;
984 			*e = *e + 1;
985 			break;
986 		case 'w':
987 		case 'W':
988 			ret *= 4;
989 			*e = *e + 1;
990 			break;
991 		}
992 	return (ret);
993 }
994 
995 
996 int
997 main(int argc, char **argv)
998 {
999 	int	i, ch;
1000 	char	*endp;
1001 	char goodfile[1024];
1002 	char logfile[1024];
1003 	struct timespec now;
1004 
1005 	goodfile[0] = 0;
1006 	logfile[0] = 0;
1007 
1008 	page_size = getpagesize();
1009 	page_mask = page_size - 1;
1010 
1011 	setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1012 
1013 	while ((ch = getopt(argc, argv,
1014 	    "b:c:di:l:m:no:p:qr:s:t:w:D:LN:OP:RS:UW")) != -1)
1015 		switch (ch) {
1016 		case 'b':
1017 			simulatedopcount = getnum(optarg, &endp);
1018 			if (!quiet)
1019 				fprintf(stdout, "Will begin at operation %ld\n",
1020 					simulatedopcount);
1021 			if (simulatedopcount == 0)
1022 				usage();
1023 			simulatedopcount -= 1;
1024 			break;
1025 		case 'c':
1026 			closeprob = getnum(optarg, &endp);
1027 			if (!quiet)
1028 				fprintf(stdout,
1029 					"Chance of close/open is 1 in %d\n",
1030 					closeprob);
1031 			if (closeprob <= 0)
1032 				usage();
1033 			break;
1034 		case 'd':
1035 			debug = 1;
1036 			break;
1037 		case 'i':
1038 			invlprob = getnum(optarg, &endp);
1039 			if (!quiet)
1040 				fprintf(stdout,
1041 					"Chance of MS_INVALIDATE is 1 in %d\n",
1042 					invlprob);
1043 			if (invlprob <= 0)
1044 				usage();
1045 			break;
1046 		case 'l':
1047 			maxfilelen = getnum(optarg, &endp);
1048 			if (maxfilelen <= 0)
1049 				usage();
1050 			break;
1051 		case 'm':
1052 			monitorstart = getnum(optarg, &endp);
1053 			if (monitorstart < 0)
1054 				usage();
1055 			if (!endp || *endp++ != ':')
1056 				usage();
1057 			monitorend = getnum(endp, &endp);
1058 			if (monitorend < 0)
1059 				usage();
1060 			if (monitorend == 0)
1061 				monitorend = -1; /* aka infinity */
1062 			debug = 1;
1063 		case 'n':
1064 			sizechecks = 0;
1065 			break;
1066 		case 'o':
1067 			maxoplen = getnum(optarg, &endp);
1068 			if (maxoplen <= 0)
1069 				usage();
1070 			break;
1071 		case 'p':
1072 			progressinterval = getnum(optarg, &endp);
1073 			if (progressinterval < 0)
1074 				usage();
1075 			break;
1076 		case 'q':
1077 			quiet = 1;
1078 			break;
1079 		case 'r':
1080 			readbdy = getnum(optarg, &endp);
1081 			if (readbdy <= 0)
1082 				usage();
1083 			break;
1084 		case 's':
1085 			style = getnum(optarg, &endp);
1086 			if (style < 0 || style > 1)
1087 				usage();
1088 			break;
1089 		case 't':
1090 			truncbdy = getnum(optarg, &endp);
1091 			if (truncbdy <= 0)
1092 				usage();
1093 			break;
1094 		case 'w':
1095 			writebdy = getnum(optarg, &endp);
1096 			if (writebdy <= 0)
1097 				usage();
1098 			break;
1099 		case 'D':
1100 			debugstart = getnum(optarg, &endp);
1101 			if (debugstart < 1)
1102 				usage();
1103 			break;
1104 		case 'L':
1105 			lite = 1;
1106 			break;
1107 		case 'N':
1108 			numops = getnum(optarg, &endp);
1109 			if (numops < 0)
1110 				usage();
1111 			break;
1112 		case 'O':
1113 			randomoplen = 0;
1114 			break;
1115 		case 'P':
1116 			strncpy(goodfile, optarg, sizeof(goodfile));
1117 			strcat(goodfile, "/");
1118 			strncpy(logfile, optarg, sizeof(logfile));
1119 			strcat(logfile, "/");
1120 			break;
1121 		case 'R':
1122 			mapped_reads = 0;
1123 			break;
1124 		case 'S':
1125 			seed = getnum(optarg, &endp);
1126 			if (seed == 0) {
1127 				if (clock_gettime(CLOCK_REALTIME, &now) != 0)
1128 					err(1, "clock_gettime");
1129 				seed = now.tv_nsec % 10000;
1130 			}
1131 			if (!quiet)
1132 				fprintf(stdout, "Seed set to %d\n", seed);
1133 			if (seed < 0)
1134 				usage();
1135 			break;
1136 		case 'W':
1137 			mapped_writes = 0;
1138 			if (!quiet)
1139 				fprintf(stdout, "mapped writes DISABLED\n");
1140 			break;
1141 		case 'U':
1142 			mapped_msync = 0;
1143 			if (!quiet)
1144 				fprintf(stdout, "mapped msync DISABLED\n");
1145 			break;
1146 
1147 		default:
1148 			usage();
1149 			/* NOTREACHED */
1150 		}
1151 	argc -= optind;
1152 	argv += optind;
1153 	if (argc != 1)
1154 		usage();
1155 	fname = argv[0];
1156 
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);
1167 
1168 	initstate(seed, state, 256);
1169 	setstate(state);
1170 	fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1171 	if (fd < 0) {
1172 		prterr(fname);
1173 		exit(91);
1174 	}
1175 	strncat(goodfile, fname, 256);
1176 	strcat (goodfile, ".fsxgood");
1177 	fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1178 	if (fsxgoodfd < 0) {
1179 		prterr(goodfile);
1180 		exit(92);
1181 	}
1182 	strncat(logfile, fname, 256);
1183 	strcat (logfile, ".fsxlog");
1184 	fsxlogf = fopen(logfile, "w");
1185 	if (fsxlogf == NULL) {
1186 		prterr(logfile);
1187 		exit(93);
1188 	}
1189 	if (lite) {
1190 		off_t ret;
1191 		file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1192 		if (file_size == (off_t)-1) {
1193 			prterr(fname);
1194 			warn("main: lseek eof");
1195 			exit(94);
1196 		}
1197 		ret = lseek(fd, (off_t)0, SEEK_SET);
1198 		if (ret == (off_t)-1) {
1199 			prterr(fname);
1200 			warn("main: lseek 0");
1201 			exit(95);
1202 		}
1203 	}
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 */
1212 		ssize_t written;
1213 
1214 		written = write(fd, good_buf, (size_t)maxfilelen);
1215 		if (written != maxfilelen) {
1216 			if (written == -1) {
1217 				prterr(fname);
1218 				warn("main: error on write");
1219 			} else
1220 				warn("main: short write, 0x%x bytes instead of 0x%lx\n",
1221 				     (unsigned)written, maxfilelen);
1222 			exit(98);
1223 		}
1224 	} else
1225 		check_trunc_hack();
1226 
1227 	while (numops == -1 || numops--)
1228 		test();
1229 
1230 	if (close(fd)) {
1231 		prterr("close");
1232 		report_failure(99);
1233 	}
1234 	prt("All operations completed A-OK!\n");
1235 
1236 	exit(0);
1237 	return 0;
1238 }
1239 
1240