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.
36 #include <linux/falloc.h>
43 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
46 * A log entry is an operation and a bunch of arguments.
56 struct log_entry oplog[LOGSIZE]; /* the log */
57 int logptr = 0; /* current position in log */
58 int logcount = 0; /* total ops */
61 * The operation matrix is complex due to conditional execution of different
62 * features. Hence when we come to deciding what operation to run, we need to
63 * be careful in how we select the different operations. The active operations
64 * are mapped to numbers as follows:
75 * When mapped read/writes are disabled, they are simply converted to normal
76 * reads and writes. When fallocate/fpunch calls are disabled, they are
77 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
78 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
79 * operation modifier rather than an operation in itself.
81 * Because of the "lite" version, we also need to have different "maximum
82 * operation" defines to allow the ops to be selected correctly based on the
86 /* common operations */
93 /* !lite operations */
95 #define OP_FALLOCATE 5
96 #define OP_PUNCH_HOLE 6
99 /* operation modifiers */
100 #define OP_CLOSEOPEN 100
101 #define OP_SKIPPED 101
104 #define PAGE_SIZE getpagesize()
106 #define PAGE_MASK (PAGE_SIZE - 1)
108 char *original_buf; /* a pointer to the original data */
109 char *good_buf; /* a pointer to the correct data */
110 char *temp_buf; /* a pointer to the current data */
111 char *fname; /* name of our test file */
112 int fd; /* fd for our test file */
117 unsigned long testcalls = 0; /* calls to function "test" */
119 unsigned long simulatedopcount = 0; /* -b flag */
120 int closeprob = 0; /* -c flag */
121 int debug = 0; /* -d flag */
122 unsigned long debugstart = 0; /* -D flag */
123 int flush = 0; /* -f flag */
124 int do_fsync = 0; /* -y flag */
125 unsigned long maxfilelen = 256 * 1024; /* -l flag */
126 int sizechecks = 1; /* -n flag disables them */
127 int maxoplen = 64 * 1024; /* -o flag */
128 int quiet = 0; /* -q flag */
129 unsigned long progressinterval = 0; /* -p flag */
130 int readbdy = 1; /* -r flag */
131 int style = 0; /* -s flag */
132 int prealloc = 0; /* -x flag */
133 int truncbdy = 1; /* -t flag */
134 int writebdy = 1; /* -w flag */
135 long monitorstart = -1; /* -m flag */
136 long monitorend = -1; /* -m flag */
137 int lite = 0; /* -L flag */
138 long numops = -1; /* -N flag */
139 int randomoplen = 1; /* -O flag disables it */
140 int seed = 1; /* -S flag */
141 int mapped_writes = 1; /* -W flag disables */
142 int fallocate_calls = 1; /* -F flag disables */
143 int punch_hole_calls = 1; /* -H flag disables */
144 int mapped_reads = 1; /* -R flag disables it */
146 int o_direct; /* -Z */
153 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
156 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
157 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
159 #define fsxread(a,b,c,d) read(a,b,c)
160 #define fsxwrite(a,b,c,d) write(a,b,c)
163 FILE * fsxlogf = NULL;
167 static void *round_up(void *ptr, unsigned long align, unsigned long offset)
169 unsigned long ret = (unsigned long)ptr;
171 ret = ((ret + align - 1) & ~(align - 1));
177 vwarnc(int code, const char *fmt, va_list ap) {
178 fprintf(stderr, "fsx: ");
180 vfprintf(stderr, fmt, ap);
181 fprintf(stderr, ": ");
183 fprintf(stderr, "%s\n", strerror(code));
187 warn(const char * fmt, ...) {
190 vwarnc(errno, fmt, ap);
194 #define BUF_SIZE 1024
200 char buffer[BUF_SIZE];
203 vsnprintf(buffer, BUF_SIZE, fmt, args);
205 fprintf(stdout, buffer);
207 fprintf(fsxlogf, buffer);
213 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
218 log4(int operation, int arg0, int arg1, int arg2)
220 struct log_entry *le;
223 le->operation = operation;
225 le->operation = ~ le->operation;
231 if (logptr >= LOGSIZE)
240 struct log_entry *lp;
241 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
243 prt("LOG DUMP (%d total operations):\n", logcount);
244 if (logcount < LOGSIZE) {
251 for ( ; count > 0; count--) {
254 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
255 prt("%d(%d mod 256): ", opnum, opnum%256);
257 if ((closeopen = lp->operation < 0))
258 lp->operation = ~ lp->operation;
260 switch (lp->operation) {
262 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
263 lp->args[0], lp->args[0] + lp->args[1] - 1,
265 if (badoff >= lp->args[0] && badoff <
266 lp->args[0] + lp->args[1])
270 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
271 lp->args[0], lp->args[0] + lp->args[1] - 1,
273 if (badoff >= lp->args[0] && badoff <
274 lp->args[0] + lp->args[1])
278 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
279 lp->args[0], lp->args[0] + lp->args[1] - 1,
281 if (badoff >= lp->args[0] &&
282 badoff < lp->args[0] + lp->args[1])
286 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
287 lp->args[0], lp->args[0] + lp->args[1] - 1,
289 if (lp->args[0] > lp->args[2])
291 else if (lp->args[0] + lp->args[1] > lp->args[2])
293 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
294 badoff < lp->args[0] + lp->args[1])
298 down = lp->args[0] < lp->args[1];
299 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
300 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
301 if (badoff >= lp->args[!down] &&
302 badoff < lp->args[!!down])
306 /* 0: offset 1: length 2: where alloced */
307 prt("FALLOCATE %s\tfrom 0x%x to 0x%x",
308 falloc_type[lp->args[2]], lp->args[0], lp->args[0] + lp->args[1]);
309 if (badoff >= lp->args[0] &&
310 badoff < lp->args[0] + lp->args[1])
314 prt("PUNCH HOLE\t0x%x thru 0x%x\t(0x%x bytes)",
315 lp->args[0], lp->args[0] + lp->args[1] - 1,
317 if (badoff >= lp->args[0] && badoff <
318 lp->args[0] + lp->args[1])
322 prt("SKIPPED (no operation)");
325 prt("BOGUS LOG ENTRY (operation code = %d)!",
329 prt("\n\t\tCLOSE/OPEN");
339 save_buffer(char *buffer, off_t bufferlength, int fd)
342 ssize_t byteswritten;
344 if (fd <= 0 || bufferlength == 0)
347 if (bufferlength > SSIZE_MAX) {
348 prt("fsx flaw: overflow in save_buffer\n");
352 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
353 if (size_by_seek == (off_t)-1)
354 prterr("save_buffer: lseek eof");
355 else if (bufferlength > size_by_seek) {
356 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
357 (unsigned long long)bufferlength);
358 bufferlength = size_by_seek;
362 ret = lseek(fd, (off_t)0, SEEK_SET);
363 if (ret == (off_t)-1)
364 prterr("save_buffer: lseek 0");
366 byteswritten = write(fd, buffer, (size_t)bufferlength);
367 if (byteswritten != bufferlength) {
368 if (byteswritten == -1)
369 prterr("save_buffer write");
371 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
372 (unsigned)byteswritten,
373 (unsigned long long)bufferlength);
379 report_failure(int status)
385 save_buffer(good_buf, file_size, fsxgoodfd);
386 prt("Correct content saved for comparison\n");
387 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
396 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
397 *(((unsigned char *)(cp)) + 1)))
400 check_buffers(unsigned offset, unsigned size)
408 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
409 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
410 offset, size, fname);
411 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
413 c = good_buf[offset];
417 bad = short_at(&temp_buf[i]);
418 prt("0x%5x\t0x%04x\t0x%04x", offset,
419 short_at(&good_buf[offset]), bad);
420 op = temp_buf[offset & 1 ? i+1 : i];
423 prt("operation# (mod 256) for "
424 "the bad data may be %u\n",
425 ((unsigned)op & 0xff));
427 prt("operation# (mod 256) for "
428 "the bad data unknown, check"
429 " HOLE and EXTEND ops\n");
449 if (fstat(fd, &statbuf)) {
450 prterr("check_size: fstat");
451 statbuf.st_size = -1;
453 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
454 if (file_size != statbuf.st_size || file_size != size_by_seek) {
455 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
456 (unsigned long long)file_size,
457 (unsigned long long)statbuf.st_size,
458 (unsigned long long)size_by_seek);
465 check_trunc_hack(void)
469 ftruncate(fd, (off_t)0);
470 ftruncate(fd, (off_t)100000);
472 if (statbuf.st_size != (off_t)100000) {
473 prt("no extend on truncate! not posix!\n");
480 doflush(unsigned offset, unsigned size)
486 if (o_direct == O_DIRECT)
489 pg_offset = offset & mmap_mask;
490 map_size = pg_offset + size;
492 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
493 MAP_FILE | MAP_SHARED, fd,
494 (off_t)(offset - pg_offset))) == (char *)-1) {
495 prterr("doflush: mmap");
498 if (msync(p, map_size, MS_INVALIDATE) != 0) {
499 prterr("doflush: msync");
502 if (munmap(p, map_size) != 0) {
503 prterr("doflush: munmap");
509 doread(unsigned offset, unsigned size)
514 offset -= offset % readbdy;
516 size -= size % readbdy;
518 if (!quiet && testcalls > simulatedopcount && !o_direct)
519 prt("skipping zero size read\n");
520 log4(OP_SKIPPED, OP_READ, offset, size);
523 if (size + offset > file_size) {
524 if (!quiet && testcalls > simulatedopcount)
525 prt("skipping seek/read past end of file\n");
526 log4(OP_SKIPPED, OP_READ, offset, size);
530 log4(OP_READ, offset, size, 0);
532 if (testcalls <= simulatedopcount)
536 ((progressinterval && testcalls % progressinterval == 0) ||
538 (monitorstart == -1 ||
539 (offset + size > monitorstart &&
540 (monitorend == -1 || offset <= monitorend))))))
541 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
542 offset, offset + size - 1, size);
543 ret = lseek(fd, (off_t)offset, SEEK_SET);
544 if (ret == (off_t)-1) {
545 prterr("doread: lseek");
548 iret = fsxread(fd, temp_buf, size, offset);
551 prterr("doread: read");
553 prt("short read: 0x%x bytes instead of 0x%x\n",
557 check_buffers(offset, size);
562 check_eofpage(char *s, unsigned offset, char *p, int size)
564 unsigned long last_page, should_be_zero;
566 if (offset + size <= (file_size & ~page_mask))
569 * we landed in the last page of the file
570 * test to make sure the VM system provided 0's
571 * beyond the true end of the file mapping
572 * (as required by mmap def in 1996 posix 1003.1)
574 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
576 for (should_be_zero = last_page + (file_size & page_mask);
577 should_be_zero < last_page + page_size;
579 if (*(char *)should_be_zero) {
580 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
581 s, file_size - 1, should_be_zero & page_mask,
582 short_at(should_be_zero));
589 domapread(unsigned offset, unsigned size)
595 offset -= offset % readbdy;
597 if (!quiet && testcalls > simulatedopcount)
598 prt("skipping zero size read\n");
599 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
602 if (size + offset > file_size) {
603 if (!quiet && testcalls > simulatedopcount)
604 prt("skipping seek/read past end of file\n");
605 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
609 log4(OP_MAPREAD, offset, size, 0);
611 if (testcalls <= simulatedopcount)
615 ((progressinterval && testcalls % progressinterval == 0) ||
617 (monitorstart == -1 ||
618 (offset + size > monitorstart &&
619 (monitorend == -1 || offset <= monitorend))))))
620 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
621 offset, offset + size - 1, size);
623 pg_offset = offset & PAGE_MASK;
624 map_size = pg_offset + size;
626 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
627 (off_t)(offset - pg_offset))) == (char *)-1) {
628 prterr("domapread: mmap");
631 memcpy(temp_buf, p + pg_offset, size);
633 check_eofpage("Read", offset, p, size);
635 if (munmap(p, map_size) != 0) {
636 prterr("domapread: munmap");
640 check_buffers(offset, size);
645 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
648 good_buf[offset] = testcalls % 256;
650 good_buf[offset] += original_buf[offset];
657 dowrite(unsigned offset, unsigned size)
662 offset -= offset % writebdy;
664 size -= size % writebdy;
666 if (!quiet && testcalls > simulatedopcount && !o_direct)
667 prt("skipping zero size write\n");
668 log4(OP_SKIPPED, OP_WRITE, offset, size);
672 log4(OP_WRITE, offset, size, file_size);
674 gendata(original_buf, good_buf, offset, size);
675 if (file_size < offset + size) {
676 if (file_size < offset)
677 memset(good_buf + file_size, '\0', offset - file_size);
678 file_size = offset + size;
680 warn("Lite file size bug in fsx!");
685 if (testcalls <= simulatedopcount)
689 ((progressinterval && testcalls % progressinterval == 0) ||
691 (monitorstart == -1 ||
692 (offset + size > monitorstart &&
693 (monitorend == -1 || offset <= monitorend))))))
694 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
695 offset, offset + size - 1, size);
696 ret = lseek(fd, (off_t)offset, SEEK_SET);
697 if (ret == (off_t)-1) {
698 prterr("dowrite: lseek");
701 iret = fsxwrite(fd, good_buf + offset, size, offset);
704 prterr("dowrite: write");
706 prt("short write: 0x%x bytes instead of 0x%x\n",
712 prt("fsync() failed: %s\n", strerror(errno));
717 doflush(offset, size);
723 domapwrite(unsigned offset, unsigned size)
730 offset -= offset % writebdy;
732 if (!quiet && testcalls > simulatedopcount)
733 prt("skipping zero size write\n");
734 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
737 cur_filesize = file_size;
739 log4(OP_MAPWRITE, offset, size, 0);
741 gendata(original_buf, good_buf, offset, size);
742 if (file_size < offset + size) {
743 if (file_size < offset)
744 memset(good_buf + file_size, '\0', offset - file_size);
745 file_size = offset + size;
747 warn("Lite file size bug in fsx!");
752 if (testcalls <= simulatedopcount)
756 ((progressinterval && testcalls % progressinterval == 0) ||
758 (monitorstart == -1 ||
759 (offset + size > monitorstart &&
760 (monitorend == -1 || offset <= monitorend))))))
761 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
762 offset, offset + size - 1, size);
764 if (file_size > cur_filesize) {
765 if (ftruncate(fd, file_size) == -1) {
766 prterr("domapwrite: ftruncate");
770 pg_offset = offset & PAGE_MASK;
771 map_size = pg_offset + size;
773 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
774 MAP_FILE | MAP_SHARED, fd,
775 (off_t)(offset - pg_offset))) == (char *)-1) {
776 prterr("domapwrite: mmap");
779 memcpy(p + pg_offset, good_buf + offset, size);
780 if (msync(p, map_size, 0) != 0) {
781 prterr("domapwrite: msync");
785 check_eofpage("Write", offset, p, size);
787 if (munmap(p, map_size) != 0) {
788 prterr("domapwrite: munmap");
795 dotruncate(unsigned size)
797 int oldsize = file_size;
799 size -= size % truncbdy;
800 if (size > biggest) {
802 if (!quiet && testcalls > simulatedopcount)
803 prt("truncating to largest ever: 0x%x\n", size);
806 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
808 if (size > file_size)
809 memset(good_buf + file_size, '\0', size - file_size);
812 if (testcalls <= simulatedopcount)
815 if ((progressinterval && testcalls % progressinterval == 0) ||
816 (debug && (monitorstart == -1 || monitorend == -1 ||
817 size <= monitorend)))
818 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
819 if (ftruncate(fd, (off_t)size) == -1) {
820 prt("ftruncate1: %x\n", size);
821 prterr("dotruncate: ftruncate");
826 #ifdef FALLOC_FL_PUNCH_HOLE
828 do_punch_hole(unsigned offset, unsigned length)
833 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
836 if (!quiet && testcalls > simulatedopcount)
837 prt("skipping zero length punch hole\n");
838 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
842 if (file_size <= (loff_t)offset) {
843 if (!quiet && testcalls > simulatedopcount)
844 prt("skipping hole punch off the end of the file\n");
845 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
849 end_offset = offset + length;
851 log4(OP_PUNCH_HOLE, offset, length, 0);
853 if (testcalls <= simulatedopcount)
856 if ((progressinterval && testcalls % progressinterval == 0) ||
857 (debug && (monitorstart == -1 || monitorend == -1 ||
858 end_offset <= monitorend))) {
859 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
860 offset, offset+length, length);
862 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
863 prt("%punch hole: %x to %x\n", offset, length);
864 prterr("do_punch_hole: fallocate");
869 max_offset = offset < file_size ? offset : file_size;
870 max_len = max_offset + length <= file_size ? length :
871 file_size - max_offset;
872 memset(good_buf + max_offset, '\0', max_len);
877 do_punch_hole(unsigned offset, unsigned length)
884 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
886 do_preallocate(unsigned offset, unsigned length)
892 if (!quiet && testcalls > simulatedopcount)
893 prt("skipping zero length fallocate\n");
894 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
898 keep_size = random() % 2;
900 end_offset = keep_size ? 0 : offset + length;
902 if (end_offset > biggest) {
903 biggest = end_offset;
904 if (!quiet && testcalls > simulatedopcount)
905 prt("fallocating to largest ever: 0x%x\n", end_offset);
910 * 1: allocate past EOF
911 * 2: extending prealloc
912 * 3: interior prealloc
914 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 1 : 2) : 3);
916 if (end_offset > file_size) {
917 memset(good_buf + file_size, '\0', end_offset - file_size);
918 file_size = end_offset;
921 if (testcalls <= simulatedopcount)
924 if ((progressinterval && testcalls % progressinterval == 0) ||
925 (debug && (monitorstart == -1 || monitorend == -1 ||
926 end_offset <= monitorend)))
927 prt("%lu falloc\tfrom 0x%x to 0x%x\n", testcalls, offset, length);
928 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
929 prt("fallocate: %x to %x\n", offset, length);
930 prterr("do_preallocate: fallocate");
936 do_preallocate(unsigned offset, unsigned length)
947 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
948 prterr("writefileimage: lseek");
951 iret = write(fd, good_buf, file_size);
952 if ((off_t)iret != file_size) {
954 prterr("writefileimage: write");
956 prt("short write: 0x%x bytes instead of 0x%llx\n",
957 iret, (unsigned long long)file_size);
960 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
961 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
962 prterr("writefileimage: ftruncate");
971 if (testcalls <= simulatedopcount)
975 prt("%lu close/open\n", testcalls);
977 prterr("docloseopen: close");
980 fd = open(fname, O_RDWR|o_direct, 0);
982 prterr("docloseopen: open");
987 #define TRIM_OFF_LEN(off, len, size, zero_offset) \
989 if (!zero_offset || file_size) \
993 if (offset + len > size) \
994 len = size - offset; \
1000 unsigned long offset;
1001 unsigned long size = maxoplen;
1002 unsigned long rv = random();
1005 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1011 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1013 if (debugstart > 0 && testcalls >= debugstart)
1016 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1017 prt("%lu...\n", testcalls);
1021 size = random() % (maxoplen + 1);
1023 /* calculate appropriate op to run */
1025 op = rv % OP_MAX_LITE;
1027 op = rv % OP_MAX_FULL;
1039 if (!fallocate_calls) {
1040 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1045 if (!punch_hole_calls) {
1046 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1054 TRIM_OFF_LEN(offset, size, file_size, false);
1055 doread(offset, size);
1059 TRIM_OFF_LEN(offset, size, maxfilelen, true);
1060 dowrite(offset, size);
1064 TRIM_OFF_LEN(offset, size, file_size, false);
1065 domapread(offset, size);
1069 TRIM_OFF_LEN(offset, size, maxfilelen, true);
1070 domapwrite(offset, size);
1075 size = random() % maxfilelen;
1080 TRIM_OFF_LEN(offset, size, maxfilelen, true);
1081 do_preallocate(offset, size);
1085 TRIM_OFF_LEN(offset, size, maxfilelen, true);
1086 do_punch_hole(offset, size);
1089 prterr("test: unknown operation");
1095 if (sizechecks && testcalls > simulatedopcount)
1107 prt("signal %d\n", sig);
1108 prt("testcalls = %lu\n", testcalls);
1116 fprintf(stdout, "usage: %s",
1117 "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\
1118 -b opnum: beginning operation number (default 1)\n\
1119 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1120 -d: debug output for all operations\n\
1121 -f flush and invalidate cache after I/O\n\
1122 -l flen: the upper bound on file size (default 262144)\n\
1123 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1124 -n: no verifications of file size\n\
1125 -o oplen: the upper bound on operation size (default 65536)\n\
1126 -p progressinterval: debug output at specified operation interval\n\
1127 -q: quieter operation\n\
1128 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1129 -s style: 1 gives smaller truncates (default 0)\n\
1130 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1131 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1132 -x: preallocate file space before starting, XFS only (default 0)\n\
1133 -y synchronize changes to a file\n"
1136 " -A: Use the AIO system calls\n"
1138 " -D startingop: debug output starting at specified operation\n"
1140 " -F: Do not use fallocate (preallocation) calls\n"
1142 #ifdef FALLOC_FL_PUNCH_HOLE
1143 " -H: Do not use punch hole calls\n"
1145 " -L: fsxLite - no file creations & no file size changes\n\
1146 -N numops: total # operations to do (default infinity)\n\
1147 -O: use oplen (see -o flag) for every op (default random)\n\
1148 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1149 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1150 -W: mapped write operations DISabled\n\
1151 -R: read() system calls only (mapped reads disabled)\n\
1152 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1153 fname: this filename is REQUIRED (no default)\n");
1159 getnum(char *s, char **e)
1164 ret = strtol(s, e, 0);
1194 io_context_t io_ctx;
1200 ret = io_queue_init(QSZ, &io_ctx);
1202 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1210 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1212 struct io_event event;
1213 static struct timespec ts;
1214 struct iocb *iocbs[] = { &iocb };
1219 io_prep_pread(&iocb, fd, buf, len, offset);
1221 io_prep_pwrite(&iocb, fd, buf, len, offset);
1226 ret = io_submit(io_ctx, 1, iocbs);
1228 fprintf(stderr, "errcode=%d\n", ret);
1229 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1234 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1237 fprintf(stderr, "aio_rw: no events available\n");
1239 fprintf(stderr, "errcode=%d\n", -ret);
1240 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1245 if (len != event.res) {
1247 * The b0rked libaio defines event.res as unsigned.
1248 * However the kernel strucuture has it signed,
1249 * and it's used to pass negated error value.
1250 * Till the library is fixed use the temp var.
1252 res = (long)event.res;
1254 fprintf(stderr, "bad io length: %lu instead of %u\n",
1257 fprintf(stderr, "errcode=%ld\n", -res);
1258 fprintf(stderr, "aio_rw: async io failed: %s\n",
1269 * The caller expects error return in traditional libc
1270 * convention, i.e. -1 and the errno set to error.
1276 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1281 ret = __aio_rw(rw, fd, buf, len, offset);
1284 ret = read(fd, buf, len);
1286 ret = write(fd, buf, len);
1297 if (!lite && fallocate_calls) {
1298 if (fallocate(fd, 0, 0, 1) && errno == EOPNOTSUPP) {
1300 warn("main: filesystem does not support fallocate, disabling\n");
1301 fallocate_calls = 0;
1306 #else /* ! FALLOCATE */
1307 fallocate_calls = 0;
1315 #ifdef FALLOC_FL_PUNCH_HOLE
1316 if (!lite && punch_hole_calls) {
1317 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1318 0, 1) && errno == EOPNOTSUPP) {
1320 warn("main: filesystem does not support fallocate punch hole, disabling");
1321 punch_hole_calls = 0;
1325 #else /* ! PUNCH HOLE */
1326 punch_hole_calls = 0;
1331 main(int argc, char **argv)
1335 char goodfile[1024];
1341 page_size = getpagesize();
1342 page_mask = page_size - 1;
1343 mmap_mask = page_mask;
1346 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1348 while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FHLN:OP:RS:WZ"))
1352 simulatedopcount = getnum(optarg, &endp);
1354 fprintf(stdout, "Will begin at operation %ld\n",
1356 if (simulatedopcount == 0)
1358 simulatedopcount -= 1;
1361 closeprob = getnum(optarg, &endp);
1364 "Chance of close/open is 1 in %d\n",
1376 maxfilelen = getnum(optarg, &endp);
1377 if (maxfilelen <= 0)
1381 monitorstart = getnum(optarg, &endp);
1382 if (monitorstart < 0)
1384 if (!endp || *endp++ != ':')
1386 monitorend = getnum(endp, &endp);
1389 if (monitorend == 0)
1390 monitorend = -1; /* aka infinity */
1396 maxoplen = getnum(optarg, &endp);
1401 progressinterval = getnum(optarg, &endp);
1402 if (progressinterval == 0)
1409 readbdy = getnum(optarg, &endp);
1414 style = getnum(optarg, &endp);
1415 if (style < 0 || style > 1)
1419 truncbdy = getnum(optarg, &endp);
1424 writebdy = getnum(optarg, &endp);
1438 debugstart = getnum(optarg, &endp);
1443 fallocate_calls = 0;
1446 punch_hole_calls = 0;
1452 numops = getnum(optarg, &endp);
1460 strncpy(goodfile, optarg, sizeof(goodfile));
1461 strcat(goodfile, "/");
1462 strncpy(logfile, optarg, sizeof(logfile));
1463 strcat(logfile, "/");
1469 seed = getnum(optarg, &endp);
1471 seed = time(0) % 10000;
1473 fprintf(stdout, "Seed set to %d\n", seed);
1480 fprintf(stdout, "mapped writes DISABLED\n");
1483 o_direct = O_DIRECT;
1495 signal(SIGHUP, cleanup);
1496 signal(SIGINT, cleanup);
1497 signal(SIGPIPE, cleanup);
1498 signal(SIGALRM, cleanup);
1499 signal(SIGTERM, cleanup);
1500 signal(SIGXCPU, cleanup);
1501 signal(SIGXFSZ, cleanup);
1502 signal(SIGVTALRM, cleanup);
1503 signal(SIGUSR1, cleanup);
1504 signal(SIGUSR2, cleanup);
1506 initstate(seed, state, 256);
1509 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1516 xfs_flock64_t resv = { 0 };
1517 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1518 if (!platform_test_xfs_fd(fd)) {
1520 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1524 resv.l_len = maxfilelen;
1525 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1531 strncat(goodfile, fname, 256);
1532 strcat (goodfile, ".fsxgood");
1533 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1534 if (fsxgoodfd < 0) {
1538 strncat(logfile, fname, 256);
1539 strcat (logfile, ".fsxlog");
1540 fsxlogf = fopen(logfile, "w");
1541 if (fsxlogf == NULL) {
1553 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1554 if (file_size == (off_t)-1) {
1556 warn("main: lseek eof");
1559 ret = lseek(fd, (off_t)0, SEEK_SET);
1560 if (ret == (off_t)-1) {
1562 warn("main: lseek 0");
1566 original_buf = (char *) malloc(maxfilelen);
1567 for (i = 0; i < maxfilelen; i++)
1568 original_buf[i] = random() % 256;
1569 good_buf = (char *) malloc(maxfilelen + writebdy);
1570 good_buf = round_up(good_buf, writebdy, 0);
1571 memset(good_buf, '\0', maxfilelen);
1572 temp_buf = (char *) malloc(maxoplen + readbdy);
1573 temp_buf = round_up(temp_buf, readbdy, 0);
1574 memset(temp_buf, '\0', maxoplen);
1575 if (lite) { /* zero entire existing file */
1578 written = write(fd, good_buf, (size_t)maxfilelen);
1579 if (written != maxfilelen) {
1580 if (written == -1) {
1582 warn("main: error on write");
1584 warn("main: short write, 0x%x bytes instead "
1596 while (numops == -1 || numops--)
1603 prt("All operations completed A-OK!\n");