ltp/fsx: drop caches if we're doing closeopen
[xfstests-dev.git] / ltp / fsx.c
index 5601c70c1b89abe4b43d84ca580df0eee215a7ba..120f4374c95b403d7cce60a761c6334ace85d6f1 100644 (file)
--- a/ltp/fsx.c
+++ b/ltp/fsx.c
@@ -34,6 +34,7 @@
 #ifdef AIO
 #include <libaio.h>
 #endif
+#include <sys/syscall.h>
 
 #ifndef MAP_FILE
 # define MAP_FILE 0
@@ -51,7 +52,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;
 };
 
@@ -88,25 +90,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
-
-/* integrity operations */
-#define OP_FSYNC               10
-#define OP_MAX_INTEGRITY       11
+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()
@@ -160,7 +167,11 @@ 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 */
@@ -254,6 +265,9 @@ 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",
 };
 
@@ -275,6 +289,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)
 {
@@ -287,6 +322,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++;
@@ -294,7 +330,6 @@ log4(int operation, int arg0, int arg1, enum opflags flags)
                logptr = 0;
 }
 
-
 void
 logdump(void)
 {
@@ -316,7 +351,7 @@ logdump(void)
                count = LOGSIZE;
        }
        for ( ; count > 0; count--) {
-               bool overlap;
+               bool overlap, overlap2;
                int opnum;
 
                opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
@@ -418,6 +453,48 @@ 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;
@@ -436,11 +513,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)
@@ -561,7 +640,7 @@ dump_fsync_buffer(void)
 }
 
 void
-check_buffers(unsigned offset, unsigned size)
+check_buffers(char *buf, unsigned offset, unsigned size)
 {
        unsigned char c, t;
        unsigned i = 0;
@@ -569,19 +648,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 "
@@ -725,10 +804,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)
 {
@@ -755,6 +833,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)
@@ -808,7 +945,7 @@ domapread(unsigned offset, unsigned size)
                report_failure(191);
        }
 
-       check_buffers(offset, size);
+       check_buffers(temp_buf, offset, size);
 }
 
 
@@ -1218,6 +1355,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
@@ -1315,10 +1778,14 @@ docloseopen(void)
                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);
        }
 }
 
@@ -1371,6 +1838,19 @@ cleanup(int sig)
        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)
 {
@@ -1403,7 +1883,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");
@@ -1434,11 +1915,19 @@ 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;
+}
 
 int
 test(void)
 {
-       unsigned long   offset;
+       unsigned long   offset, offset2;
        unsigned long   size;
        unsigned long   rv;
        unsigned long   op;
@@ -1469,6 +1958,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;
@@ -1481,6 +1971,7 @@ test(void)
                closeopen = (rv >> 3) < (1 << 28) / closeprob;
 
        offset = random();
+       offset2 = 0;
        size = maxoplen;
        if (randomoplen)
                size = random() % (maxoplen + 1);
@@ -1506,6 +1997,48 @@ test(void)
                if (zero_range_calls && size && keep_size_calls)
                        keep_size = random() % 2;
                break;
+       case OP_CLONE_RANGE:
+               TRIM_OFF_LEN(offset, size, file_size);
+               offset = offset & ~(block_size - 1);
+               size = size & ~(block_size - 1);
+               do {
+                       offset2 = random();
+                       TRIM_OFF(offset2, maxfilelen);
+                       offset2 = offset2 & ~(block_size - 1);
+               } while (range_overlaps(offset, offset2, size) ||
+                        offset2 + size > maxfilelen);
+               break;
+       case OP_DEDUPE_RANGE:
+               {
+                       int tries = 0;
+
+                       TRIM_OFF_LEN(offset, size, file_size);
+                       offset = offset & ~(block_size - 1);
+                       size = size & ~(block_size - 1);
+                       do {
+                               if (tries++ >= 30) {
+                                       size = 0;
+                                       break;
+                               }
+                               offset2 = random();
+                               TRIM_OFF(offset2, file_size);
+                               offset2 = offset2 & ~(block_size - 1);
+                       } while (range_overlaps(offset, offset2, size) ||
+                                offset2 + size > file_size);
+                       break;
+               }
+       case OP_COPY_RANGE:
+               TRIM_OFF_LEN(offset, size, file_size);
+               offset -= offset % readbdy;
+               if (o_direct)
+                       size -= size % readbdy;
+               do {
+                       offset2 = random();
+                       TRIM_OFF(offset2, maxfilelen);
+                       offset2 -= offset2 % writebdy;
+               } while (range_overlaps(offset, offset2, size) ||
+                        offset2 + size > maxfilelen);
+               break;
        }
 
 have_op:
@@ -1549,6 +2082,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) {
@@ -1615,6 +2166,42 @@ 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;
@@ -1624,11 +2211,14 @@ have_op:
                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;
 }
 
@@ -1637,7 +2227,7 @@ void
 usage(void)
 {
        fprintf(stdout, "usage: %s",
-               "fsx [-dknqxAFLOWZ] [-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\
+               "fsx [-dknqxABEFJLOWZ] [-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\
@@ -1678,12 +2268,22 @@ 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 .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\
@@ -1836,7 +2436,8 @@ __test_fallocate(int mode, const char *mode_str)
 #ifdef HAVE_LINUX_FALLOC_H
        int ret = 0;
        if (!lite) {
-               if (fallocate(fd, mode, file_size, 1) && errno == EOPNOTSUPP) {
+               if (fallocate(fd, mode, file_size, 1) &&
+                   (errno == ENOSYS || errno == EOPNOTSUPP)) {
                        if(!quiet)
                                fprintf(stderr,
                                        "main: filesystem does not support "
@@ -1880,7 +2481,7 @@ main(int argc, char **argv)
        setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
        while ((ch = getopt_long(argc, argv,
-                                "b:c:dfg:i:j:kl: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:WXZ",
                                 longopts, NULL)) != EOF)
                switch (ch) {
                case 'b':
@@ -2010,6 +2611,15 @@ 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);
@@ -2044,6 +2654,9 @@ main(int argc, char **argv)
                        if (!quiet)
                                prt("mapped writes DISABLED\n");
                        break;
+               case 'X':
+                       check_file = 1;
+                       break;
                case 'Z':
                        o_direct = O_DIRECT;
                        o_flags |= O_DIRECT;
@@ -2225,6 +2838,12 @@ 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())