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