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
100 /* operation modifiers */
101 #define OP_CLOSEOPEN 100
102 #define OP_SKIPPED 101
105 #define PAGE_SIZE getpagesize()
107 #define PAGE_MASK (PAGE_SIZE - 1)
109 char *original_buf; /* a pointer to the original data */
110 char *good_buf; /* a pointer to the correct data */
111 char *temp_buf; /* a pointer to the current data */
112 char *fname; /* name of our test file */
113 int fd; /* fd for our test file */
115 blksize_t block_size = 0;
119 unsigned long testcalls = 0; /* calls to function "test" */
121 unsigned long simulatedopcount = 0; /* -b flag */
122 int closeprob = 0; /* -c flag */
123 int debug = 0; /* -d flag */
124 unsigned long debugstart = 0; /* -D flag */
125 int flush = 0; /* -f flag */
126 int do_fsync = 0; /* -y flag */
127 unsigned long maxfilelen = 256 * 1024; /* -l flag */
128 int sizechecks = 1; /* -n flag disables them */
129 int maxoplen = 64 * 1024; /* -o flag */
130 int quiet = 0; /* -q flag */
131 unsigned long progressinterval = 0; /* -p flag */
132 int readbdy = 1; /* -r flag */
133 int style = 0; /* -s flag */
134 int prealloc = 0; /* -x flag */
135 int truncbdy = 1; /* -t flag */
136 int writebdy = 1; /* -w flag */
137 long monitorstart = -1; /* -m flag */
138 long monitorend = -1; /* -m flag */
139 int lite = 0; /* -L flag */
140 long numops = -1; /* -N flag */
141 int randomoplen = 1; /* -O flag disables it */
142 int seed = 1; /* -S flag */
143 int mapped_writes = 1; /* -W flag disables */
144 int fallocate_calls = 1; /* -F flag disables */
145 int keep_size_calls = 1; /* -K flag disables */
146 int punch_hole_calls = 1; /* -H flag disables */
147 int zero_range_calls = 1; /* -z flag disables */
148 int collapse_range_calls = 1; /* -C flag disables */
149 int mapped_reads = 1; /* -R flag disables it */
151 int o_direct; /* -Z */
158 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
161 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
162 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
164 #define fsxread(a,b,c,d) read(a,b,c)
165 #define fsxwrite(a,b,c,d) write(a,b,c)
168 FILE * fsxlogf = NULL;
172 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
174 unsigned long ret = (unsigned long)ptr;
176 ret = ((ret + align - 1) & ~(align - 1));
182 vwarnc(int code, const char *fmt, va_list ap) {
183 fprintf(stderr, "fsx: ");
185 vfprintf(stderr, fmt, ap);
186 fprintf(stderr, ": ");
188 fprintf(stderr, "%s\n", strerror(code));
192 warn(const char * fmt, ...) {
195 vwarnc(errno, fmt, ap);
199 #define BUF_SIZE 1024
205 char buffer[BUF_SIZE];
208 vsnprintf(buffer, BUF_SIZE, fmt, args);
210 fprintf(stdout, buffer);
212 fprintf(fsxlogf, buffer);
218 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
223 log4(int operation, int arg0, int arg1, int arg2)
225 struct log_entry *le;
228 le->operation = operation;
230 le->operation = ~ le->operation;
236 if (logptr >= LOGSIZE)
245 struct log_entry *lp;
246 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
248 prt("LOG DUMP (%d total operations):\n", logcount);
249 if (logcount < LOGSIZE) {
256 for ( ; count > 0; count--) {
259 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
260 prt("%d(%3d mod 256): ", opnum, opnum%256);
262 if ((closeopen = lp->operation < 0))
263 lp->operation = ~ lp->operation;
265 switch (lp->operation) {
267 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
268 lp->args[0], lp->args[0] + lp->args[1] - 1,
270 if (badoff >= lp->args[0] && badoff <
271 lp->args[0] + lp->args[1])
275 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
276 lp->args[0], lp->args[0] + lp->args[1] - 1,
278 if (badoff >= lp->args[0] && badoff <
279 lp->args[0] + lp->args[1])
283 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
284 lp->args[0], lp->args[0] + lp->args[1] - 1,
286 if (badoff >= lp->args[0] &&
287 badoff < lp->args[0] + lp->args[1])
291 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
292 lp->args[0], lp->args[0] + lp->args[1] - 1,
294 if (lp->args[0] > lp->args[2])
296 else if (lp->args[0] + lp->args[1] > lp->args[2])
298 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
299 badoff < lp->args[0] + lp->args[1])
303 down = lp->args[0] < lp->args[1];
304 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
305 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
306 if (badoff >= lp->args[!down] &&
307 badoff < lp->args[!!down])
311 /* 0: offset 1: length 2: where alloced */
312 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
313 lp->args[0], lp->args[0] + lp->args[1],
314 lp->args[1], falloc_type[lp->args[2]]);
315 if (badoff >= lp->args[0] &&
316 badoff < lp->args[0] + lp->args[1])
320 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
321 lp->args[0], lp->args[0] + lp->args[1] - 1,
323 if (badoff >= lp->args[0] && badoff <
324 lp->args[0] + lp->args[1])
328 prt("ZERO 0x%x thru 0x%x\t(0x%x bytes)",
329 lp->args[0], lp->args[0] + lp->args[1] - 1,
331 if (badoff >= lp->args[0] && badoff <
332 lp->args[0] + lp->args[1])
335 case OP_COLLAPSE_RANGE:
336 prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
337 lp->args[0], lp->args[0] + lp->args[1] - 1,
339 if (badoff >= lp->args[0] && badoff <
340 lp->args[0] + lp->args[1])
344 prt("SKIPPED (no operation)");
347 prt("BOGUS LOG ENTRY (operation code = %d)!",
351 prt("\n\t\tCLOSE/OPEN");
361 save_buffer(char *buffer, off_t bufferlength, int fd)
364 ssize_t byteswritten;
366 if (fd <= 0 || bufferlength == 0)
369 if (bufferlength > SSIZE_MAX) {
370 prt("fsx flaw: overflow in save_buffer\n");
374 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
375 if (size_by_seek == (off_t)-1)
376 prterr("save_buffer: lseek eof");
377 else if (bufferlength > size_by_seek) {
378 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
379 (unsigned long long)bufferlength);
380 bufferlength = size_by_seek;
384 ret = lseek(fd, (off_t)0, SEEK_SET);
385 if (ret == (off_t)-1)
386 prterr("save_buffer: lseek 0");
388 byteswritten = write(fd, buffer, (size_t)bufferlength);
389 if (byteswritten != bufferlength) {
390 if (byteswritten == -1)
391 prterr("save_buffer write");
393 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
394 (unsigned)byteswritten,
395 (unsigned long long)bufferlength);
401 report_failure(int status)
407 save_buffer(good_buf, file_size, fsxgoodfd);
408 prt("Correct content saved for comparison\n");
409 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
418 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
419 *(((unsigned char *)(cp)) + 1)))
422 check_buffers(unsigned offset, unsigned size)
430 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
431 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
432 offset, size, fname);
433 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
435 c = good_buf[offset];
439 bad = short_at(&temp_buf[i]);
440 prt("0x%5x\t0x%04x\t0x%04x", offset,
441 short_at(&good_buf[offset]), bad);
442 op = temp_buf[offset & 1 ? i+1 : i];
445 prt("operation# (mod 256) for "
446 "the bad data may be %u\n",
447 ((unsigned)op & 0xff));
449 prt("operation# (mod 256) for "
450 "the bad data unknown, check"
451 " HOLE and EXTEND ops\n");
471 if (fstat(fd, &statbuf)) {
472 prterr("check_size: fstat");
473 statbuf.st_size = -1;
475 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
476 if (file_size != statbuf.st_size || file_size != size_by_seek) {
477 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
478 (unsigned long long)file_size,
479 (unsigned long long)statbuf.st_size,
480 (unsigned long long)size_by_seek);
487 check_trunc_hack(void)
491 ftruncate(fd, (off_t)0);
492 ftruncate(fd, (off_t)100000);
494 if (statbuf.st_size != (off_t)100000) {
495 prt("no extend on truncate! not posix!\n");
502 doflush(unsigned offset, unsigned size)
508 if (o_direct == O_DIRECT)
511 pg_offset = offset & mmap_mask;
512 map_size = pg_offset + size;
514 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
515 MAP_FILE | MAP_SHARED, fd,
516 (off_t)(offset - pg_offset))) == (char *)-1) {
517 prterr("doflush: mmap");
520 if (msync(p, map_size, MS_INVALIDATE) != 0) {
521 prterr("doflush: msync");
524 if (munmap(p, map_size) != 0) {
525 prterr("doflush: munmap");
531 doread(unsigned offset, unsigned size)
536 offset -= offset % readbdy;
538 size -= size % readbdy;
540 if (!quiet && testcalls > simulatedopcount && !o_direct)
541 prt("skipping zero size read\n");
542 log4(OP_SKIPPED, OP_READ, offset, size);
545 if (size + offset > file_size) {
546 if (!quiet && testcalls > simulatedopcount)
547 prt("skipping seek/read past end of file\n");
548 log4(OP_SKIPPED, OP_READ, offset, size);
552 log4(OP_READ, offset, size, 0);
554 if (testcalls <= simulatedopcount)
558 ((progressinterval && testcalls % progressinterval == 0) ||
560 (monitorstart == -1 ||
561 (offset + size > monitorstart &&
562 (monitorend == -1 || offset <= monitorend))))))
563 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
564 offset, offset + size - 1, size);
565 ret = lseek(fd, (off_t)offset, SEEK_SET);
566 if (ret == (off_t)-1) {
567 prterr("doread: lseek");
570 iret = fsxread(fd, temp_buf, size, offset);
573 prterr("doread: read");
575 prt("short read: 0x%x bytes instead of 0x%x\n",
579 check_buffers(offset, size);
584 check_eofpage(char *s, unsigned offset, char *p, int size)
586 unsigned long last_page, should_be_zero;
588 if (offset + size <= (file_size & ~page_mask))
591 * we landed in the last page of the file
592 * test to make sure the VM system provided 0's
593 * beyond the true end of the file mapping
594 * (as required by mmap def in 1996 posix 1003.1)
596 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
598 for (should_be_zero = last_page + (file_size & page_mask);
599 should_be_zero < last_page + page_size;
601 if (*(char *)should_be_zero) {
602 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
603 s, file_size - 1, should_be_zero & page_mask,
604 short_at(should_be_zero));
611 domapread(unsigned offset, unsigned size)
617 offset -= offset % readbdy;
619 if (!quiet && testcalls > simulatedopcount)
620 prt("skipping zero size read\n");
621 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
624 if (size + offset > file_size) {
625 if (!quiet && testcalls > simulatedopcount)
626 prt("skipping seek/read past end of file\n");
627 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
631 log4(OP_MAPREAD, offset, size, 0);
633 if (testcalls <= simulatedopcount)
637 ((progressinterval && testcalls % progressinterval == 0) ||
639 (monitorstart == -1 ||
640 (offset + size > monitorstart &&
641 (monitorend == -1 || offset <= monitorend))))))
642 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
643 offset, offset + size - 1, size);
645 pg_offset = offset & PAGE_MASK;
646 map_size = pg_offset + size;
648 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
649 (off_t)(offset - pg_offset))) == (char *)-1) {
650 prterr("domapread: mmap");
653 memcpy(temp_buf, p + pg_offset, size);
655 check_eofpage("Read", offset, p, size);
657 if (munmap(p, map_size) != 0) {
658 prterr("domapread: munmap");
662 check_buffers(offset, size);
667 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
670 good_buf[offset] = testcalls % 256;
672 good_buf[offset] += original_buf[offset];
679 dowrite(unsigned offset, unsigned size)
684 offset -= offset % writebdy;
686 size -= size % writebdy;
688 if (!quiet && testcalls > simulatedopcount && !o_direct)
689 prt("skipping zero size write\n");
690 log4(OP_SKIPPED, OP_WRITE, offset, size);
694 log4(OP_WRITE, offset, size, file_size);
696 gendata(original_buf, good_buf, offset, size);
697 if (file_size < offset + size) {
698 if (file_size < offset)
699 memset(good_buf + file_size, '\0', offset - file_size);
700 file_size = offset + size;
702 warn("Lite file size bug in fsx!");
707 if (testcalls <= simulatedopcount)
711 ((progressinterval && testcalls % progressinterval == 0) ||
713 (monitorstart == -1 ||
714 (offset + size > monitorstart &&
715 (monitorend == -1 || offset <= monitorend))))))
716 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
717 offset, offset + size - 1, size);
718 ret = lseek(fd, (off_t)offset, SEEK_SET);
719 if (ret == (off_t)-1) {
720 prterr("dowrite: lseek");
723 iret = fsxwrite(fd, good_buf + offset, size, offset);
726 prterr("dowrite: write");
728 prt("short write: 0x%x bytes instead of 0x%x\n",
734 prt("fsync() failed: %s\n", strerror(errno));
739 doflush(offset, size);
745 domapwrite(unsigned offset, unsigned size)
752 offset -= offset % writebdy;
754 if (!quiet && testcalls > simulatedopcount)
755 prt("skipping zero size write\n");
756 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
759 cur_filesize = file_size;
761 log4(OP_MAPWRITE, offset, size, 0);
763 gendata(original_buf, good_buf, offset, size);
764 if (file_size < offset + size) {
765 if (file_size < offset)
766 memset(good_buf + file_size, '\0', offset - file_size);
767 file_size = offset + size;
769 warn("Lite file size bug in fsx!");
774 if (testcalls <= simulatedopcount)
778 ((progressinterval && testcalls % progressinterval == 0) ||
780 (monitorstart == -1 ||
781 (offset + size > monitorstart &&
782 (monitorend == -1 || offset <= monitorend))))))
783 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
784 offset, offset + size - 1, size);
786 if (file_size > cur_filesize) {
787 if (ftruncate(fd, file_size) == -1) {
788 prterr("domapwrite: ftruncate");
792 pg_offset = offset & PAGE_MASK;
793 map_size = pg_offset + size;
795 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
796 MAP_FILE | MAP_SHARED, fd,
797 (off_t)(offset - pg_offset))) == (char *)-1) {
798 prterr("domapwrite: mmap");
801 memcpy(p + pg_offset, good_buf + offset, size);
802 if (msync(p, map_size, MS_SYNC) != 0) {
803 prterr("domapwrite: msync");
807 check_eofpage("Write", offset, p, size);
809 if (munmap(p, map_size) != 0) {
810 prterr("domapwrite: munmap");
817 dotruncate(unsigned size)
819 int oldsize = file_size;
821 size -= size % truncbdy;
822 if (size > biggest) {
824 if (!quiet && testcalls > simulatedopcount)
825 prt("truncating to largest ever: 0x%x\n", size);
828 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
830 if (size > file_size)
831 memset(good_buf + file_size, '\0', size - file_size);
834 if (testcalls <= simulatedopcount)
837 if ((progressinterval && testcalls % progressinterval == 0) ||
838 (debug && (monitorstart == -1 || monitorend == -1 ||
839 size <= monitorend)))
840 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
841 if (ftruncate(fd, (off_t)size) == -1) {
842 prt("ftruncate1: %x\n", size);
843 prterr("dotruncate: ftruncate");
848 #ifdef FALLOC_FL_PUNCH_HOLE
850 do_punch_hole(unsigned offset, unsigned length)
855 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
858 if (!quiet && testcalls > simulatedopcount)
859 prt("skipping zero length punch hole\n");
860 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
864 if (file_size <= (loff_t)offset) {
865 if (!quiet && testcalls > simulatedopcount)
866 prt("skipping hole punch off the end of the file\n");
867 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
871 end_offset = offset + length;
873 log4(OP_PUNCH_HOLE, offset, length, 0);
875 if (testcalls <= simulatedopcount)
878 if ((progressinterval && testcalls % progressinterval == 0) ||
879 (debug && (monitorstart == -1 || monitorend == -1 ||
880 end_offset <= monitorend))) {
881 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
882 offset, offset+length, length);
884 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
885 prt("%punch hole: %x to %x\n", offset, length);
886 prterr("do_punch_hole: fallocate");
891 max_offset = offset < file_size ? offset : file_size;
892 max_len = max_offset + length <= file_size ? length :
893 file_size - max_offset;
894 memset(good_buf + max_offset, '\0', max_len);
899 do_punch_hole(unsigned offset, unsigned length)
905 #ifdef FALLOC_FL_ZERO_RANGE
907 do_zero_range(unsigned offset, unsigned length)
910 int mode = FALLOC_FL_ZERO_RANGE;
914 if (!quiet && testcalls > simulatedopcount)
915 prt("skipping zero length zero range\n");
916 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
921 keep_size = random() % 2;
923 end_offset = keep_size ? 0 : offset + length;
925 if (end_offset > biggest) {
926 biggest = end_offset;
927 if (!quiet && testcalls > simulatedopcount)
928 prt("zero_range to largest ever: 0x%x\n", end_offset);
932 * last arg matches fallocate string array index in logdump:
933 * 0: allocate past EOF
934 * 1: extending prealloc
935 * 2: interior prealloc
937 log4(OP_ZERO_RANGE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
939 if (testcalls <= simulatedopcount)
942 if ((progressinterval && testcalls % progressinterval == 0) ||
943 (debug && (monitorstart == -1 || monitorend == -1 ||
944 end_offset <= monitorend))) {
945 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
946 offset, offset+length, length);
948 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
949 prt("%pzero range: %x to %x\n", offset, length);
950 prterr("do_zero_range: fallocate");
954 memset(good_buf + offset, '\0', length);
959 do_zero_range(unsigned offset, unsigned length)
965 #ifdef FALLOC_FL_COLLAPSE_RANGE
967 do_collapse_range(unsigned offset, unsigned length)
970 int mode = FALLOC_FL_COLLAPSE_RANGE;
973 if (!quiet && testcalls > simulatedopcount)
974 prt("skipping zero length collapse range\n");
975 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
979 end_offset = offset + length;
980 if ((loff_t)end_offset >= file_size) {
981 if (!quiet && testcalls > simulatedopcount)
982 prt("skipping collapse range behind EOF\n");
983 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
987 log4(OP_COLLAPSE_RANGE, offset, length, 0);
989 if (testcalls <= simulatedopcount)
992 if ((progressinterval && testcalls % progressinterval == 0) ||
993 (debug && (monitorstart == -1 || monitorend == -1 ||
994 end_offset <= monitorend))) {
995 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
996 offset, offset+length, length);
998 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
999 prt("collapse range: %x to %x\n", offset, length);
1000 prterr("do_collapse_range: fallocate");
1001 report_failure(161);
1004 memmove(good_buf + offset, good_buf + end_offset,
1005 file_size - end_offset);
1006 file_size -= length;
1011 do_collapse_range(unsigned offset, unsigned length)
1017 #ifdef HAVE_LINUX_FALLOC_H
1018 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1020 do_preallocate(unsigned offset, unsigned length)
1022 unsigned end_offset;
1026 if (!quiet && testcalls > simulatedopcount)
1027 prt("skipping zero length fallocate\n");
1028 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1032 if (keep_size_calls)
1033 keep_size = random() % 2;
1035 end_offset = keep_size ? 0 : offset + length;
1037 if (end_offset > biggest) {
1038 biggest = end_offset;
1039 if (!quiet && testcalls > simulatedopcount)
1040 prt("fallocating to largest ever: 0x%x\n", end_offset);
1044 * last arg matches fallocate string array index in logdump:
1045 * 0: allocate past EOF
1046 * 1: extending prealloc
1047 * 2: interior prealloc
1049 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1051 if (end_offset > file_size) {
1052 memset(good_buf + file_size, '\0', end_offset - file_size);
1053 file_size = end_offset;
1056 if (testcalls <= simulatedopcount)
1059 if ((progressinterval && testcalls % progressinterval == 0) ||
1060 (debug && (monitorstart == -1 || monitorend == -1 ||
1061 end_offset <= monitorend)))
1062 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1063 offset, offset + length, length);
1064 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1065 prt("fallocate: %x to %x\n", offset, length);
1066 prterr("do_preallocate: fallocate");
1067 report_failure(161);
1072 do_preallocate(unsigned offset, unsigned length)
1083 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1084 prterr("writefileimage: lseek");
1085 report_failure(171);
1087 iret = write(fd, good_buf, file_size);
1088 if ((off_t)iret != file_size) {
1090 prterr("writefileimage: write");
1092 prt("short write: 0x%x bytes instead of 0x%llx\n",
1093 iret, (unsigned long long)file_size);
1094 report_failure(172);
1096 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1097 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1098 prterr("writefileimage: ftruncate");
1099 report_failure(173);
1107 if (testcalls <= simulatedopcount)
1111 prt("%lu close/open\n", testcalls);
1113 prterr("docloseopen: close");
1114 report_failure(180);
1116 fd = open(fname, O_RDWR|o_direct, 0);
1118 prterr("docloseopen: open");
1119 report_failure(181);
1123 #define TRIM_OFF_LEN(off, len, size) \
1129 if ((off) + (len) > (size)) \
1130 (len) = (size) - (off); \
1136 unsigned long offset;
1137 unsigned long size = maxoplen;
1138 unsigned long rv = random();
1141 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1147 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1149 if (debugstart > 0 && testcalls >= debugstart)
1152 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1153 prt("%lu...\n", testcalls);
1157 size = random() % (maxoplen + 1);
1159 /* calculate appropriate op to run */
1161 op = rv % OP_MAX_LITE;
1163 op = rv % OP_MAX_FULL;
1175 if (!fallocate_calls) {
1176 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1181 if (!punch_hole_calls) {
1182 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1187 if (!zero_range_calls) {
1188 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1192 case OP_COLLAPSE_RANGE:
1193 if (!collapse_range_calls) {
1194 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1202 TRIM_OFF_LEN(offset, size, file_size);
1203 doread(offset, size);
1207 TRIM_OFF_LEN(offset, size, maxfilelen);
1208 dowrite(offset, size);
1212 TRIM_OFF_LEN(offset, size, file_size);
1213 domapread(offset, size);
1217 TRIM_OFF_LEN(offset, size, maxfilelen);
1218 domapwrite(offset, size);
1223 size = random() % maxfilelen;
1228 TRIM_OFF_LEN(offset, size, maxfilelen);
1229 do_preallocate(offset, size);
1233 TRIM_OFF_LEN(offset, size, file_size);
1234 do_punch_hole(offset, size);
1237 TRIM_OFF_LEN(offset, size, file_size);
1238 do_zero_range(offset, size);
1240 case OP_COLLAPSE_RANGE:
1241 TRIM_OFF_LEN(offset, size, file_size - 1);
1242 offset = offset & ~(block_size - 1);
1243 size = size & ~(block_size - 1);
1245 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1248 do_collapse_range(offset, size);
1251 prterr("test: unknown operation");
1257 if (sizechecks && testcalls > simulatedopcount)
1269 prt("signal %d\n", sig);
1270 prt("testcalls = %lu\n", testcalls);
1278 fprintf(stdout, "usage: %s",
1279 "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\
1280 -b opnum: beginning operation number (default 1)\n\
1281 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1282 -d: debug output for all operations\n\
1283 -f flush and invalidate cache after I/O\n\
1284 -l flen: the upper bound on file size (default 262144)\n\
1285 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1286 -n: no verifications of file size\n\
1287 -o oplen: the upper bound on operation size (default 65536)\n\
1288 -p progressinterval: debug output at specified operation interval\n\
1289 -q: quieter operation\n\
1290 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1291 -s style: 1 gives smaller truncates (default 0)\n\
1292 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1293 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1294 -x: preallocate file space before starting, XFS only (default 0)\n\
1295 -y synchronize changes to a file\n"
1298 " -A: Use the AIO system calls\n"
1300 " -D startingop: debug output starting at specified operation\n"
1301 #ifdef HAVE_LINUX_FALLOC_H
1302 " -F: Do not use fallocate (preallocation) calls\n"
1304 #ifdef FALLOC_FL_PUNCH_HOLE
1305 " -H: Do not use punch hole calls\n"
1307 #ifdef FALLOC_FL_ZERO_RANGE
1308 " -z: Do not use zero range calls\n"
1310 #ifdef FALLOC_FL_COLLAPSE_RANGE
1311 " -C: Do not use collapse range calls\n"
1313 " -L: fsxLite - no file creations & no file size changes\n\
1314 -N numops: total # operations to do (default infinity)\n\
1315 -O: use oplen (see -o flag) for every op (default random)\n\
1316 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1317 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1318 -W: mapped write operations DISabled\n\
1319 -R: read() system calls only (mapped reads disabled)\n\
1320 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1321 fname: this filename is REQUIRED (no default)\n");
1327 getnum(char *s, char **e)
1332 ret = strtol(s, e, 0);
1362 io_context_t io_ctx;
1368 ret = io_queue_init(QSZ, &io_ctx);
1370 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1378 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1380 struct io_event event;
1381 static struct timespec ts;
1382 struct iocb *iocbs[] = { &iocb };
1387 io_prep_pread(&iocb, fd, buf, len, offset);
1389 io_prep_pwrite(&iocb, fd, buf, len, offset);
1394 ret = io_submit(io_ctx, 1, iocbs);
1396 fprintf(stderr, "errcode=%d\n", ret);
1397 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1402 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1405 fprintf(stderr, "aio_rw: no events available\n");
1407 fprintf(stderr, "errcode=%d\n", -ret);
1408 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1413 if (len != event.res) {
1415 * The b0rked libaio defines event.res as unsigned.
1416 * However the kernel strucuture has it signed,
1417 * and it's used to pass negated error value.
1418 * Till the library is fixed use the temp var.
1420 res = (long)event.res;
1422 fprintf(stderr, "bad io length: %lu instead of %u\n",
1425 fprintf(stderr, "errcode=%ld\n", -res);
1426 fprintf(stderr, "aio_rw: async io failed: %s\n",
1437 * The caller expects error return in traditional libc
1438 * convention, i.e. -1 and the errno set to error.
1444 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1449 ret = __aio_rw(rw, fd, buf, len, offset);
1452 ret = read(fd, buf, len);
1454 ret = write(fd, buf, len);
1462 test_fallocate(int mode)
1464 #ifdef HAVE_LINUX_FALLOC_H
1467 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1469 warn("main: filesystem does not support "
1470 "fallocate mode 0x%x, disabling!\n", mode);
1481 main(int argc, char **argv)
1485 char goodfile[1024];
1487 struct stat statbuf;
1492 page_size = getpagesize();
1493 page_mask = page_size - 1;
1494 mmap_mask = page_mask;
1497 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1499 while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCLN:OP:RS:WZ"))
1503 simulatedopcount = getnum(optarg, &endp);
1505 fprintf(stdout, "Will begin at operation %ld\n",
1507 if (simulatedopcount == 0)
1509 simulatedopcount -= 1;
1512 closeprob = getnum(optarg, &endp);
1515 "Chance of close/open is 1 in %d\n",
1527 maxfilelen = getnum(optarg, &endp);
1528 if (maxfilelen <= 0)
1532 monitorstart = getnum(optarg, &endp);
1533 if (monitorstart < 0)
1535 if (!endp || *endp++ != ':')
1537 monitorend = getnum(endp, &endp);
1540 if (monitorend == 0)
1541 monitorend = -1; /* aka infinity */
1547 maxoplen = getnum(optarg, &endp);
1552 progressinterval = getnum(optarg, &endp);
1553 if (progressinterval == 0)
1560 readbdy = getnum(optarg, &endp);
1565 style = getnum(optarg, &endp);
1566 if (style < 0 || style > 1)
1570 truncbdy = getnum(optarg, &endp);
1575 writebdy = getnum(optarg, &endp);
1589 debugstart = getnum(optarg, &endp);
1594 fallocate_calls = 0;
1597 keep_size_calls = 0;
1600 punch_hole_calls = 0;
1603 zero_range_calls = 0;
1606 collapse_range_calls = 0;
1612 numops = getnum(optarg, &endp);
1620 strncpy(goodfile, optarg, sizeof(goodfile));
1621 strcat(goodfile, "/");
1622 strncpy(logfile, optarg, sizeof(logfile));
1623 strcat(logfile, "/");
1629 seed = getnum(optarg, &endp);
1631 seed = time(0) % 10000;
1633 fprintf(stdout, "Seed set to %d\n", seed);
1640 fprintf(stdout, "mapped writes DISABLED\n");
1643 o_direct = O_DIRECT;
1655 signal(SIGHUP, cleanup);
1656 signal(SIGINT, cleanup);
1657 signal(SIGPIPE, cleanup);
1658 signal(SIGALRM, cleanup);
1659 signal(SIGTERM, cleanup);
1660 signal(SIGXCPU, cleanup);
1661 signal(SIGXFSZ, cleanup);
1662 signal(SIGVTALRM, cleanup);
1663 signal(SIGUSR1, cleanup);
1664 signal(SIGUSR2, cleanup);
1666 initstate(seed, state, 256);
1669 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1674 if (fstat(fd, &statbuf)) {
1675 prterr("check_size: fstat");
1678 block_size = statbuf.st_blksize;
1681 xfs_flock64_t resv = { 0 };
1682 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1683 if (!platform_test_xfs_fd(fd)) {
1685 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1689 resv.l_len = maxfilelen;
1690 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1696 strncat(goodfile, fname, 256);
1697 strcat (goodfile, ".fsxgood");
1698 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1699 if (fsxgoodfd < 0) {
1703 strncat(logfile, fname, 256);
1704 strcat (logfile, ".fsxlog");
1705 fsxlogf = fopen(logfile, "w");
1706 if (fsxlogf == NULL) {
1718 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1719 if (file_size == (off_t)-1) {
1721 warn("main: lseek eof");
1724 ret = lseek(fd, (off_t)0, SEEK_SET);
1725 if (ret == (off_t)-1) {
1727 warn("main: lseek 0");
1731 original_buf = (char *) malloc(maxfilelen);
1732 for (i = 0; i < maxfilelen; i++)
1733 original_buf[i] = random() % 256;
1734 good_buf = (char *) malloc(maxfilelen + writebdy);
1735 good_buf = round_ptr_up(good_buf, writebdy, 0);
1736 memset(good_buf, '\0', maxfilelen);
1737 temp_buf = (char *) malloc(maxoplen + readbdy);
1738 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1739 memset(temp_buf, '\0', maxoplen);
1740 if (lite) { /* zero entire existing file */
1743 written = write(fd, good_buf, (size_t)maxfilelen);
1744 if (written != maxfilelen) {
1745 if (written == -1) {
1747 warn("main: error on write");
1749 warn("main: short write, 0x%x bytes instead "
1758 if (fallocate_calls)
1759 fallocate_calls = test_fallocate(0);
1760 if (keep_size_calls)
1761 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
1762 if (punch_hole_calls)
1763 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE |
1764 FALLOC_FL_KEEP_SIZE);
1765 if (zero_range_calls)
1766 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1767 if (collapse_range_calls)
1768 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
1770 while (numops == -1 || numops--)
1777 prt("All operations completed A-OK!\n");