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;
120 unsigned long testcalls = 0; /* calls to function "test" */
122 unsigned long simulatedopcount = 0; /* -b flag */
123 int closeprob = 0; /* -c flag */
124 int debug = 0; /* -d flag */
125 unsigned long debugstart = 0; /* -D flag */
126 int flush = 0; /* -f flag */
127 int do_fsync = 0; /* -y flag */
128 unsigned long maxfilelen = 256 * 1024; /* -l flag */
129 int sizechecks = 1; /* -n flag disables them */
130 int maxoplen = 64 * 1024; /* -o flag */
131 int quiet = 0; /* -q flag */
132 unsigned long progressinterval = 0; /* -p flag */
133 int readbdy = 1; /* -r flag */
134 int style = 0; /* -s flag */
135 int prealloc = 0; /* -x flag */
136 int truncbdy = 1; /* -t flag */
137 int writebdy = 1; /* -w flag */
138 long monitorstart = -1; /* -m flag */
139 long monitorend = -1; /* -m flag */
140 int lite = 0; /* -L flag */
141 long numops = -1; /* -N flag */
142 int randomoplen = 1; /* -O flag disables it */
143 int seed = 1; /* -S flag */
144 int mapped_writes = 1; /* -W flag disables */
145 int fallocate_calls = 1; /* -F flag disables */
146 int keep_size_calls = 1; /* -K flag disables */
147 int punch_hole_calls = 1; /* -H flag disables */
148 int zero_range_calls = 1; /* -z flag disables */
149 int collapse_range_calls = 1; /* -C flag disables */
150 int insert_range_calls = 1; /* -I flag disables */
151 int mapped_reads = 1; /* -R flag disables it */
153 int o_direct; /* -Z */
160 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
163 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
164 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
166 #define fsxread(a,b,c,d) read(a,b,c)
167 #define fsxwrite(a,b,c,d) write(a,b,c)
170 const char *replayops = NULL;
171 FILE * fsxlogf = NULL;
172 FILE * replayopsf = NULL;
177 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
179 unsigned long ret = (unsigned long)ptr;
181 ret = ((ret + align - 1) & ~(align - 1));
187 vwarnc(int code, const char *fmt, va_list ap) {
188 fprintf(stderr, "fsx: ");
190 vfprintf(stderr, fmt, ap);
191 fprintf(stderr, ": ");
193 fprintf(stderr, "%s\n", strerror(code));
197 warn(const char * fmt, ...) {
200 vwarnc(errno, fmt, ap);
204 #define BUF_SIZE 1024
207 prt(const char *fmt, ...)
210 char buffer[BUF_SIZE];
213 vsnprintf(buffer, BUF_SIZE, fmt, args);
215 fprintf(stdout, buffer);
217 fprintf(fsxlogf, buffer);
221 prterr(const char *prefix)
223 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
227 static const char *op_names[] = {
229 [OP_WRITE] = "write",
230 [OP_MAPREAD] = "mapread",
231 [OP_MAPWRITE] = "mapwrite",
232 [OP_TRUNCATE] = "truncate",
233 [OP_FALLOCATE] = "fallocate",
234 [OP_PUNCH_HOLE] = "punch_hole",
235 [OP_ZERO_RANGE] = "zero_range",
236 [OP_COLLAPSE_RANGE] = "collapse_range",
237 [OP_INSERT_RANGE] = "insert_range",
240 static const char *op_name(int operation)
242 if (operation >= 0 &&
243 operation < sizeof(op_names) / sizeof(op_names[0]))
244 return op_names[operation];
248 static int op_code(const char *name)
252 for (i = 0; i < sizeof(op_names) / sizeof(op_names[0]); i++)
253 if (op_names[i] && strcmp(name, op_names[i]) == 0)
259 log4(int operation, int arg0, int arg1, enum opflags flags)
261 struct log_entry *le;
264 le->operation = operation;
266 flags |= FL_CLOSE_OPEN;
269 le->args[2] = file_size;
273 if (logptr >= LOGSIZE)
283 struct log_entry *lp;
285 prt("LOG DUMP (%d total operations):\n", logcount);
287 logopsf = fopen(opsfile, "w");
291 if (logcount < LOGSIZE) {
298 for ( ; count > 0; count--) {
302 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
303 prt("%d(%3d mod 256): ", opnum, opnum%256);
306 overlap = badoff >= lp->args[0] &&
307 badoff < lp->args[0] + lp->args[1];
309 if (lp->flags & FL_SKIPPED) {
310 prt("SKIPPED (no operation)");
314 switch (lp->operation) {
316 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
317 lp->args[0], lp->args[0] + lp->args[1] - 1,
323 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
324 lp->args[0], lp->args[0] + lp->args[1] - 1,
330 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
331 lp->args[0], lp->args[0] + lp->args[1] - 1,
337 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
338 lp->args[0], lp->args[0] + lp->args[1] - 1,
340 if (lp->args[0] > lp->args[2])
342 else if (lp->args[0] + lp->args[1] > lp->args[2])
344 overlap = (badoff >= lp->args[0] ||
345 badoff >=lp->args[2]) &&
346 badoff < lp->args[0] + lp->args[1];
351 down = lp->args[1] < lp->args[2];
352 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
353 down ? "DOWN" : "UP", lp->args[2], lp->args[1]);
354 overlap = badoff >= lp->args[1 + !down] &&
355 badoff < lp->args[1 + !!down];
360 /* 0: offset 1: length 2: where alloced */
361 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) ",
362 lp->args[0], lp->args[0] + lp->args[1],
364 if (lp->args[0] + lp->args[1] <= lp->args[2])
366 else if (lp->flags & FL_KEEP_SIZE)
374 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
375 lp->args[0], lp->args[0] + lp->args[1] - 1,
381 prt("ZERO 0x%x thru 0x%x\t(0x%x bytes)",
382 lp->args[0], lp->args[0] + lp->args[1] - 1,
387 case OP_COLLAPSE_RANGE:
388 prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
389 lp->args[0], lp->args[0] + lp->args[1] - 1,
394 case OP_INSERT_RANGE:
395 prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
396 lp->args[0], lp->args[0] + lp->args[1] - 1,
402 prt("BOGUS LOG ENTRY (operation code = %d)!",
408 if (lp->flags & FL_CLOSE_OPEN)
409 prt("\n\t\tCLOSE/OPEN");
416 if (lp->flags & FL_SKIPPED)
417 fprintf(logopsf, "skip ");
418 fprintf(logopsf, "%s 0x%x 0x%x 0x%x",
419 op_name(lp->operation),
420 lp->args[0], lp->args[1], lp->args[2]);
421 if (lp->flags & FL_KEEP_SIZE)
422 fprintf(logopsf, " keep_size");
423 if (lp->flags & FL_CLOSE_OPEN)
424 fprintf(logopsf, " close_open");
426 fprintf(logopsf, " *");
427 fprintf(logopsf, "\n");
432 if (fclose(logopsf) != 0)
435 prt("Log of operations saved to \"%s\"; "
436 "replay with --replay-ops\n",
443 save_buffer(char *buffer, off_t bufferlength, int fd)
446 ssize_t byteswritten;
448 if (fd <= 0 || bufferlength == 0)
451 if (bufferlength > SSIZE_MAX) {
452 prt("fsx flaw: overflow in save_buffer\n");
456 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
457 if (size_by_seek == (off_t)-1)
458 prterr("save_buffer: lseek eof");
459 else if (bufferlength > size_by_seek) {
460 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
461 (unsigned long long)bufferlength);
462 bufferlength = size_by_seek;
466 ret = lseek(fd, (off_t)0, SEEK_SET);
467 if (ret == (off_t)-1)
468 prterr("save_buffer: lseek 0");
470 byteswritten = write(fd, buffer, (size_t)bufferlength);
471 if (byteswritten != bufferlength) {
472 if (byteswritten == -1)
473 prterr("save_buffer write");
475 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
476 (unsigned)byteswritten,
477 (unsigned long long)bufferlength);
483 report_failure(int status)
489 save_buffer(good_buf, file_size, fsxgoodfd);
490 prt("Correct content saved for comparison\n");
491 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
500 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
501 *(((unsigned char *)(cp)) + 1)))
504 check_buffers(unsigned offset, unsigned size)
512 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
513 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
514 offset, size, fname);
515 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
517 c = good_buf[offset];
521 bad = short_at(&temp_buf[i]);
522 prt("0x%05x\t0x%04x\t0x%04x", offset,
523 short_at(&good_buf[offset]), bad);
524 op = temp_buf[offset & 1 ? i+1 : i];
525 prt("\t0x%05x\n", n);
527 prt("operation# (mod 256) for "
528 "the bad data may be %u\n",
529 ((unsigned)op & 0xff));
531 prt("operation# (mod 256) for "
532 "the bad data unknown, check"
533 " HOLE and EXTEND ops\n");
553 if (fstat(fd, &statbuf)) {
554 prterr("check_size: fstat");
555 statbuf.st_size = -1;
557 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
558 if (file_size != statbuf.st_size || file_size != size_by_seek) {
559 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
560 (unsigned long long)file_size,
561 (unsigned long long)statbuf.st_size,
562 (unsigned long long)size_by_seek);
569 check_trunc_hack(void)
573 ftruncate(fd, (off_t)0);
574 ftruncate(fd, (off_t)100000);
576 if (statbuf.st_size != (off_t)100000) {
577 prt("no extend on truncate! not posix!\n");
584 doflush(unsigned offset, unsigned size)
590 if (o_direct == O_DIRECT)
593 pg_offset = offset & mmap_mask;
594 map_size = pg_offset + size;
596 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
597 MAP_FILE | MAP_SHARED, fd,
598 (off_t)(offset - pg_offset))) == (char *)-1) {
599 prterr("doflush: mmap");
602 if (msync(p, map_size, MS_INVALIDATE) != 0) {
603 prterr("doflush: msync");
606 if (munmap(p, map_size) != 0) {
607 prterr("doflush: munmap");
613 doread(unsigned offset, unsigned size)
618 offset -= offset % readbdy;
620 size -= size % readbdy;
622 if (!quiet && testcalls > simulatedopcount && !o_direct)
623 prt("skipping zero size read\n");
624 log4(OP_READ, offset, size, FL_SKIPPED);
627 if (size + offset > file_size) {
628 if (!quiet && testcalls > simulatedopcount)
629 prt("skipping seek/read past end of file\n");
630 log4(OP_READ, offset, size, FL_SKIPPED);
634 log4(OP_READ, offset, size, FL_NONE);
636 if (testcalls <= simulatedopcount)
640 ((progressinterval && testcalls % progressinterval == 0) ||
642 (monitorstart == -1 ||
643 (offset + size > monitorstart &&
644 (monitorend == -1 || offset <= monitorend))))))
645 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
646 offset, offset + size - 1, size);
647 ret = lseek(fd, (off_t)offset, SEEK_SET);
648 if (ret == (off_t)-1) {
649 prterr("doread: lseek");
652 iret = fsxread(fd, temp_buf, size, offset);
655 prterr("doread: read");
657 prt("short read: 0x%x bytes instead of 0x%x\n",
661 check_buffers(offset, size);
666 check_eofpage(char *s, unsigned offset, char *p, int size)
668 unsigned long last_page, should_be_zero;
670 if (offset + size <= (file_size & ~page_mask))
673 * we landed in the last page of the file
674 * test to make sure the VM system provided 0's
675 * beyond the true end of the file mapping
676 * (as required by mmap def in 1996 posix 1003.1)
678 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
680 for (should_be_zero = last_page + (file_size & page_mask);
681 should_be_zero < last_page + page_size;
683 if (*(char *)should_be_zero) {
684 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
685 s, file_size - 1, should_be_zero & page_mask,
686 short_at(should_be_zero));
693 domapread(unsigned offset, unsigned size)
699 offset -= offset % readbdy;
701 if (!quiet && testcalls > simulatedopcount)
702 prt("skipping zero size read\n");
703 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
706 if (size + offset > file_size) {
707 if (!quiet && testcalls > simulatedopcount)
708 prt("skipping seek/read past end of file\n");
709 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
713 log4(OP_MAPREAD, offset, size, FL_NONE);
715 if (testcalls <= simulatedopcount)
719 ((progressinterval && testcalls % progressinterval == 0) ||
721 (monitorstart == -1 ||
722 (offset + size > monitorstart &&
723 (monitorend == -1 || offset <= monitorend))))))
724 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
725 offset, offset + size - 1, size);
727 pg_offset = offset & PAGE_MASK;
728 map_size = pg_offset + size;
730 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
731 (off_t)(offset - pg_offset))) == (char *)-1) {
732 prterr("domapread: mmap");
735 memcpy(temp_buf, p + pg_offset, size);
737 check_eofpage("Read", offset, p, size);
739 if (munmap(p, map_size) != 0) {
740 prterr("domapread: munmap");
744 check_buffers(offset, size);
749 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
752 good_buf[offset] = testcalls % 256;
754 good_buf[offset] += original_buf[offset];
761 dowrite(unsigned offset, unsigned size)
766 offset -= offset % writebdy;
768 size -= size % writebdy;
770 if (!quiet && testcalls > simulatedopcount && !o_direct)
771 prt("skipping zero size write\n");
772 log4(OP_WRITE, offset, size, FL_SKIPPED);
776 log4(OP_WRITE, offset, size, FL_NONE);
778 gendata(original_buf, good_buf, offset, size);
779 if (file_size < offset + size) {
780 if (file_size < offset)
781 memset(good_buf + file_size, '\0', offset - file_size);
782 file_size = offset + size;
784 warn("Lite file size bug in fsx!");
789 if (testcalls <= simulatedopcount)
793 ((progressinterval && testcalls % progressinterval == 0) ||
795 (monitorstart == -1 ||
796 (offset + size > monitorstart &&
797 (monitorend == -1 || offset <= monitorend))))))
798 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
799 offset, offset + size - 1, size);
800 ret = lseek(fd, (off_t)offset, SEEK_SET);
801 if (ret == (off_t)-1) {
802 prterr("dowrite: lseek");
805 iret = fsxwrite(fd, good_buf + offset, size, offset);
808 prterr("dowrite: write");
810 prt("short write: 0x%x bytes instead of 0x%x\n",
816 prt("fsync() failed: %s\n", strerror(errno));
821 doflush(offset, size);
827 domapwrite(unsigned offset, unsigned size)
834 offset -= offset % writebdy;
836 if (!quiet && testcalls > simulatedopcount)
837 prt("skipping zero size write\n");
838 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
841 cur_filesize = file_size;
843 log4(OP_MAPWRITE, offset, size, FL_NONE);
845 gendata(original_buf, good_buf, offset, size);
846 if (file_size < offset + size) {
847 if (file_size < offset)
848 memset(good_buf + file_size, '\0', offset - file_size);
849 file_size = offset + size;
851 warn("Lite file size bug in fsx!");
856 if (testcalls <= simulatedopcount)
860 ((progressinterval && testcalls % progressinterval == 0) ||
862 (monitorstart == -1 ||
863 (offset + size > monitorstart &&
864 (monitorend == -1 || offset <= monitorend))))))
865 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
866 offset, offset + size - 1, size);
868 if (file_size > cur_filesize) {
869 if (ftruncate(fd, file_size) == -1) {
870 prterr("domapwrite: ftruncate");
874 pg_offset = offset & PAGE_MASK;
875 map_size = pg_offset + size;
877 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
878 MAP_FILE | MAP_SHARED, fd,
879 (off_t)(offset - pg_offset))) == (char *)-1) {
880 prterr("domapwrite: mmap");
883 memcpy(p + pg_offset, good_buf + offset, size);
884 if (msync(p, map_size, MS_SYNC) != 0) {
885 prterr("domapwrite: msync");
889 check_eofpage("Write", offset, p, size);
891 if (munmap(p, map_size) != 0) {
892 prterr("domapwrite: munmap");
899 dotruncate(unsigned size)
901 int oldsize = file_size;
903 size -= size % truncbdy;
904 if (size > biggest) {
906 if (!quiet && testcalls > simulatedopcount)
907 prt("truncating to largest ever: 0x%x\n", size);
910 log4(OP_TRUNCATE, 0, size, FL_NONE);
912 if (size > file_size)
913 memset(good_buf + file_size, '\0', size - file_size);
916 if (testcalls <= simulatedopcount)
919 if ((progressinterval && testcalls % progressinterval == 0) ||
920 (debug && (monitorstart == -1 || monitorend == -1 ||
921 size <= monitorend)))
922 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
923 if (ftruncate(fd, (off_t)size) == -1) {
924 prt("ftruncate1: %x\n", size);
925 prterr("dotruncate: ftruncate");
930 #ifdef FALLOC_FL_PUNCH_HOLE
932 do_punch_hole(unsigned offset, unsigned length)
937 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
940 if (!quiet && testcalls > simulatedopcount)
941 prt("skipping zero length punch hole\n");
942 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
946 if (file_size <= (loff_t)offset) {
947 if (!quiet && testcalls > simulatedopcount)
948 prt("skipping hole punch off the end of the file\n");
949 log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
953 end_offset = offset + length;
955 log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
957 if (testcalls <= simulatedopcount)
960 if ((progressinterval && testcalls % progressinterval == 0) ||
961 (debug && (monitorstart == -1 || monitorend == -1 ||
962 end_offset <= monitorend))) {
963 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
964 offset, offset+length, length);
966 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
967 prt("punch hole: 0x%x to 0x%x\n", offset, offset + length);
968 prterr("do_punch_hole: fallocate");
973 max_offset = offset < file_size ? offset : file_size;
974 max_len = max_offset + length <= file_size ? length :
975 file_size - max_offset;
976 memset(good_buf + max_offset, '\0', max_len);
981 do_punch_hole(unsigned offset, unsigned length)
987 #ifdef FALLOC_FL_ZERO_RANGE
989 do_zero_range(unsigned offset, unsigned length, int keep_size)
992 int mode = FALLOC_FL_ZERO_RANGE;
995 if (!quiet && testcalls > simulatedopcount)
996 prt("skipping zero length zero range\n");
997 log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
998 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1002 end_offset = keep_size ? 0 : offset + length;
1004 if (end_offset > biggest) {
1005 biggest = end_offset;
1006 if (!quiet && testcalls > simulatedopcount)
1007 prt("zero_range to largest ever: 0x%x\n", end_offset);
1011 * last arg matches fallocate string array index in logdump:
1012 * 0: allocate past EOF
1013 * 1: extending prealloc
1014 * 2: interior prealloc
1016 log4(OP_ZERO_RANGE, offset, length,
1017 keep_size ? FL_KEEP_SIZE : FL_NONE);
1019 if (testcalls <= simulatedopcount)
1022 if ((progressinterval && testcalls % progressinterval == 0) ||
1023 (debug && (monitorstart == -1 || monitorend == -1 ||
1024 end_offset <= monitorend))) {
1025 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1026 offset, offset+length, length);
1028 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1029 prt("zero range: 0x%x to 0x%x\n", offset, offset + length);
1030 prterr("do_zero_range: fallocate");
1031 report_failure(161);
1034 memset(good_buf + offset, '\0', length);
1039 do_zero_range(unsigned offset, unsigned length, int keep_size)
1045 #ifdef FALLOC_FL_COLLAPSE_RANGE
1047 do_collapse_range(unsigned offset, unsigned length)
1049 unsigned end_offset;
1050 int mode = FALLOC_FL_COLLAPSE_RANGE;
1053 if (!quiet && testcalls > simulatedopcount)
1054 prt("skipping zero length collapse range\n");
1055 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1059 end_offset = offset + length;
1060 if ((loff_t)end_offset >= file_size) {
1061 if (!quiet && testcalls > simulatedopcount)
1062 prt("skipping collapse range behind EOF\n");
1063 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
1067 log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1069 if (testcalls <= simulatedopcount)
1072 if ((progressinterval && testcalls % progressinterval == 0) ||
1073 (debug && (monitorstart == -1 || monitorend == -1 ||
1074 end_offset <= monitorend))) {
1075 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1076 offset, offset+length, length);
1078 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1079 prt("collapse range: 0x%x to 0x%x\n", offset, offset + length);
1080 prterr("do_collapse_range: fallocate");
1081 report_failure(161);
1084 memmove(good_buf + offset, good_buf + end_offset,
1085 file_size - end_offset);
1086 file_size -= length;
1091 do_collapse_range(unsigned offset, unsigned length)
1097 #ifdef FALLOC_FL_INSERT_RANGE
1099 do_insert_range(unsigned offset, unsigned length)
1101 unsigned end_offset;
1102 int mode = FALLOC_FL_INSERT_RANGE;
1105 if (!quiet && testcalls > simulatedopcount)
1106 prt("skipping zero length insert range\n");
1107 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1111 if ((loff_t)offset >= file_size) {
1112 if (!quiet && testcalls > simulatedopcount)
1113 prt("skipping insert range behind EOF\n");
1114 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1118 log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1120 if (testcalls <= simulatedopcount)
1123 end_offset = offset + length;
1124 if ((progressinterval && testcalls % progressinterval == 0) ||
1125 (debug && (monitorstart == -1 || monitorend == -1 ||
1126 end_offset <= monitorend))) {
1127 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1128 offset, offset+length, length);
1130 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1131 prt("insert range: 0x%x to 0x%x\n", offset, offset + length);
1132 prterr("do_insert_range: fallocate");
1133 report_failure(161);
1136 memmove(good_buf + end_offset, good_buf + offset,
1137 file_size - offset);
1138 memset(good_buf + offset, '\0', length);
1139 file_size += length;
1144 do_insert_range(unsigned offset, unsigned length)
1150 #ifdef HAVE_LINUX_FALLOC_H
1151 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1153 do_preallocate(unsigned offset, unsigned length, int keep_size)
1155 unsigned end_offset;
1158 if (!quiet && testcalls > simulatedopcount)
1159 prt("skipping zero length fallocate\n");
1160 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1161 (keep_size ? FL_KEEP_SIZE : FL_NONE));
1165 end_offset = keep_size ? 0 : offset + length;
1167 if (end_offset > biggest) {
1168 biggest = end_offset;
1169 if (!quiet && testcalls > simulatedopcount)
1170 prt("fallocating to largest ever: 0x%x\n", end_offset);
1174 * last arg matches fallocate string array index in logdump:
1175 * 0: allocate past EOF
1176 * 1: extending prealloc
1177 * 2: interior prealloc
1179 log4(OP_FALLOCATE, offset, length,
1180 keep_size ? FL_KEEP_SIZE : FL_NONE);
1182 if (end_offset > file_size) {
1183 memset(good_buf + file_size, '\0', end_offset - file_size);
1184 file_size = end_offset;
1187 if (testcalls <= simulatedopcount)
1190 if ((progressinterval && testcalls % progressinterval == 0) ||
1191 (debug && (monitorstart == -1 || monitorend == -1 ||
1192 end_offset <= monitorend)))
1193 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1194 offset, offset + length, length);
1195 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1196 prt("fallocate: 0x%x to 0x%x\n", offset, offset + length);
1197 prterr("do_preallocate: fallocate");
1198 report_failure(161);
1203 do_preallocate(unsigned offset, unsigned length, int keep_size)
1214 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1215 prterr("writefileimage: lseek");
1216 report_failure(171);
1218 iret = write(fd, good_buf, file_size);
1219 if ((off_t)iret != file_size) {
1221 prterr("writefileimage: write");
1223 prt("short write: 0x%x bytes instead of 0x%llx\n",
1224 iret, (unsigned long long)file_size);
1225 report_failure(172);
1227 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1228 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1229 prterr("writefileimage: ftruncate");
1230 report_failure(173);
1238 if (testcalls <= simulatedopcount)
1242 prt("%lu close/open\n", testcalls);
1244 prterr("docloseopen: close");
1245 report_failure(180);
1247 fd = open(fname, O_RDWR|o_direct, 0);
1249 prterr("docloseopen: open");
1250 report_failure(181);
1255 #define TRIM_OFF(off, size) \
1263 #define TRIM_LEN(off, len, size) \
1265 if ((off) + (len) > (size)) \
1266 (len) = (size) - (off); \
1269 #define TRIM_OFF_LEN(off, len, size) \
1271 TRIM_OFF(off, size); \
1272 TRIM_LEN(off, len, size); \
1279 prt("signal %d\n", sig);
1280 prt("testcalls = %lu\n", testcalls);
1285 read_op(struct log_entry *log_entry)
1289 memset(log_entry, 0, sizeof(*log_entry));
1290 log_entry->operation = -1;
1292 while (log_entry->operation == -1) {
1297 if (!fgets(line, sizeof(line), replayopsf)) {
1298 if (feof(replayopsf)) {
1304 str = strtok(line, " \t\n");
1307 if (strcmp(str, "skip") == 0) {
1308 log_entry->flags |= FL_SKIPPED;
1309 str = strtok(NULL, " \t\n");
1313 log_entry->operation = op_code(str);
1314 if (log_entry->operation == -1)
1316 for (i = 0; i < 3; i++) {
1319 str = strtok(NULL, " \t\n");
1322 log_entry->args[i] = strtoul(str, &end, 0);
1326 while ((str = strtok(NULL, " \t\n"))) {
1327 if (strcmp(str, "keep_size") == 0)
1328 log_entry->flags |= FL_KEEP_SIZE;
1329 else if (strcmp(str, "close_open") == 0)
1330 log_entry->flags |= FL_CLOSE_OPEN;
1331 else if (strcmp(str, "*") == 0)
1332 ; /* overlap marker; ignore */
1340 fprintf(stderr, "%s: parse error\n", replayops);
1343 cleanup(100); /* doesn't return */
1351 unsigned long offset;
1357 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1362 if (debugstart > 0 && testcalls >= debugstart)
1365 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1366 prt("%lu...\n", testcalls);
1369 struct log_entry log_entry;
1371 while (read_op(&log_entry)) {
1372 if (log_entry.flags & FL_SKIPPED) {
1373 log4(log_entry.operation,
1374 log_entry.args[0], log_entry.args[1],
1379 op = log_entry.operation;
1380 offset = log_entry.args[0];
1381 size = log_entry.args[1];
1382 closeopen = !!(log_entry.flags & FL_CLOSE_OPEN);
1383 keep_size = !!(log_entry.flags & FL_KEEP_SIZE);
1391 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1396 size = random() % (maxoplen + 1);
1398 /* calculate appropriate op to run */
1400 op = rv % OP_MAX_LITE;
1402 op = rv % OP_MAX_FULL;
1407 size = random() % maxfilelen;
1410 if (fallocate_calls && size && keep_size_calls)
1411 keep_size = random() % 2;
1414 if (zero_range_calls && size && keep_size_calls)
1415 keep_size = random() % 2;
1431 if (!fallocate_calls) {
1432 log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
1437 if (!punch_hole_calls) {
1438 log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
1443 if (!zero_range_calls) {
1444 log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
1448 case OP_COLLAPSE_RANGE:
1449 if (!collapse_range_calls) {
1450 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1454 case OP_INSERT_RANGE:
1455 if (!insert_range_calls) {
1456 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1464 TRIM_OFF_LEN(offset, size, file_size);
1465 doread(offset, size);
1469 TRIM_OFF_LEN(offset, size, maxfilelen);
1470 dowrite(offset, size);
1474 TRIM_OFF_LEN(offset, size, file_size);
1475 domapread(offset, size);
1479 TRIM_OFF_LEN(offset, size, maxfilelen);
1480 domapwrite(offset, size);
1488 TRIM_OFF_LEN(offset, size, maxfilelen);
1489 do_preallocate(offset, size, keep_size);
1493 TRIM_OFF_LEN(offset, size, file_size);
1494 do_punch_hole(offset, size);
1497 TRIM_OFF_LEN(offset, size, file_size);
1498 do_zero_range(offset, size, keep_size);
1500 case OP_COLLAPSE_RANGE:
1501 TRIM_OFF_LEN(offset, size, file_size - 1);
1502 offset = offset & ~(block_size - 1);
1503 size = size & ~(block_size - 1);
1505 log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1508 do_collapse_range(offset, size);
1510 case OP_INSERT_RANGE:
1511 TRIM_OFF(offset, file_size);
1512 TRIM_LEN(file_size, size, maxfilelen);
1513 offset = offset & ~(block_size - 1);
1514 size = size & ~(block_size - 1);
1516 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1519 if (file_size + size > maxfilelen) {
1520 log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1524 do_insert_range(offset, size);
1527 prterr("test: unknown operation");
1533 if (sizechecks && testcalls > simulatedopcount)
1544 fprintf(stdout, "usage: %s",
1545 "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\
1546 -b opnum: beginning operation number (default 1)\n\
1547 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1548 -d: debug output for all operations\n\
1549 -f flush and invalidate cache after I/O\n\
1550 -l flen: the upper bound on file size (default 262144)\n\
1551 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1552 -n: no verifications of file size\n\
1553 -o oplen: the upper bound on operation size (default 65536)\n\
1554 -p progressinterval: debug output at specified operation interval\n\
1555 -q: quieter operation\n\
1556 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1557 -s style: 1 gives smaller truncates (default 0)\n\
1558 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1559 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1560 -x: preallocate file space before starting, XFS only (default 0)\n\
1561 -y synchronize changes to a file\n"
1564 " -A: Use the AIO system calls\n"
1566 " -D startingop: debug output starting at specified operation\n"
1567 #ifdef HAVE_LINUX_FALLOC_H
1568 " -F: Do not use fallocate (preallocation) calls\n"
1570 #ifdef FALLOC_FL_PUNCH_HOLE
1571 " -H: Do not use punch hole calls\n"
1573 #ifdef FALLOC_FL_ZERO_RANGE
1574 " -z: Do not use zero range calls\n"
1576 #ifdef FALLOC_FL_COLLAPSE_RANGE
1577 " -C: Do not use collapse range calls\n"
1579 #ifdef FALLOC_FL_INSERT_RANGE
1580 " -I: Do not use insert range calls\n"
1582 " -L: fsxLite - no file creations & no file size changes\n\
1583 -N numops: total # operations to do (default infinity)\n\
1584 -O: use oplen (see -o flag) for every op (default random)\n\
1585 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1586 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1587 -W: mapped write operations DISabled\n\
1588 -R: read() system calls only (mapped reads disabled)\n\
1589 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1590 fname: this filename is REQUIRED (no default)\n");
1596 getnum(char *s, char **e)
1601 ret = strtol(s, e, 0);
1631 io_context_t io_ctx;
1637 ret = io_queue_init(QSZ, &io_ctx);
1639 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1647 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1649 struct io_event event;
1650 static struct timespec ts;
1651 struct iocb *iocbs[] = { &iocb };
1656 io_prep_pread(&iocb, fd, buf, len, offset);
1658 io_prep_pwrite(&iocb, fd, buf, len, offset);
1663 ret = io_submit(io_ctx, 1, iocbs);
1665 fprintf(stderr, "errcode=%d\n", ret);
1666 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1671 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1674 fprintf(stderr, "aio_rw: no events available\n");
1676 fprintf(stderr, "errcode=%d\n", -ret);
1677 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1682 if (len != event.res) {
1684 * The b0rked libaio defines event.res as unsigned.
1685 * However the kernel strucuture has it signed,
1686 * and it's used to pass negated error value.
1687 * Till the library is fixed use the temp var.
1689 res = (long)event.res;
1691 fprintf(stderr, "bad io length: %lu instead of %u\n",
1694 fprintf(stderr, "errcode=%ld\n", -res);
1695 fprintf(stderr, "aio_rw: async io failed: %s\n",
1706 * The caller expects error return in traditional libc
1707 * convention, i.e. -1 and the errno set to error.
1713 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1718 ret = __aio_rw(rw, fd, buf, len, offset);
1721 ret = read(fd, buf, len);
1723 ret = write(fd, buf, len);
1730 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1733 __test_fallocate(int mode, const char *mode_str)
1735 #ifdef HAVE_LINUX_FALLOC_H
1738 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1741 "main: filesystem does not support "
1742 "fallocate mode %s, disabling!\n",
1753 static struct option longopts[] = {
1754 {"replay-ops", required_argument, 0, 256},
1759 main(int argc, char **argv)
1763 char goodfile[1024];
1765 struct stat statbuf;
1770 page_size = getpagesize();
1771 page_mask = page_size - 1;
1772 mmap_mask = page_mask;
1775 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1777 while ((ch = getopt_long(argc, argv,
1778 "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ",
1779 longopts, NULL)) != EOF)
1782 simulatedopcount = getnum(optarg, &endp);
1784 fprintf(stdout, "Will begin at operation %ld\n",
1786 if (simulatedopcount == 0)
1788 simulatedopcount -= 1;
1791 closeprob = getnum(optarg, &endp);
1794 "Chance of close/open is 1 in %d\n",
1806 maxfilelen = getnum(optarg, &endp);
1807 if (maxfilelen <= 0)
1811 monitorstart = getnum(optarg, &endp);
1812 if (monitorstart < 0)
1814 if (!endp || *endp++ != ':')
1816 monitorend = getnum(endp, &endp);
1819 if (monitorend == 0)
1820 monitorend = -1; /* aka infinity */
1826 maxoplen = getnum(optarg, &endp);
1831 progressinterval = getnum(optarg, &endp);
1832 if (progressinterval == 0)
1839 readbdy = getnum(optarg, &endp);
1844 style = getnum(optarg, &endp);
1845 if (style < 0 || style > 1)
1849 truncbdy = getnum(optarg, &endp);
1854 writebdy = getnum(optarg, &endp);
1868 debugstart = getnum(optarg, &endp);
1873 fallocate_calls = 0;
1876 keep_size_calls = 0;
1879 punch_hole_calls = 0;
1882 zero_range_calls = 0;
1885 collapse_range_calls = 0;
1888 insert_range_calls = 0;
1894 numops = getnum(optarg, &endp);
1902 strncpy(goodfile, optarg, sizeof(goodfile));
1903 strcat(goodfile, "/");
1904 strncpy(logfile, optarg, sizeof(logfile));
1905 strcat(logfile, "/");
1911 seed = getnum(optarg, &endp);
1913 seed = time(0) % 10000;
1915 fprintf(stdout, "Seed set to %d\n", seed);
1922 fprintf(stdout, "mapped writes DISABLED\n");
1925 o_direct = O_DIRECT;
1927 case 256: /* --replay-ops */
1940 signal(SIGHUP, cleanup);
1941 signal(SIGINT, cleanup);
1942 signal(SIGPIPE, cleanup);
1943 signal(SIGALRM, cleanup);
1944 signal(SIGTERM, cleanup);
1945 signal(SIGXCPU, cleanup);
1946 signal(SIGXFSZ, cleanup);
1947 signal(SIGVTALRM, cleanup);
1948 signal(SIGUSR1, cleanup);
1949 signal(SIGUSR2, cleanup);
1951 initstate(seed, state, 256);
1954 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1959 if (fstat(fd, &statbuf)) {
1960 prterr("check_size: fstat");
1963 block_size = statbuf.st_blksize;
1966 xfs_flock64_t resv = { 0 };
1967 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1968 if (!platform_test_xfs_fd(fd)) {
1970 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1974 resv.l_len = maxfilelen;
1975 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1981 strncat(goodfile, fname, 256);
1982 strcat (goodfile, ".fsxgood");
1983 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1984 if (fsxgoodfd < 0) {
1988 strncat(logfile, fname, 256);
1989 strcat (logfile, ".fsxlog");
1990 fsxlogf = fopen(logfile, "w");
1991 if (fsxlogf == NULL) {
1995 strncat(opsfile, fname, 256);
1996 strcat(opsfile, ".fsxops");
2000 replayopsf = fopen(replayops, "r");
2014 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
2015 if (file_size == (off_t)-1) {
2017 warn("main: lseek eof");
2020 ret = lseek(fd, (off_t)0, SEEK_SET);
2021 if (ret == (off_t)-1) {
2023 warn("main: lseek 0");
2027 original_buf = (char *) malloc(maxfilelen);
2028 for (i = 0; i < maxfilelen; i++)
2029 original_buf[i] = random() % 256;
2030 good_buf = (char *) malloc(maxfilelen + writebdy);
2031 good_buf = round_ptr_up(good_buf, writebdy, 0);
2032 memset(good_buf, '\0', maxfilelen);
2033 temp_buf = (char *) malloc(maxoplen + readbdy);
2034 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
2035 memset(temp_buf, '\0', maxoplen);
2036 if (lite) { /* zero entire existing file */
2039 written = write(fd, good_buf, (size_t)maxfilelen);
2040 if (written != maxfilelen) {
2041 if (written == -1) {
2043 warn("main: error on write");
2045 warn("main: short write, 0x%x bytes instead "
2054 if (fallocate_calls)
2055 fallocate_calls = test_fallocate(0);
2056 if (keep_size_calls)
2057 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
2058 if (punch_hole_calls)
2059 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2060 if (zero_range_calls)
2061 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
2062 if (collapse_range_calls)
2063 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
2064 if (insert_range_calls)
2065 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
2067 while (numops == -1 || numops--)
2075 prt("All %lu operations completed A-OK!\n", testcalls);