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:
80 * When mapped read/writes are disabled, they are simply converted to normal
81 * reads and writes. When fallocate/fpunch calls are disabled, they are
84 * Because of the "lite" version, we also need to have different "maximum
85 * operation" defines to allow the ops to be selected correctly based on the
89 /* common operations */
96 /* !lite operations */
98 #define OP_FALLOCATE 5
99 #define OP_PUNCH_HOLE 6
100 #define OP_ZERO_RANGE 7
101 #define OP_COLLAPSE_RANGE 8
102 #define OP_INSERT_RANGE 9
103 #define OP_MAX_FULL 10
106 #define PAGE_SIZE getpagesize()
108 #define PAGE_MASK (PAGE_SIZE - 1)
110 char *original_buf; /* a pointer to the original data */
111 char *good_buf; /* a pointer to the correct data */
112 char *temp_buf; /* a pointer to the current data */
113 char *fname; /* name of our test file */
114 int fd; /* fd for our test file */
116 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, "%s", buffer);
216 fprintf(fsxlogf, "%s", 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 if (ftruncate(fd, (off_t)0))
574 if (ftruncate(fd, (off_t)100000))
577 if (statbuf.st_size != (off_t)100000) {
578 prt("no extend on truncate! not posix!\n");
581 if (ftruncate(fd, 0)) {
583 prterr("check_trunc_hack: ftruncate");
589 doflush(unsigned offset, unsigned size)
595 if (o_direct == O_DIRECT)
598 pg_offset = offset & mmap_mask;
599 map_size = pg_offset + size;
601 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
602 MAP_FILE | MAP_SHARED, fd,
603 (off_t)(offset - pg_offset))) == (char *)-1) {
604 prterr("doflush: mmap");
607 if (msync(p, map_size, MS_INVALIDATE) != 0) {
608 prterr("doflush: msync");
611 if (munmap(p, map_size) != 0) {
612 prterr("doflush: munmap");
618 doread(unsigned offset, unsigned size)
623 offset -= offset % readbdy;
625 size -= size % readbdy;
627 if (!quiet && testcalls > simulatedopcount && !o_direct)
628 prt("skipping zero size read\n");
629 log4(OP_READ, offset, size, FL_SKIPPED);
632 if (size + offset > file_size) {
633 if (!quiet && testcalls > simulatedopcount)
634 prt("skipping seek/read past end of file\n");
635 log4(OP_READ, offset, size, FL_SKIPPED);
639 log4(OP_READ, offset, size, FL_NONE);
641 if (testcalls <= simulatedopcount)
645 ((progressinterval && testcalls % progressinterval == 0) ||
647 (monitorstart == -1 ||
648 (offset + size > monitorstart &&
649 (monitorend == -1 || offset <= monitorend))))))
650 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
651 offset, offset + size - 1, size);
652 ret = lseek(fd, (off_t)offset, SEEK_SET);
653 if (ret == (off_t)-1) {
654 prterr("doread: lseek");
657 iret = fsxread(fd, temp_buf, size, offset);
660 prterr("doread: read");
662 prt("short read: 0x%x bytes instead of 0x%x\n",
666 check_buffers(offset, size);
671 check_eofpage(char *s, unsigned offset, char *p, int size)
673 unsigned long last_page, should_be_zero;
675 if (offset + size <= (file_size & ~page_mask))
678 * we landed in the last page of the file
679 * test to make sure the VM system provided 0's
680 * beyond the true end of the file mapping
681 * (as required by mmap def in 1996 posix 1003.1)
683 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
685 for (should_be_zero = last_page + (file_size & page_mask);
686 should_be_zero < last_page + page_size;
688 if (*(char *)should_be_zero) {
689 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
690 s, file_size - 1, should_be_zero & page_mask,
691 short_at(should_be_zero));
698 domapread(unsigned offset, unsigned size)
704 offset -= offset % readbdy;
706 if (!quiet && testcalls > simulatedopcount)
707 prt("skipping zero size read\n");
708 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
711 if (size + offset > file_size) {
712 if (!quiet && testcalls > simulatedopcount)
713 prt("skipping seek/read past end of file\n");
714 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
718 log4(OP_MAPREAD, offset, size, FL_NONE);
720 if (testcalls <= simulatedopcount)
724 ((progressinterval && testcalls % progressinterval == 0) ||
726 (monitorstart == -1 ||
727 (offset + size > monitorstart &&
728 (monitorend == -1 || offset <= monitorend))))))
729 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
730 offset, offset + size - 1, size);
732 pg_offset = offset & PAGE_MASK;
733 map_size = pg_offset + size;
735 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
736 (off_t)(offset - pg_offset))) == (char *)-1) {
737 prterr("domapread: mmap");
740 memcpy(temp_buf, p + pg_offset, size);
742 check_eofpage("Read", offset, p, size);
744 if (munmap(p, map_size) != 0) {
745 prterr("domapread: munmap");
749 check_buffers(offset, size);
754 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
757 good_buf[offset] = testcalls % 256;
759 good_buf[offset] += original_buf[offset];
766 dowrite(unsigned offset, unsigned size)
771 offset -= offset % writebdy;
773 size -= size % writebdy;
775 if (!quiet && testcalls > simulatedopcount && !o_direct)
776 prt("skipping zero size write\n");
777 log4(OP_WRITE, offset, size, FL_SKIPPED);
781 log4(OP_WRITE, offset, size, FL_NONE);
783 gendata(original_buf, good_buf, offset, size);
784 if (file_size < offset + size) {
785 if (file_size < offset)
786 memset(good_buf + file_size, '\0', offset - file_size);
787 file_size = offset + size;
789 warn("Lite file size bug in fsx!");
794 if (testcalls <= simulatedopcount)
798 ((progressinterval && testcalls % progressinterval == 0) ||
800 (monitorstart == -1 ||
801 (offset + size > monitorstart &&
802 (monitorend == -1 || offset <= monitorend))))))
803 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
804 offset, offset + size - 1, size);
805 ret = lseek(fd, (off_t)offset, SEEK_SET);
806 if (ret == (off_t)-1) {
807 prterr("dowrite: lseek");
810 iret = fsxwrite(fd, good_buf + offset, size, offset);
813 prterr("dowrite: write");
815 prt("short write: 0x%x bytes instead of 0x%x\n",
821 prt("fsync() failed: %s\n", strerror(errno));
826 doflush(offset, size);
832 domapwrite(unsigned offset, unsigned size)
839 offset -= offset % writebdy;
841 if (!quiet && testcalls > simulatedopcount)
842 prt("skipping zero size write\n");
843 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
846 cur_filesize = file_size;
848 log4(OP_MAPWRITE, offset, size, FL_NONE);
850 gendata(original_buf, good_buf, offset, size);
851 if (file_size < offset + size) {
852 if (file_size < offset)
853 memset(good_buf + file_size, '\0', offset - file_size);
854 file_size = offset + size;
856 warn("Lite file size bug in fsx!");
861 if (testcalls <= simulatedopcount)
865 ((progressinterval && testcalls % progressinterval == 0) ||
867 (monitorstart == -1 ||
868 (offset + size > monitorstart &&
869 (monitorend == -1 || offset <= monitorend))))))
870 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
871 offset, offset + size - 1, size);
873 if (file_size > cur_filesize) {
874 if (ftruncate(fd, file_size) == -1) {
875 prterr("domapwrite: ftruncate");
879 pg_offset = offset & PAGE_MASK;
880 map_size = pg_offset + size;
882 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
883 MAP_FILE | MAP_SHARED, fd,
884 (off_t)(offset - pg_offset))) == (char *)-1) {
885 prterr("domapwrite: mmap");
888 memcpy(p + pg_offset, good_buf + offset, size);
889 if (msync(p, map_size, MS_SYNC) != 0) {
890 prterr("domapwrite: msync");
894 check_eofpage("Write", offset, p, size);
896 if (munmap(p, map_size) != 0) {
897 prterr("domapwrite: munmap");
904 dotruncate(unsigned size)
906 int oldsize = file_size;
908 size -= size % truncbdy;
909 if (size > biggest) {
911 if (!quiet && testcalls > simulatedopcount)
912 prt("truncating to largest ever: 0x%x\n", size);
915 log4(OP_TRUNCATE, 0, size, FL_NONE);
917 if (size > file_size)
918 memset(good_buf + file_size, '\0', size - file_size);
921 if (testcalls <= simulatedopcount)
924 if ((progressinterval && testcalls % progressinterval == 0) ||
925 (debug && (monitorstart == -1 || monitorend == -1 ||
926 size <= monitorend)))
927 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
928 if (ftruncate(fd, (off_t)size) == -1) {
929 prt("ftruncate1: %x\n", size);
930 prterr("dotruncate: ftruncate");
935 #ifdef FALLOC_FL_PUNCH_HOLE
937 do_punch_hole(unsigned offset, unsigned length)
942 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
945 if (!quiet && testcalls > simulatedopcount)
946 prt("skipping zero length punch hole\n");
947 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
951 if (file_size <= (loff_t)offset) {
952 if (!quiet && testcalls > simulatedopcount)
953 prt("skipping hole punch off the end of the file\n");
954 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
958 end_offset = offset + length;
960 log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
962 if (testcalls <= simulatedopcount)
965 if ((progressinterval && testcalls % progressinterval == 0) ||
966 (debug && (monitorstart == -1 || monitorend == -1 ||
967 end_offset <= monitorend))) {
968 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
969 offset, offset+length, length);
971 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
972 prt("punch hole: 0x%x to 0x%x\n", offset, offset + length);
973 prterr("do_punch_hole: fallocate");
978 max_offset = offset < file_size ? offset : file_size;
979 max_len = max_offset + length <= file_size ? length :
980 file_size - max_offset;
981 memset(good_buf + max_offset, '\0', max_len);
986 do_punch_hole(unsigned offset, unsigned length)
992 #ifdef FALLOC_FL_ZERO_RANGE
994 do_zero_range(unsigned offset, unsigned length, int keep_size)
997 int mode = FALLOC_FL_ZERO_RANGE;
1000 if (!quiet && testcalls > simulatedopcount)
1001 prt("skipping zero length zero range\n");
1002 log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
1003 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1007 end_offset = keep_size ? 0 : offset + length;
1009 if (end_offset > biggest) {
1010 biggest = end_offset;
1011 if (!quiet && testcalls > simulatedopcount)
1012 prt("zero_range to largest ever: 0x%x\n", end_offset);
1016 * last arg matches fallocate string array index in logdump:
1017 * 0: allocate past EOF
1018 * 1: extending prealloc
1019 * 2: interior prealloc
1021 log4(OP_ZERO_RANGE, offset, length,
1022 keep_size ? FL_KEEP_SIZE : FL_NONE);
1024 if (testcalls <= simulatedopcount)
1027 if ((progressinterval && testcalls % progressinterval == 0) ||
1028 (debug && (monitorstart == -1 || monitorend == -1 ||
1029 end_offset <= monitorend))) {
1030 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1031 offset, offset+length, length);
1033 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1034 prt("zero range: 0x%x to 0x%x\n", offset, offset + length);
1035 prterr("do_zero_range: fallocate");
1036 report_failure(161);
1039 memset(good_buf + offset, '\0', length);
1044 do_zero_range(unsigned offset, unsigned length, int keep_size)
1050 #ifdef FALLOC_FL_COLLAPSE_RANGE
1052 do_collapse_range(unsigned offset, unsigned length)
1054 unsigned end_offset;
1055 int mode = FALLOC_FL_COLLAPSE_RANGE;
1058 if (!quiet && testcalls > simulatedopcount)
1059 prt("skipping zero length collapse range\n");
1060 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1064 end_offset = offset + length;
1065 if ((loff_t)end_offset >= file_size) {
1066 if (!quiet && testcalls > simulatedopcount)
1067 prt("skipping collapse range behind EOF\n");
1068 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1072 log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1074 if (testcalls <= simulatedopcount)
1077 if ((progressinterval && testcalls % progressinterval == 0) ||
1078 (debug && (monitorstart == -1 || monitorend == -1 ||
1079 end_offset <= monitorend))) {
1080 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1081 offset, offset+length, length);
1083 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1084 prt("collapse range: 0x%x to 0x%x\n", offset, offset + length);
1085 prterr("do_collapse_range: fallocate");
1086 report_failure(161);
1089 memmove(good_buf + offset, good_buf + end_offset,
1090 file_size - end_offset);
1091 file_size -= length;
1096 do_collapse_range(unsigned offset, unsigned length)
1102 #ifdef FALLOC_FL_INSERT_RANGE
1104 do_insert_range(unsigned offset, unsigned length)
1106 unsigned end_offset;
1107 int mode = FALLOC_FL_INSERT_RANGE;
1110 if (!quiet && testcalls > simulatedopcount)
1111 prt("skipping zero length insert range\n");
1112 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1116 if ((loff_t)offset >= file_size) {
1117 if (!quiet && testcalls > simulatedopcount)
1118 prt("skipping insert range behind EOF\n");
1119 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1123 log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1125 if (testcalls <= simulatedopcount)
1128 end_offset = offset + length;
1129 if ((progressinterval && testcalls % progressinterval == 0) ||
1130 (debug && (monitorstart == -1 || monitorend == -1 ||
1131 end_offset <= monitorend))) {
1132 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1133 offset, offset+length, length);
1135 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1136 prt("insert range: 0x%x to 0x%x\n", offset, offset + length);
1137 prterr("do_insert_range: fallocate");
1138 report_failure(161);
1141 memmove(good_buf + end_offset, good_buf + offset,
1142 file_size - offset);
1143 memset(good_buf + offset, '\0', length);
1144 file_size += length;
1149 do_insert_range(unsigned offset, unsigned length)
1155 #ifdef HAVE_LINUX_FALLOC_H
1156 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1158 do_preallocate(unsigned offset, unsigned length, int keep_size)
1160 unsigned end_offset;
1163 if (!quiet && testcalls > simulatedopcount)
1164 prt("skipping zero length fallocate\n");
1165 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1166 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1170 end_offset = keep_size ? 0 : offset + length;
1172 if (end_offset > biggest) {
1173 biggest = end_offset;
1174 if (!quiet && testcalls > simulatedopcount)
1175 prt("fallocating to largest ever: 0x%x\n", end_offset);
1179 * last arg matches fallocate string array index in logdump:
1180 * 0: allocate past EOF
1181 * 1: extending prealloc
1182 * 2: interior prealloc
1184 log4(OP_FALLOCATE, offset, length,
1185 keep_size ? FL_KEEP_SIZE : FL_NONE);
1187 if (end_offset > file_size) {
1188 memset(good_buf + file_size, '\0', end_offset - file_size);
1189 file_size = end_offset;
1192 if (testcalls <= simulatedopcount)
1195 if ((progressinterval && testcalls % progressinterval == 0) ||
1196 (debug && (monitorstart == -1 || monitorend == -1 ||
1197 end_offset <= monitorend)))
1198 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1199 offset, offset + length, length);
1200 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1201 prt("fallocate: 0x%x to 0x%x\n", offset, offset + length);
1202 prterr("do_preallocate: fallocate");
1203 report_failure(161);
1208 do_preallocate(unsigned offset, unsigned length, int keep_size)
1219 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1220 prterr("writefileimage: lseek");
1221 report_failure(171);
1223 iret = write(fd, good_buf, file_size);
1224 if ((off_t)iret != file_size) {
1226 prterr("writefileimage: write");
1228 prt("short write: 0x%x bytes instead of 0x%llx\n",
1229 iret, (unsigned long long)file_size);
1230 report_failure(172);
1232 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1233 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1234 prterr("writefileimage: ftruncate");
1235 report_failure(173);
1243 if (testcalls <= simulatedopcount)
1247 prt("%lu close/open\n", testcalls);
1249 prterr("docloseopen: close");
1250 report_failure(180);
1252 fd = open(fname, O_RDWR|o_direct, 0);
1254 prterr("docloseopen: open");
1255 report_failure(181);
1260 #define TRIM_OFF(off, size) \
1268 #define TRIM_LEN(off, len, size) \
1270 if ((off) + (len) > (size)) \
1271 (len) = (size) - (off); \
1274 #define TRIM_OFF_LEN(off, len, size) \
1276 TRIM_OFF(off, size); \
1277 TRIM_LEN(off, len, size); \
1284 prt("signal %d\n", sig);
1285 prt("testcalls = %lu\n", testcalls);
1290 read_op(struct log_entry *log_entry)
1294 memset(log_entry, 0, sizeof(*log_entry));
1295 log_entry->operation = -1;
1297 while (log_entry->operation == -1) {
1302 if (!fgets(line, sizeof(line), replayopsf)) {
1303 if (feof(replayopsf)) {
1309 str = strtok(line, " \t\n");
1312 if (strcmp(str, "skip") == 0) {
1313 log_entry->flags |= FL_SKIPPED;
1314 str = strtok(NULL, " \t\n");
1318 log_entry->operation = op_code(str);
1319 if (log_entry->operation == -1)
1321 for (i = 0; i < 3; i++) {
1324 str = strtok(NULL, " \t\n");
1327 log_entry->args[i] = strtoul(str, &end, 0);
1331 while ((str = strtok(NULL, " \t\n"))) {
1332 if (strcmp(str, "keep_size") == 0)
1333 log_entry->flags |= FL_KEEP_SIZE;
1334 else if (strcmp(str, "close_open") == 0)
1335 log_entry->flags |= FL_CLOSE_OPEN;
1336 else if (strcmp(str, "*") == 0)
1337 ; /* overlap marker; ignore */
1345 fprintf(stderr, "%s: parse error\n", replayops);
1348 cleanup(100); /* doesn't return */
1356 unsigned long offset;
1362 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1367 if (debugstart > 0 && testcalls >= debugstart)
1370 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1371 prt("%lu...\n", testcalls);
1374 struct log_entry log_entry;
1376 while (read_op(&log_entry)) {
1377 if (log_entry.flags & FL_SKIPPED) {
1378 log4(log_entry.operation,
1379 log_entry.args[0], log_entry.args[1],
1384 op = log_entry.operation;
1385 offset = log_entry.args[0];
1386 size = log_entry.args[1];
1387 closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
1388 keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
1396 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1401 size = random() % (maxoplen + 1);
1403 /* calculate appropriate op to run */
1405 op = rv % OP_MAX_LITE;
1407 op = rv % OP_MAX_FULL;
1412 size = random() % maxfilelen;
1415 if (fallocate_calls && size && keep_size_calls)
1416 keep_size = random() % 2;
1419 if (zero_range_calls && size && keep_size_calls)
1420 keep_size = random() % 2;
1436 if (!fallocate_calls) {
1437 log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
1442 if (!punch_hole_calls) {
1443 log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
1448 if (!zero_range_calls) {
1449 log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
1453 case OP_COLLAPSE_RANGE:
1454 if (!collapse_range_calls) {
1455 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1459 case OP_INSERT_RANGE:
1460 if (!insert_range_calls) {
1461 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1469 TRIM_OFF_LEN(offset, size, file_size);
1470 doread(offset, size);
1474 TRIM_OFF_LEN(offset, size, maxfilelen);
1475 dowrite(offset, size);
1479 TRIM_OFF_LEN(offset, size, file_size);
1480 domapread(offset, size);
1484 TRIM_OFF_LEN(offset, size, maxfilelen);
1485 domapwrite(offset, size);
1493 TRIM_OFF_LEN(offset, size, maxfilelen);
1494 do_preallocate(offset, size, keep_size);
1498 TRIM_OFF_LEN(offset, size, file_size);
1499 do_punch_hole(offset, size);
1502 TRIM_OFF_LEN(offset, size, file_size);
1503 do_zero_range(offset, size, keep_size);
1505 case OP_COLLAPSE_RANGE:
1506 TRIM_OFF_LEN(offset, size, file_size - 1);
1507 offset = offset & ~(block_size - 1);
1508 size = size & ~(block_size - 1);
1510 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1513 do_collapse_range(offset, size);
1515 case OP_INSERT_RANGE:
1516 TRIM_OFF(offset, file_size);
1517 TRIM_LEN(file_size, size, maxfilelen);
1518 offset = offset & ~(block_size - 1);
1519 size = size & ~(block_size - 1);
1521 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1524 if (file_size + size > maxfilelen) {
1525 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1529 do_insert_range(offset, size);
1532 prterr("test: unknown operation");
1538 if (sizechecks && testcalls > simulatedopcount)
1549 fprintf(stdout, "usage: %s",
1550 "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\
1551 -b opnum: beginning operation number (default 1)\n\
1552 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1553 -d: debug output for all operations\n\
1554 -f flush and invalidate cache after I/O\n\
1555 -l flen: the upper bound on file size (default 262144)\n\
1556 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1557 -n: no verifications of file size\n\
1558 -o oplen: the upper bound on operation size (default 65536)\n\
1559 -p progressinterval: debug output at specified operation interval\n\
1560 -q: quieter operation\n\
1561 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1562 -s style: 1 gives smaller truncates (default 0)\n\
1563 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1564 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1565 -x: preallocate file space before starting, XFS only (default 0)\n\
1566 -y synchronize changes to a file\n"
1569 " -A: Use the AIO system calls\n"
1571 " -D startingop: debug output starting at specified operation\n"
1572 #ifdef HAVE_LINUX_FALLOC_H
1573 " -F: Do not use fallocate (preallocation) calls\n"
1575 #ifdef FALLOC_FL_PUNCH_HOLE
1576 " -H: Do not use punch hole calls\n"
1578 #ifdef FALLOC_FL_ZERO_RANGE
1579 " -z: Do not use zero range calls\n"
1581 #ifdef FALLOC_FL_COLLAPSE_RANGE
1582 " -C: Do not use collapse range calls\n"
1584 #ifdef FALLOC_FL_INSERT_RANGE
1585 " -I: Do not use insert range calls\n"
1587 " -L: fsxLite - no file creations & no file size changes\n\
1588 -N numops: total # operations to do (default infinity)\n\
1589 -O: use oplen (see -o flag) for every op (default random)\n\
1590 -P: save .fsxlog .fsxops and .fsxgood files in dirpath (default ./)\n\
1591 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1592 -W: mapped write operations DISabled\n\
1593 -R: read() system calls only (mapped reads disabled)\n\
1594 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1595 fname: this filename is REQUIRED (no default)\n");
1601 getnum(char *s, char **e)
1606 ret = strtol(s, e, 0);
1636 io_context_t io_ctx;
1642 ret = io_queue_init(QSZ, &io_ctx);
1644 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1652 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1654 struct io_event event;
1655 static struct timespec ts;
1656 struct iocb *iocbs[] = { &iocb };
1661 io_prep_pread(&iocb, fd, buf, len, offset);
1663 io_prep_pwrite(&iocb, fd, buf, len, offset);
1668 ret = io_submit(io_ctx, 1, iocbs);
1670 fprintf(stderr, "errcode=%d\n", ret);
1671 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1676 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1679 fprintf(stderr, "aio_rw: no events available\n");
1681 fprintf(stderr, "errcode=%d\n", -ret);
1682 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1687 if (len != event.res) {
1689 * The b0rked libaio defines event.res as unsigned.
1690 * However the kernel strucuture has it signed,
1691 * and it's used to pass negated error value.
1692 * Till the library is fixed use the temp var.
1694 res = (long)event.res;
1696 fprintf(stderr, "bad io length: %lu instead of %u\n",
1699 fprintf(stderr, "errcode=%ld\n", -res);
1700 fprintf(stderr, "aio_rw: async io failed: %s\n",
1711 * The caller expects error return in traditional libc
1712 * convention, i.e. -1 and the errno set to error.
1718 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1723 ret = __aio_rw(rw, fd, buf, len, offset);
1726 ret = read(fd, buf, len);
1728 ret = write(fd, buf, len);
1735 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1738 __test_fallocate(int mode, const char *mode_str)
1740 #ifdef HAVE_LINUX_FALLOC_H
1743 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1746 "main: filesystem does not support "
1747 "fallocate mode %s, disabling!\n",
1751 if (ftruncate(fd, 0)) {
1752 warn("main: ftruncate");
1761 static struct option longopts[] = {
1762 {"replay-ops", required_argument, 0, 256},
1767 main(int argc, char **argv)
1771 char goodfile[1024];
1774 struct stat statbuf;
1779 page_size = getpagesize();
1780 page_mask = page_size - 1;
1781 mmap_mask = page_mask;
1784 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1786 while ((ch = getopt_long(argc, argv,
1787 "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ",
1788 longopts, NULL)) != EOF)
1791 simulatedopcount = getnum(optarg, &endp);
1793 fprintf(stdout, "Will begin at operation %ld\n",
1795 if (simulatedopcount == 0)
1797 simulatedopcount -= 1;
1800 closeprob = getnum(optarg, &endp);
1803 "Chance of close/open is 1 in %d\n",
1815 maxfilelen = getnum(optarg, &endp);
1816 if (maxfilelen <= 0)
1820 monitorstart = getnum(optarg, &endp);
1821 if (monitorstart < 0)
1823 if (!endp || *endp++ != ':')
1825 monitorend = getnum(endp, &endp);
1828 if (monitorend == 0)
1829 monitorend = -1; /* aka infinity */
1835 maxoplen = getnum(optarg, &endp);
1840 progressinterval = getnum(optarg, &endp);
1841 if (progressinterval == 0)
1848 readbdy = getnum(optarg, &endp);
1853 style = getnum(optarg, &endp);
1854 if (style < 0 || style > 1)
1858 truncbdy = getnum(optarg, &endp);
1863 writebdy = getnum(optarg, &endp);
1877 debugstart = getnum(optarg, &endp);
1882 fallocate_calls = 0;
1885 keep_size_calls = 0;
1888 punch_hole_calls = 0;
1891 zero_range_calls = 0;
1894 collapse_range_calls = 0;
1897 insert_range_calls = 0;
1903 numops = getnum(optarg, &endp);
1911 strncpy(goodfile, optarg, sizeof(goodfile));
1912 strcat(goodfile, "/");
1913 strncpy(logfile, optarg, sizeof(logfile));
1914 strcat(logfile, "/");
1915 strncpy(opsfile, optarg, sizeof(logfile));
1916 strcat(opsfile, "/");
1923 seed = getnum(optarg, &endp);
1925 seed = time(0) % 10000;
1926 seed += (int)getpid();
1929 fprintf(stdout, "Seed set to %d\n", seed);
1936 fprintf(stdout, "mapped writes DISABLED\n");
1939 o_direct = O_DIRECT;
1941 case 256: /* --replay-ops */
1954 signal(SIGHUP, cleanup);
1955 signal(SIGINT, cleanup);
1956 signal(SIGPIPE, cleanup);
1957 signal(SIGALRM, cleanup);
1958 signal(SIGTERM, cleanup);
1959 signal(SIGXCPU, cleanup);
1960 signal(SIGXFSZ, cleanup);
1961 signal(SIGVTALRM, cleanup);
1962 signal(SIGUSR1, cleanup);
1963 signal(SIGUSR2, cleanup);
1967 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1972 if (fstat(fd, &statbuf)) {
1973 prterr("check_size: fstat");
1976 block_size = statbuf.st_blksize;
1979 xfs_flock64_t resv = { 0 };
1980 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1981 if (!platform_test_xfs_fd(fd)) {
1983 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1987 resv.l_len = maxfilelen;
1988 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1994 strncat(goodfile, dirpath ? basename(fname) : fname, 256);
1995 strcat (goodfile, ".fsxgood");
1996 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1997 if (fsxgoodfd < 0) {
2001 strncat(logfile, dirpath ? basename(fname) : fname, 256);
2002 strcat (logfile, ".fsxlog");
2003 fsxlogf = fopen(logfile, "w");
2004 if (fsxlogf == NULL) {
2008 strncat(opsfile, dirpath ? basename(fname) : fname, 256);
2009 strcat(opsfile, ".fsxops");
2013 replayopsf = fopen(replayops, "r");
2027 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
2028 if (file_size == (off_t)-1) {
2030 warn("main: lseek eof");
2033 ret = lseek(fd, (off_t)0, SEEK_SET);
2034 if (ret == (off_t)-1) {
2036 warn("main: lseek 0");
2040 original_buf = (char *) malloc(maxfilelen);
2041 for (i = 0; i < maxfilelen; i++)
2042 original_buf[i] = random() % 256;
2043 good_buf = (char *) malloc(maxfilelen + writebdy);
2044 good_buf = round_ptr_up(good_buf, writebdy, 0);
2045 memset(good_buf, '\0', maxfilelen);
2046 temp_buf = (char *) malloc(maxoplen + readbdy);
2047 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
2048 memset(temp_buf, '\0', maxoplen);
2049 if (lite) { /* zero entire existing file */
2052 written = write(fd, good_buf, (size_t)maxfilelen);
2053 if (written != maxfilelen) {
2054 if (written == -1) {
2056 warn("main: error on write");
2058 warn("main: short write, 0x%x bytes instead "
2067 if (fallocate_calls)
2068 fallocate_calls = test_fallocate(0);
2069 if (keep_size_calls)
2070 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
2071 if (punch_hole_calls)
2072 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2073 if (zero_range_calls)
2074 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
2075 if (collapse_range_calls)
2076 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
2077 if (insert_range_calls)
2078 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
2080 while (numops == -1 || numops--)
2088 prt("All %lu operations completed A-OK!\n", testcalls);