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",
686 ((progressinterval && testcalls % progressinterval == 0) ||
687 (debug && (monitorstart == -1 ||
688 (offset + size > monitorstart &&
689 (monitorend == -1 || offset <= monitorend)))))))
692 prt("%06lu%s %lu.%06u %-10s %#08x %s %#08x\t(0x05%x bytes)\n",
693 testcalls, fill_tf_buf(tf), tv.tv_sec, (int)tv.tv_usec,
694 ops[op], offset, op == OP_TRUNCATE ? " to " : "thru",
695 offset + size - 1, (int)size < 0 ? -(int)size : size);
698 void output_debug(unsigned int offset, unsigned int size, const char *what)
702 if (!quiet && (debug > 1 && (monitorstart == -1 ||
703 (offset + size >= monitorstart &&
704 (monitorend == -1 || offset <= monitorend))))) {
705 gettimeofday(&t, NULL);
706 prt(" %lu.%06u %s\n", t.tv_sec, (int)t.tv_usec, what);
711 doflush(unsigned int offset, unsigned int size)
713 unsigned int pg_offset;
714 unsigned int map_size;
716 struct test_file *tf = get_tf();
722 pg_offset = offset & page_mask;
723 map_size = pg_offset + size;
725 p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
726 MAP_FILE | MAP_SHARED, fd,
727 (off_t)(offset - pg_offset));
728 if (p == (char *)-1) {
732 if (msync(p, map_size, MS_INVALIDATE) != 0) {
736 if (munmap(p, map_size) != 0) {
740 output_debug(offset, size, "flush done");
744 doread(unsigned int offset, unsigned int size)
748 struct test_file *tf = get_tf();
751 offset -= offset % readbdy;
753 size -= size % readbdy;
756 if (!quiet && testcalls > simulatedopcount && !tf->o_direct)
757 prt("skipping zero size read\n");
758 log4(OP_SKIPPED, OP_READ, offset, size);
761 if (size + offset > file_size) {
762 if (!quiet && testcalls > simulatedopcount)
763 prt("skipping seek/read past end of file\n");
764 log4(OP_SKIPPED, OP_READ, offset, size);
768 log4(OP_READ + tf->o_direct, offset, size, 0);
770 if (testcalls <= simulatedopcount)
773 output_line(tf, OP_READ + tf->o_direct, offset, size);
775 ret = lseek(fd, (off_t)offset, SEEK_SET);
776 if (ret == (off_t)-1) {
780 iret = read(fd, temp_buf, size);
781 output_debug(offset, size, "read done");
786 prt("short read: 0x%x bytes instead of 0x%x\n",
790 check_buffers(offset, size);
794 check_eofpage(char *s, unsigned int offset, char *p, int size)
796 long last_page, should_be_zero;
798 if (offset + size <= (file_size & ~page_mask))
801 * we landed in the last page of the file
802 * test to make sure the VM system provided 0's
803 * beyond the true end of the file mapping
804 * (as required by mmap def in 1996 posix 1003.1)
806 last_page = ((long)p + (offset & page_mask) + size) & ~page_mask;
808 for (should_be_zero = last_page + (file_size & page_mask);
809 should_be_zero < last_page + page_size;
811 if (*(char *)should_be_zero) {
812 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%lx is 0x%04x\n",
813 s, (long long)file_size - 1,
814 should_be_zero & page_mask,
815 short_at(should_be_zero));
821 domapread(unsigned int offset, unsigned int size)
823 unsigned int pg_offset;
824 unsigned int map_size;
828 offset -= offset % readbdy;
832 if (!quiet && testcalls > simulatedopcount)
833 prt("skipping zero size read\n");
834 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
837 if (size + offset > file_size) {
838 if (!quiet && testcalls > simulatedopcount)
839 prt("skipping seek/read past end of file\n");
840 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
844 log4(OP_MAPREAD, offset, size, 0);
846 if (testcalls <= simulatedopcount)
849 output_line(tf, OP_MAPREAD, offset, size);
851 pg_offset = offset & page_mask;
852 map_size = pg_offset + size;
854 p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
855 (off_t)(offset - pg_offset));
856 if (p == MAP_FAILED) {
860 output_debug(offset, size, "mmap done");
861 if (setjmp(jmpbuf) == 0) {
863 memcpy(temp_buf, p + pg_offset, size);
864 check_eofpage("Read", offset, p, size);
867 report_failure(1901);
869 output_debug(offset, size, "memcpy done");
870 if (munmap(p, map_size) != 0) {
874 output_debug(offset, size, "munmap done");
876 check_buffers(offset, size);
880 gendata(char *original_buf, char *good_buf, unsigned int offset,
884 good_buf[offset] = testcalls % 256;
886 good_buf[offset] += original_buf[offset];
892 dowrite(unsigned int offset, unsigned int size)
900 offset -= offset % writebdy;
902 size -= size % writebdy;
904 if (!quiet && testcalls > simulatedopcount && !tf->o_direct)
905 prt("skipping zero size write\n");
906 log4(OP_SKIPPED, OP_WRITE, offset, size);
910 log4(OP_WRITE + tf->o_direct, offset, size, file_size);
912 gendata(original_buf, good_buf, offset, size);
913 if (file_size < offset + size) {
914 if (file_size < offset)
915 memset(good_buf + file_size, '\0', offset - file_size);
916 file_size = offset + size;
918 warn("Lite file size bug in fsx!");
923 if (testcalls <= simulatedopcount)
926 output_line(tf, OP_WRITE + tf->o_direct, offset, size);
928 ret = lseek(fd, (off_t)offset, SEEK_SET);
929 if (ret == (off_t)-1) {
933 iret = write(fd, good_buf + offset, size);
934 output_debug(offset, size, "write done");
939 prt("short write: 0x%x bytes instead of 0x%x\n",
945 prt("fsync() failed: %s\n", strerror(errno));
948 output_debug(offset, size, "fsync done");
951 doflush(offset, size);
952 output_debug(offset, size, "flush done");
957 domapwrite(unsigned int offset, unsigned int size)
959 unsigned int pg_offset;
960 unsigned int map_size;
967 offset -= offset % writebdy;
969 if (!quiet && testcalls > simulatedopcount)
970 prt("skipping zero size write\n");
971 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
974 cur_filesize = file_size;
976 log4(OP_MAPWRITE, offset, size, 0);
978 gendata(original_buf, good_buf, offset, size);
979 if (file_size < offset + size) {
980 if (file_size < offset)
981 memset(good_buf + file_size, '\0', offset - file_size);
982 file_size = offset + size;
984 warn("Lite file size bug in fsx!");
989 if (testcalls <= simulatedopcount)
992 output_line(tf, OP_MAPWRITE, offset, size);
994 if (file_size > cur_filesize) {
995 if (ftruncate(fd, file_size) == -1) {
999 output_debug(offset, size, "truncate done");
1001 pg_offset = offset & page_mask;
1002 map_size = pg_offset + size;
1004 p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
1005 fd, (off_t)(offset - pg_offset));
1006 if (p == MAP_FAILED) {
1008 report_failure(202);
1010 output_debug(offset, map_size, "mmap done");
1011 if (setjmp(jmpbuf) == 0) {
1013 memcpy(p + pg_offset, good_buf + offset, size);
1014 if (msync(p, map_size, MS_SYNC) != 0) {
1016 report_failure(203);
1018 check_eofpage("Write", offset, p, size);
1021 report_failure(2021);
1023 output_debug(offset, map_size, "msync done");
1024 if (munmap(p, map_size) != 0) {
1026 report_failure(204);
1028 output_debug(offset, map_size, "munmap done");
1032 dotruncate(unsigned int size)
1034 int oldsize = file_size;
1039 size -= size % truncbdy;
1040 if (size > biggest) {
1042 if (!quiet && testcalls > simulatedopcount)
1043 prt("truncating to largest ever: 0x%x\n", size);
1046 log4(OP_TRUNCATE, size, (unsigned int)file_size, 0);
1048 if (size > file_size)
1049 memset(good_buf + file_size, '\0', size - file_size);
1052 if (testcalls <= simulatedopcount)
1055 output_line(tf, OP_TRUNCATE, oldsize, size - oldsize);
1057 if (ftruncate(fd, (off_t)size) == -1) {
1058 prt("ftruncate: 0x%x\n", size);
1059 prterr("ftruncate");
1060 report_failure(160);
1062 output_debug(size, 0, "truncate done");
1066 do_punch_hole(unsigned int offset, unsigned int length)
1070 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
1076 if (!quiet && testcalls > simulatedopcount) {
1077 prt("skipping zero length punch hole\n");
1078 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
1083 if (file_size <= (loff_t)offset) {
1084 if (!quiet && testcalls > simulatedopcount) {
1085 prt("skipping hole punch off the end of the file\n");
1086 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
1091 log4(OP_PUNCH_HOLE, offset, length, 0);
1093 if (testcalls <= simulatedopcount)
1096 output_line(tf, OP_PUNCH_HOLE, offset, length);
1097 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1098 prt("punch hole: %x to %x\n", offset, length);
1099 prterr("fallocate");
1100 report_failure(161);
1102 output_debug(offset, length, "zero_range done");
1104 max_offset = offset < file_size ? offset : file_size;
1105 max_len = max_offset + length <= file_size ? length :
1106 file_size - max_offset;
1107 memset(good_buf + max_offset, '\0', max_len);
1111 do_zero_range(unsigned int offset, unsigned int length)
1113 unsigned int end_offset;
1114 int mode = FALLOC_FL_ZERO_RANGE;
1121 if (!quiet && testcalls > simulatedopcount) {
1122 prt("skipping zero length zero range\n");
1123 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, length);
1128 keep_size = random() % 2;
1130 end_offset = keep_size ? 0 : offset + length;
1132 if (end_offset > biggest) {
1133 biggest = end_offset;
1134 if (!quiet && testcalls > simulatedopcount)
1135 prt("zero_range to largest ever: 0x%x\n", end_offset);
1139 * last arg matches fallocate string array index in logdump:
1140 * 0: allocate past EOF
1141 * 1: extending prealloc
1142 * 2: interior prealloc
1144 log4(OP_ZERO_RANGE, offset, length,
1145 (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
1147 if (testcalls <= simulatedopcount)
1150 output_line(tf, OP_TRUNCATE, offset, length);
1152 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1153 prt("pzero range: %x to %x\n", offset, length);
1154 prterr("fallocate");
1155 report_failure(161);
1157 output_debug(offset, length, "zero_range done");
1159 memset(good_buf + offset, '\0', length);
1163 * fallocate is basically a no-op unless extending,
1164 * then a lot like a truncate
1167 do_preallocate(unsigned int offset, unsigned int length)
1173 struct stat statbufs;
1178 if (!quiet && testcalls > simulatedopcount)
1179 prt("skipping zero length fallocate\n");
1180 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
1184 keep_size = fl_keep_size && (random() % 2);
1186 end_offset = keep_size ? 0 : 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) {
1204 memset(good_buf + file_size, '\0', end_offset - file_size);
1205 file_size = end_offset;
1207 new_offset = file_size - (offset + length);
1208 length = length + new_offset;
1211 if (testcalls <= simulatedopcount)
1214 fstat(fd, &statbufs);
1215 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset,
1216 (loff_t)length) == -1) {
1217 prt("fallocate: %x to %x\n", offset, length);
1218 prterr("fallocate");
1219 report_failure(161);
1221 output_line(tf, OP_FALLOCATE, offset, length);
1222 output_debug(offset, length, "fallocate done");
1231 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1233 report_failure(171);
1235 iret = write(fd, good_buf, file_size);
1236 if ((off_t)iret != file_size) {
1240 prt("short write: 0x%lx bytes instead of 0x%llx\n",
1241 (unsigned long)iret, (unsigned long long)file_size);
1242 report_failure(172);
1244 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1245 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1246 prterr("ftruncate");
1247 report_failure(173);
1255 const char *tf_num = "";
1257 if (testcalls <= simulatedopcount)
1262 direct = (random() % (o_direct + 1)) ? OP_DIRECT : 0;
1264 log4(OP_CLOSEOPEN + direct, file_size, (unsigned int)file_size, 0);
1266 if (fd_policy != FD_SINGLE)
1267 tf_num = fill_tf_buf(tf);
1270 prt("%06lu %lu.%06u %sclose/open%s\n", testcalls, tv.tv_sec,
1271 (int)tv.tv_usec, tf_num, direct ? "(O_DIRECT)" : "");
1273 report_failure(180);
1275 output_debug(monitorstart, 0, "close done");
1276 tf->o_direct = direct;
1277 tf->fd = open(tf->path, O_RDWR | tf->o_direct, 0);
1279 prterr(tf->o_direct ? "open(O_DIRECT)" : "open");
1280 report_failure(181);
1282 output_debug(monitorstart, 0,
1283 tf->o_direct ? "open(O_DIRECT) done" : "open done");
1286 #define TRIM_OFF_LEN(off, len, size) \
1292 if ((off) + (len) > (size)) \
1293 (len) = (size) - (off); \
1299 unsigned long offset;
1300 unsigned long size = maxoplen;
1301 unsigned long rv = random();
1305 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1311 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1313 if (debugstart > 0 && testcalls >= debugstart)
1316 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1317 prt("%lu...\n", testcalls);
1321 size = random() % (maxoplen + 1);
1323 /* calculate appropriate op to run */
1325 op = rv % OP_MAX_LITE;
1327 op = rv % OP_MAX_FULL;
1339 if (!fallocate_calls) {
1340 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1345 if (!punch_hole_calls) {
1346 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1351 if (!zero_range_calls) {
1352 log4(OP_SKIPPED, OP_ZERO_RANGE, offset, size);
1360 TRIM_OFF_LEN(offset, size, file_size);
1361 doread(offset, size);
1364 TRIM_OFF_LEN(offset, size, maxfilelen);
1365 dowrite(offset, size);
1368 TRIM_OFF_LEN(offset, size, file_size);
1369 domapread(offset, size);
1372 TRIM_OFF_LEN(offset, size, maxfilelen);
1373 domapwrite(offset, size);
1377 size = random() % maxfilelen;
1381 TRIM_OFF_LEN(offset, size, maxfilelen);
1382 do_preallocate(offset, size);
1385 TRIM_OFF_LEN(offset, size, file_size);
1386 do_punch_hole(offset, size);
1389 TRIM_OFF_LEN(offset, size, file_size);
1390 do_zero_range(offset, size);
1397 prterr("unknown operation %d: Operation not supported");
1403 if (sizechecks && testcalls > simulatedopcount)
1414 report_failure(9999);
1422 prt("signal %d\n", sig);
1423 prt("testcalls = %lu\n", testcalls);
1431 "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"
1432 " -b opnum: beginning operation number (default 1)\n"
1433 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1434 " -d: debug output for all operations [-d -d = more debugging]\n"
1435 " -f flush and invalidate cache after I/O\n"
1436 /* OSX: -d duration: number of hours for the tool to run\n\ */
1437 /* OSX: -e: tests using an extended attribute rather than a file\n\ */
1438 /* OSX: -f forkname: test the named fork of fname\n\ */
1439 /* OSX: -g logpath: path for .fsxlog file\n\ */
1440 /* OSX: -h: write 0s instead of creating holes (i.e. sparse file)\n\ */
1441 /* OSX: -i: interactive mode, hit return before performing each operation\n\ */
1442 " -l flen: the upper bound on file size (default 262144)\n"
1443 " -m startop:endop: monitor (print debug output) specified byte range\n"
1444 " (default 0:infinity)\n"
1445 " -n: no verifications of file size\n"
1446 " -o oplen: the upper bound on operation size (default 65536)\n"
1447 " -p progressinterval: debug output at specified operation interval\n"
1448 " -q: quieter operation\n"
1449 " -r readbdy: %1$u would make reads page aligned (default 1)\n"
1450 " -s style: 1 gives smaller truncates (default 0)\n"
1451 " -t truncbdy: %1$u would make truncates page aligned (default 1)\n"
1452 " -w writebdy: %1$u would make writes page aligned (default 1)\n"
1453 /* XFS: -x: preallocate file space before starting, XFS only (default 0)\n\ */
1454 " -y synchronize changes to a file\n"
1455 /* OSX: -v: debug output for all operations\n\ */
1456 /* XFS: -A: Use the AIO system calls\n" */
1457 /* OSX: -C mix cached and un-cached read/write ops\n\ */
1458 " -D startingop: debug output starting at specified operation\n"
1459 " -F: Do not use fallocate (preallocation) calls\n"
1460 /* OSX: -G logsize: #entries in oplog (default 1024)\n\ */
1461 #ifdef FALLOC_FL_PUNCH_HOLE
1462 " -H: Do not use punch hole calls\n"
1464 #ifdef FALLOC_FL_ZERO_RANGE
1465 " -z: Do not use zero range calls\n"
1467 /* XFS: -C: Do not use collapse range calls\n\ */
1468 " -I [rotate|random]: When multiple paths to the file are given,\n"
1469 " each operation uses a different path. Iterate through them in\n"
1470 " order with 'rotate' or chose them at 'random'. (default random)\n"
1471 " -L: fsxLite - no file creations & no file size changes\n"
1472 /* OSX: -I: start interactive mode since operation opnum\n\ */
1473 /* OSX: -M: slow motion mode, wait 1 second before each op\n\ */
1474 " -N numops: total # operations to do (default infinity)\n"
1475 " -O: use oplen (see -o flag) for every op (default random)\n"
1476 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1477 " -R: read() system calls only (mapped reads disabled)\n"
1478 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1479 /* OSX: -T datasize: atomic data element write size [1,2,4] (default 4)\n\ */
1480 " -W: mapped write operations DISabled\n"
1482 " -Z[P]: O_DIRECT file IO [1 in P chance for each open] (default off)\n"
1484 " fname: this filename is REQUIRED (no default)\n",
1490 getnum(char *s, char **e)
1495 ret = strtol(s, e, 0);
1523 test_fallocate(int mode)
1529 /* Must go more than a page away so let's go 4M to be sure */
1530 if (fallocate(fd, mode, 0, 4096*1024) && errno == EOPNOTSUPP) {
1532 warn("%s: filesystem does not support fallocate mode 0x%x, disabling!",
1538 /* Always call ftruncate since file size might be adjusted
1539 * by fallocate even on error
1541 if (ftruncate(fd, 0) == -1)
1542 warn("ftruncate to 0 size failed");
1548 main(int argc, char **argv)
1557 page_size = getpagesize();
1558 page_mask = page_size - 1;
1560 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1562 while ((ch = getopt(argc, argv,
1563 "b:c:dfl:m:no:p:qr:s:t:w:xyzD:FHI:LN:OP:RS:WZ::"))
1567 simulatedopcount = getnum(optarg, &endp);
1569 fprintf(stdout, "Will begin at operation %ld\n",
1571 if (simulatedopcount == 0)
1573 simulatedopcount -= 1;
1576 closeprob = getnum(optarg, &endp);
1579 "Chance of close/open is 1 in %d\n",
1591 maxfilelen = getnum(optarg, &endp);
1592 if (maxfilelen <= 0)
1596 monitorstart = getnum(optarg, &endp);
1597 if (monitorstart < 0)
1599 if (!endp || *endp++ != ':')
1601 monitorend = getnum(endp, &endp);
1604 if (monitorend == 0)
1605 monitorend = -1; /* aka infinity */
1611 maxoplen = getnum(optarg, &endp);
1616 progressinterval = getnum(optarg, &endp);
1617 if (progressinterval <= 0)
1624 readbdy = getnum(optarg, &endp);
1629 style = getnum(optarg, &endp);
1630 if (style < 0 || style > 1)
1634 truncbdy = getnum(optarg, &endp);
1639 writebdy = getnum(optarg, &endp);
1647 debugstart = getnum(optarg, &endp);
1652 fallocate_calls = 0;
1655 punch_hole_calls = 0;
1658 zero_range_calls = 0;
1661 assign_fd_policy(optarg);
1667 numops = getnum(optarg, &endp);
1675 strncpy(goodfile, optarg, sizeof(goodfile) - 1);
1676 strncat(goodfile, "/", PATH_MAX - strlen(goodfile) - 1);
1677 strncpy(logfile, optarg, sizeof(logfile) - 1);
1678 strncat(logfile, "/", PATH_MAX - strlen(logfile) - 1);
1685 seed = getnum(optarg, &endp);
1687 seed = time(0) % 10000;
1689 fprintf(stdout, "Seed set to %d\n", seed);
1696 fprintf(stdout, "mapped writes DISABLED\n");
1701 o_direct = getnum(optarg, &endp);
1702 if (!optarg || o_direct == 0)
1716 signal(SIGHUP, cleanup);
1717 signal(SIGINT, cleanup);
1718 signal(SIGPIPE, cleanup);
1719 signal(SIGALRM, cleanup);
1720 signal(SIGTERM, cleanup);
1721 signal(SIGXCPU, cleanup);
1722 signal(SIGXFSZ, cleanup);
1723 signal(SIGVTALRM, cleanup);
1724 signal(SIGUSR1, cleanup);
1725 signal(SIGUSR2, cleanup);
1726 signal(SIGBUS, segv);
1727 signal(SIGSEGV, segv);
1729 initstate(seed, state, 256);
1732 open_test_files(argv, argc);
1734 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1735 strncat(goodfile, ".fsxgood", PATH_MAX - strlen(goodfile) - 1);
1736 fsxgoodfd = open(goodfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
1737 if (fsxgoodfd < 0) {
1741 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1742 strncat(logfile, ".fsxlog", PATH_MAX - strlen(logfile) - 1);
1743 fsxlogf = fopen(logfile, "w");
1752 maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1753 file_size = maxfilelen;
1754 if (file_size == (off_t)-1) {
1756 warn("%s: lseek eof", __func__);
1759 ret = lseek(fd, (off_t)0, SEEK_SET);
1760 if (ret == (off_t)-1) {
1762 warn("%s: lseek 0", __func__);
1766 original_buf = (char *)malloc(maxfilelen);
1769 for (i = 0; i < maxfilelen; i++)
1770 original_buf[i] = random() % 256;
1774 ret = posix_memalign((void **)&good_buf, writebdy, maxfilelen);
1776 prt("%s: posix_memalign failed: %s\n", __func__,
1781 ret = posix_memalign((void **)&temp_buf, readbdy, maxoplen);
1783 prt("%s: posix_memalign failed: %s\n", __func__,
1788 good_buf = malloc(maxfilelen);
1790 prt("malloc failed.\n");
1794 temp_buf = malloc(maxoplen);
1796 prt("malloc failed.\n");
1800 memset(good_buf, 0, maxfilelen);
1801 memset(temp_buf, 0, maxoplen);
1803 if (lite) { /* zero entire existing file */
1807 written = write(fd, good_buf, (size_t)maxfilelen);
1808 if (written != maxfilelen) {
1809 if (written == -1) {
1811 warn("%s: error on write", __func__);
1813 warn("%s: short write, 0x%x bytes instead of 0x%lx\n",
1814 __func__, (unsigned int)written,
1823 if (fallocate_calls)
1824 fallocate_calls = test_fallocate(0);
1826 if (punch_hole_calls)
1827 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE |
1828 FALLOC_FL_KEEP_SIZE);
1830 if (zero_range_calls)
1831 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1833 fl_keep_size = test_fallocate(FALLOC_FL_KEEP_SIZE);
1835 while (numops == -1 || numops--)
1839 prt("All operations completed A-OK!\n");