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.
41 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
45 enum opflags { FL_NONE = 0, FL_SKIPPED = 1, FL_CLOSE_OPEN = 2, FL_KEEP_SIZE = 4 };
48 * A log entry is an operation and a bunch of arguments.
59 struct log_entry oplog[LOGSIZE]; /* the log */
60 int logptr = 0; /* current position in log */
61 int logcount = 0; /* total ops */
64 * The operation matrix is complex due to conditional execution of different
65 * features. Hence when we come to deciding what operation to run, we need to
66 * be careful in how we select the different operations. The active operations
67 * are mapped to numbers as follows:
79 * When mapped read/writes are disabled, they are simply converted to normal
80 * reads and writes. When fallocate/fpunch calls are disabled, they are
83 * Because of the "lite" version, we also need to have different "maximum
84 * operation" defines to allow the ops to be selected correctly based on the
88 /* common operations */
95 /* !lite operations */
97 #define OP_FALLOCATE 5
98 #define OP_PUNCH_HOLE 6
99 #define OP_ZERO_RANGE 7
100 #define OP_COLLAPSE_RANGE 8
101 #define OP_INSERT_RANGE 9
102 #define OP_MAX_FULL 10
105 #define PAGE_SIZE getpagesize()
107 #define PAGE_MASK (PAGE_SIZE - 1)
109 char *original_buf; /* a pointer to the original data */
110 char *good_buf; /* a pointer to the correct data */
111 char *temp_buf; /* a pointer to the current data */
112 char *fname; /* name of our test file */
113 int fd; /* fd for our test file */
115 blksize_t block_size = 0;
119 unsigned long testcalls = 0; /* calls to function "test" */
121 unsigned long simulatedopcount = 0; /* -b flag */
122 int closeprob = 0; /* -c flag */
123 int debug = 0; /* -d flag */
124 unsigned long debugstart = 0; /* -D flag */
125 int flush = 0; /* -f flag */
126 int do_fsync = 0; /* -y flag */
127 unsigned long maxfilelen = 256 * 1024; /* -l flag */
128 int sizechecks = 1; /* -n flag disables them */
129 int maxoplen = 64 * 1024; /* -o flag */
130 int quiet = 0; /* -q flag */
131 unsigned long progressinterval = 0; /* -p flag */
132 int readbdy = 1; /* -r flag */
133 int style = 0; /* -s flag */
134 int prealloc = 0; /* -x flag */
135 int truncbdy = 1; /* -t flag */
136 int writebdy = 1; /* -w flag */
137 long monitorstart = -1; /* -m flag */
138 long monitorend = -1; /* -m flag */
139 int lite = 0; /* -L flag */
140 long numops = -1; /* -N flag */
141 int randomoplen = 1; /* -O flag disables it */
142 int seed = 1; /* -S flag */
143 int mapped_writes = 1; /* -W flag disables */
144 int fallocate_calls = 1; /* -F flag disables */
145 int keep_size_calls = 1; /* -K flag disables */
146 int punch_hole_calls = 1; /* -H flag disables */
147 int zero_range_calls = 1; /* -z flag disables */
148 int collapse_range_calls = 1; /* -C flag disables */
149 int insert_range_calls = 1; /* -I flag disables */
150 int mapped_reads = 1; /* -R flag disables it */
152 int o_direct; /* -Z */
159 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
162 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
163 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
165 #define fsxread(a,b,c,d) read(a,b,c)
166 #define fsxwrite(a,b,c,d) write(a,b,c)
169 const char *replayops = NULL;
170 FILE * fsxlogf = NULL;
171 FILE * replayopsf = NULL;
176 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
178 unsigned long ret = (unsigned long)ptr;
180 ret = ((ret + align - 1) & ~(align - 1));
186 vwarnc(int code, const char *fmt, va_list ap) {
187 fprintf(stderr, "fsx: ");
189 vfprintf(stderr, fmt, ap);
190 fprintf(stderr, ": ");
192 fprintf(stderr, "%s\n", strerror(code));
196 warn(const char * fmt, ...) {
199 vwarnc(errno, fmt, ap);
203 #define BUF_SIZE 1024
206 prt(const char *fmt, ...)
209 char buffer[BUF_SIZE];
212 vsnprintf(buffer, BUF_SIZE, fmt, args);
214 fprintf(stdout, buffer);
216 fprintf(fsxlogf, buffer);
220 prterr(const char *prefix)
222 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
226 static const char *op_names[] = {
228 [OP_WRITE] = "write",
229 [OP_MAPREAD] = "mapread",
230 [OP_MAPWRITE] = "mapwrite",
231 [OP_TRUNCATE] = "truncate",
232 [OP_FALLOCATE] = "fallocate",
233 [OP_PUNCH_HOLE] = "punch_hole",
234 [OP_ZERO_RANGE] = "zero_range",
235 [OP_COLLAPSE_RANGE] = "collapse_range",
236 [OP_INSERT_RANGE] = "insert_range",
239 static const char *op_name(int operation)
241 if (operation >= 0 &&
242 operation < sizeof(op_names) / sizeof(op_names[0]))
243 return op_names[operation];
247 static int op_code(const char *name)
251 for (i = 0; i < sizeof(op_names) / sizeof(op_names[0]); i++)
252 if (op_names[i] && strcmp(name, op_names[i]) == 0)
258 log4(int operation, int arg0, int arg1, enum opflags flags)
260 struct log_entry *le;
263 le->operation = operation;
265 flags |= FL_CLOSE_OPEN;
268 le->args[2] = file_size;
272 if (logptr >= LOGSIZE)
282 struct log_entry *lp;
284 prt("LOG DUMP (%d total operations):\n", logcount);
286 logopsf = fopen(opsfile, "w");
290 if (logcount < LOGSIZE) {
297 for ( ; count > 0; count--) {
301 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
302 prt("%d(%3d mod 256): ", opnum, opnum%256);
305 overlap = badoff >= lp->args[0] &&
306 badoff < lp->args[0] + lp->args[1];
308 if (lp->flags & FL_SKIPPED) {
309 prt("SKIPPED (no operation)");
313 switch (lp->operation) {
315 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
316 lp->args[0], lp->args[0] + lp->args[1] - 1,
322 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
323 lp->args[0], lp->args[0] + lp->args[1] - 1,
329 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
330 lp->args[0], lp->args[0] + lp->args[1] - 1,
336 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
337 lp->args[0], lp->args[0] + lp->args[1] - 1,
339 if (lp->args[0] > lp->args[2])
341 else if (lp->args[0] + lp->args[1] > lp->args[2])
343 overlap = (badoff >= lp->args[0] ||
344 badoff >=lp->args[2]) &&
345 badoff < lp->args[0] + lp->args[1];
350 down = lp->args[1] < lp->args[2];
351 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
352 down ? "DOWN" : "UP", lp->args[2], lp->args[1]);
353 overlap = badoff >= lp->args[1 + !down] &&
354 badoff < lp->args[1 + !!down];
359 /* 0: offset 1: length 2: where alloced */
360 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) ",
361 lp->args[0], lp->args[0] + lp->args[1],
363 if (lp->args[0] + lp->args[1] <= lp->args[2])
365 else if (lp->flags & FL_KEEP_SIZE)
373 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
374 lp->args[0], lp->args[0] + lp->args[1] - 1,
380 prt("ZERO 0x%x thru 0x%x\t(0x%x bytes)",
381 lp->args[0], lp->args[0] + lp->args[1] - 1,
386 case OP_COLLAPSE_RANGE:
387 prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
388 lp->args[0], lp->args[0] + lp->args[1] - 1,
393 case OP_INSERT_RANGE:
394 prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
395 lp->args[0], lp->args[0] + lp->args[1] - 1,
401 prt("BOGUS LOG ENTRY (operation code = %d)!",
407 if (lp->flags & FL_CLOSE_OPEN)
408 prt("\n\t\tCLOSE/OPEN");
415 if (lp->flags & FL_SKIPPED)
416 fprintf(logopsf, "skip ");
417 fprintf(logopsf, "%s 0x%x 0x%x 0x%x",
418 op_name(lp->operation),
419 lp->args[0], lp->args[1], lp->args[2]);
420 if (lp->flags & FL_KEEP_SIZE)
421 fprintf(logopsf, " keep_size");
422 if (lp->flags & FL_CLOSE_OPEN)
423 fprintf(logopsf, " close_open");
425 fprintf(logopsf, " *");
426 fprintf(logopsf, "\n");
431 if (fclose(logopsf) != 0)
434 prt("Log of operations saved to \"%s\"; "
435 "replay with --replay-ops\n",
442 save_buffer(char *buffer, off_t bufferlength, int fd)
445 ssize_t byteswritten;
447 if (fd <= 0 || bufferlength == 0)
450 if (bufferlength > SSIZE_MAX) {
451 prt("fsx flaw: overflow in save_buffer\n");
455 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
456 if (size_by_seek == (off_t)-1)
457 prterr("save_buffer: lseek eof");
458 else if (bufferlength > size_by_seek) {
459 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
460 (unsigned long long)bufferlength);
461 bufferlength = size_by_seek;
465 ret = lseek(fd, (off_t)0, SEEK_SET);
466 if (ret == (off_t)-1)
467 prterr("save_buffer: lseek 0");
469 byteswritten = write(fd, buffer, (size_t)bufferlength);
470 if (byteswritten != bufferlength) {
471 if (byteswritten == -1)
472 prterr("save_buffer write");
474 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
475 (unsigned)byteswritten,
476 (unsigned long long)bufferlength);
482 report_failure(int status)
488 save_buffer(good_buf, file_size, fsxgoodfd);
489 prt("Correct content saved for comparison\n");
490 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
499 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
500 *(((unsigned char *)(cp)) + 1)))
503 check_buffers(unsigned offset, unsigned size)
511 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
512 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
513 offset, size, fname);
514 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
516 c = good_buf[offset];
520 bad = short_at(&temp_buf[i]);
521 prt("0x%05x\t0x%04x\t0x%04x", offset,
522 short_at(&good_buf[offset]), bad);
523 op = temp_buf[offset & 1 ? i+1 : i];
524 prt("\t0x%05x\n", n);
526 prt("operation# (mod 256) for "
527 "the bad data may be %u\n",
528 ((unsigned)op & 0xff));
530 prt("operation# (mod 256) for "
531 "the bad data unknown, check"
532 " HOLE and EXTEND ops\n");
552 if (fstat(fd, &statbuf)) {
553 prterr("check_size: fstat");
554 statbuf.st_size = -1;
556 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
557 if (file_size != statbuf.st_size || file_size != size_by_seek) {
558 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
559 (unsigned long long)file_size,
560 (unsigned long long)statbuf.st_size,
561 (unsigned long long)size_by_seek);
568 check_trunc_hack(void)
572 ftruncate(fd, (off_t)0);
573 ftruncate(fd, (off_t)100000);
575 if (statbuf.st_size != (off_t)100000) {
576 prt("no extend on truncate! not posix!\n");
583 doflush(unsigned offset, unsigned size)
589 if (o_direct == O_DIRECT)
592 pg_offset = offset & mmap_mask;
593 map_size = pg_offset + size;
595 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
596 MAP_FILE | MAP_SHARED, fd,
597 (off_t)(offset - pg_offset))) == (char *)-1) {
598 prterr("doflush: mmap");
601 if (msync(p, map_size, MS_INVALIDATE) != 0) {
602 prterr("doflush: msync");
605 if (munmap(p, map_size) != 0) {
606 prterr("doflush: munmap");
612 doread(unsigned offset, unsigned size)
617 offset -= offset % readbdy;
619 size -= size % readbdy;
621 if (!quiet && testcalls > simulatedopcount && !o_direct)
622 prt("skipping zero size read\n");
623 log4(OP_READ, offset, size, FL_SKIPPED);
626 if (size + offset > file_size) {
627 if (!quiet && testcalls > simulatedopcount)
628 prt("skipping seek/read past end of file\n");
629 log4(OP_READ, offset, size, FL_SKIPPED);
633 log4(OP_READ, offset, size, FL_NONE);
635 if (testcalls <= simulatedopcount)
639 ((progressinterval && testcalls % progressinterval == 0) ||
641 (monitorstart == -1 ||
642 (offset + size > monitorstart &&
643 (monitorend == -1 || offset <= monitorend))))))
644 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
645 offset, offset + size - 1, size);
646 ret = lseek(fd, (off_t)offset, SEEK_SET);
647 if (ret == (off_t)-1) {
648 prterr("doread: lseek");
651 iret = fsxread(fd, temp_buf, size, offset);
654 prterr("doread: read");
656 prt("short read: 0x%x bytes instead of 0x%x\n",
660 check_buffers(offset, size);
665 check_eofpage(char *s, unsigned offset, char *p, int size)
667 unsigned long last_page, should_be_zero;
669 if (offset + size <= (file_size & ~page_mask))
672 * we landed in the last page of the file
673 * test to make sure the VM system provided 0's
674 * beyond the true end of the file mapping
675 * (as required by mmap def in 1996 posix 1003.1)
677 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
679 for (should_be_zero = last_page + (file_size & page_mask);
680 should_be_zero < last_page + page_size;
682 if (*(char *)should_be_zero) {
683 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
684 s, file_size - 1, should_be_zero & page_mask,
685 short_at(should_be_zero));
692 domapread(unsigned offset, unsigned size)
698 offset -= offset % readbdy;
700 if (!quiet && testcalls > simulatedopcount)
701 prt("skipping zero size read\n");
702 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
705 if (size + offset > file_size) {
706 if (!quiet && testcalls > simulatedopcount)
707 prt("skipping seek/read past end of file\n");
708 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
712 log4(OP_MAPREAD, offset, size, FL_NONE);
714 if (testcalls <= simulatedopcount)
718 ((progressinterval && testcalls % progressinterval == 0) ||
720 (monitorstart == -1 ||
721 (offset + size > monitorstart &&
722 (monitorend == -1 || offset <= monitorend))))))
723 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
724 offset, offset + size - 1, size);
726 pg_offset = offset & PAGE_MASK;
727 map_size = pg_offset + size;
729 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
730 (off_t)(offset - pg_offset))) == (char *)-1) {
731 prterr("domapread: mmap");
734 memcpy(temp_buf, p + pg_offset, size);
736 check_eofpage("Read", offset, p, size);
738 if (munmap(p, map_size) != 0) {
739 prterr("domapread: munmap");
743 check_buffers(offset, size);
748 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
751 good_buf[offset] = testcalls % 256;
753 good_buf[offset] += original_buf[offset];
760 dowrite(unsigned offset, unsigned size)
765 offset -= offset % writebdy;
767 size -= size % writebdy;
769 if (!quiet && testcalls > simulatedopcount && !o_direct)
770 prt("skipping zero size write\n");
771 log4(OP_WRITE, offset, size, FL_SKIPPED);
775 log4(OP_WRITE, offset, size, FL_NONE);
777 gendata(original_buf, good_buf, offset, size);
778 if (file_size < offset + size) {
779 if (file_size < offset)
780 memset(good_buf + file_size, '\0', offset - file_size);
781 file_size = offset + size;
783 warn("Lite file size bug in fsx!");
788 if (testcalls <= simulatedopcount)
792 ((progressinterval && testcalls % progressinterval == 0) ||
794 (monitorstart == -1 ||
795 (offset + size > monitorstart &&
796 (monitorend == -1 || offset <= monitorend))))))
797 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
798 offset, offset + size - 1, size);
799 ret = lseek(fd, (off_t)offset, SEEK_SET);
800 if (ret == (off_t)-1) {
801 prterr("dowrite: lseek");
804 iret = fsxwrite(fd, good_buf + offset, size, offset);
807 prterr("dowrite: write");
809 prt("short write: 0x%x bytes instead of 0x%x\n",
815 prt("fsync() failed: %s\n", strerror(errno));
820 doflush(offset, size);
826 domapwrite(unsigned offset, unsigned size)
833 offset -= offset % writebdy;
835 if (!quiet && testcalls > simulatedopcount)
836 prt("skipping zero size write\n");
837 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
840 cur_filesize = file_size;
842 log4(OP_MAPWRITE, offset, size, FL_NONE);
844 gendata(original_buf, good_buf, offset, size);
845 if (file_size < offset + size) {
846 if (file_size < offset)
847 memset(good_buf + file_size, '\0', offset - file_size);
848 file_size = offset + size;
850 warn("Lite file size bug in fsx!");
855 if (testcalls <= simulatedopcount)
859 ((progressinterval && testcalls % progressinterval == 0) ||
861 (monitorstart == -1 ||
862 (offset + size > monitorstart &&
863 (monitorend == -1 || offset <= monitorend))))))
864 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
865 offset, offset + size - 1, size);
867 if (file_size > cur_filesize) {
868 if (ftruncate(fd, file_size) == -1) {
869 prterr("domapwrite: ftruncate");
873 pg_offset = offset & PAGE_MASK;
874 map_size = pg_offset + size;
876 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
877 MAP_FILE | MAP_SHARED, fd,
878 (off_t)(offset - pg_offset))) == (char *)-1) {
879 prterr("domapwrite: mmap");
882 memcpy(p + pg_offset, good_buf + offset, size);
883 if (msync(p, map_size, MS_SYNC) != 0) {
884 prterr("domapwrite: msync");
888 check_eofpage("Write", offset, p, size);
890 if (munmap(p, map_size) != 0) {
891 prterr("domapwrite: munmap");
898 dotruncate(unsigned size)
900 int oldsize = file_size;
902 size -= size % truncbdy;
903 if (size > biggest) {
905 if (!quiet && testcalls > simulatedopcount)
906 prt("truncating to largest ever: 0x%x\n", size);
909 log4(OP_TRUNCATE, 0, size, FL_NONE);
911 if (size > file_size)
912 memset(good_buf + file_size, '\0', size - file_size);
915 if (testcalls <= simulatedopcount)
918 if ((progressinterval && testcalls % progressinterval == 0) ||
919 (debug && (monitorstart == -1 || monitorend == -1 ||
920 size <= monitorend)))
921 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
922 if (ftruncate(fd, (off_t)size) == -1) {
923 prt("ftruncate1: %x\n", size);
924 prterr("dotruncate: ftruncate");
929 #ifdef FALLOC_FL_PUNCH_HOLE
931 do_punch_hole(unsigned offset, unsigned length)
936 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
939 if (!quiet && testcalls > simulatedopcount)
940 prt("skipping zero length punch hole\n");
941 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
945 if (file_size <= (loff_t)offset) {
946 if (!quiet && testcalls > simulatedopcount)
947 prt("skipping hole punch off the end of the file\n");
948 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
952 end_offset = offset + length;
954 log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
956 if (testcalls <= simulatedopcount)
959 if ((progressinterval && testcalls % progressinterval == 0) ||
960 (debug && (monitorstart == -1 || monitorend == -1 ||
961 end_offset <= monitorend))) {
962 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
963 offset, offset+length, length);
965 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
966 prt("%punch hole: %x to %x\n", offset, length);
967 prterr("do_punch_hole: fallocate");
972 max_offset = offset < file_size ? offset : file_size;
973 max_len = max_offset + length <= file_size ? length :
974 file_size - max_offset;
975 memset(good_buf + max_offset, '\0', max_len);
980 do_punch_hole(unsigned offset, unsigned length)
986 #ifdef FALLOC_FL_ZERO_RANGE
988 do_zero_range(unsigned offset, unsigned length, int keep_size)
991 int mode = FALLOC_FL_ZERO_RANGE;
994 if (!quiet && testcalls > simulatedopcount)
995 prt("skipping zero length zero range\n");
996 log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
997 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1001 end_offset = keep_size ? 0 : offset + length;
1003 if (end_offset > biggest) {
1004 biggest = end_offset;
1005 if (!quiet && testcalls > simulatedopcount)
1006 prt("zero_range to largest ever: 0x%x\n", end_offset);
1010 * last arg matches fallocate string array index in logdump:
1011 * 0: allocate past EOF
1012 * 1: extending prealloc
1013 * 2: interior prealloc
1015 log4(OP_ZERO_RANGE, offset, length,
1016 keep_size ? FL_KEEP_SIZE : FL_NONE);
1018 if (testcalls <= simulatedopcount)
1021 if ((progressinterval && testcalls % progressinterval == 0) ||
1022 (debug && (monitorstart == -1 || monitorend == -1 ||
1023 end_offset <= monitorend))) {
1024 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1025 offset, offset+length, length);
1027 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1028 prt("%pzero range: %x to %x\n", offset, length);
1029 prterr("do_zero_range: fallocate");
1030 report_failure(161);
1033 memset(good_buf + offset, '\0', length);
1038 do_zero_range(unsigned offset, unsigned length, int keep_size)
1044 #ifdef FALLOC_FL_COLLAPSE_RANGE
1046 do_collapse_range(unsigned offset, unsigned length)
1048 unsigned end_offset;
1049 int mode = FALLOC_FL_COLLAPSE_RANGE;
1052 if (!quiet && testcalls > simulatedopcount)
1053 prt("skipping zero length collapse range\n");
1054 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1058 end_offset = offset + length;
1059 if ((loff_t)end_offset >= file_size) {
1060 if (!quiet && testcalls > simulatedopcount)
1061 prt("skipping collapse range behind EOF\n");
1062 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1066 log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1068 if (testcalls <= simulatedopcount)
1071 if ((progressinterval && testcalls % progressinterval == 0) ||
1072 (debug && (monitorstart == -1 || monitorend == -1 ||
1073 end_offset <= monitorend))) {
1074 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1075 offset, offset+length, length);
1077 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1078 prt("collapse range: %x to %x\n", offset, length);
1079 prterr("do_collapse_range: fallocate");
1080 report_failure(161);
1083 memmove(good_buf + offset, good_buf + end_offset,
1084 file_size - end_offset);
1085 file_size -= length;
1090 do_collapse_range(unsigned offset, unsigned length)
1096 #ifdef FALLOC_FL_INSERT_RANGE
1098 do_insert_range(unsigned offset, unsigned length)
1100 unsigned end_offset;
1101 int mode = FALLOC_FL_INSERT_RANGE;
1104 if (!quiet && testcalls > simulatedopcount)
1105 prt("skipping zero length insert range\n");
1106 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1110 if ((loff_t)offset >= file_size) {
1111 if (!quiet && testcalls > simulatedopcount)
1112 prt("skipping insert range behind EOF\n");
1113 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1117 log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1119 if (testcalls <= simulatedopcount)
1122 end_offset = offset + length;
1123 if ((progressinterval && testcalls % progressinterval == 0) ||
1124 (debug && (monitorstart == -1 || monitorend == -1 ||
1125 end_offset <= monitorend))) {
1126 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1127 offset, offset+length, length);
1129 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1130 prt("insert range: %x to %x\n", offset, length);
1131 prterr("do_insert_range: fallocate");
1132 report_failure(161);
1135 memmove(good_buf + end_offset, good_buf + offset,
1136 file_size - offset);
1137 memset(good_buf + offset, '\0', length);
1138 file_size += length;
1143 do_insert_range(unsigned offset, unsigned length)
1149 #ifdef HAVE_LINUX_FALLOC_H
1150 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1152 do_preallocate(unsigned offset, unsigned length, int keep_size)
1154 unsigned end_offset;
1157 if (!quiet && testcalls > simulatedopcount)
1158 prt("skipping zero length fallocate\n");
1159 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1160 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1164 end_offset = keep_size ? 0 : offset + length;
1166 if (end_offset > biggest) {
1167 biggest = end_offset;
1168 if (!quiet && testcalls > simulatedopcount)
1169 prt("fallocating to largest ever: 0x%x\n", end_offset);
1173 * last arg matches fallocate string array index in logdump:
1174 * 0: allocate past EOF
1175 * 1: extending prealloc
1176 * 2: interior prealloc
1178 log4(OP_FALLOCATE, offset, length,
1179 keep_size ? FL_KEEP_SIZE : FL_NONE);
1181 if (end_offset > file_size) {
1182 memset(good_buf + file_size, '\0', end_offset - file_size);
1183 file_size = end_offset;
1186 if (testcalls <= simulatedopcount)
1189 if ((progressinterval && testcalls % progressinterval == 0) ||
1190 (debug && (monitorstart == -1 || monitorend == -1 ||
1191 end_offset <= monitorend)))
1192 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1193 offset, offset + length, length);
1194 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1195 prt("fallocate: %x to %x\n", offset, length);
1196 prterr("do_preallocate: fallocate");
1197 report_failure(161);
1202 do_preallocate(unsigned offset, unsigned length, int keep_size)
1213 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1214 prterr("writefileimage: lseek");
1215 report_failure(171);
1217 iret = write(fd, good_buf, file_size);
1218 if ((off_t)iret != file_size) {
1220 prterr("writefileimage: write");
1222 prt("short write: 0x%x bytes instead of 0x%llx\n",
1223 iret, (unsigned long long)file_size);
1224 report_failure(172);
1226 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1227 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1228 prterr("writefileimage: ftruncate");
1229 report_failure(173);
1237 if (testcalls <= simulatedopcount)
1241 prt("%lu close/open\n", testcalls);
1243 prterr("docloseopen: close");
1244 report_failure(180);
1246 fd = open(fname, O_RDWR|o_direct, 0);
1248 prterr("docloseopen: open");
1249 report_failure(181);
1254 #define TRIM_OFF(off, size) \
1262 #define TRIM_LEN(off, len, size) \
1264 if ((off) + (len) > (size)) \
1265 (len) = (size) - (off); \
1268 #define TRIM_OFF_LEN(off, len, size) \
1270 TRIM_OFF(off, size); \
1271 TRIM_LEN(off, len, size); \
1277 read_op(struct log_entry *log_entry)
1281 memset(log_entry, 0, sizeof(*log_entry));
1282 log_entry->operation = -1;
1284 while (log_entry->operation == -1) {
1289 if (!fgets(line, sizeof(line), replayopsf)) {
1290 if (feof(replayopsf)) {
1296 str = strtok(line, " \t\n");
1299 if (strcmp(str, "skip") == 0) {
1300 log_entry->flags |= FL_SKIPPED;
1301 str = strtok(NULL, " \t\n");
1305 log_entry->operation = op_code(str);
1306 if (log_entry->operation == -1)
1308 for (i = 0; i < 3; i++) {
1311 str = strtok(NULL, " \t\n");
1314 log_entry->args[i] = strtoul(str, &end, 0);
1318 while ((str = strtok(NULL, " \t\n"))) {
1319 if (strcmp(str, "keep_size") == 0)
1320 log_entry->flags |= FL_KEEP_SIZE;
1321 else if (strcmp(str, "close_open") == 0)
1322 log_entry->flags |= FL_CLOSE_OPEN;
1323 else if (strcmp(str, "*") == 0)
1324 ; /* overlap marker; ignore */
1332 fprintf(stderr, "%s: parse error\n", replayops);
1335 cleanup(100); /* doesn't return */
1343 unsigned long offset;
1349 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1354 if (debugstart > 0 && testcalls >= debugstart)
1357 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1358 prt("%lu...\n", testcalls);
1361 struct log_entry log_entry;
1363 while (read_op(&log_entry)) {
1364 if (log_entry.flags & FL_SKIPPED) {
1365 log4(log_entry.operation,
1366 log_entry.args[0], log_entry.args[1],
1371 op = log_entry.operation;
1372 offset = log_entry.args[0];
1373 size = log_entry.args[1];
1374 closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
1375 keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
1383 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1388 size = random() % (maxoplen + 1);
1390 /* calculate appropriate op to run */
1392 op = rv % OP_MAX_LITE;
1394 op = rv % OP_MAX_FULL;
1399 size = random() % maxfilelen;
1402 if (fallocate_calls && size && keep_size_calls)
1403 keep_size = random() % 2;
1406 if (zero_range_calls && size && keep_size_calls)
1407 keep_size = random() % 2;
1423 if (!fallocate_calls) {
1424 log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
1429 if (!punch_hole_calls) {
1430 log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
1435 if (!zero_range_calls) {
1436 log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
1440 case OP_COLLAPSE_RANGE:
1441 if (!collapse_range_calls) {
1442 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1446 case OP_INSERT_RANGE:
1447 if (!insert_range_calls) {
1448 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1456 TRIM_OFF_LEN(offset, size, file_size);
1457 doread(offset, size);
1461 TRIM_OFF_LEN(offset, size, maxfilelen);
1462 dowrite(offset, size);
1466 TRIM_OFF_LEN(offset, size, file_size);
1467 domapread(offset, size);
1471 TRIM_OFF_LEN(offset, size, maxfilelen);
1472 domapwrite(offset, size);
1480 TRIM_OFF_LEN(offset, size, maxfilelen);
1481 do_preallocate(offset, size, keep_size);
1485 TRIM_OFF_LEN(offset, size, file_size);
1486 do_punch_hole(offset, size);
1489 TRIM_OFF_LEN(offset, size, file_size);
1490 do_zero_range(offset, size, keep_size);
1492 case OP_COLLAPSE_RANGE:
1493 TRIM_OFF_LEN(offset, size, file_size - 1);
1494 offset = offset & ~(block_size - 1);
1495 size = size & ~(block_size - 1);
1497 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1500 do_collapse_range(offset, size);
1502 case OP_INSERT_RANGE:
1503 TRIM_OFF(offset, file_size);
1504 TRIM_LEN(file_size, size, maxfilelen);
1505 offset = offset & ~(block_size - 1);
1506 size = size & ~(block_size - 1);
1508 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1511 if (file_size + size > maxfilelen) {
1512 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1516 do_insert_range(offset, size);
1519 prterr("test: unknown operation");
1525 if (sizechecks && testcalls > simulatedopcount)
1538 prt("signal %d\n", sig);
1539 prt("testcalls = %lu\n", testcalls);
1547 fprintf(stdout, "usage: %s",
1548 "fsx [-dnqxAFLOWZ] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
1549 -b opnum: beginning operation number (default 1)\n\
1550 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1551 -d: debug output for all operations\n\
1552 -f flush and invalidate cache after I/O\n\
1553 -l flen: the upper bound on file size (default 262144)\n\
1554 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1555 -n: no verifications of file size\n\
1556 -o oplen: the upper bound on operation size (default 65536)\n\
1557 -p progressinterval: debug output at specified operation interval\n\
1558 -q: quieter operation\n\
1559 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1560 -s style: 1 gives smaller truncates (default 0)\n\
1561 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1562 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1563 -x: preallocate file space before starting, XFS only (default 0)\n\
1564 -y synchronize changes to a file\n"
1567 " -A: Use the AIO system calls\n"
1569 " -D startingop: debug output starting at specified operation\n"
1570 #ifdef HAVE_LINUX_FALLOC_H
1571 " -F: Do not use fallocate (preallocation) calls\n"
1573 #ifdef FALLOC_FL_PUNCH_HOLE
1574 " -H: Do not use punch hole calls\n"
1576 #ifdef FALLOC_FL_ZERO_RANGE
1577 " -z: Do not use zero range calls\n"
1579 #ifdef FALLOC_FL_COLLAPSE_RANGE
1580 " -C: Do not use collapse range calls\n"
1582 #ifdef FALLOC_FL_INSERT_RANGE
1583 " -I: Do not use insert range calls\n"
1585 " -L: fsxLite - no file creations & no file size changes\n\
1586 -N numops: total # operations to do (default infinity)\n\
1587 -O: use oplen (see -o flag) for every op (default random)\n\
1588 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1589 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1590 -W: mapped write operations DISabled\n\
1591 -R: read() system calls only (mapped reads disabled)\n\
1592 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1593 fname: this filename is REQUIRED (no default)\n");
1599 getnum(char *s, char **e)
1604 ret = strtol(s, e, 0);
1634 io_context_t io_ctx;
1640 ret = io_queue_init(QSZ, &io_ctx);
1642 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1650 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1652 struct io_event event;
1653 static struct timespec ts;
1654 struct iocb *iocbs[] = { &iocb };
1659 io_prep_pread(&iocb, fd, buf, len, offset);
1661 io_prep_pwrite(&iocb, fd, buf, len, offset);
1666 ret = io_submit(io_ctx, 1, iocbs);
1668 fprintf(stderr, "errcode=%d\n", ret);
1669 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1674 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1677 fprintf(stderr, "aio_rw: no events available\n");
1679 fprintf(stderr, "errcode=%d\n", -ret);
1680 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1685 if (len != event.res) {
1687 * The b0rked libaio defines event.res as unsigned.
1688 * However the kernel strucuture has it signed,
1689 * and it's used to pass negated error value.
1690 * Till the library is fixed use the temp var.
1692 res = (long)event.res;
1694 fprintf(stderr, "bad io length: %lu instead of %u\n",
1697 fprintf(stderr, "errcode=%ld\n", -res);
1698 fprintf(stderr, "aio_rw: async io failed: %s\n",
1709 * The caller expects error return in traditional libc
1710 * convention, i.e. -1 and the errno set to error.
1716 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1721 ret = __aio_rw(rw, fd, buf, len, offset);
1724 ret = read(fd, buf, len);
1726 ret = write(fd, buf, len);
1733 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1736 __test_fallocate(int mode, const char *mode_str)
1738 #ifdef HAVE_LINUX_FALLOC_H
1741 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1744 "main: filesystem does not support "
1745 "fallocate mode %s, disabling!\n",
1756 static struct option longopts[] = {
1757 {"replay-ops", required_argument, 0, 256},
1762 main(int argc, char **argv)
1766 char goodfile[1024];
1768 struct stat statbuf;
1773 page_size = getpagesize();
1774 page_mask = page_size - 1;
1775 mmap_mask = page_mask;
1778 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1780 while ((ch = getopt_long(argc, argv,
1781 "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ",
1782 longopts, NULL)) != EOF)
1785 simulatedopcount = getnum(optarg, &endp);
1787 fprintf(stdout, "Will begin at operation %ld\n",
1789 if (simulatedopcount == 0)
1791 simulatedopcount -= 1;
1794 closeprob = getnum(optarg, &endp);
1797 "Chance of close/open is 1 in %d\n",
1809 maxfilelen = getnum(optarg, &endp);
1810 if (maxfilelen <= 0)
1814 monitorstart = getnum(optarg, &endp);
1815 if (monitorstart < 0)
1817 if (!endp || *endp++ != ':')
1819 monitorend = getnum(endp, &endp);
1822 if (monitorend == 0)
1823 monitorend = -1; /* aka infinity */
1829 maxoplen = getnum(optarg, &endp);
1834 progressinterval = getnum(optarg, &endp);
1835 if (progressinterval == 0)
1842 readbdy = getnum(optarg, &endp);
1847 style = getnum(optarg, &endp);
1848 if (style < 0 || style > 1)
1852 truncbdy = getnum(optarg, &endp);
1857 writebdy = getnum(optarg, &endp);
1871 debugstart = getnum(optarg, &endp);
1876 fallocate_calls = 0;
1879 keep_size_calls = 0;
1882 punch_hole_calls = 0;
1885 zero_range_calls = 0;
1888 collapse_range_calls = 0;
1891 insert_range_calls = 0;
1897 numops = getnum(optarg, &endp);
1905 strncpy(goodfile, optarg, sizeof(goodfile));
1906 strcat(goodfile, "/");
1907 strncpy(logfile, optarg, sizeof(logfile));
1908 strcat(logfile, "/");
1914 seed = getnum(optarg, &endp);
1916 seed = time(0) % 10000;
1918 fprintf(stdout, "Seed set to %d\n", seed);
1925 fprintf(stdout, "mapped writes DISABLED\n");
1928 o_direct = O_DIRECT;
1930 case 256: /* --replay-ops */
1943 signal(SIGHUP, cleanup);
1944 signal(SIGINT, cleanup);
1945 signal(SIGPIPE, cleanup);
1946 signal(SIGALRM, cleanup);
1947 signal(SIGTERM, cleanup);
1948 signal(SIGXCPU, cleanup);
1949 signal(SIGXFSZ, cleanup);
1950 signal(SIGVTALRM, cleanup);
1951 signal(SIGUSR1, cleanup);
1952 signal(SIGUSR2, cleanup);
1954 initstate(seed, state, 256);
1957 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1962 if (fstat(fd, &statbuf)) {
1963 prterr("check_size: fstat");
1966 block_size = statbuf.st_blksize;
1969 xfs_flock64_t resv = { 0 };
1970 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1971 if (!platform_test_xfs_fd(fd)) {
1973 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1977 resv.l_len = maxfilelen;
1978 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1984 strncat(goodfile, fname, 256);
1985 strcat (goodfile, ".fsxgood");
1986 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1987 if (fsxgoodfd < 0) {
1991 strncat(logfile, fname, 256);
1992 strcat (logfile, ".fsxlog");
1993 fsxlogf = fopen(logfile, "w");
1994 if (fsxlogf == NULL) {
1998 strncat(opsfile, fname, 256);
1999 strcat(opsfile, ".fsxops");
2003 replayopsf = fopen(replayops, "r");
2017 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
2018 if (file_size == (off_t)-1) {
2020 warn("main: lseek eof");
2023 ret = lseek(fd, (off_t)0, SEEK_SET);
2024 if (ret == (off_t)-1) {
2026 warn("main: lseek 0");
2030 original_buf = (char *) malloc(maxfilelen);
2031 for (i = 0; i < maxfilelen; i++)
2032 original_buf[i] = random() % 256;
2033 good_buf = (char *) malloc(maxfilelen + writebdy);
2034 good_buf = round_ptr_up(good_buf, writebdy, 0);
2035 memset(good_buf, '\0', maxfilelen);
2036 temp_buf = (char *) malloc(maxoplen + readbdy);
2037 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
2038 memset(temp_buf, '\0', maxoplen);
2039 if (lite) { /* zero entire existing file */
2042 written = write(fd, good_buf, (size_t)maxfilelen);
2043 if (written != maxfilelen) {
2044 if (written == -1) {
2046 warn("main: error on write");
2048 warn("main: short write, 0x%x bytes instead "
2057 if (fallocate_calls)
2058 fallocate_calls = test_fallocate(0);
2059 if (keep_size_calls)
2060 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
2061 if (punch_hole_calls)
2062 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2063 if (zero_range_calls)
2064 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
2065 if (collapse_range_calls)
2066 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
2067 if (insert_range_calls)
2068 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
2070 while (numops == -1 || numops--)
2078 prt("All %lu operations completed A-OK!\n", testcalls);