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 */
44 * A log entry is an operation and a bunch of arguments.
54 struct log_entry oplog[LOGSIZE]; /* the log */
55 int logptr = 0; /* current position in log */
56 int logcount = 0; /* total ops */
59 * The operation matrix is complex due to conditional execution of different
60 * features. Hence when we come to deciding what operation to run, we need to
61 * be careful in how we select the different operations. The active operations
62 * are mapped to numbers as follows:
74 * When mapped read/writes are disabled, they are simply converted to normal
75 * reads and writes. When fallocate/fpunch calls are disabled, they are
76 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
77 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
78 * operation modifier rather than an operation in itself.
80 * Because of the "lite" version, we also need to have different "maximum
81 * operation" defines to allow the ops to be selected correctly based on the
85 /* common operations */
92 /* !lite operations */
94 #define OP_FALLOCATE 5
95 #define OP_PUNCH_HOLE 6
96 #define OP_ZERO_RANGE 7
97 #define OP_COLLAPSE_RANGE 8
98 #define OP_INSERT_RANGE 9
99 #define OP_MAX_FULL 10
101 /* operation modifiers */
102 #define OP_CLOSEOPEN 100
103 #define OP_SKIPPED 101
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 FILE * fsxlogf = NULL;
174 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
176 unsigned long ret = (unsigned long)ptr;
178 ret = ((ret + align - 1) & ~(align - 1));
184 vwarnc(int code, const char *fmt, va_list ap) {
185 fprintf(stderr, "fsx: ");
187 vfprintf(stderr, fmt, ap);
188 fprintf(stderr, ": ");
190 fprintf(stderr, "%s\n", strerror(code));
194 warn(const char * fmt, ...) {
197 vwarnc(errno, fmt, ap);
201 #define BUF_SIZE 1024
207 char buffer[BUF_SIZE];
210 vsnprintf(buffer, BUF_SIZE, fmt, args);
212 fprintf(stdout, buffer);
214 fprintf(fsxlogf, buffer);
220 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
225 log4(int operation, int arg0, int arg1, int arg2)
227 struct log_entry *le;
230 le->operation = operation;
232 le->operation = ~ le->operation;
238 if (logptr >= LOGSIZE)
247 struct log_entry *lp;
248 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
250 prt("LOG DUMP (%d total operations):\n", logcount);
251 if (logcount < LOGSIZE) {
258 for ( ; count > 0; count--) {
261 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
262 prt("%d(%3d mod 256): ", opnum, opnum%256);
264 if ((closeopen = lp->operation < 0))
265 lp->operation = ~ lp->operation;
267 switch (lp->operation) {
269 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
270 lp->args[0], lp->args[0] + lp->args[1] - 1,
272 if (badoff >= lp->args[0] && badoff <
273 lp->args[0] + lp->args[1])
277 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
278 lp->args[0], lp->args[0] + lp->args[1] - 1,
280 if (badoff >= lp->args[0] && badoff <
281 lp->args[0] + lp->args[1])
285 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
286 lp->args[0], lp->args[0] + lp->args[1] - 1,
288 if (badoff >= lp->args[0] &&
289 badoff < lp->args[0] + lp->args[1])
293 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
294 lp->args[0], lp->args[0] + lp->args[1] - 1,
296 if (lp->args[0] > lp->args[2])
298 else if (lp->args[0] + lp->args[1] > lp->args[2])
300 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
301 badoff < lp->args[0] + lp->args[1])
305 down = lp->args[0] < lp->args[1];
306 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
307 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
308 if (badoff >= lp->args[!down] &&
309 badoff < lp->args[!!down])
313 /* 0: offset 1: length 2: where alloced */
314 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
315 lp->args[0], lp->args[0] + lp->args[1],
316 lp->args[1], falloc_type[lp->args[2]]);
317 if (badoff >= lp->args[0] &&
318 badoff < lp->args[0] + lp->args[1])
322 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
323 lp->args[0], lp->args[0] + lp->args[1] - 1,
325 if (badoff >= lp->args[0] && badoff <
326 lp->args[0] + lp->args[1])
330 prt("ZERO 0x%x thru 0x%x\t(0x%x bytes)",
331 lp->args[0], lp->args[0] + lp->args[1] - 1,
333 if (badoff >= lp->args[0] && badoff <
334 lp->args[0] + lp->args[1])
337 case OP_COLLAPSE_RANGE:
338 prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
339 lp->args[0], lp->args[0] + lp->args[1] - 1,
341 if (badoff >= lp->args[0] && badoff <
342 lp->args[0] + lp->args[1])
345 case OP_INSERT_RANGE:
346 prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
347 lp->args[0], lp->args[0] + lp->args[1] - 1,
349 if (badoff >= lp->args[0] && badoff <
350 lp->args[0] + lp->args[1])
354 prt("SKIPPED (no operation)");
357 prt("BOGUS LOG ENTRY (operation code = %d)!",
361 prt("\n\t\tCLOSE/OPEN");
371 save_buffer(char *buffer, off_t bufferlength, int fd)
374 ssize_t byteswritten;
376 if (fd <= 0 || bufferlength == 0)
379 if (bufferlength > SSIZE_MAX) {
380 prt("fsx flaw: overflow in save_buffer\n");
384 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
385 if (size_by_seek == (off_t)-1)
386 prterr("save_buffer: lseek eof");
387 else if (bufferlength > size_by_seek) {
388 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
389 (unsigned long long)bufferlength);
390 bufferlength = size_by_seek;
394 ret = lseek(fd, (off_t)0, SEEK_SET);
395 if (ret == (off_t)-1)
396 prterr("save_buffer: lseek 0");
398 byteswritten = write(fd, buffer, (size_t)bufferlength);
399 if (byteswritten != bufferlength) {
400 if (byteswritten == -1)
401 prterr("save_buffer write");
403 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
404 (unsigned)byteswritten,
405 (unsigned long long)bufferlength);
411 report_failure(int status)
417 save_buffer(good_buf, file_size, fsxgoodfd);
418 prt("Correct content saved for comparison\n");
419 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
428 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
429 *(((unsigned char *)(cp)) + 1)))
432 check_buffers(unsigned offset, unsigned size)
440 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
441 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
442 offset, size, fname);
443 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
445 c = good_buf[offset];
449 bad = short_at(&temp_buf[i]);
450 prt("0x%5x\t0x%04x\t0x%04x", offset,
451 short_at(&good_buf[offset]), bad);
452 op = temp_buf[offset & 1 ? i+1 : i];
455 prt("operation# (mod 256) for "
456 "the bad data may be %u\n",
457 ((unsigned)op & 0xff));
459 prt("operation# (mod 256) for "
460 "the bad data unknown, check"
461 " HOLE and EXTEND ops\n");
481 if (fstat(fd, &statbuf)) {
482 prterr("check_size: fstat");
483 statbuf.st_size = -1;
485 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
486 if (file_size != statbuf.st_size || file_size != size_by_seek) {
487 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
488 (unsigned long long)file_size,
489 (unsigned long long)statbuf.st_size,
490 (unsigned long long)size_by_seek);
497 check_trunc_hack(void)
501 ftruncate(fd, (off_t)0);
502 ftruncate(fd, (off_t)100000);
504 if (statbuf.st_size != (off_t)100000) {
505 prt("no extend on truncate! not posix!\n");
512 doflush(unsigned offset, unsigned size)
518 if (o_direct == O_DIRECT)
521 pg_offset = offset & mmap_mask;
522 map_size = pg_offset + size;
524 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
525 MAP_FILE | MAP_SHARED, fd,
526 (off_t)(offset - pg_offset))) == (char *)-1) {
527 prterr("doflush: mmap");
530 if (msync(p, map_size, MS_INVALIDATE) != 0) {
531 prterr("doflush: msync");
534 if (munmap(p, map_size) != 0) {
535 prterr("doflush: munmap");
541 doread(unsigned offset, unsigned size)
546 offset -= offset % readbdy;
548 size -= size % readbdy;
550 if (!quiet && testcalls > simulatedopcount && !o_direct)
551 prt("skipping zero size read\n");
552 log4(OP_SKIPPED, OP_READ, offset, size);
555 if (size + offset > file_size) {
556 if (!quiet && testcalls > simulatedopcount)
557 prt("skipping seek/read past end of file\n");
558 log4(OP_SKIPPED, OP_READ, offset, size);
562 log4(OP_READ, offset, size, 0);
564 if (testcalls <= simulatedopcount)
568 ((progressinterval && testcalls % progressinterval == 0) ||
570 (monitorstart == -1 ||
571 (offset + size > monitorstart &&
572 (monitorend == -1 || offset <= monitorend))))))
573 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
574 offset, offset + size - 1, size);
575 ret = lseek(fd, (off_t)offset, SEEK_SET);
576 if (ret == (off_t)-1) {
577 prterr("doread: lseek");
580 iret = fsxread(fd, temp_buf, size, offset);
583 prterr("doread: read");
585 prt("short read: 0x%x bytes instead of 0x%x\n",
589 check_buffers(offset, size);
594 check_eofpage(char *s, unsigned offset, char *p, int size)
596 unsigned long last_page, should_be_zero;
598 if (offset + size <= (file_size & ~page_mask))
601 * we landed in the last page of the file
602 * test to make sure the VM system provided 0's
603 * beyond the true end of the file mapping
604 * (as required by mmap def in 1996 posix 1003.1)
606 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
608 for (should_be_zero = last_page + (file_size & page_mask);
609 should_be_zero < last_page + page_size;
611 if (*(char *)should_be_zero) {
612 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
613 s, file_size - 1, should_be_zero & page_mask,
614 short_at(should_be_zero));
621 domapread(unsigned offset, unsigned size)
627 offset -= offset % readbdy;
629 if (!quiet && testcalls > simulatedopcount)
630 prt("skipping zero size read\n");
631 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
634 if (size + offset > file_size) {
635 if (!quiet && testcalls > simulatedopcount)
636 prt("skipping seek/read past end of file\n");
637 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
641 log4(OP_MAPREAD, offset, size, 0);
643 if (testcalls <= simulatedopcount)
647 ((progressinterval && testcalls % progressinterval == 0) ||
649 (monitorstart == -1 ||
650 (offset + size > monitorstart &&
651 (monitorend == -1 || offset <= monitorend))))))
652 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
653 offset, offset + size - 1, size);
655 pg_offset = offset & PAGE_MASK;
656 map_size = pg_offset + size;
658 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
659 (off_t)(offset - pg_offset))) == (char *)-1) {
660 prterr("domapread: mmap");
663 memcpy(temp_buf, p + pg_offset, size);
665 check_eofpage("Read", offset, p, size);
667 if (munmap(p, map_size) != 0) {
668 prterr("domapread: munmap");
672 check_buffers(offset, size);
677 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
680 good_buf[offset] = testcalls % 256;
682 good_buf[offset] += original_buf[offset];
689 dowrite(unsigned offset, unsigned size)
694 offset -= offset % writebdy;
696 size -= size % writebdy;
698 if (!quiet && testcalls > simulatedopcount && !o_direct)
699 prt("skipping zero size write\n");
700 log4(OP_SKIPPED, OP_WRITE, offset, size);
704 log4(OP_WRITE, offset, size, file_size);
706 gendata(original_buf, good_buf, offset, size);
707 if (file_size < offset + size) {
708 if (file_size < offset)
709 memset(good_buf + file_size, '\0', offset - file_size);
710 file_size = offset + size;
712 warn("Lite file size bug in fsx!");
717 if (testcalls <= simulatedopcount)
721 ((progressinterval && testcalls % progressinterval == 0) ||
723 (monitorstart == -1 ||
724 (offset + size > monitorstart &&
725 (monitorend == -1 || offset <= monitorend))))))
726 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
727 offset, offset + size - 1, size);
728 ret = lseek(fd, (off_t)offset, SEEK_SET);
729 if (ret == (off_t)-1) {
730 prterr("dowrite: lseek");
733 iret = fsxwrite(fd, good_buf + offset, size, offset);
736 prterr("dowrite: write");
738 prt("short write: 0x%x bytes instead of 0x%x\n",
744 prt("fsync() failed: %s\n", strerror(errno));
749 doflush(offset, size);
755 domapwrite(unsigned offset, unsigned size)
762 offset -= offset % writebdy;
764 if (!quiet && testcalls > simulatedopcount)
765 prt("skipping zero size write\n");
766 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
769 cur_filesize = file_size;
771 log4(OP_MAPWRITE, offset, size, 0);
773 gendata(original_buf, good_buf, offset, size);
774 if (file_size < offset + size) {
775 if (file_size < offset)
776 memset(good_buf + file_size, '\0', offset - file_size);
777 file_size = offset + size;
779 warn("Lite file size bug in fsx!");
784 if (testcalls <= simulatedopcount)
788 ((progressinterval && testcalls % progressinterval == 0) ||
790 (monitorstart == -1 ||
791 (offset + size > monitorstart &&
792 (monitorend == -1 || offset <= monitorend))))))
793 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
794 offset, offset + size - 1, size);
796 if (file_size > cur_filesize) {
797 if (ftruncate(fd, file_size) == -1) {
798 prterr("domapwrite: ftruncate");
802 pg_offset = offset & PAGE_MASK;
803 map_size = pg_offset + size;
805 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
806 MAP_FILE | MAP_SHARED, fd,
807 (off_t)(offset - pg_offset))) == (char *)-1) {
808 prterr("domapwrite: mmap");
811 memcpy(p + pg_offset, good_buf + offset, size);
812 if (msync(p, map_size, MS_SYNC) != 0) {
813 prterr("domapwrite: msync");
817 check_eofpage("Write", offset, p, size);
819 if (munmap(p, map_size) != 0) {
820 prterr("domapwrite: munmap");
827 dotruncate(unsigned size)
829 int oldsize = file_size;
831 size -= size % truncbdy;
832 if (size > biggest) {
834 if (!quiet && testcalls > simulatedopcount)
835 prt("truncating to largest ever: 0x%x\n", size);
838 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
840 if (size > file_size)
841 memset(good_buf + file_size, '\0', size - file_size);
844 if (testcalls <= simulatedopcount)
847 if ((progressinterval && testcalls % progressinterval == 0) ||
848 (debug && (monitorstart == -1 || monitorend == -1 ||
849 size <= monitorend)))
850 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
851 if (ftruncate(fd, (off_t)size) == -1) {
852 prt("ftruncate1: %x\n", size);
853 prterr("dotruncate: ftruncate");
858 #ifdef FALLOC_FL_PUNCH_HOLE
860 do_punch_hole(unsigned offset, unsigned length)
865 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
868 if (!quiet && testcalls > simulatedopcount)
869 prt("skipping zero length punch hole\n");
870 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
874 if (file_size <= (loff_t)offset) {
875 if (!quiet && testcalls > simulatedopcount)
876 prt("skipping hole punch off the end of the file\n");
877 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
881 end_offset = offset + length;
883 log4(OP_PUNCH_HOLE, offset, length, 0);
885 if (testcalls <= simulatedopcount)
888 if ((progressinterval && testcalls % progressinterval == 0) ||
889 (debug && (monitorstart == -1 || monitorend == -1 ||
890 end_offset <= monitorend))) {
891 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
892 offset, offset+length, length);
894 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
895 prt("%punch hole: %x to %x\n", offset, length);
896 prterr("do_punch_hole: fallocate");
901 max_offset = offset < file_size ? offset : file_size;
902 max_len = max_offset + length <= file_size ? length :
903 file_size - max_offset;
904 memset(good_buf + max_offset, '\0', max_len);
909 do_punch_hole(unsigned offset, unsigned length)
915 #ifdef FALLOC_FL_ZERO_RANGE
917 do_zero_range(unsigned offset, unsigned length)
920 int mode = FALLOC_FL_ZERO_RANGE;
924 if (!quiet && testcalls > simulatedopcount)
925 prt("skipping zero length zero range\n");
926 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
931 keep_size = random() % 2;
933 end_offset = keep_size ? 0 : offset + length;
935 if (end_offset > biggest) {
936 biggest = end_offset;
937 if (!quiet && testcalls > simulatedopcount)
938 prt("zero_range to largest ever: 0x%x\n", end_offset);
942 * last arg matches fallocate string array index in logdump:
943 * 0: allocate past EOF
944 * 1: extending prealloc
945 * 2: interior prealloc
947 log4(OP_ZERO_RANGE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
949 if (testcalls <= simulatedopcount)
952 if ((progressinterval && testcalls % progressinterval == 0) ||
953 (debug && (monitorstart == -1 || monitorend == -1 ||
954 end_offset <= monitorend))) {
955 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
956 offset, offset+length, length);
958 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
959 prt("%pzero range: %x to %x\n", offset, length);
960 prterr("do_zero_range: fallocate");
964 memset(good_buf + offset, '\0', length);
969 do_zero_range(unsigned offset, unsigned length)
975 #ifdef FALLOC_FL_COLLAPSE_RANGE
977 do_collapse_range(unsigned offset, unsigned length)
980 int mode = FALLOC_FL_COLLAPSE_RANGE;
983 if (!quiet && testcalls > simulatedopcount)
984 prt("skipping zero length collapse range\n");
985 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
989 end_offset = offset + length;
990 if ((loff_t)end_offset >= file_size) {
991 if (!quiet && testcalls > simulatedopcount)
992 prt("skipping collapse range behind EOF\n");
993 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
997 log4(OP_COLLAPSE_RANGE, offset, length, 0);
999 if (testcalls <= simulatedopcount)
1002 if ((progressinterval && testcalls % progressinterval == 0) ||
1003 (debug && (monitorstart == -1 || monitorend == -1 ||
1004 end_offset <= monitorend))) {
1005 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1006 offset, offset+length, length);
1008 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1009 prt("collapse range: %x to %x\n", offset, length);
1010 prterr("do_collapse_range: fallocate");
1011 report_failure(161);
1014 memmove(good_buf + offset, good_buf + end_offset,
1015 file_size - end_offset);
1016 file_size -= length;
1021 do_collapse_range(unsigned offset, unsigned length)
1027 #ifdef FALLOC_FL_INSERT_RANGE
1029 do_insert_range(unsigned offset, unsigned length)
1031 unsigned end_offset;
1032 int mode = FALLOC_FL_INSERT_RANGE;
1035 if (!quiet && testcalls > simulatedopcount)
1036 prt("skipping zero length insert range\n");
1037 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, length);
1041 if ((loff_t)offset >= file_size) {
1042 if (!quiet && testcalls > simulatedopcount)
1043 prt("skipping insert range behind EOF\n");
1044 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, length);
1048 log4(OP_INSERT_RANGE, offset, length, 0);
1050 if (testcalls <= simulatedopcount)
1053 end_offset = offset + length;
1054 if ((progressinterval && testcalls % progressinterval == 0) ||
1055 (debug && (monitorstart == -1 || monitorend == -1 ||
1056 end_offset <= monitorend))) {
1057 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1058 offset, offset+length, length);
1060 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1061 prt("insert range: %x to %x\n", offset, length);
1062 prterr("do_insert_range: fallocate");
1063 report_failure(161);
1066 memmove(good_buf + end_offset, good_buf + offset,
1067 file_size - offset);
1068 memset(good_buf + offset, '\0', length);
1069 file_size += length;
1074 do_insert_range(unsigned offset, unsigned length)
1080 #ifdef HAVE_LINUX_FALLOC_H
1081 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1083 do_preallocate(unsigned offset, unsigned length)
1085 unsigned end_offset;
1089 if (!quiet && testcalls > simulatedopcount)
1090 prt("skipping zero length fallocate\n");
1091 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1095 if (keep_size_calls)
1096 keep_size = random() % 2;
1098 end_offset = keep_size ? 0 : offset + length;
1100 if (end_offset > biggest) {
1101 biggest = end_offset;
1102 if (!quiet && testcalls > simulatedopcount)
1103 prt("fallocating to largest ever: 0x%x\n", end_offset);
1107 * last arg matches fallocate string array index in logdump:
1108 * 0: allocate past EOF
1109 * 1: extending prealloc
1110 * 2: interior prealloc
1112 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1114 if (end_offset > file_size) {
1115 memset(good_buf + file_size, '\0', end_offset - file_size);
1116 file_size = end_offset;
1119 if (testcalls <= simulatedopcount)
1122 if ((progressinterval && testcalls % progressinterval == 0) ||
1123 (debug && (monitorstart == -1 || monitorend == -1 ||
1124 end_offset <= monitorend)))
1125 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1126 offset, offset + length, length);
1127 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1128 prt("fallocate: %x to %x\n", offset, length);
1129 prterr("do_preallocate: fallocate");
1130 report_failure(161);
1135 do_preallocate(unsigned offset, unsigned length)
1146 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1147 prterr("writefileimage: lseek");
1148 report_failure(171);
1150 iret = write(fd, good_buf, file_size);
1151 if ((off_t)iret != file_size) {
1153 prterr("writefileimage: write");
1155 prt("short write: 0x%x bytes instead of 0x%llx\n",
1156 iret, (unsigned long long)file_size);
1157 report_failure(172);
1159 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1160 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1161 prterr("writefileimage: ftruncate");
1162 report_failure(173);
1170 if (testcalls <= simulatedopcount)
1174 prt("%lu close/open\n", testcalls);
1176 prterr("docloseopen: close");
1177 report_failure(180);
1179 fd = open(fname, O_RDWR|o_direct, 0);
1181 prterr("docloseopen: open");
1182 report_failure(181);
1187 #define TRIM_OFF(off, size) \
1195 #define TRIM_LEN(off, len, size) \
1197 if ((off) + (len) > (size)) \
1198 (len) = (size) - (off); \
1201 #define TRIM_OFF_LEN(off, len, size) \
1203 TRIM_OFF(off, size); \
1204 TRIM_LEN(off, len, size); \
1210 unsigned long offset;
1211 unsigned long size = maxoplen;
1212 unsigned long rv = random();
1215 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1221 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1223 if (debugstart > 0 && testcalls >= debugstart)
1226 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1227 prt("%lu...\n", testcalls);
1231 size = random() % (maxoplen + 1);
1233 /* calculate appropriate op to run */
1235 op = rv % OP_MAX_LITE;
1237 op = rv % OP_MAX_FULL;
1249 if (!fallocate_calls) {
1250 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1255 if (!punch_hole_calls) {
1256 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1261 if (!zero_range_calls) {
1262 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1266 case OP_COLLAPSE_RANGE:
1267 if (!collapse_range_calls) {
1268 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1272 case OP_INSERT_RANGE:
1273 if (!insert_range_calls) {
1274 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1282 TRIM_OFF_LEN(offset, size, file_size);
1283 doread(offset, size);
1287 TRIM_OFF_LEN(offset, size, maxfilelen);
1288 dowrite(offset, size);
1292 TRIM_OFF_LEN(offset, size, file_size);
1293 domapread(offset, size);
1297 TRIM_OFF_LEN(offset, size, maxfilelen);
1298 domapwrite(offset, size);
1303 size = random() % maxfilelen;
1308 TRIM_OFF_LEN(offset, size, maxfilelen);
1309 do_preallocate(offset, size);
1313 TRIM_OFF_LEN(offset, size, file_size);
1314 do_punch_hole(offset, size);
1317 TRIM_OFF_LEN(offset, size, file_size);
1318 do_zero_range(offset, size);
1320 case OP_COLLAPSE_RANGE:
1321 TRIM_OFF_LEN(offset, size, file_size - 1);
1322 offset = offset & ~(block_size - 1);
1323 size = size & ~(block_size - 1);
1325 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1328 do_collapse_range(offset, size);
1330 case OP_INSERT_RANGE:
1331 TRIM_OFF(offset, file_size);
1332 TRIM_LEN(file_size, size, maxfilelen);
1333 offset = offset & ~(block_size - 1);
1334 size = size & ~(block_size - 1);
1336 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1339 if (file_size + size > maxfilelen) {
1340 log4(OP_SKIPPED, OP_INSERT_RANGE, offset, size);
1344 do_insert_range(offset, size);
1347 prterr("test: unknown operation");
1353 if (sizechecks && testcalls > simulatedopcount)
1365 prt("signal %d\n", sig);
1366 prt("testcalls = %lu\n", testcalls);
1374 fprintf(stdout, "usage: %s",
1375 "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\
1376 -b opnum: beginning operation number (default 1)\n\
1377 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1378 -d: debug output for all operations\n\
1379 -f flush and invalidate cache after I/O\n\
1380 -l flen: the upper bound on file size (default 262144)\n\
1381 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1382 -n: no verifications of file size\n\
1383 -o oplen: the upper bound on operation size (default 65536)\n\
1384 -p progressinterval: debug output at specified operation interval\n\
1385 -q: quieter operation\n\
1386 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1387 -s style: 1 gives smaller truncates (default 0)\n\
1388 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1389 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1390 -x: preallocate file space before starting, XFS only (default 0)\n\
1391 -y synchronize changes to a file\n"
1394 " -A: Use the AIO system calls\n"
1396 " -D startingop: debug output starting at specified operation\n"
1397 #ifdef HAVE_LINUX_FALLOC_H
1398 " -F: Do not use fallocate (preallocation) calls\n"
1400 #ifdef FALLOC_FL_PUNCH_HOLE
1401 " -H: Do not use punch hole calls\n"
1403 #ifdef FALLOC_FL_ZERO_RANGE
1404 " -z: Do not use zero range calls\n"
1406 #ifdef FALLOC_FL_COLLAPSE_RANGE
1407 " -C: Do not use collapse range calls\n"
1409 #ifdef FALLOC_FL_INSERT_RANGE
1410 " -I: Do not use insert range calls\n"
1412 " -L: fsxLite - no file creations & no file size changes\n\
1413 -N numops: total # operations to do (default infinity)\n\
1414 -O: use oplen (see -o flag) for every op (default random)\n\
1415 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1416 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1417 -W: mapped write operations DISabled\n\
1418 -R: read() system calls only (mapped reads disabled)\n\
1419 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1420 fname: this filename is REQUIRED (no default)\n");
1426 getnum(char *s, char **e)
1431 ret = strtol(s, e, 0);
1461 io_context_t io_ctx;
1467 ret = io_queue_init(QSZ, &io_ctx);
1469 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1477 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1479 struct io_event event;
1480 static struct timespec ts;
1481 struct iocb *iocbs[] = { &iocb };
1486 io_prep_pread(&iocb, fd, buf, len, offset);
1488 io_prep_pwrite(&iocb, fd, buf, len, offset);
1493 ret = io_submit(io_ctx, 1, iocbs);
1495 fprintf(stderr, "errcode=%d\n", ret);
1496 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1501 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1504 fprintf(stderr, "aio_rw: no events available\n");
1506 fprintf(stderr, "errcode=%d\n", -ret);
1507 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1512 if (len != event.res) {
1514 * The b0rked libaio defines event.res as unsigned.
1515 * However the kernel strucuture has it signed,
1516 * and it's used to pass negated error value.
1517 * Till the library is fixed use the temp var.
1519 res = (long)event.res;
1521 fprintf(stderr, "bad io length: %lu instead of %u\n",
1524 fprintf(stderr, "errcode=%ld\n", -res);
1525 fprintf(stderr, "aio_rw: async io failed: %s\n",
1536 * The caller expects error return in traditional libc
1537 * convention, i.e. -1 and the errno set to error.
1543 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1548 ret = __aio_rw(rw, fd, buf, len, offset);
1551 ret = read(fd, buf, len);
1553 ret = write(fd, buf, len);
1560 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1563 __test_fallocate(int mode, const char *mode_str)
1565 #ifdef HAVE_LINUX_FALLOC_H
1568 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1571 "main: filesystem does not support "
1572 "fallocate mode %s, disabling!\n",
1584 main(int argc, char **argv)
1588 char goodfile[1024];
1590 struct stat statbuf;
1595 page_size = getpagesize();
1596 page_mask = page_size - 1;
1597 mmap_mask = page_mask;
1600 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1602 while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ"))
1606 simulatedopcount = getnum(optarg, &endp);
1608 fprintf(stdout, "Will begin at operation %ld\n",
1610 if (simulatedopcount == 0)
1612 simulatedopcount -= 1;
1615 closeprob = getnum(optarg, &endp);
1618 "Chance of close/open is 1 in %d\n",
1630 maxfilelen = getnum(optarg, &endp);
1631 if (maxfilelen <= 0)
1635 monitorstart = getnum(optarg, &endp);
1636 if (monitorstart < 0)
1638 if (!endp || *endp++ != ':')
1640 monitorend = getnum(endp, &endp);
1643 if (monitorend == 0)
1644 monitorend = -1; /* aka infinity */
1650 maxoplen = getnum(optarg, &endp);
1655 progressinterval = getnum(optarg, &endp);
1656 if (progressinterval == 0)
1663 readbdy = getnum(optarg, &endp);
1668 style = getnum(optarg, &endp);
1669 if (style < 0 || style > 1)
1673 truncbdy = getnum(optarg, &endp);
1678 writebdy = getnum(optarg, &endp);
1692 debugstart = getnum(optarg, &endp);
1697 fallocate_calls = 0;
1700 keep_size_calls = 0;
1703 punch_hole_calls = 0;
1706 zero_range_calls = 0;
1709 collapse_range_calls = 0;
1712 insert_range_calls = 0;
1718 numops = getnum(optarg, &endp);
1726 strncpy(goodfile, optarg, sizeof(goodfile));
1727 strcat(goodfile, "/");
1728 strncpy(logfile, optarg, sizeof(logfile));
1729 strcat(logfile, "/");
1735 seed = getnum(optarg, &endp);
1737 seed = time(0) % 10000;
1739 fprintf(stdout, "Seed set to %d\n", seed);
1746 fprintf(stdout, "mapped writes DISABLED\n");
1749 o_direct = O_DIRECT;
1761 signal(SIGHUP, cleanup);
1762 signal(SIGINT, cleanup);
1763 signal(SIGPIPE, cleanup);
1764 signal(SIGALRM, cleanup);
1765 signal(SIGTERM, cleanup);
1766 signal(SIGXCPU, cleanup);
1767 signal(SIGXFSZ, cleanup);
1768 signal(SIGVTALRM, cleanup);
1769 signal(SIGUSR1, cleanup);
1770 signal(SIGUSR2, cleanup);
1772 initstate(seed, state, 256);
1775 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1780 if (fstat(fd, &statbuf)) {
1781 prterr("check_size: fstat");
1784 block_size = statbuf.st_blksize;
1787 xfs_flock64_t resv = { 0 };
1788 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1789 if (!platform_test_xfs_fd(fd)) {
1791 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1795 resv.l_len = maxfilelen;
1796 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1802 strncat(goodfile, fname, 256);
1803 strcat (goodfile, ".fsxgood");
1804 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1805 if (fsxgoodfd < 0) {
1809 strncat(logfile, fname, 256);
1810 strcat (logfile, ".fsxlog");
1811 fsxlogf = fopen(logfile, "w");
1812 if (fsxlogf == NULL) {
1824 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1825 if (file_size == (off_t)-1) {
1827 warn("main: lseek eof");
1830 ret = lseek(fd, (off_t)0, SEEK_SET);
1831 if (ret == (off_t)-1) {
1833 warn("main: lseek 0");
1837 original_buf = (char *) malloc(maxfilelen);
1838 for (i = 0; i < maxfilelen; i++)
1839 original_buf[i] = random() % 256;
1840 good_buf = (char *) malloc(maxfilelen + writebdy);
1841 good_buf = round_ptr_up(good_buf, writebdy, 0);
1842 memset(good_buf, '\0', maxfilelen);
1843 temp_buf = (char *) malloc(maxoplen + readbdy);
1844 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1845 memset(temp_buf, '\0', maxoplen);
1846 if (lite) { /* zero entire existing file */
1849 written = write(fd, good_buf, (size_t)maxfilelen);
1850 if (written != maxfilelen) {
1851 if (written == -1) {
1853 warn("main: error on write");
1855 warn("main: short write, 0x%x bytes instead "
1864 if (fallocate_calls)
1865 fallocate_calls = test_fallocate(0);
1866 if (keep_size_calls)
1867 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
1868 if (punch_hole_calls)
1869 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1870 if (zero_range_calls)
1871 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1872 if (collapse_range_calls)
1873 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
1874 if (insert_range_calls)
1875 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
1877 while (numops == -1 || numops--)
1884 prt("All operations completed A-OK!\n");