fsx: increase number of logged operations
[xfstests-dev.git] / ltp / fsx.c
index ebe532494b90277062a983bd96ed9ba971c8998d..3709419b197deb69900d09c57f2ff4eec20b4b12 100644 (file)
--- a/ltp/fsx.c
+++ b/ltp/fsx.c
@@ -9,6 +9,8 @@
  *     Rewritten 8/98 by Conrad Minshall.
  *
  *     Small changes to work under Linux -- davej.
+ *
+ *     Checks for mmap last-page zero fill.
  */
 
 #include "global.h"
@@ -23,6 +25,7 @@
 #endif
 #include <signal.h>
 #include <stdio.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
@@ -46,23 +49,57 @@ struct log_entry {
        int     args[3];
 };
 
-#define        LOGSIZE 1000
+#define        LOGSIZE 10000
 
 struct log_entry       oplog[LOGSIZE]; /* the log */
 int                    logptr = 0;     /* current position in log */
 int                    logcount = 0;   /* total ops */
 
 /*
- *     Define operations
+ * The operation matrix is complex due to conditional execution of different
+ * features. Hence when we come to deciding what operation to run, we need to
+ * be careful in how we select the different operations. The active operations
+ * are mapped to numbers as follows:
+ *
+ *             lite    !lite
+ * READ:       0       0
+ * WRITE:      1       1
+ * MAPREAD:    2       2
+ * MAPWRITE:   3       3
+ * TRUNCATE:   -       4
+ * FALLOCATE:  -       5
+ * PUNCH HOLE: -       6
+ * ZERO RANGE: -       7
+ *
+ * When mapped read/writes are disabled, they are simply converted to normal
+ * reads and writes. When fallocate/fpunch calls are disabled, they are
+ * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
+ * the operation selction matrix, as does the OP_CLOSEOPEN which is an
+ * operation modifier rather than an operation in itself.
+ *
+ * Because of the "lite" version, we also need to have different "maximum
+ * operation" defines to allow the ops to be selected correctly based on the
+ * mode being run.
  */
 
-#define        OP_READ         1
-#define OP_WRITE       2
-#define OP_TRUNCATE    3
-#define OP_CLOSEOPEN   4
-#define OP_MAPREAD     5
-#define OP_MAPWRITE    6
-#define OP_SKIPPED     7
+/* common operations */
+#define        OP_READ         0
+#define OP_WRITE       1
+#define OP_MAPREAD     2
+#define OP_MAPWRITE    3
+#define OP_MAX_LITE    4
+
+/* !lite operations */
+#define OP_TRUNCATE            4
+#define OP_FALLOCATE           5
+#define OP_PUNCH_HOLE          6
+#define OP_ZERO_RANGE          7
+#define OP_COLLAPSE_RANGE      8
+#define OP_MAX_FULL            9
+
+/* operation modifiers */
+#define OP_CLOSEOPEN   100
+#define OP_SKIPPED     101
 
 #undef PAGE_SIZE
 #define PAGE_SIZE       getpagesize()
@@ -75,6 +112,7 @@ char *temp_buf;                      /* a pointer to the current data */
 char   *fname;                         /* name of our test file */
 int    fd;                             /* fd for our test file */
 
+blksize_t      block_size = 0;
 off_t          file_size = 0;
 off_t          biggest = 0;
 char           state[256];
@@ -103,6 +141,10 @@ long       numops = -1;                    /* -N flag */
 int    randomoplen = 1;                /* -O flag disables it */
 int    seed = 1;                       /* -S flag */
 int     mapped_writes = 1;              /* -W flag disables */
+int     fallocate_calls = 1;            /* -F flag disables */
+int     punch_hole_calls = 1;           /* -H flag disables */
+int     zero_range_calls = 1;           /* -z flag disables */
+int    collapse_range_calls = 1;       /* -C flag disables */
 int    mapped_reads = 1;               /* -R flag disables it */
 int    fsxgoodfd = 0;
 int    o_direct;                       /* -Z */
@@ -126,7 +168,7 @@ FILE *      fsxlogf = NULL;
 int badoff = -1;
 int closeopen = 0;
 
-static void *round_up(void *ptr, unsigned long align, unsigned long offset)
+static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
 {
        unsigned long ret = (unsigned long)ptr;
 
@@ -200,6 +242,7 @@ logdump(void)
 {
        int     i, count, down;
        struct log_entry        *lp;
+       char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
 
        prt("LOG DUMP (%d total operations):\n", logcount);
        if (logcount < LOGSIZE) {
@@ -213,14 +256,14 @@ logdump(void)
                int opnum;
 
                opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
-               prt("%d(%d mod 256): ", opnum, opnum%256);
+               prt("%d(%3d mod 256): ", opnum, opnum%256);
                lp = &oplog[i];
                if ((closeopen = lp->operation < 0))
                        lp->operation = ~ lp->operation;
                        
                switch (lp->operation) {
                case OP_MAPREAD:
-                       prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
+                       prt("MAPREAD  0x%x thru 0x%x\t(0x%x bytes)",
                            lp->args[0], lp->args[0] + lp->args[1] - 1,
                            lp->args[1]);
                        if (badoff >= lp->args[0] && badoff <
@@ -236,7 +279,7 @@ logdump(void)
                                prt("\t******WWWW");
                        break;
                case OP_READ:
-                       prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
+                       prt("READ     0x%x thru 0x%x\t(0x%x bytes)",
                            lp->args[0], lp->args[0] + lp->args[1] - 1,
                            lp->args[1]);
                        if (badoff >= lp->args[0] &&
@@ -244,7 +287,7 @@ logdump(void)
                                prt("\t***RRRR***");
                        break;
                case OP_WRITE:
-                       prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
+                       prt("WRITE    0x%x thru 0x%x\t(0x%x bytes)",
                            lp->args[0], lp->args[0] + lp->args[1] - 1,
                            lp->args[1]);
                        if (lp->args[0] > lp->args[2])
@@ -263,6 +306,39 @@ logdump(void)
                            badoff < lp->args[!!down])
                                prt("\t******WWWW");
                        break;
+               case OP_FALLOCATE:
+                       /* 0: offset 1: length 2: where alloced */
+                       prt("FALLOC   0x%x thru 0x%x\t(0x%x bytes) %s",
+                               lp->args[0], lp->args[0] + lp->args[1],
+                               lp->args[1], falloc_type[lp->args[2]]);
+                       if (badoff >= lp->args[0] &&
+                           badoff < lp->args[0] + lp->args[1])
+                               prt("\t******FFFF");
+                       break;
+               case OP_PUNCH_HOLE:
+                       prt("PUNCH    0x%x thru 0x%x\t(0x%x bytes)",
+                           lp->args[0], lp->args[0] + lp->args[1] - 1,
+                           lp->args[1]);
+                       if (badoff >= lp->args[0] && badoff <
+                                                    lp->args[0] + lp->args[1])
+                               prt("\t******PPPP");
+                       break;
+               case OP_ZERO_RANGE:
+                       prt("ZERO     0x%x thru 0x%x\t(0x%x bytes)",
+                           lp->args[0], lp->args[0] + lp->args[1] - 1,
+                           lp->args[1]);
+                       if (badoff >= lp->args[0] && badoff <
+                                                    lp->args[0] + lp->args[1])
+                               prt("\t******ZZZZ");
+                       break;
+               case OP_COLLAPSE_RANGE:
+                       prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
+                           lp->args[0], lp->args[0] + lp->args[1] - 1,
+                           lp->args[1]);
+                       if (badoff >= lp->args[0] && badoff <
+                                                    lp->args[0] + lp->args[1])
+                               prt("\t******CCCC");
+                       break;
                case OP_SKIPPED:
                        prt("SKIPPED (no operation)");
                        break;
@@ -294,11 +370,11 @@ save_buffer(char *buffer, off_t bufferlength, int fd)
                exit(67);
        }
        if (lite) {
-               off_t size_by_seek = lseek(fd, (off_t)0, L_XTND);
+               off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
                if (size_by_seek == (off_t)-1)
                        prterr("save_buffer: lseek eof");
                else if (bufferlength > size_by_seek) {
-                       warn("save_buffer: .fsxgood file too short... will save 0x%qx bytes instead of 0x%qx\n", (unsigned long long)size_by_seek,
+                       warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
                             (unsigned long long)bufferlength);
                        bufferlength = size_by_seek;
                }
@@ -313,7 +389,7 @@ save_buffer(char *buffer, off_t bufferlength, int fd)
                if (byteswritten == -1)
                        prterr("save_buffer write");
                else
-                       warn("save_buffer: short write, 0x%x bytes instead of 0x%qx\n",
+                       warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
                             (unsigned)byteswritten,
                             (unsigned long long)bufferlength);
        }
@@ -350,7 +426,7 @@ check_buffers(unsigned offset, unsigned size)
        unsigned op = 0;
        unsigned bad = 0;
 
-       if (bcmp(good_buf + offset, temp_buf, size) != 0) {
+       if (memcmp(good_buf + offset, temp_buf, size) != 0) {
                prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
                    offset, size, fname);
                prt("OFFSET\tGOOD\tBAD\tRANGE\n");
@@ -395,9 +471,9 @@ check_size(void)
                prterr("check_size: fstat");
                statbuf.st_size = -1;
        }
-       size_by_seek = lseek(fd, (off_t)0, L_XTND);
+       size_by_seek = lseek(fd, (off_t)0, SEEK_END);
        if (file_size != statbuf.st_size || file_size != size_by_seek) {
-               prt("Size error: expected 0x%qx stat 0x%qx seek 0x%qx\n",
+               prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
                    (unsigned long long)file_size,
                    (unsigned long long)statbuf.st_size,
                    (unsigned long long)size_by_seek);
@@ -503,6 +579,33 @@ doread(unsigned offset, unsigned size)
 }
 
 
+void
+check_eofpage(char *s, unsigned offset, char *p, int size)
+{
+       unsigned long last_page, should_be_zero;
+
+       if (offset + size <= (file_size & ~page_mask))
+               return;
+       /*
+        * we landed in the last page of the file
+        * test to make sure the VM system provided 0's 
+        * beyond the true end of the file mapping
+        * (as required by mmap def in 1996 posix 1003.1)
+        */
+       last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
+
+       for (should_be_zero = last_page + (file_size & page_mask);
+            should_be_zero < last_page + page_size;
+            should_be_zero++)
+               if (*(char *)should_be_zero) {
+                       prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
+                           s, file_size - 1, should_be_zero & page_mask,
+                           short_at(should_be_zero));
+                       report_failure(205);
+               }
+}
+
+
 void
 domapread(unsigned offset, unsigned size)
 {
@@ -547,6 +650,9 @@ domapread(unsigned offset, unsigned size)
                report_failure(190);
        }
        memcpy(temp_buf, p + pg_offset, size);
+
+       check_eofpage("Read", offset, p, size);
+
        if (munmap(p, map_size) != 0) {
                prterr("domapread: munmap");
                report_failure(191);
@@ -589,7 +695,7 @@ dowrite(unsigned offset, unsigned size)
        gendata(original_buf, good_buf, offset, size);
        if (file_size < offset + size) {
                if (file_size < offset)
-                       bzero(good_buf + file_size, offset - file_size);
+                       memset(good_buf + file_size, '\0', offset - file_size);
                file_size = offset + size;
                if (lite) {
                        warn("Lite file size bug in fsx!");
@@ -656,7 +762,7 @@ domapwrite(unsigned offset, unsigned size)
        gendata(original_buf, good_buf, offset, size);
        if (file_size < offset + size) {
                if (file_size < offset)
-                       bzero(good_buf + file_size, offset - file_size);
+                       memset(good_buf + file_size, '\0', offset - file_size);
                file_size = offset + size;
                if (lite) {
                        warn("Lite file size bug in fsx!");
@@ -692,10 +798,13 @@ domapwrite(unsigned offset, unsigned size)
                report_failure(202);
        }
        memcpy(p + pg_offset, good_buf + offset, size);
-       if (msync(p, map_size, 0) != 0) {
+       if (msync(p, map_size, MS_SYNC) != 0) {
                prterr("domapwrite: msync");
                report_failure(203);
        }
+
+       check_eofpage("Write", offset, p, size);
+
        if (munmap(p, map_size) != 0) {
                prterr("domapwrite: munmap");
                report_failure(204);
@@ -718,7 +827,7 @@ dotruncate(unsigned size)
        log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
 
        if (size > file_size)
-               bzero(good_buf + file_size, size - file_size);
+               memset(good_buf + file_size, '\0', size - file_size);
        file_size = size;
 
        if (testcalls <= simulatedopcount)
@@ -735,6 +844,233 @@ dotruncate(unsigned size)
        }
 }
 
+#ifdef FALLOC_FL_PUNCH_HOLE
+void
+do_punch_hole(unsigned offset, unsigned length)
+{
+       unsigned end_offset;
+       int max_offset = 0;
+       int max_len = 0;
+       int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
+
+       if (length == 0) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping zero length punch hole\n");
+                       log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
+               return;
+       }
+
+       if (file_size <= (loff_t)offset) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping hole punch off the end of the file\n");
+                       log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
+               return;
+       }
+
+       end_offset = offset + length;
+
+       log4(OP_PUNCH_HOLE, offset, length, 0);
+
+       if (testcalls <= simulatedopcount)
+               return;
+
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                     end_offset <= monitorend))) {
+               prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
+                       offset, offset+length, length);
+       }
+       if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
+               prt("%punch hole: %x to %x\n", offset, length);
+               prterr("do_punch_hole: fallocate");
+               report_failure(161);
+       }
+
+
+       max_offset = offset < file_size ? offset : file_size;
+       max_len = max_offset + length <= file_size ? length :
+                       file_size - max_offset;
+       memset(good_buf + max_offset, '\0', max_len);
+}
+
+#else
+void
+do_punch_hole(unsigned offset, unsigned length)
+{
+       return;
+}
+#endif
+
+#ifdef FALLOC_FL_ZERO_RANGE
+void
+do_zero_range(unsigned offset, unsigned length)
+{
+       unsigned end_offset;
+       int mode = FALLOC_FL_ZERO_RANGE;
+       int keep_size;
+
+       if (length == 0) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping zero length zero range\n");
+                       log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
+               return;
+       }
+
+       keep_size = random() % 2;
+
+       end_offset = keep_size ? 0 : offset + length;
+
+       if (end_offset > biggest) {
+               biggest = end_offset;
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("zero_range to largest ever: 0x%x\n", end_offset);
+       }
+
+       /*
+        * last arg matches fallocate string array index in logdump:
+        *      0: allocate past EOF
+        *      1: extending prealloc
+        *      2: interior prealloc
+        */
+       log4(OP_ZERO_RANGE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
+
+       if (testcalls <= simulatedopcount)
+               return;
+
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                     end_offset <= monitorend))) {
+               prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
+                       offset, offset+length, length);
+       }
+       if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
+               prt("%pzero range: %x to %x\n", offset, length);
+               prterr("do_zero_range: fallocate");
+               report_failure(161);
+       }
+
+       memset(good_buf + offset, '\0', length);
+}
+
+#else
+void
+do_zero_range(unsigned offset, unsigned length)
+{
+       return;
+}
+#endif
+
+#ifdef FALLOC_FL_COLLAPSE_RANGE
+void
+do_collapse_range(unsigned offset, unsigned length)
+{
+       unsigned end_offset;
+       int mode = FALLOC_FL_COLLAPSE_RANGE;
+
+       if (length == 0) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping zero length collapse range\n");
+               log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
+               return;
+       }
+
+       end_offset = offset + length;
+       if ((loff_t)end_offset >= file_size) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping collapse range behind EOF\n");
+               log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
+               return;
+       }
+
+       log4(OP_COLLAPSE_RANGE, offset, length, 0);
+
+       if (testcalls <= simulatedopcount)
+               return;
+
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                     end_offset <= monitorend))) {
+               prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
+                       offset, offset+length, length);
+       }
+       if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
+               prt("collapse range: %x to %x\n", offset, length);
+               prterr("do_collapse_range: fallocate");
+               report_failure(161);
+       }
+
+       memmove(good_buf + offset, good_buf + end_offset,
+               file_size - end_offset);
+       file_size -= length;
+}
+
+#else
+void
+do_collapse_range(unsigned offset, unsigned length)
+{
+       return;
+}
+#endif
+
+#ifdef HAVE_LINUX_FALLOC_H
+/* fallocate is basically a no-op unless extending, then a lot like a truncate */
+void
+do_preallocate(unsigned offset, unsigned length)
+{
+       unsigned end_offset;
+       int keep_size;
+
+        if (length == 0) {
+                if (!quiet && testcalls > simulatedopcount)
+                        prt("skipping zero length fallocate\n");
+                log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
+                return;
+        }
+
+       keep_size = random() % 2;
+
+       end_offset = keep_size ? 0 : offset + length;
+
+       if (end_offset > biggest) {
+               biggest = end_offset;
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("fallocating to largest ever: 0x%x\n", end_offset);
+       }
+
+       /*
+        * last arg matches fallocate string array index in logdump:
+        *      0: allocate past EOF
+        *      1: extending prealloc
+        *      2: interior prealloc
+        */
+       log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
+
+       if (end_offset > file_size) {
+               memset(good_buf + file_size, '\0', end_offset - file_size);
+               file_size = end_offset;
+       }
+
+       if (testcalls <= simulatedopcount)
+               return;
+       
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                     end_offset <= monitorend)))
+               prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
+                               offset, offset + length, length);
+       if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
+               prt("fallocate: %x to %x\n", offset, length);
+               prterr("do_preallocate: fallocate");
+               report_failure(161);
+       }
+}
+#else
+void
+do_preallocate(unsigned offset, unsigned length)
+{
+       return;
+}
+#endif
 
 void
 writefileimage()
@@ -750,12 +1086,12 @@ writefileimage()
                if (iret == -1)
                        prterr("writefileimage: write");
                else
-                       prt("short write: 0x%x bytes instead of 0x%qx\n",
+                       prt("short write: 0x%x bytes instead of 0x%llx\n",
                            iret, (unsigned long long)file_size);
                report_failure(172);
        }
        if (lite ? 0 : ftruncate(fd, file_size) == -1) {
-               prt("ftruncate2: %qx\n", (unsigned long long)file_size);
+               prt("ftruncate2: %llx\n", (unsigned long long)file_size);
                prterr("writefileimage: ftruncate");
                report_failure(173);
        }
@@ -781,6 +1117,15 @@ docloseopen(void)
        }
 }
 
+#define TRIM_OFF_LEN(off, len, size)   \
+do {                                   \
+       if (size)                       \
+               (off) %= (size);        \
+       else                            \
+               (off) = 0;              \
+       if ((off) + (len) > (size))     \
+               (len) = (size) - (off); \
+} while (0)
 
 void
 test(void)
@@ -788,12 +1133,7 @@ test(void)
        unsigned long   offset;
        unsigned long   size = maxoplen;
        unsigned long   rv = random();
-       unsigned long   op = rv % (3 + !lite + mapped_writes);
-
-        /* turn off the map read if necessary */
-
-        if (op == 2 && !mapped_reads)
-            op = 0;
+       unsigned long   op;
 
        if (simulatedopcount > 0 && testcalls == simulatedopcount)
                writefileimage();
@@ -809,44 +1149,108 @@ test(void)
        if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
                prt("%lu...\n", testcalls);
 
-       /*
-        * READ:        op = 0
-        * WRITE:       op = 1
-        * MAPREAD:     op = 2
-        * TRUNCATE:    op = 3
-        * MAPWRITE:    op = 3 or 4
-        */
-       if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
-               dotruncate(random() % maxfilelen);
-       else {
-               if (randomoplen)
-                       size = random() % (maxoplen+1);
-               if (lite ? 0 : op == 3)
-                       dotruncate(size);
-               else {
-                       offset = random();
-                       if (op == 1 || op == (lite ? 3 : 4)) {
-                               offset %= maxfilelen;
-                               if (offset + size > maxfilelen)
-                                       size = maxfilelen - offset;
-                               if (op != 1)
-                                       domapwrite(offset, size);
-                               else
-                                       dowrite(offset, size);
-                       } else {
-                               if (file_size)
-                                       offset %= file_size;
-                               else
-                                       offset = 0;
-                               if (offset + size > file_size)
-                                       size = file_size - offset;
-                               if (op != 0)
-                                       domapread(offset, size);
-                               else
-                                       doread(offset, size);
-                       }
+       offset = random();
+       if (randomoplen)
+               size = random() % (maxoplen + 1);
+
+       /* calculate appropriate op to run */
+       if (lite)
+               op = rv % OP_MAX_LITE;
+       else
+               op = rv % OP_MAX_FULL;
+
+       switch (op) {
+       case OP_MAPREAD:
+               if (!mapped_reads)
+                       op = OP_READ;
+               break;
+       case OP_MAPWRITE:
+               if (!mapped_writes)
+                       op = OP_WRITE;
+               break;
+       case OP_FALLOCATE:
+               if (!fallocate_calls) {
+                       log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
+                       goto out;
+               }
+               break;
+       case OP_PUNCH_HOLE:
+               if (!punch_hole_calls) {
+                       log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
+                       goto out;
+               }
+               break;
+       case OP_ZERO_RANGE:
+               if (!zero_range_calls) {
+                       log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
+                       goto out;
                }
+               break;
+       case OP_COLLAPSE_RANGE:
+               if (!collapse_range_calls) {
+                       log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
+                       goto out;
+               }
+               break;
+       }
+
+       switch (op) {
+       case OP_READ:
+               TRIM_OFF_LEN(offset, size, file_size);
+               doread(offset, size);
+               break;
+
+       case OP_WRITE:
+               TRIM_OFF_LEN(offset, size, maxfilelen);
+               dowrite(offset, size);
+               break;
+
+       case OP_MAPREAD:
+               TRIM_OFF_LEN(offset, size, file_size);
+               domapread(offset, size);
+               break;
+
+       case OP_MAPWRITE:
+               TRIM_OFF_LEN(offset, size, maxfilelen);
+               domapwrite(offset, size);
+               break;
+
+       case OP_TRUNCATE:
+               if (!style)
+                       size = random() % maxfilelen;
+               dotruncate(size);
+               break;
+
+       case OP_FALLOCATE:
+               TRIM_OFF_LEN(offset, size, maxfilelen);
+               do_preallocate(offset, size);
+               break;
+
+       case OP_PUNCH_HOLE:
+               TRIM_OFF_LEN(offset, size, file_size);
+               do_punch_hole(offset, size);
+               break;
+       case OP_ZERO_RANGE:
+               TRIM_OFF_LEN(offset, size, file_size);
+               do_zero_range(offset, size);
+               break;
+       case OP_COLLAPSE_RANGE:
+               TRIM_OFF_LEN(offset, size, file_size - 1);
+               offset = offset & ~(block_size - 1);
+               size = size & ~(block_size - 1);
+               if (size == 0) {
+                       log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
+                       goto out;
+               }
+               do_collapse_range(offset, size);
+               break;
+       default:
+               prterr("test: unknown operation");
+               report_failure(42);
+               break;
        }
+
+out:
        if (sizechecks && testcalls > simulatedopcount)
                check_size();
        if (closeopen)
@@ -869,7 +1273,7 @@ void
 usage(void)
 {
        fprintf(stdout, "usage: %s",
-               "fsx [-dnqxALOWZ] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
+               "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\
        -b opnum: beginning operation number (default 1)\n\
        -c P: 1 in P chance of file close+open at each op (default infinity)\n\
        -d: debug output for all operations\n\
@@ -890,8 +1294,20 @@ usage(void)
 #ifdef AIO
 "      -A: Use the AIO system calls\n"
 #endif
-"      -D startingop: debug output starting at specified operation\n\
-       -L: fsxLite - no file creations & no file size changes\n\
+"      -D startingop: debug output starting at specified operation\n"
+#ifdef HAVE_LINUX_FALLOC_H
+"      -F: Do not use fallocate (preallocation) calls\n"
+#endif
+#ifdef FALLOC_FL_PUNCH_HOLE
+"      -H: Do not use punch hole calls\n"
+#endif
+#ifdef FALLOC_FL_ZERO_RANGE
+"      -z: Do not use zero range calls\n"
+#endif
+#ifdef FALLOC_FL_COLLAPSE_RANGE
+"      -C: Do not use collapse range calls\n"
+#endif
+"      -L: fsxLite - no file creations & no file size changes\n\
        -N numops: total # operations to do (default infinity)\n\
        -O: use oplen (see -o flag) for every op (default random)\n\
        -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
@@ -962,6 +1378,7 @@ __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
        static struct timespec ts;
        struct iocb *iocbs[] = { &iocb };
        int ret;
+       long res;
 
        if (rw == READ) {
                io_prep_pread(&iocb, fd, buf, len, offset);
@@ -976,21 +1393,49 @@ __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
                fprintf(stderr, "errcode=%d\n", ret);
                fprintf(stderr, "aio_rw: io_submit failed: %s\n",
                                strerror(ret));
-               return(-1);
+               goto out_error;
        }
 
        ret = io_getevents(io_ctx, 1, 1, &event, &ts);
        if (ret != 1) {
-               fprintf(stderr, "errcode=%d\n", ret);
-               fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
-                                strerror(ret));
-               return -1;
+               if (ret == 0)
+                       fprintf(stderr, "aio_rw: no events available\n");
+               else {
+                       fprintf(stderr, "errcode=%d\n", -ret);
+                       fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
+                                       strerror(-ret));
+               }
+               goto out_error;
        }
        if (len != event.res) {
-               fprintf(stderr, "bad read length: %lu instead of %u\n",
-                               event.res, len);
+               /*
+                * The b0rked libaio defines event.res as unsigned.
+                * However the kernel strucuture has it signed,
+                * and it's used to pass negated error value.
+                * Till the library is fixed use the temp var.
+                */
+               res = (long)event.res;
+               if (res >= 0)
+                       fprintf(stderr, "bad io length: %lu instead of %u\n",
+                                       res, len);
+               else {
+                       fprintf(stderr, "errcode=%ld\n", -res);
+                       fprintf(stderr, "aio_rw: async io failed: %s\n",
+                                       strerror(-res));
+                       ret = res;
+                       goto out_error;
+               }
+
        }
        return event.res;
+
+out_error:
+       /*
+        * The caller expects error return in traditional libc
+        * convention, i.e. -1 and the errno set to error.
+        */
+       errno = -ret;
+       return -1;
 }
 
 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
@@ -1010,6 +1455,25 @@ int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
 
 #endif
 
+int
+test_fallocate(int mode)
+{
+#ifdef HAVE_LINUX_FALLOC_H
+       int ret = 0;
+       if (!lite) {
+               if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
+                       if(!quiet)
+                               warn("main: filesystem does not support "
+                                    "fallocate mode 0x%x, disabling!\n", mode);
+               } else {
+                       ret = 1;
+                       ftruncate(fd, 0);
+               }
+       }
+       return ret;
+#endif
+}
+
 int
 main(int argc, char **argv)
 {
@@ -1017,6 +1481,7 @@ main(int argc, char **argv)
        char    *endp;
        char goodfile[1024];
        char logfile[1024];
+       struct stat statbuf;
 
        goodfile[0] = 0;
        logfile[0] = 0;
@@ -1028,7 +1493,7 @@ main(int argc, char **argv)
 
        setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
-       while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:LN:OP:RS:WZ"))
+       while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FHzCLN:OP:RS:WZ"))
               != EOF)
                switch (ch) {
                case 'b':
@@ -1122,6 +1587,18 @@ main(int argc, char **argv)
                        if (debugstart < 1)
                                usage();
                        break;
+               case 'F':
+                       fallocate_calls = 0;
+                       break;
+               case 'H':
+                       punch_hole_calls = 0;
+                       break;
+               case 'z':
+                       zero_range_calls = 0;
+                       break;
+               case 'C':
+                       collapse_range_calls = 0;
+                       break;
                case 'L':
                        lite = 1;
                        break;
@@ -1188,6 +1665,11 @@ main(int argc, char **argv)
                prterr(fname);
                exit(91);
        }
+       if (fstat(fd, &statbuf)) {
+               prterr("check_size: fstat");
+               exit(91);
+       }
+       block_size = statbuf.st_blksize;
 #ifdef XFS
        if (prealloc) {
                xfs_flock64_t   resv = { 0 };
@@ -1227,7 +1709,7 @@ main(int argc, char **argv)
 
        if (lite) {
                off_t ret;
-               file_size = maxfilelen = lseek(fd, (off_t)0, L_XTND);
+               file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
                if (file_size == (off_t)-1) {
                        prterr(fname);
                        warn("main: lseek eof");
@@ -1244,11 +1726,11 @@ main(int argc, char **argv)
        for (i = 0; i < maxfilelen; i++)
                original_buf[i] = random() % 256;
        good_buf = (char *) malloc(maxfilelen + writebdy);
-       good_buf = round_up(good_buf, writebdy, 0);
-       bzero(good_buf, maxfilelen);
+       good_buf = round_ptr_up(good_buf, writebdy, 0);
+       memset(good_buf, '\0', maxfilelen);
        temp_buf = (char *) malloc(maxoplen + readbdy);
-       temp_buf = round_up(temp_buf, readbdy, 0);
-       bzero(temp_buf, maxoplen);
+       temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+       memset(temp_buf, '\0', maxoplen);
        if (lite) {     /* zero entire existing file */
                ssize_t written;
 
@@ -1267,6 +1749,16 @@ main(int argc, char **argv)
        } else 
                check_trunc_hack();
 
+       if (fallocate_calls)
+               fallocate_calls = test_fallocate(0);
+       if (punch_hole_calls)
+               punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE |
+                                                 FALLOC_FL_KEEP_SIZE);
+       if (zero_range_calls)
+               zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
+       if (collapse_range_calls)
+               collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
+
        while (numops == -1 || numops--)
                test();