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 punch_hole_calls = 1; /* -H flag disables */
146 int zero_range_calls = 1; /* -z flag disables */
147 int collapse_range_calls = 1; /* -C flag disables */
148 int mapped_reads = 1; /* -R flag disables it */
150 int o_direct; /* -Z */
157 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
160 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
161 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
163 #define fsxread(a,b,c,d) read(a,b,c)
164 #define fsxwrite(a,b,c,d) write(a,b,c)
167 FILE * fsxlogf = NULL;
171 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
173 unsigned long ret = (unsigned long)ptr;
175 ret = ((ret + align - 1) & ~(align - 1));
181 vwarnc(int code, const char *fmt, va_list ap) {
182 fprintf(stderr, "fsx: ");
184 vfprintf(stderr, fmt, ap);
185 fprintf(stderr, ": ");
187 fprintf(stderr, "%s\n", strerror(code));
191 warn(const char * fmt, ...) {
194 vwarnc(errno, fmt, ap);
198 #define BUF_SIZE 1024
204 char buffer[BUF_SIZE];
207 vsnprintf(buffer, BUF_SIZE, fmt, args);
209 fprintf(stdout, buffer);
211 fprintf(fsxlogf, buffer);
217 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
222 log4(int operation, int arg0, int arg1, int arg2)
224 struct log_entry *le;
227 le->operation = operation;
229 le->operation = ~ le->operation;
235 if (logptr >= LOGSIZE)
244 struct log_entry *lp;
245 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
247 prt("LOG DUMP (%d total operations):\n", logcount);
248 if (logcount < LOGSIZE) {
255 for ( ; count > 0; count--) {
258 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
259 prt("%d(%3d mod 256): ", opnum, opnum%256);
261 if ((closeopen = lp->operation < 0))
262 lp->operation = ~ lp->operation;
264 switch (lp->operation) {
266 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
267 lp->args[0], lp->args[0] + lp->args[1] - 1,
269 if (badoff >= lp->args[0] && badoff <
270 lp->args[0] + lp->args[1])
274 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
275 lp->args[0], lp->args[0] + lp->args[1] - 1,
277 if (badoff >= lp->args[0] && badoff <
278 lp->args[0] + lp->args[1])
282 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
283 lp->args[0], lp->args[0] + lp->args[1] - 1,
285 if (badoff >= lp->args[0] &&
286 badoff < lp->args[0] + lp->args[1])
290 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
291 lp->args[0], lp->args[0] + lp->args[1] - 1,
293 if (lp->args[0] > lp->args[2])
295 else if (lp->args[0] + lp->args[1] > lp->args[2])
297 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
298 badoff < lp->args[0] + lp->args[1])
302 down = lp->args[0] < lp->args[1];
303 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
304 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
305 if (badoff >= lp->args[!down] &&
306 badoff < lp->args[!!down])
310 /* 0: offset 1: length 2: where alloced */
311 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
312 lp->args[0], lp->args[0] + lp->args[1],
313 lp->args[1], falloc_type[lp->args[2]]);
314 if (badoff >= lp->args[0] &&
315 badoff < lp->args[0] + lp->args[1])
319 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
320 lp->args[0], lp->args[0] + lp->args[1] - 1,
322 if (badoff >= lp->args[0] && badoff <
323 lp->args[0] + lp->args[1])
327 prt("ZERO 0x%x thru 0x%x\t(0x%x bytes)",
328 lp->args[0], lp->args[0] + lp->args[1] - 1,
330 if (badoff >= lp->args[0] && badoff <
331 lp->args[0] + lp->args[1])
334 case OP_COLLAPSE_RANGE:
335 prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
336 lp->args[0], lp->args[0] + lp->args[1] - 1,
338 if (badoff >= lp->args[0] && badoff <
339 lp->args[0] + lp->args[1])
343 prt("SKIPPED (no operation)");
346 prt("BOGUS LOG ENTRY (operation code = %d)!",
350 prt("\n\t\tCLOSE/OPEN");
360 save_buffer(char *buffer, off_t bufferlength, int fd)
363 ssize_t byteswritten;
365 if (fd <= 0 || bufferlength == 0)
368 if (bufferlength > SSIZE_MAX) {
369 prt("fsx flaw: overflow in save_buffer\n");
373 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
374 if (size_by_seek == (off_t)-1)
375 prterr("save_buffer: lseek eof");
376 else if (bufferlength > size_by_seek) {
377 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
378 (unsigned long long)bufferlength);
379 bufferlength = size_by_seek;
383 ret = lseek(fd, (off_t)0, SEEK_SET);
384 if (ret == (off_t)-1)
385 prterr("save_buffer: lseek 0");
387 byteswritten = write(fd, buffer, (size_t)bufferlength);
388 if (byteswritten != bufferlength) {
389 if (byteswritten == -1)
390 prterr("save_buffer write");
392 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
393 (unsigned)byteswritten,
394 (unsigned long long)bufferlength);
400 report_failure(int status)
406 save_buffer(good_buf, file_size, fsxgoodfd);
407 prt("Correct content saved for comparison\n");
408 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
417 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
418 *(((unsigned char *)(cp)) + 1)))
421 check_buffers(unsigned offset, unsigned size)
429 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
430 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
431 offset, size, fname);
432 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
434 c = good_buf[offset];
438 bad = short_at(&temp_buf[i]);
439 prt("0x%5x\t0x%04x\t0x%04x", offset,
440 short_at(&good_buf[offset]), bad);
441 op = temp_buf[offset & 1 ? i+1 : i];
444 prt("operation# (mod 256) for "
445 "the bad data may be %u\n",
446 ((unsigned)op & 0xff));
448 prt("operation# (mod 256) for "
449 "the bad data unknown, check"
450 " HOLE and EXTEND ops\n");
470 if (fstat(fd, &statbuf)) {
471 prterr("check_size: fstat");
472 statbuf.st_size = -1;
474 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
475 if (file_size != statbuf.st_size || file_size != size_by_seek) {
476 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
477 (unsigned long long)file_size,
478 (unsigned long long)statbuf.st_size,
479 (unsigned long long)size_by_seek);
486 check_trunc_hack(void)
490 ftruncate(fd, (off_t)0);
491 ftruncate(fd, (off_t)100000);
493 if (statbuf.st_size != (off_t)100000) {
494 prt("no extend on truncate! not posix!\n");
501 doflush(unsigned offset, unsigned size)
507 if (o_direct == O_DIRECT)
510 pg_offset = offset & mmap_mask;
511 map_size = pg_offset + size;
513 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
514 MAP_FILE | MAP_SHARED, fd,
515 (off_t)(offset - pg_offset))) == (char *)-1) {
516 prterr("doflush: mmap");
519 if (msync(p, map_size, MS_INVALIDATE) != 0) {
520 prterr("doflush: msync");
523 if (munmap(p, map_size) != 0) {
524 prterr("doflush: munmap");
530 doread(unsigned offset, unsigned size)
535 offset -= offset % readbdy;
537 size -= size % readbdy;
539 if (!quiet && testcalls > simulatedopcount && !o_direct)
540 prt("skipping zero size read\n");
541 log4(OP_SKIPPED, OP_READ, offset, size);
544 if (size + offset > file_size) {
545 if (!quiet && testcalls > simulatedopcount)
546 prt("skipping seek/read past end of file\n");
547 log4(OP_SKIPPED, OP_READ, offset, size);
551 log4(OP_READ, offset, size, 0);
553 if (testcalls <= simulatedopcount)
557 ((progressinterval && testcalls % progressinterval == 0) ||
559 (monitorstart == -1 ||
560 (offset + size > monitorstart &&
561 (monitorend == -1 || offset <= monitorend))))))
562 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
563 offset, offset + size - 1, size);
564 ret = lseek(fd, (off_t)offset, SEEK_SET);
565 if (ret == (off_t)-1) {
566 prterr("doread: lseek");
569 iret = fsxread(fd, temp_buf, size, offset);
572 prterr("doread: read");
574 prt("short read: 0x%x bytes instead of 0x%x\n",
578 check_buffers(offset, size);
583 check_eofpage(char *s, unsigned offset, char *p, int size)
585 unsigned long last_page, should_be_zero;
587 if (offset + size <= (file_size & ~page_mask))
590 * we landed in the last page of the file
591 * test to make sure the VM system provided 0's
592 * beyond the true end of the file mapping
593 * (as required by mmap def in 1996 posix 1003.1)
595 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
597 for (should_be_zero = last_page + (file_size & page_mask);
598 should_be_zero < last_page + page_size;
600 if (*(char *)should_be_zero) {
601 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
602 s, file_size - 1, should_be_zero & page_mask,
603 short_at(should_be_zero));
610 domapread(unsigned offset, unsigned size)
616 offset -= offset % readbdy;
618 if (!quiet && testcalls > simulatedopcount)
619 prt("skipping zero size read\n");
620 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
623 if (size + offset > file_size) {
624 if (!quiet && testcalls > simulatedopcount)
625 prt("skipping seek/read past end of file\n");
626 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
630 log4(OP_MAPREAD, offset, size, 0);
632 if (testcalls <= simulatedopcount)
636 ((progressinterval && testcalls % progressinterval == 0) ||
638 (monitorstart == -1 ||
639 (offset + size > monitorstart &&
640 (monitorend == -1 || offset <= monitorend))))))
641 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
642 offset, offset + size - 1, size);
644 pg_offset = offset & PAGE_MASK;
645 map_size = pg_offset + size;
647 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
648 (off_t)(offset - pg_offset))) == (char *)-1) {
649 prterr("domapread: mmap");
652 memcpy(temp_buf, p + pg_offset, size);
654 check_eofpage("Read", offset, p, size);
656 if (munmap(p, map_size) != 0) {
657 prterr("domapread: munmap");
661 check_buffers(offset, size);
666 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
669 good_buf[offset] = testcalls % 256;
671 good_buf[offset] += original_buf[offset];
678 dowrite(unsigned offset, unsigned size)
683 offset -= offset % writebdy;
685 size -= size % writebdy;
687 if (!quiet && testcalls > simulatedopcount && !o_direct)
688 prt("skipping zero size write\n");
689 log4(OP_SKIPPED, OP_WRITE, offset, size);
693 log4(OP_WRITE, offset, size, file_size);
695 gendata(original_buf, good_buf, offset, size);
696 if (file_size < offset + size) {
697 if (file_size < offset)
698 memset(good_buf + file_size, '\0', offset - file_size);
699 file_size = offset + size;
701 warn("Lite file size bug in fsx!");
706 if (testcalls <= simulatedopcount)
710 ((progressinterval && testcalls % progressinterval == 0) ||
712 (monitorstart == -1 ||
713 (offset + size > monitorstart &&
714 (monitorend == -1 || offset <= monitorend))))))
715 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
716 offset, offset + size - 1, size);
717 ret = lseek(fd, (off_t)offset, SEEK_SET);
718 if (ret == (off_t)-1) {
719 prterr("dowrite: lseek");
722 iret = fsxwrite(fd, good_buf + offset, size, offset);
725 prterr("dowrite: write");
727 prt("short write: 0x%x bytes instead of 0x%x\n",
733 prt("fsync() failed: %s\n", strerror(errno));
738 doflush(offset, size);
744 domapwrite(unsigned offset, unsigned size)
751 offset -= offset % writebdy;
753 if (!quiet && testcalls > simulatedopcount)
754 prt("skipping zero size write\n");
755 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
758 cur_filesize = file_size;
760 log4(OP_MAPWRITE, offset, size, 0);
762 gendata(original_buf, good_buf, offset, size);
763 if (file_size < offset + size) {
764 if (file_size < offset)
765 memset(good_buf + file_size, '\0', offset - file_size);
766 file_size = offset + size;
768 warn("Lite file size bug in fsx!");
773 if (testcalls <= simulatedopcount)
777 ((progressinterval && testcalls % progressinterval == 0) ||
779 (monitorstart == -1 ||
780 (offset + size > monitorstart &&
781 (monitorend == -1 || offset <= monitorend))))))
782 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
783 offset, offset + size - 1, size);
785 if (file_size > cur_filesize) {
786 if (ftruncate(fd, file_size) == -1) {
787 prterr("domapwrite: ftruncate");
791 pg_offset = offset & PAGE_MASK;
792 map_size = pg_offset + size;
794 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
795 MAP_FILE | MAP_SHARED, fd,
796 (off_t)(offset - pg_offset))) == (char *)-1) {
797 prterr("domapwrite: mmap");
800 memcpy(p + pg_offset, good_buf + offset, size);
801 if (msync(p, map_size, MS_SYNC) != 0) {
802 prterr("domapwrite: msync");
806 check_eofpage("Write", offset, p, size);
808 if (munmap(p, map_size) != 0) {
809 prterr("domapwrite: munmap");
816 dotruncate(unsigned size)
818 int oldsize = file_size;
820 size -= size % truncbdy;
821 if (size > biggest) {
823 if (!quiet && testcalls > simulatedopcount)
824 prt("truncating to largest ever: 0x%x\n", size);
827 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
829 if (size > file_size)
830 memset(good_buf + file_size, '\0', size - file_size);
833 if (testcalls <= simulatedopcount)
836 if ((progressinterval && testcalls % progressinterval == 0) ||
837 (debug && (monitorstart == -1 || monitorend == -1 ||
838 size <= monitorend)))
839 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
840 if (ftruncate(fd, (off_t)size) == -1) {
841 prt("ftruncate1: %x\n", size);
842 prterr("dotruncate: ftruncate");
847 #ifdef FALLOC_FL_PUNCH_HOLE
849 do_punch_hole(unsigned offset, unsigned length)
854 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
857 if (!quiet && testcalls > simulatedopcount)
858 prt("skipping zero length punch hole\n");
859 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
863 if (file_size <= (loff_t)offset) {
864 if (!quiet && testcalls > simulatedopcount)
865 prt("skipping hole punch off the end of the file\n");
866 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
870 end_offset = offset + length;
872 log4(OP_PUNCH_HOLE, offset, length, 0);
874 if (testcalls <= simulatedopcount)
877 if ((progressinterval && testcalls % progressinterval == 0) ||
878 (debug && (monitorstart == -1 || monitorend == -1 ||
879 end_offset <= monitorend))) {
880 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
881 offset, offset+length, length);
883 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
884 prt("%punch hole: %x to %x\n", offset, length);
885 prterr("do_punch_hole: fallocate");
890 max_offset = offset < file_size ? offset : file_size;
891 max_len = max_offset + length <= file_size ? length :
892 file_size - max_offset;
893 memset(good_buf + max_offset, '\0', max_len);
898 do_punch_hole(unsigned offset, unsigned length)
904 #ifdef FALLOC_FL_ZERO_RANGE
906 do_zero_range(unsigned offset, unsigned length)
909 int mode = FALLOC_FL_ZERO_RANGE;
913 if (!quiet && testcalls > simulatedopcount)
914 prt("skipping zero length zero range\n");
915 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
919 keep_size = random() % 2;
921 end_offset = keep_size ? 0 : offset + length;
923 if (end_offset > biggest) {
924 biggest = end_offset;
925 if (!quiet && testcalls > simulatedopcount)
926 prt("zero_range to largest ever: 0x%x\n", end_offset);
930 * last arg matches fallocate string array index in logdump:
931 * 0: allocate past EOF
932 * 1: extending prealloc
933 * 2: interior prealloc
935 log4(OP_ZERO_RANGE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
937 if (testcalls <= simulatedopcount)
940 if ((progressinterval && testcalls % progressinterval == 0) ||
941 (debug && (monitorstart == -1 || monitorend == -1 ||
942 end_offset <= monitorend))) {
943 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
944 offset, offset+length, length);
946 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
947 prt("%pzero range: %x to %x\n", offset, length);
948 prterr("do_zero_range: fallocate");
952 memset(good_buf + offset, '\0', length);
957 do_zero_range(unsigned offset, unsigned length)
963 #ifdef FALLOC_FL_COLLAPSE_RANGE
965 do_collapse_range(unsigned offset, unsigned length)
968 int mode = FALLOC_FL_COLLAPSE_RANGE;
971 if (!quiet && testcalls > simulatedopcount)
972 prt("skipping zero length collapse range\n");
973 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
977 end_offset = offset + length;
978 if ((loff_t)end_offset >= file_size) {
979 if (!quiet && testcalls > simulatedopcount)
980 prt("skipping collapse range behind EOF\n");
981 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, length);
985 log4(OP_COLLAPSE_RANGE, offset, length, 0);
987 if (testcalls <= simulatedopcount)
990 if ((progressinterval && testcalls % progressinterval == 0) ||
991 (debug && (monitorstart == -1 || monitorend == -1 ||
992 end_offset <= monitorend))) {
993 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
994 offset, offset+length, length);
996 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
997 prt("collapse range: %x to %x\n", offset, length);
998 prterr("do_collapse_range: fallocate");
1002 memmove(good_buf + offset, good_buf + end_offset,
1003 file_size - end_offset);
1004 file_size -= length;
1009 do_collapse_range(unsigned offset, unsigned length)
1015 #ifdef HAVE_LINUX_FALLOC_H
1016 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1018 do_preallocate(unsigned offset, unsigned length)
1020 unsigned end_offset;
1024 if (!quiet && testcalls > simulatedopcount)
1025 prt("skipping zero length fallocate\n");
1026 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1030 keep_size = random() % 2;
1032 end_offset = keep_size ? 0 : offset + length;
1034 if (end_offset > biggest) {
1035 biggest = end_offset;
1036 if (!quiet && testcalls > simulatedopcount)
1037 prt("fallocating to largest ever: 0x%x\n", end_offset);
1041 * last arg matches fallocate string array index in logdump:
1042 * 0: allocate past EOF
1043 * 1: extending prealloc
1044 * 2: interior prealloc
1046 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1048 if (end_offset > file_size) {
1049 memset(good_buf + file_size, '\0', end_offset - file_size);
1050 file_size = end_offset;
1053 if (testcalls <= simulatedopcount)
1056 if ((progressinterval && testcalls % progressinterval == 0) ||
1057 (debug && (monitorstart == -1 || monitorend == -1 ||
1058 end_offset <= monitorend)))
1059 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1060 offset, offset + length, length);
1061 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1062 prt("fallocate: %x to %x\n", offset, length);
1063 prterr("do_preallocate: fallocate");
1064 report_failure(161);
1069 do_preallocate(unsigned offset, unsigned length)
1080 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1081 prterr("writefileimage: lseek");
1082 report_failure(171);
1084 iret = write(fd, good_buf, file_size);
1085 if ((off_t)iret != file_size) {
1087 prterr("writefileimage: write");
1089 prt("short write: 0x%x bytes instead of 0x%llx\n",
1090 iret, (unsigned long long)file_size);
1091 report_failure(172);
1093 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1094 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1095 prterr("writefileimage: ftruncate");
1096 report_failure(173);
1104 if (testcalls <= simulatedopcount)
1108 prt("%lu close/open\n", testcalls);
1110 prterr("docloseopen: close");
1111 report_failure(180);
1113 fd = open(fname, O_RDWR|o_direct, 0);
1115 prterr("docloseopen: open");
1116 report_failure(181);
1120 #define TRIM_OFF_LEN(off, len, size) \
1126 if ((off) + (len) > (size)) \
1127 (len) = (size) - (off); \
1133 unsigned long offset;
1134 unsigned long size = maxoplen;
1135 unsigned long rv = random();
1138 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1144 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1146 if (debugstart > 0 && testcalls >= debugstart)
1149 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1150 prt("%lu...\n", testcalls);
1154 size = random() % (maxoplen + 1);
1156 /* calculate appropriate op to run */
1158 op = rv % OP_MAX_LITE;
1160 op = rv % OP_MAX_FULL;
1172 if (!fallocate_calls) {
1173 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1178 if (!punch_hole_calls) {
1179 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1184 if (!zero_range_calls) {
1185 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1189 case OP_COLLAPSE_RANGE:
1190 if (!collapse_range_calls) {
1191 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1199 TRIM_OFF_LEN(offset, size, file_size);
1200 doread(offset, size);
1204 TRIM_OFF_LEN(offset, size, maxfilelen);
1205 dowrite(offset, size);
1209 TRIM_OFF_LEN(offset, size, file_size);
1210 domapread(offset, size);
1214 TRIM_OFF_LEN(offset, size, maxfilelen);
1215 domapwrite(offset, size);
1220 size = random() % maxfilelen;
1225 TRIM_OFF_LEN(offset, size, maxfilelen);
1226 do_preallocate(offset, size);
1230 TRIM_OFF_LEN(offset, size, file_size);
1231 do_punch_hole(offset, size);
1234 TRIM_OFF_LEN(offset, size, file_size);
1235 do_zero_range(offset, size);
1237 case OP_COLLAPSE_RANGE:
1238 TRIM_OFF_LEN(offset, size, file_size - 1);
1239 offset = offset & ~(block_size - 1);
1240 size = size & ~(block_size - 1);
1242 log4(OP_SKIPPED, OP_COLLAPSE_RANGE, offset, size);
1245 do_collapse_range(offset, size);
1248 prterr("test: unknown operation");
1254 if (sizechecks && testcalls > simulatedopcount)
1266 prt("signal %d\n", sig);
1267 prt("testcalls = %lu\n", testcalls);
1275 fprintf(stdout, "usage: %s",
1276 "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\
1277 -b opnum: beginning operation number (default 1)\n\
1278 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1279 -d: debug output for all operations\n\
1280 -f flush and invalidate cache after I/O\n\
1281 -l flen: the upper bound on file size (default 262144)\n\
1282 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1283 -n: no verifications of file size\n\
1284 -o oplen: the upper bound on operation size (default 65536)\n\
1285 -p progressinterval: debug output at specified operation interval\n\
1286 -q: quieter operation\n\
1287 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1288 -s style: 1 gives smaller truncates (default 0)\n\
1289 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1290 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1291 -x: preallocate file space before starting, XFS only (default 0)\n\
1292 -y synchronize changes to a file\n"
1295 " -A: Use the AIO system calls\n"
1297 " -D startingop: debug output starting at specified operation\n"
1298 #ifdef HAVE_LINUX_FALLOC_H
1299 " -F: Do not use fallocate (preallocation) calls\n"
1301 #ifdef FALLOC_FL_PUNCH_HOLE
1302 " -H: Do not use punch hole calls\n"
1304 #ifdef FALLOC_FL_ZERO_RANGE
1305 " -z: Do not use zero range calls\n"
1307 #ifdef FALLOC_FL_COLLAPSE_RANGE
1308 " -C: Do not use collapse range calls\n"
1310 " -L: fsxLite - no file creations & no file size changes\n\
1311 -N numops: total # operations to do (default infinity)\n\
1312 -O: use oplen (see -o flag) for every op (default random)\n\
1313 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1314 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1315 -W: mapped write operations DISabled\n\
1316 -R: read() system calls only (mapped reads disabled)\n\
1317 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1318 fname: this filename is REQUIRED (no default)\n");
1324 getnum(char *s, char **e)
1329 ret = strtol(s, e, 0);
1359 io_context_t io_ctx;
1365 ret = io_queue_init(QSZ, &io_ctx);
1367 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1375 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1377 struct io_event event;
1378 static struct timespec ts;
1379 struct iocb *iocbs[] = { &iocb };
1384 io_prep_pread(&iocb, fd, buf, len, offset);
1386 io_prep_pwrite(&iocb, fd, buf, len, offset);
1391 ret = io_submit(io_ctx, 1, iocbs);
1393 fprintf(stderr, "errcode=%d\n", ret);
1394 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1399 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1402 fprintf(stderr, "aio_rw: no events available\n");
1404 fprintf(stderr, "errcode=%d\n", -ret);
1405 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1410 if (len != event.res) {
1412 * The b0rked libaio defines event.res as unsigned.
1413 * However the kernel strucuture has it signed,
1414 * and it's used to pass negated error value.
1415 * Till the library is fixed use the temp var.
1417 res = (long)event.res;
1419 fprintf(stderr, "bad io length: %lu instead of %u\n",
1422 fprintf(stderr, "errcode=%ld\n", -res);
1423 fprintf(stderr, "aio_rw: async io failed: %s\n",
1434 * The caller expects error return in traditional libc
1435 * convention, i.e. -1 and the errno set to error.
1441 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1446 ret = __aio_rw(rw, fd, buf, len, offset);
1449 ret = read(fd, buf, len);
1451 ret = write(fd, buf, len);
1459 test_fallocate(int mode)
1461 #ifdef HAVE_LINUX_FALLOC_H
1464 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1466 warn("main: filesystem does not support "
1467 "fallocate mode 0x%x, disabling!\n", mode);
1478 main(int argc, char **argv)
1482 char goodfile[1024];
1484 struct stat statbuf;
1489 page_size = getpagesize();
1490 page_mask = page_size - 1;
1491 mmap_mask = page_mask;
1494 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1496 while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FHzCLN:OP:RS:WZ"))
1500 simulatedopcount = getnum(optarg, &endp);
1502 fprintf(stdout, "Will begin at operation %ld\n",
1504 if (simulatedopcount == 0)
1506 simulatedopcount -= 1;
1509 closeprob = getnum(optarg, &endp);
1512 "Chance of close/open is 1 in %d\n",
1524 maxfilelen = getnum(optarg, &endp);
1525 if (maxfilelen <= 0)
1529 monitorstart = getnum(optarg, &endp);
1530 if (monitorstart < 0)
1532 if (!endp || *endp++ != ':')
1534 monitorend = getnum(endp, &endp);
1537 if (monitorend == 0)
1538 monitorend = -1; /* aka infinity */
1544 maxoplen = getnum(optarg, &endp);
1549 progressinterval = getnum(optarg, &endp);
1550 if (progressinterval == 0)
1557 readbdy = getnum(optarg, &endp);
1562 style = getnum(optarg, &endp);
1563 if (style < 0 || style > 1)
1567 truncbdy = getnum(optarg, &endp);
1572 writebdy = getnum(optarg, &endp);
1586 debugstart = getnum(optarg, &endp);
1591 fallocate_calls = 0;
1594 punch_hole_calls = 0;
1597 zero_range_calls = 0;
1600 collapse_range_calls = 0;
1606 numops = getnum(optarg, &endp);
1614 strncpy(goodfile, optarg, sizeof(goodfile));
1615 strcat(goodfile, "/");
1616 strncpy(logfile, optarg, sizeof(logfile));
1617 strcat(logfile, "/");
1623 seed = getnum(optarg, &endp);
1625 seed = time(0) % 10000;
1627 fprintf(stdout, "Seed set to %d\n", seed);
1634 fprintf(stdout, "mapped writes DISABLED\n");
1637 o_direct = O_DIRECT;
1649 signal(SIGHUP, cleanup);
1650 signal(SIGINT, cleanup);
1651 signal(SIGPIPE, cleanup);
1652 signal(SIGALRM, cleanup);
1653 signal(SIGTERM, cleanup);
1654 signal(SIGXCPU, cleanup);
1655 signal(SIGXFSZ, cleanup);
1656 signal(SIGVTALRM, cleanup);
1657 signal(SIGUSR1, cleanup);
1658 signal(SIGUSR2, cleanup);
1660 initstate(seed, state, 256);
1663 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1668 if (fstat(fd, &statbuf)) {
1669 prterr("check_size: fstat");
1672 block_size = statbuf.st_blksize;
1675 xfs_flock64_t resv = { 0 };
1676 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1677 if (!platform_test_xfs_fd(fd)) {
1679 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1683 resv.l_len = maxfilelen;
1684 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1690 strncat(goodfile, fname, 256);
1691 strcat (goodfile, ".fsxgood");
1692 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1693 if (fsxgoodfd < 0) {
1697 strncat(logfile, fname, 256);
1698 strcat (logfile, ".fsxlog");
1699 fsxlogf = fopen(logfile, "w");
1700 if (fsxlogf == NULL) {
1712 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1713 if (file_size == (off_t)-1) {
1715 warn("main: lseek eof");
1718 ret = lseek(fd, (off_t)0, SEEK_SET);
1719 if (ret == (off_t)-1) {
1721 warn("main: lseek 0");
1725 original_buf = (char *) malloc(maxfilelen);
1726 for (i = 0; i < maxfilelen; i++)
1727 original_buf[i] = random() % 256;
1728 good_buf = (char *) malloc(maxfilelen + writebdy);
1729 good_buf = round_ptr_up(good_buf, writebdy, 0);
1730 memset(good_buf, '\0', maxfilelen);
1731 temp_buf = (char *) malloc(maxoplen + readbdy);
1732 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1733 memset(temp_buf, '\0', maxoplen);
1734 if (lite) { /* zero entire existing file */
1737 written = write(fd, good_buf, (size_t)maxfilelen);
1738 if (written != maxfilelen) {
1739 if (written == -1) {
1741 warn("main: error on write");
1743 warn("main: short write, 0x%x bytes instead "
1752 if (fallocate_calls)
1753 fallocate_calls = test_fallocate(0);
1754 if (punch_hole_calls)
1755 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE |
1756 FALLOC_FL_KEEP_SIZE);
1757 if (zero_range_calls)
1758 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1759 if (collapse_range_calls)
1760 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
1762 while (numops == -1 || numops--)
1769 prt("All operations completed A-OK!\n");