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