common/rc: add _scratch_{u}mount_idmapped() helpers
[xfstests-dev.git] / ltp / fsx.c
index ebe8ecf88342531d41337b30c079c61f04e65be7..cd0bae55aeb887330c27f1c9ca19ffa811b8e87d 100644 (file)
--- a/ltp/fsx.c
+++ b/ltp/fsx.c
 #ifdef AIO
 #include <libaio.h>
 #endif
+#ifdef URING
+#include <liburing.h>
+#endif
+#include <sys/syscall.h>
 
 #ifndef MAP_FILE
 # define MAP_FILE 0
@@ -51,7 +55,8 @@ enum opflags { FL_NONE = 0, FL_SKIPPED = 1, FL_CLOSE_OPEN = 2, FL_KEEP_SIZE = 4
 
 struct log_entry {
        int     operation;
-       int     args[3];
+       int     nr_args;
+       int     args[4];
        enum opflags flags;
 };
 
@@ -67,15 +72,17 @@ int                 logcount = 0;   /* total ops */
  * 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
+ *                     lite    !lite   integrity
+ * READ:               0       0       0
+ * WRITE:              1       1       1
+ * MAPREAD:            2       2       2
+ * MAPWRITE:           3       3       3
+ * TRUNCATE:           -       4       4
+ * FALLOCATE:          -       5       5
+ * PUNCH HOLE:         -       6       6
+ * ZERO RANGE:         -       7       7
+ * COLLAPSE RANGE:     -       8       8
+ * FSYNC:              -       -       9
  *
  * When mapped read/writes are disabled, they are simply converted to normal
  * reads and writes. When fallocate/fpunch calls are disabled, they are
@@ -86,21 +93,30 @@ int                 logcount = 0;   /* total ops */
  * mode being run.
  */
 
-/* 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_INSERT_RANGE        9
-#define OP_MAX_FULL            10
+enum {
+       /* common operations */
+       OP_READ = 0,
+       OP_WRITE,
+       OP_MAPREAD,
+       OP_MAPWRITE,
+       OP_MAX_LITE,
+
+       /* !lite operations */
+       OP_TRUNCATE = OP_MAX_LITE,
+       OP_FALLOCATE,
+       OP_PUNCH_HOLE,
+       OP_ZERO_RANGE,
+       OP_COLLAPSE_RANGE,
+       OP_INSERT_RANGE,
+       OP_CLONE_RANGE,
+       OP_DEDUPE_RANGE,
+       OP_COPY_RANGE,
+       OP_MAX_FULL,
+
+       /* integrity operations */
+       OP_FSYNC = OP_MAX_FULL,
+       OP_MAX_INTEGRITY,
+};
 
 #undef PAGE_SIZE
 #define PAGE_SIZE       getpagesize()
@@ -111,25 +127,31 @@ char      *original_buf;                  /* a pointer to the original data */
 char   *good_buf;                      /* a pointer to the correct data */
 char   *temp_buf;                      /* a pointer to the current data */
 char   *fname;                         /* name of our test file */
+char   *bname;                         /* basename of our test file */
+char   *logdev;                        /* -i flag */
+char   *logid;                         /* -j flag */
+char   dname[1024];                    /* -P flag */
+char   goodfile[PATH_MAX];
+int    dirpath = 0;                    /* -P flag */
 int    fd;                             /* fd for our test file */
 
 blksize_t      block_size = 0;
 off_t          file_size = 0;
 off_t          biggest = 0;
-char           state[256];
-unsigned long  testcalls = 0;          /* calls to function "test" */
+long long      testcalls = 0;          /* calls to function "test" */
 
-unsigned long  simulatedopcount = 0;   /* -b flag */
+long long      simulatedopcount = 0;   /* -b flag */
 int    closeprob = 0;                  /* -c flag */
 int    debug = 0;                      /* -d flag */
-unsigned long  debugstart = 0;         /* -D flag */
+long long      debugstart = 0;         /* -D flag */
+char   filldata = 0;                   /* -g flag */
 int    flush = 0;                      /* -f flag */
 int    do_fsync = 0;                   /* -y flag */
 unsigned long  maxfilelen = 256 * 1024;        /* -l flag */
 int    sizechecks = 1;                 /* -n flag disables them */
 int    maxoplen = 64 * 1024;           /* -o flag */
 int    quiet = 0;                      /* -q flag */
-unsigned long progressinterval = 0;    /* -p flag */
+long long      progressinterval = 0;   /* -p flag */
 int    readbdy = 1;                    /* -r flag */
 int    style = 0;                      /* -s flag */
 int    prealloc = 0;                   /* -x flag */
@@ -138,7 +160,7 @@ int writebdy = 1;                   /* -w flag */
 long   monitorstart = -1;              /* -m flag */
 long   monitorend = -1;                /* -m flag */
 int    lite = 0;                       /* -L flag */
-long   numops = -1;                    /* -N flag */
+long long numops = -1;                 /* -N flag */
 int    randomoplen = 1;                /* -O flag disables it */
 int    seed = 1;                       /* -S flag */
 int     mapped_writes = 1;              /* -W flag disables */
@@ -148,29 +170,32 @@ 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    insert_range_calls = 1;         /* -I flag disables */
-int    mapped_reads = 1;               /* -R flag disables it */
+int    mapped_reads = 1;               /* -R flag disables it */
+int    check_file = 0;                 /* -X flag enables */
+int    clone_range_calls = 1;          /* -J flag disables */
+int    dedupe_range_calls = 1;         /* -B flag disables */
+int    copy_range_calls = 1;           /* -E flag disables */
+int    integrity = 0;                  /* -i flag */
 int    fsxgoodfd = 0;
 int    o_direct;                       /* -Z */
 int    aio = 0;
+int    uring = 0;
+int    mark_nr = 0;
 
 int page_size;
 int page_mask;
 int mmap_mask;
-#ifdef AIO
-int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
+int fsx_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
 #define READ 0
 #define WRITE 1
-#define fsxread(a,b,c,d)       aio_rw(READ, a,b,c,d)
-#define fsxwrite(a,b,c,d)      aio_rw(WRITE, a,b,c,d)
-#else
-#define fsxread(a,b,c,d)       read(a,b,c)
-#define fsxwrite(a,b,c,d)      write(a,b,c)
-#endif
+#define fsxread(a,b,c,d)       fsx_rw(READ, a,b,c,d)
+#define fsxwrite(a,b,c,d)      fsx_rw(WRITE, a,b,c,d)
 
 const char *replayops = NULL;
+const char *recordops = NULL;
 FILE * fsxlogf = NULL;
 FILE * replayopsf = NULL;
-char opsfile[1024];
+char opsfile[PATH_MAX];
 int badoff = -1;
 int closeopen = 0;
 
@@ -184,13 +209,16 @@ static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
 }
 
 void
-vwarnc(int code, const char *fmt, va_list ap) {
-  fprintf(stderr, "fsx: ");
-  if (fmt != NULL) {
-       vfprintf(stderr, fmt, ap);
-       fprintf(stderr, ": ");
-  }
-  fprintf(stderr, "%s\n", strerror(code));
+vwarnc(int code, const char *fmt, va_list ap)
+{
+       if (logid)
+               fprintf(stderr, "%s: ", logid);
+       fprintf(stderr, "fsx: ");
+       if (fmt != NULL) {
+               vfprintf(stderr, fmt, ap);
+               fprintf(stderr, ": ");
+       }
+       fprintf(stderr, "%s\n", strerror(code));
 }
 
 void
@@ -201,20 +229,21 @@ warn(const char * fmt, ...)  {
        va_end(ap);
 }
 
-#define BUF_SIZE 1024
-
 void
 prt(const char *fmt, ...)
 {
        va_list args;
-       char buffer[BUF_SIZE];
 
+       if (logid)
+               fprintf(stdout, "%s: ", logid);
        va_start(args, fmt);
-       vsnprintf(buffer, BUF_SIZE, fmt, args);
+       vfprintf(stdout, fmt, args);
        va_end(args);
-       fprintf(stdout, buffer);
-       if (fsxlogf)
-               fprintf(fsxlogf, buffer);
+       if (fsxlogf) {
+               va_start(args, fmt);
+               vfprintf(fsxlogf, fmt, args);
+               va_end(args);
+       }
 }
 
 void
@@ -235,6 +264,10 @@ static const char *op_names[] = {
        [OP_ZERO_RANGE] = "zero_range",
        [OP_COLLAPSE_RANGE] = "collapse_range",
        [OP_INSERT_RANGE] = "insert_range",
+       [OP_CLONE_RANGE] = "clone_range",
+       [OP_DEDUPE_RANGE] = "dedupe_range",
+       [OP_COPY_RANGE] = "copy_range",
+       [OP_FSYNC] = "fsync",
 };
 
 static const char *op_name(int operation)
@@ -255,6 +288,27 @@ static int op_code(const char *name)
        return -1;
 }
 
+void
+log5(int operation, int arg0, int arg1, int arg2, enum opflags flags)
+{
+       struct log_entry *le;
+
+       le = &oplog[logptr];
+       le->operation = operation;
+       if (closeopen)
+               flags |= FL_CLOSE_OPEN;
+       le->args[0] = arg0;
+       le->args[1] = arg1;
+       le->args[2] = arg2;
+       le->args[3] = file_size;
+       le->nr_args = 4;
+       le->flags = flags;
+       logptr++;
+       logcount++;
+       if (logptr >= LOGSIZE)
+               logptr = 0;
+}
+
 void
 log4(int operation, int arg0, int arg1, enum opflags flags)
 {
@@ -267,6 +321,7 @@ log4(int operation, int arg0, int arg1, enum opflags flags)
        le->args[0] = arg0;
        le->args[1] = arg1;
        le->args[2] = file_size;
+       le->nr_args = 3;
        le->flags = flags;
        logptr++;
        logcount++;
@@ -274,7 +329,6 @@ log4(int operation, int arg0, int arg1, enum opflags flags)
                logptr = 0;
 }
 
-
 void
 logdump(void)
 {
@@ -296,7 +350,7 @@ logdump(void)
                count = LOGSIZE;
        }
        for ( ; count > 0; count--) {
-               bool overlap;
+               bool overlap, overlap2;
                int opnum;
 
                opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
@@ -398,6 +452,51 @@ logdump(void)
                        if (overlap)
                                prt("\t******IIII");
                        break;
+               case OP_CLONE_RANGE:
+                       prt("CLONE 0x%x thru 0x%x\t(0x%x bytes) to 0x%x thru 0x%x",
+                           lp->args[0], lp->args[0] + lp->args[1] - 1,
+                           lp->args[1],
+                           lp->args[2], lp->args[2] + lp->args[1] - 1);
+                       overlap2 = badoff >= lp->args[2] &&
+                                 badoff < lp->args[2] + lp->args[1];
+                       if (overlap && overlap2)
+                               prt("\tJJJJ**JJJJ");
+                       else if (overlap)
+                               prt("\tJJJJ******");
+                       else if (overlap2)
+                               prt("\t******JJJJ");
+                       break;
+               case OP_DEDUPE_RANGE:
+                       prt("DEDUPE 0x%x thru 0x%x\t(0x%x bytes) to 0x%x thru 0x%x",
+                           lp->args[0], lp->args[0] + lp->args[1] - 1,
+                           lp->args[1],
+                           lp->args[2], lp->args[2] + lp->args[1] - 1);
+                       overlap2 = badoff >= lp->args[2] &&
+                                 badoff < lp->args[2] + lp->args[1];
+                       if (overlap && overlap2)
+                               prt("\tBBBB**BBBB");
+                       else if (overlap)
+                               prt("\tBBBB******");
+                       else if (overlap2)
+                               prt("\t******BBBB");
+                       break;
+               case OP_COPY_RANGE:
+                       prt("COPY 0x%x thru 0x%x\t(0x%x bytes) to 0x%x thru 0x%x",
+                           lp->args[0], lp->args[0] + lp->args[1] - 1,
+                           lp->args[1],
+                           lp->args[2], lp->args[2] + lp->args[1] - 1);
+                       overlap2 = badoff >= lp->args[2] &&
+                                 badoff < lp->args[2] + lp->args[1];
+                       if (overlap && overlap2)
+                               prt("\tEEEE**EEEE");
+                       else if (overlap)
+                               prt("\tEEEE******");
+                       else if (overlap2)
+                               prt("\t******EEEE");
+                       break;
+               case OP_FSYNC:
+                       prt("FSYNC");
+                       break;
                default:
                        prt("BOGUS LOG ENTRY (operation code = %d)!",
                            lp->operation);
@@ -413,11 +512,13 @@ logdump(void)
                        i = 0;
 
                if (logopsf) {
+                       int j;
+
                        if (lp->flags & FL_SKIPPED)
                                fprintf(logopsf, "skip ");
-                       fprintf(logopsf, "%s 0x%x 0x%x 0x%x",
-                               op_name(lp->operation),
-                               lp->args[0], lp->args[1], lp->args[2]);
+                       fprintf(logopsf, "%s", op_name(lp->operation));
+                       for (j = 0; j < lp->nr_args; j++)
+                               fprintf(logopsf, " 0x%x", lp->args[j]);
                        if (lp->flags & FL_KEEP_SIZE)
                                fprintf(logopsf, " keep_size");
                        if (lp->flags & FL_CLOSE_OPEN)
@@ -488,8 +589,8 @@ report_failure(int status)
                if (good_buf) {
                        save_buffer(good_buf, file_size, fsxgoodfd);
                        prt("Correct content saved for comparison\n");
-                       prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
-                           fname, fname);
+                       prt("(maybe hexdump \"%s\" vs \"%s\")\n",
+                           fname, goodfile);
                }
                close(fsxgoodfd);
        }
@@ -501,7 +602,44 @@ report_failure(int status)
                                        *(((unsigned char *)(cp)) + 1)))
 
 void
-check_buffers(unsigned offset, unsigned size)
+mark_log(void)
+{
+       char command[256];
+       int ret;
+
+       snprintf(command, 256, "dmsetup message %s 0 mark %s.mark%d", logdev,
+                bname, mark_nr);
+       ret = system(command);
+       if (ret) {
+               prterr("dmsetup mark failed");
+               exit(211);
+       }
+}
+
+void
+dump_fsync_buffer(void)
+{
+       char fname_buffer[PATH_MAX];
+       int good_fd;
+
+       if (!good_buf)
+               return;
+
+       snprintf(fname_buffer, sizeof(fname_buffer), "%s%s.mark%d", dname,
+                bname, mark_nr);
+       good_fd = open(fname_buffer, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+       if (good_fd < 0) {
+               prterr(fname_buffer);
+               exit(212);
+       }
+
+       save_buffer(good_buf, file_size, good_fd);
+       close(good_fd);
+       prt("Dumped fsync buffer to %s\n", fname_buffer + dirpath);
+}
+
+void
+check_buffers(char *buf, unsigned offset, unsigned size)
 {
        unsigned char c, t;
        unsigned i = 0;
@@ -509,19 +647,19 @@ check_buffers(unsigned offset, unsigned size)
        unsigned op = 0;
        unsigned bad = 0;
 
-       if (memcmp(good_buf + offset, temp_buf, size) != 0) {
+       if (memcmp(good_buf + offset, 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");
                while (size > 0) {
                        c = good_buf[offset];
-                       t = temp_buf[i];
+                       t = buf[i];
                        if (c != t) {
                                if (n < 16) {
-                                       bad = short_at(&temp_buf[i]);
+                                       bad = short_at(&buf[i]);
                                        prt("0x%05x\t0x%04x\t0x%04x", offset,
                                            short_at(&good_buf[offset]), bad);
-                                       op = temp_buf[offset & 1 ? i+1 : i];
+                                       op = buf[offset & 1 ? i+1 : i];
                                        prt("\t0x%05x\n", n);
                                        if (op)
                                                prt("operation# (mod 256) for "
@@ -569,15 +707,22 @@ void
 check_trunc_hack(void)
 {
        struct stat statbuf;
+       off_t offset = file_size + (off_t)100000;
 
-       ftruncate(fd, (off_t)0);
-       ftruncate(fd, (off_t)100000);
+       if (ftruncate(fd, file_size))
+               goto ftruncate_err;
+       if (ftruncate(fd, offset))
+               goto ftruncate_err;
        fstat(fd, &statbuf);
-       if (statbuf.st_size != (off_t)100000) {
+       if (statbuf.st_size != offset) {
                prt("no extend on truncate! not posix!\n");
                exit(130);
        }
-       ftruncate(fd, 0);
+       if (ftruncate(fd, file_size)) {
+ftruncate_err:
+               prterr("check_trunc_hack: ftruncate");
+               exit(131);
+       }
 }
 
 void
@@ -642,7 +787,7 @@ doread(unsigned offset, unsigned size)
                       (monitorstart == -1 ||
                        (offset + size > monitorstart &&
                        (monitorend == -1 || offset <= monitorend))))))
-               prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+               prt("%lld read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
                    offset, offset + size - 1, size);
        ret = lseek(fd, (off_t)offset, SEEK_SET);
        if (ret == (off_t)-1) {
@@ -658,10 +803,9 @@ doread(unsigned offset, unsigned size)
                            iret, size);
                report_failure(141);
        }
-       check_buffers(offset, size);
+       check_buffers(temp_buf, offset, size);
 }
 
-
 void
 check_eofpage(char *s, unsigned offset, char *p, int size)
 {
@@ -688,6 +832,65 @@ check_eofpage(char *s, unsigned offset, char *p, int size)
                }
 }
 
+void
+check_contents(void)
+{
+       static char *check_buf;
+       unsigned offset = 0;
+       unsigned size = file_size;
+       unsigned map_offset;
+       unsigned map_size;
+       char *p;
+       off_t ret;
+       unsigned iret;
+
+       if (!check_buf) {
+               check_buf = (char *) malloc(maxfilelen + writebdy);
+               assert(check_buf != NULL);
+               check_buf = round_ptr_up(check_buf, writebdy, 0);
+               memset(check_buf, '\0', maxfilelen);
+       }
+
+       if (o_direct)
+               size -= size % readbdy;
+       if (size == 0)
+               return;
+
+       ret = lseek(fd, (off_t)offset, SEEK_SET);
+       if (ret == (off_t)-1) {
+               prterr("doread: lseek");
+               report_failure(140);
+       }
+
+       iret = fsxread(fd, check_buf, size, offset);
+       if (iret != size) {
+               if (iret == -1)
+                       prterr("check_contents: read");
+               else
+                       prt("short check read: 0x%x bytes instead of 0x%x\n",
+                           iret, size);
+               report_failure(141);
+       }
+       check_buffers(check_buf, offset, size);
+
+       /* Map eof page, check it */
+       map_offset = size - (size & PAGE_MASK);
+       if (map_offset == size)
+               map_offset -= PAGE_SIZE;
+       map_size  = size - map_offset;
+
+       p = mmap(0, map_size, PROT_READ, MAP_SHARED, fd, map_offset);
+       if (p == MAP_FAILED) {
+               prterr("check_contents: mmap");
+               report_failure(190);
+       }
+       check_eofpage("check_contents", map_offset, p, map_size);
+
+       if (munmap(p, map_size) != 0) {
+               prterr("check_contents: munmap");
+               report_failure(191);
+       }
+}
 
 void
 domapread(unsigned offset, unsigned size)
@@ -721,7 +924,7 @@ domapread(unsigned offset, unsigned size)
                       (monitorstart == -1 ||
                        (offset + size > monitorstart &&
                        (monitorend == -1 || offset <= monitorend))))))
-               prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+               prt("%lld mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
                    offset, offset + size - 1, size);
 
        pg_offset = offset & PAGE_MASK;
@@ -741,7 +944,7 @@ domapread(unsigned offset, unsigned size)
                report_failure(191);
        }
 
-       check_buffers(offset, size);
+       check_buffers(temp_buf, offset, size);
 }
 
 
@@ -749,9 +952,13 @@ void
 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
 {
        while (size--) {
-               good_buf[offset] = testcalls % 256; 
-               if (offset % 2)
-                       good_buf[offset] += original_buf[offset];
+               if (filldata) {
+                       good_buf[offset] = filldata;
+               } else {
+                       good_buf[offset] = testcalls % 256;
+                       if (offset % 2)
+                               good_buf[offset] += original_buf[offset];
+               }
                offset++;
        }
 }
@@ -795,7 +1002,7 @@ dowrite(unsigned offset, unsigned size)
                       (monitorstart == -1 ||
                        (offset + size > monitorstart &&
                        (monitorend == -1 || offset <= monitorend))))))
-               prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+               prt("%lld write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
                    offset, offset + size - 1, size);
        ret = lseek(fd, (off_t)offset, SEEK_SET);
        if (ret == (off_t)-1) {
@@ -862,7 +1069,7 @@ domapwrite(unsigned offset, unsigned size)
                       (monitorstart == -1 ||
                        (offset + size > monitorstart &&
                        (monitorend == -1 || offset <= monitorend))))))
-               prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+               prt("%lld mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
                    offset, offset + size - 1, size);
 
        if (file_size > cur_filesize) {
@@ -915,11 +1122,12 @@ dotruncate(unsigned size)
 
        if (testcalls <= simulatedopcount)
                return;
-       
+
        if ((progressinterval && testcalls % progressinterval == 0) ||
            (debug && (monitorstart == -1 || monitorend == -1 ||
                      size <= monitorend)))
-               prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
+               prt("%lld trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize,
+                               size);
        if (ftruncate(fd, (off_t)size) == -1) {
                prt("ftruncate1: %x\n", size);
                prterr("dotruncate: ftruncate");
@@ -939,14 +1147,14 @@ do_punch_hole(unsigned offset, unsigned length)
        if (length == 0) {
                if (!quiet && testcalls > simulatedopcount)
                        prt("skipping zero length punch hole\n");
-                       log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
+               log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
                return;
        }
 
        if (file_size <= (loff_t)offset) {
                if (!quiet && testcalls > simulatedopcount)
                        prt("skipping hole punch off the end of the file\n");
-                       log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
+               log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
                return;
        }
 
@@ -960,11 +1168,11 @@ do_punch_hole(unsigned offset, unsigned length)
        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,
+               prt("%lld 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);
+               prt("punch hole: 0x%x to 0x%x\n", offset, offset + length);
                prterr("do_punch_hole: fallocate");
                report_failure(161);
        }
@@ -991,11 +1199,14 @@ do_zero_range(unsigned offset, unsigned length, int keep_size)
        unsigned end_offset;
        int mode = FALLOC_FL_ZERO_RANGE;
 
+       if (keep_size)
+               mode |= FALLOC_FL_KEEP_SIZE;
+
        if (length == 0) {
                if (!quiet && testcalls > simulatedopcount)
                        prt("skipping zero length zero range\n");
-                       log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
-                            (keep_size ? FL_KEEP_SIZE : FL_NONE));
+               log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
+                    (keep_size ? FL_KEEP_SIZE : FL_NONE));
                return;
        }
 
@@ -1022,16 +1233,27 @@ do_zero_range(unsigned offset, unsigned length, int keep_size)
        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,
+               prt("%lld 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);
+               prt("zero range: 0x%x to 0x%x\n", offset, offset + length);
                prterr("do_zero_range: fallocate");
                report_failure(161);
        }
 
        memset(good_buf + offset, '\0', length);
+
+       if (!keep_size && end_offset > file_size) {
+               /*
+                * If there's a gap between the old file size and the offset of
+                * the zero range operation, fill the gap with zeroes.
+                */
+               if (offset > file_size)
+                       memset(good_buf + file_size, '\0', offset - file_size);
+
+               file_size = end_offset;
+       }
 }
 
 #else
@@ -1072,11 +1294,11 @@ do_collapse_range(unsigned offset, unsigned length)
        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);
+               prt("%lld 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);
+               prt("collapse range: 0x%x to 0x%x\n", offset, offset + length);
                prterr("do_collapse_range: fallocate");
                report_failure(161);
        }
@@ -1124,11 +1346,11 @@ do_insert_range(unsigned offset, unsigned length)
        if ((progressinterval && testcalls % progressinterval == 0) ||
            (debug && (monitorstart == -1 || monitorend == -1 ||
                      end_offset <= monitorend))) {
-               prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
+               prt("%lld insert\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("insert range: %x to %x\n", offset, length);
+               prt("insert range: 0x%x to 0x%x\n", offset, offset + length);
                prterr("do_insert_range: fallocate");
                report_failure(161);
        }
@@ -1147,6 +1369,332 @@ do_insert_range(unsigned offset, unsigned length)
 }
 #endif
 
+#ifdef FICLONERANGE
+int
+test_clone_range(void)
+{
+       struct file_clone_range fcr = {
+               .src_fd = fd,
+       };
+
+       if (ioctl(fd, FICLONERANGE, &fcr) &&
+           (errno == EOPNOTSUPP || errno == ENOTTY)) {
+               if (!quiet)
+                       fprintf(stderr,
+                               "main: filesystem does not support "
+                               "clone range, disabling!\n");
+               return 0;
+       }
+
+       return 1;
+}
+
+void
+do_clone_range(unsigned offset, unsigned length, unsigned dest)
+{
+       struct file_clone_range fcr = {
+               .src_fd = fd,
+               .src_offset = offset,
+               .src_length = length,
+               .dest_offset = dest,
+       };
+
+       if (length == 0) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping zero length clone range\n");
+               log5(OP_CLONE_RANGE, offset, length, dest, FL_SKIPPED);
+               return;
+       }
+
+       if ((loff_t)offset >= file_size) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping clone range behind EOF\n");
+               log5(OP_CLONE_RANGE, offset, length, dest, FL_SKIPPED);
+               return;
+       }
+
+       if (dest + length > biggest) {
+               biggest = dest + length;
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("cloning to largest ever: 0x%x\n", dest + length);
+       }
+
+       log5(OP_CLONE_RANGE, offset, length, dest, FL_NONE);
+
+       if (testcalls <= simulatedopcount)
+               return;
+
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                      dest <= monitorstart || dest + length <= monitorend))) {
+               prt("%lu clone\tfrom 0x%x to 0x%x, (0x%x bytes) at 0x%x\n",
+                       testcalls, offset, offset+length, length, dest);
+       }
+
+       if (ioctl(fd, FICLONERANGE, &fcr) == -1) {
+               prt("clone range: 0x%x to 0x%x at 0x%x\n", offset,
+                               offset + length, dest);
+               prterr("do_clone_range: FICLONERANGE");
+               report_failure(161);
+       }
+
+       memcpy(good_buf + dest, good_buf + offset, length);
+       if (dest > file_size)
+               memset(good_buf + file_size, '\0', dest - file_size);
+       if (dest + length > file_size)
+               file_size = dest + length;
+}
+
+#else
+int
+test_clone_range(void)
+{
+       return 0;
+}
+
+void
+do_clone_range(unsigned offset, unsigned length, unsigned dest)
+{
+       return;
+}
+#endif
+
+#ifdef FIDEDUPERANGE
+int
+test_dedupe_range(void)
+{
+       struct file_dedupe_range *fdr;
+       off_t new_len;
+       int error;
+       int ret = 1;
+
+       /* Alloc memory */
+       fdr = calloc(sizeof(struct file_dedupe_range_info) +
+                    sizeof(struct file_dedupe_range), 1);
+       if (!fdr) {
+               prterr("do_dedupe_range: malloc");
+               report_failure(161);
+       }
+
+       /* Make sure we have at least two blocks */
+       new_len = block_size * 2;
+       if (file_size < new_len && ftruncate(fd, new_len)) {
+               warn("main: ftruncate");
+               exit(132);
+       }
+
+       /* Try to dedupe them */
+       fdr->src_length = block_size;
+       fdr->dest_count = 1;
+       fdr->info[0].dest_fd = fd;
+       fdr->info[0].dest_offset = block_size;
+
+       if (ioctl(fd, FIDEDUPERANGE, fdr))
+               error = errno;
+       else if (fdr->info[0].status < 0)
+               error = -fdr->info[0].status;
+       else
+               error = 0;
+
+       /* Older kernels may return EINVAL... */
+       if (error == EOPNOTSUPP || error == ENOTTY || error == EINVAL) {
+               if (!quiet)
+                       fprintf(stderr,
+                               "main: filesystem does not support "
+                               "dedupe range, disabling!\n");
+               ret = 0;
+       }
+
+       /* Put the file back the way it was. */
+       if (file_size < new_len && ftruncate(fd, file_size)) {
+               warn("main: ftruncate");
+               exit(132);
+       }
+
+       free(fdr);
+       return ret;
+}
+
+void
+do_dedupe_range(unsigned offset, unsigned length, unsigned dest)
+{
+       struct file_dedupe_range *fdr;
+
+       if (length == 0) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping zero length dedupe range\n");
+               log5(OP_DEDUPE_RANGE, offset, length, dest, FL_SKIPPED);
+               return;
+       }
+
+       if ((loff_t)offset >= file_size) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping dedupe range behind EOF\n");
+               log5(OP_DEDUPE_RANGE, offset, length, dest, FL_SKIPPED);
+               return;
+       }
+
+       log5(OP_DEDUPE_RANGE, offset, length, dest, FL_NONE);
+
+       if (testcalls <= simulatedopcount)
+               return;
+
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                      dest <= monitorstart || dest + length <= monitorend))) {
+               prt("%lu dedupe\tfrom 0x%x to 0x%x, (0x%x bytes) at 0x%x\n",
+                       testcalls, offset, offset+length, length, dest);
+       }
+
+       /* Alloc memory */
+       fdr = calloc(sizeof(struct file_dedupe_range_info) +
+                    sizeof(struct file_dedupe_range), 1);
+       if (!fdr) {
+               prterr("do_dedupe_range: malloc");
+               report_failure(161);
+       }
+
+       /* Dedupe data blocks */
+       fdr->src_offset = offset;
+       fdr->src_length = length;
+       fdr->dest_count = 1;
+       fdr->info[0].dest_fd = fd;
+       fdr->info[0].dest_offset = dest;
+
+       if (ioctl(fd, FIDEDUPERANGE, fdr) == -1) {
+               prt("dedupe range: 0x%x to 0x%x at 0x%x\n", offset,
+                               offset + length, dest);
+               prterr("do_dedupe_range(0): FIDEDUPERANGE");
+               report_failure(161);
+       } else if (fdr->info[0].status < 0) {
+               errno = -fdr->info[0].status;
+               prt("dedupe range: 0x%x to 0x%x at 0x%x\n", offset,
+                               offset + length, dest);
+               prterr("do_dedupe_range(1): FIDEDUPERANGE");
+               report_failure(161);
+       }
+
+       free(fdr);
+}
+
+#else
+int
+test_dedupe_range(void)
+{
+       return 0;
+}
+
+void
+do_dedupe_range(unsigned offset, unsigned length, unsigned dest)
+{
+       return;
+}
+#endif
+
+#ifdef HAVE_COPY_FILE_RANGE
+int
+test_copy_range(void)
+{
+       loff_t o1 = 0, o2 = 1;
+
+       if (syscall(__NR_copy_file_range, fd, &o1, fd, &o2, 1, 0) == -1 &&
+           (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTTY)) {
+               if (!quiet)
+                       fprintf(stderr,
+                               "main: filesystem does not support "
+                               "copy range, disabling!\n");
+               return 0;
+       }
+
+       return 1;
+}
+
+void
+do_copy_range(unsigned offset, unsigned length, unsigned dest)
+{
+       loff_t o1, o2;
+       size_t olen;
+       ssize_t nr;
+       int tries = 0;
+
+       if (length == 0) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping zero length copy range\n");
+               log5(OP_COPY_RANGE, offset, length, dest, FL_SKIPPED);
+               return;
+       }
+
+       if ((loff_t)offset >= file_size) {
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("skipping copy range behind EOF\n");
+               log5(OP_COPY_RANGE, offset, length, dest, FL_SKIPPED);
+               return;
+       }
+
+       if (dest + length > biggest) {
+               biggest = dest + length;
+               if (!quiet && testcalls > simulatedopcount)
+                       prt("copying to largest ever: 0x%x\n", dest + length);
+       }
+
+       log5(OP_COPY_RANGE, offset, length, dest, FL_NONE);
+
+       if (testcalls <= simulatedopcount)
+               return;
+
+       if ((progressinterval && testcalls % progressinterval == 0) ||
+           (debug && (monitorstart == -1 || monitorend == -1 ||
+                      dest <= monitorstart || dest + length <= monitorend))) {
+               prt("%lu copy\tfrom 0x%x to 0x%x, (0x%x bytes) at 0x%x\n",
+                       testcalls, offset, offset+length, length, dest);
+       }
+
+       o1 = offset;
+       o2 = dest;
+       olen = length;
+
+       while (olen > 0) {
+               nr = syscall(__NR_copy_file_range, fd, &o1, fd, &o2, olen, 0);
+               if (nr < 0) {
+                       if (errno != EAGAIN || tries++ >= 300)
+                               break;
+               } else if (nr > olen) {
+                       prt("copy range: 0x%x to 0x%x at 0x%x\n", offset,
+                                       offset + length, dest);
+                       prt("do_copy_range: asked %u, copied %u??\n",
+                                       nr, olen);
+                       report_failure(161);
+               } else if (nr > 0)
+                       olen -= nr;
+       }
+       if (nr < 0) {
+               prt("copy range: 0x%x to 0x%x at 0x%x\n", offset,
+                               offset + length, dest);
+               prterr("do_copy_range:");
+               report_failure(161);
+       }
+
+       memcpy(good_buf + dest, good_buf + offset, length);
+       if (dest > file_size)
+               memset(good_buf + file_size, '\0', dest - file_size);
+       if (dest + length > file_size)
+               file_size = dest + length;
+}
+
+#else
+int
+test_copy_range(void)
+{
+       return 0;
+}
+
+void
+do_copy_range(unsigned offset, unsigned length, unsigned dest)
+{
+       return;
+}
+#endif
+
 #ifdef HAVE_LINUX_FALLOC_H
 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
 void
@@ -1190,10 +1738,10 @@ do_preallocate(unsigned offset, unsigned length, int keep_size)
        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,
+               prt("%lld 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);
+               prt("fallocate: 0x%x to 0x%x\n", offset, offset + length);
                prterr("do_preallocate: fallocate");
                report_failure(161);
        }
@@ -1239,18 +1787,41 @@ docloseopen(void)
                return;
 
        if (debug)
-               prt("%lu close/open\n", testcalls);
+               prt("%lld close/open\n", testcalls);
        if (close(fd)) {
                prterr("docloseopen: close");
                report_failure(180);
        }
+       if (system("echo 3 > /proc/sys/vm/drop_caches")) {
+               prterr("docloseopen: drop_caches");
+               report_failure(181);
+       }
        fd = open(fname, O_RDWR|o_direct, 0);
        if (fd < 0) {
                prterr("docloseopen: open");
-               report_failure(181);
+               report_failure(182);
        }
 }
 
+void
+dofsync(void)
+{
+       int ret;
+
+       if (testcalls <= simulatedopcount)
+               return;
+       if (debug)
+               prt("%lld fsync\n", testcalls);
+       log4(OP_FSYNC, 0, 0, 0);
+       ret = fsync(fd);
+       if (ret < 0) {
+               prterr("dofsync");
+               report_failure(210);
+       }
+       mark_log();
+       dump_fsync_buffer();
+       mark_nr++;
+}
 
 #define TRIM_OFF(off, size)                    \
 do {                                           \
@@ -1277,10 +1848,23 @@ cleanup(int sig)
 {
        if (sig)
                prt("signal %d\n", sig);
-       prt("testcalls = %lu\n", testcalls);
+       prt("testcalls = %lld\n", testcalls);
        exit(sig);
 }
 
+static int
+op_args_count(int operation)
+{
+       switch (operation) {
+       case OP_CLONE_RANGE:
+       case OP_DEDUPE_RANGE:
+       case OP_COPY_RANGE:
+               return 4;
+       default:
+               return 3;
+       }
+}
+
 static int
 read_op(struct log_entry *log_entry)
 {
@@ -1302,7 +1886,7 @@ read_op(struct log_entry *log_entry)
                                goto fail;
                        }
                        str = strtok(line, " \t\n");
-               } while (!str);
+               } while (!str || str[0] == '#');
 
                if (strcmp(str, "skip") == 0) {
                        log_entry->flags |= FL_SKIPPED;
@@ -1313,7 +1897,8 @@ read_op(struct log_entry *log_entry)
                log_entry->operation = op_code(str);
                if (log_entry->operation == -1)
                        goto fail;
-               for (i = 0; i < 3; i++) {
+               log_entry->nr_args = op_args_count(log_entry->operation);
+               for (i = 0; i < log_entry->nr_args; i++) {
                        char *end;
 
                        str = strtok(NULL, " \t\n");
@@ -1344,11 +1929,52 @@ fail:
        return 0;
 }
 
+static inline bool
+range_overlaps(
+       unsigned long   off0,
+       unsigned long   off1,
+       unsigned long   size)
+{
+       return llabs((unsigned long long)off1 - off0) < size;
+}
+
+static void generate_dest_range(bool bdy_align,
+                               unsigned long max_range_end,
+                               unsigned long *src_offset,
+                               unsigned long *size,
+                               unsigned long *dst_offset)
+{
+       int tries = 0;
+
+       TRIM_OFF_LEN(*src_offset, *size, file_size);
+       if (bdy_align) {
+               *src_offset -= *src_offset % readbdy;
+               if (o_direct)
+                       *size -= *size % readbdy;
+       } else {
+               *src_offset = *src_offset & ~(block_size - 1);
+               *size = *size & ~(block_size - 1);
+       }
+
+       do {
+               if (tries++ >= 30) {
+                       *size = 0;
+                       break;
+               }
+               *dst_offset = random();
+               TRIM_OFF(*dst_offset, max_range_end);
+               if (bdy_align)
+                       *dst_offset -= *dst_offset % writebdy;
+               else
+                       *dst_offset = *dst_offset & ~(block_size - 1);
+       } while (range_overlaps(*src_offset, *dst_offset, *size) ||
+                *dst_offset + *size > max_range_end);
+}
 
 int
 test(void)
 {
-       unsigned long   offset;
+       unsigned long   offset, offset2;
        unsigned long   size;
        unsigned long   rv;
        unsigned long   op;
@@ -1363,7 +1989,7 @@ test(void)
                debug = 1;
 
        if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
-               prt("%lu...\n", testcalls);
+               prt("%lld...\n", testcalls);
 
        if (replayopsf) {
                struct log_entry log_entry;
@@ -1379,6 +2005,7 @@ test(void)
                        op = log_entry.operation;
                        offset = log_entry.args[0];
                        size = log_entry.args[1];
+                       offset2 = log_entry.args[2];
                        closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
                        keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
                        goto have_op;
@@ -1391,6 +2018,7 @@ test(void)
                closeopen = (rv >> 3) < (1 << 28) / closeprob;
 
        offset = random();
+       offset2 = 0;
        size = maxoplen;
        if (randomoplen)
                size = random() % (maxoplen + 1);
@@ -1398,8 +2026,10 @@ test(void)
        /* calculate appropriate op to run */
        if (lite)
                op = rv % OP_MAX_LITE;
-       else
+       else if (!integrity)
                op = rv % OP_MAX_FULL;
+       else
+               op = rv % OP_MAX_INTEGRITY;
 
        switch(op) {
        case OP_TRUNCATE:
@@ -1414,6 +2044,15 @@ test(void)
                if (zero_range_calls && size && keep_size_calls)
                        keep_size = random() % 2;
                break;
+       case OP_CLONE_RANGE:
+               generate_dest_range(false, maxfilelen, &offset, &size, &offset2);
+               break;
+       case OP_DEDUPE_RANGE:
+               generate_dest_range(false, file_size, &offset, &size, &offset2);
+               break;
+       case OP_COPY_RANGE:
+               generate_dest_range(true, maxfilelen, &offset, &size, &offset2);
+               break;
        }
 
 have_op:
@@ -1457,6 +2096,24 @@ have_op:
                        goto out;
                }
                break;
+       case OP_CLONE_RANGE:
+               if (!clone_range_calls) {
+                       log5(op, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+               break;
+       case OP_DEDUPE_RANGE:
+               if (!dedupe_range_calls) {
+                       log5(op, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+               break;
+       case OP_COPY_RANGE:
+               if (!copy_range_calls) {
+                       log5(op, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+               break;
        }
 
        switch (op) {
@@ -1494,7 +2151,7 @@ have_op:
                do_punch_hole(offset, size);
                break;
        case OP_ZERO_RANGE:
-               TRIM_OFF_LEN(offset, size, file_size);
+               TRIM_OFF_LEN(offset, size, maxfilelen);
                do_zero_range(offset, size, keep_size);
                break;
        case OP_COLLAPSE_RANGE:
@@ -1523,17 +2180,59 @@ have_op:
 
                do_insert_range(offset, size);
                break;
+       case OP_CLONE_RANGE:
+               if (size == 0) {
+                       log5(OP_CLONE_RANGE, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+               if (offset2 + size > maxfilelen) {
+                       log5(OP_CLONE_RANGE, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+
+               do_clone_range(offset, size, offset2);
+               break;
+       case OP_DEDUPE_RANGE:
+               if (size == 0) {
+                       log5(OP_DEDUPE_RANGE, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+               if (offset2 + size > maxfilelen) {
+                       log5(OP_DEDUPE_RANGE, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+
+               do_dedupe_range(offset, size, offset2);
+               break;
+       case OP_COPY_RANGE:
+               if (size == 0) {
+                       log5(OP_COPY_RANGE, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+               if (offset2 + size > maxfilelen) {
+                       log5(OP_COPY_RANGE, offset, size, offset2, FL_SKIPPED);
+                       goto out;
+               }
+
+               do_copy_range(offset, size, offset2);
+               break;
+       case OP_FSYNC:
+               dofsync();
+               break;
        default:
                prterr("test: unknown operation");
                report_failure(42);
                break;
        }
 
+       if (check_file && testcalls > simulatedopcount)
+               check_contents();
+
 out:
-       if (sizechecks && testcalls > simulatedopcount)
-               check_size();
        if (closeopen)
                docloseopen();
+       if (sizechecks && testcalls > simulatedopcount)
+               check_size();
        return 1;
 }
 
@@ -1542,11 +2241,15 @@ void
 usage(void)
 {
        fprintf(stdout, "usage: %s",
-               "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\
+               "fsx [-dknqxBEFJLOWZ][-A|-U] [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid] [-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\
        -f flush and invalidate cache after I/O\n\
+       -g X: write character X instead of random generated data\n\
+       -i logdev: do integrity testing, logdev is the dm log writes device\n\
+       -j logid: prefix debug log messsages with this id\n\
+       -k: do not truncate existing file and use its size as upper bound on file size\n\
        -l flen: the upper bound on file size (default 262144)\n\
        -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
        -n: no verifications of file size\n\
@@ -1561,8 +2264,11 @@ usage(void)
        -y synchronize changes to a file\n"
 
 #ifdef AIO
-"      -A: Use the AIO system calls\n"
+"      -A: Use the AIO system calls, -A excludes -U\n"
 #endif
+#ifdef URING
+"      -U: Use the IO_URING system calls, -U excludes -A\n"
+ #endif
 "      -D startingop: debug output starting at specified operation\n"
 #ifdef HAVE_LINUX_FALLOC_H
 "      -F: Do not use fallocate (preallocation) calls\n"
@@ -1579,26 +2285,38 @@ usage(void)
 #ifdef FALLOC_FL_INSERT_RANGE
 "      -I: Do not use insert range calls\n"
 #endif
+#ifdef FICLONERANGE
+"      -J: Do not use clone range calls\n"
+#endif
+#ifdef FIDEDUPERANGE
+"      -B: Do not use dedupe range calls\n"
+#endif
+#ifdef HAVE_COPY_FILE_RANGE
+"      -E: Do not use copy 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\
+       -P: save .fsxlog .fsxops and .fsxgood files in dirpath (default ./)\n\
        -S seed: for random # generator (default 1) 0 gets timestamp\n\
        -W: mapped write operations DISabled\n\
+       -X: Read file and compare to good buffer after every operation.\n\
         -R: read() system calls only (mapped reads disabled)\n\
         -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
+       --replay-ops opsfile: replay ops from recorded .fsxops file\n\
+       --record-ops[=opsfile]: dump ops file also on success. optionally specify ops file name\n\
        fname: this filename is REQUIRED (no default)\n");
        exit(90);
 }
 
 
-int
+long long
 getnum(char *s, char **e)
 {
-       int ret;
+       long long ret;
 
        *e = (char *) 0;
-       ret = strtol(s, e, 0);
+       ret = strtoll(s, e, 0);
        if (*e)
                switch (**e) {
                case 'b':
@@ -1631,7 +2349,8 @@ getnum(char *s, char **e)
 io_context_t   io_ctx;
 struct iocb    iocb;
 
-int aio_setup()
+int
+aio_setup()
 {
        int ret;
        ret = io_queue_init(QSZ, &io_ctx);
@@ -1644,7 +2363,7 @@ int aio_setup()
 }
 
 int
-__aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
 {
        struct io_event event;
        static struct timespec ts;
@@ -1709,13 +2428,129 @@ out_error:
        errno = -ret;
        return -1;
 }
+#else
+aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+{
+       fprintf(stderr, "io_rw: need AIO support!\n");
+       exit(111);
+}
+#endif
+
+#ifdef URING
+
+struct io_uring ring;
+#define URING_ENTRIES  1024
+
+int
+uring_setup()
+{
+       int ret;
+
+       ret = io_uring_queue_init(URING_ENTRIES, &ring, 0);
+       if (ret != 0) {
+               fprintf(stderr, "uring_setup: io_uring_queue_init failed: %s\n",
+                               strerror(ret));
+               return -1;
+       }
+       return 0;
+}
+
+int
+uring_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+{
+       struct io_uring_sqe     *sqe;
+       struct io_uring_cqe     *cqe;
+       struct iovec            iovec;
+       int ret;
+       int res = 0;
+       char *p = buf;
+       unsigned l = len;
+       unsigned o = offset;
+
+       /*
+        * Due to io_uring tries non-blocking IOs (especially read), that
+        * always cause 'normal' short reading. To avoid this short read
+        * fail, try to loop read/write (escpecilly read) data.
+        */
+       while (l > 0) {
+               sqe = io_uring_get_sqe(&ring);
+               if (!sqe) {
+                       fprintf(stderr, "uring_rw: io_uring_get_sqe failed: %s\n",
+                                       strerror(errno));
+                       return -1;
+               }
+
+               iovec.iov_base = p;
+               iovec.iov_len = l;
+               if (rw == READ) {
+                       io_uring_prep_readv(sqe, fd, &iovec, 1, o);
+               } else {
+                       io_uring_prep_writev(sqe, fd, &iovec, 1, o);
+               }
+
+               ret = io_uring_submit_and_wait(&ring, 1);
+               if (ret != 1) {
+                       fprintf(stderr, "errcode=%d\n", -ret);
+                       fprintf(stderr, "uring %s: io_uring_submit failed: %s\n",
+                                       rw == READ ? "read":"write", strerror(-ret));
+                       goto uring_error;
+               }
+
+               ret = io_uring_wait_cqe(&ring, &cqe);
+               if (ret != 0) {
+                       fprintf(stderr, "errcode=%d\n", -ret);
+                       fprintf(stderr, "uring %s: io_uring_wait_cqe failed: %s\n",
+                                       rw == READ ? "read":"write", strerror(-ret));
+                       goto uring_error;
+               }
+
+               ret = cqe->res;
+               io_uring_cqe_seen(&ring, cqe);
+
+               if (ret > 0) {
+                       o += ret;
+                       l -= ret;
+                       p += ret;
+                       res += ret;
+               } else if (ret < 0) {
+                       fprintf(stderr, "errcode=%d\n", -ret);
+                       fprintf(stderr, "uring %s: io_uring failed: %s\n",
+                                       rw == READ ? "read":"write", strerror(-ret));
+                       goto uring_error;
+               } else {
+                       fprintf(stderr, "uring %s bad io length: %d instead of %u\n",
+                                       rw == READ ? "read":"write", res, len);
+                       break;
+               }
+       }
+       return res;
+
+ uring_error:
+       /*
+        * The caller expects error return in traditional libc
+        * convention, i.e. -1 and the errno set to error.
+        */
+       errno = -ret;
+       return -1;
+}
+#else
+int
+uring_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+{
+       fprintf(stderr, "io_rw: need IO_URING support!\n");
+       exit(111);
+}
+#endif
 
-int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+int
+fsx_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
 {
        int ret;
 
        if (aio) {
-               ret = __aio_rw(rw, fd, buf, len, offset);
+               ret = aio_rw(rw, fd, buf, len, offset);
+       } else if (uring) {
+               ret = uring_rw(rw, fd, buf, len, offset);
        } else {
                if (rw == READ)
                        ret = read(fd, buf, len);
@@ -1725,8 +2560,6 @@ int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
        return ret;
 }
 
-#endif
-
 #define test_fallocate(mode) __test_fallocate(mode, #mode)
 
 int
@@ -1735,7 +2568,8 @@ __test_fallocate(int mode, const char *mode_str)
 #ifdef HAVE_LINUX_FALLOC_H
        int ret = 0;
        if (!lite) {
-               if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
+               if (fallocate(fd, mode, file_size, 1) &&
+                   (errno == ENOSYS || errno == EOPNOTSUPP)) {
                        if(!quiet)
                                fprintf(stderr,
                                        "main: filesystem does not support "
@@ -1743,7 +2577,10 @@ __test_fallocate(int mode, const char *mode_str)
                                        mode_str);
                } else {
                        ret = 1;
-                       ftruncate(fd, 0);
+                       if (ftruncate(fd, file_size)) {
+                               warn("main: ftruncate");
+                               exit(132);
+                       }
                }
        }
        return ret;
@@ -1752,6 +2589,7 @@ __test_fallocate(int mode, const char *mode_str)
 
 static struct option longopts[] = {
        {"replay-ops", required_argument, 0, 256},
+       {"record-ops", optional_argument, 0, 255},
        { }
 };
 
@@ -1759,13 +2597,13 @@ int
 main(int argc, char **argv)
 {
        int     i, style, ch;
-       char    *endp;
-       char goodfile[1024];
-       char logfile[1024];
+       char    *endp, *tmp;
+       char logfile[PATH_MAX];
        struct stat statbuf;
+       int o_flags = O_RDWR|O_CREAT|O_TRUNC;
 
-       goodfile[0] = 0;
        logfile[0] = 0;
+       dname[0] = 0;
 
        page_size = getpagesize();
        page_mask = page_size - 1;
@@ -1775,14 +2613,14 @@ main(int argc, char **argv)
        setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
        while ((ch = getopt_long(argc, argv,
-                                "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ",
+                                "b:c:dfg:i:j:kl:m:no:p:qr:s:t:w:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                                 longopts, NULL)) != EOF)
                switch (ch) {
                case 'b':
                        simulatedopcount = getnum(optarg, &endp);
                        if (!quiet)
-                               fprintf(stdout, "Will begin at operation %ld\n",
-                                       simulatedopcount);
+                               prt("Will begin at operation %lld\n",
+                                               simulatedopcount);
                        if (simulatedopcount == 0)
                                usage();
                        simulatedopcount -= 1;
@@ -1790,9 +2628,7 @@ main(int argc, char **argv)
                case 'c':
                        closeprob = getnum(optarg, &endp);
                        if (!quiet)
-                               fprintf(stdout,
-                                       "Chance of close/open is 1 in %d\n",
-                                       closeprob);
+                               prt("Chance of close/open is 1 in %d\n", closeprob);
                        if (closeprob <= 0)
                                usage();
                        break;
@@ -1802,6 +2638,27 @@ main(int argc, char **argv)
                case 'f':
                        flush = 1;
                        break;
+               case 'g':
+                       filldata = *optarg;
+                       break;
+               case 'i':
+                       integrity = 1;
+                       logdev = strdup(optarg);
+                       if (!logdev) {
+                               prterr("strdup");
+                               exit(101);
+                       }
+                       break;
+               case 'j':
+                       logid = strdup(optarg);
+                       if (!logid) {
+                               prterr("strdup");
+                               exit(101);
+                       }
+                       break;
+               case 'k':
+                       o_flags &= ~O_TRUNC;
+                       break;
                case 'l':
                        maxfilelen = getnum(optarg, &endp);
                        if (maxfilelen <= 0)
@@ -1862,7 +2719,10 @@ main(int argc, char **argv)
                        do_fsync = 1;
                        break;
                case 'A':
-                       aio = 1;
+                       aio = 1;
+                       break;
+               case 'U':
+                       uring = 1;
                        break;
                case 'D':
                        debugstart = getnum(optarg, &endp);
@@ -1887,8 +2747,18 @@ main(int argc, char **argv)
                case 'I':
                        insert_range_calls = 0;
                        break;
+               case 'J':
+                       clone_range_calls = 0;
+                       break;
+               case 'B':
+                       dedupe_range_calls = 0;
+                       break;
+               case 'E':
+                       copy_range_calls = 0;
+                       break;
                case 'L':
                        lite = 1;
+                       o_flags &= ~(O_CREAT|O_TRUNC);
                        break;
                case 'N':
                        numops = getnum(optarg, &endp);
@@ -1899,30 +2769,37 @@ main(int argc, char **argv)
                        randomoplen = 0;
                        break;
                case 'P':
-                       strncpy(goodfile, optarg, sizeof(goodfile));
-                       strcat(goodfile, "/");
-                       strncpy(logfile, optarg, sizeof(logfile));
-                       strcat(logfile, "/");
+                       snprintf(dname, sizeof(dname), "%s/", optarg);
+                       dirpath = strlen(dname);
                        break;
                 case 'R':
                         mapped_reads = 0;
                         break;
                case 'S':
                         seed = getnum(optarg, &endp);
-                       if (seed == 0)
+                       if (seed == 0) {
                                seed = time(0) % 10000;
-                       if (!quiet)
-                               fprintf(stdout, "Seed set to %d\n", seed);
+                               seed += (int)getpid();
+                       }
                        if (seed < 0)
                                usage();
                        break;
                case 'W':
                        mapped_writes = 0;
                        if (!quiet)
-                               fprintf(stdout, "mapped writes DISABLED\n");
+                               prt("mapped writes DISABLED\n");
+                       break;
+               case 'X':
+                       check_file = 1;
                        break;
                case 'Z':
                        o_direct = O_DIRECT;
+                       o_flags |= O_DIRECT;
+                       break;
+               case 255:  /* --record-ops */
+                       if (optarg)
+                               snprintf(opsfile, sizeof(opsfile), "%s", optarg);
+                       recordops = opsfile;
                        break;
                case 256:  /* --replay-ops */
                        replayops = optarg;
@@ -1935,7 +2812,24 @@ main(int argc, char **argv)
        argv += optind;
        if (argc != 1)
                usage();
+
+       if (aio && uring) {
+               fprintf(stderr, "-A and -U shouldn't be used together\n");
+               usage();
+       }
+
+       if (integrity && !dirpath) {
+               fprintf(stderr, "option -i <logdev> requires -P <dirpath>\n");
+               usage();
+       }
+
        fname = argv[0];
+       tmp = strdup(fname);
+       if (!tmp) {
+               prterr("strdup");
+               exit(101);
+       }
+       bname = basename(tmp);
 
        signal(SIGHUP,  cleanup);
        signal(SIGINT,  cleanup);
@@ -1948,10 +2842,10 @@ main(int argc, char **argv)
        signal(SIGUSR1, cleanup);
        signal(SIGUSR2, cleanup);
 
-       initstate(seed, state, 256);
-       setstate(state);
-       fd = open(fname,
-               O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
+       if (!quiet && seed)
+               prt("Seed set to %d\n", seed);
+       srandom(seed);
+       fd = open(fname, o_flags, 0666);
        if (fd < 0) {
                prterr(fname);
                exit(91);
@@ -1978,22 +2872,28 @@ main(int argc, char **argv)
                }
        }
 #endif
-       strncat(goodfile, fname, 256);
-       strcat (goodfile, ".fsxgood");
+
+       if (dirpath) {
+               snprintf(goodfile, sizeof(goodfile), "%s%s.fsxgood", dname, bname);
+               snprintf(logfile, sizeof(logfile), "%s%s.fsxlog", dname, bname);
+               if (!*opsfile)
+                       snprintf(opsfile, sizeof(opsfile), "%s%s.fsxops", dname, bname);
+       } else {
+               snprintf(goodfile, sizeof(goodfile), "%s.fsxgood", fname);
+               snprintf(logfile, sizeof(logfile), "%s.fsxlog", fname);
+               if (!*opsfile)
+                       snprintf(opsfile, sizeof(opsfile), "%s.fsxops", fname);
+       }
        fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
        if (fsxgoodfd < 0) {
                prterr(goodfile);
                exit(92);
        }
-       strncat(logfile, fname, 256);
-       strcat (logfile, ".fsxlog");
        fsxlogf = fopen(logfile, "w");
        if (fsxlogf == NULL) {
                prterr(logfile);
                exit(93);
        }
-       strncat(opsfile, fname, 256);
-       strcat(opsfile, ".fsxops");
        unlink(opsfile);
 
        if (replayops) {
@@ -2008,10 +2908,14 @@ main(int argc, char **argv)
        if (aio) 
                aio_setup();
 #endif
+#ifdef URING
+       if (uring)
+               uring_setup();
+#endif
 
-       if (lite) {
+       if (!(o_flags & O_TRUNC)) {
                off_t ret;
-               file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
+               file_size = maxfilelen = biggest = lseek(fd, (off_t)0, SEEK_END);
                if (file_size == (off_t)-1) {
                        prterr(fname);
                        warn("main: lseek eof");
@@ -2048,8 +2952,23 @@ main(int argc, char **argv)
                                        maxfilelen);
                        exit(98);
                }
-       } else 
+       } else {
+               ssize_t ret, len = file_size;
+               off_t off = 0;
+
+               while (len > 0) {
+                       ret = read(fd, good_buf + off, len);
+                       if (ret == -1) {
+                               prterr(fname);
+                               warn("main: error on read");
+                               exit(98);
+                       }
+                       len -= ret;
+                       off += ret;
+               }
+
                check_trunc_hack();
+       }
 
        if (fallocate_calls)
                fallocate_calls = test_fallocate(0);
@@ -2063,16 +2982,25 @@ main(int argc, char **argv)
                collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
        if (insert_range_calls)
                insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
+       if (clone_range_calls)
+               clone_range_calls = test_clone_range();
+       if (dedupe_range_calls)
+               dedupe_range_calls = test_dedupe_range();
+       if (copy_range_calls)
+               copy_range_calls = test_copy_range();
 
        while (numops == -1 || numops--)
                if (!test())
                        break;
 
+       free(tmp);
        if (close(fd)) {
                prterr("close");
                report_failure(99);
        }
-       prt("All %lu operations completed A-OK!\n", testcalls);
+       prt("All %lld operations completed A-OK!\n", testcalls);
+       if (recordops)
+               logdump();
 
        exit(0);
        return 0;