805fdfb0391286420b0ad2108642d32e57efab94
[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%5x\t0x%04x\t0x%04x", offset,
451                                             short_at(&good_buf[offset]), bad);
452                                         op = temp_buf[offset & 1 ? i+1 : i];
453                                         prt("\t0x%5x\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)
918 {
919         unsigned end_offset;
920         int mode = FALLOC_FL_ZERO_RANGE;
921         int keep_size = 0;
922
923         if (length == 0) {
924                 if (!quiet && testcalls > simulatedopcount)
925                         prt("skipping zero length zero range\n");
926                         log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
927                 return;
928         }
929
930         if (keep_size_calls)
931                 keep_size = random() % 2;
932
933         end_offset = keep_size ? 0 : offset + length;
934
935         if (end_offset > biggest) {
936                 biggest = end_offset;
937                 if (!quiet && testcalls > simulatedopcount)
938                         prt("zero_range to largest ever: 0x%x\n", end_offset);
939         }
940
941         /*
942          * last arg matches fallocate string array index in logdump:
943          *      0: allocate past EOF
944          *      1: extending prealloc
945          *      2: interior prealloc
946          */
947         log4(OP_ZERO_RANGE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
948
949         if (testcalls <= simulatedopcount)
950                 return;
951
952         if ((progressinterval && testcalls % progressinterval == 0) ||
953             (debug && (monitorstart == -1 || monitorend == -1 ||
954                       end_offset <= monitorend))) {
955                 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
956                         offset, offset+length, length);
957         }
958         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
959                 prt("%pzero range: %x to %x\n", offset, length);
960                 prterr("do_zero_range: fallocate");
961                 report_failure(161);
962         }
963
964         memset(good_buf + offset, '\0', length);
965 }
966
967 #else
968 void
969 do_zero_range(unsigned offset, unsigned length)
970 {
971         return;
972 }
973 #endif
974
975 #ifdef FALLOC_FL_COLLAPSE_RANGE
976 void
977 do_collapse_range(unsigned offset, unsigned length)
978 {
979         unsigned end_offset;
980         int mode = FALLOC_FL_COLLAPSE_RANGE;
981
982         if (length == 0) {
983                 if (!quiet && testcalls > simulatedopcount)
984                         prt("skipping zero length collapse range\n");
985                 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
986                 return;
987         }
988
989         end_offset = offset + length;
990         if ((loff_t)end_offset >= file_size) {
991                 if (!quiet && testcalls > simulatedopcount)
992                         prt("skipping collapse range behind EOF\n");
993                 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
994                 return;
995         }
996
997         log4(OP_COLLAPSE_RANGE, offset, length, 0);
998
999         if (testcalls <= simulatedopcount)
1000                 return;
1001
1002         if ((progressinterval && testcalls % progressinterval == 0) ||
1003             (debug && (monitorstart == -1 || monitorend == -1 ||
1004                       end_offset <= monitorend))) {
1005                 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1006                         offset, offset+length, length);
1007         }
1008         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1009                 prt("collapse range: %x to %x\n", offset, length);
1010                 prterr("do_collapse_range: fallocate");
1011                 report_failure(161);
1012         }
1013
1014         memmove(good_buf + offset, good_buf + end_offset,
1015                 file_size - end_offset);
1016         file_size -= length;
1017 }
1018
1019 #else
1020 void
1021 do_collapse_range(unsigned offset, unsigned length)
1022 {
1023         return;
1024 }
1025 #endif
1026
1027 #ifdef FALLOC_FL_INSERT_RANGE
1028 void
1029 do_insert_range(unsigned offset, unsigned length)
1030 {
1031         unsigned end_offset;
1032         int mode = FALLOC_FL_INSERT_RANGE;
1033
1034         if (length == 0) {
1035                 if (!quiet && testcalls > simulatedopcount)
1036                         prt("skipping zero length insert range\n");
1037                 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, length);
1038                 return;
1039         }
1040
1041         if ((loff_t)offset >= file_size) {
1042                 if (!quiet && testcalls > simulatedopcount)
1043                         prt("skipping insert range behind EOF\n");
1044                 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, length);
1045                 return;
1046         }
1047
1048         log4(OP_INSERT_RANGE, offset, length, 0);
1049
1050         if (testcalls <= simulatedopcount)
1051                 return;
1052
1053         end_offset = offset + length;
1054         if ((progressinterval && testcalls % progressinterval == 0) ||
1055             (debug && (monitorstart == -1 || monitorend == -1 ||
1056                       end_offset <= monitorend))) {
1057                 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1058                         offset, offset+length, length);
1059         }
1060         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1061                 prt("insert range: %x to %x\n", offset, length);
1062                 prterr("do_insert_range: fallocate");
1063                 report_failure(161);
1064         }
1065
1066         memmove(good_buf + end_offset, good_buf + offset,
1067                 file_size - offset);
1068         memset(good_buf + offset, '\0', length);
1069         file_size += length;
1070 }
1071
1072 #else
1073 void
1074 do_insert_range(unsigned offset, unsigned length)
1075 {
1076         return;
1077 }
1078 #endif
1079
1080 #ifdef HAVE_LINUX_FALLOC_H
1081 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1082 void
1083 do_preallocate(unsigned offset, unsigned length)
1084 {
1085         unsigned end_offset;
1086         int keep_size = 0;
1087
1088         if (length == 0) {
1089                 if (!quiet && testcalls > simulatedopcount)
1090                         prt("skipping zero length fallocate\n");
1091                 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1092                 return;
1093         }
1094
1095         if (keep_size_calls)
1096                 keep_size = random() % 2;
1097
1098         end_offset = keep_size ? 0 : offset + length;
1099
1100         if (end_offset > biggest) {
1101                 biggest = end_offset;
1102                 if (!quiet && testcalls > simulatedopcount)
1103                         prt("fallocating to largest ever: 0x%x\n", end_offset);
1104         }
1105
1106         /*
1107          * last arg matches fallocate string array index in logdump:
1108          *      0: allocate past EOF
1109          *      1: extending prealloc
1110          *      2: interior prealloc
1111          */
1112         log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1113
1114         if (end_offset > file_size) {
1115                 memset(good_buf + file_size, '\0', end_offset - file_size);
1116                 file_size = end_offset;
1117         }
1118
1119         if (testcalls <= simulatedopcount)
1120                 return;
1121         
1122         if ((progressinterval && testcalls % progressinterval == 0) ||
1123             (debug && (monitorstart == -1 || monitorend == -1 ||
1124                       end_offset <= monitorend)))
1125                 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1126                                 offset, offset + length, length);
1127         if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1128                 prt("fallocate: %x to %x\n", offset, length);
1129                 prterr("do_preallocate: fallocate");
1130                 report_failure(161);
1131         }
1132 }
1133 #else
1134 void
1135 do_preallocate(unsigned offset, unsigned length)
1136 {
1137         return;
1138 }
1139 #endif
1140
1141 void
1142 writefileimage()
1143 {
1144         ssize_t iret;
1145
1146         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1147                 prterr("writefileimage: lseek");
1148                 report_failure(171);
1149         }
1150         iret = write(fd, good_buf, file_size);
1151         if ((off_t)iret != file_size) {
1152                 if (iret == -1)
1153                         prterr("writefileimage: write");
1154                 else
1155                         prt("short write: 0x%x bytes instead of 0x%llx\n",
1156                             iret, (unsigned long long)file_size);
1157                 report_failure(172);
1158         }
1159         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1160                 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1161                 prterr("writefileimage: ftruncate");
1162                 report_failure(173);
1163         }
1164 }
1165
1166
1167 void
1168 docloseopen(void)
1169
1170         if (testcalls <= simulatedopcount)
1171                 return;
1172
1173         if (debug)
1174                 prt("%lu close/open\n", testcalls);
1175         if (close(fd)) {
1176                 prterr("docloseopen: close");
1177                 report_failure(180);
1178         }
1179         fd = open(fname, O_RDWR|o_direct, 0);
1180         if (fd < 0) {
1181                 prterr("docloseopen: open");
1182                 report_failure(181);
1183         }
1184 }
1185
1186
1187 #define TRIM_OFF(off, size)                     \
1188 do {                                            \
1189         if (size)                               \
1190                 (off) %= (size);                \
1191         else                                    \
1192                 (off) = 0;                      \
1193 } while (0)
1194
1195 #define TRIM_LEN(off, len, size)                \
1196 do {                                            \
1197         if ((off) + (len) > (size))             \
1198                 (len) = (size) - (off);         \
1199 } while (0)
1200
1201 #define TRIM_OFF_LEN(off, len, size)            \
1202 do {                                            \
1203         TRIM_OFF(off, size);                    \
1204         TRIM_LEN(off, len, size);               \
1205 } while (0)
1206
1207 void
1208 test(void)
1209 {
1210         unsigned long   offset;
1211         unsigned long   size = maxoplen;
1212         unsigned long   rv = random();
1213         unsigned long   op;
1214
1215         if (simulatedopcount > 0 && testcalls == simulatedopcount)
1216                 writefileimage();
1217
1218         testcalls++;
1219
1220         if (closeprob)
1221                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1222
1223         if (debugstart > 0 && testcalls >= debugstart)
1224                 debug = 1;
1225
1226         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1227                 prt("%lu...\n", testcalls);
1228
1229         offset = random();
1230         if (randomoplen)
1231                 size = random() % (maxoplen + 1);
1232
1233         /* calculate appropriate op to run */
1234         if (lite)
1235                 op = rv % OP_MAX_LITE;
1236         else
1237                 op = rv % OP_MAX_FULL;
1238
1239         switch (op) {
1240         case OP_MAPREAD:
1241                 if (!mapped_reads)
1242                         op = OP_READ;
1243                 break;
1244         case OP_MAPWRITE:
1245                 if (!mapped_writes)
1246                         op = OP_WRITE;
1247                 break;
1248         case OP_FALLOCATE:
1249                 if (!fallocate_calls) {
1250                         log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1251                         goto out;
1252                 }
1253                 break;
1254         case OP_PUNCH_HOLE:
1255                 if (!punch_hole_calls) {
1256                         log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1257                         goto out;
1258                 }
1259                 break;
1260         case OP_ZERO_RANGE:
1261                 if (!zero_range_calls) {
1262                         log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1263                         goto out;
1264                 }
1265                 break;
1266         case OP_COLLAPSE_RANGE:
1267                 if (!collapse_range_calls) {
1268                         log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1269                         goto out;
1270                 }
1271                 break;
1272         case OP_INSERT_RANGE:
1273                 if (!insert_range_calls) {
1274                         log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1275                         goto out;
1276                 }
1277                 break;
1278         }
1279
1280         switch (op) {
1281         case OP_READ:
1282                 TRIM_OFF_LEN(offset, size, file_size);
1283                 doread(offset, size);
1284                 break;
1285
1286         case OP_WRITE:
1287                 TRIM_OFF_LEN(offset, size, maxfilelen);
1288                 dowrite(offset, size);
1289                 break;
1290
1291         case OP_MAPREAD:
1292                 TRIM_OFF_LEN(offset, size, file_size);
1293                 domapread(offset, size);
1294                 break;
1295
1296         case OP_MAPWRITE:
1297                 TRIM_OFF_LEN(offset, size, maxfilelen);
1298                 domapwrite(offset, size);
1299                 break;
1300
1301         case OP_TRUNCATE:
1302                 if (!style)
1303                         size = random() % maxfilelen;
1304                 dotruncate(size);
1305                 break;
1306
1307         case OP_FALLOCATE:
1308                 TRIM_OFF_LEN(offset, size, maxfilelen);
1309                 do_preallocate(offset, size);
1310                 break;
1311
1312         case OP_PUNCH_HOLE:
1313                 TRIM_OFF_LEN(offset, size, file_size);
1314                 do_punch_hole(offset, size);
1315                 break;
1316         case OP_ZERO_RANGE:
1317                 TRIM_OFF_LEN(offset, size, file_size);
1318                 do_zero_range(offset, size);
1319                 break;
1320         case OP_COLLAPSE_RANGE:
1321                 TRIM_OFF_LEN(offset, size, file_size - 1);
1322                 offset = offset & ~(block_size - 1);
1323                 size = size & ~(block_size - 1);
1324                 if (size == 0) {
1325                         log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1326                         goto out;
1327                 }
1328                 do_collapse_range(offset, size);
1329                 break;
1330         case OP_INSERT_RANGE:
1331                 TRIM_OFF(offset, file_size);
1332                 TRIM_LEN(file_size, size, maxfilelen);
1333                 offset = offset & ~(block_size - 1);
1334                 size = size & ~(block_size - 1);
1335                 if (size == 0) {
1336                         log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1337                         goto out;
1338                 }
1339                 if (file_size + size > maxfilelen) {
1340                         log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1341                         goto out;
1342                 }
1343
1344                 do_insert_range(offset, size);
1345                 break;
1346         default:
1347                 prterr("test: unknown operation");
1348                 report_failure(42);
1349                 break;
1350         }
1351
1352 out:
1353         if (sizechecks && testcalls > simulatedopcount)
1354                 check_size();
1355         if (closeopen)
1356                 docloseopen();
1357 }
1358
1359
1360 void
1361 cleanup(sig)
1362         int     sig;
1363 {
1364         if (sig)
1365                 prt("signal %d\n", sig);
1366         prt("testcalls = %lu\n", testcalls);
1367         exit(sig);
1368 }
1369
1370
1371 void
1372 usage(void)
1373 {
1374         fprintf(stdout, "usage: %s",
1375                 "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\
1376         -b opnum: beginning operation number (default 1)\n\
1377         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1378         -d: debug output for all operations\n\
1379         -f flush and invalidate cache after I/O\n\
1380         -l flen: the upper bound on file size (default 262144)\n\
1381         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1382         -n: no verifications of file size\n\
1383         -o oplen: the upper bound on operation size (default 65536)\n\
1384         -p progressinterval: debug output at specified operation interval\n\
1385         -q: quieter operation\n\
1386         -r readbdy: 4096 would make reads page aligned (default 1)\n\
1387         -s style: 1 gives smaller truncates (default 0)\n\
1388         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1389         -w writebdy: 4096 would make writes page aligned (default 1)\n\
1390         -x: preallocate file space before starting, XFS only (default 0)\n\
1391         -y synchronize changes to a file\n"
1392
1393 #ifdef AIO
1394 "       -A: Use the AIO system calls\n"
1395 #endif
1396 "       -D startingop: debug output starting at specified operation\n"
1397 #ifdef HAVE_LINUX_FALLOC_H
1398 "       -F: Do not use fallocate (preallocation) calls\n"
1399 #endif
1400 #ifdef FALLOC_FL_PUNCH_HOLE
1401 "       -H: Do not use punch hole calls\n"
1402 #endif
1403 #ifdef FALLOC_FL_ZERO_RANGE
1404 "       -z: Do not use zero range calls\n"
1405 #endif
1406 #ifdef FALLOC_FL_COLLAPSE_RANGE
1407 "       -C: Do not use collapse range calls\n"
1408 #endif
1409 #ifdef FALLOC_FL_INSERT_RANGE
1410 "       -I: Do not use insert range calls\n"
1411 #endif
1412 "       -L: fsxLite - no file creations & no file size changes\n\
1413         -N numops: total # operations to do (default infinity)\n\
1414         -O: use oplen (see -o flag) for every op (default random)\n\
1415         -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1416         -S seed: for random # generator (default 1) 0 gets timestamp\n\
1417         -W: mapped write operations DISabled\n\
1418         -R: read() system calls only (mapped reads disabled)\n\
1419         -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1420         fname: this filename is REQUIRED (no default)\n");
1421         exit(90);
1422 }
1423
1424
1425 int
1426 getnum(char *s, char **e)
1427 {
1428         int ret;
1429
1430         *e = (char *) 0;
1431         ret = strtol(s, e, 0);
1432         if (*e)
1433                 switch (**e) {
1434                 case 'b':
1435                 case 'B':
1436                         ret *= 512;
1437                         *e = *e + 1;
1438                         break;
1439                 case 'k':
1440                 case 'K':
1441                         ret *= 1024;
1442                         *e = *e + 1;
1443                         break;
1444                 case 'm':
1445                 case 'M':
1446                         ret *= 1024*1024;
1447                         *e = *e + 1;
1448                         break;
1449                 case 'w':
1450                 case 'W':
1451                         ret *= 4;
1452                         *e = *e + 1;
1453                         break;
1454                 }
1455         return (ret);
1456 }
1457
1458 #ifdef AIO
1459
1460 #define QSZ     1024
1461 io_context_t    io_ctx;
1462 struct iocb     iocb;
1463
1464 int aio_setup()
1465 {
1466         int ret;
1467         ret = io_queue_init(QSZ, &io_ctx);
1468         if (ret != 0) {
1469                 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1470                         strerror(ret));
1471                 return(-1);
1472         }
1473         return(0);
1474 }
1475
1476 int
1477 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1478 {
1479         struct io_event event;
1480         static struct timespec ts;
1481         struct iocb *iocbs[] = { &iocb };
1482         int ret;
1483         long res;
1484
1485         if (rw == READ) {
1486                 io_prep_pread(&iocb, fd, buf, len, offset);
1487         } else {
1488                 io_prep_pwrite(&iocb, fd, buf, len, offset);
1489         }
1490
1491         ts.tv_sec = 30;
1492         ts.tv_nsec = 0;
1493         ret = io_submit(io_ctx, 1, iocbs);
1494         if (ret != 1) {
1495                 fprintf(stderr, "errcode=%d\n", ret);
1496                 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1497                                 strerror(ret));
1498                 goto out_error;
1499         }
1500
1501         ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1502         if (ret != 1) {
1503                 if (ret == 0)
1504                         fprintf(stderr, "aio_rw: no events available\n");
1505                 else {
1506                         fprintf(stderr, "errcode=%d\n", -ret);
1507                         fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1508                                         strerror(-ret));
1509                 }
1510                 goto out_error;
1511         }
1512         if (len != event.res) {
1513                 /*
1514                  * The b0rked libaio defines event.res as unsigned.
1515                  * However the kernel strucuture has it signed,
1516                  * and it's used to pass negated error value.
1517                  * Till the library is fixed use the temp var.
1518                  */
1519                 res = (long)event.res;
1520                 if (res >= 0)
1521                         fprintf(stderr, "bad io length: %lu instead of %u\n",
1522                                         res, len);
1523                 else {
1524                         fprintf(stderr, "errcode=%ld\n", -res);
1525                         fprintf(stderr, "aio_rw: async io failed: %s\n",
1526                                         strerror(-res));
1527                         ret = res;
1528                         goto out_error;
1529                 }
1530
1531         }
1532         return event.res;
1533
1534 out_error:
1535         /*
1536          * The caller expects error return in traditional libc
1537          * convention, i.e. -1 and the errno set to error.
1538          */
1539         errno = -ret;
1540         return -1;
1541 }
1542
1543 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1544 {
1545         int ret;
1546
1547         if (aio) {
1548                 ret = __aio_rw(rw, fd, buf, len, offset);
1549         } else {
1550                 if (rw == READ)
1551                         ret = read(fd, buf, len);
1552                 else
1553                         ret = write(fd, buf, len);
1554         }
1555         return ret;
1556 }
1557
1558 #endif
1559
1560 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1561
1562 int
1563 __test_fallocate(int mode, const char *mode_str)
1564 {
1565 #ifdef HAVE_LINUX_FALLOC_H
1566         int ret = 0;
1567         if (!lite) {
1568                 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1569                         if(!quiet)
1570                                 fprintf(stderr,
1571                                         "main: filesystem does not support "
1572                                         "fallocate mode %s, disabling!\n",
1573                                         mode_str);
1574                 } else {
1575                         ret = 1;
1576                         ftruncate(fd, 0);
1577                 }
1578         }
1579         return ret;
1580 #endif
1581 }
1582
1583 int
1584 main(int argc, char **argv)
1585 {
1586         int     i, style, ch;
1587         char    *endp;
1588         char goodfile[1024];
1589         char logfile[1024];
1590         struct stat statbuf;
1591
1592         goodfile[0] = 0;
1593         logfile[0] = 0;
1594
1595         page_size = getpagesize();
1596         page_mask = page_size - 1;
1597         mmap_mask = page_mask;
1598         
1599
1600         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1601
1602         while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ"))
1603                != EOF)
1604                 switch (ch) {
1605                 case 'b':
1606                         simulatedopcount = getnum(optarg, &endp);
1607                         if (!quiet)
1608                                 fprintf(stdout, "Will begin at operation %ld\n",
1609                                         simulatedopcount);
1610                         if (simulatedopcount == 0)
1611                                 usage();
1612                         simulatedopcount -= 1;
1613                         break;
1614                 case 'c':
1615                         closeprob = getnum(optarg, &endp);
1616                         if (!quiet)
1617                                 fprintf(stdout,
1618                                         "Chance of close/open is 1 in %d\n",
1619                                         closeprob);
1620                         if (closeprob <= 0)
1621                                 usage();
1622                         break;
1623                 case 'd':
1624                         debug = 1;
1625                         break;
1626                 case 'f':
1627                         flush = 1;
1628                         break;
1629                 case 'l':
1630                         maxfilelen = getnum(optarg, &endp);
1631                         if (maxfilelen <= 0)
1632                                 usage();
1633                         break;
1634                 case 'm':
1635                         monitorstart = getnum(optarg, &endp);
1636                         if (monitorstart < 0)
1637                                 usage();
1638                         if (!endp || *endp++ != ':')
1639                                 usage();
1640                         monitorend = getnum(endp, &endp);
1641                         if (monitorend < 0)
1642                                 usage();
1643                         if (monitorend == 0)
1644                                 monitorend = -1; /* aka infinity */
1645                         debug = 1;
1646                 case 'n':
1647                         sizechecks = 0;
1648                         break;
1649                 case 'o':
1650                         maxoplen = getnum(optarg, &endp);
1651                         if (maxoplen <= 0)
1652                                 usage();
1653                         break;
1654                 case 'p':
1655                         progressinterval = getnum(optarg, &endp);
1656                         if (progressinterval == 0)
1657                                 usage();
1658                         break;
1659                 case 'q':
1660                         quiet = 1;
1661                         break;
1662                 case 'r':
1663                         readbdy = getnum(optarg, &endp);
1664                         if (readbdy <= 0)
1665                                 usage();
1666                         break;
1667                 case 's':
1668                         style = getnum(optarg, &endp);
1669                         if (style < 0 || style > 1)
1670                                 usage();
1671                         break;
1672                 case 't':
1673                         truncbdy = getnum(optarg, &endp);
1674                         if (truncbdy <= 0)
1675                                 usage();
1676                         break;
1677                 case 'w':
1678                         writebdy = getnum(optarg, &endp);
1679                         if (writebdy <= 0)
1680                                 usage();
1681                         break;
1682                 case 'x':
1683                         prealloc = 1;
1684                         break;
1685                 case 'y':
1686                         do_fsync = 1;
1687                         break;
1688                 case 'A':
1689                         aio = 1;
1690                         break;
1691                 case 'D':
1692                         debugstart = getnum(optarg, &endp);
1693                         if (debugstart < 1)
1694                                 usage();
1695                         break;
1696                 case 'F':
1697                         fallocate_calls = 0;
1698                         break;
1699                 case 'K':
1700                         keep_size_calls = 0;
1701                         break;
1702                 case 'H':
1703                         punch_hole_calls = 0;
1704                         break;
1705                 case 'z':
1706                         zero_range_calls = 0;
1707                         break;
1708                 case 'C':
1709                         collapse_range_calls = 0;
1710                         break;
1711                 case 'I':
1712                         insert_range_calls = 0;
1713                         break;
1714                 case 'L':
1715                         lite = 1;
1716                         break;
1717                 case 'N':
1718                         numops = getnum(optarg, &endp);
1719                         if (numops < 0)
1720                                 usage();
1721                         break;
1722                 case 'O':
1723                         randomoplen = 0;
1724                         break;
1725                 case 'P':
1726                         strncpy(goodfile, optarg, sizeof(goodfile));
1727                         strcat(goodfile, "/");
1728                         strncpy(logfile, optarg, sizeof(logfile));
1729                         strcat(logfile, "/");
1730                         break;
1731                 case 'R':
1732                         mapped_reads = 0;
1733                         break;
1734                 case 'S':
1735                         seed = getnum(optarg, &endp);
1736                         if (seed == 0)
1737                                 seed = time(0) % 10000;
1738                         if (!quiet)
1739                                 fprintf(stdout, "Seed set to %d\n", seed);
1740                         if (seed < 0)
1741                                 usage();
1742                         break;
1743                 case 'W':
1744                         mapped_writes = 0;
1745                         if (!quiet)
1746                                 fprintf(stdout, "mapped writes DISABLED\n");
1747                         break;
1748                 case 'Z':
1749                         o_direct = O_DIRECT;
1750                         break;
1751                 default:
1752                         usage();
1753                         /* NOTREACHED */
1754                 }
1755         argc -= optind;
1756         argv += optind;
1757         if (argc != 1)
1758                 usage();
1759         fname = argv[0];
1760
1761         signal(SIGHUP,  cleanup);
1762         signal(SIGINT,  cleanup);
1763         signal(SIGPIPE, cleanup);
1764         signal(SIGALRM, cleanup);
1765         signal(SIGTERM, cleanup);
1766         signal(SIGXCPU, cleanup);
1767         signal(SIGXFSZ, cleanup);
1768         signal(SIGVTALRM,       cleanup);
1769         signal(SIGUSR1, cleanup);
1770         signal(SIGUSR2, cleanup);
1771
1772         initstate(seed, state, 256);
1773         setstate(state);
1774         fd = open(fname,
1775                 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1776         if (fd < 0) {
1777                 prterr(fname);
1778                 exit(91);
1779         }
1780         if (fstat(fd, &statbuf)) {
1781                 prterr("check_size: fstat");
1782                 exit(91);
1783         }
1784         block_size = statbuf.st_blksize;
1785 #ifdef XFS
1786         if (prealloc) {
1787                 xfs_flock64_t   resv = { 0 };
1788 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1789                 if (!platform_test_xfs_fd(fd)) {
1790                         prterr(fname);
1791                         fprintf(stderr, "main: cannot prealloc, non XFS\n");
1792                         exit(96);
1793                 }
1794 #endif
1795                 resv.l_len = maxfilelen;
1796                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1797                         prterr(fname);
1798                         exit(97);
1799                 }
1800         }
1801 #endif
1802         strncat(goodfile, fname, 256);
1803         strcat (goodfile, ".fsxgood");
1804         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1805         if (fsxgoodfd < 0) {
1806                 prterr(goodfile);
1807                 exit(92);
1808         }
1809         strncat(logfile, fname, 256);
1810         strcat (logfile, ".fsxlog");
1811         fsxlogf = fopen(logfile, "w");
1812         if (fsxlogf == NULL) {
1813                 prterr(logfile);
1814                 exit(93);
1815         }
1816
1817 #ifdef AIO
1818         if (aio) 
1819                 aio_setup();
1820 #endif
1821
1822         if (lite) {
1823                 off_t ret;
1824                 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1825                 if (file_size == (off_t)-1) {
1826                         prterr(fname);
1827                         warn("main: lseek eof");
1828                         exit(94);
1829                 }
1830                 ret = lseek(fd, (off_t)0, SEEK_SET);
1831                 if (ret == (off_t)-1) {
1832                         prterr(fname);
1833                         warn("main: lseek 0");
1834                         exit(95);
1835                 }
1836         }
1837         original_buf = (char *) malloc(maxfilelen);
1838         for (i = 0; i < maxfilelen; i++)
1839                 original_buf[i] = random() % 256;
1840         good_buf = (char *) malloc(maxfilelen + writebdy);
1841         good_buf = round_ptr_up(good_buf, writebdy, 0);
1842         memset(good_buf, '\0', maxfilelen);
1843         temp_buf = (char *) malloc(maxoplen + readbdy);
1844         temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1845         memset(temp_buf, '\0', maxoplen);
1846         if (lite) {     /* zero entire existing file */
1847                 ssize_t written;
1848
1849                 written = write(fd, good_buf, (size_t)maxfilelen);
1850                 if (written != maxfilelen) {
1851                         if (written == -1) {
1852                                 prterr(fname);
1853                                 warn("main: error on write");
1854                         } else
1855                                 warn("main: short write, 0x%x bytes instead "
1856                                         "of 0x%lx\n",
1857                                         (unsigned)written,
1858                                         maxfilelen);
1859                         exit(98);
1860                 }
1861         } else 
1862                 check_trunc_hack();
1863
1864         if (fallocate_calls)
1865                 fallocate_calls = test_fallocate(0);
1866         if (keep_size_calls)
1867                 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
1868         if (punch_hole_calls)
1869                 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1870         if (zero_range_calls)
1871                 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1872         if (collapse_range_calls)
1873                 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
1874         if (insert_range_calls)
1875                 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
1876
1877         while (numops == -1 || numops--)
1878                 test();
1879
1880         if (close(fd)) {
1881                 prterr("close");
1882                 report_failure(99);
1883         }
1884         prt("All operations completed A-OK!\n");
1885
1886         exit(0);
1887         return 0;
1888 }