fsx: fix compile warnings
[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 #include <stdbool.h>
24 #ifdef HAVE_ERR_H
25 #include <err.h>
26 #endif
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #ifdef AIO
35 #include <libaio.h>
36 #endif
37
38 #ifndef MAP_FILE
39 # define MAP_FILE 0
40 #endif
41
42 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
43
44 /* Operation flags */
45
46 enum opflags { FL_NONE = 0, FL_SKIPPED = 1, FL_CLOSE_OPEN = 2, FL_KEEP_SIZE = 4 };
47
48 /*
49  *      A log entry is an operation and a bunch of arguments.
50  */
51
52 struct log_entry {
53         int     operation;
54         int     args[3];
55         enum opflags flags;
56 };
57
58 #define LOGSIZE 10000
59
60 struct log_entry        oplog[LOGSIZE]; /* the log */
61 int                     logptr = 0;     /* current position in log */
62 int                     logcount = 0;   /* total ops */
63
64 /*
65  * The operation matrix is complex due to conditional execution of different
66  * features. Hence when we come to deciding what operation to run, we need to
67  * be careful in how we select the different operations. The active operations
68  * are mapped to numbers as follows:
69  *
70  *              lite    !lite
71  * READ:        0       0
72  * WRITE:       1       1
73  * MAPREAD:     2       2
74  * MAPWRITE:    3       3
75  * TRUNCATE:    -       4
76  * FALLOCATE:   -       5
77  * PUNCH HOLE:  -       6
78  * ZERO RANGE:  -       7
79  *
80  * When mapped read/writes are disabled, they are simply converted to normal
81  * reads and writes. When fallocate/fpunch calls are disabled, they are
82  * skipped.
83  *
84  * Because of the "lite" version, we also need to have different "maximum
85  * operation" defines to allow the ops to be selected correctly based on the
86  * mode being run.
87  */
88
89 /* common operations */
90 #define OP_READ         0
91 #define OP_WRITE        1
92 #define OP_MAPREAD      2
93 #define OP_MAPWRITE     3
94 #define OP_MAX_LITE     4
95
96 /* !lite operations */
97 #define OP_TRUNCATE             4
98 #define OP_FALLOCATE            5
99 #define OP_PUNCH_HOLE           6
100 #define OP_ZERO_RANGE           7
101 #define OP_COLLAPSE_RANGE       8
102 #define OP_INSERT_RANGE 9
103 #define OP_MAX_FULL             10
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 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, "%s", buffer);
215         if (fsxlogf)
216                 fprintf(fsxlogf, "%s", 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         if (ftruncate(fd, (off_t)0))
573                 goto ftruncate_err;
574         if (ftruncate(fd, (off_t)100000))
575                 goto ftruncate_err;
576         fstat(fd, &statbuf);
577         if (statbuf.st_size != (off_t)100000) {
578                 prt("no extend on truncate! not posix!\n");
579                 exit(130);
580         }
581         if (ftruncate(fd, 0)) {
582 ftruncate_err:
583                 prterr("check_trunc_hack: ftruncate");
584                 exit(131);
585         }
586 }
587
588 void
589 doflush(unsigned offset, unsigned size)
590 {
591         unsigned pg_offset;
592         unsigned map_size;
593         char    *p;
594
595         if (o_direct == O_DIRECT)
596                 return;
597
598         pg_offset = offset & mmap_mask;
599         map_size  = pg_offset + size;
600
601         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
602                               MAP_FILE | MAP_SHARED, fd,
603                               (off_t)(offset - pg_offset))) == (char *)-1) {
604                 prterr("doflush: mmap");
605                 report_failure(202);
606         }
607         if (msync(p, map_size, MS_INVALIDATE) != 0) {
608                 prterr("doflush: msync");
609                 report_failure(203);
610         }
611         if (munmap(p, map_size) != 0) {
612                 prterr("doflush: munmap");
613                 report_failure(204);
614         }
615 }
616
617 void
618 doread(unsigned offset, unsigned size)
619 {
620         off_t ret;
621         unsigned iret;
622
623         offset -= offset % readbdy;
624         if (o_direct)
625                 size -= size % readbdy;
626         if (size == 0) {
627                 if (!quiet && testcalls > simulatedopcount && !o_direct)
628                         prt("skipping zero size read\n");
629                 log4(OP_READ, offset, size, FL_SKIPPED);
630                 return;
631         }
632         if (size + offset > file_size) {
633                 if (!quiet && testcalls > simulatedopcount)
634                         prt("skipping seek/read past end of file\n");
635                 log4(OP_READ, offset, size, FL_SKIPPED);
636                 return;
637         }
638
639         log4(OP_READ, offset, size, FL_NONE);
640
641         if (testcalls <= simulatedopcount)
642                 return;
643
644         if (!quiet &&
645                 ((progressinterval && testcalls % progressinterval == 0)  ||
646                 (debug &&
647                        (monitorstart == -1 ||
648                         (offset + size > monitorstart &&
649                         (monitorend == -1 || offset <= monitorend))))))
650                 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
651                     offset, offset + size - 1, size);
652         ret = lseek(fd, (off_t)offset, SEEK_SET);
653         if (ret == (off_t)-1) {
654                 prterr("doread: lseek");
655                 report_failure(140);
656         }
657         iret = fsxread(fd, temp_buf, size, offset);
658         if (iret != size) {
659                 if (iret == -1)
660                         prterr("doread: read");
661                 else
662                         prt("short read: 0x%x bytes instead of 0x%x\n",
663                             iret, size);
664                 report_failure(141);
665         }
666         check_buffers(offset, size);
667 }
668
669
670 void
671 check_eofpage(char *s, unsigned offset, char *p, int size)
672 {
673         unsigned long last_page, should_be_zero;
674
675         if (offset + size <= (file_size & ~page_mask))
676                 return;
677         /*
678          * we landed in the last page of the file
679          * test to make sure the VM system provided 0's 
680          * beyond the true end of the file mapping
681          * (as required by mmap def in 1996 posix 1003.1)
682          */
683         last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
684
685         for (should_be_zero = last_page + (file_size & page_mask);
686              should_be_zero < last_page + page_size;
687              should_be_zero++)
688                 if (*(char *)should_be_zero) {
689                         prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
690                             s, file_size - 1, should_be_zero & page_mask,
691                             short_at(should_be_zero));
692                         report_failure(205);
693                 }
694 }
695
696
697 void
698 domapread(unsigned offset, unsigned size)
699 {
700         unsigned pg_offset;
701         unsigned map_size;
702         char    *p;
703
704         offset -= offset % readbdy;
705         if (size == 0) {
706                 if (!quiet && testcalls > simulatedopcount)
707                         prt("skipping zero size read\n");
708                 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
709                 return;
710         }
711         if (size + offset > file_size) {
712                 if (!quiet && testcalls > simulatedopcount)
713                         prt("skipping seek/read past end of file\n");
714                 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
715                 return;
716         }
717
718         log4(OP_MAPREAD, offset, size, FL_NONE);
719
720         if (testcalls <= simulatedopcount)
721                 return;
722
723         if (!quiet &&
724                 ((progressinterval && testcalls % progressinterval == 0) ||
725                        (debug &&
726                        (monitorstart == -1 ||
727                         (offset + size > monitorstart &&
728                         (monitorend == -1 || offset <= monitorend))))))
729                 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
730                     offset, offset + size - 1, size);
731
732         pg_offset = offset & PAGE_MASK;
733         map_size  = pg_offset + size;
734
735         if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
736                               (off_t)(offset - pg_offset))) == (char *)-1) {
737                 prterr("domapread: mmap");
738                 report_failure(190);
739         }
740         memcpy(temp_buf, p + pg_offset, size);
741
742         check_eofpage("Read", offset, p, size);
743
744         if (munmap(p, map_size) != 0) {
745                 prterr("domapread: munmap");
746                 report_failure(191);
747         }
748
749         check_buffers(offset, size);
750 }
751
752
753 void
754 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
755 {
756         while (size--) {
757                 good_buf[offset] = testcalls % 256; 
758                 if (offset % 2)
759                         good_buf[offset] += original_buf[offset];
760                 offset++;
761         }
762 }
763
764
765 void
766 dowrite(unsigned offset, unsigned size)
767 {
768         off_t ret;
769         unsigned iret;
770
771         offset -= offset % writebdy;
772         if (o_direct)
773                 size -= size % writebdy;
774         if (size == 0) {
775                 if (!quiet && testcalls > simulatedopcount && !o_direct)
776                         prt("skipping zero size write\n");
777                 log4(OP_WRITE, offset, size, FL_SKIPPED);
778                 return;
779         }
780
781         log4(OP_WRITE, offset, size, FL_NONE);
782
783         gendata(original_buf, good_buf, offset, size);
784         if (file_size < offset + size) {
785                 if (file_size < offset)
786                         memset(good_buf + file_size, '\0', offset - file_size);
787                 file_size = offset + size;
788                 if (lite) {
789                         warn("Lite file size bug in fsx!");
790                         report_failure(149);
791                 }
792         }
793
794         if (testcalls <= simulatedopcount)
795                 return;
796
797         if (!quiet &&
798                 ((progressinterval && testcalls % progressinterval == 0) ||
799                        (debug &&
800                        (monitorstart == -1 ||
801                         (offset + size > monitorstart &&
802                         (monitorend == -1 || offset <= monitorend))))))
803                 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
804                     offset, offset + size - 1, size);
805         ret = lseek(fd, (off_t)offset, SEEK_SET);
806         if (ret == (off_t)-1) {
807                 prterr("dowrite: lseek");
808                 report_failure(150);
809         }
810         iret = fsxwrite(fd, good_buf + offset, size, offset);
811         if (iret != size) {
812                 if (iret == -1)
813                         prterr("dowrite: write");
814                 else
815                         prt("short write: 0x%x bytes instead of 0x%x\n",
816                             iret, size);
817                 report_failure(151);
818         }
819         if (do_fsync) {
820                 if (fsync(fd)) {
821                         prt("fsync() failed: %s\n", strerror(errno));
822                         report_failure(152);
823                 }
824         }
825         if (flush) {
826                 doflush(offset, size);
827         }
828 }
829
830
831 void
832 domapwrite(unsigned offset, unsigned size)
833 {
834         unsigned pg_offset;
835         unsigned map_size;
836         off_t    cur_filesize;
837         char    *p;
838
839         offset -= offset % writebdy;
840         if (size == 0) {
841                 if (!quiet && testcalls > simulatedopcount)
842                         prt("skipping zero size write\n");
843                 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
844                 return;
845         }
846         cur_filesize = file_size;
847
848         log4(OP_MAPWRITE, offset, size, FL_NONE);
849
850         gendata(original_buf, good_buf, offset, size);
851         if (file_size < offset + size) {
852                 if (file_size < offset)
853                         memset(good_buf + file_size, '\0', offset - file_size);
854                 file_size = offset + size;
855                 if (lite) {
856                         warn("Lite file size bug in fsx!");
857                         report_failure(200);
858                 }
859         }
860
861         if (testcalls <= simulatedopcount)
862                 return;
863
864         if (!quiet &&
865                 ((progressinterval && testcalls % progressinterval == 0) ||
866                        (debug &&
867                        (monitorstart == -1 ||
868                         (offset + size > monitorstart &&
869                         (monitorend == -1 || offset <= monitorend))))))
870                 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
871                     offset, offset + size - 1, size);
872
873         if (file_size > cur_filesize) {
874                 if (ftruncate(fd, file_size) == -1) {
875                         prterr("domapwrite: ftruncate");
876                         exit(201);
877                 }
878         }
879         pg_offset = offset & PAGE_MASK;
880         map_size  = pg_offset + size;
881
882         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
883                               MAP_FILE | MAP_SHARED, fd,
884                               (off_t)(offset - pg_offset))) == (char *)-1) {
885                 prterr("domapwrite: mmap");
886                 report_failure(202);
887         }
888         memcpy(p + pg_offset, good_buf + offset, size);
889         if (msync(p, map_size, MS_SYNC) != 0) {
890                 prterr("domapwrite: msync");
891                 report_failure(203);
892         }
893
894         check_eofpage("Write", offset, p, size);
895
896         if (munmap(p, map_size) != 0) {
897                 prterr("domapwrite: munmap");
898                 report_failure(204);
899         }
900 }
901
902
903 void
904 dotruncate(unsigned size)
905 {
906         int oldsize = file_size;
907
908         size -= size % truncbdy;
909         if (size > biggest) {
910                 biggest = size;
911                 if (!quiet && testcalls > simulatedopcount)
912                         prt("truncating to largest ever: 0x%x\n", size);
913         }
914
915         log4(OP_TRUNCATE, 0, size, FL_NONE);
916
917         if (size > file_size)
918                 memset(good_buf + file_size, '\0', size - file_size);
919         file_size = size;
920
921         if (testcalls <= simulatedopcount)
922                 return;
923         
924         if ((progressinterval && testcalls % progressinterval == 0) ||
925             (debug && (monitorstart == -1 || monitorend == -1 ||
926                       size <= monitorend)))
927                 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
928         if (ftruncate(fd, (off_t)size) == -1) {
929                 prt("ftruncate1: %x\n", size);
930                 prterr("dotruncate: ftruncate");
931                 report_failure(160);
932         }
933 }
934
935 #ifdef FALLOC_FL_PUNCH_HOLE
936 void
937 do_punch_hole(unsigned offset, unsigned length)
938 {
939         unsigned end_offset;
940         int max_offset = 0;
941         int max_len = 0;
942         int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
943
944         if (length == 0) {
945                 if (!quiet && testcalls > simulatedopcount)
946                         prt("skipping zero length punch hole\n");
947                 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
948                 return;
949         }
950
951         if (file_size <= (loff_t)offset) {
952                 if (!quiet && testcalls > simulatedopcount)
953                         prt("skipping hole punch off the end of the file\n");
954                 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
955                 return;
956         }
957
958         end_offset = offset + length;
959
960         log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
961
962         if (testcalls <= simulatedopcount)
963                 return;
964
965         if ((progressinterval && testcalls % progressinterval == 0) ||
966             (debug && (monitorstart == -1 || monitorend == -1 ||
967                       end_offset <= monitorend))) {
968                 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
969                         offset, offset+length, length);
970         }
971         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
972                 prt("punch hole: 0x%x to 0x%x\n", offset, offset + length);
973                 prterr("do_punch_hole: fallocate");
974                 report_failure(161);
975         }
976
977
978         max_offset = offset < file_size ? offset : file_size;
979         max_len = max_offset + length <= file_size ? length :
980                         file_size - max_offset;
981         memset(good_buf + max_offset, '\0', max_len);
982 }
983
984 #else
985 void
986 do_punch_hole(unsigned offset, unsigned length)
987 {
988         return;
989 }
990 #endif
991
992 #ifdef FALLOC_FL_ZERO_RANGE
993 void
994 do_zero_range(unsigned offset, unsigned length, int keep_size)
995 {
996         unsigned end_offset;
997         int mode = FALLOC_FL_ZERO_RANGE;
998
999         if (length == 0) {
1000                 if (!quiet && testcalls > simulatedopcount)
1001                         prt("skipping zero length zero range\n");
1002                 log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
1003                      (keep_size ? FL_KEEP_SIZE : FL_NONE));
1004                 return;
1005         }
1006
1007         end_offset = keep_size ? 0 : offset + length;
1008
1009         if (end_offset > biggest) {
1010                 biggest = end_offset;
1011                 if (!quiet && testcalls > simulatedopcount)
1012                         prt("zero_range to largest ever: 0x%x\n", end_offset);
1013         }
1014
1015         /*
1016          * last arg matches fallocate string array index in logdump:
1017          *      0: allocate past EOF
1018          *      1: extending prealloc
1019          *      2: interior prealloc
1020          */
1021         log4(OP_ZERO_RANGE, offset, length,
1022              keep_size ? FL_KEEP_SIZE : FL_NONE);
1023
1024         if (testcalls <= simulatedopcount)
1025                 return;
1026
1027         if ((progressinterval && testcalls % progressinterval == 0) ||
1028             (debug && (monitorstart == -1 || monitorend == -1 ||
1029                       end_offset <= monitorend))) {
1030                 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1031                         offset, offset+length, length);
1032         }
1033         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1034                 prt("zero range: 0x%x to 0x%x\n", offset, offset + length);
1035                 prterr("do_zero_range: fallocate");
1036                 report_failure(161);
1037         }
1038
1039         memset(good_buf + offset, '\0', length);
1040 }
1041
1042 #else
1043 void
1044 do_zero_range(unsigned offset, unsigned length, int keep_size)
1045 {
1046         return;
1047 }
1048 #endif
1049
1050 #ifdef FALLOC_FL_COLLAPSE_RANGE
1051 void
1052 do_collapse_range(unsigned offset, unsigned length)
1053 {
1054         unsigned end_offset;
1055         int mode = FALLOC_FL_COLLAPSE_RANGE;
1056
1057         if (length == 0) {
1058                 if (!quiet && testcalls > simulatedopcount)
1059                         prt("skipping zero length collapse range\n");
1060                 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1061                 return;
1062         }
1063
1064         end_offset = offset + length;
1065         if ((loff_t)end_offset >= file_size) {
1066                 if (!quiet && testcalls > simulatedopcount)
1067                         prt("skipping collapse range behind EOF\n");
1068                 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1069                 return;
1070         }
1071
1072         log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1073
1074         if (testcalls <= simulatedopcount)
1075                 return;
1076
1077         if ((progressinterval && testcalls % progressinterval == 0) ||
1078             (debug && (monitorstart == -1 || monitorend == -1 ||
1079                       end_offset <= monitorend))) {
1080                 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1081                         offset, offset+length, length);
1082         }
1083         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1084                 prt("collapse range: 0x%x to 0x%x\n", offset, offset + length);
1085                 prterr("do_collapse_range: fallocate");
1086                 report_failure(161);
1087         }
1088
1089         memmove(good_buf + offset, good_buf + end_offset,
1090                 file_size - end_offset);
1091         file_size -= length;
1092 }
1093
1094 #else
1095 void
1096 do_collapse_range(unsigned offset, unsigned length)
1097 {
1098         return;
1099 }
1100 #endif
1101
1102 #ifdef FALLOC_FL_INSERT_RANGE
1103 void
1104 do_insert_range(unsigned offset, unsigned length)
1105 {
1106         unsigned end_offset;
1107         int mode = FALLOC_FL_INSERT_RANGE;
1108
1109         if (length == 0) {
1110                 if (!quiet && testcalls > simulatedopcount)
1111                         prt("skipping zero length insert range\n");
1112                 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1113                 return;
1114         }
1115
1116         if ((loff_t)offset >= file_size) {
1117                 if (!quiet && testcalls > simulatedopcount)
1118                         prt("skipping insert range behind EOF\n");
1119                 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1120                 return;
1121         }
1122
1123         log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1124
1125         if (testcalls <= simulatedopcount)
1126                 return;
1127
1128         end_offset = offset + length;
1129         if ((progressinterval && testcalls % progressinterval == 0) ||
1130             (debug && (monitorstart == -1 || monitorend == -1 ||
1131                       end_offset <= monitorend))) {
1132                 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1133                         offset, offset+length, length);
1134         }
1135         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1136                 prt("insert range: 0x%x to 0x%x\n", offset, offset + length);
1137                 prterr("do_insert_range: fallocate");
1138                 report_failure(161);
1139         }
1140
1141         memmove(good_buf + end_offset, good_buf + offset,
1142                 file_size - offset);
1143         memset(good_buf + offset, '\0', length);
1144         file_size += length;
1145 }
1146
1147 #else
1148 void
1149 do_insert_range(unsigned offset, unsigned length)
1150 {
1151         return;
1152 }
1153 #endif
1154
1155 #ifdef HAVE_LINUX_FALLOC_H
1156 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1157 void
1158 do_preallocate(unsigned offset, unsigned length, int keep_size)
1159 {
1160         unsigned end_offset;
1161
1162         if (length == 0) {
1163                 if (!quiet && testcalls > simulatedopcount)
1164                         prt("skipping zero length fallocate\n");
1165                 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1166                      (keep_size ? FL_KEEP_SIZE : FL_NONE));
1167                 return;
1168         }
1169
1170         end_offset = keep_size ? 0 : offset + length;
1171
1172         if (end_offset > biggest) {
1173                 biggest = end_offset;
1174                 if (!quiet && testcalls > simulatedopcount)
1175                         prt("fallocating to largest ever: 0x%x\n", end_offset);
1176         }
1177
1178         /*
1179          * last arg matches fallocate string array index in logdump:
1180          *      0: allocate past EOF
1181          *      1: extending prealloc
1182          *      2: interior prealloc
1183          */
1184         log4(OP_FALLOCATE, offset, length,
1185              keep_size ? FL_KEEP_SIZE : FL_NONE);
1186
1187         if (end_offset > file_size) {
1188                 memset(good_buf + file_size, '\0', end_offset - file_size);
1189                 file_size = end_offset;
1190         }
1191
1192         if (testcalls <= simulatedopcount)
1193                 return;
1194         
1195         if ((progressinterval && testcalls % progressinterval == 0) ||
1196             (debug && (monitorstart == -1 || monitorend == -1 ||
1197                       end_offset <= monitorend)))
1198                 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1199                                 offset, offset + length, length);
1200         if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1201                 prt("fallocate: 0x%x to 0x%x\n", offset, offset + length);
1202                 prterr("do_preallocate: fallocate");
1203                 report_failure(161);
1204         }
1205 }
1206 #else
1207 void
1208 do_preallocate(unsigned offset, unsigned length, int keep_size)
1209 {
1210         return;
1211 }
1212 #endif
1213
1214 void
1215 writefileimage()
1216 {
1217         ssize_t iret;
1218
1219         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1220                 prterr("writefileimage: lseek");
1221                 report_failure(171);
1222         }
1223         iret = write(fd, good_buf, file_size);
1224         if ((off_t)iret != file_size) {
1225                 if (iret == -1)
1226                         prterr("writefileimage: write");
1227                 else
1228                         prt("short write: 0x%x bytes instead of 0x%llx\n",
1229                             iret, (unsigned long long)file_size);
1230                 report_failure(172);
1231         }
1232         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1233                 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1234                 prterr("writefileimage: ftruncate");
1235                 report_failure(173);
1236         }
1237 }
1238
1239
1240 void
1241 docloseopen(void)
1242
1243         if (testcalls <= simulatedopcount)
1244                 return;
1245
1246         if (debug)
1247                 prt("%lu close/open\n", testcalls);
1248         if (close(fd)) {
1249                 prterr("docloseopen: close");
1250                 report_failure(180);
1251         }
1252         fd = open(fname, O_RDWR|o_direct, 0);
1253         if (fd < 0) {
1254                 prterr("docloseopen: open");
1255                 report_failure(181);
1256         }
1257 }
1258
1259
1260 #define TRIM_OFF(off, size)                     \
1261 do {                                            \
1262         if (size)                               \
1263                 (off) %= (size);                \
1264         else                                    \
1265                 (off) = 0;                      \
1266 } while (0)
1267
1268 #define TRIM_LEN(off, len, size)                \
1269 do {                                            \
1270         if ((off) + (len) > (size))             \
1271                 (len) = (size) - (off);         \
1272 } while (0)
1273
1274 #define TRIM_OFF_LEN(off, len, size)            \
1275 do {                                            \
1276         TRIM_OFF(off, size);                    \
1277         TRIM_LEN(off, len, size);               \
1278 } while (0)
1279
1280 void
1281 cleanup(int sig)
1282 {
1283         if (sig)
1284                 prt("signal %d\n", sig);
1285         prt("testcalls = %lu\n", testcalls);
1286         exit(sig);
1287 }
1288
1289 static int
1290 read_op(struct log_entry *log_entry)
1291 {
1292         char line[256];
1293
1294         memset(log_entry, 0, sizeof(*log_entry));
1295         log_entry->operation = -1;
1296
1297         while (log_entry->operation == -1) {
1298                 char *str;
1299                 int i;
1300
1301                 do {
1302                         if (!fgets(line, sizeof(line), replayopsf)) {
1303                                 if (feof(replayopsf)) {
1304                                         replayopsf = NULL;
1305                                         return 0;
1306                                 }
1307                                 goto fail;
1308                         }
1309                         str = strtok(line, " \t\n");
1310                 } while (!str);
1311
1312                 if (strcmp(str, "skip") == 0) {
1313                         log_entry->flags |= FL_SKIPPED;
1314                         str = strtok(NULL, " \t\n");
1315                         if (!str)
1316                                 goto fail;
1317                 }
1318                 log_entry->operation = op_code(str);
1319                 if (log_entry->operation == -1)
1320                         goto fail;
1321                 for (i = 0; i < 3; i++) {
1322                         char *end;
1323
1324                         str = strtok(NULL, " \t\n");
1325                         if (!str)
1326                                 goto fail;
1327                         log_entry->args[i] = strtoul(str, &end, 0);
1328                         if (*end)
1329                                 goto fail;
1330                 }
1331                 while ((str = strtok(NULL, " \t\n"))) {
1332                         if (strcmp(str, "keep_size") == 0)
1333                                 log_entry->flags |= FL_KEEP_SIZE;
1334                         else if (strcmp(str, "close_open") == 0)
1335                                 log_entry->flags |= FL_CLOSE_OPEN;
1336                         else if (strcmp(str, "*") == 0)
1337                                 ;  /* overlap marker; ignore */
1338                         else
1339                                 goto fail;
1340                 }
1341         }
1342         return 1;
1343
1344 fail:
1345         fprintf(stderr, "%s: parse error\n", replayops);
1346         fclose(replayopsf);
1347         replayopsf = NULL;
1348         cleanup(100);  /* doesn't return */
1349         return 0;
1350 }
1351
1352
1353 int
1354 test(void)
1355 {
1356         unsigned long   offset;
1357         unsigned long   size;
1358         unsigned long   rv;
1359         unsigned long   op;
1360         int             keep_size = 0;
1361
1362         if (simulatedopcount > 0 && testcalls == simulatedopcount)
1363                 writefileimage();
1364
1365         testcalls++;
1366
1367         if (debugstart > 0 && testcalls >= debugstart)
1368                 debug = 1;
1369
1370         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1371                 prt("%lu...\n", testcalls);
1372
1373         if (replayopsf) {
1374                 struct log_entry log_entry;
1375
1376                 while (read_op(&log_entry)) {
1377                         if (log_entry.flags & FL_SKIPPED) {
1378                                 log4(log_entry.operation,
1379                                      log_entry.args[0], log_entry.args[1],
1380                                      log_entry.flags);
1381                                 continue;
1382                         }
1383
1384                         op = log_entry.operation;
1385                         offset = log_entry.args[0];
1386                         size = log_entry.args[1];
1387                         closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
1388                         keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
1389                         goto have_op;
1390                 }
1391                 return 0;
1392         }
1393
1394         rv = random();
1395         if (closeprob)
1396                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1397
1398         offset = random();
1399         size = maxoplen;
1400         if (randomoplen)
1401                 size = random() % (maxoplen + 1);
1402
1403         /* calculate appropriate op to run */
1404         if (lite)
1405                 op = rv % OP_MAX_LITE;
1406         else
1407                 op = rv % OP_MAX_FULL;
1408
1409         switch(op) {
1410         case OP_TRUNCATE:
1411                 if (!style)
1412                         size = random() % maxfilelen;
1413                 break;
1414         case OP_FALLOCATE:
1415                 if (fallocate_calls && size && keep_size_calls)
1416                         keep_size = random() % 2;
1417                 break;
1418         case OP_ZERO_RANGE:
1419                 if (zero_range_calls && size && keep_size_calls)
1420                         keep_size = random() % 2;
1421                 break;
1422         }
1423
1424 have_op:
1425
1426         switch (op) {
1427         case OP_MAPREAD:
1428                 if (!mapped_reads)
1429                         op = OP_READ;
1430                 break;
1431         case OP_MAPWRITE:
1432                 if (!mapped_writes)
1433                         op = OP_WRITE;
1434                 break;
1435         case OP_FALLOCATE:
1436                 if (!fallocate_calls) {
1437                         log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
1438                         goto out;
1439                 }
1440                 break;
1441         case OP_PUNCH_HOLE:
1442                 if (!punch_hole_calls) {
1443                         log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
1444                         goto out;
1445                 }
1446                 break;
1447         case OP_ZERO_RANGE:
1448                 if (!zero_range_calls) {
1449                         log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
1450                         goto out;
1451                 }
1452                 break;
1453         case OP_COLLAPSE_RANGE:
1454                 if (!collapse_range_calls) {
1455                         log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1456                         goto out;
1457                 }
1458                 break;
1459         case OP_INSERT_RANGE:
1460                 if (!insert_range_calls) {
1461                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1462                         goto out;
1463                 }
1464                 break;
1465         }
1466
1467         switch (op) {
1468         case OP_READ:
1469                 TRIM_OFF_LEN(offset, size, file_size);
1470                 doread(offset, size);
1471                 break;
1472
1473         case OP_WRITE:
1474                 TRIM_OFF_LEN(offset, size, maxfilelen);
1475                 dowrite(offset, size);
1476                 break;
1477
1478         case OP_MAPREAD:
1479                 TRIM_OFF_LEN(offset, size, file_size);
1480                 domapread(offset, size);
1481                 break;
1482
1483         case OP_MAPWRITE:
1484                 TRIM_OFF_LEN(offset, size, maxfilelen);
1485                 domapwrite(offset, size);
1486                 break;
1487
1488         case OP_TRUNCATE:
1489                 dotruncate(size);
1490                 break;
1491
1492         case OP_FALLOCATE:
1493                 TRIM_OFF_LEN(offset, size, maxfilelen);
1494                 do_preallocate(offset, size, keep_size);
1495                 break;
1496
1497         case OP_PUNCH_HOLE:
1498                 TRIM_OFF_LEN(offset, size, file_size);
1499                 do_punch_hole(offset, size);
1500                 break;
1501         case OP_ZERO_RANGE:
1502                 TRIM_OFF_LEN(offset, size, file_size);
1503                 do_zero_range(offset, size, keep_size);
1504                 break;
1505         case OP_COLLAPSE_RANGE:
1506                 TRIM_OFF_LEN(offset, size, file_size - 1);
1507                 offset = offset & ~(block_size - 1);
1508                 size = size & ~(block_size - 1);
1509                 if (size == 0) {
1510                         log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1511                         goto out;
1512                 }
1513                 do_collapse_range(offset, size);
1514                 break;
1515         case OP_INSERT_RANGE:
1516                 TRIM_OFF(offset, file_size);
1517                 TRIM_LEN(file_size, size, maxfilelen);
1518                 offset = offset & ~(block_size - 1);
1519                 size = size & ~(block_size - 1);
1520                 if (size == 0) {
1521                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1522                         goto out;
1523                 }
1524                 if (file_size + size > maxfilelen) {
1525                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1526                         goto out;
1527                 }
1528
1529                 do_insert_range(offset, size);
1530                 break;
1531         default:
1532                 prterr("test: unknown operation");
1533                 report_failure(42);
1534                 break;
1535         }
1536
1537 out:
1538         if (sizechecks && testcalls > simulatedopcount)
1539                 check_size();
1540         if (closeopen)
1541                 docloseopen();
1542         return 1;
1543 }
1544
1545
1546 void
1547 usage(void)
1548 {
1549         fprintf(stdout, "usage: %s",
1550                 "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\
1551         -b opnum: beginning operation number (default 1)\n\
1552         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1553         -d: debug output for all operations\n\
1554         -f flush and invalidate cache after I/O\n\
1555         -l flen: the upper bound on file size (default 262144)\n\
1556         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1557         -n: no verifications of file size\n\
1558         -o oplen: the upper bound on operation size (default 65536)\n\
1559         -p progressinterval: debug output at specified operation interval\n\
1560         -q: quieter operation\n\
1561         -r readbdy: 4096 would make reads page aligned (default 1)\n\
1562         -s style: 1 gives smaller truncates (default 0)\n\
1563         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1564         -w writebdy: 4096 would make writes page aligned (default 1)\n\
1565         -x: preallocate file space before starting, XFS only (default 0)\n\
1566         -y synchronize changes to a file\n"
1567
1568 #ifdef AIO
1569 "       -A: Use the AIO system calls\n"
1570 #endif
1571 "       -D startingop: debug output starting at specified operation\n"
1572 #ifdef HAVE_LINUX_FALLOC_H
1573 "       -F: Do not use fallocate (preallocation) calls\n"
1574 #endif
1575 #ifdef FALLOC_FL_PUNCH_HOLE
1576 "       -H: Do not use punch hole calls\n"
1577 #endif
1578 #ifdef FALLOC_FL_ZERO_RANGE
1579 "       -z: Do not use zero range calls\n"
1580 #endif
1581 #ifdef FALLOC_FL_COLLAPSE_RANGE
1582 "       -C: Do not use collapse range calls\n"
1583 #endif
1584 #ifdef FALLOC_FL_INSERT_RANGE
1585 "       -I: Do not use insert range calls\n"
1586 #endif
1587 "       -L: fsxLite - no file creations & no file size changes\n\
1588         -N numops: total # operations to do (default infinity)\n\
1589         -O: use oplen (see -o flag) for every op (default random)\n\
1590         -P: save .fsxlog .fsxops and .fsxgood files in dirpath (default ./)\n\
1591         -S seed: for random # generator (default 1) 0 gets timestamp\n\
1592         -W: mapped write operations DISabled\n\
1593         -R: read() system calls only (mapped reads disabled)\n\
1594         -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1595         fname: this filename is REQUIRED (no default)\n");
1596         exit(90);
1597 }
1598
1599
1600 int
1601 getnum(char *s, char **e)
1602 {
1603         int ret;
1604
1605         *e = (char *) 0;
1606         ret = strtol(s, e, 0);
1607         if (*e)
1608                 switch (**e) {
1609                 case 'b':
1610                 case 'B':
1611                         ret *= 512;
1612                         *e = *e + 1;
1613                         break;
1614                 case 'k':
1615                 case 'K':
1616                         ret *= 1024;
1617                         *e = *e + 1;
1618                         break;
1619                 case 'm':
1620                 case 'M':
1621                         ret *= 1024*1024;
1622                         *e = *e + 1;
1623                         break;
1624                 case 'w':
1625                 case 'W':
1626                         ret *= 4;
1627                         *e = *e + 1;
1628                         break;
1629                 }
1630         return (ret);
1631 }
1632
1633 #ifdef AIO
1634
1635 #define QSZ     1024
1636 io_context_t    io_ctx;
1637 struct iocb     iocb;
1638
1639 int aio_setup()
1640 {
1641         int ret;
1642         ret = io_queue_init(QSZ, &io_ctx);
1643         if (ret != 0) {
1644                 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1645                         strerror(ret));
1646                 return(-1);
1647         }
1648         return(0);
1649 }
1650
1651 int
1652 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1653 {
1654         struct io_event event;
1655         static struct timespec ts;
1656         struct iocb *iocbs[] = { &iocb };
1657         int ret;
1658         long res;
1659
1660         if (rw == READ) {
1661                 io_prep_pread(&iocb, fd, buf, len, offset);
1662         } else {
1663                 io_prep_pwrite(&iocb, fd, buf, len, offset);
1664         }
1665
1666         ts.tv_sec = 30;
1667         ts.tv_nsec = 0;
1668         ret = io_submit(io_ctx, 1, iocbs);
1669         if (ret != 1) {
1670                 fprintf(stderr, "errcode=%d\n", ret);
1671                 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1672                                 strerror(ret));
1673                 goto out_error;
1674         }
1675
1676         ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1677         if (ret != 1) {
1678                 if (ret == 0)
1679                         fprintf(stderr, "aio_rw: no events available\n");
1680                 else {
1681                         fprintf(stderr, "errcode=%d\n", -ret);
1682                         fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1683                                         strerror(-ret));
1684                 }
1685                 goto out_error;
1686         }
1687         if (len != event.res) {
1688                 /*
1689                  * The b0rked libaio defines event.res as unsigned.
1690                  * However the kernel strucuture has it signed,
1691                  * and it's used to pass negated error value.
1692                  * Till the library is fixed use the temp var.
1693                  */
1694                 res = (long)event.res;
1695                 if (res >= 0)
1696                         fprintf(stderr, "bad io length: %lu instead of %u\n",
1697                                         res, len);
1698                 else {
1699                         fprintf(stderr, "errcode=%ld\n", -res);
1700                         fprintf(stderr, "aio_rw: async io failed: %s\n",
1701                                         strerror(-res));
1702                         ret = res;
1703                         goto out_error;
1704                 }
1705
1706         }
1707         return event.res;
1708
1709 out_error:
1710         /*
1711          * The caller expects error return in traditional libc
1712          * convention, i.e. -1 and the errno set to error.
1713          */
1714         errno = -ret;
1715         return -1;
1716 }
1717
1718 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1719 {
1720         int ret;
1721
1722         if (aio) {
1723                 ret = __aio_rw(rw, fd, buf, len, offset);
1724         } else {
1725                 if (rw == READ)
1726                         ret = read(fd, buf, len);
1727                 else
1728                         ret = write(fd, buf, len);
1729         }
1730         return ret;
1731 }
1732
1733 #endif
1734
1735 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1736
1737 int
1738 __test_fallocate(int mode, const char *mode_str)
1739 {
1740 #ifdef HAVE_LINUX_FALLOC_H
1741         int ret = 0;
1742         if (!lite) {
1743                 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1744                         if(!quiet)
1745                                 fprintf(stderr,
1746                                         "main: filesystem does not support "
1747                                         "fallocate mode %s, disabling!\n",
1748                                         mode_str);
1749                 } else {
1750                         ret = 1;
1751                         if (ftruncate(fd, 0)) {
1752                                 warn("main: ftruncate");
1753                                 exit(132);
1754                         }
1755                 }
1756         }
1757         return ret;
1758 #endif
1759 }
1760
1761 static struct option longopts[] = {
1762         {"replay-ops", required_argument, 0, 256},
1763         { }
1764 };
1765
1766 int
1767 main(int argc, char **argv)
1768 {
1769         int     i, style, ch;
1770         char    *endp;
1771         char goodfile[1024];
1772         char logfile[1024];
1773         int dirpath = 0;
1774         struct stat statbuf;
1775
1776         goodfile[0] = 0;
1777         logfile[0] = 0;
1778
1779         page_size = getpagesize();
1780         page_mask = page_size - 1;
1781         mmap_mask = page_mask;
1782         
1783
1784         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1785
1786         while ((ch = getopt_long(argc, argv,
1787                                  "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ",
1788                                  longopts, NULL)) != EOF)
1789                 switch (ch) {
1790                 case 'b':
1791                         simulatedopcount = getnum(optarg, &endp);
1792                         if (!quiet)
1793                                 fprintf(stdout, "Will begin at operation %ld\n",
1794                                         simulatedopcount);
1795                         if (simulatedopcount == 0)
1796                                 usage();
1797                         simulatedopcount -= 1;
1798                         break;
1799                 case 'c':
1800                         closeprob = getnum(optarg, &endp);
1801                         if (!quiet)
1802                                 fprintf(stdout,
1803                                         "Chance of close/open is 1 in %d\n",
1804                                         closeprob);
1805                         if (closeprob <= 0)
1806                                 usage();
1807                         break;
1808                 case 'd':
1809                         debug = 1;
1810                         break;
1811                 case 'f':
1812                         flush = 1;
1813                         break;
1814                 case 'l':
1815                         maxfilelen = getnum(optarg, &endp);
1816                         if (maxfilelen <= 0)
1817                                 usage();
1818                         break;
1819                 case 'm':
1820                         monitorstart = getnum(optarg, &endp);
1821                         if (monitorstart < 0)
1822                                 usage();
1823                         if (!endp || *endp++ != ':')
1824                                 usage();
1825                         monitorend = getnum(endp, &endp);
1826                         if (monitorend < 0)
1827                                 usage();
1828                         if (monitorend == 0)
1829                                 monitorend = -1; /* aka infinity */
1830                         debug = 1;
1831                 case 'n':
1832                         sizechecks = 0;
1833                         break;
1834                 case 'o':
1835                         maxoplen = getnum(optarg, &endp);
1836                         if (maxoplen <= 0)
1837                                 usage();
1838                         break;
1839                 case 'p':
1840                         progressinterval = getnum(optarg, &endp);
1841                         if (progressinterval == 0)
1842                                 usage();
1843                         break;
1844                 case 'q':
1845                         quiet = 1;
1846                         break;
1847                 case 'r':
1848                         readbdy = getnum(optarg, &endp);
1849                         if (readbdy <= 0)
1850                                 usage();
1851                         break;
1852                 case 's':
1853                         style = getnum(optarg, &endp);
1854                         if (style < 0 || style > 1)
1855                                 usage();
1856                         break;
1857                 case 't':
1858                         truncbdy = getnum(optarg, &endp);
1859                         if (truncbdy <= 0)
1860                                 usage();
1861                         break;
1862                 case 'w':
1863                         writebdy = getnum(optarg, &endp);
1864                         if (writebdy <= 0)
1865                                 usage();
1866                         break;
1867                 case 'x':
1868                         prealloc = 1;
1869                         break;
1870                 case 'y':
1871                         do_fsync = 1;
1872                         break;
1873                 case 'A':
1874                         aio = 1;
1875                         break;
1876                 case 'D':
1877                         debugstart = getnum(optarg, &endp);
1878                         if (debugstart < 1)
1879                                 usage();
1880                         break;
1881                 case 'F':
1882                         fallocate_calls = 0;
1883                         break;
1884                 case 'K':
1885                         keep_size_calls = 0;
1886                         break;
1887                 case 'H':
1888                         punch_hole_calls = 0;
1889                         break;
1890                 case 'z':
1891                         zero_range_calls = 0;
1892                         break;
1893                 case 'C':
1894                         collapse_range_calls = 0;
1895                         break;
1896                 case 'I':
1897                         insert_range_calls = 0;
1898                         break;
1899                 case 'L':
1900                         lite = 1;
1901                         break;
1902                 case 'N':
1903                         numops = getnum(optarg, &endp);
1904                         if (numops < 0)
1905                                 usage();
1906                         break;
1907                 case 'O':
1908                         randomoplen = 0;
1909                         break;
1910                 case 'P':
1911                         strncpy(goodfile, optarg, sizeof(goodfile));
1912                         strcat(goodfile, "/");
1913                         strncpy(logfile, optarg, sizeof(logfile));
1914                         strcat(logfile, "/");
1915                         strncpy(opsfile, optarg, sizeof(logfile));
1916                         strcat(opsfile, "/");
1917                         dirpath = 1;
1918                         break;
1919                 case 'R':
1920                         mapped_reads = 0;
1921                         break;
1922                 case 'S':
1923                         seed = getnum(optarg, &endp);
1924                         if (seed == 0) {
1925                                 seed = time(0) % 10000;
1926                                 seed += (int)getpid();
1927                         }
1928                         if (!quiet)
1929                                 fprintf(stdout, "Seed set to %d\n", seed);
1930                         if (seed < 0)
1931                                 usage();
1932                         break;
1933                 case 'W':
1934                         mapped_writes = 0;
1935                         if (!quiet)
1936                                 fprintf(stdout, "mapped writes DISABLED\n");
1937                         break;
1938                 case 'Z':
1939                         o_direct = O_DIRECT;
1940                         break;
1941                 case 256:  /* --replay-ops */
1942                         replayops = optarg;
1943                         break;
1944                 default:
1945                         usage();
1946                         /* NOTREACHED */
1947                 }
1948         argc -= optind;
1949         argv += optind;
1950         if (argc != 1)
1951                 usage();
1952         fname = argv[0];
1953
1954         signal(SIGHUP,  cleanup);
1955         signal(SIGINT,  cleanup);
1956         signal(SIGPIPE, cleanup);
1957         signal(SIGALRM, cleanup);
1958         signal(SIGTERM, cleanup);
1959         signal(SIGXCPU, cleanup);
1960         signal(SIGXFSZ, cleanup);
1961         signal(SIGVTALRM,       cleanup);
1962         signal(SIGUSR1, cleanup);
1963         signal(SIGUSR2, cleanup);
1964
1965         srandom(seed);
1966         fd = open(fname,
1967                 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1968         if (fd < 0) {
1969                 prterr(fname);
1970                 exit(91);
1971         }
1972         if (fstat(fd, &statbuf)) {
1973                 prterr("check_size: fstat");
1974                 exit(91);
1975         }
1976         block_size = statbuf.st_blksize;
1977 #ifdef XFS
1978         if (prealloc) {
1979                 xfs_flock64_t   resv = { 0 };
1980 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1981                 if (!platform_test_xfs_fd(fd)) {
1982                         prterr(fname);
1983                         fprintf(stderr, "main: cannot prealloc, non XFS\n");
1984                         exit(96);
1985                 }
1986 #endif
1987                 resv.l_len = maxfilelen;
1988                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1989                         prterr(fname);
1990                         exit(97);
1991                 }
1992         }
1993 #endif
1994         strncat(goodfile, dirpath ? basename(fname) : fname, 256);
1995         strcat (goodfile, ".fsxgood");
1996         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1997         if (fsxgoodfd < 0) {
1998                 prterr(goodfile);
1999                 exit(92);
2000         }
2001         strncat(logfile, dirpath ? basename(fname) : fname, 256);
2002         strcat (logfile, ".fsxlog");
2003         fsxlogf = fopen(logfile, "w");
2004         if (fsxlogf == NULL) {
2005                 prterr(logfile);
2006                 exit(93);
2007         }
2008         strncat(opsfile, dirpath ? basename(fname) : fname, 256);
2009         strcat(opsfile, ".fsxops");
2010         unlink(opsfile);
2011
2012         if (replayops) {
2013                 replayopsf = fopen(replayops, "r");
2014                 if (!replayopsf) {
2015                         prterr(replayops);
2016                         exit(93);
2017                 }
2018         }
2019
2020 #ifdef AIO
2021         if (aio) 
2022                 aio_setup();
2023 #endif
2024
2025         if (lite) {
2026                 off_t ret;
2027                 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
2028                 if (file_size == (off_t)-1) {
2029                         prterr(fname);
2030                         warn("main: lseek eof");
2031                         exit(94);
2032                 }
2033                 ret = lseek(fd, (off_t)0, SEEK_SET);
2034                 if (ret == (off_t)-1) {
2035                         prterr(fname);
2036                         warn("main: lseek 0");
2037                         exit(95);
2038                 }
2039         }
2040         original_buf = (char *) malloc(maxfilelen);
2041         for (i = 0; i < maxfilelen; i++)
2042                 original_buf[i] = random() % 256;
2043         good_buf = (char *) malloc(maxfilelen + writebdy);
2044         good_buf = round_ptr_up(good_buf, writebdy, 0);
2045         memset(good_buf, '\0', maxfilelen);
2046         temp_buf = (char *) malloc(maxoplen + readbdy);
2047         temp_buf = round_ptr_up(temp_buf, readbdy, 0);
2048         memset(temp_buf, '\0', maxoplen);
2049         if (lite) {     /* zero entire existing file */
2050                 ssize_t written;
2051
2052                 written = write(fd, good_buf, (size_t)maxfilelen);
2053                 if (written != maxfilelen) {
2054                         if (written == -1) {
2055                                 prterr(fname);
2056                                 warn("main: error on write");
2057                         } else
2058                                 warn("main: short write, 0x%x bytes instead "
2059                                         "of 0x%lx\n",
2060                                         (unsigned)written,
2061                                         maxfilelen);
2062                         exit(98);
2063                 }
2064         } else 
2065                 check_trunc_hack();
2066
2067         if (fallocate_calls)
2068                 fallocate_calls = test_fallocate(0);
2069         if (keep_size_calls)
2070                 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
2071         if (punch_hole_calls)
2072                 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2073         if (zero_range_calls)
2074                 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
2075         if (collapse_range_calls)
2076                 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
2077         if (insert_range_calls)
2078                 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
2079
2080         while (numops == -1 || numops--)
2081                 if (!test())
2082                         break;
2083
2084         if (close(fd)) {
2085                 prterr("close");
2086                 report_failure(99);
2087         }
2088         prt("All %lu operations completed A-OK!\n", testcalls);
2089
2090         exit(0);
2091         return 0;
2092 }