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 -- [email protected]
28 *
29 * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
30 *
31 * Small changes to work under Linux -- [email protected]
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 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
prt(const char * fmt,...)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
prterr(const char * prefix)157 prterr(const char *prefix)
158 {
159 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
160 }
161
162
163 void
do_log4(int operation,int arg0,int arg1,int arg2)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
log4(int operation,int arg0,int arg1,int arg2)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
logdump(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
save_buffer(char * buffer,off_t bufferlength,int savefd)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
report_failure(int status)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
check_buffers(unsigned offset,unsigned size)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
check_size(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
check_trunc_hack(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
doread(off_t offset,off_t size)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
check_eofpage(const char * s,unsigned offset,char * p,int size)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
domapread(unsigned offset,unsigned size)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
gendata(unsigned offset,unsigned size)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
dowrite(unsigned offset,unsigned size)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
domapwrite(unsigned offset,unsigned size)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
dotruncate(unsigned size)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
writefileimage()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
docloseopen(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
doinvl(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
test(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
cleanup(sig)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
usage(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)\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
getnum(char * s,char ** e)938 getnum(char *s, char **e)
939 {
940 int ret = -1;
941
942 *e = (char *) 0;
943 ret = strtol(s, e, 0);
944 if (*e)
945 switch (**e) {
946 case 'b':
947 case 'B':
948 ret *= 512;
949 *e = *e + 1;
950 break;
951 case 'k':
952 case 'K':
953 ret *= 1024;
954 *e = *e + 1;
955 break;
956 case 'm':
957 case 'M':
958 ret *= 1024*1024;
959 *e = *e + 1;
960 break;
961 case 'w':
962 case 'W':
963 ret *= 4;
964 *e = *e + 1;
965 break;
966 }
967 return (ret);
968 }
969
970
971 int
main(int argc,char ** argv)972 main(int argc, char **argv)
973 {
974 int i, ch;
975 char *endp;
976 char goodfile[1024];
977 char logfile[1024];
978 struct timespec now;
979
980 goodfile[0] = 0;
981 logfile[0] = 0;
982
983 page_size = getpagesize();
984 page_mask = page_size - 1;
985
986 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
987
988 while ((ch = getopt(argc, argv,
989 "b:c:di:l:m:no:p:qr:s:t:w:D:LN:OP:RS:UW")) != -1)
990 switch (ch) {
991 case 'b':
992 simulatedopcount = getnum(optarg, &endp);
993 if (!quiet)
994 fprintf(stdout, "Will begin at operation %ld\n",
995 simulatedopcount);
996 if (simulatedopcount == 0)
997 usage();
998 simulatedopcount -= 1;
999 break;
1000 case 'c':
1001 closeprob = getnum(optarg, &endp);
1002 if (!quiet)
1003 fprintf(stdout,
1004 "Chance of close/open is 1 in %d\n",
1005 closeprob);
1006 if (closeprob <= 0)
1007 usage();
1008 break;
1009 case 'd':
1010 debug = 1;
1011 break;
1012 case 'i':
1013 invlprob = getnum(optarg, &endp);
1014 if (!quiet)
1015 fprintf(stdout,
1016 "Chance of MS_INVALIDATE is 1 in %d\n",
1017 invlprob);
1018 if (invlprob <= 0)
1019 usage();
1020 break;
1021 case 'l':
1022 maxfilelen = getnum(optarg, &endp);
1023 if (maxfilelen <= 0)
1024 usage();
1025 break;
1026 case 'm':
1027 monitorstart = getnum(optarg, &endp);
1028 if (monitorstart < 0)
1029 usage();
1030 if (!endp || *endp++ != ':')
1031 usage();
1032 monitorend = getnum(endp, &endp);
1033 if (monitorend < 0)
1034 usage();
1035 if (monitorend == 0)
1036 monitorend = -1; /* aka infinity */
1037 debug = 1;
1038 case 'n':
1039 sizechecks = 0;
1040 break;
1041 case 'o':
1042 maxoplen = getnum(optarg, &endp);
1043 if (maxoplen <= 0)
1044 usage();
1045 break;
1046 case 'p':
1047 progressinterval = getnum(optarg, &endp);
1048 if (progressinterval < 0)
1049 usage();
1050 break;
1051 case 'q':
1052 quiet = 1;
1053 break;
1054 case 'r':
1055 readbdy = getnum(optarg, &endp);
1056 if (readbdy <= 0)
1057 usage();
1058 break;
1059 case 's':
1060 style = getnum(optarg, &endp);
1061 if (style < 0 || style > 1)
1062 usage();
1063 break;
1064 case 't':
1065 truncbdy = getnum(optarg, &endp);
1066 if (truncbdy <= 0)
1067 usage();
1068 break;
1069 case 'w':
1070 writebdy = getnum(optarg, &endp);
1071 if (writebdy <= 0)
1072 usage();
1073 break;
1074 case 'D':
1075 debugstart = getnum(optarg, &endp);
1076 if (debugstart < 1)
1077 usage();
1078 break;
1079 case 'L':
1080 lite = 1;
1081 break;
1082 case 'N':
1083 numops = getnum(optarg, &endp);
1084 if (numops < 0)
1085 usage();
1086 break;
1087 case 'O':
1088 randomoplen = 0;
1089 break;
1090 case 'P':
1091 strncpy(goodfile, optarg, sizeof(goodfile));
1092 strcat(goodfile, "/");
1093 strncpy(logfile, optarg, sizeof(logfile));
1094 strcat(logfile, "/");
1095 break;
1096 case 'R':
1097 mapped_reads = 0;
1098 break;
1099 case 'S':
1100 seed = getnum(optarg, &endp);
1101 if (seed == 0) {
1102 if (clock_gettime(CLOCK_REALTIME, &now) != 0)
1103 err(1, "clock_gettime");
1104 seed = now.tv_nsec % 10000;
1105 }
1106 if (!quiet)
1107 fprintf(stdout, "Seed set to %d\n", seed);
1108 if (seed < 0)
1109 usage();
1110 break;
1111 case 'W':
1112 mapped_writes = 0;
1113 if (!quiet)
1114 fprintf(stdout, "mapped writes DISABLED\n");
1115 break;
1116 case 'U':
1117 mapped_msync = 0;
1118 if (!quiet)
1119 fprintf(stdout, "mapped msync DISABLED\n");
1120 break;
1121
1122 default:
1123 usage();
1124 /* NOTREACHED */
1125 }
1126 argc -= optind;
1127 argv += optind;
1128 if (argc != 1)
1129 usage();
1130 fname = argv[0];
1131
1132 signal(SIGHUP, cleanup);
1133 signal(SIGINT, cleanup);
1134 signal(SIGPIPE, cleanup);
1135 signal(SIGALRM, cleanup);
1136 signal(SIGTERM, cleanup);
1137 signal(SIGXCPU, cleanup);
1138 signal(SIGXFSZ, cleanup);
1139 signal(SIGVTALRM, cleanup);
1140 signal(SIGUSR1, cleanup);
1141 signal(SIGUSR2, cleanup);
1142
1143 initstate(seed, state, 256);
1144 setstate(state);
1145 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1146 if (fd < 0) {
1147 prterr(fname);
1148 exit(91);
1149 }
1150 strncat(goodfile, fname, 256);
1151 strcat (goodfile, ".fsxgood");
1152 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1153 if (fsxgoodfd < 0) {
1154 prterr(goodfile);
1155 exit(92);
1156 }
1157 strncat(logfile, fname, 256);
1158 strcat (logfile, ".fsxlog");
1159 fsxlogf = fopen(logfile, "w");
1160 if (fsxlogf == NULL) {
1161 prterr(logfile);
1162 exit(93);
1163 }
1164 if (lite) {
1165 off_t ret;
1166 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1167 if (file_size == (off_t)-1) {
1168 prterr(fname);
1169 warn("main: lseek eof");
1170 exit(94);
1171 }
1172 ret = lseek(fd, (off_t)0, SEEK_SET);
1173 if (ret == (off_t)-1) {
1174 prterr(fname);
1175 warn("main: lseek 0");
1176 exit(95);
1177 }
1178 }
1179 original_buf = (char *) malloc(maxfilelen);
1180 for (i = 0; i < maxfilelen; i++)
1181 original_buf[i] = random() % 256;
1182 good_buf = (char *) malloc(maxfilelen);
1183 memset(good_buf, '\0', maxfilelen);
1184 temp_buf = (char *) malloc(maxoplen);
1185 memset(temp_buf, '\0', maxoplen);
1186 if (lite) { /* zero entire existing file */
1187 ssize_t written;
1188
1189 written = write(fd, good_buf, (size_t)maxfilelen);
1190 if (written != maxfilelen) {
1191 if (written == -1) {
1192 prterr(fname);
1193 warn("main: error on write");
1194 } else
1195 warn("main: short write, 0x%x bytes instead of 0x%lx\n",
1196 (unsigned)written, maxfilelen);
1197 exit(98);
1198 }
1199 } else
1200 check_trunc_hack();
1201
1202 while (numops == -1 || numops--)
1203 test();
1204
1205 if (close(fd)) {
1206 prterr("close");
1207 report_failure(99);
1208 }
1209 prt("All operations completed A-OK!\n");
1210
1211 exit(0);
1212 return 0;
1213 }
1214
1215