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