fsx: Add fallocate collapse range operation
[xfstests-dev.git] / ltp / fsx.c
index 074212f484f03efc17eff465ce8683380540da08..47d3ee8d45556f8d3c11d64932a3da0994deac1b 100644 (file)
--- a/ltp/fsx.c
+++ b/ltp/fsx.c
@@ -25,6 +25,7 @@
 #endif
 #include <signal.h>
 #include <stdio.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
@@ -32,9 +33,6 @@
 #ifdef AIO
 #include <libaio.h>
 #endif
-#ifdef FALLOCATE
-#include <linux/falloc.h>
-#endif
 
 #ifndef MAP_FILE
 # define MAP_FILE 0
@@ -71,6 +69,7 @@ int                   logcount = 0;   /* total ops */
  * 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
@@ -91,10 +90,12 @@ int                 logcount = 0;   /* total ops */
 #define OP_MAX_LITE    4
 
 /* !lite operations */
-#define OP_TRUNCATE    4
-#define OP_FALLOCATE   5
-#define OP_PUNCH_HOLE  6
-#define OP_MAX_FULL    7
+#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
@@ -111,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];
@@ -141,6 +143,8 @@ 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 */
@@ -164,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;
 
@@ -252,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 <
@@ -275,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] &&
@@ -283,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])
@@ -304,20 +308,37 @@ logdump(void)
                        break;
                case OP_FALLOCATE:
                        /* 0: offset 1: length 2: where alloced */
-                       prt("FALLOCATE %s\tfrom 0x%x to 0x%x",
-                           falloc_type[lp->args[2]], lp->args[0], lp->args[0] + lp->args[1]);
+                       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 HOLE\t0x%x thru 0x%x\t(0x%x bytes)",
+                       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;
@@ -777,7 +798,7 @@ 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);
        }
@@ -880,7 +901,118 @@ do_punch_hole(unsigned offset, unsigned length)
 }
 #endif
 
-#ifdef FALLOCATE
+#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)
@@ -906,12 +1038,12 @@ do_preallocate(unsigned offset, unsigned length)
        }
 
        /*
-        * last arg:
-        *      1: allocate past EOF
-        *      2: extending prealloc
-        *      3: interior prealloc
+        * 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 ? 1 : 2) : 3);
+       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);
@@ -924,7 +1056,8 @@ do_preallocate(unsigned offset, unsigned length)
        if ((progressinterval && testcalls % progressinterval == 0) ||
            (debug && (monitorstart == -1 || monitorend == -1 ||
                      end_offset <= monitorend)))
-               prt("%lu falloc\tfrom 0x%x to 0x%x\n", testcalls, offset, length);
+               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");
@@ -984,14 +1117,14 @@ docloseopen(void)
        }
 }
 
-#define TRIM_OFF_LEN(off, len, size, zero_offset)      \
+#define TRIM_OFF_LEN(off, len, size)   \
 do {                                   \
-       if (!zero_offset || file_size)  \
-               offset %= size;         \
+       if (size)                       \
+               (off) %= (size);        \
        else                            \
-               offset = 0;             \
-       if (offset + len > size)        \
-               len = size - offset;    \
+               (off) = 0;              \
+       if ((off) + (len) > (size))     \
+               (len) = (size) - (off); \
 } while (0)
 
 void
@@ -1047,26 +1180,38 @@ test(void)
                        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, false);
+               TRIM_OFF_LEN(offset, size, file_size);
                doread(offset, size);
                break;
 
        case OP_WRITE:
-               TRIM_OFF_LEN(offset, size, maxfilelen, true);
+               TRIM_OFF_LEN(offset, size, maxfilelen);
                dowrite(offset, size);
                break;
 
        case OP_MAPREAD:
-               TRIM_OFF_LEN(offset, size, file_size, false);
+               TRIM_OFF_LEN(offset, size, file_size);
                domapread(offset, size);
                break;
 
        case OP_MAPWRITE:
-               TRIM_OFF_LEN(offset, size, maxfilelen, true);
+               TRIM_OFF_LEN(offset, size, maxfilelen);
                domapwrite(offset, size);
                break;
 
@@ -1077,14 +1222,28 @@ test(void)
                break;
 
        case OP_FALLOCATE:
-               TRIM_OFF_LEN(offset, size, maxfilelen, true);
+               TRIM_OFF_LEN(offset, size, maxfilelen);
                do_preallocate(offset, size);
                break;
 
        case OP_PUNCH_HOLE:
-               TRIM_OFF_LEN(offset, size, maxfilelen, true);
+               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);
@@ -1136,11 +1295,17 @@ usage(void)
 "      -A: Use the AIO system calls\n"
 #endif
 "      -D startingop: debug output starting at specified operation\n"
-#ifdef FALLOCATE
+#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"
+"      -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\
@@ -1290,40 +1455,22 @@ int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
 
 #endif
 
-void
-test_fallocate()
+int
+test_fallocate(int mode)
 {
-#ifdef FALLOCATE
-       if (!lite && fallocate_calls) {
-               if (fallocate(fd, 0, 0, 1) && errno == EOPNOTSUPP) {
+#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, disabling\n");
-                       fallocate_calls = 0;
+                               warn("main: filesystem does not support "
+                                    "fallocate mode 0x%x, disabling!\n", mode);
                } else {
+                       ret = 1;
                        ftruncate(fd, 0);
                }
        }
-#else /* ! FALLOCATE */
-       fallocate_calls = 0;
-#endif
-
-}
-
-void
-test_punch_hole()
-{
-#ifdef FALLOC_FL_PUNCH_HOLE
-       if (!lite && punch_hole_calls) {
-               if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                               0, 1) && errno == EOPNOTSUPP) {
-                       if(!quiet)
-                               warn("main: filesystem does not support fallocate punch hole, disabling");
-                       punch_hole_calls = 0;
-               } else
-                       ftruncate(fd, 0);
-       }
-#else /* ! PUNCH HOLE */
-       punch_hole_calls = 0;
+       return ret;
 #endif
 }
 
@@ -1334,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;
@@ -1345,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:FHLN: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':
@@ -1445,6 +1593,12 @@ main(int argc, char **argv)
                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;
@@ -1511,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 };
@@ -1567,10 +1726,10 @@ 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);
+       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);
+       temp_buf = round_ptr_up(temp_buf, readbdy, 0);
        memset(temp_buf, '\0', maxoplen);
        if (lite) {     /* zero entire existing file */
                ssize_t written;
@@ -1590,8 +1749,15 @@ main(int argc, char **argv)
        } else 
                check_trunc_hack();
 
-       test_fallocate();
-       test_punch_hole();
+       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();