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