fsx: Generate test parameters in test()
[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  *      Checks for mmap last-page zero fill.
14  */
15
16 #include "global.h"
17
18 #include <limits.h>
19 #include <time.h>
20 #include <strings.h>
21 #include <sys/file.h>
22 #include <sys/mman.h>
23 #ifdef HAVE_ERR_H
24 #include <err.h>
25 #endif
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <errno.h>
33 #ifdef AIO
34 #include <libaio.h>
35 #endif
36
37 #ifndef MAP_FILE
38 # define MAP_FILE 0
39 #endif
40
41 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
42
43 /*
44  *      A log entry is an operation and a bunch of arguments.
45  */
46
47 struct log_entry {
48         int     operation;
49         int     args[3];
50 };
51
52 #define LOGSIZE 10000
53
54 struct log_entry        oplog[LOGSIZE]; /* the log */
55 int                     logptr = 0;     /* current position in log */
56 int                     logcount = 0;   /* total ops */
57
58 /*
59  * The operation matrix is complex due to conditional execution of different
60  * features. Hence when we come to deciding what operation to run, we need to
61  * be careful in how we select the different operations. The active operations
62  * are mapped to numbers as follows:
63  *
64  *              lite    !lite
65  * READ:        0       0
66  * WRITE:       1       1
67  * MAPREAD:     2       2
68  * MAPWRITE:    3       3
69  * TRUNCATE:    -       4
70  * FALLOCATE:   -       5
71  * PUNCH HOLE:  -       6
72  * ZERO RANGE:  -       7
73  *
74  * When mapped read/writes are disabled, they are simply converted to normal
75  * reads and writes. When fallocate/fpunch calls are disabled, they are
76  * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
77  * the operation selction matrix, as does the OP_CLOSEOPEN which is an
78  * operation modifier rather than an operation in itself.
79  *
80  * Because of the "lite" version, we also need to have different "maximum
81  * operation" defines to allow the ops to be selected correctly based on the
82  * mode being run.
83  */
84
85 /* common operations */
86 #define OP_READ         0
87 #define OP_WRITE        1
88 #define OP_MAPREAD      2
89 #define OP_MAPWRITE     3
90 #define OP_MAX_LITE     4
91
92 /* !lite operations */
93 #define OP_TRUNCATE             4
94 #define OP_FALLOCATE            5
95 #define OP_PUNCH_HOLE           6
96 #define OP_ZERO_RANGE           7
97 #define OP_COLLAPSE_RANGE       8
98 #define OP_INSERT_RANGE 9
99 #define OP_MAX_FULL             10
100
101 /* operation modifiers */
102 #define OP_CLOSEOPEN    100
103 #define OP_SKIPPED      101
104
105 #undef PAGE_SIZE
106 #define PAGE_SIZE       getpagesize()
107 #undef PAGE_MASK
108 #define PAGE_MASK       (PAGE_SIZE - 1)
109
110 char    *original_buf;                  /* a pointer to the original data */
111 char    *good_buf;                      /* a pointer to the correct data */
112 char    *temp_buf;                      /* a pointer to the current data */
113 char    *fname;                         /* name of our test file */
114 int     fd;                             /* fd for our test file */
115
116 blksize_t       block_size = 0;
117 off_t           file_size = 0;
118 off_t           biggest = 0;
119 char            state[256];
120 unsigned long   testcalls = 0;          /* calls to function "test" */
121
122 unsigned long   simulatedopcount = 0;   /* -b flag */
123 int     closeprob = 0;                  /* -c flag */
124 int     debug = 0;                      /* -d flag */
125 unsigned long   debugstart = 0;         /* -D flag */
126 int     flush = 0;                      /* -f flag */
127 int     do_fsync = 0;                   /* -y flag */
128 unsigned long   maxfilelen = 256 * 1024;        /* -l flag */
129 int     sizechecks = 1;                 /* -n flag disables them */
130 int     maxoplen = 64 * 1024;           /* -o flag */
131 int     quiet = 0;                      /* -q flag */
132 unsigned long progressinterval = 0;     /* -p flag */
133 int     readbdy = 1;                    /* -r flag */
134 int     style = 0;                      /* -s flag */
135 int     prealloc = 0;                   /* -x flag */
136 int     truncbdy = 1;                   /* -t flag */
137 int     writebdy = 1;                   /* -w flag */
138 long    monitorstart = -1;              /* -m flag */
139 long    monitorend = -1;                /* -m flag */
140 int     lite = 0;                       /* -L flag */
141 long    numops = -1;                    /* -N flag */
142 int     randomoplen = 1;                /* -O flag disables it */
143 int     seed = 1;                       /* -S flag */
144 int     mapped_writes = 1;              /* -W flag disables */
145 int     fallocate_calls = 1;            /* -F flag disables */
146 int     keep_size_calls = 1;            /* -K flag disables */
147 int     punch_hole_calls = 1;           /* -H flag disables */
148 int     zero_range_calls = 1;           /* -z flag disables */
149 int     collapse_range_calls = 1;       /* -C flag disables */
150 int     insert_range_calls = 1;         /* -I flag disables */
151 int     mapped_reads = 1;               /* -R flag disables it */
152 int     fsxgoodfd = 0;
153 int     o_direct;                       /* -Z */
154 int     aio = 0;
155
156 int page_size;
157 int page_mask;
158 int mmap_mask;
159 #ifdef AIO
160 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
161 #define READ 0
162 #define WRITE 1
163 #define fsxread(a,b,c,d)        aio_rw(READ, a,b,c,d)
164 #define fsxwrite(a,b,c,d)       aio_rw(WRITE, a,b,c,d)
165 #else
166 #define fsxread(a,b,c,d)        read(a,b,c)
167 #define fsxwrite(a,b,c,d)       write(a,b,c)
168 #endif
169
170 FILE *  fsxlogf = NULL;
171 int badoff = -1;
172 int closeopen = 0;
173
174 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
175 {
176         unsigned long ret = (unsigned long)ptr;
177
178         ret = ((ret + align - 1) & ~(align - 1));
179         ret += offset;
180         return (void *)ret;
181 }
182
183 void
184 vwarnc(int code, const char *fmt, va_list ap) {
185   fprintf(stderr, "fsx: ");
186   if (fmt != NULL) {
187         vfprintf(stderr, fmt, ap);
188         fprintf(stderr, ": ");
189   }
190   fprintf(stderr, "%s\n", strerror(code));
191 }
192
193 void
194 warn(const char * fmt, ...)  {
195         va_list ap;
196         va_start(ap, fmt);
197         vwarnc(errno, fmt, ap);
198         va_end(ap);
199 }
200
201 #define BUF_SIZE 1024
202
203 void
204 prt(char *fmt, ...)
205 {
206         va_list args;
207         char buffer[BUF_SIZE];
208
209         va_start(args, fmt);
210         vsnprintf(buffer, BUF_SIZE, fmt, args);
211         va_end(args);
212         fprintf(stdout, buffer);
213         if (fsxlogf)
214                 fprintf(fsxlogf, buffer);
215 }
216
217 void
218 prterr(char *prefix)
219 {
220         prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
221 }
222
223
224 void
225 log4(int operation, int arg0, int arg1, int arg2)
226 {
227         struct log_entry *le;
228
229         le = &oplog[logptr];
230         le->operation = operation;
231         if (closeopen)
232                 le->operation = ~ le->operation;
233         le->args[0] = arg0;
234         le->args[1] = arg1;
235         le->args[2] = arg2;
236         logptr++;
237         logcount++;
238         if (logptr >= LOGSIZE)
239                 logptr = 0;
240 }
241
242
243 void
244 logdump(void)
245 {
246         int     i, count, down;
247         struct log_entry        *lp;
248         char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
249
250         prt("LOG DUMP (%d total operations):\n", logcount);
251         if (logcount < LOGSIZE) {
252                 i = 0;
253                 count = logcount;
254         } else {
255                 i = logptr;
256                 count = LOGSIZE;
257         }
258         for ( ; count > 0; count--) {
259                 int opnum;
260
261                 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
262                 prt("%d(%3d mod 256): ", opnum, opnum%256);
263                 lp = &oplog[i];
264                 if ((closeopen = lp->operation < 0))
265                         lp->operation = ~ lp->operation;
266                         
267                 switch (lp->operation) {
268                 case OP_MAPREAD:
269                         prt("MAPREAD  0x%x thru 0x%x\t(0x%x bytes)",
270                             lp->args[0], lp->args[0] + lp->args[1] - 1,
271                             lp->args[1]);
272                         if (badoff >= lp->args[0] && badoff <
273                                                      lp->args[0] + lp->args[1])
274                                 prt("\t***RRRR***");
275                         break;
276                 case OP_MAPWRITE:
277                         prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
278                             lp->args[0], lp->args[0] + lp->args[1] - 1,
279                             lp->args[1]);
280                         if (badoff >= lp->args[0] && badoff <
281                                                      lp->args[0] + lp->args[1])
282                                 prt("\t******WWWW");
283                         break;
284                 case OP_READ:
285                         prt("READ     0x%x thru 0x%x\t(0x%x bytes)",
286                             lp->args[0], lp->args[0] + lp->args[1] - 1,
287                             lp->args[1]);
288                         if (badoff >= lp->args[0] &&
289                             badoff < lp->args[0] + lp->args[1])
290                                 prt("\t***RRRR***");
291                         break;
292                 case OP_WRITE:
293                         prt("WRITE    0x%x thru 0x%x\t(0x%x bytes)",
294                             lp->args[0], lp->args[0] + lp->args[1] - 1,
295                             lp->args[1]);
296                         if (lp->args[0] > lp->args[2])
297                                 prt(" HOLE");
298                         else if (lp->args[0] + lp->args[1] > lp->args[2])
299                                 prt(" EXTEND");
300                         if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
301                             badoff < lp->args[0] + lp->args[1])
302                                 prt("\t***WWWW");
303                         break;
304                 case OP_TRUNCATE:
305                         down = lp->args[0] < lp->args[1];
306                         prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
307                             down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
308                         if (badoff >= lp->args[!down] &&
309                             badoff < lp->args[!!down])
310                                 prt("\t******WWWW");
311                         break;
312                 case OP_FALLOCATE:
313                         /* 0: offset 1: length 2: where alloced */
314                         prt("FALLOC   0x%x thru 0x%x\t(0x%x bytes) %s",
315                                 lp->args[0], lp->args[0] + lp->args[1],
316                                 lp->args[1], falloc_type[lp->args[2]]);
317                         if (badoff >= lp->args[0] &&
318                             badoff < lp->args[0] + lp->args[1])
319                                 prt("\t******FFFF");
320                         break;
321                 case OP_PUNCH_HOLE:
322                         prt("PUNCH    0x%x thru 0x%x\t(0x%x bytes)",
323                             lp->args[0], lp->args[0] + lp->args[1] - 1,
324                             lp->args[1]);
325                         if (badoff >= lp->args[0] && badoff <
326                                                      lp->args[0] + lp->args[1])
327                                 prt("\t******PPPP");
328                         break;
329                 case OP_ZERO_RANGE:
330                         prt("ZERO     0x%x thru 0x%x\t(0x%x bytes)",
331                             lp->args[0], lp->args[0] + lp->args[1] - 1,
332                             lp->args[1]);
333                         if (badoff >= lp->args[0] && badoff <
334                                                      lp->args[0] + lp->args[1])
335                                 prt("\t******ZZZZ");
336                         break;
337                 case OP_COLLAPSE_RANGE:
338                         prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
339                             lp->args[0], lp->args[0] + lp->args[1] - 1,
340                             lp->args[1]);
341                         if (badoff >= lp->args[0] && badoff <
342                                                      lp->args[0] + lp->args[1])
343                                 prt("\t******CCCC");
344                         break;
345                 case OP_INSERT_RANGE:
346                         prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
347                             lp->args[0], lp->args[0] + lp->args[1] - 1,
348                             lp->args[1]);
349                         if (badoff >= lp->args[0] && badoff <
350                                                      lp->args[0] + lp->args[1])
351                                 prt("\t******IIII");
352                         break;
353                 case OP_SKIPPED:
354                         prt("SKIPPED (no operation)");
355                         break;
356                 default:
357                         prt("BOGUS LOG ENTRY (operation code = %d)!",
358                             lp->operation);
359                 }
360                 if (closeopen)
361                         prt("\n\t\tCLOSE/OPEN");
362                 prt("\n");
363                 i++;
364                 if (i == LOGSIZE)
365                         i = 0;
366         }
367 }
368
369
370 void
371 save_buffer(char *buffer, off_t bufferlength, int fd)
372 {
373         off_t ret;
374         ssize_t byteswritten;
375
376         if (fd <= 0 || bufferlength == 0)
377                 return;
378
379         if (bufferlength > SSIZE_MAX) {
380                 prt("fsx flaw: overflow in save_buffer\n");
381                 exit(67);
382         }
383         if (lite) {
384                 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
385                 if (size_by_seek == (off_t)-1)
386                         prterr("save_buffer: lseek eof");
387                 else if (bufferlength > size_by_seek) {
388                         warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
389                              (unsigned long long)bufferlength);
390                         bufferlength = size_by_seek;
391                 }
392         }
393
394         ret = lseek(fd, (off_t)0, SEEK_SET);
395         if (ret == (off_t)-1)
396                 prterr("save_buffer: lseek 0");
397         
398         byteswritten = write(fd, buffer, (size_t)bufferlength);
399         if (byteswritten != bufferlength) {
400                 if (byteswritten == -1)
401                         prterr("save_buffer write");
402                 else
403                         warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
404                              (unsigned)byteswritten,
405                              (unsigned long long)bufferlength);
406         }
407 }
408
409
410 void
411 report_failure(int status)
412 {
413         logdump();
414         
415         if (fsxgoodfd) {
416                 if (good_buf) {
417                         save_buffer(good_buf, file_size, fsxgoodfd);
418                         prt("Correct content saved for comparison\n");
419                         prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
420                             fname, fname);
421                 }
422                 close(fsxgoodfd);
423         }
424         exit(status);
425 }
426
427
428 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
429                                         *(((unsigned char *)(cp)) + 1)))
430
431 void
432 check_buffers(unsigned offset, unsigned size)
433 {
434         unsigned char c, t;
435         unsigned i = 0;
436         unsigned n = 0;
437         unsigned op = 0;
438         unsigned bad = 0;
439
440         if (memcmp(good_buf + offset, temp_buf, size) != 0) {
441                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
442                     offset, size, fname);
443                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
444                 while (size > 0) {
445                         c = good_buf[offset];
446                         t = temp_buf[i];
447                         if (c != t) {
448                                 if (n < 16) {
449                                         bad = short_at(&temp_buf[i]);
450                                         prt("0x%05x\t0x%04x\t0x%04x", offset,
451                                             short_at(&good_buf[offset]), bad);
452                                         op = temp_buf[offset & 1 ? i+1 : i];
453                                         prt("\t0x%05x\n", n);
454                                         if (op)
455                                                 prt("operation# (mod 256) for "
456                                                   "the bad data may be %u\n",
457                                                 ((unsigned)op & 0xff));
458                                         else
459                                                 prt("operation# (mod 256) for "
460                                                   "the bad data unknown, check"
461                                                   " HOLE and EXTEND ops\n");
462                                 }
463                                 n++;
464                                 badoff = offset;
465                         }
466                         offset++;
467                         i++;
468                         size--;
469                 }
470                 report_failure(110);
471         }
472 }
473
474
475 void
476 check_size(void)
477 {
478         struct stat     statbuf;
479         off_t   size_by_seek;
480
481         if (fstat(fd, &statbuf)) {
482                 prterr("check_size: fstat");
483                 statbuf.st_size = -1;
484         }
485         size_by_seek = lseek(fd, (off_t)0, SEEK_END);
486         if (file_size != statbuf.st_size || file_size != size_by_seek) {
487                 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
488                     (unsigned long long)file_size,
489                     (unsigned long long)statbuf.st_size,
490                     (unsigned long long)size_by_seek);
491                 report_failure(120);
492         }
493 }
494
495
496 void
497 check_trunc_hack(void)
498 {
499         struct stat statbuf;
500
501         ftruncate(fd, (off_t)0);
502         ftruncate(fd, (off_t)100000);
503         fstat(fd, &statbuf);
504         if (statbuf.st_size != (off_t)100000) {
505                 prt("no extend on truncate! not posix!\n");
506                 exit(130);
507         }
508         ftruncate(fd, 0);
509 }
510
511 void
512 doflush(unsigned offset, unsigned size)
513 {
514         unsigned pg_offset;
515         unsigned map_size;
516         char    *p;
517
518         if (o_direct == O_DIRECT)
519                 return;
520
521         pg_offset = offset & mmap_mask;
522         map_size  = pg_offset + size;
523
524         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
525                               MAP_FILE | MAP_SHARED, fd,
526                               (off_t)(offset - pg_offset))) == (char *)-1) {
527                 prterr("doflush: mmap");
528                 report_failure(202);
529         }
530         if (msync(p, map_size, MS_INVALIDATE) != 0) {
531                 prterr("doflush: msync");
532                 report_failure(203);
533         }
534         if (munmap(p, map_size) != 0) {
535                 prterr("doflush: munmap");
536                 report_failure(204);
537         }
538 }
539
540 void
541 doread(unsigned offset, unsigned size)
542 {
543         off_t ret;
544         unsigned iret;
545
546         offset -= offset % readbdy;
547         if (o_direct)
548                 size -= size % readbdy;
549         if (size == 0) {
550                 if (!quiet && testcalls > simulatedopcount && !o_direct)
551                         prt("skipping zero size read\n");
552                 log4(OP_SKIPPED, OP_READ, offset, size);
553                 return;
554         }
555         if (size + offset > file_size) {
556                 if (!quiet && testcalls > simulatedopcount)
557                         prt("skipping seek/read past end of file\n");
558                 log4(OP_SKIPPED, OP_READ, offset, size);
559                 return;
560         }
561
562         log4(OP_READ, offset, size, 0);
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 read\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("doread: lseek");
578                 report_failure(140);
579         }
580         iret = fsxread(fd, temp_buf, size, offset);
581         if (iret != size) {
582                 if (iret == -1)
583                         prterr("doread: read");
584                 else
585                         prt("short read: 0x%x bytes instead of 0x%x\n",
586                             iret, size);
587                 report_failure(141);
588         }
589         check_buffers(offset, size);
590 }
591
592
593 void
594 check_eofpage(char *s, unsigned offset, char *p, int size)
595 {
596         unsigned long last_page, should_be_zero;
597
598         if (offset + size <= (file_size & ~page_mask))
599                 return;
600         /*
601          * we landed in the last page of the file
602          * test to make sure the VM system provided 0's 
603          * beyond the true end of the file mapping
604          * (as required by mmap def in 1996 posix 1003.1)
605          */
606         last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
607
608         for (should_be_zero = last_page + (file_size & page_mask);
609              should_be_zero < last_page + page_size;
610              should_be_zero++)
611                 if (*(char *)should_be_zero) {
612                         prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
613                             s, file_size - 1, should_be_zero & page_mask,
614                             short_at(should_be_zero));
615                         report_failure(205);
616                 }
617 }
618
619
620 void
621 domapread(unsigned offset, unsigned size)
622 {
623         unsigned pg_offset;
624         unsigned map_size;
625         char    *p;
626
627         offset -= offset % readbdy;
628         if (size == 0) {
629                 if (!quiet && testcalls > simulatedopcount)
630                         prt("skipping zero size read\n");
631                 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
632                 return;
633         }
634         if (size + offset > file_size) {
635                 if (!quiet && testcalls > simulatedopcount)
636                         prt("skipping seek/read past end of file\n");
637                 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
638                 return;
639         }
640
641         log4(OP_MAPREAD, offset, size, 0);
642
643         if (testcalls <= simulatedopcount)
644                 return;
645
646         if (!quiet &&
647                 ((progressinterval && testcalls % progressinterval == 0) ||
648                        (debug &&
649                        (monitorstart == -1 ||
650                         (offset + size > monitorstart &&
651                         (monitorend == -1 || offset <= monitorend))))))
652                 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
653                     offset, offset + size - 1, size);
654
655         pg_offset = offset & PAGE_MASK;
656         map_size  = pg_offset + size;
657
658         if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
659                               (off_t)(offset - pg_offset))) == (char *)-1) {
660                 prterr("domapread: mmap");
661                 report_failure(190);
662         }
663         memcpy(temp_buf, p + pg_offset, size);
664
665         check_eofpage("Read", offset, p, size);
666
667         if (munmap(p, map_size) != 0) {
668                 prterr("domapread: munmap");
669                 report_failure(191);
670         }
671
672         check_buffers(offset, size);
673 }
674
675
676 void
677 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
678 {
679         while (size--) {
680                 good_buf[offset] = testcalls % 256; 
681                 if (offset % 2)
682                         good_buf[offset] += original_buf[offset];
683                 offset++;
684         }
685 }
686
687
688 void
689 dowrite(unsigned offset, unsigned size)
690 {
691         off_t ret;
692         unsigned iret;
693
694         offset -= offset % writebdy;
695         if (o_direct)
696                 size -= size % writebdy;
697         if (size == 0) {
698                 if (!quiet && testcalls > simulatedopcount && !o_direct)
699                         prt("skipping zero size write\n");
700                 log4(OP_SKIPPED, OP_WRITE, offset, size);
701                 return;
702         }
703
704         log4(OP_WRITE, offset, size, file_size);
705
706         gendata(original_buf, good_buf, offset, size);
707         if (file_size < offset + size) {
708                 if (file_size < offset)
709                         memset(good_buf + file_size, '\0', offset - file_size);
710                 file_size = offset + size;
711                 if (lite) {
712                         warn("Lite file size bug in fsx!");
713                         report_failure(149);
714                 }
715         }
716
717         if (testcalls <= simulatedopcount)
718                 return;
719
720         if (!quiet &&
721                 ((progressinterval && testcalls % progressinterval == 0) ||
722                        (debug &&
723                        (monitorstart == -1 ||
724                         (offset + size > monitorstart &&
725                         (monitorend == -1 || offset <= monitorend))))))
726                 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
727                     offset, offset + size - 1, size);
728         ret = lseek(fd, (off_t)offset, SEEK_SET);
729         if (ret == (off_t)-1) {
730                 prterr("dowrite: lseek");
731                 report_failure(150);
732         }
733         iret = fsxwrite(fd, good_buf + offset, size, offset);
734         if (iret != size) {
735                 if (iret == -1)
736                         prterr("dowrite: write");
737                 else
738                         prt("short write: 0x%x bytes instead of 0x%x\n",
739                             iret, size);
740                 report_failure(151);
741         }
742         if (do_fsync) {
743                 if (fsync(fd)) {
744                         prt("fsync() failed: %s\n", strerror(errno));
745                         report_failure(152);
746                 }
747         }
748         if (flush) {
749                 doflush(offset, size);
750         }
751 }
752
753
754 void
755 domapwrite(unsigned offset, unsigned size)
756 {
757         unsigned pg_offset;
758         unsigned map_size;
759         off_t    cur_filesize;
760         char    *p;
761
762         offset -= offset % writebdy;
763         if (size == 0) {
764                 if (!quiet && testcalls > simulatedopcount)
765                         prt("skipping zero size write\n");
766                 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
767                 return;
768         }
769         cur_filesize = file_size;
770
771         log4(OP_MAPWRITE, offset, size, 0);
772
773         gendata(original_buf, good_buf, offset, size);
774         if (file_size < offset + size) {
775                 if (file_size < offset)
776                         memset(good_buf + file_size, '\0', offset - file_size);
777                 file_size = offset + size;
778                 if (lite) {
779                         warn("Lite file size bug in fsx!");
780                         report_failure(200);
781                 }
782         }
783
784         if (testcalls <= simulatedopcount)
785                 return;
786
787         if (!quiet &&
788                 ((progressinterval && testcalls % progressinterval == 0) ||
789                        (debug &&
790                        (monitorstart == -1 ||
791                         (offset + size > monitorstart &&
792                         (monitorend == -1 || offset <= monitorend))))))
793                 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
794                     offset, offset + size - 1, size);
795
796         if (file_size > cur_filesize) {
797                 if (ftruncate(fd, file_size) == -1) {
798                         prterr("domapwrite: ftruncate");
799                         exit(201);
800                 }
801         }
802         pg_offset = offset & PAGE_MASK;
803         map_size  = pg_offset + size;
804
805         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
806                               MAP_FILE | MAP_SHARED, fd,
807                               (off_t)(offset - pg_offset))) == (char *)-1) {
808                 prterr("domapwrite: mmap");
809                 report_failure(202);
810         }
811         memcpy(p + pg_offset, good_buf + offset, size);
812         if (msync(p, map_size, MS_SYNC) != 0) {
813                 prterr("domapwrite: msync");
814                 report_failure(203);
815         }
816
817         check_eofpage("Write", offset, p, size);
818
819         if (munmap(p, map_size) != 0) {
820                 prterr("domapwrite: munmap");
821                 report_failure(204);
822         }
823 }
824
825
826 void
827 dotruncate(unsigned size)
828 {
829         int oldsize = file_size;
830
831         size -= size % truncbdy;
832         if (size > biggest) {
833                 biggest = size;
834                 if (!quiet && testcalls > simulatedopcount)
835                         prt("truncating to largest ever: 0x%x\n", size);
836         }
837
838         log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
839
840         if (size > file_size)
841                 memset(good_buf + file_size, '\0', size - file_size);
842         file_size = size;
843
844         if (testcalls <= simulatedopcount)
845                 return;
846         
847         if ((progressinterval && testcalls % progressinterval == 0) ||
848             (debug && (monitorstart == -1 || monitorend == -1 ||
849                       size <= monitorend)))
850                 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
851         if (ftruncate(fd, (off_t)size) == -1) {
852                 prt("ftruncate1: %x\n", size);
853                 prterr("dotruncate: ftruncate");
854                 report_failure(160);
855         }
856 }
857
858 #ifdef FALLOC_FL_PUNCH_HOLE
859 void
860 do_punch_hole(unsigned offset, unsigned length)
861 {
862         unsigned end_offset;
863         int max_offset = 0;
864         int max_len = 0;
865         int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
866
867         if (length == 0) {
868                 if (!quiet && testcalls > simulatedopcount)
869                         prt("skipping zero length punch hole\n");
870                         log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
871                 return;
872         }
873
874         if (file_size <= (loff_t)offset) {
875                 if (!quiet && testcalls > simulatedopcount)
876                         prt("skipping hole punch off the end of the file\n");
877                         log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
878                 return;
879         }
880
881         end_offset = offset + length;
882
883         log4(OP_PUNCH_HOLE, offset, length, 0);
884
885         if (testcalls <= simulatedopcount)
886                 return;
887
888         if ((progressinterval && testcalls % progressinterval == 0) ||
889             (debug && (monitorstart == -1 || monitorend == -1 ||
890                       end_offset <= monitorend))) {
891                 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
892                         offset, offset+length, length);
893         }
894         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
895                 prt("%punch hole: %x to %x\n", offset, length);
896                 prterr("do_punch_hole: fallocate");
897                 report_failure(161);
898         }
899
900
901         max_offset = offset < file_size ? offset : file_size;
902         max_len = max_offset + length <= file_size ? length :
903                         file_size - max_offset;
904         memset(good_buf + max_offset, '\0', max_len);
905 }
906
907 #else
908 void
909 do_punch_hole(unsigned offset, unsigned length)
910 {
911         return;
912 }
913 #endif
914
915 #ifdef FALLOC_FL_ZERO_RANGE
916 void
917 do_zero_range(unsigned offset, unsigned length, int keep_size)
918 {
919         unsigned end_offset;
920         int mode = FALLOC_FL_ZERO_RANGE;
921
922         if (length == 0) {
923                 if (!quiet && testcalls > simulatedopcount)
924                         prt("skipping zero length zero range\n");
925                         log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
926                 return;
927         }
928
929         end_offset = keep_size ? 0 : offset + length;
930
931         if (end_offset > biggest) {
932                 biggest = end_offset;
933                 if (!quiet && testcalls > simulatedopcount)
934                         prt("zero_range to largest ever: 0x%x\n", end_offset);
935         }
936
937         /*
938          * last arg matches fallocate string array index in logdump:
939          *      0: allocate past EOF
940          *      1: extending prealloc
941          *      2: interior prealloc
942          */
943         log4(OP_ZERO_RANGE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
944
945         if (testcalls <= simulatedopcount)
946                 return;
947
948         if ((progressinterval && testcalls % progressinterval == 0) ||
949             (debug && (monitorstart == -1 || monitorend == -1 ||
950                       end_offset <= monitorend))) {
951                 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
952                         offset, offset+length, length);
953         }
954         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
955                 prt("%pzero range: %x to %x\n", offset, length);
956                 prterr("do_zero_range: fallocate");
957                 report_failure(161);
958         }
959
960         memset(good_buf + offset, '\0', length);
961 }
962
963 #else
964 void
965 do_zero_range(unsigned offset, unsigned length, int keep_size)
966 {
967         return;
968 }
969 #endif
970
971 #ifdef FALLOC_FL_COLLAPSE_RANGE
972 void
973 do_collapse_range(unsigned offset, unsigned length)
974 {
975         unsigned end_offset;
976         int mode = FALLOC_FL_COLLAPSE_RANGE;
977
978         if (length == 0) {
979                 if (!quiet && testcalls > simulatedopcount)
980                         prt("skipping zero length collapse range\n");
981                 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
982                 return;
983         }
984
985         end_offset = offset + length;
986         if ((loff_t)end_offset >= file_size) {
987                 if (!quiet && testcalls > simulatedopcount)
988                         prt("skipping collapse range behind EOF\n");
989                 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
990                 return;
991         }
992
993         log4(OP_COLLAPSE_RANGE, offset, length, 0);
994
995         if (testcalls <= simulatedopcount)
996                 return;
997
998         if ((progressinterval && testcalls % progressinterval == 0) ||
999             (debug && (monitorstart == -1 || monitorend == -1 ||
1000                       end_offset <= monitorend))) {
1001                 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1002                         offset, offset+length, length);
1003         }
1004         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1005                 prt("collapse range: %x to %x\n", offset, length);
1006                 prterr("do_collapse_range: fallocate");
1007                 report_failure(161);
1008         }
1009
1010         memmove(good_buf + offset, good_buf + end_offset,
1011                 file_size - end_offset);
1012         file_size -= length;
1013 }
1014
1015 #else
1016 void
1017 do_collapse_range(unsigned offset, unsigned length)
1018 {
1019         return;
1020 }
1021 #endif
1022
1023 #ifdef FALLOC_FL_INSERT_RANGE
1024 void
1025 do_insert_range(unsigned offset, unsigned length)
1026 {
1027         unsigned end_offset;
1028         int mode = FALLOC_FL_INSERT_RANGE;
1029
1030         if (length == 0) {
1031                 if (!quiet && testcalls > simulatedopcount)
1032                         prt("skipping zero length insert range\n");
1033                 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, length);
1034                 return;
1035         }
1036
1037         if ((loff_t)offset >= file_size) {
1038                 if (!quiet && testcalls > simulatedopcount)
1039                         prt("skipping insert range behind EOF\n");
1040                 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, length);
1041                 return;
1042         }
1043
1044         log4(OP_INSERT_RANGE, offset, length, 0);
1045
1046         if (testcalls <= simulatedopcount)
1047                 return;
1048
1049         end_offset = offset + length;
1050         if ((progressinterval && testcalls % progressinterval == 0) ||
1051             (debug && (monitorstart == -1 || monitorend == -1 ||
1052                       end_offset <= monitorend))) {
1053                 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1054                         offset, offset+length, length);
1055         }
1056         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1057                 prt("insert range: %x to %x\n", offset, length);
1058                 prterr("do_insert_range: fallocate");
1059                 report_failure(161);
1060         }
1061
1062         memmove(good_buf + end_offset, good_buf + offset,
1063                 file_size - offset);
1064         memset(good_buf + offset, '\0', length);
1065         file_size += length;
1066 }
1067
1068 #else
1069 void
1070 do_insert_range(unsigned offset, unsigned length)
1071 {
1072         return;
1073 }
1074 #endif
1075
1076 #ifdef HAVE_LINUX_FALLOC_H
1077 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1078 void
1079 do_preallocate(unsigned offset, unsigned length, int keep_size)
1080 {
1081         unsigned end_offset;
1082
1083         if (length == 0) {
1084                 if (!quiet && testcalls > simulatedopcount)
1085                         prt("skipping zero length fallocate\n");
1086                 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1087                 return;
1088         }
1089
1090         end_offset = keep_size ? 0 : offset + length;
1091
1092         if (end_offset > biggest) {
1093                 biggest = end_offset;
1094                 if (!quiet && testcalls > simulatedopcount)
1095                         prt("fallocating to largest ever: 0x%x\n", end_offset);
1096         }
1097
1098         /*
1099          * last arg matches fallocate string array index in logdump:
1100          *      0: allocate past EOF
1101          *      1: extending prealloc
1102          *      2: interior prealloc
1103          */
1104         log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1105
1106         if (end_offset > file_size) {
1107                 memset(good_buf + file_size, '\0', end_offset - file_size);
1108                 file_size = end_offset;
1109         }
1110
1111         if (testcalls <= simulatedopcount)
1112                 return;
1113         
1114         if ((progressinterval && testcalls % progressinterval == 0) ||
1115             (debug && (monitorstart == -1 || monitorend == -1 ||
1116                       end_offset <= monitorend)))
1117                 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1118                                 offset, offset + length, length);
1119         if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1120                 prt("fallocate: %x to %x\n", offset, length);
1121                 prterr("do_preallocate: fallocate");
1122                 report_failure(161);
1123         }
1124 }
1125 #else
1126 void
1127 do_preallocate(unsigned offset, unsigned length, int keep_size)
1128 {
1129         return;
1130 }
1131 #endif
1132
1133 void
1134 writefileimage()
1135 {
1136         ssize_t iret;
1137
1138         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1139                 prterr("writefileimage: lseek");
1140                 report_failure(171);
1141         }
1142         iret = write(fd, good_buf, file_size);
1143         if ((off_t)iret != file_size) {
1144                 if (iret == -1)
1145                         prterr("writefileimage: write");
1146                 else
1147                         prt("short write: 0x%x bytes instead of 0x%llx\n",
1148                             iret, (unsigned long long)file_size);
1149                 report_failure(172);
1150         }
1151         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1152                 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1153                 prterr("writefileimage: ftruncate");
1154                 report_failure(173);
1155         }
1156 }
1157
1158
1159 void
1160 docloseopen(void)
1161
1162         if (testcalls <= simulatedopcount)
1163                 return;
1164
1165         if (debug)
1166                 prt("%lu close/open\n", testcalls);
1167         if (close(fd)) {
1168                 prterr("docloseopen: close");
1169                 report_failure(180);
1170         }
1171         fd = open(fname, O_RDWR|o_direct, 0);
1172         if (fd < 0) {
1173                 prterr("docloseopen: open");
1174                 report_failure(181);
1175         }
1176 }
1177
1178
1179 #define TRIM_OFF(off, size)                     \
1180 do {                                            \
1181         if (size)                               \
1182                 (off) %= (size);                \
1183         else                                    \
1184                 (off) = 0;                      \
1185 } while (0)
1186
1187 #define TRIM_LEN(off, len, size)                \
1188 do {                                            \
1189         if ((off) + (len) > (size))             \
1190                 (len) = (size) - (off);         \
1191 } while (0)
1192
1193 #define TRIM_OFF_LEN(off, len, size)            \
1194 do {                                            \
1195         TRIM_OFF(off, size);                    \
1196         TRIM_LEN(off, len, size);               \
1197 } while (0)
1198
1199 void
1200 test(void)
1201 {
1202         unsigned long   offset;
1203         unsigned long   size = maxoplen;
1204         unsigned long   rv = random();
1205         unsigned long   op;
1206         int             keep_size = 0;
1207
1208         if (simulatedopcount > 0 && testcalls == simulatedopcount)
1209                 writefileimage();
1210
1211         testcalls++;
1212
1213         if (closeprob)
1214                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1215
1216         if (debugstart > 0 && testcalls >= debugstart)
1217                 debug = 1;
1218
1219         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1220                 prt("%lu...\n", testcalls);
1221
1222         offset = random();
1223         if (randomoplen)
1224                 size = random() % (maxoplen + 1);
1225
1226         /* calculate appropriate op to run */
1227         if (lite)
1228                 op = rv % OP_MAX_LITE;
1229         else
1230                 op = rv % OP_MAX_FULL;
1231
1232         switch(op) {
1233         case OP_FALLOCATE:
1234                 if (fallocate_calls && size && keep_size_calls)
1235                         keep_size = random() % 2;
1236                 break;
1237         case OP_ZERO_RANGE:
1238                 if (zero_range_calls && size && keep_size_calls)
1239                         keep_size = random() % 2;
1240                 break;
1241         }
1242
1243         switch (op) {
1244         case OP_MAPREAD:
1245                 if (!mapped_reads)
1246                         op = OP_READ;
1247                 break;
1248         case OP_MAPWRITE:
1249                 if (!mapped_writes)
1250                         op = OP_WRITE;
1251                 break;
1252         case OP_FALLOCATE:
1253                 if (!fallocate_calls) {
1254                         log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1255                         goto out;
1256                 }
1257                 break;
1258         case OP_PUNCH_HOLE:
1259                 if (!punch_hole_calls) {
1260                         log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1261                         goto out;
1262                 }
1263                 break;
1264         case OP_ZERO_RANGE:
1265                 if (!zero_range_calls) {
1266                         log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1267                         goto out;
1268                 }
1269                 break;
1270         case OP_COLLAPSE_RANGE:
1271                 if (!collapse_range_calls) {
1272                         log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1273                         goto out;
1274                 }
1275                 break;
1276         case OP_INSERT_RANGE:
1277                 if (!insert_range_calls) {
1278                         log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1279                         goto out;
1280                 }
1281                 break;
1282         }
1283
1284         switch (op) {
1285         case OP_READ:
1286                 TRIM_OFF_LEN(offset, size, file_size);
1287                 doread(offset, size);
1288                 break;
1289
1290         case OP_WRITE:
1291                 TRIM_OFF_LEN(offset, size, maxfilelen);
1292                 dowrite(offset, size);
1293                 break;
1294
1295         case OP_MAPREAD:
1296                 TRIM_OFF_LEN(offset, size, file_size);
1297                 domapread(offset, size);
1298                 break;
1299
1300         case OP_MAPWRITE:
1301                 TRIM_OFF_LEN(offset, size, maxfilelen);
1302                 domapwrite(offset, size);
1303                 break;
1304
1305         case OP_TRUNCATE:
1306                 if (!style)
1307                         size = random() % maxfilelen;
1308                 dotruncate(size);
1309                 break;
1310
1311         case OP_FALLOCATE:
1312                 TRIM_OFF_LEN(offset, size, maxfilelen);
1313                 do_preallocate(offset, size, keep_size);
1314                 break;
1315
1316         case OP_PUNCH_HOLE:
1317                 TRIM_OFF_LEN(offset, size, file_size);
1318                 do_punch_hole(offset, size);
1319                 break;
1320         case OP_ZERO_RANGE:
1321                 TRIM_OFF_LEN(offset, size, file_size);
1322                 do_zero_range(offset, size, keep_size);
1323                 break;
1324         case OP_COLLAPSE_RANGE:
1325                 TRIM_OFF_LEN(offset, size, file_size - 1);
1326                 offset = offset & ~(block_size - 1);
1327                 size = size & ~(block_size - 1);
1328                 if (size == 0) {
1329                         log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1330                         goto out;
1331                 }
1332                 do_collapse_range(offset, size);
1333                 break;
1334         case OP_INSERT_RANGE:
1335                 TRIM_OFF(offset, file_size);
1336                 TRIM_LEN(file_size, size, maxfilelen);
1337                 offset = offset & ~(block_size - 1);
1338                 size = size & ~(block_size - 1);
1339                 if (size == 0) {
1340                         log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1341                         goto out;
1342                 }
1343                 if (file_size + size > maxfilelen) {
1344                         log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1345                         goto out;
1346                 }
1347
1348                 do_insert_range(offset, size);
1349                 break;
1350         default:
1351                 prterr("test: unknown operation");
1352                 report_failure(42);
1353                 break;
1354         }
1355
1356 out:
1357         if (sizechecks && testcalls > simulatedopcount)
1358                 check_size();
1359         if (closeopen)
1360                 docloseopen();
1361 }
1362
1363
1364 void
1365 cleanup(sig)
1366         int     sig;
1367 {
1368         if (sig)
1369                 prt("signal %d\n", sig);
1370         prt("testcalls = %lu\n", testcalls);
1371         exit(sig);
1372 }
1373
1374
1375 void
1376 usage(void)
1377 {
1378         fprintf(stdout, "usage: %s",
1379                 "fsx [-dnqxAFLOWZ] [-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\
1380         -b opnum: beginning operation number (default 1)\n\
1381         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1382         -d: debug output for all operations\n\
1383         -f flush and invalidate cache after I/O\n\
1384         -l flen: the upper bound on file size (default 262144)\n\
1385         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1386         -n: no verifications of file size\n\
1387         -o oplen: the upper bound on operation size (default 65536)\n\
1388         -p progressinterval: debug output at specified operation interval\n\
1389         -q: quieter operation\n\
1390         -r readbdy: 4096 would make reads page aligned (default 1)\n\
1391         -s style: 1 gives smaller truncates (default 0)\n\
1392         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1393         -w writebdy: 4096 would make writes page aligned (default 1)\n\
1394         -x: preallocate file space before starting, XFS only (default 0)\n\
1395         -y synchronize changes to a file\n"
1396
1397 #ifdef AIO
1398 "       -A: Use the AIO system calls\n"
1399 #endif
1400 "       -D startingop: debug output starting at specified operation\n"
1401 #ifdef HAVE_LINUX_FALLOC_H
1402 "       -F: Do not use fallocate (preallocation) calls\n"
1403 #endif
1404 #ifdef FALLOC_FL_PUNCH_HOLE
1405 "       -H: Do not use punch hole calls\n"
1406 #endif
1407 #ifdef FALLOC_FL_ZERO_RANGE
1408 "       -z: Do not use zero range calls\n"
1409 #endif
1410 #ifdef FALLOC_FL_COLLAPSE_RANGE
1411 "       -C: Do not use collapse range calls\n"
1412 #endif
1413 #ifdef FALLOC_FL_INSERT_RANGE
1414 "       -I: Do not use insert range calls\n"
1415 #endif
1416 "       -L: fsxLite - no file creations & no file size changes\n\
1417         -N numops: total # operations to do (default infinity)\n\
1418         -O: use oplen (see -o flag) for every op (default random)\n\
1419         -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1420         -S seed: for random # generator (default 1) 0 gets timestamp\n\
1421         -W: mapped write operations DISabled\n\
1422         -R: read() system calls only (mapped reads disabled)\n\
1423         -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1424         fname: this filename is REQUIRED (no default)\n");
1425         exit(90);
1426 }
1427
1428
1429 int
1430 getnum(char *s, char **e)
1431 {
1432         int ret;
1433
1434         *e = (char *) 0;
1435         ret = strtol(s, e, 0);
1436         if (*e)
1437                 switch (**e) {
1438                 case 'b':
1439                 case 'B':
1440                         ret *= 512;
1441                         *e = *e + 1;
1442                         break;
1443                 case 'k':
1444                 case 'K':
1445                         ret *= 1024;
1446                         *e = *e + 1;
1447                         break;
1448                 case 'm':
1449                 case 'M':
1450                         ret *= 1024*1024;
1451                         *e = *e + 1;
1452                         break;
1453                 case 'w':
1454                 case 'W':
1455                         ret *= 4;
1456                         *e = *e + 1;
1457                         break;
1458                 }
1459         return (ret);
1460 }
1461
1462 #ifdef AIO
1463
1464 #define QSZ     1024
1465 io_context_t    io_ctx;
1466 struct iocb     iocb;
1467
1468 int aio_setup()
1469 {
1470         int ret;
1471         ret = io_queue_init(QSZ, &io_ctx);
1472         if (ret != 0) {
1473                 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1474                         strerror(ret));
1475                 return(-1);
1476         }
1477         return(0);
1478 }
1479
1480 int
1481 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1482 {
1483         struct io_event event;
1484         static struct timespec ts;
1485         struct iocb *iocbs[] = { &iocb };
1486         int ret;
1487         long res;
1488
1489         if (rw == READ) {
1490                 io_prep_pread(&iocb, fd, buf, len, offset);
1491         } else {
1492                 io_prep_pwrite(&iocb, fd, buf, len, offset);
1493         }
1494
1495         ts.tv_sec = 30;
1496         ts.tv_nsec = 0;
1497         ret = io_submit(io_ctx, 1, iocbs);
1498         if (ret != 1) {
1499                 fprintf(stderr, "errcode=%d\n", ret);
1500                 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1501                                 strerror(ret));
1502                 goto out_error;
1503         }
1504
1505         ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1506         if (ret != 1) {
1507                 if (ret == 0)
1508                         fprintf(stderr, "aio_rw: no events available\n");
1509                 else {
1510                         fprintf(stderr, "errcode=%d\n", -ret);
1511                         fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1512                                         strerror(-ret));
1513                 }
1514                 goto out_error;
1515         }
1516         if (len != event.res) {
1517                 /*
1518                  * The b0rked libaio defines event.res as unsigned.
1519                  * However the kernel strucuture has it signed,
1520                  * and it's used to pass negated error value.
1521                  * Till the library is fixed use the temp var.
1522                  */
1523                 res = (long)event.res;
1524                 if (res >= 0)
1525                         fprintf(stderr, "bad io length: %lu instead of %u\n",
1526                                         res, len);
1527                 else {
1528                         fprintf(stderr, "errcode=%ld\n", -res);
1529                         fprintf(stderr, "aio_rw: async io failed: %s\n",
1530                                         strerror(-res));
1531                         ret = res;
1532                         goto out_error;
1533                 }
1534
1535         }
1536         return event.res;
1537
1538 out_error:
1539         /*
1540          * The caller expects error return in traditional libc
1541          * convention, i.e. -1 and the errno set to error.
1542          */
1543         errno = -ret;
1544         return -1;
1545 }
1546
1547 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1548 {
1549         int ret;
1550
1551         if (aio) {
1552                 ret = __aio_rw(rw, fd, buf, len, offset);
1553         } else {
1554                 if (rw == READ)
1555                         ret = read(fd, buf, len);
1556                 else
1557                         ret = write(fd, buf, len);
1558         }
1559         return ret;
1560 }
1561
1562 #endif
1563
1564 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1565
1566 int
1567 __test_fallocate(int mode, const char *mode_str)
1568 {
1569 #ifdef HAVE_LINUX_FALLOC_H
1570         int ret = 0;
1571         if (!lite) {
1572                 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1573                         if(!quiet)
1574                                 fprintf(stderr,
1575                                         "main: filesystem does not support "
1576                                         "fallocate mode %s, disabling!\n",
1577                                         mode_str);
1578                 } else {
1579                         ret = 1;
1580                         ftruncate(fd, 0);
1581                 }
1582         }
1583         return ret;
1584 #endif
1585 }
1586
1587 int
1588 main(int argc, char **argv)
1589 {
1590         int     i, style, ch;
1591         char    *endp;
1592         char goodfile[1024];
1593         char logfile[1024];
1594         struct stat statbuf;
1595
1596         goodfile[0] = 0;
1597         logfile[0] = 0;
1598
1599         page_size = getpagesize();
1600         page_mask = page_size - 1;
1601         mmap_mask = page_mask;
1602         
1603
1604         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1605
1606         while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ"))
1607                != EOF)
1608                 switch (ch) {
1609                 case 'b':
1610                         simulatedopcount = getnum(optarg, &endp);
1611                         if (!quiet)
1612                                 fprintf(stdout, "Will begin at operation %ld\n",
1613                                         simulatedopcount);
1614                         if (simulatedopcount == 0)
1615                                 usage();
1616                         simulatedopcount -= 1;
1617                         break;
1618                 case 'c':
1619                         closeprob = getnum(optarg, &endp);
1620                         if (!quiet)
1621                                 fprintf(stdout,
1622                                         "Chance of close/open is 1 in %d\n",
1623                                         closeprob);
1624                         if (closeprob <= 0)
1625                                 usage();
1626                         break;
1627                 case 'd':
1628                         debug = 1;
1629                         break;
1630                 case 'f':
1631                         flush = 1;
1632                         break;
1633                 case 'l':
1634                         maxfilelen = getnum(optarg, &endp);
1635                         if (maxfilelen <= 0)
1636                                 usage();
1637                         break;
1638                 case 'm':
1639                         monitorstart = getnum(optarg, &endp);
1640                         if (monitorstart < 0)
1641                                 usage();
1642                         if (!endp || *endp++ != ':')
1643                                 usage();
1644                         monitorend = getnum(endp, &endp);
1645                         if (monitorend < 0)
1646                                 usage();
1647                         if (monitorend == 0)
1648                                 monitorend = -1; /* aka infinity */
1649                         debug = 1;
1650                 case 'n':
1651                         sizechecks = 0;
1652                         break;
1653                 case 'o':
1654                         maxoplen = getnum(optarg, &endp);
1655                         if (maxoplen <= 0)
1656                                 usage();
1657                         break;
1658                 case 'p':
1659                         progressinterval = getnum(optarg, &endp);
1660                         if (progressinterval == 0)
1661                                 usage();
1662                         break;
1663                 case 'q':
1664                         quiet = 1;
1665                         break;
1666                 case 'r':
1667                         readbdy = getnum(optarg, &endp);
1668                         if (readbdy <= 0)
1669                                 usage();
1670                         break;
1671                 case 's':
1672                         style = getnum(optarg, &endp);
1673                         if (style < 0 || style > 1)
1674                                 usage();
1675                         break;
1676                 case 't':
1677                         truncbdy = getnum(optarg, &endp);
1678                         if (truncbdy <= 0)
1679                                 usage();
1680                         break;
1681                 case 'w':
1682                         writebdy = getnum(optarg, &endp);
1683                         if (writebdy <= 0)
1684                                 usage();
1685                         break;
1686                 case 'x':
1687                         prealloc = 1;
1688                         break;
1689                 case 'y':
1690                         do_fsync = 1;
1691                         break;
1692                 case 'A':
1693                         aio = 1;
1694                         break;
1695                 case 'D':
1696                         debugstart = getnum(optarg, &endp);
1697                         if (debugstart < 1)
1698                                 usage();
1699                         break;
1700                 case 'F':
1701                         fallocate_calls = 0;
1702                         break;
1703                 case 'K':
1704                         keep_size_calls = 0;
1705                         break;
1706                 case 'H':
1707                         punch_hole_calls = 0;
1708                         break;
1709                 case 'z':
1710                         zero_range_calls = 0;
1711                         break;
1712                 case 'C':
1713                         collapse_range_calls = 0;
1714                         break;
1715                 case 'I':
1716                         insert_range_calls = 0;
1717                         break;
1718                 case 'L':
1719                         lite = 1;
1720                         break;
1721                 case 'N':
1722                         numops = getnum(optarg, &endp);
1723                         if (numops < 0)
1724                                 usage();
1725                         break;
1726                 case 'O':
1727                         randomoplen = 0;
1728                         break;
1729                 case 'P':
1730                         strncpy(goodfile, optarg, sizeof(goodfile));
1731                         strcat(goodfile, "/");
1732                         strncpy(logfile, optarg, sizeof(logfile));
1733                         strcat(logfile, "/");
1734                         break;
1735                 case 'R':
1736                         mapped_reads = 0;
1737                         break;
1738                 case 'S':
1739                         seed = getnum(optarg, &endp);
1740                         if (seed == 0)
1741                                 seed = time(0) % 10000;
1742                         if (!quiet)
1743                                 fprintf(stdout, "Seed set to %d\n", seed);
1744                         if (seed < 0)
1745                                 usage();
1746                         break;
1747                 case 'W':
1748                         mapped_writes = 0;
1749                         if (!quiet)
1750                                 fprintf(stdout, "mapped writes DISABLED\n");
1751                         break;
1752                 case 'Z':
1753                         o_direct = O_DIRECT;
1754                         break;
1755                 default:
1756                         usage();
1757                         /* NOTREACHED */
1758                 }
1759         argc -= optind;
1760         argv += optind;
1761         if (argc != 1)
1762                 usage();
1763         fname = argv[0];
1764
1765         signal(SIGHUP,  cleanup);
1766         signal(SIGINT,  cleanup);
1767         signal(SIGPIPE, cleanup);
1768         signal(SIGALRM, cleanup);
1769         signal(SIGTERM, cleanup);
1770         signal(SIGXCPU, cleanup);
1771         signal(SIGXFSZ, cleanup);
1772         signal(SIGVTALRM,       cleanup);
1773         signal(SIGUSR1, cleanup);
1774         signal(SIGUSR2, cleanup);
1775
1776         initstate(seed, state, 256);
1777         setstate(state);
1778         fd = open(fname,
1779                 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1780         if (fd < 0) {
1781                 prterr(fname);
1782                 exit(91);
1783         }
1784         if (fstat(fd, &statbuf)) {
1785                 prterr("check_size: fstat");
1786                 exit(91);
1787         }
1788         block_size = statbuf.st_blksize;
1789 #ifdef XFS
1790         if (prealloc) {
1791                 xfs_flock64_t   resv = { 0 };
1792 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1793                 if (!platform_test_xfs_fd(fd)) {
1794                         prterr(fname);
1795                         fprintf(stderr, "main: cannot prealloc, non XFS\n");
1796                         exit(96);
1797                 }
1798 #endif
1799                 resv.l_len = maxfilelen;
1800                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1801                         prterr(fname);
1802                         exit(97);
1803                 }
1804         }
1805 #endif
1806         strncat(goodfile, fname, 256);
1807         strcat (goodfile, ".fsxgood");
1808         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1809         if (fsxgoodfd < 0) {
1810                 prterr(goodfile);
1811                 exit(92);
1812         }
1813         strncat(logfile, fname, 256);
1814         strcat (logfile, ".fsxlog");
1815         fsxlogf = fopen(logfile, "w");
1816         if (fsxlogf == NULL) {
1817                 prterr(logfile);
1818                 exit(93);
1819         }
1820
1821 #ifdef AIO
1822         if (aio) 
1823                 aio_setup();
1824 #endif
1825
1826         if (lite) {
1827                 off_t ret;
1828                 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1829                 if (file_size == (off_t)-1) {
1830                         prterr(fname);
1831                         warn("main: lseek eof");
1832                         exit(94);
1833                 }
1834                 ret = lseek(fd, (off_t)0, SEEK_SET);
1835                 if (ret == (off_t)-1) {
1836                         prterr(fname);
1837                         warn("main: lseek 0");
1838                         exit(95);
1839                 }
1840         }
1841         original_buf = (char *) malloc(maxfilelen);
1842         for (i = 0; i < maxfilelen; i++)
1843                 original_buf[i] = random() % 256;
1844         good_buf = (char *) malloc(maxfilelen + writebdy);
1845         good_buf = round_ptr_up(good_buf, writebdy, 0);
1846         memset(good_buf, '\0', maxfilelen);
1847         temp_buf = (char *) malloc(maxoplen + readbdy);
1848         temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1849         memset(temp_buf, '\0', maxoplen);
1850         if (lite) {     /* zero entire existing file */
1851                 ssize_t written;
1852
1853                 written = write(fd, good_buf, (size_t)maxfilelen);
1854                 if (written != maxfilelen) {
1855                         if (written == -1) {
1856                                 prterr(fname);
1857                                 warn("main: error on write");
1858                         } else
1859                                 warn("main: short write, 0x%x bytes instead "
1860                                         "of 0x%lx\n",
1861                                         (unsigned)written,
1862                                         maxfilelen);
1863                         exit(98);
1864                 }
1865         } else 
1866                 check_trunc_hack();
1867
1868         if (fallocate_calls)
1869                 fallocate_calls = test_fallocate(0);
1870         if (keep_size_calls)
1871                 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
1872         if (punch_hole_calls)
1873                 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1874         if (zero_range_calls)
1875                 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1876         if (collapse_range_calls)
1877                 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
1878         if (insert_range_calls)
1879                 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
1880
1881         while (numops == -1 || numops--)
1882                 test();
1883
1884         if (close(fd)) {
1885                 prterr("close");
1886                 report_failure(99);
1887         }
1888         prt("All %lu operations completed A-OK!\n", testcalls);
1889
1890         exit(0);
1891         return 0;
1892 }