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