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