fsx: fix infinite/too long loops when generating ranges for clone operations
[xfstests-dev.git] / ltp / fsx.c
1 /*
2  *      Copyright (C) 1991, NeXT Computer, Inc.  All Rights Reserverd.
3  *
4  *      File:   fsx.c
5  *      Author: Avadis Tevanian, Jr.
6  *
7  *      File system exerciser. 
8  *
9  *      Rewritten 8/98 by Conrad Minshall.
10  *
11  *      Small changes to work under Linux -- davej.
12  *
13  *      Checks for mmap last-page zero fill.
14  */
15
16 #include "global.h"
17
18 #include <limits.h>
19 #include <time.h>
20 #include <strings.h>
21 #include <sys/file.h>
22 #include <sys/mman.h>
23 #include <stdbool.h>
24 #ifdef HAVE_ERR_H
25 #include <err.h>
26 #endif
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #ifdef AIO
35 #include <libaio.h>
36 #endif
37 #include <sys/syscall.h>
38
39 #ifndef MAP_FILE
40 # define MAP_FILE 0
41 #endif
42
43 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
44
45 /* Operation flags */
46
47 enum opflags { FL_NONE = 0, FL_SKIPPED = 1, FL_CLOSE_OPEN = 2, FL_KEEP_SIZE = 4 };
48
49 /*
50  *      A log entry is an operation and a bunch of arguments.
51  */
52
53 struct log_entry {
54         int     operation;
55         int     nr_args;
56         int     args[4];
57         enum opflags flags;
58 };
59
60 #define LOGSIZE 10000
61
62 struct log_entry        oplog[LOGSIZE]; /* the log */
63 int                     logptr = 0;     /* current position in log */
64 int                     logcount = 0;   /* total ops */
65
66 /*
67  * The operation matrix is complex due to conditional execution of different
68  * features. Hence when we come to deciding what operation to run, we need to
69  * be careful in how we select the different operations. The active operations
70  * are mapped to numbers as follows:
71  *
72  *                      lite    !lite   integrity
73  * READ:                0       0       0
74  * WRITE:               1       1       1
75  * MAPREAD:             2       2       2
76  * MAPWRITE:            3       3       3
77  * TRUNCATE:            -       4       4
78  * FALLOCATE:           -       5       5
79  * PUNCH HOLE:          -       6       6
80  * ZERO RANGE:          -       7       7
81  * COLLAPSE RANGE:      -       8       8
82  * FSYNC:               -       -       9
83  *
84  * When mapped read/writes are disabled, they are simply converted to normal
85  * reads and writes. When fallocate/fpunch calls are disabled, they are
86  * skipped.
87  *
88  * Because of the "lite" version, we also need to have different "maximum
89  * operation" defines to allow the ops to be selected correctly based on the
90  * mode being run.
91  */
92
93 enum {
94         /* common operations */
95         OP_READ = 0,
96         OP_WRITE,
97         OP_MAPREAD,
98         OP_MAPWRITE,
99         OP_MAX_LITE,
100
101         /* !lite operations */
102         OP_TRUNCATE = OP_MAX_LITE,
103         OP_FALLOCATE,
104         OP_PUNCH_HOLE,
105         OP_ZERO_RANGE,
106         OP_COLLAPSE_RANGE,
107         OP_INSERT_RANGE,
108         OP_CLONE_RANGE,
109         OP_DEDUPE_RANGE,
110         OP_COPY_RANGE,
111         OP_MAX_FULL,
112
113         /* integrity operations */
114         OP_FSYNC = OP_MAX_FULL,
115         OP_MAX_INTEGRITY,
116 };
117
118 #undef PAGE_SIZE
119 #define PAGE_SIZE       getpagesize()
120 #undef PAGE_MASK
121 #define PAGE_MASK       (PAGE_SIZE - 1)
122
123 char    *original_buf;                  /* a pointer to the original data */
124 char    *good_buf;                      /* a pointer to the correct data */
125 char    *temp_buf;                      /* a pointer to the current data */
126 char    *fname;                         /* name of our test file */
127 char    *bname;                         /* basename of our test file */
128 char    *logdev;                        /* -i flag */
129 char    *logid;                         /* -j flag */
130 char    dname[1024];                    /* -P flag */
131 char    goodfile[PATH_MAX];
132 int     dirpath = 0;                    /* -P flag */
133 int     fd;                             /* fd for our test file */
134
135 blksize_t       block_size = 0;
136 off_t           file_size = 0;
137 off_t           biggest = 0;
138 long long       testcalls = 0;          /* calls to function "test" */
139
140 long long       simulatedopcount = 0;   /* -b flag */
141 int     closeprob = 0;                  /* -c flag */
142 int     debug = 0;                      /* -d flag */
143 long long       debugstart = 0;         /* -D flag */
144 char    filldata = 0;                   /* -g flag */
145 int     flush = 0;                      /* -f flag */
146 int     do_fsync = 0;                   /* -y flag */
147 unsigned long   maxfilelen = 256 * 1024;        /* -l flag */
148 int     sizechecks = 1;                 /* -n flag disables them */
149 int     maxoplen = 64 * 1024;           /* -o flag */
150 int     quiet = 0;                      /* -q flag */
151 long long       progressinterval = 0;   /* -p flag */
152 int     readbdy = 1;                    /* -r flag */
153 int     style = 0;                      /* -s flag */
154 int     prealloc = 0;                   /* -x flag */
155 int     truncbdy = 1;                   /* -t flag */
156 int     writebdy = 1;                   /* -w flag */
157 long    monitorstart = -1;              /* -m flag */
158 long    monitorend = -1;                /* -m flag */
159 int     lite = 0;                       /* -L flag */
160 long long numops = -1;                  /* -N flag */
161 int     randomoplen = 1;                /* -O flag disables it */
162 int     seed = 1;                       /* -S flag */
163 int     mapped_writes = 1;              /* -W flag disables */
164 int     fallocate_calls = 1;            /* -F flag disables */
165 int     keep_size_calls = 1;            /* -K flag disables */
166 int     punch_hole_calls = 1;           /* -H flag disables */
167 int     zero_range_calls = 1;           /* -z flag disables */
168 int     collapse_range_calls = 1;       /* -C flag disables */
169 int     insert_range_calls = 1;         /* -I flag disables */
170 int     mapped_reads = 1;               /* -R flag disables it */
171 int     check_file = 0;                 /* -X flag enables */
172 int     clone_range_calls = 1;          /* -J flag disables */
173 int     dedupe_range_calls = 1;         /* -B flag disables */
174 int     copy_range_calls = 1;           /* -E flag disables */
175 int     integrity = 0;                  /* -i flag */
176 int     fsxgoodfd = 0;
177 int     o_direct;                       /* -Z */
178 int     aio = 0;
179 int     mark_nr = 0;
180
181 int page_size;
182 int page_mask;
183 int mmap_mask;
184 #ifdef AIO
185 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
186 #define READ 0
187 #define WRITE 1
188 #define fsxread(a,b,c,d)        aio_rw(READ, a,b,c,d)
189 #define fsxwrite(a,b,c,d)       aio_rw(WRITE, a,b,c,d)
190 #else
191 #define fsxread(a,b,c,d)        read(a,b,c)
192 #define fsxwrite(a,b,c,d)       write(a,b,c)
193 #endif
194
195 const char *replayops = NULL;
196 const char *recordops = NULL;
197 FILE *  fsxlogf = NULL;
198 FILE *  replayopsf = NULL;
199 char opsfile[PATH_MAX];
200 int badoff = -1;
201 int closeopen = 0;
202
203 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
204 {
205         unsigned long ret = (unsigned long)ptr;
206
207         ret = ((ret + align - 1) & ~(align - 1));
208         ret += offset;
209         return (void *)ret;
210 }
211
212 void
213 vwarnc(int code, const char *fmt, va_list ap)
214 {
215         if (logid)
216                 fprintf(stderr, "%s: ", logid);
217         fprintf(stderr, "fsx: ");
218         if (fmt != NULL) {
219                 vfprintf(stderr, fmt, ap);
220                 fprintf(stderr, ": ");
221         }
222         fprintf(stderr, "%s\n", strerror(code));
223 }
224
225 void
226 warn(const char * fmt, ...)  {
227         va_list ap;
228         va_start(ap, fmt);
229         vwarnc(errno, fmt, ap);
230         va_end(ap);
231 }
232
233 void
234 prt(const char *fmt, ...)
235 {
236         va_list args;
237
238         if (logid)
239                 fprintf(stdout, "%s: ", logid);
240         va_start(args, fmt);
241         vfprintf(stdout, fmt, args);
242         va_end(args);
243         if (fsxlogf) {
244                 va_start(args, fmt);
245                 vfprintf(fsxlogf, fmt, args);
246                 va_end(args);
247         }
248 }
249
250 void
251 prterr(const char *prefix)
252 {
253         prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
254 }
255
256
257 static const char *op_names[] = {
258         [OP_READ] = "read",
259         [OP_WRITE] = "write",
260         [OP_MAPREAD] = "mapread",
261         [OP_MAPWRITE] = "mapwrite",
262         [OP_TRUNCATE] = "truncate",
263         [OP_FALLOCATE] = "fallocate",
264         [OP_PUNCH_HOLE] = "punch_hole",
265         [OP_ZERO_RANGE] = "zero_range",
266         [OP_COLLAPSE_RANGE] = "collapse_range",
267         [OP_INSERT_RANGE] = "insert_range",
268         [OP_CLONE_RANGE] = "clone_range",
269         [OP_DEDUPE_RANGE] = "dedupe_range",
270         [OP_COPY_RANGE] = "copy_range",
271         [OP_FSYNC] = "fsync",
272 };
273
274 static const char *op_name(int operation)
275 {
276         if (operation >= 0 &&
277             operation < sizeof(op_names) / sizeof(op_names[0]))
278                 return op_names[operation];
279         return NULL;
280 }
281
282 static int op_code(const char *name)
283 {
284         int i;
285
286         for (i = 0; i < sizeof(op_names) / sizeof(op_names[0]); i++)
287                 if (op_names[i] && strcmp(name, op_names[i]) == 0)
288                         return i;
289         return -1;
290 }
291
292 void
293 log5(int operation, int arg0, int arg1, int arg2, enum opflags flags)
294 {
295         struct log_entry *le;
296
297         le = &oplog[logptr];
298         le->operation = operation;
299         if (closeopen)
300                 flags |= FL_CLOSE_OPEN;
301         le->args[0] = arg0;
302         le->args[1] = arg1;
303         le->args[2] = arg2;
304         le->args[3] = file_size;
305         le->nr_args = 4;
306         le->flags = flags;
307         logptr++;
308         logcount++;
309         if (logptr >= LOGSIZE)
310                 logptr = 0;
311 }
312
313 void
314 log4(int operation, int arg0, int arg1, enum opflags flags)
315 {
316         struct log_entry *le;
317
318         le = &oplog[logptr];
319         le->operation = operation;
320         if (closeopen)
321                 flags |= FL_CLOSE_OPEN;
322         le->args[0] = arg0;
323         le->args[1] = arg1;
324         le->args[2] = file_size;
325         le->nr_args = 3;
326         le->flags = flags;
327         logptr++;
328         logcount++;
329         if (logptr >= LOGSIZE)
330                 logptr = 0;
331 }
332
333 void
334 logdump(void)
335 {
336         FILE    *logopsf;
337         int     i, count, down;
338         struct log_entry        *lp;
339
340         prt("LOG DUMP (%d total operations):\n", logcount);
341
342         logopsf = fopen(opsfile, "w");
343         if (!logopsf)
344                 prterr(opsfile);
345
346         if (logcount < LOGSIZE) {
347                 i = 0;
348                 count = logcount;
349         } else {
350                 i = logptr;
351                 count = LOGSIZE;
352         }
353         for ( ; count > 0; count--) {
354                 bool overlap, overlap2;
355                 int opnum;
356
357                 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
358                 prt("%d(%3d mod 256): ", opnum, opnum%256);
359                 lp = &oplog[i];
360
361                 overlap = badoff >= lp->args[0] &&
362                           badoff < lp->args[0] + lp->args[1];
363
364                 if (lp->flags & FL_SKIPPED) {
365                         prt("SKIPPED (no operation)");
366                         goto skipped;
367                 }
368
369                 switch (lp->operation) {
370                 case OP_MAPREAD:
371                         prt("MAPREAD  0x%x thru 0x%x\t(0x%x bytes)",
372                             lp->args[0], lp->args[0] + lp->args[1] - 1,
373                             lp->args[1]);
374                         if (overlap)
375                                 prt("\t***RRRR***");
376                         break;
377                 case OP_MAPWRITE:
378                         prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
379                             lp->args[0], lp->args[0] + lp->args[1] - 1,
380                             lp->args[1]);
381                         if (overlap)
382                                 prt("\t******WWWW");
383                         break;
384                 case OP_READ:
385                         prt("READ     0x%x thru 0x%x\t(0x%x bytes)",
386                             lp->args[0], lp->args[0] + lp->args[1] - 1,
387                             lp->args[1]);
388                         if (overlap)
389                                 prt("\t***RRRR***");
390                         break;
391                 case OP_WRITE:
392                         prt("WRITE    0x%x thru 0x%x\t(0x%x bytes)",
393                             lp->args[0], lp->args[0] + lp->args[1] - 1,
394                             lp->args[1]);
395                         if (lp->args[0] > lp->args[2])
396                                 prt(" HOLE");
397                         else if (lp->args[0] + lp->args[1] > lp->args[2])
398                                 prt(" EXTEND");
399                         overlap = (badoff >= lp->args[0] ||
400                                    badoff >=lp->args[2]) &&
401                                   badoff < lp->args[0] + lp->args[1];
402                         if (overlap)
403                                 prt("\t***WWWW");
404                         break;
405                 case OP_TRUNCATE:
406                         down = lp->args[1] < lp->args[2];
407                         prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
408                             down ? "DOWN" : "UP", lp->args[2], lp->args[1]);
409                         overlap = badoff >= lp->args[1 + !down] &&
410                                   badoff < lp->args[1 + !!down];
411                         if (overlap)
412                                 prt("\t******WWWW");
413                         break;
414                 case OP_FALLOCATE:
415                         /* 0: offset 1: length 2: where alloced */
416                         prt("FALLOC   0x%x thru 0x%x\t(0x%x bytes) ",
417                                 lp->args[0], lp->args[0] + lp->args[1],
418                                 lp->args[1]);
419                         if (lp->args[0] + lp->args[1] <= lp->args[2])
420                                 prt("INTERIOR");
421                         else if (lp->flags & FL_KEEP_SIZE)
422                                 prt("PAST_EOF");
423                         else
424                                 prt("EXTENDING");
425                         if (overlap)
426                                 prt("\t******FFFF");
427                         break;
428                 case OP_PUNCH_HOLE:
429                         prt("PUNCH    0x%x thru 0x%x\t(0x%x bytes)",
430                             lp->args[0], lp->args[0] + lp->args[1] - 1,
431                             lp->args[1]);
432                         if (overlap)
433                                 prt("\t******PPPP");
434                         break;
435                 case OP_ZERO_RANGE:
436                         prt("ZERO     0x%x thru 0x%x\t(0x%x bytes)",
437                             lp->args[0], lp->args[0] + lp->args[1] - 1,
438                             lp->args[1]);
439                         if (overlap)
440                                 prt("\t******ZZZZ");
441                         break;
442                 case OP_COLLAPSE_RANGE:
443                         prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
444                             lp->args[0], lp->args[0] + lp->args[1] - 1,
445                             lp->args[1]);
446                         if (overlap)
447                                 prt("\t******CCCC");
448                         break;
449                 case OP_INSERT_RANGE:
450                         prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
451                             lp->args[0], lp->args[0] + lp->args[1] - 1,
452                             lp->args[1]);
453                         if (overlap)
454                                 prt("\t******IIII");
455                         break;
456                 case OP_CLONE_RANGE:
457                         prt("CLONE 0x%x thru 0x%x\t(0x%x bytes) to 0x%x thru 0x%x",
458                             lp->args[0], lp->args[0] + lp->args[1] - 1,
459                             lp->args[1],
460                             lp->args[2], lp->args[2] + lp->args[1] - 1);
461                         overlap2 = badoff >= lp->args[2] &&
462                                   badoff < lp->args[2] + lp->args[1];
463                         if (overlap && overlap2)
464                                 prt("\tJJJJ**JJJJ");
465                         else if (overlap)
466                                 prt("\tJJJJ******");
467                         else if (overlap2)
468                                 prt("\t******JJJJ");
469                         break;
470                 case OP_DEDUPE_RANGE:
471                         prt("DEDUPE 0x%x thru 0x%x\t(0x%x bytes) to 0x%x thru 0x%x",
472                             lp->args[0], lp->args[0] + lp->args[1] - 1,
473                             lp->args[1],
474                             lp->args[2], lp->args[2] + lp->args[1] - 1);
475                         overlap2 = badoff >= lp->args[2] &&
476                                   badoff < lp->args[2] + lp->args[1];
477                         if (overlap && overlap2)
478                                 prt("\tBBBB**BBBB");
479                         else if (overlap)
480                                 prt("\tBBBB******");
481                         else if (overlap2)
482                                 prt("\t******BBBB");
483                         break;
484                 case OP_COPY_RANGE:
485                         prt("COPY 0x%x thru 0x%x\t(0x%x bytes) to 0x%x thru 0x%x",
486                             lp->args[0], lp->args[0] + lp->args[1] - 1,
487                             lp->args[1],
488                             lp->args[2], lp->args[2] + lp->args[1] - 1);
489                         overlap2 = badoff >= lp->args[2] &&
490                                   badoff < lp->args[2] + lp->args[1];
491                         if (overlap && overlap2)
492                                 prt("\tEEEE**EEEE");
493                         else if (overlap)
494                                 prt("\tEEEE******");
495                         else if (overlap2)
496                                 prt("\t******EEEE");
497                         break;
498                 case OP_FSYNC:
499                         prt("FSYNC");
500                         break;
501                 default:
502                         prt("BOGUS LOG ENTRY (operation code = %d)!",
503                             lp->operation);
504                         continue;
505                 }
506
507             skipped:
508                 if (lp->flags & FL_CLOSE_OPEN)
509                         prt("\n\t\tCLOSE/OPEN");
510                 prt("\n");
511                 i++;
512                 if (i == LOGSIZE)
513                         i = 0;
514
515                 if (logopsf) {
516                         int j;
517
518                         if (lp->flags & FL_SKIPPED)
519                                 fprintf(logopsf, "skip ");
520                         fprintf(logopsf, "%s", op_name(lp->operation));
521                         for (j = 0; j < lp->nr_args; j++)
522                                 fprintf(logopsf, " 0x%x", lp->args[j]);
523                         if (lp->flags & FL_KEEP_SIZE)
524                                 fprintf(logopsf, " keep_size");
525                         if (lp->flags & FL_CLOSE_OPEN)
526                                 fprintf(logopsf, " close_open");
527                         if (overlap)
528                                 fprintf(logopsf, " *");
529                         fprintf(logopsf, "\n");
530                 }
531         }
532
533         if (logopsf) {
534                 if (fclose(logopsf) != 0)
535                         prterr(opsfile);
536                 else
537                         prt("Log of operations saved to \"%s\"; "
538                             "replay with --replay-ops\n",
539                             opsfile);
540         }
541 }
542
543
544 void
545 save_buffer(char *buffer, off_t bufferlength, int fd)
546 {
547         off_t ret;
548         ssize_t byteswritten;
549
550         if (fd <= 0 || bufferlength == 0)
551                 return;
552
553         if (bufferlength > SSIZE_MAX) {
554                 prt("fsx flaw: overflow in save_buffer\n");
555                 exit(67);
556         }
557         if (lite) {
558                 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
559                 if (size_by_seek == (off_t)-1)
560                         prterr("save_buffer: lseek eof");
561                 else if (bufferlength > size_by_seek) {
562                         warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
563                              (unsigned long long)bufferlength);
564                         bufferlength = size_by_seek;
565                 }
566         }
567
568         ret = lseek(fd, (off_t)0, SEEK_SET);
569         if (ret == (off_t)-1)
570                 prterr("save_buffer: lseek 0");
571         
572         byteswritten = write(fd, buffer, (size_t)bufferlength);
573         if (byteswritten != bufferlength) {
574                 if (byteswritten == -1)
575                         prterr("save_buffer write");
576                 else
577                         warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
578                              (unsigned)byteswritten,
579                              (unsigned long long)bufferlength);
580         }
581 }
582
583
584 void
585 report_failure(int status)
586 {
587         logdump();
588         
589         if (fsxgoodfd) {
590                 if (good_buf) {
591                         save_buffer(good_buf, file_size, fsxgoodfd);
592                         prt("Correct content saved for comparison\n");
593                         prt("(maybe hexdump \"%s\" vs \"%s\")\n",
594                             fname, goodfile);
595                 }
596                 close(fsxgoodfd);
597         }
598         exit(status);
599 }
600
601
602 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
603                                         *(((unsigned char *)(cp)) + 1)))
604
605 void
606 mark_log(void)
607 {
608         char command[256];
609         int ret;
610
611         snprintf(command, 256, "dmsetup message %s 0 mark %s.mark%d", logdev,
612                  bname, mark_nr);
613         ret = system(command);
614         if (ret) {
615                 prterr("dmsetup mark failed");
616                 exit(211);
617         }
618 }
619
620 void
621 dump_fsync_buffer(void)
622 {
623         char fname_buffer[PATH_MAX];
624         int good_fd;
625
626         if (!good_buf)
627                 return;
628
629         snprintf(fname_buffer, sizeof(fname_buffer), "%s%s.mark%d", dname,
630                  bname, mark_nr);
631         good_fd = open(fname_buffer, O_WRONLY|O_CREAT|O_TRUNC, 0666);
632         if (good_fd < 0) {
633                 prterr(fname_buffer);
634                 exit(212);
635         }
636
637         save_buffer(good_buf, file_size, good_fd);
638         close(good_fd);
639         prt("Dumped fsync buffer to %s\n", fname_buffer + dirpath);
640 }
641
642 void
643 check_buffers(char *buf, unsigned offset, unsigned size)
644 {
645         unsigned char c, t;
646         unsigned i = 0;
647         unsigned n = 0;
648         unsigned op = 0;
649         unsigned bad = 0;
650
651         if (memcmp(good_buf + offset, buf, size) != 0) {
652                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
653                     offset, size, fname);
654                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
655                 while (size > 0) {
656                         c = good_buf[offset];
657                         t = buf[i];
658                         if (c != t) {
659                                 if (n < 16) {
660                                         bad = short_at(&buf[i]);
661                                         prt("0x%05x\t0x%04x\t0x%04x", offset,
662                                             short_at(&good_buf[offset]), bad);
663                                         op = buf[offset & 1 ? i+1 : i];
664                                         prt("\t0x%05x\n", n);
665                                         if (op)
666                                                 prt("operation# (mod 256) for "
667                                                   "the bad data may be %u\n",
668                                                 ((unsigned)op & 0xff));
669                                         else
670                                                 prt("operation# (mod 256) for "
671                                                   "the bad data unknown, check"
672                                                   " HOLE and EXTEND ops\n");
673                                 }
674                                 n++;
675                                 badoff = offset;
676                         }
677                         offset++;
678                         i++;
679                         size--;
680                 }
681                 report_failure(110);
682         }
683 }
684
685
686 void
687 check_size(void)
688 {
689         struct stat     statbuf;
690         off_t   size_by_seek;
691
692         if (fstat(fd, &statbuf)) {
693                 prterr("check_size: fstat");
694                 statbuf.st_size = -1;
695         }
696         size_by_seek = lseek(fd, (off_t)0, SEEK_END);
697         if (file_size != statbuf.st_size || file_size != size_by_seek) {
698                 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
699                     (unsigned long long)file_size,
700                     (unsigned long long)statbuf.st_size,
701                     (unsigned long long)size_by_seek);
702                 report_failure(120);
703         }
704 }
705
706
707 void
708 check_trunc_hack(void)
709 {
710         struct stat statbuf;
711         off_t offset = file_size + (off_t)100000;
712
713         if (ftruncate(fd, file_size))
714                 goto ftruncate_err;
715         if (ftruncate(fd, offset))
716                 goto ftruncate_err;
717         fstat(fd, &statbuf);
718         if (statbuf.st_size != offset) {
719                 prt("no extend on truncate! not posix!\n");
720                 exit(130);
721         }
722         if (ftruncate(fd, file_size)) {
723 ftruncate_err:
724                 prterr("check_trunc_hack: ftruncate");
725                 exit(131);
726         }
727 }
728
729 void
730 doflush(unsigned offset, unsigned size)
731 {
732         unsigned pg_offset;
733         unsigned map_size;
734         char    *p;
735
736         if (o_direct == O_DIRECT)
737                 return;
738
739         pg_offset = offset & mmap_mask;
740         map_size  = pg_offset + size;
741
742         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
743                               MAP_FILE | MAP_SHARED, fd,
744                               (off_t)(offset - pg_offset))) == (char *)-1) {
745                 prterr("doflush: mmap");
746                 report_failure(202);
747         }
748         if (msync(p, map_size, MS_INVALIDATE) != 0) {
749                 prterr("doflush: msync");
750                 report_failure(203);
751         }
752         if (munmap(p, map_size) != 0) {
753                 prterr("doflush: munmap");
754                 report_failure(204);
755         }
756 }
757
758 void
759 doread(unsigned offset, unsigned size)
760 {
761         off_t ret;
762         unsigned iret;
763
764         offset -= offset % readbdy;
765         if (o_direct)
766                 size -= size % readbdy;
767         if (size == 0) {
768                 if (!quiet && testcalls > simulatedopcount && !o_direct)
769                         prt("skipping zero size read\n");
770                 log4(OP_READ, offset, size, FL_SKIPPED);
771                 return;
772         }
773         if (size + offset > file_size) {
774                 if (!quiet && testcalls > simulatedopcount)
775                         prt("skipping seek/read past end of file\n");
776                 log4(OP_READ, offset, size, FL_SKIPPED);
777                 return;
778         }
779
780         log4(OP_READ, offset, size, FL_NONE);
781
782         if (testcalls <= simulatedopcount)
783                 return;
784
785         if (!quiet &&
786                 ((progressinterval && testcalls % progressinterval == 0)  ||
787                 (debug &&
788                        (monitorstart == -1 ||
789                         (offset + size > monitorstart &&
790                         (monitorend == -1 || offset <= monitorend))))))
791                 prt("%lld read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
792                     offset, offset + size - 1, size);
793         ret = lseek(fd, (off_t)offset, SEEK_SET);
794         if (ret == (off_t)-1) {
795                 prterr("doread: lseek");
796                 report_failure(140);
797         }
798         iret = fsxread(fd, temp_buf, size, offset);
799         if (iret != size) {
800                 if (iret == -1)
801                         prterr("doread: read");
802                 else
803                         prt("short read: 0x%x bytes instead of 0x%x\n",
804                             iret, size);
805                 report_failure(141);
806         }
807         check_buffers(temp_buf, offset, size);
808 }
809
810 void
811 check_eofpage(char *s, unsigned offset, char *p, int size)
812 {
813         unsigned long last_page, should_be_zero;
814
815         if (offset + size <= (file_size & ~page_mask))
816                 return;
817         /*
818          * we landed in the last page of the file
819          * test to make sure the VM system provided 0's 
820          * beyond the true end of the file mapping
821          * (as required by mmap def in 1996 posix 1003.1)
822          */
823         last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
824
825         for (should_be_zero = last_page + (file_size & page_mask);
826              should_be_zero < last_page + page_size;
827              should_be_zero++)
828                 if (*(char *)should_be_zero) {
829                         prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
830                             s, file_size - 1, should_be_zero & page_mask,
831                             short_at(should_be_zero));
832                         report_failure(205);
833                 }
834 }
835
836 void
837 check_contents(void)
838 {
839         static char *check_buf;
840         unsigned offset = 0;
841         unsigned size = file_size;
842         unsigned map_offset;
843         unsigned map_size;
844         char *p;
845         off_t ret;
846         unsigned iret;
847
848         if (!check_buf) {
849                 check_buf = (char *) malloc(maxfilelen + writebdy);
850                 assert(check_buf != NULL);
851                 check_buf = round_ptr_up(check_buf, writebdy, 0);
852                 memset(check_buf, '\0', maxfilelen);
853         }
854
855         if (o_direct)
856                 size -= size % readbdy;
857         if (size == 0)
858                 return;
859
860         ret = lseek(fd, (off_t)offset, SEEK_SET);
861         if (ret == (off_t)-1) {
862                 prterr("doread: lseek");
863                 report_failure(140);
864         }
865
866         iret = fsxread(fd, check_buf, size, offset);
867         if (iret != size) {
868                 if (iret == -1)
869                         prterr("check_contents: read");
870                 else
871                         prt("short check read: 0x%x bytes instead of 0x%x\n",
872                             iret, size);
873                 report_failure(141);
874         }
875         check_buffers(check_buf, offset, size);
876
877         /* Map eof page, check it */
878         map_offset = size - (size & PAGE_MASK);
879         if (map_offset == size)
880                 map_offset -= PAGE_SIZE;
881         map_size  = size - map_offset;
882
883         p = mmap(0, map_size, PROT_READ, MAP_SHARED, fd, map_offset);
884         if (p == MAP_FAILED) {
885                 prterr("check_contents: mmap");
886                 report_failure(190);
887         }
888         check_eofpage("check_contents", map_offset, p, map_size);
889
890         if (munmap(p, map_size) != 0) {
891                 prterr("check_contents: munmap");
892                 report_failure(191);
893         }
894 }
895
896 void
897 domapread(unsigned offset, unsigned size)
898 {
899         unsigned pg_offset;
900         unsigned map_size;
901         char    *p;
902
903         offset -= offset % readbdy;
904         if (size == 0) {
905                 if (!quiet && testcalls > simulatedopcount)
906                         prt("skipping zero size read\n");
907                 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
908                 return;
909         }
910         if (size + offset > file_size) {
911                 if (!quiet && testcalls > simulatedopcount)
912                         prt("skipping seek/read past end of file\n");
913                 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
914                 return;
915         }
916
917         log4(OP_MAPREAD, offset, size, FL_NONE);
918
919         if (testcalls <= simulatedopcount)
920                 return;
921
922         if (!quiet &&
923                 ((progressinterval && testcalls % progressinterval == 0) ||
924                        (debug &&
925                        (monitorstart == -1 ||
926                         (offset + size > monitorstart &&
927                         (monitorend == -1 || offset <= monitorend))))))
928                 prt("%lld mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
929                     offset, offset + size - 1, size);
930
931         pg_offset = offset & PAGE_MASK;
932         map_size  = pg_offset + size;
933
934         if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
935                               (off_t)(offset - pg_offset))) == (char *)-1) {
936                 prterr("domapread: mmap");
937                 report_failure(190);
938         }
939         memcpy(temp_buf, p + pg_offset, size);
940
941         check_eofpage("Read", offset, p, size);
942
943         if (munmap(p, map_size) != 0) {
944                 prterr("domapread: munmap");
945                 report_failure(191);
946         }
947
948         check_buffers(temp_buf, offset, size);
949 }
950
951
952 void
953 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
954 {
955         while (size--) {
956                 if (filldata) {
957                         good_buf[offset] = filldata;
958                 } else {
959                         good_buf[offset] = testcalls % 256;
960                         if (offset % 2)
961                                 good_buf[offset] += original_buf[offset];
962                 }
963                 offset++;
964         }
965 }
966
967
968 void
969 dowrite(unsigned offset, unsigned size)
970 {
971         off_t ret;
972         unsigned iret;
973
974         offset -= offset % writebdy;
975         if (o_direct)
976                 size -= size % writebdy;
977         if (size == 0) {
978                 if (!quiet && testcalls > simulatedopcount && !o_direct)
979                         prt("skipping zero size write\n");
980                 log4(OP_WRITE, offset, size, FL_SKIPPED);
981                 return;
982         }
983
984         log4(OP_WRITE, offset, size, FL_NONE);
985
986         gendata(original_buf, good_buf, offset, size);
987         if (file_size < offset + size) {
988                 if (file_size < offset)
989                         memset(good_buf + file_size, '\0', offset - file_size);
990                 file_size = offset + size;
991                 if (lite) {
992                         warn("Lite file size bug in fsx!");
993                         report_failure(149);
994                 }
995         }
996
997         if (testcalls <= simulatedopcount)
998                 return;
999
1000         if (!quiet &&
1001                 ((progressinterval && testcalls % progressinterval == 0) ||
1002                        (debug &&
1003                        (monitorstart == -1 ||
1004                         (offset + size > monitorstart &&
1005                         (monitorend == -1 || offset <= monitorend))))))
1006                 prt("%lld write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
1007                     offset, offset + size - 1, size);
1008         ret = lseek(fd, (off_t)offset, SEEK_SET);
1009         if (ret == (off_t)-1) {
1010                 prterr("dowrite: lseek");
1011                 report_failure(150);
1012         }
1013         iret = fsxwrite(fd, good_buf + offset, size, offset);
1014         if (iret != size) {
1015                 if (iret == -1)
1016                         prterr("dowrite: write");
1017                 else
1018                         prt("short write: 0x%x bytes instead of 0x%x\n",
1019                             iret, size);
1020                 report_failure(151);
1021         }
1022         if (do_fsync) {
1023                 if (fsync(fd)) {
1024                         prt("fsync() failed: %s\n", strerror(errno));
1025                         report_failure(152);
1026                 }
1027         }
1028         if (flush) {
1029                 doflush(offset, size);
1030         }
1031 }
1032
1033
1034 void
1035 domapwrite(unsigned offset, unsigned size)
1036 {
1037         unsigned pg_offset;
1038         unsigned map_size;
1039         off_t    cur_filesize;
1040         char    *p;
1041
1042         offset -= offset % writebdy;
1043         if (size == 0) {
1044                 if (!quiet && testcalls > simulatedopcount)
1045                         prt("skipping zero size write\n");
1046                 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
1047                 return;
1048         }
1049         cur_filesize = file_size;
1050
1051         log4(OP_MAPWRITE, offset, size, FL_NONE);
1052
1053         gendata(original_buf, good_buf, offset, size);
1054         if (file_size < offset + size) {
1055                 if (file_size < offset)
1056                         memset(good_buf + file_size, '\0', offset - file_size);
1057                 file_size = offset + size;
1058                 if (lite) {
1059                         warn("Lite file size bug in fsx!");
1060                         report_failure(200);
1061                 }
1062         }
1063
1064         if (testcalls <= simulatedopcount)
1065                 return;
1066
1067         if (!quiet &&
1068                 ((progressinterval && testcalls % progressinterval == 0) ||
1069                        (debug &&
1070                        (monitorstart == -1 ||
1071                         (offset + size > monitorstart &&
1072                         (monitorend == -1 || offset <= monitorend))))))
1073                 prt("%lld mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
1074                     offset, offset + size - 1, size);
1075
1076         if (file_size > cur_filesize) {
1077                 if (ftruncate(fd, file_size) == -1) {
1078                         prterr("domapwrite: ftruncate");
1079                         exit(201);
1080                 }
1081         }
1082         pg_offset = offset & PAGE_MASK;
1083         map_size  = pg_offset + size;
1084
1085         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
1086                               MAP_FILE | MAP_SHARED, fd,
1087                               (off_t)(offset - pg_offset))) == (char *)-1) {
1088                 prterr("domapwrite: mmap");
1089                 report_failure(202);
1090         }
1091         memcpy(p + pg_offset, good_buf + offset, size);
1092         if (msync(p, map_size, MS_SYNC) != 0) {
1093                 prterr("domapwrite: msync");
1094                 report_failure(203);
1095         }
1096
1097         check_eofpage("Write", offset, p, size);
1098
1099         if (munmap(p, map_size) != 0) {
1100                 prterr("domapwrite: munmap");
1101                 report_failure(204);
1102         }
1103 }
1104
1105
1106 void
1107 dotruncate(unsigned size)
1108 {
1109         int oldsize = file_size;
1110
1111         size -= size % truncbdy;
1112         if (size > biggest) {
1113                 biggest = size;
1114                 if (!quiet && testcalls > simulatedopcount)
1115                         prt("truncating to largest ever: 0x%x\n", size);
1116         }
1117
1118         log4(OP_TRUNCATE, 0, size, FL_NONE);
1119
1120         if (size > file_size)
1121                 memset(good_buf + file_size, '\0', size - file_size);
1122         file_size = size;
1123
1124         if (testcalls <= simulatedopcount)
1125                 return;
1126
1127         if ((progressinterval && testcalls % progressinterval == 0) ||
1128             (debug && (monitorstart == -1 || monitorend == -1 ||
1129                       size <= monitorend)))
1130                 prt("%lld trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize,
1131                                 size);
1132         if (ftruncate(fd, (off_t)size) == -1) {
1133                 prt("ftruncate1: %x\n", size);
1134                 prterr("dotruncate: ftruncate");
1135                 report_failure(160);
1136         }
1137 }
1138
1139 #ifdef FALLOC_FL_PUNCH_HOLE
1140 void
1141 do_punch_hole(unsigned offset, unsigned length)
1142 {
1143         unsigned end_offset;
1144         int max_offset = 0;
1145         int max_len = 0;
1146         int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
1147
1148         if (length == 0) {
1149                 if (!quiet && testcalls > simulatedopcount)
1150                         prt("skipping zero length punch hole\n");
1151                 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
1152                 return;
1153         }
1154
1155         if (file_size <= (loff_t)offset) {
1156                 if (!quiet && testcalls > simulatedopcount)
1157                         prt("skipping hole punch off the end of the file\n");
1158                 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
1159                 return;
1160         }
1161
1162         end_offset = offset + length;
1163
1164         log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
1165
1166         if (testcalls <= simulatedopcount)
1167                 return;
1168
1169         if ((progressinterval && testcalls % progressinterval == 0) ||
1170             (debug && (monitorstart == -1 || monitorend == -1 ||
1171                       end_offset <= monitorend))) {
1172                 prt("%lld punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1173                         offset, offset+length, length);
1174         }
1175         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1176                 prt("punch hole: 0x%x to 0x%x\n", offset, offset + length);
1177                 prterr("do_punch_hole: fallocate");
1178                 report_failure(161);
1179         }
1180
1181
1182         max_offset = offset < file_size ? offset : file_size;
1183         max_len = max_offset + length <= file_size ? length :
1184                         file_size - max_offset;
1185         memset(good_buf + max_offset, '\0', max_len);
1186 }
1187
1188 #else
1189 void
1190 do_punch_hole(unsigned offset, unsigned length)
1191 {
1192         return;
1193 }
1194 #endif
1195
1196 #ifdef FALLOC_FL_ZERO_RANGE
1197 void
1198 do_zero_range(unsigned offset, unsigned length, int keep_size)
1199 {
1200         unsigned end_offset;
1201         int mode = FALLOC_FL_ZERO_RANGE;
1202
1203         if (keep_size)
1204                 mode |= FALLOC_FL_KEEP_SIZE;
1205
1206         if (length == 0) {
1207                 if (!quiet && testcalls > simulatedopcount)
1208                         prt("skipping zero length zero range\n");
1209                 log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
1210                      (keep_size ? FL_KEEP_SIZE : FL_NONE));
1211                 return;
1212         }
1213
1214         end_offset = keep_size ? 0 : offset + length;
1215
1216         if (end_offset > biggest) {
1217                 biggest = end_offset;
1218                 if (!quiet && testcalls > simulatedopcount)
1219                         prt("zero_range to largest ever: 0x%x\n", end_offset);
1220         }
1221
1222         /*
1223          * last arg matches fallocate string array index in logdump:
1224          *      0: allocate past EOF
1225          *      1: extending prealloc
1226          *      2: interior prealloc
1227          */
1228         log4(OP_ZERO_RANGE, offset, length,
1229              keep_size ? FL_KEEP_SIZE : FL_NONE);
1230
1231         if (testcalls <= simulatedopcount)
1232                 return;
1233
1234         if ((progressinterval && testcalls % progressinterval == 0) ||
1235             (debug && (monitorstart == -1 || monitorend == -1 ||
1236                       end_offset <= monitorend))) {
1237                 prt("%lld zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1238                         offset, offset+length, length);
1239         }
1240         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1241                 prt("zero range: 0x%x to 0x%x\n", offset, offset + length);
1242                 prterr("do_zero_range: fallocate");
1243                 report_failure(161);
1244         }
1245
1246         memset(good_buf + offset, '\0', length);
1247
1248         if (!keep_size && end_offset > file_size) {
1249                 /*
1250                  * If there's a gap between the old file size and the offset of
1251                  * the zero range operation, fill the gap with zeroes.
1252                  */
1253                 if (offset > file_size)
1254                         memset(good_buf + file_size, '\0', offset - file_size);
1255
1256                 file_size = end_offset;
1257         }
1258 }
1259
1260 #else
1261 void
1262 do_zero_range(unsigned offset, unsigned length, int keep_size)
1263 {
1264         return;
1265 }
1266 #endif
1267
1268 #ifdef FALLOC_FL_COLLAPSE_RANGE
1269 void
1270 do_collapse_range(unsigned offset, unsigned length)
1271 {
1272         unsigned end_offset;
1273         int mode = FALLOC_FL_COLLAPSE_RANGE;
1274
1275         if (length == 0) {
1276                 if (!quiet && testcalls > simulatedopcount)
1277                         prt("skipping zero length collapse range\n");
1278                 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1279                 return;
1280         }
1281
1282         end_offset = offset + length;
1283         if ((loff_t)end_offset >= file_size) {
1284                 if (!quiet && testcalls > simulatedopcount)
1285                         prt("skipping collapse range behind EOF\n");
1286                 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1287                 return;
1288         }
1289
1290         log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1291
1292         if (testcalls <= simulatedopcount)
1293                 return;
1294
1295         if ((progressinterval && testcalls % progressinterval == 0) ||
1296             (debug && (monitorstart == -1 || monitorend == -1 ||
1297                       end_offset <= monitorend))) {
1298                 prt("%lld collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n",
1299                                 testcalls, offset, offset+length, length);
1300         }
1301         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1302                 prt("collapse range: 0x%x to 0x%x\n", offset, offset + length);
1303                 prterr("do_collapse_range: fallocate");
1304                 report_failure(161);
1305         }
1306
1307         memmove(good_buf + offset, good_buf + end_offset,
1308                 file_size - end_offset);
1309         file_size -= length;
1310 }
1311
1312 #else
1313 void
1314 do_collapse_range(unsigned offset, unsigned length)
1315 {
1316         return;
1317 }
1318 #endif
1319
1320 #ifdef FALLOC_FL_INSERT_RANGE
1321 void
1322 do_insert_range(unsigned offset, unsigned length)
1323 {
1324         unsigned end_offset;
1325         int mode = FALLOC_FL_INSERT_RANGE;
1326
1327         if (length == 0) {
1328                 if (!quiet && testcalls > simulatedopcount)
1329                         prt("skipping zero length insert range\n");
1330                 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1331                 return;
1332         }
1333
1334         if ((loff_t)offset >= file_size) {
1335                 if (!quiet && testcalls > simulatedopcount)
1336                         prt("skipping insert range behind EOF\n");
1337                 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1338                 return;
1339         }
1340
1341         log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1342
1343         if (testcalls <= simulatedopcount)
1344                 return;
1345
1346         end_offset = offset + length;
1347         if ((progressinterval && testcalls % progressinterval == 0) ||
1348             (debug && (monitorstart == -1 || monitorend == -1 ||
1349                       end_offset <= monitorend))) {
1350                 prt("%lld insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1351                         offset, offset+length, length);
1352         }
1353         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1354                 prt("insert range: 0x%x to 0x%x\n", offset, offset + length);
1355                 prterr("do_insert_range: fallocate");
1356                 report_failure(161);
1357         }
1358
1359         memmove(good_buf + end_offset, good_buf + offset,
1360                 file_size - offset);
1361         memset(good_buf + offset, '\0', length);
1362         file_size += length;
1363 }
1364
1365 #else
1366 void
1367 do_insert_range(unsigned offset, unsigned length)
1368 {
1369         return;
1370 }
1371 #endif
1372
1373 #ifdef FICLONERANGE
1374 int
1375 test_clone_range(void)
1376 {
1377         struct file_clone_range fcr = {
1378                 .src_fd = fd,
1379         };
1380
1381         if (ioctl(fd, FICLONERANGE, &fcr) &&
1382             (errno == EOPNOTSUPP || errno == ENOTTY)) {
1383                 if (!quiet)
1384                         fprintf(stderr,
1385                                 "main: filesystem does not support "
1386                                 "clone range, disabling!\n");
1387                 return 0;
1388         }
1389
1390         return 1;
1391 }
1392
1393 void
1394 do_clone_range(unsigned offset, unsigned length, unsigned dest)
1395 {
1396         struct file_clone_range fcr = {
1397                 .src_fd = fd,
1398                 .src_offset = offset,
1399                 .src_length = length,
1400                 .dest_offset = dest,
1401         };
1402
1403         if (length == 0) {
1404                 if (!quiet && testcalls > simulatedopcount)
1405                         prt("skipping zero length clone range\n");
1406                 log5(OP_CLONE_RANGE, offset, length, dest, FL_SKIPPED);
1407                 return;
1408         }
1409
1410         if ((loff_t)offset >= file_size) {
1411                 if (!quiet && testcalls > simulatedopcount)
1412                         prt("skipping clone range behind EOF\n");
1413                 log5(OP_CLONE_RANGE, offset, length, dest, FL_SKIPPED);
1414                 return;
1415         }
1416
1417         if (dest + length > biggest) {
1418                 biggest = dest + length;
1419                 if (!quiet && testcalls > simulatedopcount)
1420                         prt("cloning to largest ever: 0x%x\n", dest + length);
1421         }
1422
1423         log5(OP_CLONE_RANGE, offset, length, dest, FL_NONE);
1424
1425         if (testcalls <= simulatedopcount)
1426                 return;
1427
1428         if ((progressinterval && testcalls % progressinterval == 0) ||
1429             (debug && (monitorstart == -1 || monitorend == -1 ||
1430                        dest <= monitorstart || dest + length <= monitorend))) {
1431                 prt("%lu clone\tfrom 0x%x to 0x%x, (0x%x bytes) at 0x%x\n",
1432                         testcalls, offset, offset+length, length, dest);
1433         }
1434
1435         if (ioctl(fd, FICLONERANGE, &fcr) == -1) {
1436                 prt("clone range: 0x%x to 0x%x at 0x%x\n", offset,
1437                                 offset + length, dest);
1438                 prterr("do_clone_range: FICLONERANGE");
1439                 report_failure(161);
1440         }
1441
1442         memcpy(good_buf + dest, good_buf + offset, length);
1443         if (dest > file_size)
1444                 memset(good_buf + file_size, '\0', dest - file_size);
1445         if (dest + length > file_size)
1446                 file_size = dest + length;
1447 }
1448
1449 #else
1450 int
1451 test_clone_range(void)
1452 {
1453         return 0;
1454 }
1455
1456 void
1457 do_clone_range(unsigned offset, unsigned length, unsigned dest)
1458 {
1459         return;
1460 }
1461 #endif
1462
1463 #ifdef FIDEDUPERANGE
1464 int
1465 test_dedupe_range(void)
1466 {
1467         struct file_dedupe_range *fdr;
1468         off_t new_len;
1469         int error;
1470         int ret = 1;
1471
1472         /* Alloc memory */
1473         fdr = calloc(sizeof(struct file_dedupe_range_info) +
1474                      sizeof(struct file_dedupe_range), 1);
1475         if (!fdr) {
1476                 prterr("do_dedupe_range: malloc");
1477                 report_failure(161);
1478         }
1479
1480         /* Make sure we have at least two blocks */
1481         new_len = block_size * 2;
1482         if (file_size < new_len && ftruncate(fd, new_len)) {
1483                 warn("main: ftruncate");
1484                 exit(132);
1485         }
1486
1487         /* Try to dedupe them */
1488         fdr->src_length = block_size;
1489         fdr->dest_count = 1;
1490         fdr->info[0].dest_fd = fd;
1491         fdr->info[0].dest_offset = block_size;
1492
1493         if (ioctl(fd, FIDEDUPERANGE, fdr))
1494                 error = errno;
1495         else if (fdr->info[0].status < 0)
1496                 error = -fdr->info[0].status;
1497         else
1498                 error = 0;
1499
1500         /* Older kernels may return EINVAL... */
1501         if (error == EOPNOTSUPP || error == ENOTTY || error == EINVAL) {
1502                 if (!quiet)
1503                         fprintf(stderr,
1504                                 "main: filesystem does not support "
1505                                 "dedupe range, disabling!\n");
1506                 ret = 0;
1507         }
1508
1509         /* Put the file back the way it was. */
1510         if (file_size < new_len && ftruncate(fd, file_size)) {
1511                 warn("main: ftruncate");
1512                 exit(132);
1513         }
1514
1515         free(fdr);
1516         return ret;
1517 }
1518
1519 void
1520 do_dedupe_range(unsigned offset, unsigned length, unsigned dest)
1521 {
1522         struct file_dedupe_range *fdr;
1523
1524         if (length == 0) {
1525                 if (!quiet && testcalls > simulatedopcount)
1526                         prt("skipping zero length dedupe range\n");
1527                 log5(OP_DEDUPE_RANGE, offset, length, dest, FL_SKIPPED);
1528                 return;
1529         }
1530
1531         if ((loff_t)offset >= file_size) {
1532                 if (!quiet && testcalls > simulatedopcount)
1533                         prt("skipping dedupe range behind EOF\n");
1534                 log5(OP_DEDUPE_RANGE, offset, length, dest, FL_SKIPPED);
1535                 return;
1536         }
1537
1538         log5(OP_DEDUPE_RANGE, offset, length, dest, FL_NONE);
1539
1540         if (testcalls <= simulatedopcount)
1541                 return;
1542
1543         if ((progressinterval && testcalls % progressinterval == 0) ||
1544             (debug && (monitorstart == -1 || monitorend == -1 ||
1545                        dest <= monitorstart || dest + length <= monitorend))) {
1546                 prt("%lu dedupe\tfrom 0x%x to 0x%x, (0x%x bytes) at 0x%x\n",
1547                         testcalls, offset, offset+length, length, dest);
1548         }
1549
1550         /* Alloc memory */
1551         fdr = calloc(sizeof(struct file_dedupe_range_info) +
1552                      sizeof(struct file_dedupe_range), 1);
1553         if (!fdr) {
1554                 prterr("do_dedupe_range: malloc");
1555                 report_failure(161);
1556         }
1557
1558         /* Dedupe data blocks */
1559         fdr->src_offset = offset;
1560         fdr->src_length = length;
1561         fdr->dest_count = 1;
1562         fdr->info[0].dest_fd = fd;
1563         fdr->info[0].dest_offset = dest;
1564
1565         if (ioctl(fd, FIDEDUPERANGE, fdr) == -1) {
1566                 prt("dedupe range: 0x%x to 0x%x at 0x%x\n", offset,
1567                                 offset + length, dest);
1568                 prterr("do_dedupe_range(0): FIDEDUPERANGE");
1569                 report_failure(161);
1570         } else if (fdr->info[0].status < 0) {
1571                 errno = -fdr->info[0].status;
1572                 prt("dedupe range: 0x%x to 0x%x at 0x%x\n", offset,
1573                                 offset + length, dest);
1574                 prterr("do_dedupe_range(1): FIDEDUPERANGE");
1575                 report_failure(161);
1576         }
1577
1578         free(fdr);
1579 }
1580
1581 #else
1582 int
1583 test_dedupe_range(void)
1584 {
1585         return 0;
1586 }
1587
1588 void
1589 do_dedupe_range(unsigned offset, unsigned length, unsigned dest)
1590 {
1591         return;
1592 }
1593 #endif
1594
1595 #ifdef HAVE_COPY_FILE_RANGE
1596 int
1597 test_copy_range(void)
1598 {
1599         loff_t o1 = 0, o2 = 1;
1600
1601         if (syscall(__NR_copy_file_range, fd, &o1, fd, &o2, 1, 0) == -1 &&
1602             (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTTY)) {
1603                 if (!quiet)
1604                         fprintf(stderr,
1605                                 "main: filesystem does not support "
1606                                 "copy range, disabling!\n");
1607                 return 0;
1608         }
1609
1610         return 1;
1611 }
1612
1613 void
1614 do_copy_range(unsigned offset, unsigned length, unsigned dest)
1615 {
1616         loff_t o1, o2;
1617         size_t olen;
1618         ssize_t nr;
1619         int tries = 0;
1620
1621         if (length == 0) {
1622                 if (!quiet && testcalls > simulatedopcount)
1623                         prt("skipping zero length copy range\n");
1624                 log5(OP_COPY_RANGE, offset, length, dest, FL_SKIPPED);
1625                 return;
1626         }
1627
1628         if ((loff_t)offset >= file_size) {
1629                 if (!quiet && testcalls > simulatedopcount)
1630                         prt("skipping copy range behind EOF\n");
1631                 log5(OP_COPY_RANGE, offset, length, dest, FL_SKIPPED);
1632                 return;
1633         }
1634
1635         if (dest + length > biggest) {
1636                 biggest = dest + length;
1637                 if (!quiet && testcalls > simulatedopcount)
1638                         prt("copying to largest ever: 0x%x\n", dest + length);
1639         }
1640
1641         log5(OP_COPY_RANGE, offset, length, dest, FL_NONE);
1642
1643         if (testcalls <= simulatedopcount)
1644                 return;
1645
1646         if ((progressinterval && testcalls % progressinterval == 0) ||
1647             (debug && (monitorstart == -1 || monitorend == -1 ||
1648                        dest <= monitorstart || dest + length <= monitorend))) {
1649                 prt("%lu copy\tfrom 0x%x to 0x%x, (0x%x bytes) at 0x%x\n",
1650                         testcalls, offset, offset+length, length, dest);
1651         }
1652
1653         o1 = offset;
1654         o2 = dest;
1655         olen = length;
1656
1657         while (olen > 0) {
1658                 nr = syscall(__NR_copy_file_range, fd, &o1, fd, &o2, olen, 0);
1659                 if (nr < 0) {
1660                         if (errno != EAGAIN || tries++ >= 300)
1661                                 break;
1662                 } else if (nr > olen) {
1663                         prt("copy range: 0x%x to 0x%x at 0x%x\n", offset,
1664                                         offset + length, dest);
1665                         prt("do_copy_range: asked %u, copied %u??\n",
1666                                         nr, olen);
1667                         report_failure(161);
1668                 } else if (nr > 0)
1669                         olen -= nr;
1670         }
1671         if (nr < 0) {
1672                 prt("copy range: 0x%x to 0x%x at 0x%x\n", offset,
1673                                 offset + length, dest);
1674                 prterr("do_copy_range:");
1675                 report_failure(161);
1676         }
1677
1678         memcpy(good_buf + dest, good_buf + offset, length);
1679         if (dest > file_size)
1680                 memset(good_buf + file_size, '\0', dest - file_size);
1681         if (dest + length > file_size)
1682                 file_size = dest + length;
1683 }
1684
1685 #else
1686 int
1687 test_copy_range(void)
1688 {
1689         return 0;
1690 }
1691
1692 void
1693 do_copy_range(unsigned offset, unsigned length, unsigned dest)
1694 {
1695         return;
1696 }
1697 #endif
1698
1699 #ifdef HAVE_LINUX_FALLOC_H
1700 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1701 void
1702 do_preallocate(unsigned offset, unsigned length, int keep_size)
1703 {
1704         unsigned end_offset;
1705
1706         if (length == 0) {
1707                 if (!quiet && testcalls > simulatedopcount)
1708                         prt("skipping zero length fallocate\n");
1709                 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1710                      (keep_size ? FL_KEEP_SIZE : FL_NONE));
1711                 return;
1712         }
1713
1714         end_offset = keep_size ? 0 : offset + length;
1715
1716         if (end_offset > biggest) {
1717                 biggest = end_offset;
1718                 if (!quiet && testcalls > simulatedopcount)
1719                         prt("fallocating to largest ever: 0x%x\n", end_offset);
1720         }
1721
1722         /*
1723          * last arg matches fallocate string array index in logdump:
1724          *      0: allocate past EOF
1725          *      1: extending prealloc
1726          *      2: interior prealloc
1727          */
1728         log4(OP_FALLOCATE, offset, length,
1729              keep_size ? FL_KEEP_SIZE : FL_NONE);
1730
1731         if (end_offset > file_size) {
1732                 memset(good_buf + file_size, '\0', end_offset - file_size);
1733                 file_size = end_offset;
1734         }
1735
1736         if (testcalls <= simulatedopcount)
1737                 return;
1738         
1739         if ((progressinterval && testcalls % progressinterval == 0) ||
1740             (debug && (monitorstart == -1 || monitorend == -1 ||
1741                       end_offset <= monitorend)))
1742                 prt("%lld falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1743                                 offset, offset + length, length);
1744         if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1745                 prt("fallocate: 0x%x to 0x%x\n", offset, offset + length);
1746                 prterr("do_preallocate: fallocate");
1747                 report_failure(161);
1748         }
1749 }
1750 #else
1751 void
1752 do_preallocate(unsigned offset, unsigned length, int keep_size)
1753 {
1754         return;
1755 }
1756 #endif
1757
1758 void
1759 writefileimage()
1760 {
1761         ssize_t iret;
1762
1763         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1764                 prterr("writefileimage: lseek");
1765                 report_failure(171);
1766         }
1767         iret = write(fd, good_buf, file_size);
1768         if ((off_t)iret != file_size) {
1769                 if (iret == -1)
1770                         prterr("writefileimage: write");
1771                 else
1772                         prt("short write: 0x%x bytes instead of 0x%llx\n",
1773                             iret, (unsigned long long)file_size);
1774                 report_failure(172);
1775         }
1776         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1777                 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1778                 prterr("writefileimage: ftruncate");
1779                 report_failure(173);
1780         }
1781 }
1782
1783
1784 void
1785 docloseopen(void)
1786
1787         if (testcalls <= simulatedopcount)
1788                 return;
1789
1790         if (debug)
1791                 prt("%lld close/open\n", testcalls);
1792         if (close(fd)) {
1793                 prterr("docloseopen: close");
1794                 report_failure(180);
1795         }
1796         if (system("echo 3 > /proc/sys/vm/drop_caches")) {
1797                 prterr("docloseopen: drop_caches");
1798                 report_failure(181);
1799         }
1800         fd = open(fname, O_RDWR|o_direct, 0);
1801         if (fd < 0) {
1802                 prterr("docloseopen: open");
1803                 report_failure(182);
1804         }
1805 }
1806
1807 void
1808 dofsync(void)
1809 {
1810         int ret;
1811
1812         if (testcalls <= simulatedopcount)
1813                 return;
1814         if (debug)
1815                 prt("%lld fsync\n", testcalls);
1816         log4(OP_FSYNC, 0, 0, 0);
1817         ret = fsync(fd);
1818         if (ret < 0) {
1819                 prterr("dofsync");
1820                 report_failure(210);
1821         }
1822         mark_log();
1823         dump_fsync_buffer();
1824         mark_nr++;
1825 }
1826
1827 #define TRIM_OFF(off, size)                     \
1828 do {                                            \
1829         if (size)                               \
1830                 (off) %= (size);                \
1831         else                                    \
1832                 (off) = 0;                      \
1833 } while (0)
1834
1835 #define TRIM_LEN(off, len, size)                \
1836 do {                                            \
1837         if ((off) + (len) > (size))             \
1838                 (len) = (size) - (off);         \
1839 } while (0)
1840
1841 #define TRIM_OFF_LEN(off, len, size)            \
1842 do {                                            \
1843         TRIM_OFF(off, size);                    \
1844         TRIM_LEN(off, len, size);               \
1845 } while (0)
1846
1847 void
1848 cleanup(int sig)
1849 {
1850         if (sig)
1851                 prt("signal %d\n", sig);
1852         prt("testcalls = %lld\n", testcalls);
1853         exit(sig);
1854 }
1855
1856 static int
1857 op_args_count(int operation)
1858 {
1859         switch (operation) {
1860         case OP_CLONE_RANGE:
1861         case OP_DEDUPE_RANGE:
1862         case OP_COPY_RANGE:
1863                 return 4;
1864         default:
1865                 return 3;
1866         }
1867 }
1868
1869 static int
1870 read_op(struct log_entry *log_entry)
1871 {
1872         char line[256];
1873
1874         memset(log_entry, 0, sizeof(*log_entry));
1875         log_entry->operation = -1;
1876
1877         while (log_entry->operation == -1) {
1878                 char *str;
1879                 int i;
1880
1881                 do {
1882                         if (!fgets(line, sizeof(line), replayopsf)) {
1883                                 if (feof(replayopsf)) {
1884                                         replayopsf = NULL;
1885                                         return 0;
1886                                 }
1887                                 goto fail;
1888                         }
1889                         str = strtok(line, " \t\n");
1890                 } while (!str || str[0] == '#');
1891
1892                 if (strcmp(str, "skip") == 0) {
1893                         log_entry->flags |= FL_SKIPPED;
1894                         str = strtok(NULL, " \t\n");
1895                         if (!str)
1896                                 goto fail;
1897                 }
1898                 log_entry->operation = op_code(str);
1899                 if (log_entry->operation == -1)
1900                         goto fail;
1901                 log_entry->nr_args = op_args_count(log_entry->operation);
1902                 for (i = 0; i < log_entry->nr_args; i++) {
1903                         char *end;
1904
1905                         str = strtok(NULL, " \t\n");
1906                         if (!str)
1907                                 goto fail;
1908                         log_entry->args[i] = strtoul(str, &end, 0);
1909                         if (*end)
1910                                 goto fail;
1911                 }
1912                 while ((str = strtok(NULL, " \t\n"))) {
1913                         if (strcmp(str, "keep_size") == 0)
1914                                 log_entry->flags |= FL_KEEP_SIZE;
1915                         else if (strcmp(str, "close_open") == 0)
1916                                 log_entry->flags |= FL_CLOSE_OPEN;
1917                         else if (strcmp(str, "*") == 0)
1918                                 ;  /* overlap marker; ignore */
1919                         else
1920                                 goto fail;
1921                 }
1922         }
1923         return 1;
1924
1925 fail:
1926         fprintf(stderr, "%s: parse error\n", replayops);
1927         fclose(replayopsf);
1928         replayopsf = NULL;
1929         cleanup(100);  /* doesn't return */
1930         return 0;
1931 }
1932
1933 static inline bool
1934 range_overlaps(
1935         unsigned long   off0,
1936         unsigned long   off1,
1937         unsigned long   size)
1938 {
1939         return llabs((unsigned long long)off1 - off0) < size;
1940 }
1941
1942 int
1943 test(void)
1944 {
1945         unsigned long   offset, offset2;
1946         unsigned long   size;
1947         unsigned long   rv;
1948         unsigned long   op;
1949         int             keep_size = 0;
1950
1951         if (simulatedopcount > 0 && testcalls == simulatedopcount)
1952                 writefileimage();
1953
1954         testcalls++;
1955
1956         if (debugstart > 0 && testcalls >= debugstart)
1957                 debug = 1;
1958
1959         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1960                 prt("%lld...\n", testcalls);
1961
1962         if (replayopsf) {
1963                 struct log_entry log_entry;
1964
1965                 while (read_op(&log_entry)) {
1966                         if (log_entry.flags & FL_SKIPPED) {
1967                                 log4(log_entry.operation,
1968                                      log_entry.args[0], log_entry.args[1],
1969                                      log_entry.flags);
1970                                 continue;
1971                         }
1972
1973                         op = log_entry.operation;
1974                         offset = log_entry.args[0];
1975                         size = log_entry.args[1];
1976                         offset2 = log_entry.args[2];
1977                         closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
1978                         keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
1979                         goto have_op;
1980                 }
1981                 return 0;
1982         }
1983
1984         rv = random();
1985         if (closeprob)
1986                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1987
1988         offset = random();
1989         offset2 = 0;
1990         size = maxoplen;
1991         if (randomoplen)
1992                 size = random() % (maxoplen + 1);
1993
1994         /* calculate appropriate op to run */
1995         if (lite)
1996                 op = rv % OP_MAX_LITE;
1997         else if (!integrity)
1998                 op = rv % OP_MAX_FULL;
1999         else
2000                 op = rv % OP_MAX_INTEGRITY;
2001
2002         switch(op) {
2003         case OP_TRUNCATE:
2004                 if (!style)
2005                         size = random() % maxfilelen;
2006                 break;
2007         case OP_FALLOCATE:
2008                 if (fallocate_calls && size && keep_size_calls)
2009                         keep_size = random() % 2;
2010                 break;
2011         case OP_ZERO_RANGE:
2012                 if (zero_range_calls && size && keep_size_calls)
2013                         keep_size = random() % 2;
2014                 break;
2015         case OP_CLONE_RANGE:
2016                 {
2017                         int tries = 0;
2018
2019                         TRIM_OFF_LEN(offset, size, file_size);
2020                         offset = offset & ~(block_size - 1);
2021                         size = size & ~(block_size - 1);
2022                         do {
2023                                 if (tries++ >= 30) {
2024                                         size = 0;
2025                                         break;
2026                                 }
2027                                 offset2 = random();
2028                                 TRIM_OFF(offset2, maxfilelen);
2029                                 offset2 = offset2 & ~(block_size - 1);
2030                         } while (range_overlaps(offset, offset2, size) ||
2031                                  offset2 + size > maxfilelen);
2032                         break;
2033                 }
2034         case OP_DEDUPE_RANGE:
2035                 {
2036                         int tries = 0;
2037
2038                         TRIM_OFF_LEN(offset, size, file_size);
2039                         offset = offset & ~(block_size - 1);
2040                         size = size & ~(block_size - 1);
2041                         do {
2042                                 if (tries++ >= 30) {
2043                                         size = 0;
2044                                         break;
2045                                 }
2046                                 offset2 = random();
2047                                 TRIM_OFF(offset2, file_size);
2048                                 offset2 = offset2 & ~(block_size - 1);
2049                         } while (range_overlaps(offset, offset2, size) ||
2050                                  offset2 + size > file_size);
2051                         break;
2052                 }
2053         case OP_COPY_RANGE:
2054                 TRIM_OFF_LEN(offset, size, file_size);
2055                 offset -= offset % readbdy;
2056                 if (o_direct)
2057                         size -= size % readbdy;
2058                 do {
2059                         offset2 = random();
2060                         TRIM_OFF(offset2, maxfilelen);
2061                         offset2 -= offset2 % writebdy;
2062                 } while (range_overlaps(offset, offset2, size) ||
2063                          offset2 + size > maxfilelen);
2064                 break;
2065         }
2066
2067 have_op:
2068
2069         switch (op) {
2070         case OP_MAPREAD:
2071                 if (!mapped_reads)
2072                         op = OP_READ;
2073                 break;
2074         case OP_MAPWRITE:
2075                 if (!mapped_writes)
2076                         op = OP_WRITE;
2077                 break;
2078         case OP_FALLOCATE:
2079                 if (!fallocate_calls) {
2080                         log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
2081                         goto out;
2082                 }
2083                 break;
2084         case OP_PUNCH_HOLE:
2085                 if (!punch_hole_calls) {
2086                         log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
2087                         goto out;
2088                 }
2089                 break;
2090         case OP_ZERO_RANGE:
2091                 if (!zero_range_calls) {
2092                         log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
2093                         goto out;
2094                 }
2095                 break;
2096         case OP_COLLAPSE_RANGE:
2097                 if (!collapse_range_calls) {
2098                         log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
2099                         goto out;
2100                 }
2101                 break;
2102         case OP_INSERT_RANGE:
2103                 if (!insert_range_calls) {
2104                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
2105                         goto out;
2106                 }
2107                 break;
2108         case OP_CLONE_RANGE:
2109                 if (!clone_range_calls) {
2110                         log5(op, offset, size, offset2, FL_SKIPPED);
2111                         goto out;
2112                 }
2113                 break;
2114         case OP_DEDUPE_RANGE:
2115                 if (!dedupe_range_calls) {
2116                         log5(op, offset, size, offset2, FL_SKIPPED);
2117                         goto out;
2118                 }
2119                 break;
2120         case OP_COPY_RANGE:
2121                 if (!copy_range_calls) {
2122                         log5(op, offset, size, offset2, FL_SKIPPED);
2123                         goto out;
2124                 }
2125                 break;
2126         }
2127
2128         switch (op) {
2129         case OP_READ:
2130                 TRIM_OFF_LEN(offset, size, file_size);
2131                 doread(offset, size);
2132                 break;
2133
2134         case OP_WRITE:
2135                 TRIM_OFF_LEN(offset, size, maxfilelen);
2136                 dowrite(offset, size);
2137                 break;
2138
2139         case OP_MAPREAD:
2140                 TRIM_OFF_LEN(offset, size, file_size);
2141                 domapread(offset, size);
2142                 break;
2143
2144         case OP_MAPWRITE:
2145                 TRIM_OFF_LEN(offset, size, maxfilelen);
2146                 domapwrite(offset, size);
2147                 break;
2148
2149         case OP_TRUNCATE:
2150                 dotruncate(size);
2151                 break;
2152
2153         case OP_FALLOCATE:
2154                 TRIM_OFF_LEN(offset, size, maxfilelen);
2155                 do_preallocate(offset, size, keep_size);
2156                 break;
2157
2158         case OP_PUNCH_HOLE:
2159                 TRIM_OFF_LEN(offset, size, file_size);
2160                 do_punch_hole(offset, size);
2161                 break;
2162         case OP_ZERO_RANGE:
2163                 TRIM_OFF_LEN(offset, size, maxfilelen);
2164                 do_zero_range(offset, size, keep_size);
2165                 break;
2166         case OP_COLLAPSE_RANGE:
2167                 TRIM_OFF_LEN(offset, size, file_size - 1);
2168                 offset = offset & ~(block_size - 1);
2169                 size = size & ~(block_size - 1);
2170                 if (size == 0) {
2171                         log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
2172                         goto out;
2173                 }
2174                 do_collapse_range(offset, size);
2175                 break;
2176         case OP_INSERT_RANGE:
2177                 TRIM_OFF(offset, file_size);
2178                 TRIM_LEN(file_size, size, maxfilelen);
2179                 offset = offset & ~(block_size - 1);
2180                 size = size & ~(block_size - 1);
2181                 if (size == 0) {
2182                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
2183                         goto out;
2184                 }
2185                 if (file_size + size > maxfilelen) {
2186                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
2187                         goto out;
2188                 }
2189
2190                 do_insert_range(offset, size);
2191                 break;
2192         case OP_CLONE_RANGE:
2193                 if (size == 0) {
2194                         log5(OP_CLONE_RANGE, offset, size, offset2, FL_SKIPPED);
2195                         goto out;
2196                 }
2197                 if (offset2 + size > maxfilelen) {
2198                         log5(OP_CLONE_RANGE, offset, size, offset2, FL_SKIPPED);
2199                         goto out;
2200                 }
2201
2202                 do_clone_range(offset, size, offset2);
2203                 break;
2204         case OP_DEDUPE_RANGE:
2205                 if (size == 0) {
2206                         log5(OP_DEDUPE_RANGE, offset, size, offset2, FL_SKIPPED);
2207                         goto out;
2208                 }
2209                 if (offset2 + size > maxfilelen) {
2210                         log5(OP_DEDUPE_RANGE, offset, size, offset2, FL_SKIPPED);
2211                         goto out;
2212                 }
2213
2214                 do_dedupe_range(offset, size, offset2);
2215                 break;
2216         case OP_COPY_RANGE:
2217                 if (size == 0) {
2218                         log5(OP_COPY_RANGE, offset, size, offset2, FL_SKIPPED);
2219                         goto out;
2220                 }
2221                 if (offset2 + size > maxfilelen) {
2222                         log5(OP_COPY_RANGE, offset, size, offset2, FL_SKIPPED);
2223                         goto out;
2224                 }
2225
2226                 do_copy_range(offset, size, offset2);
2227                 break;
2228         case OP_FSYNC:
2229                 dofsync();
2230                 break;
2231         default:
2232                 prterr("test: unknown operation");
2233                 report_failure(42);
2234                 break;
2235         }
2236
2237         if (check_file && testcalls > simulatedopcount)
2238                 check_contents();
2239
2240 out:
2241         if (closeopen)
2242                 docloseopen();
2243         if (sizechecks && testcalls > simulatedopcount)
2244                 check_size();
2245         return 1;
2246 }
2247
2248
2249 void
2250 usage(void)
2251 {
2252         fprintf(stdout, "usage: %s",
2253                 "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\
2254         -b opnum: beginning operation number (default 1)\n\
2255         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
2256         -d: debug output for all operations\n\
2257         -f flush and invalidate cache after I/O\n\
2258         -g X: write character X instead of random generated data\n\
2259         -i logdev: do integrity testing, logdev is the dm log writes device\n\
2260         -j logid: prefix debug log messsages with this id\n\
2261         -k: do not truncate existing file and use its size as upper bound on file size\n\
2262         -l flen: the upper bound on file size (default 262144)\n\
2263         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
2264         -n: no verifications of file size\n\
2265         -o oplen: the upper bound on operation size (default 65536)\n\
2266         -p progressinterval: debug output at specified operation interval\n\
2267         -q: quieter operation\n\
2268         -r readbdy: 4096 would make reads page aligned (default 1)\n\
2269         -s style: 1 gives smaller truncates (default 0)\n\
2270         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
2271         -w writebdy: 4096 would make writes page aligned (default 1)\n\
2272         -x: preallocate file space before starting, XFS only (default 0)\n\
2273         -y synchronize changes to a file\n"
2274
2275 #ifdef AIO
2276 "       -A: Use the AIO system calls\n"
2277 #endif
2278 "       -D startingop: debug output starting at specified operation\n"
2279 #ifdef HAVE_LINUX_FALLOC_H
2280 "       -F: Do not use fallocate (preallocation) calls\n"
2281 #endif
2282 #ifdef FALLOC_FL_PUNCH_HOLE
2283 "       -H: Do not use punch hole calls\n"
2284 #endif
2285 #ifdef FALLOC_FL_ZERO_RANGE
2286 "       -z: Do not use zero range calls\n"
2287 #endif
2288 #ifdef FALLOC_FL_COLLAPSE_RANGE
2289 "       -C: Do not use collapse range calls\n"
2290 #endif
2291 #ifdef FALLOC_FL_INSERT_RANGE
2292 "       -I: Do not use insert range calls\n"
2293 #endif
2294 #ifdef FICLONERANGE
2295 "       -J: Do not use clone range calls\n"
2296 #endif
2297 #ifdef FIDEDUPERANGE
2298 "       -B: Do not use dedupe range calls\n"
2299 #endif
2300 #ifdef HAVE_COPY_FILE_RANGE
2301 "       -E: Do not use copy range calls\n"
2302 #endif
2303 "       -L: fsxLite - no file creations & no file size changes\n\
2304         -N numops: total # operations to do (default infinity)\n\
2305         -O: use oplen (see -o flag) for every op (default random)\n\
2306         -P: save .fsxlog .fsxops and .fsxgood files in dirpath (default ./)\n\
2307         -S seed: for random # generator (default 1) 0 gets timestamp\n\
2308         -W: mapped write operations DISabled\n\
2309         -X: Read file and compare to good buffer after every operation.\n\
2310         -R: read() system calls only (mapped reads disabled)\n\
2311         -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
2312         --replay-ops opsfile: replay ops from recorded .fsxops file\n\
2313         --record-ops[=opsfile]: dump ops file also on success. optionally specify ops file name\n\
2314         fname: this filename is REQUIRED (no default)\n");
2315         exit(90);
2316 }
2317
2318
2319 long long
2320 getnum(char *s, char **e)
2321 {
2322         long long ret;
2323
2324         *e = (char *) 0;
2325         ret = strtoll(s, e, 0);
2326         if (*e)
2327                 switch (**e) {
2328                 case 'b':
2329                 case 'B':
2330                         ret *= 512;
2331                         *e = *e + 1;
2332                         break;
2333                 case 'k':
2334                 case 'K':
2335                         ret *= 1024;
2336                         *e = *e + 1;
2337                         break;
2338                 case 'm':
2339                 case 'M':
2340                         ret *= 1024*1024;
2341                         *e = *e + 1;
2342                         break;
2343                 case 'w':
2344                 case 'W':
2345                         ret *= 4;
2346                         *e = *e + 1;
2347                         break;
2348                 }
2349         return (ret);
2350 }
2351
2352 #ifdef AIO
2353
2354 #define QSZ     1024
2355 io_context_t    io_ctx;
2356 struct iocb     iocb;
2357
2358 int aio_setup()
2359 {
2360         int ret;
2361         ret = io_queue_init(QSZ, &io_ctx);
2362         if (ret != 0) {
2363                 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
2364                         strerror(ret));
2365                 return(-1);
2366         }
2367         return(0);
2368 }
2369
2370 int
2371 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
2372 {
2373         struct io_event event;
2374         static struct timespec ts;
2375         struct iocb *iocbs[] = { &iocb };
2376         int ret;
2377         long res;
2378
2379         if (rw == READ) {
2380                 io_prep_pread(&iocb, fd, buf, len, offset);
2381         } else {
2382                 io_prep_pwrite(&iocb, fd, buf, len, offset);
2383         }
2384
2385         ts.tv_sec = 30;
2386         ts.tv_nsec = 0;
2387         ret = io_submit(io_ctx, 1, iocbs);
2388         if (ret != 1) {
2389                 fprintf(stderr, "errcode=%d\n", ret);
2390                 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
2391                                 strerror(ret));
2392                 goto out_error;
2393         }
2394
2395         ret = io_getevents(io_ctx, 1, 1, &event, &ts);
2396         if (ret != 1) {
2397                 if (ret == 0)
2398                         fprintf(stderr, "aio_rw: no events available\n");
2399                 else {
2400                         fprintf(stderr, "errcode=%d\n", -ret);
2401                         fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
2402                                         strerror(-ret));
2403                 }
2404                 goto out_error;
2405         }
2406         if (len != event.res) {
2407                 /*
2408                  * The b0rked libaio defines event.res as unsigned.
2409                  * However the kernel strucuture has it signed,
2410                  * and it's used to pass negated error value.
2411                  * Till the library is fixed use the temp var.
2412                  */
2413                 res = (long)event.res;
2414                 if (res >= 0)
2415                         fprintf(stderr, "bad io length: %lu instead of %u\n",
2416                                         res, len);
2417                 else {
2418                         fprintf(stderr, "errcode=%ld\n", -res);
2419                         fprintf(stderr, "aio_rw: async io failed: %s\n",
2420                                         strerror(-res));
2421                         ret = res;
2422                         goto out_error;
2423                 }
2424
2425         }
2426         return event.res;
2427
2428 out_error:
2429         /*
2430          * The caller expects error return in traditional libc
2431          * convention, i.e. -1 and the errno set to error.
2432          */
2433         errno = -ret;
2434         return -1;
2435 }
2436
2437 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
2438 {
2439         int ret;
2440
2441         if (aio) {
2442                 ret = __aio_rw(rw, fd, buf, len, offset);
2443         } else {
2444                 if (rw == READ)
2445                         ret = read(fd, buf, len);
2446                 else
2447                         ret = write(fd, buf, len);
2448         }
2449         return ret;
2450 }
2451
2452 #endif
2453
2454 #define test_fallocate(mode) __test_fallocate(mode, #mode)
2455
2456 int
2457 __test_fallocate(int mode, const char *mode_str)
2458 {
2459 #ifdef HAVE_LINUX_FALLOC_H
2460         int ret = 0;
2461         if (!lite) {
2462                 if (fallocate(fd, mode, file_size, 1) &&
2463                     (errno == ENOSYS || errno == EOPNOTSUPP)) {
2464                         if(!quiet)
2465                                 fprintf(stderr,
2466                                         "main: filesystem does not support "
2467                                         "fallocate mode %s, disabling!\n",
2468                                         mode_str);
2469                 } else {
2470                         ret = 1;
2471                         if (ftruncate(fd, file_size)) {
2472                                 warn("main: ftruncate");
2473                                 exit(132);
2474                         }
2475                 }
2476         }
2477         return ret;
2478 #endif
2479 }
2480
2481 static struct option longopts[] = {
2482         {"replay-ops", required_argument, 0, 256},
2483         {"record-ops", optional_argument, 0, 255},
2484         { }
2485 };
2486
2487 int
2488 main(int argc, char **argv)
2489 {
2490         int     i, style, ch;
2491         char    *endp, *tmp;
2492         char logfile[PATH_MAX];
2493         struct stat statbuf;
2494         int o_flags = O_RDWR|O_CREAT|O_TRUNC;
2495
2496         logfile[0] = 0;
2497         dname[0] = 0;
2498
2499         page_size = getpagesize();
2500         page_mask = page_size - 1;
2501         mmap_mask = page_mask;
2502         
2503
2504         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
2505
2506         while ((ch = getopt_long(argc, argv,
2507                                  "b:c:dfg:i:j:kl:m:no:p:qr:s:t:w:xyABD:EFJKHzCILN:OP:RS:WXZ",
2508                                  longopts, NULL)) != EOF)
2509                 switch (ch) {
2510                 case 'b':
2511                         simulatedopcount = getnum(optarg, &endp);
2512                         if (!quiet)
2513                                 prt("Will begin at operation %lld\n",
2514                                                 simulatedopcount);
2515                         if (simulatedopcount == 0)
2516                                 usage();
2517                         simulatedopcount -= 1;
2518                         break;
2519                 case 'c':
2520                         closeprob = getnum(optarg, &endp);
2521                         if (!quiet)
2522                                 prt("Chance of close/open is 1 in %d\n", closeprob);
2523                         if (closeprob <= 0)
2524                                 usage();
2525                         break;
2526                 case 'd':
2527                         debug = 1;
2528                         break;
2529                 case 'f':
2530                         flush = 1;
2531                         break;
2532                 case 'g':
2533                         filldata = *optarg;
2534                         break;
2535                 case 'i':
2536                         integrity = 1;
2537                         logdev = strdup(optarg);
2538                         if (!logdev) {
2539                                 prterr("strdup");
2540                                 exit(101);
2541                         }
2542                         break;
2543                 case 'j':
2544                         logid = strdup(optarg);
2545                         if (!logid) {
2546                                 prterr("strdup");
2547                                 exit(101);
2548                         }
2549                         break;
2550                 case 'k':
2551                         o_flags &= ~O_TRUNC;
2552                         break;
2553                 case 'l':
2554                         maxfilelen = getnum(optarg, &endp);
2555                         if (maxfilelen <= 0)
2556                                 usage();
2557                         break;
2558                 case 'm':
2559                         monitorstart = getnum(optarg, &endp);
2560                         if (monitorstart < 0)
2561                                 usage();
2562                         if (!endp || *endp++ != ':')
2563                                 usage();
2564                         monitorend = getnum(endp, &endp);
2565                         if (monitorend < 0)
2566                                 usage();
2567                         if (monitorend == 0)
2568                                 monitorend = -1; /* aka infinity */
2569                         debug = 1;
2570                 case 'n':
2571                         sizechecks = 0;
2572                         break;
2573                 case 'o':
2574                         maxoplen = getnum(optarg, &endp);
2575                         if (maxoplen <= 0)
2576                                 usage();
2577                         break;
2578                 case 'p':
2579                         progressinterval = getnum(optarg, &endp);
2580                         if (progressinterval == 0)
2581                                 usage();
2582                         break;
2583                 case 'q':
2584                         quiet = 1;
2585                         break;
2586                 case 'r':
2587                         readbdy = getnum(optarg, &endp);
2588                         if (readbdy <= 0)
2589                                 usage();
2590                         break;
2591                 case 's':
2592                         style = getnum(optarg, &endp);
2593                         if (style < 0 || style > 1)
2594                                 usage();
2595                         break;
2596                 case 't':
2597                         truncbdy = getnum(optarg, &endp);
2598                         if (truncbdy <= 0)
2599                                 usage();
2600                         break;
2601                 case 'w':
2602                         writebdy = getnum(optarg, &endp);
2603                         if (writebdy <= 0)
2604                                 usage();
2605                         break;
2606                 case 'x':
2607                         prealloc = 1;
2608                         break;
2609                 case 'y':
2610                         do_fsync = 1;
2611                         break;
2612                 case 'A':
2613                         aio = 1;
2614                         break;
2615                 case 'D':
2616                         debugstart = getnum(optarg, &endp);
2617                         if (debugstart < 1)
2618                                 usage();
2619                         break;
2620                 case 'F':
2621                         fallocate_calls = 0;
2622                         break;
2623                 case 'K':
2624                         keep_size_calls = 0;
2625                         break;
2626                 case 'H':
2627                         punch_hole_calls = 0;
2628                         break;
2629                 case 'z':
2630                         zero_range_calls = 0;
2631                         break;
2632                 case 'C':
2633                         collapse_range_calls = 0;
2634                         break;
2635                 case 'I':
2636                         insert_range_calls = 0;
2637                         break;
2638                 case 'J':
2639                         clone_range_calls = 0;
2640                         break;
2641                 case 'B':
2642                         dedupe_range_calls = 0;
2643                         break;
2644                 case 'E':
2645                         copy_range_calls = 0;
2646                         break;
2647                 case 'L':
2648                         lite = 1;
2649                         o_flags &= ~(O_CREAT|O_TRUNC);
2650                         break;
2651                 case 'N':
2652                         numops = getnum(optarg, &endp);
2653                         if (numops < 0)
2654                                 usage();
2655                         break;
2656                 case 'O':
2657                         randomoplen = 0;
2658                         break;
2659                 case 'P':
2660                         strncpy(dname, optarg, sizeof(dname));
2661                         strcat(dname, "/");
2662                         dirpath = strlen(dname);
2663                         break;
2664                 case 'R':
2665                         mapped_reads = 0;
2666                         break;
2667                 case 'S':
2668                         seed = getnum(optarg, &endp);
2669                         if (seed == 0) {
2670                                 seed = time(0) % 10000;
2671                                 seed += (int)getpid();
2672                         }
2673                         if (seed < 0)
2674                                 usage();
2675                         break;
2676                 case 'W':
2677                         mapped_writes = 0;
2678                         if (!quiet)
2679                                 prt("mapped writes DISABLED\n");
2680                         break;
2681                 case 'X':
2682                         check_file = 1;
2683                         break;
2684                 case 'Z':
2685                         o_direct = O_DIRECT;
2686                         o_flags |= O_DIRECT;
2687                         break;
2688                 case 255:  /* --record-ops */
2689                         if (optarg)
2690                                 strncpy(opsfile, optarg, sizeof(opsfile));
2691                         recordops = opsfile;
2692                         break;
2693                 case 256:  /* --replay-ops */
2694                         replayops = optarg;
2695                         break;
2696                 default:
2697                         usage();
2698                         /* NOTREACHED */
2699                 }
2700         argc -= optind;
2701         argv += optind;
2702         if (argc != 1)
2703                 usage();
2704
2705         if (integrity && !dirpath) {
2706                 fprintf(stderr, "option -i <logdev> requires -P <dirpath>\n");
2707                 usage();
2708         }
2709
2710         fname = argv[0];
2711         tmp = strdup(fname);
2712         if (!tmp) {
2713                 prterr("strdup");
2714                 exit(101);
2715         }
2716         bname = basename(tmp);
2717
2718         signal(SIGHUP,  cleanup);
2719         signal(SIGINT,  cleanup);
2720         signal(SIGPIPE, cleanup);
2721         signal(SIGALRM, cleanup);
2722         signal(SIGTERM, cleanup);
2723         signal(SIGXCPU, cleanup);
2724         signal(SIGXFSZ, cleanup);
2725         signal(SIGVTALRM,       cleanup);
2726         signal(SIGUSR1, cleanup);
2727         signal(SIGUSR2, cleanup);
2728
2729         if (!quiet && seed)
2730                 prt("Seed set to %d\n", seed);
2731         srandom(seed);
2732         fd = open(fname, o_flags, 0666);
2733         if (fd < 0) {
2734                 prterr(fname);
2735                 exit(91);
2736         }
2737         if (fstat(fd, &statbuf)) {
2738                 prterr("check_size: fstat");
2739                 exit(91);
2740         }
2741         block_size = statbuf.st_blksize;
2742 #ifdef XFS
2743         if (prealloc) {
2744                 xfs_flock64_t   resv = { 0 };
2745 #ifdef HAVE_XFS_PLATFORM_DEFS_H
2746                 if (!platform_test_xfs_fd(fd)) {
2747                         prterr(fname);
2748                         fprintf(stderr, "main: cannot prealloc, non XFS\n");
2749                         exit(96);
2750                 }
2751 #endif
2752                 resv.l_len = maxfilelen;
2753                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
2754                         prterr(fname);
2755                         exit(97);
2756                 }
2757         }
2758 #endif
2759
2760         if (dirpath) {
2761                 snprintf(goodfile, sizeof(goodfile), "%s%s.fsxgood", dname, bname);
2762                 snprintf(logfile, sizeof(logfile), "%s%s.fsxlog", dname, bname);
2763                 if (!*opsfile)
2764                         snprintf(opsfile, sizeof(opsfile), "%s%s.fsxops", dname, bname);
2765         } else {
2766                 snprintf(goodfile, sizeof(goodfile), "%s.fsxgood", fname);
2767                 snprintf(logfile, sizeof(logfile), "%s.fsxlog", fname);
2768                 if (!*opsfile)
2769                         snprintf(opsfile, sizeof(opsfile), "%s.fsxops", fname);
2770         }
2771         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
2772         if (fsxgoodfd < 0) {
2773                 prterr(goodfile);
2774                 exit(92);
2775         }
2776         fsxlogf = fopen(logfile, "w");
2777         if (fsxlogf == NULL) {
2778                 prterr(logfile);
2779                 exit(93);
2780         }
2781         unlink(opsfile);
2782
2783         if (replayops) {
2784                 replayopsf = fopen(replayops, "r");
2785                 if (!replayopsf) {
2786                         prterr(replayops);
2787                         exit(93);
2788                 }
2789         }
2790
2791 #ifdef AIO
2792         if (aio) 
2793                 aio_setup();
2794 #endif
2795
2796         if (!(o_flags & O_TRUNC)) {
2797                 off_t ret;
2798                 file_size = maxfilelen = biggest = lseek(fd, (off_t)0, SEEK_END);
2799                 if (file_size == (off_t)-1) {
2800                         prterr(fname);
2801                         warn("main: lseek eof");
2802                         exit(94);
2803                 }
2804                 ret = lseek(fd, (off_t)0, SEEK_SET);
2805                 if (ret == (off_t)-1) {
2806                         prterr(fname);
2807                         warn("main: lseek 0");
2808                         exit(95);
2809                 }
2810         }
2811         original_buf = (char *) malloc(maxfilelen);
2812         for (i = 0; i < maxfilelen; i++)
2813                 original_buf[i] = random() % 256;
2814         good_buf = (char *) malloc(maxfilelen + writebdy);
2815         good_buf = round_ptr_up(good_buf, writebdy, 0);
2816         memset(good_buf, '\0', maxfilelen);
2817         temp_buf = (char *) malloc(maxoplen + readbdy);
2818         temp_buf = round_ptr_up(temp_buf, readbdy, 0);
2819         memset(temp_buf, '\0', maxoplen);
2820         if (lite) {     /* zero entire existing file */
2821                 ssize_t written;
2822
2823                 written = write(fd, good_buf, (size_t)maxfilelen);
2824                 if (written != maxfilelen) {
2825                         if (written == -1) {
2826                                 prterr(fname);
2827                                 warn("main: error on write");
2828                         } else
2829                                 warn("main: short write, 0x%x bytes instead "
2830                                         "of 0x%lx\n",
2831                                         (unsigned)written,
2832                                         maxfilelen);
2833                         exit(98);
2834                 }
2835         } else {
2836                 ssize_t ret, len = file_size;
2837                 off_t off = 0;
2838
2839                 while (len > 0) {
2840                         ret = read(fd, good_buf + off, len);
2841                         if (ret == -1) {
2842                                 prterr(fname);
2843                                 warn("main: error on read");
2844                                 exit(98);
2845                         }
2846                         len -= ret;
2847                         off += ret;
2848                 }
2849
2850                 check_trunc_hack();
2851         }
2852
2853         if (fallocate_calls)
2854                 fallocate_calls = test_fallocate(0);
2855         if (keep_size_calls)
2856                 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
2857         if (punch_hole_calls)
2858                 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2859         if (zero_range_calls)
2860                 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
2861         if (collapse_range_calls)
2862                 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
2863         if (insert_range_calls)
2864                 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
2865         if (clone_range_calls)
2866                 clone_range_calls = test_clone_range();
2867         if (dedupe_range_calls)
2868                 dedupe_range_calls = test_dedupe_range();
2869         if (copy_range_calls)
2870                 copy_range_calls = test_copy_range();
2871
2872         while (numops == -1 || numops--)
2873                 if (!test())
2874                         break;
2875
2876         free(tmp);
2877         if (close(fd)) {
2878                 prterr("close");
2879                 report_failure(99);
2880         }
2881         prt("All %lld operations completed A-OK!\n", testcalls);
2882         if (recordops)
2883                 logdump();
2884
2885         exit(0);
2886         return 0;
2887 }