2 * Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserverd.
5 * Author: Avadis Tevanian, Jr.
7 * File system exerciser.
9 * Rewritten 8/98 by Conrad Minshall.
11 * Small changes to work under Linux -- davej.
13 * Checks for mmap last-page zero fill.
37 #include <linux/falloc.h>
44 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
47 * A log entry is an operation and a bunch of arguments.
57 struct log_entry oplog[LOGSIZE]; /* the log */
58 int logptr = 0; /* current position in log */
59 int logcount = 0; /* total ops */
62 * The operation matrix is complex due to conditional execution of different
63 * features. Hence when we come to deciding what operation to run, we need to
64 * be careful in how we select the different operations. The active operations
65 * are mapped to numbers as follows:
76 * When mapped read/writes are disabled, they are simply converted to normal
77 * reads and writes. When fallocate/fpunch calls are disabled, they are
78 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
79 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
80 * operation modifier rather than an operation in itself.
82 * Because of the "lite" version, we also need to have different "maximum
83 * operation" defines to allow the ops to be selected correctly based on the
87 /* common operations */
94 /* !lite operations */
96 #define OP_FALLOCATE 5
97 #define OP_PUNCH_HOLE 6
100 /* operation modifiers */
101 #define OP_CLOSEOPEN 100
102 #define OP_SKIPPED 101
105 #define PAGE_SIZE getpagesize()
107 #define PAGE_MASK (PAGE_SIZE - 1)
109 char *original_buf; /* a pointer to the original data */
110 char *good_buf; /* a pointer to the correct data */
111 char *temp_buf; /* a pointer to the current data */
112 char *fname; /* name of our test file */
113 int fd; /* fd for our test file */
118 unsigned long testcalls = 0; /* calls to function "test" */
120 unsigned long simulatedopcount = 0; /* -b flag */
121 int closeprob = 0; /* -c flag */
122 int debug = 0; /* -d flag */
123 unsigned long debugstart = 0; /* -D flag */
124 int flush = 0; /* -f flag */
125 int do_fsync = 0; /* -y flag */
126 unsigned long maxfilelen = 256 * 1024; /* -l flag */
127 int sizechecks = 1; /* -n flag disables them */
128 int maxoplen = 64 * 1024; /* -o flag */
129 int quiet = 0; /* -q flag */
130 unsigned long progressinterval = 0; /* -p flag */
131 int readbdy = 1; /* -r flag */
132 int style = 0; /* -s flag */
133 int prealloc = 0; /* -x flag */
134 int truncbdy = 1; /* -t flag */
135 int writebdy = 1; /* -w flag */
136 long monitorstart = -1; /* -m flag */
137 long monitorend = -1; /* -m flag */
138 int lite = 0; /* -L flag */
139 long numops = -1; /* -N flag */
140 int randomoplen = 1; /* -O flag disables it */
141 int seed = 1; /* -S flag */
142 int mapped_writes = 1; /* -W flag disables */
143 int fallocate_calls = 1; /* -F flag disables */
144 int punch_hole_calls = 1; /* -H flag disables */
145 int mapped_reads = 1; /* -R flag disables it */
147 int o_direct; /* -Z */
154 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
157 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
158 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
160 #define fsxread(a,b,c,d) read(a,b,c)
161 #define fsxwrite(a,b,c,d) write(a,b,c)
164 FILE * fsxlogf = NULL;
168 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
170 unsigned long ret = (unsigned long)ptr;
172 ret = ((ret + align - 1) & ~(align - 1));
178 vwarnc(int code, const char *fmt, va_list ap) {
179 fprintf(stderr, "fsx: ");
181 vfprintf(stderr, fmt, ap);
182 fprintf(stderr, ": ");
184 fprintf(stderr, "%s\n", strerror(code));
188 warn(const char * fmt, ...) {
191 vwarnc(errno, fmt, ap);
195 #define BUF_SIZE 1024
201 char buffer[BUF_SIZE];
204 vsnprintf(buffer, BUF_SIZE, fmt, args);
206 fprintf(stdout, buffer);
208 fprintf(fsxlogf, buffer);
214 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
219 log4(int operation, int arg0, int arg1, int arg2)
221 struct log_entry *le;
224 le->operation = operation;
226 le->operation = ~ le->operation;
232 if (logptr >= LOGSIZE)
241 struct log_entry *lp;
242 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
244 prt("LOG DUMP (%d total operations):\n", logcount);
245 if (logcount < LOGSIZE) {
252 for ( ; count > 0; count--) {
255 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
256 prt("%d(%3d mod 256): ", opnum, opnum%256);
258 if ((closeopen = lp->operation < 0))
259 lp->operation = ~ lp->operation;
261 switch (lp->operation) {
263 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
264 lp->args[0], lp->args[0] + lp->args[1] - 1,
266 if (badoff >= lp->args[0] && badoff <
267 lp->args[0] + lp->args[1])
271 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
272 lp->args[0], lp->args[0] + lp->args[1] - 1,
274 if (badoff >= lp->args[0] && badoff <
275 lp->args[0] + lp->args[1])
279 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
280 lp->args[0], lp->args[0] + lp->args[1] - 1,
282 if (badoff >= lp->args[0] &&
283 badoff < lp->args[0] + lp->args[1])
287 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
288 lp->args[0], lp->args[0] + lp->args[1] - 1,
290 if (lp->args[0] > lp->args[2])
292 else if (lp->args[0] + lp->args[1] > lp->args[2])
294 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
295 badoff < lp->args[0] + lp->args[1])
299 down = lp->args[0] < lp->args[1];
300 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
301 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
302 if (badoff >= lp->args[!down] &&
303 badoff < lp->args[!!down])
307 /* 0: offset 1: length 2: where alloced */
308 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
309 lp->args[0], lp->args[0] + lp->args[1],
310 lp->args[1], falloc_type[lp->args[2]]);
311 if (badoff >= lp->args[0] &&
312 badoff < lp->args[0] + lp->args[1])
316 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
317 lp->args[0], lp->args[0] + lp->args[1] - 1,
319 if (badoff >= lp->args[0] && badoff <
320 lp->args[0] + lp->args[1])
324 prt("SKIPPED (no operation)");
327 prt("BOGUS LOG ENTRY (operation code = %d)!",
331 prt("\n\t\tCLOSE/OPEN");
341 save_buffer(char *buffer, off_t bufferlength, int fd)
344 ssize_t byteswritten;
346 if (fd <= 0 || bufferlength == 0)
349 if (bufferlength > SSIZE_MAX) {
350 prt("fsx flaw: overflow in save_buffer\n");
354 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
355 if (size_by_seek == (off_t)-1)
356 prterr("save_buffer: lseek eof");
357 else if (bufferlength > size_by_seek) {
358 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
359 (unsigned long long)bufferlength);
360 bufferlength = size_by_seek;
364 ret = lseek(fd, (off_t)0, SEEK_SET);
365 if (ret == (off_t)-1)
366 prterr("save_buffer: lseek 0");
368 byteswritten = write(fd, buffer, (size_t)bufferlength);
369 if (byteswritten != bufferlength) {
370 if (byteswritten == -1)
371 prterr("save_buffer write");
373 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
374 (unsigned)byteswritten,
375 (unsigned long long)bufferlength);
381 report_failure(int status)
387 save_buffer(good_buf, file_size, fsxgoodfd);
388 prt("Correct content saved for comparison\n");
389 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
398 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
399 *(((unsigned char *)(cp)) + 1)))
402 check_buffers(unsigned offset, unsigned size)
410 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
411 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
412 offset, size, fname);
413 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
415 c = good_buf[offset];
419 bad = short_at(&temp_buf[i]);
420 prt("0x%5x\t0x%04x\t0x%04x", offset,
421 short_at(&good_buf[offset]), bad);
422 op = temp_buf[offset & 1 ? i+1 : i];
425 prt("operation# (mod 256) for "
426 "the bad data may be %u\n",
427 ((unsigned)op & 0xff));
429 prt("operation# (mod 256) for "
430 "the bad data unknown, check"
431 " HOLE and EXTEND ops\n");
451 if (fstat(fd, &statbuf)) {
452 prterr("check_size: fstat");
453 statbuf.st_size = -1;
455 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
456 if (file_size != statbuf.st_size || file_size != size_by_seek) {
457 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
458 (unsigned long long)file_size,
459 (unsigned long long)statbuf.st_size,
460 (unsigned long long)size_by_seek);
467 check_trunc_hack(void)
471 ftruncate(fd, (off_t)0);
472 ftruncate(fd, (off_t)100000);
474 if (statbuf.st_size != (off_t)100000) {
475 prt("no extend on truncate! not posix!\n");
482 doflush(unsigned offset, unsigned size)
488 if (o_direct == O_DIRECT)
491 pg_offset = offset & mmap_mask;
492 map_size = pg_offset + size;
494 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
495 MAP_FILE | MAP_SHARED, fd,
496 (off_t)(offset - pg_offset))) == (char *)-1) {
497 prterr("doflush: mmap");
500 if (msync(p, map_size, MS_INVALIDATE) != 0) {
501 prterr("doflush: msync");
504 if (munmap(p, map_size) != 0) {
505 prterr("doflush: munmap");
511 doread(unsigned offset, unsigned size)
516 offset -= offset % readbdy;
518 size -= size % readbdy;
520 if (!quiet && testcalls > simulatedopcount && !o_direct)
521 prt("skipping zero size read\n");
522 log4(OP_SKIPPED, OP_READ, offset, size);
525 if (size + offset > file_size) {
526 if (!quiet && testcalls > simulatedopcount)
527 prt("skipping seek/read past end of file\n");
528 log4(OP_SKIPPED, OP_READ, offset, size);
532 log4(OP_READ, offset, size, 0);
534 if (testcalls <= simulatedopcount)
538 ((progressinterval && testcalls % progressinterval == 0) ||
540 (monitorstart == -1 ||
541 (offset + size > monitorstart &&
542 (monitorend == -1 || offset <= monitorend))))))
543 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
544 offset, offset + size - 1, size);
545 ret = lseek(fd, (off_t)offset, SEEK_SET);
546 if (ret == (off_t)-1) {
547 prterr("doread: lseek");
550 iret = fsxread(fd, temp_buf, size, offset);
553 prterr("doread: read");
555 prt("short read: 0x%x bytes instead of 0x%x\n",
559 check_buffers(offset, size);
564 check_eofpage(char *s, unsigned offset, char *p, int size)
566 unsigned long last_page, should_be_zero;
568 if (offset + size <= (file_size & ~page_mask))
571 * we landed in the last page of the file
572 * test to make sure the VM system provided 0's
573 * beyond the true end of the file mapping
574 * (as required by mmap def in 1996 posix 1003.1)
576 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
578 for (should_be_zero = last_page + (file_size & page_mask);
579 should_be_zero < last_page + page_size;
581 if (*(char *)should_be_zero) {
582 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
583 s, file_size - 1, should_be_zero & page_mask,
584 short_at(should_be_zero));
591 domapread(unsigned offset, unsigned size)
597 offset -= offset % readbdy;
599 if (!quiet && testcalls > simulatedopcount)
600 prt("skipping zero size read\n");
601 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
604 if (size + offset > file_size) {
605 if (!quiet && testcalls > simulatedopcount)
606 prt("skipping seek/read past end of file\n");
607 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
611 log4(OP_MAPREAD, offset, size, 0);
613 if (testcalls <= simulatedopcount)
617 ((progressinterval && testcalls % progressinterval == 0) ||
619 (monitorstart == -1 ||
620 (offset + size > monitorstart &&
621 (monitorend == -1 || offset <= monitorend))))))
622 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
623 offset, offset + size - 1, size);
625 pg_offset = offset & PAGE_MASK;
626 map_size = pg_offset + size;
628 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
629 (off_t)(offset - pg_offset))) == (char *)-1) {
630 prterr("domapread: mmap");
633 memcpy(temp_buf, p + pg_offset, size);
635 check_eofpage("Read", offset, p, size);
637 if (munmap(p, map_size) != 0) {
638 prterr("domapread: munmap");
642 check_buffers(offset, size);
647 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
650 good_buf[offset] = testcalls % 256;
652 good_buf[offset] += original_buf[offset];
659 dowrite(unsigned offset, unsigned size)
664 offset -= offset % writebdy;
666 size -= size % writebdy;
668 if (!quiet && testcalls > simulatedopcount && !o_direct)
669 prt("skipping zero size write\n");
670 log4(OP_SKIPPED, OP_WRITE, offset, size);
674 log4(OP_WRITE, offset, size, file_size);
676 gendata(original_buf, good_buf, offset, size);
677 if (file_size < offset + size) {
678 if (file_size < offset)
679 memset(good_buf + file_size, '\0', offset - file_size);
680 file_size = offset + size;
682 warn("Lite file size bug in fsx!");
687 if (testcalls <= simulatedopcount)
691 ((progressinterval && testcalls % progressinterval == 0) ||
693 (monitorstart == -1 ||
694 (offset + size > monitorstart &&
695 (monitorend == -1 || offset <= monitorend))))))
696 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
697 offset, offset + size - 1, size);
698 ret = lseek(fd, (off_t)offset, SEEK_SET);
699 if (ret == (off_t)-1) {
700 prterr("dowrite: lseek");
703 iret = fsxwrite(fd, good_buf + offset, size, offset);
706 prterr("dowrite: write");
708 prt("short write: 0x%x bytes instead of 0x%x\n",
714 prt("fsync() failed: %s\n", strerror(errno));
719 doflush(offset, size);
725 domapwrite(unsigned offset, unsigned size)
732 offset -= offset % writebdy;
734 if (!quiet && testcalls > simulatedopcount)
735 prt("skipping zero size write\n");
736 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
739 cur_filesize = file_size;
741 log4(OP_MAPWRITE, offset, size, 0);
743 gendata(original_buf, good_buf, offset, size);
744 if (file_size < offset + size) {
745 if (file_size < offset)
746 memset(good_buf + file_size, '\0', offset - file_size);
747 file_size = offset + size;
749 warn("Lite file size bug in fsx!");
754 if (testcalls <= simulatedopcount)
758 ((progressinterval && testcalls % progressinterval == 0) ||
760 (monitorstart == -1 ||
761 (offset + size > monitorstart &&
762 (monitorend == -1 || offset <= monitorend))))))
763 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
764 offset, offset + size - 1, size);
766 if (file_size > cur_filesize) {
767 if (ftruncate(fd, file_size) == -1) {
768 prterr("domapwrite: ftruncate");
772 pg_offset = offset & PAGE_MASK;
773 map_size = pg_offset + size;
775 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
776 MAP_FILE | MAP_SHARED, fd,
777 (off_t)(offset - pg_offset))) == (char *)-1) {
778 prterr("domapwrite: mmap");
781 memcpy(p + pg_offset, good_buf + offset, size);
782 if (msync(p, map_size, 0) != 0) {
783 prterr("domapwrite: msync");
787 check_eofpage("Write", offset, p, size);
789 if (munmap(p, map_size) != 0) {
790 prterr("domapwrite: munmap");
797 dotruncate(unsigned size)
799 int oldsize = file_size;
801 size -= size % truncbdy;
802 if (size > biggest) {
804 if (!quiet && testcalls > simulatedopcount)
805 prt("truncating to largest ever: 0x%x\n", size);
808 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
810 if (size > file_size)
811 memset(good_buf + file_size, '\0', size - file_size);
814 if (testcalls <= simulatedopcount)
817 if ((progressinterval && testcalls % progressinterval == 0) ||
818 (debug && (monitorstart == -1 || monitorend == -1 ||
819 size <= monitorend)))
820 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
821 if (ftruncate(fd, (off_t)size) == -1) {
822 prt("ftruncate1: %x\n", size);
823 prterr("dotruncate: ftruncate");
828 #ifdef FALLOC_FL_PUNCH_HOLE
830 do_punch_hole(unsigned offset, unsigned length)
835 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
838 if (!quiet && testcalls > simulatedopcount)
839 prt("skipping zero length punch hole\n");
840 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
844 if (file_size <= (loff_t)offset) {
845 if (!quiet && testcalls > simulatedopcount)
846 prt("skipping hole punch off the end of the file\n");
847 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
851 end_offset = offset + length;
853 log4(OP_PUNCH_HOLE, offset, length, 0);
855 if (testcalls <= simulatedopcount)
858 if ((progressinterval && testcalls % progressinterval == 0) ||
859 (debug && (monitorstart == -1 || monitorend == -1 ||
860 end_offset <= monitorend))) {
861 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
862 offset, offset+length, length);
864 if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
865 prt("%punch hole: %x to %x\n", offset, length);
866 prterr("do_punch_hole: fallocate");
871 max_offset = offset < file_size ? offset : file_size;
872 max_len = max_offset + length <= file_size ? length :
873 file_size - max_offset;
874 memset(good_buf + max_offset, '\0', max_len);
879 do_punch_hole(unsigned offset, unsigned length)
886 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
888 do_preallocate(unsigned offset, unsigned length)
894 if (!quiet && testcalls > simulatedopcount)
895 prt("skipping zero length fallocate\n");
896 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
900 keep_size = random() % 2;
902 end_offset = keep_size ? 0 : offset + length;
904 if (end_offset > biggest) {
905 biggest = end_offset;
906 if (!quiet && testcalls > simulatedopcount)
907 prt("fallocating to largest ever: 0x%x\n", end_offset);
911 * last arg matches fallocate string array index in logdump:
912 * 0: allocate past EOF
913 * 1: extending prealloc
914 * 2: interior prealloc
916 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 0 : 1) : 2);
918 if (end_offset > file_size) {
919 memset(good_buf + file_size, '\0', end_offset - file_size);
920 file_size = end_offset;
923 if (testcalls <= simulatedopcount)
926 if ((progressinterval && testcalls % progressinterval == 0) ||
927 (debug && (monitorstart == -1 || monitorend == -1 ||
928 end_offset <= monitorend)))
929 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
930 offset, offset + length, length);
931 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
932 prt("fallocate: %x to %x\n", offset, length);
933 prterr("do_preallocate: fallocate");
939 do_preallocate(unsigned offset, unsigned length)
950 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
951 prterr("writefileimage: lseek");
954 iret = write(fd, good_buf, file_size);
955 if ((off_t)iret != file_size) {
957 prterr("writefileimage: write");
959 prt("short write: 0x%x bytes instead of 0x%llx\n",
960 iret, (unsigned long long)file_size);
963 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
964 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
965 prterr("writefileimage: ftruncate");
974 if (testcalls <= simulatedopcount)
978 prt("%lu close/open\n", testcalls);
980 prterr("docloseopen: close");
983 fd = open(fname, O_RDWR|o_direct, 0);
985 prterr("docloseopen: open");
990 #define TRIM_OFF_LEN(off, len, size) \
996 if ((off) + (len) > (size)) \
997 (len) = (size) - (off); \
1003 unsigned long offset;
1004 unsigned long size = maxoplen;
1005 unsigned long rv = random();
1008 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1014 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1016 if (debugstart > 0 && testcalls >= debugstart)
1019 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1020 prt("%lu...\n", testcalls);
1024 size = random() % (maxoplen + 1);
1026 /* calculate appropriate op to run */
1028 op = rv % OP_MAX_LITE;
1030 op = rv % OP_MAX_FULL;
1042 if (!fallocate_calls) {
1043 log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
1048 if (!punch_hole_calls) {
1049 log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
1057 TRIM_OFF_LEN(offset, size, file_size);
1058 doread(offset, size);
1062 TRIM_OFF_LEN(offset, size, maxfilelen);
1063 dowrite(offset, size);
1067 TRIM_OFF_LEN(offset, size, file_size);
1068 domapread(offset, size);
1072 TRIM_OFF_LEN(offset, size, maxfilelen);
1073 domapwrite(offset, size);
1078 size = random() % maxfilelen;
1083 TRIM_OFF_LEN(offset, size, maxfilelen);
1084 do_preallocate(offset, size);
1088 TRIM_OFF_LEN(offset, size, file_size);
1089 do_punch_hole(offset, size);
1092 prterr("test: unknown operation");
1098 if (sizechecks && testcalls > simulatedopcount)
1110 prt("signal %d\n", sig);
1111 prt("testcalls = %lu\n", testcalls);
1119 fprintf(stdout, "usage: %s",
1120 "fsx [-dnqxAFLOWZ] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
1121 -b opnum: beginning operation number (default 1)\n\
1122 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1123 -d: debug output for all operations\n\
1124 -f flush and invalidate cache after I/O\n\
1125 -l flen: the upper bound on file size (default 262144)\n\
1126 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1127 -n: no verifications of file size\n\
1128 -o oplen: the upper bound on operation size (default 65536)\n\
1129 -p progressinterval: debug output at specified operation interval\n\
1130 -q: quieter operation\n\
1131 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1132 -s style: 1 gives smaller truncates (default 0)\n\
1133 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1134 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1135 -x: preallocate file space before starting, XFS only (default 0)\n\
1136 -y synchronize changes to a file\n"
1139 " -A: Use the AIO system calls\n"
1141 " -D startingop: debug output starting at specified operation\n"
1143 " -F: Do not use fallocate (preallocation) calls\n"
1145 #ifdef FALLOC_FL_PUNCH_HOLE
1146 " -H: Do not use punch hole calls\n"
1148 " -L: fsxLite - no file creations & no file size changes\n\
1149 -N numops: total # operations to do (default infinity)\n\
1150 -O: use oplen (see -o flag) for every op (default random)\n\
1151 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1152 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1153 -W: mapped write operations DISabled\n\
1154 -R: read() system calls only (mapped reads disabled)\n\
1155 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1156 fname: this filename is REQUIRED (no default)\n");
1162 getnum(char *s, char **e)
1167 ret = strtol(s, e, 0);
1197 io_context_t io_ctx;
1203 ret = io_queue_init(QSZ, &io_ctx);
1205 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1213 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1215 struct io_event event;
1216 static struct timespec ts;
1217 struct iocb *iocbs[] = { &iocb };
1222 io_prep_pread(&iocb, fd, buf, len, offset);
1224 io_prep_pwrite(&iocb, fd, buf, len, offset);
1229 ret = io_submit(io_ctx, 1, iocbs);
1231 fprintf(stderr, "errcode=%d\n", ret);
1232 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1237 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1240 fprintf(stderr, "aio_rw: no events available\n");
1242 fprintf(stderr, "errcode=%d\n", -ret);
1243 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1248 if (len != event.res) {
1250 * The b0rked libaio defines event.res as unsigned.
1251 * However the kernel strucuture has it signed,
1252 * and it's used to pass negated error value.
1253 * Till the library is fixed use the temp var.
1255 res = (long)event.res;
1257 fprintf(stderr, "bad io length: %lu instead of %u\n",
1260 fprintf(stderr, "errcode=%ld\n", -res);
1261 fprintf(stderr, "aio_rw: async io failed: %s\n",
1272 * The caller expects error return in traditional libc
1273 * convention, i.e. -1 and the errno set to error.
1279 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1284 ret = __aio_rw(rw, fd, buf, len, offset);
1287 ret = read(fd, buf, len);
1289 ret = write(fd, buf, len);
1300 if (!lite && fallocate_calls) {
1301 if (fallocate(fd, 0, 0, 1) && errno == EOPNOTSUPP) {
1303 warn("main: filesystem does not support fallocate, disabling\n");
1304 fallocate_calls = 0;
1309 #else /* ! FALLOCATE */
1310 fallocate_calls = 0;
1318 #ifdef FALLOC_FL_PUNCH_HOLE
1319 if (!lite && punch_hole_calls) {
1320 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1321 0, 1) && errno == EOPNOTSUPP) {
1323 warn("main: filesystem does not support fallocate punch hole, disabling");
1324 punch_hole_calls = 0;
1328 #else /* ! PUNCH HOLE */
1329 punch_hole_calls = 0;
1334 main(int argc, char **argv)
1338 char goodfile[1024];
1344 page_size = getpagesize();
1345 page_mask = page_size - 1;
1346 mmap_mask = page_mask;
1349 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1351 while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FHLN:OP:RS:WZ"))
1355 simulatedopcount = getnum(optarg, &endp);
1357 fprintf(stdout, "Will begin at operation %ld\n",
1359 if (simulatedopcount == 0)
1361 simulatedopcount -= 1;
1364 closeprob = getnum(optarg, &endp);
1367 "Chance of close/open is 1 in %d\n",
1379 maxfilelen = getnum(optarg, &endp);
1380 if (maxfilelen <= 0)
1384 monitorstart = getnum(optarg, &endp);
1385 if (monitorstart < 0)
1387 if (!endp || *endp++ != ':')
1389 monitorend = getnum(endp, &endp);
1392 if (monitorend == 0)
1393 monitorend = -1; /* aka infinity */
1399 maxoplen = getnum(optarg, &endp);
1404 progressinterval = getnum(optarg, &endp);
1405 if (progressinterval == 0)
1412 readbdy = getnum(optarg, &endp);
1417 style = getnum(optarg, &endp);
1418 if (style < 0 || style > 1)
1422 truncbdy = getnum(optarg, &endp);
1427 writebdy = getnum(optarg, &endp);
1441 debugstart = getnum(optarg, &endp);
1446 fallocate_calls = 0;
1449 punch_hole_calls = 0;
1455 numops = getnum(optarg, &endp);
1463 strncpy(goodfile, optarg, sizeof(goodfile));
1464 strcat(goodfile, "/");
1465 strncpy(logfile, optarg, sizeof(logfile));
1466 strcat(logfile, "/");
1472 seed = getnum(optarg, &endp);
1474 seed = time(0) % 10000;
1476 fprintf(stdout, "Seed set to %d\n", seed);
1483 fprintf(stdout, "mapped writes DISABLED\n");
1486 o_direct = O_DIRECT;
1498 signal(SIGHUP, cleanup);
1499 signal(SIGINT, cleanup);
1500 signal(SIGPIPE, cleanup);
1501 signal(SIGALRM, cleanup);
1502 signal(SIGTERM, cleanup);
1503 signal(SIGXCPU, cleanup);
1504 signal(SIGXFSZ, cleanup);
1505 signal(SIGVTALRM, cleanup);
1506 signal(SIGUSR1, cleanup);
1507 signal(SIGUSR2, cleanup);
1509 initstate(seed, state, 256);
1512 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1519 xfs_flock64_t resv = { 0 };
1520 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1521 if (!platform_test_xfs_fd(fd)) {
1523 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1527 resv.l_len = maxfilelen;
1528 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1534 strncat(goodfile, fname, 256);
1535 strcat (goodfile, ".fsxgood");
1536 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1537 if (fsxgoodfd < 0) {
1541 strncat(logfile, fname, 256);
1542 strcat (logfile, ".fsxlog");
1543 fsxlogf = fopen(logfile, "w");
1544 if (fsxlogf == NULL) {
1556 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1557 if (file_size == (off_t)-1) {
1559 warn("main: lseek eof");
1562 ret = lseek(fd, (off_t)0, SEEK_SET);
1563 if (ret == (off_t)-1) {
1565 warn("main: lseek 0");
1569 original_buf = (char *) malloc(maxfilelen);
1570 for (i = 0; i < maxfilelen; i++)
1571 original_buf[i] = random() % 256;
1572 good_buf = (char *) malloc(maxfilelen + writebdy);
1573 good_buf = round_ptr_up(good_buf, writebdy, 0);
1574 memset(good_buf, '\0', maxfilelen);
1575 temp_buf = (char *) malloc(maxoplen + readbdy);
1576 temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1577 memset(temp_buf, '\0', maxoplen);
1578 if (lite) { /* zero entire existing file */
1581 written = write(fd, good_buf, (size_t)maxfilelen);
1582 if (written != maxfilelen) {
1583 if (written == -1) {
1585 warn("main: error on write");
1587 warn("main: short write, 0x%x bytes instead "
1599 while (numops == -1 || numops--)
1606 prt("All operations completed A-OK!\n");