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:
73 * When mapped read/writes are disabled, they are simply converted to normal
74 * reads and writes. When fallocate/fpunch calls are disabled, they are
75 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
76 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
77 * operation modifier rather than an operation in itself.
79 * Because of the "lite" version, we also need to have different "maximum
80 * operation" defines to allow the ops to be selected correctly based on the
84 /* common operations */
91 /* !lite operations */
93 #define OP_FALLOCATE 5
94 #define OP_PUNCH_HOLE 6
97 /* operation modifiers */
98 #define OP_CLOSEOPEN 100
99 #define OP_SKIPPED 101
102 #define PAGE_SIZE getpagesize()
104 #define PAGE_MASK (PAGE_SIZE - 1)
106 char *original_buf; /* a pointer to the original data */
107 char *good_buf; /* a pointer to the correct data */
108 char *temp_buf; /* a pointer to the current data */
109 char *fname; /* name of our test file */
110 int fd; /* fd for our test file */
115 unsigned long testcalls = 0; /* calls to function "test" */
117 unsigned long simulatedopcount = 0; /* -b flag */
118 int closeprob = 0; /* -c flag */
119 int debug = 0; /* -d flag */
120 unsigned long debugstart = 0; /* -D flag */
121 int flush = 0; /* -f flag */
122 int do_fsync = 0; /* -y flag */
123 unsigned long maxfilelen = 256 * 1024; /* -l flag */
124 int sizechecks = 1; /* -n flag disables them */
125 int maxoplen = 64 * 1024; /* -o flag */
126 int quiet = 0; /* -q flag */
127 unsigned long progressinterval = 0; /* -p flag */
128 int readbdy = 1; /* -r flag */
129 int style = 0; /* -s flag */
130 int prealloc = 0; /* -x flag */
131 int truncbdy = 1; /* -t flag */
132 int writebdy = 1; /* -w flag */
133 long monitorstart = -1; /* -m flag */
134 long monitorend = -1; /* -m flag */
135 int lite = 0; /* -L flag */
136 long numops = -1; /* -N flag */
137 int randomoplen = 1; /* -O flag disables it */
138 int seed = 1; /* -S flag */
139 int mapped_writes = 1; /* -W flag disables */
140 int fallocate_calls = 1; /* -F flag disables */
141 int punch_hole_calls = 1; /* -H flag disables */
142 int mapped_reads = 1; /* -R flag disables it */
144 int o_direct; /* -Z */
151 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
154 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
155 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
157 #define fsxread(a,b,c,d) read(a,b,c)
158 #define fsxwrite(a,b,c,d) write(a,b,c)
161 FILE * fsxlogf = NULL;
165 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
167 unsigned long ret = (unsigned long)ptr;
169 ret = ((ret + align - 1) & ~(align - 1));
175 vwarnc(int code, const char *fmt, va_list ap) {
176 fprintf(stderr, "fsx: ");
178 vfprintf(stderr, fmt, ap);
179 fprintf(stderr, ": ");
181 fprintf(stderr, "%s\n", strerror(code));
185 warn(const char * fmt, ...) {
188 vwarnc(errno, fmt, ap);
192 #define BUF_SIZE 1024
198 char buffer[BUF_SIZE];
201 vsnprintf(buffer, BUF_SIZE, fmt, args);
203 fprintf(stdout, buffer);
205 fprintf(fsxlogf, buffer);
211 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
216 log4(int operation, int arg0, int arg1, int arg2)
218 struct log_entry *le;
221 le->operation = operation;
223 le->operation = ~ le->operation;
229 if (logptr >= LOGSIZE)
238 struct log_entry *lp;
239 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
241 prt("LOG DUMP (%d total operations):\n", logcount);
242 if (logcount < LOGSIZE) {
249 for ( ; count > 0; count--) {
252 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
253 prt("%d(%3d mod 256): ", opnum, opnum%256);
255 if ((closeopen = lp->operation < 0))
256 lp->operation = ~ lp->operation;
258 switch (lp->operation) {
260 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
261 lp->args[0], lp->args[0] + lp->args[1] - 1,
263 if (badoff >= lp->args[0] && badoff <
264 lp->args[0] + lp->args[1])
268 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
269 lp->args[0], lp->args[0] + lp->args[1] - 1,
271 if (badoff >= lp->args[0] && badoff <
272 lp->args[0] + lp->args[1])
276 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
277 lp->args[0], lp->args[0] + lp->args[1] - 1,
279 if (badoff >= lp->args[0] &&
280 badoff < lp->args[0] + lp->args[1])
284 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
285 lp->args[0], lp->args[0] + lp->args[1] - 1,
287 if (lp->args[0] > lp->args[2])
289 else if (lp->args[0] + lp->args[1] > lp->args[2])
291 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
292 badoff < lp->args[0] + lp->args[1])
296 down = lp->args[0] < lp->args[1];
297 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
298 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
299 if (badoff >= lp->args[!down] &&
300 badoff < lp->args[!!down])
304 /* 0: offset 1: length 2: where alloced */
305 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
306 lp->args[0], lp->args[0] + lp->args[1],
307 lp->args[1], falloc_type[lp->args[2]]);
308 if (badoff >= lp->args[0] &&
309 badoff < lp->args[0] + lp->args[1])
313 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
314 lp->args[0], lp->args[0] + lp->args[1] - 1,
316 if (badoff >= lp->args[0] && badoff <
317 lp->args[0] + lp->args[1])
321 prt("SKIPPED (no operation)");
324 prt("BOGUS LOG ENTRY (operation code = %d)!",
328 prt("\n\t\tCLOSE/OPEN");
338 save_buffer(char *buffer, off_t bufferlength, int fd)
341 ssize_t byteswritten;
343 if (fd <= 0 || bufferlength == 0)
346 if (bufferlength > SSIZE_MAX) {
347 prt("fsx flaw: overflow in save_buffer\n");
351 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
352 if (size_by_seek == (off_t)-1)
353 prterr("save_buffer: lseek eof");
354 else if (bufferlength > size_by_seek) {
355 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
356 (unsigned long long)bufferlength);
357 bufferlength = size_by_seek;
361 ret = lseek(fd, (off_t)0, SEEK_SET);
362 if (ret == (off_t)-1)
363 prterr("save_buffer: lseek 0");
365 byteswritten = write(fd, buffer, (size_t)bufferlength);
366 if (byteswritten != bufferlength) {
367 if (byteswritten == -1)
368 prterr("save_buffer write");
370 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
371 (unsigned)byteswritten,
372 (unsigned long long)bufferlength);
378 report_failure(int status)
384 save_buffer(good_buf, file_size, fsxgoodfd);
385 prt("Correct content saved for comparison\n");
386 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
395 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
396 *(((unsigned char *)(cp)) + 1)))
399 check_buffers(unsigned offset, unsigned size)
407 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
408 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
409 offset, size, fname);
410 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
412 c = good_buf[offset];
416 bad = short_at(&temp_buf[i]);
417 prt("0x%5x\t0x%04x\t0x%04x", offset,
418 short_at(&good_buf[offset]), bad);
419 op = temp_buf[offset & 1 ? i+1 : i];
422 prt("operation# (mod 256) for "
423 "the bad data may be %u\n",
424 ((unsigned)op & 0xff));
426 prt("operation# (mod 256) for "
427 "the bad data unknown, check"
428 " HOLE and EXTEND ops\n");
448 if (fstat(fd, &statbuf)) {
449 prterr("check_size: fstat");
450 statbuf.st_size = -1;
452 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
453 if (file_size != statbuf.st_size || file_size != size_by_seek) {
454 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
455 (unsigned long long)file_size,
456 (unsigned long long)statbuf.st_size,
457 (unsigned long long)size_by_seek);
464 check_trunc_hack(void)
468 ftruncate(fd, (off_t)0);
469 ftruncate(fd, (off_t)100000);
471 if (statbuf.st_size != (off_t)100000) {
472 prt("no extend on truncate! not posix!\n");
479 doflush(unsigned offset, unsigned size)
485 if (o_direct == O_DIRECT)
488 pg_offset = offset & mmap_mask;
489 map_size = pg_offset + size;
491 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
492 MAP_FILE | MAP_SHARED, fd,
493 (off_t)(offset - pg_offset))) == (char *)-1) {
494 prterr("doflush: mmap");
497 if (msync(p, map_size, MS_INVALIDATE) != 0) {
498 prterr("doflush: msync");
501 if (munmap(p, map_size) != 0) {
502 prterr("doflush: munmap");
508 doread(unsigned offset, unsigned size)
513 offset -= offset % readbdy;
515 size -= size % readbdy;
517 if (!quiet && testcalls > simulatedopcount && !o_direct)
518 prt("skipping zero size read\n");
519 log4(OP_SKIPPED, OP_READ, offset, size);
522 if (size + offset > file_size) {
523 if (!quiet && testcalls > simulatedopcount)
524 prt("skipping seek/read past end of file\n");
525 log4(OP_SKIPPED, OP_READ, offset, size);
529 log4(OP_READ, offset, size, 0);
531 if (testcalls <= simulatedopcount)
535 ((progressinterval && testcalls % progressinterval == 0) ||
537 (monitorstart == -1 ||
538 (offset + size > monitorstart &&
539 (monitorend == -1 || offset <= monitorend))))))
540 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
541 offset, offset + size - 1, size);
542 ret = lseek(fd, (off_t)offset, SEEK_SET);
543 if (ret == (off_t)-1) {
544 prterr("doread: lseek");
547 iret = fsxread(fd, temp_buf, size, offset);
550 prterr("doread: read");
552 prt("short read: 0x%x bytes instead of 0x%x\n",
556 check_buffers(offset, size);
561 check_eofpage(char *s, unsigned offset, char *p, int size)
563 unsigned long last_page, should_be_zero;
565 if (offset + size <= (file_size & ~page_mask))
568 * we landed in the last page of the file
569 * test to make sure the VM system provided 0's
570 * beyond the true end of the file mapping
571 * (as required by mmap def in 1996 posix 1003.1)
573 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
575 for (should_be_zero = last_page + (file_size & page_mask);
576 should_be_zero < last_page + page_size;
578 if (*(char *)should_be_zero) {
579 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
580 s, file_size - 1, should_be_zero & page_mask,
581 short_at(should_be_zero));
588 domapread(unsigned offset, unsigned size)
594 offset -= offset % readbdy;
596 if (!quiet && testcalls > simulatedopcount)
597 prt("skipping zero size read\n");
598 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
601 if (size + offset > file_size) {
602 if (!quiet && testcalls > simulatedopcount)
603 prt("skipping seek/read past end of file\n");
604 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
608 log4(OP_MAPREAD, offset, size, 0);
610 if (testcalls <= simulatedopcount)
614 ((progressinterval && testcalls % progressinterval == 0) ||
616 (monitorstart == -1 ||
617 (offset + size > monitorstart &&
618 (monitorend == -1 || offset <= monitorend))))))
619 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
620 offset, offset + size - 1, size);
622 pg_offset = offset & PAGE_MASK;
623 map_size = pg_offset + size;
625 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
626 (off_t)(offset - pg_offset))) == (char *)-1) {
627 prterr("domapread: mmap");
630 memcpy(temp_buf, p + pg_offset, size);
632 check_eofpage("Read", offset, p, size);
634 if (munmap(p, map_size) != 0) {
635 prterr("domapread: munmap");
639 check_buffers(offset, size);
644 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
647 good_buf[offset] = testcalls % 256;
649 good_buf[offset] += original_buf[offset];
656 dowrite(unsigned offset, unsigned size)
661 offset -= offset % writebdy;
663 size -= size % writebdy;
665 if (!quiet && testcalls > simulatedopcount && !o_direct)
666 prt("skipping zero size write\n");
667 log4(OP_SKIPPED, OP_WRITE, offset, size);
671 log4(OP_WRITE, offset, size, file_size);
673 gendata(original_buf, good_buf, offset, size);
674 if (file_size < offset + size) {
675 if (file_size < offset)
676 memset(good_buf + file_size, '\0', offset - file_size);
677 file_size = offset + size;
679 warn("Lite file size bug in fsx!");
684 if (testcalls <= simulatedopcount)
688 ((progressinterval && testcalls % progressinterval == 0) ||
690 (monitorstart == -1 ||
691 (offset + size > monitorstart &&
692 (monitorend == -1 || offset <= monitorend))))))
693 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
694 offset, offset + size - 1, size);
695 ret = lseek(fd, (off_t)offset, SEEK_SET);
696 if (ret == (off_t)-1) {
697 prterr("dowrite: lseek");
700 iret = fsxwrite(fd, good_buf + offset, size, offset);
703 prterr("dowrite: write");
705 prt("short write: 0x%x bytes instead of 0x%x\n",
711 prt("fsync() failed: %s\n", strerror(errno));
716 doflush(offset, size);
722 domapwrite(unsigned offset, unsigned size)
729 offset -= offset % writebdy;
731 if (!quiet && testcalls > simulatedopcount)
732 prt("skipping zero size write\n");
733 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
736 cur_filesize = file_size;
738 log4(OP_MAPWRITE, offset, size, 0);
740 gendata(original_buf, good_buf, offset, size);
741 if (file_size < offset + size) {
742 if (file_size < offset)
743 memset(good_buf + file_size, '\0', offset - file_size);
744 file_size = offset + size;
746 warn("Lite file size bug in fsx!");
751 if (testcalls <= simulatedopcount)
755 ((progressinterval && testcalls % progressinterval == 0) ||
757 (monitorstart == -1 ||
758 (offset + size > monitorstart &&
759 (monitorend == -1 || offset <= monitorend))))))
760 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
761 offset, offset + size - 1, size);
763 if (file_size > cur_filesize) {
764 if (ftruncate(fd, file_size) == -1) {
765 prterr("domapwrite: ftruncate");
769 pg_offset = offset & PAGE_MASK;
770 map_size = pg_offset + size;
772 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
773 MAP_FILE | MAP_SHARED, fd,
774 (off_t)(offset - pg_offset))) == (char *)-1) {
775 prterr("domapwrite: mmap");
778 memcpy(p + pg_offset, good_buf + offset, size);
779 if (msync(p, map_size, MS_SYNC) != 0) {
780 prterr("domapwrite: msync");
784 check_eofpage("Write", offset, p, size);
786 if (munmap(p, map_size) != 0) {
787 prterr("domapwrite: munmap");
794 dotruncate(unsigned size)
796 int oldsize = file_size;
798 size -= size % truncbdy;
799 if (size > biggest) {
801 if (!quiet && testcalls > simulatedopcount)
802 prt("truncating to largest ever: 0x%x\n", size);
805 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
807 if (size > file_size)
808 memset(good_buf + file_size, '\0', size - file_size);
811 if (testcalls <= simulatedopcount)
814 if ((progressinterval && testcalls % progressinterval == 0) ||
815 (debug && (monitorstart == -1 || monitorend == -1 ||
816 size <= monitorend)))
817 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
818 if (ftruncate(fd, (off_t)size) == -1) {
819 prt("ftruncate1: %x\n", size);
820 prterr("dotruncate: ftruncate");
825 #ifdef FALLOC_FL_PUNCH_HOLE
827 do_punch_hole(unsigned offset, unsigned length)
832 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
835 if (!quiet && testcalls > simulatedopcount)
836 prt("skipping zero length punch hole\n");
837 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
841 if (file_size <= (loff_t)offset) {
842 if (!quiet && testcalls > simulatedopcount)
843 prt("skipping hole punch off the end of the file\n");
844 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
848 end_offset = offset + length;
850 log4(OP_PUNCH_HOLE, offset, length, 0);
852 if (testcalls <= simulatedopcount)
855 if ((progressinterval && testcalls % progressinterval == 0) ||
856 (debug && (monitorstart == -1 || monitorend == -1 ||
857 end_offset <= monitorend))) {
858 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
859 offset, offset+length, length);
861 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
862 prt("%punch hole: %x to %x\n", offset, length);
863 prterr("do_punch_hole: fallocate");
868 max_offset = offset < file_size ? offset : file_size;
869 max_len = max_offset + length <= file_size ? length :
870 file_size - max_offset;
871 memset(good_buf + max_offset, '\0', max_len);
876 do_punch_hole(unsigned offset, unsigned length)
882 #ifdef HAVE_LINUX_FALLOC_H
883 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
885 do_preallocate(unsigned offset, unsigned length)
891 if (!quiet && testcalls > simulatedopcount)
892 prt("skipping zero length fallocate\n");
893 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
897 keep_size = random() % 2;
899 end_offset = keep_size ? 0 : offset + length;
901 if (end_offset > biggest) {
902 biggest = end_offset;
903 if (!quiet && testcalls > simulatedopcount)
904 prt("fallocating to largest ever: 0x%x\n", end_offset);
908 * last arg matches fallocate string array index in logdump:
909 * 0: allocate past EOF
910 * 1: extending prealloc
911 * 2: interior prealloc
913 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
915 if (end_offset > file_size) {
916 memset(good_buf + file_size, '\0', end_offset - file_size);
917 file_size = end_offset;
920 if (testcalls <= simulatedopcount)
923 if ((progressinterval && testcalls % progressinterval == 0) ||
924 (debug && (monitorstart == -1 || monitorend == -1 ||
925 end_offset <= monitorend)))
926 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
927 offset, offset + length, 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) \
993 if ((off) + (len) > (size)) \
994 (len) = (size) - (off); \
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);
1055 doread(offset, size);
1059 TRIM_OFF_LEN(offset, size, maxfilelen);
1060 dowrite(offset, size);
1064 TRIM_OFF_LEN(offset, size, file_size);
1065 domapread(offset, size);
1069 TRIM_OFF_LEN(offset, size, maxfilelen);
1070 domapwrite(offset, size);
1075 size = random() % maxfilelen;
1080 TRIM_OFF_LEN(offset, size, maxfilelen);
1081 do_preallocate(offset, size);
1085 TRIM_OFF_LEN(offset, size, file_size);
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"
1139 #ifdef HAVE_LINUX_FALLOC_H
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);
1296 #ifdef HAVE_LINUX_FALLOC_H
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 /* ! HAVE_LINUX_FALLOC_H */
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_ptr_up(good_buf, writebdy, 0);
1571 memset(good_buf, '\0', maxfilelen);
1572 temp_buf = (char *) malloc(maxoplen + readbdy);
1573 temp_buf = round_ptr_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");