2 * Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserverd.
5 * Author: Avadis Tevanian, Jr.
7 * File system exerciser.
9 * Rewritten 8/98 by Conrad Minshall.
11 * Small changes to work under Linux -- davej.
13 * Checks for mmap last-page zero fill.
42 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
46 enum opflags { FL_NONE = 0, FL_SKIPPED = 1, FL_CLOSE_OPEN = 2, FL_KEEP_SIZE = 4 };
49 * A log entry is an operation and a bunch of arguments.
60 struct log_entry oplog[LOGSIZE]; /* the log */
61 int logptr = 0; /* current position in log */
62 int logcount = 0; /* total ops */
65 * The operation matrix is complex due to conditional execution of different
66 * features. Hence when we come to deciding what operation to run, we need to
67 * be careful in how we select the different operations. The active operations
68 * are mapped to numbers as follows:
70 * lite !lite integrity
79 * COLLAPSE RANGE: - 8 8
82 * When mapped read/writes are disabled, they are simply converted to normal
83 * reads and writes. When fallocate/fpunch calls are disabled, they are
86 * Because of the "lite" version, we also need to have different "maximum
87 * operation" defines to allow the ops to be selected correctly based on the
92 /* common operations */
99 /* !lite operations */
100 OP_TRUNCATE = OP_MAX_LITE,
108 /* integrity operations */
109 OP_FSYNC = OP_MAX_FULL,
114 #define PAGE_SIZE getpagesize()
116 #define PAGE_MASK (PAGE_SIZE - 1)
118 char *original_buf; /* a pointer to the original data */
119 char *good_buf; /* a pointer to the correct data */
120 char *temp_buf; /* a pointer to the current data */
121 char *fname; /* name of our test file */
122 char *bname; /* basename of our test file */
123 char *logdev; /* -i flag */
124 char *logid; /* -j flag */
125 char dname[1024]; /* -P flag */
126 char goodfile[PATH_MAX];
127 int dirpath = 0; /* -P flag */
128 int fd; /* fd for our test file */
130 blksize_t block_size = 0;
133 unsigned long testcalls = 0; /* calls to function "test" */
135 unsigned long simulatedopcount = 0; /* -b flag */
136 int closeprob = 0; /* -c flag */
137 int debug = 0; /* -d flag */
138 unsigned long debugstart = 0; /* -D flag */
139 char filldata = 0; /* -g flag */
140 int flush = 0; /* -f flag */
141 int do_fsync = 0; /* -y flag */
142 unsigned long maxfilelen = 256 * 1024; /* -l flag */
143 int sizechecks = 1; /* -n flag disables them */
144 int maxoplen = 64 * 1024; /* -o flag */
145 int quiet = 0; /* -q flag */
146 unsigned long progressinterval = 0; /* -p flag */
147 int readbdy = 1; /* -r flag */
148 int style = 0; /* -s flag */
149 int prealloc = 0; /* -x flag */
150 int truncbdy = 1; /* -t flag */
151 int writebdy = 1; /* -w flag */
152 long monitorstart = -1; /* -m flag */
153 long monitorend = -1; /* -m flag */
154 int lite = 0; /* -L flag */
155 long numops = -1; /* -N flag */
156 int randomoplen = 1; /* -O flag disables it */
157 int seed = 1; /* -S flag */
158 int mapped_writes = 1; /* -W flag disables */
159 int fallocate_calls = 1; /* -F flag disables */
160 int keep_size_calls = 1; /* -K flag disables */
161 int punch_hole_calls = 1; /* -H flag disables */
162 int zero_range_calls = 1; /* -z flag disables */
163 int collapse_range_calls = 1; /* -C flag disables */
164 int insert_range_calls = 1; /* -I flag disables */
165 int mapped_reads = 1; /* -R flag disables it */
166 int check_file = 0; /* -X flag enables */
167 int integrity = 0; /* -i flag */
169 int o_direct; /* -Z */
177 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
180 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
181 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
183 #define fsxread(a,b,c,d) read(a,b,c)
184 #define fsxwrite(a,b,c,d) write(a,b,c)
187 const char *replayops = NULL;
188 const char *recordops = NULL;
189 FILE * fsxlogf = NULL;
190 FILE * replayopsf = NULL;
191 char opsfile[PATH_MAX];
195 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
197 unsigned long ret = (unsigned long)ptr;
199 ret = ((ret + align - 1) & ~(align - 1));
205 vwarnc(int code, const char *fmt, va_list ap)
208 fprintf(stderr, "%s: ", logid);
209 fprintf(stderr, "fsx: ");
211 vfprintf(stderr, fmt, ap);
212 fprintf(stderr, ": ");
214 fprintf(stderr, "%s\n", strerror(code));
218 warn(const char * fmt, ...) {
221 vwarnc(errno, fmt, ap);
226 prt(const char *fmt, ...)
231 fprintf(stdout, "%s: ", logid);
233 vfprintf(stdout, fmt, args);
237 vfprintf(fsxlogf, fmt, args);
243 prterr(const char *prefix)
245 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
249 static const char *op_names[] = {
251 [OP_WRITE] = "write",
252 [OP_MAPREAD] = "mapread",
253 [OP_MAPWRITE] = "mapwrite",
254 [OP_TRUNCATE] = "truncate",
255 [OP_FALLOCATE] = "fallocate",
256 [OP_PUNCH_HOLE] = "punch_hole",
257 [OP_ZERO_RANGE] = "zero_range",
258 [OP_COLLAPSE_RANGE] = "collapse_range",
259 [OP_INSERT_RANGE] = "insert_range",
260 [OP_FSYNC] = "fsync",
263 static const char *op_name(int operation)
265 if (operation >= 0 &&
266 operation < sizeof(op_names) / sizeof(op_names[0]))
267 return op_names[operation];
271 static int op_code(const char *name)
275 for (i = 0; i < sizeof(op_names) / sizeof(op_names[0]); i++)
276 if (op_names[i] && strcmp(name, op_names[i]) == 0)
282 log4(int operation, int arg0, int arg1, enum opflags flags)
284 struct log_entry *le;
287 le->operation = operation;
289 flags |= FL_CLOSE_OPEN;
292 le->args[2] = file_size;
296 if (logptr >= LOGSIZE)
306 struct log_entry *lp;
308 prt("LOG DUMP (%d total operations):\n", logcount);
310 logopsf = fopen(opsfile, "w");
314 if (logcount < LOGSIZE) {
321 for ( ; count > 0; count--) {
325 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
326 prt("%d(%3d mod 256): ", opnum, opnum%256);
329 overlap = badoff >= lp->args[0] &&
330 badoff < lp->args[0] + lp->args[1];
332 if (lp->flags & FL_SKIPPED) {
333 prt("SKIPPED (no operation)");
337 switch (lp->operation) {
339 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
340 lp->args[0], lp->args[0] + lp->args[1] - 1,
346 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
347 lp->args[0], lp->args[0] + lp->args[1] - 1,
353 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
354 lp->args[0], lp->args[0] + lp->args[1] - 1,
360 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
361 lp->args[0], lp->args[0] + lp->args[1] - 1,
363 if (lp->args[0] > lp->args[2])
365 else if (lp->args[0] + lp->args[1] > lp->args[2])
367 overlap = (badoff >= lp->args[0] ||
368 badoff >=lp->args[2]) &&
369 badoff < lp->args[0] + lp->args[1];
374 down = lp->args[1] < lp->args[2];
375 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
376 down ? "DOWN" : "UP", lp->args[2], lp->args[1]);
377 overlap = badoff >= lp->args[1 + !down] &&
378 badoff < lp->args[1 + !!down];
383 /* 0: offset 1: length 2: where alloced */
384 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) ",
385 lp->args[0], lp->args[0] + lp->args[1],
387 if (lp->args[0] + lp->args[1] <= lp->args[2])
389 else if (lp->flags & FL_KEEP_SIZE)
397 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
398 lp->args[0], lp->args[0] + lp->args[1] - 1,
404 prt("ZERO 0x%x thru 0x%x\t(0x%x bytes)",
405 lp->args[0], lp->args[0] + lp->args[1] - 1,
410 case OP_COLLAPSE_RANGE:
411 prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
412 lp->args[0], lp->args[0] + lp->args[1] - 1,
417 case OP_INSERT_RANGE:
418 prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
419 lp->args[0], lp->args[0] + lp->args[1] - 1,
428 prt("BOGUS LOG ENTRY (operation code = %d)!",
434 if (lp->flags & FL_CLOSE_OPEN)
435 prt("\n\t\tCLOSE/OPEN");
442 if (lp->flags & FL_SKIPPED)
443 fprintf(logopsf, "skip ");
444 fprintf(logopsf, "%s 0x%x 0x%x 0x%x",
445 op_name(lp->operation),
446 lp->args[0], lp->args[1], lp->args[2]);
447 if (lp->flags & FL_KEEP_SIZE)
448 fprintf(logopsf, " keep_size");
449 if (lp->flags & FL_CLOSE_OPEN)
450 fprintf(logopsf, " close_open");
452 fprintf(logopsf, " *");
453 fprintf(logopsf, "\n");
458 if (fclose(logopsf) != 0)
461 prt("Log of operations saved to \"%s\"; "
462 "replay with --replay-ops\n",
469 save_buffer(char *buffer, off_t bufferlength, int fd)
472 ssize_t byteswritten;
474 if (fd <= 0 || bufferlength == 0)
477 if (bufferlength > SSIZE_MAX) {
478 prt("fsx flaw: overflow in save_buffer\n");
482 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
483 if (size_by_seek == (off_t)-1)
484 prterr("save_buffer: lseek eof");
485 else if (bufferlength > size_by_seek) {
486 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
487 (unsigned long long)bufferlength);
488 bufferlength = size_by_seek;
492 ret = lseek(fd, (off_t)0, SEEK_SET);
493 if (ret == (off_t)-1)
494 prterr("save_buffer: lseek 0");
496 byteswritten = write(fd, buffer, (size_t)bufferlength);
497 if (byteswritten != bufferlength) {
498 if (byteswritten == -1)
499 prterr("save_buffer write");
501 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
502 (unsigned)byteswritten,
503 (unsigned long long)bufferlength);
509 report_failure(int status)
515 save_buffer(good_buf, file_size, fsxgoodfd);
516 prt("Correct content saved for comparison\n");
517 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
526 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
527 *(((unsigned char *)(cp)) + 1)))
535 snprintf(command, 256, "dmsetup message %s 0 mark %s.mark%d", logdev,
537 ret = system(command);
539 prterr("dmsetup mark failed");
545 dump_fsync_buffer(void)
547 char fname_buffer[PATH_MAX];
553 snprintf(fname_buffer, sizeof(fname_buffer), "%s%s.mark%d", dname,
555 good_fd = open(fname_buffer, O_WRONLY|O_CREAT|O_TRUNC, 0666);
557 prterr(fname_buffer);
561 save_buffer(good_buf, file_size, good_fd);
563 prt("Dumped fsync buffer to %s\n", fname_buffer + dirpath);
567 check_buffers(char *buf, unsigned offset, unsigned size)
575 if (memcmp(good_buf + offset, buf, size) != 0) {
576 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
577 offset, size, fname);
578 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
580 c = good_buf[offset];
584 bad = short_at(&buf[i]);
585 prt("0x%05x\t0x%04x\t0x%04x", offset,
586 short_at(&good_buf[offset]), bad);
587 op = buf[offset & 1 ? i+1 : i];
588 prt("\t0x%05x\n", n);
590 prt("operation# (mod 256) for "
591 "the bad data may be %u\n",
592 ((unsigned)op & 0xff));
594 prt("operation# (mod 256) for "
595 "the bad data unknown, check"
596 " HOLE and EXTEND ops\n");
616 if (fstat(fd, &statbuf)) {
617 prterr("check_size: fstat");
618 statbuf.st_size = -1;
620 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
621 if (file_size != statbuf.st_size || file_size != size_by_seek) {
622 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
623 (unsigned long long)file_size,
624 (unsigned long long)statbuf.st_size,
625 (unsigned long long)size_by_seek);
632 check_trunc_hack(void)
635 off_t offset = file_size + (off_t)100000;
637 if (ftruncate(fd, file_size))
639 if (ftruncate(fd, offset))
642 if (statbuf.st_size != offset) {
643 prt("no extend on truncate! not posix!\n");
646 if (ftruncate(fd, file_size)) {
648 prterr("check_trunc_hack: ftruncate");
654 doflush(unsigned offset, unsigned size)
660 if (o_direct == O_DIRECT)
663 pg_offset = offset & mmap_mask;
664 map_size = pg_offset + size;
666 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
667 MAP_FILE | MAP_SHARED, fd,
668 (off_t)(offset - pg_offset))) == (char *)-1) {
669 prterr("doflush: mmap");
672 if (msync(p, map_size, MS_INVALIDATE) != 0) {
673 prterr("doflush: msync");
676 if (munmap(p, map_size) != 0) {
677 prterr("doflush: munmap");
683 doread(unsigned offset, unsigned size)
688 offset -= offset % readbdy;
690 size -= size % readbdy;
692 if (!quiet && testcalls > simulatedopcount && !o_direct)
693 prt("skipping zero size read\n");
694 log4(OP_READ, offset, size, FL_SKIPPED);
697 if (size + offset > file_size) {
698 if (!quiet && testcalls > simulatedopcount)
699 prt("skipping seek/read past end of file\n");
700 log4(OP_READ, offset, size, FL_SKIPPED);
704 log4(OP_READ, offset, size, FL_NONE);
706 if (testcalls <= simulatedopcount)
710 ((progressinterval && testcalls % progressinterval == 0) ||
712 (monitorstart == -1 ||
713 (offset + size > monitorstart &&
714 (monitorend == -1 || offset <= monitorend))))))
715 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
716 offset, offset + size - 1, size);
717 ret = lseek(fd, (off_t)offset, SEEK_SET);
718 if (ret == (off_t)-1) {
719 prterr("doread: lseek");
722 iret = fsxread(fd, temp_buf, size, offset);
725 prterr("doread: read");
727 prt("short read: 0x%x bytes instead of 0x%x\n",
731 check_buffers(temp_buf, offset, size);
735 check_eofpage(char *s, unsigned offset, char *p, int size)
737 unsigned long last_page, should_be_zero;
739 if (offset + size <= (file_size & ~page_mask))
742 * we landed in the last page of the file
743 * test to make sure the VM system provided 0's
744 * beyond the true end of the file mapping
745 * (as required by mmap def in 1996 posix 1003.1)
747 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
749 for (should_be_zero = last_page + (file_size & page_mask);
750 should_be_zero < last_page + page_size;
752 if (*(char *)should_be_zero) {
753 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
754 s, file_size - 1, should_be_zero & page_mask,
755 short_at(should_be_zero));
763 static char *check_buf;
765 unsigned size = file_size;
773 check_buf = (char *) malloc(maxfilelen + writebdy);
774 assert(check_buf != NULL);
775 check_buf = round_ptr_up(check_buf, writebdy, 0);
776 memset(check_buf, '\0', maxfilelen);
780 size -= size % readbdy;
784 ret = lseek(fd, (off_t)offset, SEEK_SET);
785 if (ret == (off_t)-1) {
786 prterr("doread: lseek");
790 iret = fsxread(fd, check_buf, size, offset);
793 prterr("check_contents: read");
795 prt("short check read: 0x%x bytes instead of 0x%x\n",
799 check_buffers(check_buf, offset, size);
801 /* Map eof page, check it */
802 map_offset = size - (size & PAGE_MASK);
803 if (map_offset == size)
804 map_offset -= PAGE_SIZE;
805 map_size = size - map_offset;
807 p = mmap(0, map_size, PROT_READ, MAP_SHARED, fd, map_offset);
808 if (p == MAP_FAILED) {
809 prterr("check_contents: mmap");
812 check_eofpage("check_contents", map_offset, p, map_size);
814 if (munmap(p, map_size) != 0) {
815 prterr("check_contents: munmap");
821 domapread(unsigned offset, unsigned size)
827 offset -= offset % readbdy;
829 if (!quiet && testcalls > simulatedopcount)
830 prt("skipping zero size read\n");
831 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
834 if (size + offset > file_size) {
835 if (!quiet && testcalls > simulatedopcount)
836 prt("skipping seek/read past end of file\n");
837 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
841 log4(OP_MAPREAD, offset, size, FL_NONE);
843 if (testcalls <= simulatedopcount)
847 ((progressinterval && testcalls % progressinterval == 0) ||
849 (monitorstart == -1 ||
850 (offset + size > monitorstart &&
851 (monitorend == -1 || offset <= monitorend))))))
852 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
853 offset, offset + size - 1, size);
855 pg_offset = offset & PAGE_MASK;
856 map_size = pg_offset + size;
858 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
859 (off_t)(offset - pg_offset))) == (char *)-1) {
860 prterr("domapread: mmap");
863 memcpy(temp_buf, p + pg_offset, size);
865 check_eofpage("Read", offset, p, size);
867 if (munmap(p, map_size) != 0) {
868 prterr("domapread: munmap");
872 check_buffers(temp_buf, offset, size);
877 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
881 good_buf[offset] = filldata;
883 good_buf[offset] = testcalls % 256;
885 good_buf[offset] += original_buf[offset];
893 dowrite(unsigned offset, unsigned size)
898 offset -= offset % writebdy;
900 size -= size % writebdy;
902 if (!quiet && testcalls > simulatedopcount && !o_direct)
903 prt("skipping zero size write\n");
904 log4(OP_WRITE, offset, size, FL_SKIPPED);
908 log4(OP_WRITE, offset, size, FL_NONE);
910 gendata(original_buf, good_buf, offset, size);
911 if (file_size < offset + size) {
912 if (file_size < offset)
913 memset(good_buf + file_size, '\0', offset - file_size);
914 file_size = offset + size;
916 warn("Lite file size bug in fsx!");
921 if (testcalls <= simulatedopcount)
925 ((progressinterval && testcalls % progressinterval == 0) ||
927 (monitorstart == -1 ||
928 (offset + size > monitorstart &&
929 (monitorend == -1 || offset <= monitorend))))))
930 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
931 offset, offset + size - 1, size);
932 ret = lseek(fd, (off_t)offset, SEEK_SET);
933 if (ret == (off_t)-1) {
934 prterr("dowrite: lseek");
937 iret = fsxwrite(fd, good_buf + offset, size, offset);
940 prterr("dowrite: write");
942 prt("short write: 0x%x bytes instead of 0x%x\n",
948 prt("fsync() failed: %s\n", strerror(errno));
953 doflush(offset, size);
959 domapwrite(unsigned offset, unsigned size)
966 offset -= offset % writebdy;
968 if (!quiet && testcalls > simulatedopcount)
969 prt("skipping zero size write\n");
970 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
973 cur_filesize = file_size;
975 log4(OP_MAPWRITE, offset, size, FL_NONE);
977 gendata(original_buf, good_buf, offset, size);
978 if (file_size < offset + size) {
979 if (file_size < offset)
980 memset(good_buf + file_size, '\0', offset - file_size);
981 file_size = offset + size;
983 warn("Lite file size bug in fsx!");
988 if (testcalls <= simulatedopcount)
992 ((progressinterval && testcalls % progressinterval == 0) ||
994 (monitorstart == -1 ||
995 (offset + size > monitorstart &&
996 (monitorend == -1 || offset <= monitorend))))))
997 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
998 offset, offset + size - 1, size);
1000 if (file_size > cur_filesize) {
1001 if (ftruncate(fd, file_size) == -1) {
1002 prterr("domapwrite: ftruncate");
1006 pg_offset = offset & PAGE_MASK;
1007 map_size = pg_offset + size;
1009 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
1010 MAP_FILE | MAP_SHARED, fd,
1011 (off_t)(offset - pg_offset))) == (char *)-1) {
1012 prterr("domapwrite: mmap");
1013 report_failure(202);
1015 memcpy(p + pg_offset, good_buf + offset, size);
1016 if (msync(p, map_size, MS_SYNC) != 0) {
1017 prterr("domapwrite: msync");
1018 report_failure(203);
1021 check_eofpage("Write", offset, p, size);
1023 if (munmap(p, map_size) != 0) {
1024 prterr("domapwrite: munmap");
1025 report_failure(204);
1031 dotruncate(unsigned size)
1033 int oldsize = file_size;
1035 size -= size % truncbdy;
1036 if (size > biggest) {
1038 if (!quiet && testcalls > simulatedopcount)
1039 prt("truncating to largest ever: 0x%x\n", size);
1042 log4(OP_TRUNCATE, 0, size, FL_NONE);
1044 if (size > file_size)
1045 memset(good_buf + file_size, '\0', size - file_size);
1048 if (testcalls <= simulatedopcount)
1051 if ((progressinterval && testcalls % progressinterval == 0) ||
1052 (debug && (monitorstart == -1 || monitorend == -1 ||
1053 size <= monitorend)))
1054 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
1055 if (ftruncate(fd, (off_t)size) == -1) {
1056 prt("ftruncate1: %x\n", size);
1057 prterr("dotruncate: ftruncate");
1058 report_failure(160);
1062 #ifdef FALLOC_FL_PUNCH_HOLE
1064 do_punch_hole(unsigned offset, unsigned length)
1066 unsigned end_offset;
1069 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
1072 if (!quiet && testcalls > simulatedopcount)
1073 prt("skipping zero length punch hole\n");
1074 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
1078 if (file_size <= (loff_t)offset) {
1079 if (!quiet && testcalls > simulatedopcount)
1080 prt("skipping hole punch off the end of the file\n");
1081 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
1085 end_offset = offset + length;
1087 log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
1089 if (testcalls <= simulatedopcount)
1092 if ((progressinterval && testcalls % progressinterval == 0) ||
1093 (debug && (monitorstart == -1 || monitorend == -1 ||
1094 end_offset <= monitorend))) {
1095 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1096 offset, offset+length, length);
1098 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1099 prt("punch hole: 0x%x to 0x%x\n", offset, offset + length);
1100 prterr("do_punch_hole: fallocate");
1101 report_failure(161);
1105 max_offset = offset < file_size ? offset : file_size;
1106 max_len = max_offset + length <= file_size ? length :
1107 file_size - max_offset;
1108 memset(good_buf + max_offset, '\0', max_len);
1113 do_punch_hole(unsigned offset, unsigned length)
1119 #ifdef FALLOC_FL_ZERO_RANGE
1121 do_zero_range(unsigned offset, unsigned length, int keep_size)
1123 unsigned end_offset;
1124 int mode = FALLOC_FL_ZERO_RANGE;
1127 if (!quiet && testcalls > simulatedopcount)
1128 prt("skipping zero length zero range\n");
1129 log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
1130 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1134 end_offset = keep_size ? 0 : offset + length;
1136 if (end_offset > biggest) {
1137 biggest = end_offset;
1138 if (!quiet && testcalls > simulatedopcount)
1139 prt("zero_range to largest ever: 0x%x\n", end_offset);
1143 * last arg matches fallocate string array index in logdump:
1144 * 0: allocate past EOF
1145 * 1: extending prealloc
1146 * 2: interior prealloc
1148 log4(OP_ZERO_RANGE, offset, length,
1149 keep_size ? FL_KEEP_SIZE : FL_NONE);
1151 if (testcalls <= simulatedopcount)
1154 if ((progressinterval && testcalls % progressinterval == 0) ||
1155 (debug && (monitorstart == -1 || monitorend == -1 ||
1156 end_offset <= monitorend))) {
1157 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1158 offset, offset+length, length);
1160 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1161 prt("zero range: 0x%x to 0x%x\n", offset, offset + length);
1162 prterr("do_zero_range: fallocate");
1163 report_failure(161);
1166 memset(good_buf + offset, '\0', length);
1171 do_zero_range(unsigned offset, unsigned length, int keep_size)
1177 #ifdef FALLOC_FL_COLLAPSE_RANGE
1179 do_collapse_range(unsigned offset, unsigned length)
1181 unsigned end_offset;
1182 int mode = FALLOC_FL_COLLAPSE_RANGE;
1185 if (!quiet && testcalls > simulatedopcount)
1186 prt("skipping zero length collapse range\n");
1187 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1191 end_offset = offset + length;
1192 if ((loff_t)end_offset >= file_size) {
1193 if (!quiet && testcalls > simulatedopcount)
1194 prt("skipping collapse range behind EOF\n");
1195 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1199 log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1201 if (testcalls <= simulatedopcount)
1204 if ((progressinterval && testcalls % progressinterval == 0) ||
1205 (debug && (monitorstart == -1 || monitorend == -1 ||
1206 end_offset <= monitorend))) {
1207 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1208 offset, offset+length, length);
1210 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1211 prt("collapse range: 0x%x to 0x%x\n", offset, offset + length);
1212 prterr("do_collapse_range: fallocate");
1213 report_failure(161);
1216 memmove(good_buf + offset, good_buf + end_offset,
1217 file_size - end_offset);
1218 file_size -= length;
1223 do_collapse_range(unsigned offset, unsigned length)
1229 #ifdef FALLOC_FL_INSERT_RANGE
1231 do_insert_range(unsigned offset, unsigned length)
1233 unsigned end_offset;
1234 int mode = FALLOC_FL_INSERT_RANGE;
1237 if (!quiet && testcalls > simulatedopcount)
1238 prt("skipping zero length insert range\n");
1239 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1243 if ((loff_t)offset >= file_size) {
1244 if (!quiet && testcalls > simulatedopcount)
1245 prt("skipping insert range behind EOF\n");
1246 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1250 log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1252 if (testcalls <= simulatedopcount)
1255 end_offset = offset + length;
1256 if ((progressinterval && testcalls % progressinterval == 0) ||
1257 (debug && (monitorstart == -1 || monitorend == -1 ||
1258 end_offset <= monitorend))) {
1259 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1260 offset, offset+length, length);
1262 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1263 prt("insert range: 0x%x to 0x%x\n", offset, offset + length);
1264 prterr("do_insert_range: fallocate");
1265 report_failure(161);
1268 memmove(good_buf + end_offset, good_buf + offset,
1269 file_size - offset);
1270 memset(good_buf + offset, '\0', length);
1271 file_size += length;
1276 do_insert_range(unsigned offset, unsigned length)
1282 #ifdef HAVE_LINUX_FALLOC_H
1283 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1285 do_preallocate(unsigned offset, unsigned length, int keep_size)
1287 unsigned end_offset;
1290 if (!quiet && testcalls > simulatedopcount)
1291 prt("skipping zero length fallocate\n");
1292 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1293 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1297 end_offset = keep_size ? 0 : offset + length;
1299 if (end_offset > biggest) {
1300 biggest = end_offset;
1301 if (!quiet && testcalls > simulatedopcount)
1302 prt("fallocating to largest ever: 0x%x\n", end_offset);
1306 * last arg matches fallocate string array index in logdump:
1307 * 0: allocate past EOF
1308 * 1: extending prealloc
1309 * 2: interior prealloc
1311 log4(OP_FALLOCATE, offset, length,
1312 keep_size ? FL_KEEP_SIZE : FL_NONE);
1314 if (end_offset > file_size) {
1315 memset(good_buf + file_size, '\0', end_offset - file_size);
1316 file_size = end_offset;
1319 if (testcalls <= simulatedopcount)
1322 if ((progressinterval && testcalls % progressinterval == 0) ||
1323 (debug && (monitorstart == -1 || monitorend == -1 ||
1324 end_offset <= monitorend)))
1325 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1326 offset, offset + length, length);
1327 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1328 prt("fallocate: 0x%x to 0x%x\n", offset, offset + length);
1329 prterr("do_preallocate: fallocate");
1330 report_failure(161);
1335 do_preallocate(unsigned offset, unsigned length, int keep_size)
1346 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1347 prterr("writefileimage: lseek");
1348 report_failure(171);
1350 iret = write(fd, good_buf, file_size);
1351 if ((off_t)iret != file_size) {
1353 prterr("writefileimage: write");
1355 prt("short write: 0x%x bytes instead of 0x%llx\n",
1356 iret, (unsigned long long)file_size);
1357 report_failure(172);
1359 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1360 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1361 prterr("writefileimage: ftruncate");
1362 report_failure(173);
1370 if (testcalls <= simulatedopcount)
1374 prt("%lu close/open\n", testcalls);
1376 prterr("docloseopen: close");
1377 report_failure(180);
1379 fd = open(fname, O_RDWR|o_direct, 0);
1381 prterr("docloseopen: open");
1382 report_failure(181);
1391 if (testcalls <= simulatedopcount)
1394 prt("%lu fsync\n", testcalls);
1395 log4(OP_FSYNC, 0, 0, 0);
1399 report_failure(210);
1402 dump_fsync_buffer();
1406 #define TRIM_OFF(off, size) \
1414 #define TRIM_LEN(off, len, size) \
1416 if ((off) + (len) > (size)) \
1417 (len) = (size) - (off); \
1420 #define TRIM_OFF_LEN(off, len, size) \
1422 TRIM_OFF(off, size); \
1423 TRIM_LEN(off, len, size); \
1430 prt("signal %d\n", sig);
1431 prt("testcalls = %lu\n", testcalls);
1436 read_op(struct log_entry *log_entry)
1440 memset(log_entry, 0, sizeof(*log_entry));
1441 log_entry->operation = -1;
1443 while (log_entry->operation == -1) {
1448 if (!fgets(line, sizeof(line), replayopsf)) {
1449 if (feof(replayopsf)) {
1455 str = strtok(line, " \t\n");
1456 } while (!str || str[0] == '#');
1458 if (strcmp(str, "skip") == 0) {
1459 log_entry->flags |= FL_SKIPPED;
1460 str = strtok(NULL, " \t\n");
1464 log_entry->operation = op_code(str);
1465 if (log_entry->operation == -1)
1467 for (i = 0; i < 3; i++) {
1470 str = strtok(NULL, " \t\n");
1473 log_entry->args[i] = strtoul(str, &end, 0);
1477 while ((str = strtok(NULL, " \t\n"))) {
1478 if (strcmp(str, "keep_size") == 0)
1479 log_entry->flags |= FL_KEEP_SIZE;
1480 else if (strcmp(str, "close_open") == 0)
1481 log_entry->flags |= FL_CLOSE_OPEN;
1482 else if (strcmp(str, "*") == 0)
1483 ; /* overlap marker; ignore */
1491 fprintf(stderr, "%s: parse error\n", replayops);
1494 cleanup(100); /* doesn't return */
1502 unsigned long offset;
1508 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1513 if (debugstart > 0 && testcalls >= debugstart)
1516 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1517 prt("%lu...\n", testcalls);
1520 struct log_entry log_entry;
1522 while (read_op(&log_entry)) {
1523 if (log_entry.flags & FL_SKIPPED) {
1524 log4(log_entry.operation,
1525 log_entry.args[0], log_entry.args[1],
1530 op = log_entry.operation;
1531 offset = log_entry.args[0];
1532 size = log_entry.args[1];
1533 closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
1534 keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
1542 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1547 size = random() % (maxoplen + 1);
1549 /* calculate appropriate op to run */
1551 op = rv % OP_MAX_LITE;
1552 else if (!integrity)
1553 op = rv % OP_MAX_FULL;
1555 op = rv % OP_MAX_INTEGRITY;
1560 size = random() % maxfilelen;
1563 if (fallocate_calls && size && keep_size_calls)
1564 keep_size = random() % 2;
1567 if (zero_range_calls && size && keep_size_calls)
1568 keep_size = random() % 2;
1584 if (!fallocate_calls) {
1585 log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
1590 if (!punch_hole_calls) {
1591 log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
1596 if (!zero_range_calls) {
1597 log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
1601 case OP_COLLAPSE_RANGE:
1602 if (!collapse_range_calls) {
1603 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1607 case OP_INSERT_RANGE:
1608 if (!insert_range_calls) {
1609 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1617 TRIM_OFF_LEN(offset, size, file_size);
1618 doread(offset, size);
1622 TRIM_OFF_LEN(offset, size, maxfilelen);
1623 dowrite(offset, size);
1627 TRIM_OFF_LEN(offset, size, file_size);
1628 domapread(offset, size);
1632 TRIM_OFF_LEN(offset, size, maxfilelen);
1633 domapwrite(offset, size);
1641 TRIM_OFF_LEN(offset, size, maxfilelen);
1642 do_preallocate(offset, size, keep_size);
1646 TRIM_OFF_LEN(offset, size, file_size);
1647 do_punch_hole(offset, size);
1650 TRIM_OFF_LEN(offset, size, file_size);
1651 do_zero_range(offset, size, keep_size);
1653 case OP_COLLAPSE_RANGE:
1654 TRIM_OFF_LEN(offset, size, file_size - 1);
1655 offset = offset & ~(block_size - 1);
1656 size = size & ~(block_size - 1);
1658 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1661 do_collapse_range(offset, size);
1663 case OP_INSERT_RANGE:
1664 TRIM_OFF(offset, file_size);
1665 TRIM_LEN(file_size, size, maxfilelen);
1666 offset = offset & ~(block_size - 1);
1667 size = size & ~(block_size - 1);
1669 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1672 if (file_size + size > maxfilelen) {
1673 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1677 do_insert_range(offset, size);
1683 prterr("test: unknown operation");
1688 if (check_file && testcalls > simulatedopcount)
1692 if (sizechecks && testcalls > simulatedopcount)
1703 fprintf(stdout, "usage: %s",
1704 "fsx [-dknqxAFLOWZ] [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
1705 -b opnum: beginning operation number (default 1)\n\
1706 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1707 -d: debug output for all operations\n\
1708 -f flush and invalidate cache after I/O\n\
1709 -g X: write character X instead of random generated data\n\
1710 -i logdev: do integrity testing, logdev is the dm log writes device\n\
1711 -j logid: prefix debug log messsages with this id\n\
1712 -k: do not truncate existing file and use its size as upper bound on file size\n\
1713 -l flen: the upper bound on file size (default 262144)\n\
1714 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1715 -n: no verifications of file size\n\
1716 -o oplen: the upper bound on operation size (default 65536)\n\
1717 -p progressinterval: debug output at specified operation interval\n\
1718 -q: quieter operation\n\
1719 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1720 -s style: 1 gives smaller truncates (default 0)\n\
1721 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1722 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1723 -x: preallocate file space before starting, XFS only (default 0)\n\
1724 -y synchronize changes to a file\n"
1727 " -A: Use the AIO system calls\n"
1729 " -D startingop: debug output starting at specified operation\n"
1730 #ifdef HAVE_LINUX_FALLOC_H
1731 " -F: Do not use fallocate (preallocation) calls\n"
1733 #ifdef FALLOC_FL_PUNCH_HOLE
1734 " -H: Do not use punch hole calls\n"
1736 #ifdef FALLOC_FL_ZERO_RANGE
1737 " -z: Do not use zero range calls\n"
1739 #ifdef FALLOC_FL_COLLAPSE_RANGE
1740 " -C: Do not use collapse range calls\n"
1742 #ifdef FALLOC_FL_INSERT_RANGE
1743 " -I: Do not use insert range calls\n"
1745 " -L: fsxLite - no file creations & no file size changes\n\
1746 -N numops: total # operations to do (default infinity)\n\
1747 -O: use oplen (see -o flag) for every op (default random)\n\
1748 -P: save .fsxlog .fsxops and .fsxgood files in dirpath (default ./)\n\
1749 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1750 -W: mapped write operations DISabled\n\
1751 -X: Read file and compare to good buffer after every operation.\n\
1752 -R: read() system calls only (mapped reads disabled)\n\
1753 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1754 --replay-ops opsfile: replay ops from recorded .fsxops file\n\
1755 --record-ops[=opsfile]: dump ops file also on success. optionally specify ops file name\n\
1756 fname: this filename is REQUIRED (no default)\n");
1762 getnum(char *s, char **e)
1767 ret = strtol(s, e, 0);
1797 io_context_t io_ctx;
1803 ret = io_queue_init(QSZ, &io_ctx);
1805 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1813 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1815 struct io_event event;
1816 static struct timespec ts;
1817 struct iocb *iocbs[] = { &iocb };
1822 io_prep_pread(&iocb, fd, buf, len, offset);
1824 io_prep_pwrite(&iocb, fd, buf, len, offset);
1829 ret = io_submit(io_ctx, 1, iocbs);
1831 fprintf(stderr, "errcode=%d\n", ret);
1832 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1837 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1840 fprintf(stderr, "aio_rw: no events available\n");
1842 fprintf(stderr, "errcode=%d\n", -ret);
1843 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1848 if (len != event.res) {
1850 * The b0rked libaio defines event.res as unsigned.
1851 * However the kernel strucuture has it signed,
1852 * and it's used to pass negated error value.
1853 * Till the library is fixed use the temp var.
1855 res = (long)event.res;
1857 fprintf(stderr, "bad io length: %lu instead of %u\n",
1860 fprintf(stderr, "errcode=%ld\n", -res);
1861 fprintf(stderr, "aio_rw: async io failed: %s\n",
1872 * The caller expects error return in traditional libc
1873 * convention, i.e. -1 and the errno set to error.
1879 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1884 ret = __aio_rw(rw, fd, buf, len, offset);
1887 ret = read(fd, buf, len);
1889 ret = write(fd, buf, len);
1896 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1899 __test_fallocate(int mode, const char *mode_str)
1901 #ifdef HAVE_LINUX_FALLOC_H
1904 if (fallocate(fd, mode, file_size, 1) && errno == EOPNOTSUPP) {
1907 "main: filesystem does not support "
1908 "fallocate mode %s, disabling!\n",
1912 if (ftruncate(fd, file_size)) {
1913 warn("main: ftruncate");
1922 static struct option longopts[] = {
1923 {"replay-ops", required_argument, 0, 256},
1924 {"record-ops", optional_argument, 0, 255},
1929 main(int argc, char **argv)
1933 char logfile[PATH_MAX];
1934 struct stat statbuf;
1935 int o_flags = O_RDWR|O_CREAT|O_TRUNC;
1940 page_size = getpagesize();
1941 page_mask = page_size - 1;
1942 mmap_mask = page_mask;
1945 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1947 while ((ch = getopt_long(argc, argv,
1948 "b:c:dfg:i:j:kl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WXZ",
1949 longopts, NULL)) != EOF)
1952 simulatedopcount = getnum(optarg, &endp);
1954 prt("Will begin at operation %ld\n", simulatedopcount);
1955 if (simulatedopcount == 0)
1957 simulatedopcount -= 1;
1960 closeprob = getnum(optarg, &endp);
1962 prt("Chance of close/open is 1 in %d\n", closeprob);
1977 logdev = strdup(optarg);
1984 logid = strdup(optarg);
1991 o_flags &= ~O_TRUNC;
1994 maxfilelen = getnum(optarg, &endp);
1995 if (maxfilelen <= 0)
1999 monitorstart = getnum(optarg, &endp);
2000 if (monitorstart < 0)
2002 if (!endp || *endp++ != ':')
2004 monitorend = getnum(endp, &endp);
2007 if (monitorend == 0)
2008 monitorend = -1; /* aka infinity */
2014 maxoplen = getnum(optarg, &endp);
2019 progressinterval = getnum(optarg, &endp);
2020 if (progressinterval == 0)
2027 readbdy = getnum(optarg, &endp);
2032 style = getnum(optarg, &endp);
2033 if (style < 0 || style > 1)
2037 truncbdy = getnum(optarg, &endp);
2042 writebdy = getnum(optarg, &endp);
2056 debugstart = getnum(optarg, &endp);
2061 fallocate_calls = 0;
2064 keep_size_calls = 0;
2067 punch_hole_calls = 0;
2070 zero_range_calls = 0;
2073 collapse_range_calls = 0;
2076 insert_range_calls = 0;
2080 o_flags &= ~(O_CREAT|O_TRUNC);
2083 numops = getnum(optarg, &endp);
2091 strncpy(dname, optarg, sizeof(dname));
2093 dirpath = strlen(dname);
2099 seed = getnum(optarg, &endp);
2101 seed = time(0) % 10000;
2102 seed += (int)getpid();
2110 prt("mapped writes DISABLED\n");
2116 o_direct = O_DIRECT;
2117 o_flags |= O_DIRECT;
2119 case 255: /* --record-ops */
2121 strncpy(opsfile, optarg, sizeof(opsfile));
2122 recordops = opsfile;
2124 case 256: /* --replay-ops */
2136 if (integrity && !dirpath) {
2137 fprintf(stderr, "option -i <logdev> requires -P <dirpath>\n");
2142 tmp = strdup(fname);
2147 bname = basename(tmp);
2149 signal(SIGHUP, cleanup);
2150 signal(SIGINT, cleanup);
2151 signal(SIGPIPE, cleanup);
2152 signal(SIGALRM, cleanup);
2153 signal(SIGTERM, cleanup);
2154 signal(SIGXCPU, cleanup);
2155 signal(SIGXFSZ, cleanup);
2156 signal(SIGVTALRM, cleanup);
2157 signal(SIGUSR1, cleanup);
2158 signal(SIGUSR2, cleanup);
2161 prt("Seed set to %d\n", seed);
2163 fd = open(fname, o_flags, 0666);
2168 if (fstat(fd, &statbuf)) {
2169 prterr("check_size: fstat");
2172 block_size = statbuf.st_blksize;
2175 xfs_flock64_t resv = { 0 };
2176 #ifdef HAVE_XFS_PLATFORM_DEFS_H
2177 if (!platform_test_xfs_fd(fd)) {
2179 fprintf(stderr, "main: cannot prealloc, non XFS\n");
2183 resv.l_len = maxfilelen;
2184 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
2192 snprintf(goodfile, sizeof(goodfile), "%s%s.fsxgood", dname, bname);
2193 snprintf(logfile, sizeof(logfile), "%s%s.fsxlog", dname, bname);
2195 snprintf(opsfile, sizeof(opsfile), "%s%s.fsxops", dname, bname);
2197 snprintf(goodfile, sizeof(goodfile), "%s.fsxgood", fname);
2198 snprintf(logfile, sizeof(logfile), "%s.fsxlog", fname);
2200 snprintf(opsfile, sizeof(opsfile), "%s.fsxops", fname);
2202 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
2203 if (fsxgoodfd < 0) {
2207 fsxlogf = fopen(logfile, "w");
2208 if (fsxlogf == NULL) {
2215 replayopsf = fopen(replayops, "r");
2227 if (!(o_flags & O_TRUNC)) {
2229 file_size = maxfilelen = biggest = lseek(fd, (off_t)0, SEEK_END);
2230 if (file_size == (off_t)-1) {
2232 warn("main: lseek eof");
2235 ret = lseek(fd, (off_t)0, SEEK_SET);
2236 if (ret == (off_t)-1) {
2238 warn("main: lseek 0");
2242 original_buf = (char *) malloc(maxfilelen);
2243 for (i = 0; i < maxfilelen; i++)
2244 original_buf[i] = random() % 256;
2245 good_buf = (char *) malloc(maxfilelen + writebdy);
2246 good_buf = round_ptr_up(good_buf, writebdy, 0);
2247 memset(good_buf, '\0', maxfilelen);
2248 temp_buf = (char *) malloc(maxoplen + readbdy);
2249 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
2250 memset(temp_buf, '\0', maxoplen);
2251 if (lite) { /* zero entire existing file */
2254 written = write(fd, good_buf, (size_t)maxfilelen);
2255 if (written != maxfilelen) {
2256 if (written == -1) {
2258 warn("main: error on write");
2260 warn("main: short write, 0x%x bytes instead "
2267 ssize_t ret, len = file_size;
2271 ret = read(fd, good_buf + off, len);
2274 warn("main: error on read");
2284 if (fallocate_calls)
2285 fallocate_calls = test_fallocate(0);
2286 if (keep_size_calls)
2287 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
2288 if (punch_hole_calls)
2289 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2290 if (zero_range_calls)
2291 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
2292 if (collapse_range_calls)
2293 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
2294 if (insert_range_calls)
2295 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
2297 while (numops == -1 || numops--)
2306 prt("All %lu operations completed A-OK!\n", testcalls);