Whamcloud - gitweb
8524c91f173f48f7167dcef7763ce8e0dc1135b9
[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", 
299                                 (unsigned long long)size_by_seek,
300                                 (unsigned long long)bufferlength);
301                         bufferlength = size_by_seek;
302                 }
303         }
304
305         ret = lseek(fd, (off_t)0, SEEK_SET);
306         if (ret == (off_t)-1)
307                 prterr("save_buffer: lseek 0");
308
309         byteswritten = write(fd, buffer, (size_t)bufferlength);
310         if (byteswritten != bufferlength) {
311                 if (byteswritten == -1)
312                         prterr("save_buffer write");
313                 else
314                         warn("save_buffer: short write, 0x%x bytes instead"
315                                 "of 0x%llx\n",
316                              (unsigned)byteswritten,
317                              (unsigned long long)bufferlength);
318         }
319 }
320
321
322 void
323 report_failure(int status)
324 {
325         logdump();
326
327         if (fsxgoodfd) {
328                 if (good_buf) {
329                         save_buffer(good_buf, file_size, fsxgoodfd);
330                         prt("Correct content saved for comparison\n");
331                         prt("(maybe hexdump \"%s\" vs \"%s\")\n",
332                             fname, goodfile);
333                 }
334                 close(fsxgoodfd);
335         }
336         exit(status);
337 }
338
339
340 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
341                                         *(((unsigned char *)(cp)) + 1)))
342
343 void
344 check_buffers(unsigned offset, unsigned size)
345 {
346         unsigned char c, t;
347         unsigned i = 0;
348         unsigned n = 0;
349         unsigned op = 0;
350         unsigned bad = 0;
351
352         if (memcmp(good_buf + offset, temp_buf, size) != 0) {
353                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
354                     offset, size);
355                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
356                 while (size > 0) {
357                         c = good_buf[offset];
358                         t = temp_buf[i];
359                         if (c != t) {
360                                 if (n == 0) {
361                                         bad = short_at(&temp_buf[i]);
362                                         prt("0x%5x\t0x%04x\t0x%04x", offset,
363                                             short_at(&good_buf[offset]), bad);
364                                         op = temp_buf[offset & 1 ? i+1 : i];
365                                 }
366                                 n++;
367                                 badoff = offset;
368                         }
369                         offset++;
370                         i++;
371                         size--;
372                 }
373                 if (n) {
374                         prt("\t0x%5x\n", n);
375                         if (bad)
376                                 prt("operation# (mod 256) for the bad data"
377                                         "may be %u\n", ((unsigned)op & 0xff));
378                         else
379                                 prt("operation# (mod 256) for the bad data"
380                                         "unknown, check HOLE and EXTEND ops\n");
381                 } else
382                         prt("????????????????\n");
383                 report_failure(110);
384         }
385 }
386
387
388 void
389 check_size(void)
390 {
391         struct stat     statbuf;
392         off_t   size_by_seek;
393
394         if (fstat(fd, &statbuf)) {
395                 prterr("check_size: fstat");
396                 statbuf.st_size = -1;
397         }
398         size_by_seek = lseek(fd, (off_t)0, SEEK_END);
399         if (file_size != statbuf.st_size || file_size != size_by_seek) {
400                 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
401                     (unsigned long long)file_size,
402                     (unsigned long long)statbuf.st_size,
403                     (unsigned long long)size_by_seek);
404                 report_failure(120);
405         }
406 }
407
408
409 void
410 check_trunc_hack(void)
411 {
412         struct stat statbuf;
413
414         ftruncate(fd, (off_t)0);
415         ftruncate(fd, (off_t)100000);
416         fstat(fd, &statbuf);
417         if (statbuf.st_size != (off_t)100000) {
418                 prt("no extend on truncate! not posix!\n");
419                 exit(130);
420         }
421         ftruncate(fd, 0);
422 }
423
424
425 void
426 doread(unsigned offset, unsigned size)
427 {
428         struct timeval t;
429         off_t ret;
430         unsigned iret;
431
432         offset -= offset % readbdy;
433         gettimeofday(&t, NULL);
434         if (size == 0) {
435                 if (!quiet && testcalls > simulatedopcount)
436                         prt("skipping zero size read\n");
437                 log4(OP_SKIPPED, OP_READ, offset, size, &t);
438                 return;
439         }
440         if (size + offset > file_size) {
441                 if (!quiet && testcalls > simulatedopcount)
442                         prt("skipping seek/read past end of file\n");
443                 log4(OP_SKIPPED, OP_READ, offset, size, &t);
444                 return;
445         }
446
447         log4(OP_READ, offset, size, 0, &t);
448
449         if (testcalls <= simulatedopcount)
450                 return;
451
452         if (!quiet && ((progressinterval &&
453                         testcalls % progressinterval == 0) ||
454                        (debug &&
455                         (monitorstart == -1 ||
456                          (offset + size > monitorstart &&
457                           (monitorend == -1 || offset <= monitorend))))))
458                 prt("%06lu %lu.%06lu read       %#08x thru %#08x\t(0x%x bytes)\n",
459                     testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
460                     size);
461         ret = lseek(fd, (off_t)offset, SEEK_SET);
462         if (ret == (off_t)-1) {
463                 prterr("doread: lseek");
464                 report_failure(140);
465         }
466         iret = read(fd, temp_buf, size);
467         if (!quiet && (debug > 1 &&
468                         (monitorstart == -1 ||
469                          (offset + size > monitorstart &&
470                           (monitorend == -1 || offset <= monitorend))))) {
471                 gettimeofday(&t, NULL);
472                 prt("       %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
473         }
474         if (iret != size) {
475                 if (iret == -1)
476                         prterr("doread: read");
477                 else
478                         prt("short read: 0x%x bytes instead of 0x%x\n",
479                             iret, size);
480                 report_failure(141);
481         }
482         check_buffers(offset, size);
483 }
484
485
486 void
487 domapread(unsigned offset, unsigned size)
488 {
489         struct timeval t;
490         unsigned pg_offset;
491         unsigned map_size;
492         char    *p;
493
494         offset -= offset % readbdy;
495         gettimeofday(&t, NULL);
496         if (size == 0) {
497                 if (!quiet && testcalls > simulatedopcount)
498                         prt("skipping zero size read\n");
499                 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
500                 return;
501         }
502         if (size + offset > file_size) {
503                 if (!quiet && testcalls > simulatedopcount)
504                         prt("skipping seek/read past end of file\n");
505                 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
506                 return;
507         }
508
509         log4(OP_MAPREAD, offset, size, 0, &t);
510
511         if (testcalls <= simulatedopcount)
512                 return;
513
514         if (!quiet && ((progressinterval &&
515                         testcalls % progressinterval == 0) ||
516                        (debug &&
517                         (monitorstart == -1 ||
518                          (offset + size > monitorstart &&
519                           (monitorend == -1 || offset <= monitorend))))))
520                 prt("%06lu %lu.%06lu mapread    %#08x thru %#08x\t(0x%x bytes)\n",
521                     testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
522                     size);
523
524         pg_offset = offset & page_mask;
525         map_size  = pg_offset + size;
526
527         if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
528                       (off_t)(offset - pg_offset))) == MAP_FAILED) {
529                 prterr("domapread: mmap");
530                 report_failure(190);
531         }
532         if (!quiet && (debug > 1 &&
533                         (monitorstart == -1 ||
534                          (offset + size > monitorstart &&
535                           (monitorend == -1 || offset <= monitorend))))) {
536                 gettimeofday(&t, NULL);
537                 prt("       %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
538         }
539         memcpy(temp_buf, p + pg_offset, size);
540         if (!quiet && (debug > 1 &&
541                         (monitorstart == -1 ||
542                          (offset + size > monitorstart &&
543                           (monitorend == -1 || offset <= monitorend))))) {
544                 gettimeofday(&t, NULL);
545                 prt("       %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
546         }
547         if (munmap(p, map_size) != 0) {
548                 prterr("domapread: munmap");
549                 report_failure(191);
550         }
551         if (!quiet && (debug > 1 &&
552                         (monitorstart == -1 ||
553                          (offset + size > monitorstart &&
554                           (monitorend == -1 || offset <= monitorend))))) {
555                 gettimeofday(&t, NULL);
556                 prt("       %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
557         }
558
559         check_buffers(offset, size);
560 }
561
562
563 void
564 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
565 {
566         while (size--) {
567                 good_buf[offset] = testcalls % 256;
568                 if (offset % 2)
569                         good_buf[offset] += original_buf[offset];
570                 offset++;
571         }
572 }
573
574
575 void
576 dowrite(unsigned offset, unsigned size)
577 {
578         struct timeval t;
579         off_t ret;
580         unsigned iret;
581
582         offset -= offset % writebdy;
583         gettimeofday(&t, NULL);
584         if (size == 0) {
585                 if (!quiet && testcalls > simulatedopcount)
586                         prt("skipping zero size write\n");
587                 log4(OP_SKIPPED, OP_WRITE, offset, size, &t);
588                 return;
589         }
590
591         log4(OP_WRITE, offset, size, file_size, &t);
592
593         gendata(original_buf, good_buf, offset, size);
594         if (file_size < offset + size) {
595                 if (file_size < offset)
596                         memset(good_buf + file_size, '\0', offset - file_size);
597                 file_size = offset + size;
598                 if (lite) {
599                         warn("Lite file size bug in fsx!");
600                         report_failure(149);
601                 }
602         }
603
604         if (testcalls <= simulatedopcount)
605                 return;
606
607         if (!quiet && ((progressinterval &&
608                         testcalls % progressinterval == 0) ||
609                        (debug &&
610                         (monitorstart == -1 ||
611                          (offset + size > monitorstart &&
612                           (monitorend == -1 || offset <= monitorend))))))
613                 prt("%06lu %lu.%06lu write      %#08x thru %#08x\t(0x%x bytes)\n",
614                     testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
615                     size);
616         ret = lseek(fd, (off_t)offset, SEEK_SET);
617         if (ret == (off_t)-1) {
618                 prterr("dowrite: lseek");
619                 report_failure(150);
620         }
621         iret = write(fd, good_buf + offset, size);
622         if (!quiet && (debug > 1 &&
623                         (monitorstart == -1 ||
624                          (offset + size > monitorstart &&
625                           (monitorend == -1 || offset <= monitorend))))) {
626                 gettimeofday(&t, NULL);
627                 prt("       %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
628         }
629         if (iret != size) {
630                 if (iret == -1)
631                         prterr("dowrite: write");
632                 else
633                         prt("short write: 0x%x bytes instead of 0x%x\n",
634                             iret, size);
635                 report_failure(151);
636         }
637 }
638
639
640 void
641 domapwrite(unsigned offset, unsigned size)
642 {
643         struct timeval t;
644         unsigned pg_offset;
645         unsigned map_size;
646         off_t    cur_filesize;
647         char    *p;
648
649         offset -= offset % writebdy;
650         gettimeofday(&t, NULL);
651         if (size == 0) {
652                 if (!quiet && testcalls > simulatedopcount)
653                         prt("skipping zero size write\n");
654                 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t);
655                 return;
656         }
657         cur_filesize = file_size;
658
659         log4(OP_MAPWRITE, offset, size, 0, &t);
660
661         gendata(original_buf, good_buf, offset, size);
662         if (file_size < offset + size) {
663                 if (file_size < offset)
664                         memset(good_buf + file_size, '\0', offset - file_size);
665                 file_size = offset + size;
666                 if (lite) {
667                         warn("Lite file size bug in fsx!");
668                         report_failure(200);
669                 }
670         }
671
672         if (testcalls <= simulatedopcount)
673                 return;
674
675         if (!quiet && ((progressinterval &&
676                         testcalls % progressinterval == 0) ||
677                        (debug &&
678                         (monitorstart == -1 ||
679                          (offset + size > monitorstart &&
680                           (monitorend == -1 || offset <= monitorend))))))
681                 prt("%06lu %lu.%06lu mapwrite   %#08x thru %#08x\t(0x%x bytes)\n",
682                     testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
683                     size);
684
685         if (file_size > cur_filesize) {
686                 if (ftruncate(fd, file_size) == -1) {
687                         prterr("domapwrite: ftruncate");
688                         exit(201);
689                 }
690                 if (!quiet && (debug > 1 &&
691                                (monitorstart == -1 ||
692                                 (offset + size > monitorstart &&
693                                  (monitorend == -1 || offset <= monitorend))))) {
694                         gettimeofday(&t, NULL);
695                         prt("       %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
696         }
697         }
698         pg_offset = offset & page_mask;
699         map_size  = pg_offset + size;
700
701         if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
702                       fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
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 rang"
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 }