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 /* _GNU_SOURCE defines O_DIRECT as 14th bit which is 0x4000(16384) */
140 #define OP_DIRECT 16384
142 #ifndef FALLOC_FL_PUNCH_HOLE
143 #define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */
146 #ifndef FALLOC_FL_KEEP_SIZE
147 #define FALLOC_FL_KEEP_SIZE 0x01 /* default is extend size */
150 #ifndef FALLOC_FL_ZERO_RANGE
151 #define FALLOC_FL_ZERO_RANGE 0x10 /* convert range to zeros */
155 char *original_buf; /* a pointer to the original data */
156 char *good_buf; /* a pointer to the correct data */
157 char *temp_buf; /* a pointer to the current data */
158 char *fname; /* name of our test file */
159 char logfile[PATH_MAX]; /* name of our log file */
160 char goodfile[PATH_MAX]; /* name of our test file */
162 struct timeval tv; /* time current operation started */
166 unsigned long testcalls; /* calls to function "test" */
168 long simulatedopcount; /* -b flag */
169 int closeprob; /* -c flag */
170 int debug ; /* -d flag */
171 long debugstart; /* -D flag */
172 int flush; /* -f flag */
173 int do_fsync; /* -y flag */
174 long maxfilelen = 256 * 1024; /* -l flag */
175 int sizechecks = 1; /* -n flag disables them */
176 int maxoplen = 64 * 1024; /* -o flag */
177 int quiet; /* -q flag */
178 long progressinterval; /* -p flag */
179 int readbdy = 1; /* -r flag */
180 int style; /* -s flag */
181 int truncbdy = 1; /* -t flag */
182 int writebdy = 1; /* -w flag */
183 long monitorstart = -1; /* -m flag */
184 long monitorend = -1; /* -m flag */
185 int lite; /* -L flag */
186 long numops = -1; /* -N flag */
187 int randomoplen = 1; /* -O flag disables it */
188 int seed = 1; /* -S flag */
189 int mapped_writes = 1; /* -W flag disables */
190 int fallocate_calls = 1; /* -F flag disables */
191 int punch_hole_calls = 1; /* -H flag disables */
192 int zero_range_calls = 1; /* -z flag disables */
193 int mapped_reads = 1; /* -R flag disables it */
195 int o_direct; /* -Z */
205 vwarnc(code, fmt, ap)
210 fprintf(stderr, "fsx: ");
212 vfprintf(stderr, fmt, ap);
213 fprintf(stderr, ": ");
215 fprintf(stderr, "%s\n", strerror(code));
219 __attribute__((format(__printf__, 1, 2)))
220 warn(const char *fmt, ...)
225 vwarnc(errno, fmt, ap);
230 __attribute__((format(__printf__, 1, 2)))
236 vfprintf(stdout, fmt, args);
241 vfprintf(fsxlogf, fmt, args);
247 * prterr() is now a macro. It internally calls ptrerr_func()
248 * which transparently handles passing of function name.
249 * This version also keeps checkpatch happy.
252 ptrerr_func(const char *func, const char *prefix)
254 prt("%s: %s%s%s\n", func, prefix, prefix ? ": " : "", strerror(errno));
256 #define prterr(prefix) ptrerr_func(__func__, prefix)
259 log4(int operation, int arg0, int arg1, int arg2)
261 struct log_entry *le;
264 le->operation = operation;
268 gettimeofday(&tv, NULL);
273 if (logptr >= LOGSIZE)
278 fill_tf_buf(const struct test_file *tf)
280 static int max_tf_len;
281 static char tf_buf[32];
283 if (fd_policy == FD_SINGLE)
287 max_tf_len = scnprintf(tf_buf, sizeof(tf_buf) - 1,
288 "%u", num_test_files - 1);
290 snprintf(tf_buf, sizeof(tf_buf), "[%0*lu]", max_tf_len,
291 (unsigned long)(tf - test_files));
300 struct log_entry *lp;
301 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
303 prt("LOG DUMP (%d total operations):\n", logcount);
304 if (logcount < LOGSIZE) {
311 for ( ; count > 0; count--) {
314 opnum = i + 1 + (logcount / LOGSIZE) * LOGSIZE;
316 prt("%d%s: %lu.%06u ", opnum, fill_tf_buf(lp->tf),
317 lp->tv.tv_sec, (int)lp->tv.tv_usec);
319 switch (lp->operation) {
321 prt("MAPREAD 0x%05x thru 0x%05x (0x%05x bytes)",
322 lp->args[0], lp->args[0] + lp->args[1] - 1,
324 if (badoff >= lp->args[0] && badoff <
325 lp->args[0] + lp->args[1])
329 prt("MAPWRITE 0x%05x thru 0x%05x (0x%05x bytes)",
330 lp->args[0], lp->args[0] + lp->args[1] - 1,
332 if (badoff >= lp->args[0] && badoff <
333 lp->args[0] + lp->args[1])
337 case OP_READ + OP_DIRECT:
338 prt("READ%s 0x%05x thru 0x%05x (0x%05x bytes)",
339 lp->operation & OP_DIRECT ? "_OD" : " ",
340 lp->args[0], lp->args[0] + lp->args[1] - 1,
342 if (badoff >= lp->args[0] &&
343 badoff < lp->args[0] + lp->args[1])
347 case OP_WRITE + OP_DIRECT:
348 prt("WRITE%s 0x%05x thru 0x%05x (0x%05x bytes)",
349 lp->operation & OP_DIRECT ? "_OD" : " ",
350 lp->args[0], lp->args[0] + lp->args[1] - 1,
352 if (lp->args[0] > lp->args[2])
354 else if (lp->args[0] + lp->args[1] > lp->args[2])
356 if ((badoff >= lp->args[0] || badoff >= lp->args[2]) &&
357 badoff < lp->args[0] + lp->args[1])
361 down = lp->args[0] < lp->args[1];
362 prt("TRUNCATE %s\tfrom 0x%05x to 0x%05x",
363 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
364 if (badoff >= lp->args[!down] &&
365 badoff < lp->args[!!down])
369 /* 0: offset 1: length 2: where alloced */
370 prt("FALLOC \tfrom 0x%05x to 0x%05x\t(0x%05x bytes)%s",
371 lp->args[0], lp->args[0] + lp->args[1],
372 lp->args[1], falloc_type[lp->args[2]]);
373 if (badoff >= lp->args[0] &&
374 badoff < lp->args[0] + lp->args[1])
378 prt("PUNCH 0x%05x thru 0x%05x\t(0x%05x bytes)",
379 lp->args[0], lp->args[0] + lp->args[1] - 1,
381 if (badoff >= lp->args[0] && badoff <
382 lp->args[0] + lp->args[1])
386 prt("ZERO 0x%05x thru 0x%05x\t(0x%05x bytes)",
387 lp->args[0], lp->args[0] + lp->args[1] - 1,
389 if (badoff >= lp->args[0] && badoff <
390 lp->args[0] + lp->args[1])
394 case OP_CLOSEOPEN + OP_DIRECT:
396 lp->operation & OP_DIRECT ? "_OD" : " ");
399 prt("SKIPPED (no operation)");
402 prt("BOGUS LOG ENTRY (operation code = %d)!",
413 save_buffer(char *buffer, off_t bufferlength, int fd)
416 ssize_t byteswritten;
418 if (fd <= 0 || bufferlength == 0)
421 if (bufferlength > INT_MAX) {
422 prt("fsx flaw: overflow in %s\n", __func__);
426 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
428 if (size_by_seek == (off_t)-1) {
430 } else if (bufferlength > size_by_seek) {
431 warn("%s: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n",
432 __func__, (unsigned long long)size_by_seek,
433 (unsigned long long)bufferlength);
434 bufferlength = size_by_seek;
438 ret = lseek(fd, (off_t)0, SEEK_SET);
439 if (ret == (off_t)-1)
442 byteswritten = write(fd, buffer, (size_t)bufferlength);
443 if (byteswritten != bufferlength) {
444 if (byteswritten == -1)
447 warn("%s: short write, 0x%x bytes instead of 0x%llx\n",
448 __func__, (unsigned int)byteswritten,
449 (unsigned long long)bufferlength);
454 report_failure(int status)
460 save_buffer(good_buf, file_size, fsxgoodfd);
461 prt("Correct content saved for comparison\n");
462 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
470 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
471 *(((unsigned char *)(cp)) + 1)))
474 check_buffers(unsigned int offset, unsigned int size)
480 unsigned int bad = 0;
482 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
483 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
485 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
487 c = good_buf[offset];
491 bad = short_at(&temp_buf[i]);
492 prt("%#07x\t%#06x\t%#06x", offset,
493 short_at(&good_buf[offset]), bad);
494 op = temp_buf[offset & 1 ? i + 1 : i];
506 prt("operation# (mod 256) for the bad data may be %u\n",
507 ((unsigned int)op & 0xff));
509 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
511 prt("????????????????\n");
520 unsigned int index = 0;
533 prt("unknown policy");
537 return &test_files[index % num_test_files];
541 assign_fd_policy(char *policy)
543 if (!strcmp(policy, "random")) {
544 fd_policy = FD_RANDOM;
545 } else if (!strcmp(policy, "rotate")) {
546 fd_policy = FD_ROTATE;
548 prt("unknown -I policy: '%s'\n", policy);
556 struct test_file *tf = get_tf();
561 static const char *my_basename(const char *path)
563 char *c = strrchr(path, '/');
565 return c ? c++ : path;
569 open_test_files(char **argv, int argc)
571 struct test_file *tf;
574 num_test_files = argc;
575 if (num_test_files == 1)
576 fd_policy = FD_SINGLE;
578 test_files = calloc(num_test_files, sizeof(*test_files));
580 prterr("reallocating space for test files");
584 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
587 tf->o_direct = (random() % (o_direct + 1)) ? OP_DIRECT : 0;
589 tf->fd = open(tf->path,
590 O_RDWR | (lite ? 0 : O_CREAT | O_TRUNC) |
598 if (quiet || fd_policy == FD_SINGLE)
601 for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
602 prt("fd %d: %s\n", i, tf->path);
606 close_test_files(void)
609 struct test_file *tf;
611 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
626 if (fstat(fd, &statbuf)) {
628 statbuf.st_size = -1;
630 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
631 if (file_size != statbuf.st_size || file_size != size_by_seek) {
632 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
633 (unsigned long long)file_size,
634 (unsigned long long)statbuf.st_size,
635 (unsigned long long)size_by_seek);
641 check_trunc_hack(void)
646 /* should not ignore ftruncate(2)'s return value */
647 if (ftruncate(fd, (off_t)0) < 0) {
648 prterr("trunc_hack: ftruncate(0)");
651 if (ftruncate(fd, (off_t)100000) < 0) {
652 prterr("trunc_hack: ftruncate(100000)");
655 if (fstat(fd, &statbuf)) {
656 prterr("trunc_hack: fstat");
657 statbuf.st_size = -1;
659 if (statbuf.st_size != (off_t)100000) {
660 prt("no extend on truncate! not posix!\n");
663 if (ftruncate(fd, 0) < 0) {
664 prterr("trunc_hack: ftruncate(0) (2nd call)");
670 output_line(struct test_file *tf, int op, unsigned int offset,
675 [OP_WRITE] = "write",
676 [OP_TRUNCATE] = "trunc from",
677 [OP_MAPREAD] = "mapread",
678 [OP_MAPWRITE] = "mapwrite",
679 [OP_READ + OP_DIRECT] = "read_OD",
680 [OP_WRITE + OP_DIRECT] = "write_OD",
681 [OP_FALLOCATE] = "fallocate",
682 [OP_PUNCH_HOLE] = "punch from",
687 ((progressinterval && testcalls % progressinterval == 0) ||
688 (debug && (monitorstart == -1 ||
689 (offset + size > monitorstart &&
690 (monitorend == -1 || offset <= monitorend)))))))
693 prt("%06lu%s %lu.%06u %-10s %#08x %s %#08x\t(0x0%x bytes)\n",
694 testcalls, fill_tf_buf(tf), tv.tv_sec, (int)tv.tv_usec,
695 ops[op], offset, op == OP_TRUNCATE || op == OP_PUNCH_HOLE ?
696 " to " : "thru", offset + size - 1,
697 (int)size < 0 ? -(int)size : size);
700 void output_debug(unsigned int offset, unsigned int size, const char *what)
704 if (!quiet && (debug > 1 && (monitorstart == -1 ||
705 (offset + size >= monitorstart &&
706 (monitorend == -1 || offset <= monitorend))))) {
707 gettimeofday(&t, NULL);
708 prt(" %lu.%06u %s\n", t.tv_sec, (int)t.tv_usec, what);
713 doflush(unsigned int offset, unsigned int size)
715 unsigned int pg_offset;
716 unsigned int map_size;
718 struct test_file *tf = get_tf();
724 pg_offset = offset & page_mask;
725 map_size = pg_offset + size;
727 p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
728 MAP_FILE | MAP_SHARED, fd,
729 (off_t)(offset - pg_offset));
730 if (p == (char *)-1) {
734 if (msync(p, map_size, MS_INVALIDATE) != 0) {
738 if (munmap(p, map_size) != 0) {
742 output_debug(offset, size, "flush done");
746 doread(unsigned int offset, unsigned int size)
750 struct test_file *tf = get_tf();
753 offset -= offset % readbdy;
755 size -= size % readbdy;
758 if (!quiet && testcalls > simulatedopcount && !tf->o_direct)
759 prt("skipping zero size read\n");
760 log4(OP_SKIPPED, OP_READ, offset, size);
763 if (size + offset > file_size) {
764 if (!quiet && testcalls > simulatedopcount)
765 prt("skipping seek/read past end of file\n");
766 log4(OP_SKIPPED, OP_READ, offset, size);
770 log4(OP_READ + tf->o_direct, offset, size, 0);
772 if (testcalls <= simulatedopcount)
775 output_line(tf, OP_READ + tf->o_direct, offset, size);
777 ret = lseek(fd, (off_t)offset, SEEK_SET);
778 if (ret == (off_t)-1) {
782 iret = read(fd, temp_buf, size);
783 output_debug(offset, size, "read done");
788 prt("short read: 0x%x bytes instead of 0x%x\n",
792 check_buffers(offset, size);
796 check_eofpage(char *s, unsigned int offset, char *p, int size)
798 long last_page, should_be_zero;
800 if (offset + size <= (file_size & ~page_mask))
803 * we landed in the last page of the file
804 * test to make sure the VM system provided 0's
805 * beyond the true end of the file mapping
806 * (as required by mmap def in 1996 posix 1003.1)
808 last_page = ((long)p + (offset & page_mask) + size) & ~page_mask;
810 for (should_be_zero = last_page + (file_size & page_mask);
811 should_be_zero < last_page + page_size;
813 if (*(char *)should_be_zero) {
814 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%lx is 0x%04x\n",
815 s, (long long)file_size - 1,
816 should_be_zero & page_mask,
817 short_at(should_be_zero));
823 domapread(unsigned int offset, unsigned int size)
825 unsigned int pg_offset;
826 unsigned int map_size;
830 offset -= offset % readbdy;
834 if (!quiet && testcalls > simulatedopcount)
835 prt("skipping zero size read\n");
836 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
839 if (size + offset > file_size) {
840 if (!quiet && testcalls > simulatedopcount)
841 prt("skipping seek/read past end of file\n");
842 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
846 log4(OP_MAPREAD, offset, size, 0);
848 if (testcalls <= simulatedopcount)
851 output_line(tf, OP_MAPREAD, offset, size);
853 pg_offset = offset & page_mask;
854 map_size = pg_offset + size;
856 p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
857 (off_t)(offset - pg_offset));
858 if (p == MAP_FAILED) {
862 output_debug(offset, size, "mmap done");
863 if (setjmp(jmpbuf) == 0) {
865 memcpy(temp_buf, p + pg_offset, size);
866 check_eofpage("Read", offset, p, size);
869 report_failure(1901);
871 output_debug(offset, size, "memcpy done");
872 if (munmap(p, map_size) != 0) {
876 output_debug(offset, size, "munmap done");
878 check_buffers(offset, size);
882 gendata(char *original_buf, char *good_buf, unsigned int offset,
886 good_buf[offset] = testcalls % 256;
888 good_buf[offset] += original_buf[offset];
894 dowrite(unsigned int offset, unsigned int size)
902 offset -= offset % writebdy;
904 size -= size % writebdy;
906 if (!quiet && testcalls > simulatedopcount && !tf->o_direct)
907 prt("skipping zero size write\n");
908 log4(OP_SKIPPED, OP_WRITE, offset, size);
912 log4(OP_WRITE + tf->o_direct, offset, size, file_size);
914 gendata(original_buf, good_buf, offset, size);
915 if (file_size < offset + size) {
916 if (file_size < offset)
917 memset(good_buf + file_size, '\0', offset - file_size);
918 file_size = offset + size;
920 warn("Lite file size bug in fsx!");
925 if (testcalls <= simulatedopcount)
928 output_line(tf, OP_WRITE + tf->o_direct, offset, size);
930 ret = lseek(fd, (off_t)offset, SEEK_SET);
931 if (ret == (off_t)-1) {
935 iret = write(fd, good_buf + offset, size);
936 output_debug(offset, size, "write done");
941 prt("short write: 0x%x bytes instead of 0x%x\n",
947 prt("fsync() failed: %s\n", strerror(errno));
950 output_debug(offset, size, "fsync done");
953 doflush(offset, size);
954 output_debug(offset, size, "flush done");
959 domapwrite(unsigned int offset, unsigned int size)
961 unsigned int pg_offset;
962 unsigned int map_size;
969 offset -= offset % writebdy;
971 if (!quiet && testcalls > simulatedopcount)
972 prt("skipping zero size write\n");
973 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
976 cur_filesize = file_size;
978 log4(OP_MAPWRITE, offset, size, 0);
980 gendata(original_buf, good_buf, offset, size);
981 if (file_size < offset + size) {
982 if (file_size < offset)
983 memset(good_buf + file_size, '\0', offset - file_size);
984 file_size = offset + size;
986 warn("Lite file size bug in fsx!");
991 if (testcalls <= simulatedopcount)
994 output_line(tf, OP_MAPWRITE, offset, size);
996 if (file_size > cur_filesize) {
997 if (ftruncate(fd, file_size) == -1) {
1001 output_debug(offset, size, "truncate done");
1003 pg_offset = offset & page_mask;
1004 map_size = pg_offset + size;
1006 p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
1007 fd, (off_t)(offset - pg_offset));
1008 if (p == MAP_FAILED) {
1010 report_failure(202);
1012 output_debug(offset, map_size, "mmap done");
1013 if (setjmp(jmpbuf) == 0) {
1015 memcpy(p + pg_offset, good_buf + offset, size);
1016 if (msync(p, map_size, MS_SYNC) != 0) {
1018 report_failure(203);
1020 check_eofpage("Write", offset, p, size);
1023 report_failure(2021);
1025 output_debug(offset, map_size, "msync done");
1026 if (munmap(p, map_size) != 0) {
1028 report_failure(204);
1030 output_debug(offset, map_size, "munmap done");
1034 dotruncate(unsigned int size)
1036 int oldsize = file_size;
1041 size -= size % truncbdy;
1042 if (size > biggest) {
1044 if (!quiet && testcalls > simulatedopcount)
1045 prt("truncating to largest ever: 0x%x\n", size);
1048 log4(OP_TRUNCATE, size, (unsigned int)file_size, 0);
1050 if (size > file_size)
1051 memset(good_buf + file_size, '\0', size - file_size);
1054 if (testcalls <= simulatedopcount)
1057 output_line(tf, OP_TRUNCATE, oldsize, size - oldsize);
1059 if (ftruncate(fd, (off_t)size) == -1) {
1060 prt("ftruncate: 0x%x\n", size);
1061 prterr("ftruncate");
1062 report_failure(160);
1064 output_debug(size, 0, "truncate done");
1068 do_punch_hole(unsigned int offset, unsigned int length)
1072 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
1078 if (!quiet && testcalls > simulatedopcount) {
1079 prt("skipping zero length punch hole\n");
1080 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
1085 if (file_size <= (loff_t)offset) {
1086 if (!quiet && testcalls > simulatedopcount) {
1087 prt("skipping hole punch off the end of the file\n");
1088 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
1093 log4(OP_PUNCH_HOLE, offset, length, 0);
1095 if (testcalls <= simulatedopcount)
1098 output_line(tf, OP_PUNCH_HOLE, offset, length);
1099 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1100 prt("punch hole: %x to %x\n", offset, length);
1101 prterr("fallocate");
1102 report_failure(161);
1104 output_debug(offset, length, "zero_range done");
1106 max_offset = offset < file_size ? offset : file_size;
1107 max_len = max_offset + length <= file_size ? length :
1108 file_size - max_offset;
1109 memset(good_buf + max_offset, '\0', max_len);
1113 do_zero_range(unsigned int offset, unsigned int length)
1115 unsigned int end_offset;
1116 int mode = FALLOC_FL_ZERO_RANGE;
1123 if (!quiet && testcalls > simulatedopcount) {
1124 prt("skipping zero length zero range\n");
1125 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
1130 keep_size = random() % 2;
1132 end_offset = keep_size ? 0 : offset + length;
1134 if (end_offset > biggest) {
1135 biggest = end_offset;
1136 if (!quiet && testcalls > simulatedopcount)
1137 prt("zero_range to largest ever: 0x%x\n", end_offset);
1141 * last arg matches fallocate string array index in logdump:
1142 * 0: allocate past EOF
1143 * 1: extending prealloc
1144 * 2: interior prealloc
1146 log4(OP_ZERO_RANGE, offset, length,
1147 (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1149 if (testcalls <= simulatedopcount)
1152 output_line(tf, OP_TRUNCATE, offset, length);
1154 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1155 prt("pzero range: %x to %x\n", offset, length);
1156 prterr("fallocate");
1157 report_failure(161);
1159 output_debug(offset, length, "zero_range done");
1161 memset(good_buf + offset, '\0', length);
1165 * fallocate is basically a no-op unless extending,
1166 * then a lot like a truncate
1169 do_preallocate(unsigned int offset, unsigned int length)
1174 struct stat statbufs;
1179 if (!quiet && testcalls > simulatedopcount)
1180 prt("skipping zero length fallocate\n");
1181 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1185 keep_size = fl_keep_size && (random() % 2);
1187 end_offset = offset + length;
1188 if (end_offset > biggest) {
1189 biggest = end_offset;
1190 if (!quiet && testcalls > simulatedopcount)
1191 prt("fallocating to largest ever: 0x%jx\n", end_offset);
1195 * last arg matches fallocate string array index in logdump:
1196 * 0: allocate past EOF
1197 * 1: extending prealloc
1198 * 2: interior prealloc
1200 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ?
1201 (keep_size ? 0 : 1) : 2);
1203 if (end_offset > file_size && !keep_size) {
1204 memset(good_buf + file_size, '\0', end_offset - file_size);
1205 file_size = end_offset;
1208 if (testcalls <= simulatedopcount)
1211 fstat(fd, &statbufs);
1212 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset,
1213 (loff_t)length) == -1) {
1214 prt("fallocate: %x to %x\n", offset, length);
1215 prterr("fallocate");
1216 report_failure(161);
1218 output_line(tf, OP_FALLOCATE, offset, length);
1219 output_debug(offset, length, "fallocate done");
1228 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1230 report_failure(171);
1232 iret = write(fd, good_buf, file_size);
1233 if ((off_t)iret != file_size) {
1237 prt("short write: 0x%lx bytes instead of 0x%llx\n",
1238 (unsigned long)iret, (unsigned long long)file_size);
1239 report_failure(172);
1241 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1242 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1243 prterr("ftruncate");
1244 report_failure(173);
1252 const char *tf_num = "";
1254 if (testcalls <= simulatedopcount)
1259 direct = (random() % (o_direct + 1)) ? OP_DIRECT : 0;
1261 log4(OP_CLOSEOPEN + direct, file_size, (unsigned int)file_size, 0);
1263 if (fd_policy != FD_SINGLE)
1264 tf_num = fill_tf_buf(tf);
1267 prt("%06lu %lu.%06u %sclose/open%s\n", testcalls, tv.tv_sec,
1268 (int)tv.tv_usec, tf_num, direct ? "(O_DIRECT)" : "");
1270 report_failure(180);
1272 output_debug(monitorstart, 0, "close done");
1273 tf->o_direct = direct;
1274 tf->fd = open(tf->path, O_RDWR | tf->o_direct, 0);
1276 prterr(tf->o_direct ? "open(O_DIRECT)" : "open");
1277 report_failure(181);
1279 output_debug(monitorstart, 0,
1280 tf->o_direct ? "open(O_DIRECT) done" : "open done");
1283 #define TRIM_OFF_LEN(off, len, size) \
1289 if ((off) + (len) > (size)) \
1290 (len) = (size) - (off); \
1296 unsigned long offset;
1297 unsigned long size = maxoplen;
1298 unsigned long rv = random();
1302 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1308 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1310 if (debugstart > 0 && testcalls >= debugstart)
1313 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1314 prt("%lu...\n", testcalls);
1318 size = random() % (maxoplen + 1);
1320 /* calculate appropriate op to run */
1322 op = rv % OP_MAX_LITE;
1324 op = rv % OP_MAX_FULL;
1336 if (!fallocate_calls) {
1337 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1342 if (!punch_hole_calls) {
1343 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1348 if (!zero_range_calls) {
1349 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1357 TRIM_OFF_LEN(offset, size, file_size);
1358 doread(offset, size);
1361 TRIM_OFF_LEN(offset, size, maxfilelen);
1362 dowrite(offset, size);
1365 TRIM_OFF_LEN(offset, size, file_size);
1366 domapread(offset, size);
1369 TRIM_OFF_LEN(offset, size, maxfilelen);
1370 domapwrite(offset, size);
1374 size = random() % maxfilelen;
1378 TRIM_OFF_LEN(offset, size, maxfilelen);
1379 do_preallocate(offset, size);
1382 TRIM_OFF_LEN(offset, size, file_size);
1383 do_punch_hole(offset, size);
1386 TRIM_OFF_LEN(offset, size, file_size);
1387 do_zero_range(offset, size);
1394 prterr("unknown operation %d: Operation not supported");
1400 if (sizechecks && testcalls > simulatedopcount)
1411 report_failure(9999);
1419 prt("signal %d\n", sig);
1420 prt("testcalls = %lu\n", testcalls);
1428 "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"
1429 " -b opnum: beginning operation number (default 1)\n"
1430 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1431 " -d: debug output for all operations [-d -d = more debugging]\n"
1432 " -f flush and invalidate cache after I/O\n"
1433 /* OSX: -d duration: number of hours for the tool to run\n\ */
1434 /* OSX: -e: tests using an extended attribute rather than a file\n\ */
1435 /* OSX: -f forkname: test the named fork of fname\n\ */
1436 /* OSX: -g logpath: path for .fsxlog file\n\ */
1437 /* OSX: -h: write 0s instead of creating holes (i.e. sparse file)\n\ */
1438 /* OSX: -i: interactive mode, hit return before performing each operation\n\ */
1439 " -l flen: the upper bound on file size (default 262144)\n"
1440 " -m startop:endop: monitor (print debug output) specified byte range\n"
1441 " (default 0:infinity)\n"
1442 " -n: no verifications of file size\n"
1443 " -o oplen: the upper bound on operation size (default 65536)\n"
1444 " -p progressinterval: debug output at specified operation interval\n"
1445 " -q: quieter operation\n"
1446 " -r readbdy: %1$u would make reads page aligned (default 1)\n"
1447 " -s style: 1 gives smaller truncates (default 0)\n"
1448 " -t truncbdy: %1$u would make truncates page aligned (default 1)\n"
1449 " -w writebdy: %1$u would make writes page aligned (default 1)\n"
1450 /* XFS: -x: preallocate file space before starting, XFS only (default 0)\n\ */
1451 " -y synchronize changes to a file\n"
1452 /* OSX: -v: debug output for all operations\n\ */
1453 /* XFS: -A: Use the AIO system calls\n" */
1454 /* OSX: -C mix cached and un-cached read/write ops\n\ */
1455 " -D startingop: debug output starting at specified operation\n"
1456 " -F: Do not use fallocate (preallocation) calls\n"
1457 /* OSX: -G logsize: #entries in oplog (default 1024)\n\ */
1458 #ifdef FALLOC_FL_PUNCH_HOLE
1459 " -H: Do not use punch hole calls\n"
1461 #ifdef FALLOC_FL_ZERO_RANGE
1462 " -z: Do not use zero range calls\n"
1464 /* XFS: -C: Do not use collapse range calls\n\ */
1465 " -I [rotate|random]: When multiple paths to the file are given,\n"
1466 " each operation uses a different path. Iterate through them in\n"
1467 " order with 'rotate' or chose them at 'random'. (default random)\n"
1468 " -L: fsxLite - no file creations & no file size changes\n"
1469 /* OSX: -I: start interactive mode since operation opnum\n\ */
1470 /* OSX: -M: slow motion mode, wait 1 second before each op\n\ */
1471 " -N numops: total # operations to do (default infinity)\n"
1472 " -O: use oplen (see -o flag) for every op (default random)\n"
1473 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1474 " -R: read() system calls only (mapped reads disabled)\n"
1475 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1476 /* OSX: -T datasize: atomic data element write size [1,2,4] (default 4)\n\ */
1477 " -W: mapped write operations DISabled\n"
1479 " -Z[P]: O_DIRECT file IO [1 in P chance for each open] (default off)\n"
1481 " fname: this filename is REQUIRED (no default)\n",
1487 getnum(char *s, char **e)
1492 ret = strtol(s, e, 0);
1520 test_fallocate(int mode)
1526 /* Must go more than a page away so let's go 4M to be sure */
1527 if (fallocate(fd, mode, 0, 4096*1024) && errno == EOPNOTSUPP) {
1529 warn("%s: filesystem does not support fallocate mode 0x%x, disabling!",
1535 /* Always call ftruncate since file size might be adjusted
1536 * by fallocate even on error
1538 if (ftruncate(fd, 0) == -1)
1539 warn("ftruncate to 0 size failed");
1545 main(int argc, char **argv)
1554 page_size = getpagesize();
1555 page_mask = page_size - 1;
1557 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1559 while ((ch = getopt(argc, argv,
1560 "b:c:dfl:m:no:p:qr:s:t:w:xyzD:FHI:LN:OP:RS:WZ::"))
1564 simulatedopcount = getnum(optarg, &endp);
1566 fprintf(stdout, "Will begin at operation %ld\n",
1568 if (simulatedopcount == 0)
1570 simulatedopcount -= 1;
1573 closeprob = getnum(optarg, &endp);
1576 "Chance of close/open is 1 in %d\n",
1588 maxfilelen = getnum(optarg, &endp);
1589 if (maxfilelen <= 0)
1593 monitorstart = getnum(optarg, &endp);
1594 if (monitorstart < 0)
1596 if (!endp || *endp++ != ':')
1598 monitorend = getnum(endp, &endp);
1601 if (monitorend == 0)
1602 monitorend = -1; /* aka infinity */
1608 maxoplen = getnum(optarg, &endp);
1613 progressinterval = getnum(optarg, &endp);
1614 if (progressinterval <= 0)
1621 readbdy = getnum(optarg, &endp);
1626 style = getnum(optarg, &endp);
1627 if (style < 0 || style > 1)
1631 truncbdy = getnum(optarg, &endp);
1636 writebdy = getnum(optarg, &endp);
1644 debugstart = getnum(optarg, &endp);
1649 fallocate_calls = 0;
1652 punch_hole_calls = 0;
1655 zero_range_calls = 0;
1658 assign_fd_policy(optarg);
1664 numops = getnum(optarg, &endp);
1672 strncpy(goodfile, optarg, sizeof(goodfile) - 1);
1673 strncat(goodfile, "/", PATH_MAX - strlen(goodfile) - 1);
1674 strncpy(logfile, optarg, sizeof(logfile) - 1);
1675 strncat(logfile, "/", PATH_MAX - strlen(logfile) - 1);
1682 seed = getnum(optarg, &endp);
1684 seed = time(0) % 10000;
1686 fprintf(stdout, "Seed set to %d\n", seed);
1693 fprintf(stdout, "mapped writes DISABLED\n");
1698 o_direct = getnum(optarg, &endp);
1699 if (!optarg || o_direct == 0)
1713 signal(SIGHUP, cleanup);
1714 signal(SIGINT, cleanup);
1715 signal(SIGPIPE, cleanup);
1716 signal(SIGALRM, cleanup);
1717 signal(SIGTERM, cleanup);
1718 signal(SIGXCPU, cleanup);
1719 signal(SIGXFSZ, cleanup);
1720 signal(SIGVTALRM, cleanup);
1721 signal(SIGUSR1, cleanup);
1722 signal(SIGUSR2, cleanup);
1723 signal(SIGBUS, segv);
1724 signal(SIGSEGV, segv);
1726 initstate(seed, state, 256);
1729 open_test_files(argv, argc);
1731 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1732 strncat(goodfile, ".fsxgood", PATH_MAX - strlen(goodfile) - 1);
1733 fsxgoodfd = open(goodfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
1734 if (fsxgoodfd < 0) {
1738 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1739 strncat(logfile, ".fsxlog", PATH_MAX - strlen(logfile) - 1);
1740 fsxlogf = fopen(logfile, "w");
1749 maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1750 file_size = maxfilelen;
1751 if (file_size == (off_t)-1) {
1753 warn("%s: lseek eof", __func__);
1756 ret = lseek(fd, (off_t)0, SEEK_SET);
1757 if (ret == (off_t)-1) {
1759 warn("%s: lseek 0", __func__);
1763 original_buf = (char *)malloc(maxfilelen);
1766 for (i = 0; i < maxfilelen; i++)
1767 original_buf[i] = random() % 256;
1771 ret = posix_memalign((void **)&good_buf, writebdy, maxfilelen);
1773 prt("%s: posix_memalign failed: %s\n", __func__,
1778 ret = posix_memalign((void **)&temp_buf, readbdy, maxoplen);
1780 prt("%s: posix_memalign failed: %s\n", __func__,
1785 good_buf = malloc(maxfilelen);
1787 prt("malloc failed.\n");
1791 temp_buf = malloc(maxoplen);
1793 prt("malloc failed.\n");
1797 memset(good_buf, 0, maxfilelen);
1798 memset(temp_buf, 0, maxoplen);
1800 if (lite) { /* zero entire existing file */
1804 written = write(fd, good_buf, (size_t)maxfilelen);
1805 if (written != maxfilelen) {
1806 if (written == -1) {
1808 warn("%s: error on write", __func__);
1810 warn("%s: short write, 0x%x bytes instead of 0x%lx\n",
1811 __func__, (unsigned int)written,
1820 if (fallocate_calls)
1821 fallocate_calls = test_fallocate(0);
1823 if (punch_hole_calls)
1824 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE |
1825 FALLOC_FL_KEEP_SIZE);
1827 if (zero_range_calls)
1828 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1830 fl_keep_size = test_fallocate(FALLOC_FL_KEEP_SIZE);
1832 while (numops == -1 || numops--)
1836 prt("All operations completed A-OK!\n");