2 * Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserverd.
3 * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
5 * Copyright (c) 2012, Intel Corporation.
7 * @APPLE_LICENSE_HEADER_START@
9 * The contents of this file constitute Original Code as defined in and
10 * are subject to the Apple Public Source License Version 1.1 (the
11 * "License"). You may not use this file except in compliance with the
12 * License. Please obtain a copy of the License at
13 * http://www.apple.com/publicsource and read it before using this file.
15 * This Original Code and all software distributed under the License are
16 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
20 * License for the specific language governing rights and limitations
23 * @APPLE_LICENSE_HEADER_END@
26 * Author: Avadis Tevanian, Jr.
28 * File system exerciser.
30 * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
32 * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
34 * Small changes to work under Linux -- davej.
36 * Sundry porting patches from Guy Harris 12/2001
37 * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.1 2001/12/20 04:15:57 jkh Exp $
39 * Checks for mmap last-page zero fill.
41 * Add multi-file testing feature -- Zach Brown <zab@clusterfs.com>
43 * Add random preallocation calls - Eric Sandeen <sandeen@redhat.com>
45 * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.2 2003/04/23 23:42:23 jkh Exp $
46 * $DragonFly: src/test/stress/fsx/fsx.c,v 1.2 2005/05/02 19:31:56 dillon Exp $
52 #include <sys/types.h>
54 #if defined(_UWIN) || defined(__linux__)
55 # include <sys/param.h>
75 #include <libcfs/util/string.h>
79 * Each test run will work with one or more separate file descriptors for the
80 * same file. This allows testing cache coherency across multiple mountpoints
81 * of the same network filesystem on a single client.
87 } *test_files = NULL, *tf;
91 enum fd_iteration_policy {
97 int fd_policy = FD_RANDOM;
101 * A log entry is an operation and a bunch of arguments.
108 const struct test_file *tf;
111 #define LOGSIZE 100000
113 struct log_entry oplog[LOGSIZE]; /* the log */
114 int logptr; /* current position in log */
115 int logcount; /* total ops */
123 /* common operations */
127 #define OP_MAPWRITE 3
128 #define OP_MAX_LITE 4
130 /* !lite operations */
131 #define OP_TRUNCATE 4
132 #define OP_FALLOCATE 5
133 #define OP_PUNCH_HOLE 6
134 #define OP_ZERO_RANGE 7
135 #define OP_CLOSEOPEN 8
136 #define OP_MAX_FULL 9
138 #define OP_SKIPPED 101
139 #define OP_DIRECT O_DIRECT
141 #ifndef FALLOC_FL_PUNCH_HOLE
142 #define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */
145 #ifndef FALLOC_FL_KEEP_SIZE
146 #define FALLOC_FL_KEEP_SIZE 0x01 /* default is extend size */
149 #ifndef FALLOC_FL_ZERO_RANGE
150 #define FALLOC_FL_ZERO_RANGE 0x10 /* convert range to zeros */
154 char *original_buf; /* a pointer to the original data */
155 char *good_buf; /* a pointer to the correct data */
156 char *temp_buf; /* a pointer to the current data */
157 char *fname; /* name of our test file */
158 char logfile[PATH_MAX]; /* name of our log file */
159 char goodfile[PATH_MAX]; /* name of our test file */
161 struct timeval tv; /* time current operation started */
165 unsigned long testcalls; /* calls to function "test" */
167 long simulatedopcount; /* -b flag */
168 int closeprob; /* -c flag */
169 int debug ; /* -d flag */
170 long debugstart; /* -D flag */
171 int flush; /* -f flag */
172 int do_fsync; /* -y flag */
173 long maxfilelen = 256 * 1024; /* -l flag */
174 int sizechecks = 1; /* -n flag disables them */
175 int maxoplen = 64 * 1024; /* -o flag */
176 int quiet; /* -q flag */
177 long progressinterval; /* -p flag */
178 int readbdy = 1; /* -r flag */
179 int style; /* -s flag */
180 int truncbdy = 1; /* -t flag */
181 int writebdy = 1; /* -w flag */
182 long monitorstart = -1; /* -m flag */
183 long monitorend = -1; /* -m flag */
184 int lite; /* -L flag */
185 long numops = -1; /* -N flag */
186 int randomoplen = 1; /* -O flag disables it */
187 int seed = 1; /* -S flag */
188 int mapped_writes = 1; /* -W flag disables */
189 int fallocate_calls = 1; /* -F flag disables */
190 int punch_hole_calls = 1; /* -H flag disables */
191 int zero_range_calls = 1; /* -z flag disables */
192 int mapped_reads = 1; /* -R flag disables it */
194 int o_direct; /* -Z */
204 vwarnc(code, fmt, ap)
209 fprintf(stderr, "fsx: ");
211 vfprintf(stderr, fmt, ap);
212 fprintf(stderr, ": ");
214 fprintf(stderr, "%s\n", strerror(code));
218 __attribute__((format(__printf__, 1, 2)))
219 warn(const char *fmt, ...)
224 vwarnc(errno, fmt, ap);
229 __attribute__((format(__printf__, 1, 2)))
235 vfprintf(stdout, fmt, args);
240 vfprintf(fsxlogf, fmt, args);
246 * prterr() is now a macro. It internally calls ptrerr_func()
247 * which transparently handles passing of function name.
248 * This version also keeps checkpatch happy.
251 ptrerr_func(const char *func, const char *prefix)
253 prt("%s: %s%s%s\n", func, prefix, prefix ? ": " : "", strerror(errno));
255 #define prterr(prefix) ptrerr_func(__func__, prefix)
258 log4(int operation, int arg0, int arg1, int arg2)
260 struct log_entry *le;
263 le->operation = operation;
267 gettimeofday(&tv, NULL);
272 if (logptr >= LOGSIZE)
277 fill_tf_buf(const struct test_file *tf)
279 static int max_tf_len;
280 static char tf_buf[32];
282 if (fd_policy == FD_SINGLE)
286 max_tf_len = scnprintf(tf_buf, sizeof(tf_buf) - 1,
287 "%u", num_test_files - 1);
289 snprintf(tf_buf, sizeof(tf_buf), "[%0*lu]", max_tf_len,
290 (unsigned long)(tf - test_files));
299 struct log_entry *lp;
300 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
302 prt("LOG DUMP (%d total operations):\n", logcount);
303 if (logcount < LOGSIZE) {
310 for ( ; count > 0; count--) {
313 opnum = i + 1 + (logcount / LOGSIZE) * LOGSIZE;
315 prt("%d%s: %lu.%06u ", opnum, fill_tf_buf(lp->tf),
316 lp->tv.tv_sec, (int)lp->tv.tv_usec);
318 switch (lp->operation) {
320 prt("MAPREAD 0x%05x thru 0x%05x (0x%05x 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("MAPWRITE 0x%05x thru 0x%05x (0x%05x 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])
336 case OP_READ + OP_DIRECT:
337 prt("READ%s 0x%05x thru 0x%05x (0x%05x bytes)",
338 lp->operation & OP_DIRECT ? "_OD" : " ",
339 lp->args[0], lp->args[0] + lp->args[1] - 1,
341 if (badoff >= lp->args[0] &&
342 badoff < lp->args[0] + lp->args[1])
346 case OP_WRITE + OP_DIRECT:
347 prt("WRITE%s 0x%05x thru 0x%05x (0x%05x bytes)",
348 lp->operation & OP_DIRECT ? "_OD" : " ",
349 lp->args[0], lp->args[0] + lp->args[1] - 1,
351 if (lp->args[0] > lp->args[2])
353 else if (lp->args[0] + lp->args[1] > lp->args[2])
355 if ((badoff >= lp->args[0] || badoff >= lp->args[2]) &&
356 badoff < lp->args[0] + lp->args[1])
360 down = lp->args[0] < lp->args[1];
361 prt("TRUNC%s 0x%05x to 0x%05x",
362 down ? "_DN" : "_UP", lp->args[1], lp->args[0]);
363 if (badoff >= lp->args[!down] &&
364 badoff < lp->args[!!down])
368 /* 0: offset 1: length 2: where alloced */
369 prt("FALLOC 0x%05x thru 0x%05x\t(0x%05x bytes)%s",
370 lp->args[0], lp->args[0] + lp->args[1],
371 lp->args[1], falloc_type[lp->args[2]]);
372 if (badoff >= lp->args[0] &&
373 badoff < lp->args[0] + lp->args[1])
377 prt("PUNCH 0x%05x thru 0x%05x\t(0x%05x bytes)",
378 lp->args[0], lp->args[0] + lp->args[1] - 1,
380 if (badoff >= lp->args[0] && badoff <
381 lp->args[0] + lp->args[1])
385 prt("ZERO 0x%05x thru 0x%05x\t(0x%05x bytes)",
386 lp->args[0], lp->args[0] + lp->args[1] - 1,
388 if (badoff >= lp->args[0] && badoff <
389 lp->args[0] + lp->args[1])
393 case OP_CLOSEOPEN + OP_DIRECT:
395 lp->operation & OP_DIRECT ? "_OD" : " ");
398 prt("SKIPPED (no operation)");
401 prt("BOGUS LOG ENTRY (operation code = %d)!",
412 save_buffer(char *buffer, off_t bufferlength, int fd)
415 ssize_t byteswritten;
417 if (fd <= 0 || bufferlength == 0)
420 if (bufferlength > INT_MAX) {
421 prt("fsx flaw: overflow in %s\n", __func__);
425 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
427 if (size_by_seek == (off_t)-1) {
429 } else if (bufferlength > size_by_seek) {
430 warn("%s: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n",
431 __func__, (unsigned long long)size_by_seek,
432 (unsigned long long)bufferlength);
433 bufferlength = size_by_seek;
437 ret = lseek(fd, (off_t)0, SEEK_SET);
438 if (ret == (off_t)-1)
441 byteswritten = write(fd, buffer, (size_t)bufferlength);
442 if (byteswritten != bufferlength) {
443 if (byteswritten == -1)
446 warn("%s: short write, 0x%x bytes instead of 0x%llx\n",
447 __func__, (unsigned int)byteswritten,
448 (unsigned long long)bufferlength);
453 report_failure(int status)
459 save_buffer(good_buf, file_size, fsxgoodfd);
460 prt("Correct content saved for comparison\n");
461 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
469 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
470 *(((unsigned char *)(cp)) + 1)))
473 check_buffers(unsigned int offset, unsigned int size)
479 unsigned int bad = 0;
481 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
482 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
484 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
486 c = good_buf[offset];
490 bad = short_at(&temp_buf[i]);
491 prt("%#07x\t%#06x\t%#06x", offset,
492 short_at(&good_buf[offset]), bad);
493 op = temp_buf[offset & 1 ? i + 1 : i];
505 prt("operation# (mod 256) for the bad data may be %u\n",
506 ((unsigned int)op & 0xff));
508 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
510 prt("????????????????\n");
519 unsigned int index = 0;
532 prt("unknown policy");
536 return &test_files[index % num_test_files];
540 assign_fd_policy(char *policy)
542 if (!strcmp(policy, "random")) {
543 fd_policy = FD_RANDOM;
544 } else if (!strcmp(policy, "rotate")) {
545 fd_policy = FD_ROTATE;
547 prt("unknown -I policy: '%s'\n", policy);
555 struct test_file *tf = get_tf();
560 static const char *my_basename(const char *path)
562 char *c = strrchr(path, '/');
564 return c ? c++ : path;
568 open_test_files(char **argv, int argc)
570 struct test_file *tf;
573 num_test_files = argc;
574 if (num_test_files == 1)
575 fd_policy = FD_SINGLE;
577 test_files = calloc(num_test_files, sizeof(*test_files));
579 prterr("reallocating space for test files");
583 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
585 tf->o_direct = (random() % (o_direct + 1)) ? OP_DIRECT : 0;
586 tf->fd = open(tf->path,
587 O_RDWR | (lite ? 0 : O_CREAT | O_TRUNC) |
595 if (quiet || fd_policy == FD_SINGLE)
598 for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
599 prt("fd %d: %s\n", i, tf->path);
603 close_test_files(void)
606 struct test_file *tf;
608 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
623 if (fstat(fd, &statbuf)) {
625 statbuf.st_size = -1;
627 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
628 if (file_size != statbuf.st_size || file_size != size_by_seek) {
629 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
630 (unsigned long long)file_size,
631 (unsigned long long)statbuf.st_size,
632 (unsigned long long)size_by_seek);
638 check_trunc_hack(void)
643 /* should not ignore ftruncate(2)'s return value */
644 if (ftruncate(fd, (off_t)0) < 0) {
645 prterr("trunc_hack: ftruncate(0)");
648 if (ftruncate(fd, (off_t)100000) < 0) {
649 prterr("trunc_hack: ftruncate(100000)");
652 if (fstat(fd, &statbuf)) {
653 prterr("trunc_hack: fstat");
654 statbuf.st_size = -1;
656 if (statbuf.st_size != (off_t)100000) {
657 prt("no extend on truncate! not posix!\n");
660 if (ftruncate(fd, 0) < 0) {
661 prterr("trunc_hack: ftruncate(0) (2nd call)");
667 output_line(struct test_file *tf, int op, unsigned int offset,
672 [OP_WRITE] = "write",
673 [OP_TRUNCATE] = "trunc from",
674 [OP_MAPREAD] = "mapread",
675 [OP_MAPWRITE] = "mapwrite",
676 [OP_READ + OP_DIRECT] = "read_OD",
677 [OP_WRITE + OP_DIRECT] = "write_OD",
678 [OP_FALLOCATE] = "fallocate",
679 [OP_PUNCH_HOLE] = "punch from",
684 ((progressinterval && testcalls % progressinterval == 0) ||
685 (debug && (monitorstart == -1 ||
686 (offset + size > monitorstart &&
687 (monitorend == -1 || offset <= monitorend)))))))
690 prt("%06lu%s %lu.%06u %-10s %#08x %s %#08x\t(0x%x bytes)\n",
691 testcalls, fill_tf_buf(tf), tv.tv_sec, (int)tv.tv_usec,
692 ops[op], offset, op == OP_TRUNCATE || op == OP_PUNCH_HOLE ?
693 " to " : "thru", offset + size - 1,
694 (int)size < 0 ? -(int)size : size);
697 void output_debug(unsigned int offset, unsigned int size, const char *what)
701 if (!quiet && (debug > 1 && (monitorstart == -1 ||
702 (offset + size >= monitorstart &&
703 (monitorend == -1 || offset <= monitorend))))) {
704 gettimeofday(&t, NULL);
705 prt(" %lu.%06u %s\n", t.tv_sec, (int)t.tv_usec, what);
710 doflush(unsigned int offset, unsigned int size)
712 unsigned int pg_offset;
713 unsigned int map_size;
715 struct test_file *tf = get_tf();
721 pg_offset = offset & page_mask;
722 map_size = pg_offset + size;
724 p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
725 MAP_FILE | MAP_SHARED, fd,
726 (off_t)(offset - pg_offset));
727 if (p == (char *)-1) {
731 if (msync(p, map_size, MS_INVALIDATE) != 0) {
735 if (munmap(p, map_size) != 0) {
739 output_debug(offset, size, "flush done");
743 doread(unsigned int offset, unsigned int size)
747 struct test_file *tf = get_tf();
750 offset -= offset % readbdy;
752 size -= size % readbdy;
755 if (!quiet && testcalls > simulatedopcount && !tf->o_direct)
756 prt("skipping zero size read\n");
757 log4(OP_SKIPPED, OP_READ, offset, size);
760 if (size + offset > file_size) {
761 if (!quiet && testcalls > simulatedopcount)
762 prt("skipping seek/read past end of file\n");
763 log4(OP_SKIPPED, OP_READ, offset, size);
767 log4(OP_READ + tf->o_direct, offset, size, 0);
769 if (testcalls <= simulatedopcount)
772 output_line(tf, OP_READ + tf->o_direct, offset, size);
774 ret = lseek(fd, (off_t)offset, SEEK_SET);
775 if (ret == (off_t)-1) {
779 iret = read(fd, temp_buf, size);
780 output_debug(offset, size, "read done");
785 prt("short read: 0x%x bytes instead of 0x%x\n",
789 check_buffers(offset, size);
793 check_eofpage(char *s, unsigned int offset, char *p, int size)
795 long last_page, should_be_zero;
797 if (offset + size <= (file_size & ~page_mask))
800 * we landed in the last page of the file
801 * test to make sure the VM system provided 0's
802 * beyond the true end of the file mapping
803 * (as required by mmap def in 1996 posix 1003.1)
805 last_page = ((long)p + (offset & page_mask) + size) & ~page_mask;
807 for (should_be_zero = last_page + (file_size & page_mask);
808 should_be_zero < last_page + page_size;
810 if (*(char *)should_be_zero) {
811 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%lx is 0x%04x\n",
812 s, (long long)file_size - 1,
813 should_be_zero & page_mask,
814 short_at(should_be_zero));
820 domapread(unsigned int offset, unsigned int size)
822 unsigned int pg_offset;
823 unsigned int map_size;
827 offset -= offset % readbdy;
831 if (!quiet && testcalls > simulatedopcount)
832 prt("skipping zero size read\n");
833 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
836 if (size + offset > file_size) {
837 if (!quiet && testcalls > simulatedopcount)
838 prt("skipping seek/read past end of file\n");
839 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
843 log4(OP_MAPREAD, offset, size, 0);
845 if (testcalls <= simulatedopcount)
848 output_line(tf, OP_MAPREAD, offset, size);
850 pg_offset = offset & page_mask;
851 map_size = pg_offset + size;
853 p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
854 (off_t)(offset - pg_offset));
855 if (p == MAP_FAILED) {
859 output_debug(offset, size, "mmap done");
860 if (setjmp(jmpbuf) == 0) {
862 memcpy(temp_buf, p + pg_offset, size);
863 check_eofpage("Read", offset, p, size);
866 report_failure(1901);
868 output_debug(offset, size, "memcpy done");
869 if (munmap(p, map_size) != 0) {
873 output_debug(offset, size, "munmap done");
875 check_buffers(offset, size);
879 gendata(char *original_buf, char *good_buf, unsigned int offset,
883 good_buf[offset] = testcalls % 256;
885 good_buf[offset] += original_buf[offset];
891 dowrite(unsigned int offset, unsigned int size)
899 offset -= offset % writebdy;
901 size -= size % writebdy;
903 if (!quiet && testcalls > simulatedopcount && !tf->o_direct)
904 prt("skipping zero size write\n");
905 log4(OP_SKIPPED, OP_WRITE, offset, size);
909 log4(OP_WRITE + tf->o_direct, offset, size, file_size);
911 gendata(original_buf, good_buf, offset, size);
912 if (file_size < offset + size) {
913 if (file_size < offset)
914 memset(good_buf + file_size, '\0', offset - file_size);
915 file_size = offset + size;
917 warn("Lite file size bug in fsx!");
922 if (testcalls <= simulatedopcount)
925 output_line(tf, OP_WRITE + tf->o_direct, offset, size);
927 ret = lseek(fd, (off_t)offset, SEEK_SET);
928 if (ret == (off_t)-1) {
932 iret = write(fd, good_buf + offset, size);
933 output_debug(offset, size, "write done");
938 prt("short write: 0x%x bytes instead of 0x%x\n",
944 prt("fsync() failed: %s\n", strerror(errno));
947 output_debug(offset, size, "fsync done");
950 doflush(offset, size);
951 output_debug(offset, size, "flush done");
956 domapwrite(unsigned int offset, unsigned int size)
958 unsigned int pg_offset;
959 unsigned int map_size;
966 offset -= offset % writebdy;
968 if (!quiet && testcalls > simulatedopcount)
969 prt("skipping zero size write\n");
970 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
973 cur_filesize = file_size;
975 log4(OP_MAPWRITE, offset, size, 0);
977 gendata(original_buf, good_buf, offset, size);
978 if (file_size < offset + size) {
979 if (file_size < offset)
980 memset(good_buf + file_size, '\0', offset - file_size);
981 file_size = offset + size;
983 warn("Lite file size bug in fsx!");
988 if (testcalls <= simulatedopcount)
991 output_line(tf, OP_MAPWRITE, offset, size);
993 if (file_size > cur_filesize) {
994 if (ftruncate(fd, file_size) == -1) {
998 output_debug(offset, size, "truncate done");
1000 pg_offset = offset & page_mask;
1001 map_size = pg_offset + size;
1003 p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
1004 fd, (off_t)(offset - pg_offset));
1005 if (p == MAP_FAILED) {
1007 report_failure(202);
1009 output_debug(offset, map_size, "mmap done");
1010 if (setjmp(jmpbuf) == 0) {
1012 memcpy(p + pg_offset, good_buf + offset, size);
1013 if (msync(p, map_size, MS_SYNC) != 0) {
1015 report_failure(203);
1017 check_eofpage("Write", offset, p, size);
1020 report_failure(2021);
1022 output_debug(offset, map_size, "msync done");
1023 if (munmap(p, map_size) != 0) {
1025 report_failure(204);
1027 output_debug(offset, map_size, "munmap done");
1031 dotruncate(unsigned int size)
1033 int oldsize = file_size;
1038 size -= size % truncbdy;
1039 if (size > biggest) {
1041 if (!quiet && testcalls > simulatedopcount)
1042 prt("truncating to largest ever: 0x%x\n", size);
1045 log4(OP_TRUNCATE, size, (unsigned int)file_size, 0);
1047 if (size > file_size)
1048 memset(good_buf + file_size, '\0', size - file_size);
1051 if (testcalls <= simulatedopcount)
1054 output_line(tf, OP_TRUNCATE, oldsize, size - oldsize);
1056 if (ftruncate(fd, (off_t)size) == -1) {
1057 prt("ftruncate: 0x%x\n", size);
1058 prterr("ftruncate");
1059 report_failure(160);
1061 output_debug(size, 0, "truncate done");
1065 do_punch_hole(unsigned int offset, unsigned int length)
1069 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
1075 if (!quiet && testcalls > simulatedopcount) {
1076 prt("skipping zero length punch hole\n");
1077 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
1082 if (file_size <= (loff_t)offset) {
1083 if (!quiet && testcalls > simulatedopcount) {
1084 prt("skipping hole punch off the end of the file\n");
1085 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
1090 log4(OP_PUNCH_HOLE, offset, length, 0);
1092 if (testcalls <= simulatedopcount)
1095 output_line(tf, OP_PUNCH_HOLE, offset, length);
1096 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1097 prt("punch hole: %x to %x\n", offset, length);
1098 prterr("fallocate");
1099 report_failure(161);
1101 output_debug(offset, length, "zero_range done");
1103 max_offset = offset < file_size ? offset : file_size;
1104 max_len = max_offset + length <= file_size ? length :
1105 file_size - max_offset;
1106 memset(good_buf + max_offset, '\0', max_len);
1110 do_zero_range(unsigned int offset, unsigned int length)
1112 unsigned int end_offset;
1113 int mode = FALLOC_FL_ZERO_RANGE;
1120 if (!quiet && testcalls > simulatedopcount) {
1121 prt("skipping zero length zero range\n");
1122 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
1127 keep_size = random() % 2;
1129 end_offset = keep_size ? 0 : offset + length;
1131 if (end_offset > biggest) {
1132 biggest = end_offset;
1133 if (!quiet && testcalls > simulatedopcount)
1134 prt("zero_range to largest ever: 0x%x\n", end_offset);
1138 * last arg matches fallocate string array index in logdump:
1139 * 0: allocate past EOF
1140 * 1: extending prealloc
1141 * 2: interior prealloc
1143 log4(OP_ZERO_RANGE, offset, length,
1144 (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1146 if (testcalls <= simulatedopcount)
1149 output_line(tf, OP_TRUNCATE, offset, length);
1151 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1152 prt("pzero range: %x to %x\n", offset, length);
1153 prterr("fallocate");
1154 report_failure(161);
1156 output_debug(offset, length, "zero_range done");
1158 memset(good_buf + offset, '\0', length);
1162 * fallocate is basically a no-op unless extending,
1163 * then a lot like a truncate
1166 do_preallocate(unsigned int offset, unsigned int length)
1171 struct stat statbufs;
1176 if (!quiet && testcalls > simulatedopcount)
1177 prt("skipping zero length fallocate\n");
1178 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1182 keep_size = fl_keep_size && (random() % 2);
1184 end_offset = offset + length;
1185 if (end_offset > biggest) {
1186 biggest = end_offset;
1187 if (!quiet && testcalls > simulatedopcount)
1188 prt("fallocating to largest ever: 0x%jx\n", end_offset);
1192 * last arg matches fallocate string array index in logdump:
1193 * 0: allocate past EOF
1194 * 1: extending prealloc
1195 * 2: interior prealloc
1197 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ?
1198 (keep_size ? 0 : 1) : 2);
1200 if (end_offset > file_size && !keep_size) {
1201 memset(good_buf + file_size, '\0', end_offset - file_size);
1202 file_size = end_offset;
1205 if (testcalls <= simulatedopcount)
1208 fstat(fd, &statbufs);
1209 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset,
1210 (loff_t)length) == -1) {
1211 prt("fallocate: %x to %x\n", offset, length);
1212 prterr("fallocate");
1213 report_failure(161);
1215 output_line(tf, OP_FALLOCATE, offset, length);
1216 output_debug(offset, length, "fallocate done");
1225 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1227 report_failure(171);
1229 iret = write(fd, good_buf, file_size);
1230 if ((off_t)iret != file_size) {
1234 prt("short write: 0x%lx bytes instead of 0x%llx\n",
1235 (unsigned long)iret, (unsigned long long)file_size);
1236 report_failure(172);
1238 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1239 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1240 prterr("ftruncate");
1241 report_failure(173);
1249 const char *tf_num = "";
1251 if (testcalls <= simulatedopcount)
1255 direct = (random() % (o_direct + 1)) ? OP_DIRECT : 0;
1256 log4(OP_CLOSEOPEN + direct, file_size, (unsigned int)file_size, 0);
1258 if (fd_policy != FD_SINGLE)
1259 tf_num = fill_tf_buf(tf);
1262 prt("%06lu %lu.%06u %sclose/open%s\n", testcalls, tv.tv_sec,
1263 (int)tv.tv_usec, tf_num, direct ? "(O_DIRECT)" : "");
1265 report_failure(180);
1267 output_debug(monitorstart, 0, "close done");
1268 tf->o_direct = direct;
1269 tf->fd = open(tf->path, O_RDWR | tf->o_direct, 0);
1271 prterr(tf->o_direct ? "open(O_DIRECT)" : "open");
1272 report_failure(181);
1274 output_debug(monitorstart, 0,
1275 tf->o_direct ? "open(O_DIRECT) done" : "open done");
1278 #define TRIM_OFF_LEN(off, len, size) \
1284 if ((off) + (len) > (size)) \
1285 (len) = (size) - (off); \
1291 unsigned long offset;
1292 unsigned long size = maxoplen;
1293 unsigned long rv = random();
1297 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1303 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1305 if (debugstart > 0 && testcalls >= debugstart)
1308 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1309 prt("%lu...\n", testcalls);
1313 size = random() % (maxoplen + 1);
1315 /* calculate appropriate op to run */
1317 op = rv % OP_MAX_LITE;
1319 op = rv % OP_MAX_FULL;
1331 if (!fallocate_calls) {
1332 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1337 if (!punch_hole_calls) {
1338 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1343 if (!zero_range_calls) {
1344 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1352 TRIM_OFF_LEN(offset, size, file_size);
1353 doread(offset, size);
1356 TRIM_OFF_LEN(offset, size, maxfilelen);
1357 dowrite(offset, size);
1360 TRIM_OFF_LEN(offset, size, file_size);
1361 domapread(offset, size);
1364 TRIM_OFF_LEN(offset, size, maxfilelen);
1365 domapwrite(offset, size);
1369 size = random() % maxfilelen;
1373 TRIM_OFF_LEN(offset, size, maxfilelen);
1374 do_preallocate(offset, size);
1377 TRIM_OFF_LEN(offset, size, file_size);
1378 do_punch_hole(offset, size);
1381 TRIM_OFF_LEN(offset, size, file_size);
1382 do_zero_range(offset, size);
1389 prterr("unknown operation %d: Operation not supported");
1395 if (sizechecks && testcalls > simulatedopcount)
1406 report_failure(9999);
1414 prt("signal %d\n", sig);
1415 prt("testcalls = %lu\n", testcalls);
1423 "usage: fsx [-dfnqFLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [ -I random|rotate ] [-N numops] [-P dirpath] [-S seed] [-Z [prob]] fname [additional paths to fname..]\n"
1424 " -b opnum: beginning operation number (default 1)\n"
1425 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1426 " -d: debug output for all operations [-d -d = more debugging]\n"
1427 " -f flush and invalidate cache after I/O\n"
1428 /* OSX: -d duration: number of hours for the tool to run\n\ */
1429 /* OSX: -e: tests using an extended attribute rather than a file\n\ */
1430 /* OSX: -f forkname: test the named fork of fname\n\ */
1431 /* OSX: -g logpath: path for .fsxlog file\n\ */
1432 /* OSX: -h: write 0s instead of creating holes (i.e. sparse file)\n\ */
1433 /* OSX: -i: interactive mode, hit return before performing each operation\n\ */
1434 " -l flen: the upper bound on file size (default 262144)\n"
1435 " -m startop:endop: monitor (print debug output) specified byte range\n"
1436 " (default 0:infinity)\n"
1437 " -n: no verifications of file size\n"
1438 " -o oplen: the upper bound on operation size (default 65536)\n"
1439 " -p progressinterval: debug output at specified operation interval\n"
1440 " -q: quieter operation\n"
1441 " -r readbdy: %1$u would make reads page aligned (default 1)\n"
1442 " -s style: 1 gives smaller truncates (default 0)\n"
1443 " -t truncbdy: %1$u would make truncates page aligned (default 1)\n"
1444 " -w writebdy: %1$u would make writes page aligned (default 1)\n"
1445 /* XFS: -x: preallocate file space before starting, XFS only (default 0)\n\ */
1446 " -y synchronize changes to a file\n"
1447 /* OSX: -v: debug output for all operations\n\ */
1448 /* XFS: -A: Use the AIO system calls\n" */
1449 /* OSX: -C mix cached and un-cached read/write ops\n\ */
1450 " -D startingop: debug output starting at specified operation\n"
1451 " -F: Do not use fallocate (preallocation) calls\n"
1452 /* OSX: -G logsize: #entries in oplog (default 1024)\n\ */
1453 #ifdef FALLOC_FL_PUNCH_HOLE
1454 " -H: Do not use punch hole calls\n"
1456 #ifdef FALLOC_FL_ZERO_RANGE
1457 " -z: Do not use zero range calls\n"
1459 /* XFS: -C: Do not use collapse range calls\n\ */
1460 " -I [rotate|random]: When multiple paths to the file are given,\n"
1461 " each operation uses a different path. Iterate through them in\n"
1462 " order with 'rotate' or chose them at 'random'. (default random)\n"
1463 " -L: fsxLite - no file creations & no file size changes\n"
1464 /* OSX: -I: start interactive mode since operation opnum\n\ */
1465 /* OSX: -M: slow motion mode, wait 1 second before each op\n\ */
1466 " -N numops: total # operations to do (default infinity)\n"
1467 " -O: use oplen (see -o flag) for every op (default random)\n"
1468 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1469 " -R: read() system calls only (mapped reads disabled)\n"
1470 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1471 /* OSX: -T datasize: atomic data element write size [1,2,4] (default 4)\n\ */
1472 " -W: mapped write operations DISabled\n"
1473 " -Z[P]: O_DIRECT file IO [1 in P chance for each open] (default off)\n"
1474 " fname: this filename is REQUIRED (no default)\n",
1480 getnum(char *s, char **e)
1485 ret = strtol(s, e, 0);
1513 test_fallocate(int mode)
1519 /* Must go more than a page away so let's go 4M to be sure */
1520 if (fallocate(fd, mode, 0, 4096*1024) && errno == EOPNOTSUPP) {
1522 warn("%s: filesystem does not support fallocate mode 0x%x, disabling!",
1528 /* Always call ftruncate since file size might be adjusted
1529 * by fallocate even on error
1531 if (ftruncate(fd, 0) == -1)
1532 warn("ftruncate to 0 size failed");
1538 main(int argc, char **argv)
1547 page_size = getpagesize();
1548 page_mask = page_size - 1;
1550 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1552 while ((ch = getopt(argc, argv,
1553 "b:c:dfl:m:no:p:qr:s:t:w:xyzD:FHI:LN:OP:RS:WZ::"))
1557 simulatedopcount = getnum(optarg, &endp);
1559 fprintf(stdout, "Will begin at operation %ld\n",
1561 if (simulatedopcount == 0)
1563 simulatedopcount -= 1;
1566 closeprob = getnum(optarg, &endp);
1569 "Chance of close/open is 1 in %d\n",
1581 maxfilelen = getnum(optarg, &endp);
1582 if (maxfilelen <= 0)
1586 monitorstart = getnum(optarg, &endp);
1587 if (monitorstart < 0)
1589 if (!endp || *endp++ != ':')
1591 monitorend = getnum(endp, &endp);
1594 if (monitorend == 0)
1595 monitorend = -1; /* aka infinity */
1601 maxoplen = getnum(optarg, &endp);
1606 progressinterval = getnum(optarg, &endp);
1607 if (progressinterval <= 0)
1614 readbdy = getnum(optarg, &endp);
1619 style = getnum(optarg, &endp);
1620 if (style < 0 || style > 1)
1624 truncbdy = getnum(optarg, &endp);
1629 writebdy = getnum(optarg, &endp);
1637 debugstart = getnum(optarg, &endp);
1642 fallocate_calls = 0;
1645 punch_hole_calls = 0;
1648 zero_range_calls = 0;
1651 assign_fd_policy(optarg);
1657 numops = getnum(optarg, &endp);
1665 strncpy(goodfile, optarg, sizeof(goodfile) - 1);
1666 strncat(goodfile, "/", PATH_MAX - strlen(goodfile) - 1);
1667 strncpy(logfile, optarg, sizeof(logfile) - 1);
1668 strncat(logfile, "/", PATH_MAX - strlen(logfile) - 1);
1675 seed = getnum(optarg, &endp);
1677 seed = time(0) % 10000;
1679 fprintf(stdout, "Seed set to %d\n", seed);
1686 fprintf(stdout, "mapped writes DISABLED\n");
1690 o_direct = getnum(optarg, &endp);
1691 if (!optarg || o_direct == 0)
1704 signal(SIGHUP, cleanup);
1705 signal(SIGINT, cleanup);
1706 signal(SIGPIPE, cleanup);
1707 signal(SIGALRM, cleanup);
1708 signal(SIGTERM, cleanup);
1709 signal(SIGXCPU, cleanup);
1710 signal(SIGXFSZ, cleanup);
1711 signal(SIGVTALRM, cleanup);
1712 signal(SIGUSR1, cleanup);
1713 signal(SIGUSR2, cleanup);
1714 signal(SIGBUS, segv);
1715 signal(SIGSEGV, segv);
1717 initstate(seed, state, 256);
1720 open_test_files(argv, argc);
1722 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1723 strncat(goodfile, ".fsxgood", PATH_MAX - strlen(goodfile) - 1);
1724 fsxgoodfd = open(goodfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
1725 if (fsxgoodfd < 0) {
1729 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1730 strncat(logfile, ".fsxlog", PATH_MAX - strlen(logfile) - 1);
1731 fsxlogf = fopen(logfile, "w");
1740 maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1741 file_size = maxfilelen;
1742 if (file_size == (off_t)-1) {
1744 warn("%s: lseek eof", __func__);
1747 ret = lseek(fd, (off_t)0, SEEK_SET);
1748 if (ret == (off_t)-1) {
1750 warn("%s: lseek 0", __func__);
1754 original_buf = (char *)malloc(maxfilelen);
1757 for (i = 0; i < maxfilelen; i++)
1758 original_buf[i] = random() % 256;
1762 ret = posix_memalign((void **)&good_buf, writebdy, maxfilelen);
1764 prt("%s: posix_memalign failed: %s\n", __func__,
1769 ret = posix_memalign((void **)&temp_buf, readbdy, maxoplen);
1771 prt("%s: posix_memalign failed: %s\n", __func__,
1776 good_buf = malloc(maxfilelen);
1778 prt("malloc failed.\n");
1782 temp_buf = malloc(maxoplen);
1784 prt("malloc failed.\n");
1788 memset(good_buf, 0, maxfilelen);
1789 memset(temp_buf, 0, maxoplen);
1791 if (lite) { /* zero entire existing file */
1795 written = write(fd, good_buf, (size_t)maxfilelen);
1796 if (written != maxfilelen) {
1797 if (written == -1) {
1799 warn("%s: error on write", __func__);
1801 warn("%s: short write, 0x%x bytes instead of 0x%lx\n",
1802 __func__, (unsigned int)written,
1811 if (fallocate_calls)
1812 fallocate_calls = test_fallocate(0);
1814 if (punch_hole_calls)
1815 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE |
1816 FALLOC_FL_KEEP_SIZE);
1818 if (zero_range_calls)
1819 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1821 fl_keep_size = test_fallocate(FALLOC_FL_KEEP_SIZE);
1823 while (numops == -1 || numops--)
1827 prt("All operations completed A-OK!\n");