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.
36 #include <linux/falloc.h>
43 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
46 * A log entry is an operation and a bunch of arguments.
56 struct log_entry oplog[LOGSIZE]; /* the log */
57 int logptr = 0; /* current position in log */
58 int logcount = 0; /* total ops */
67 #define OP_CLOSEOPEN 4
71 #define OP_FALLOCATE 8
74 #define PAGE_SIZE getpagesize()
76 #define PAGE_MASK (PAGE_SIZE - 1)
78 char *original_buf; /* a pointer to the original data */
79 char *good_buf; /* a pointer to the correct data */
80 char *temp_buf; /* a pointer to the current data */
81 char *fname; /* name of our test file */
82 int fd; /* fd for our test file */
87 unsigned long testcalls = 0; /* calls to function "test" */
89 unsigned long simulatedopcount = 0; /* -b flag */
90 int closeprob = 0; /* -c flag */
91 int debug = 0; /* -d flag */
92 unsigned long debugstart = 0; /* -D flag */
93 int flush = 0; /* -f flag */
94 int do_fsync = 0; /* -y flag */
95 unsigned long maxfilelen = 256 * 1024; /* -l flag */
96 int sizechecks = 1; /* -n flag disables them */
97 int maxoplen = 64 * 1024; /* -o flag */
98 int quiet = 0; /* -q flag */
99 unsigned long progressinterval = 0; /* -p flag */
100 int readbdy = 1; /* -r flag */
101 int style = 0; /* -s flag */
102 int prealloc = 0; /* -x flag */
103 int truncbdy = 1; /* -t flag */
104 int writebdy = 1; /* -w flag */
105 long monitorstart = -1; /* -m flag */
106 long monitorend = -1; /* -m flag */
107 int lite = 0; /* -L flag */
108 long numops = -1; /* -N flag */
109 int randomoplen = 1; /* -O flag disables it */
110 int seed = 1; /* -S flag */
111 int mapped_writes = 1; /* -W flag disables */
112 int fallocate_calls = 1; /* -F flag disables */
113 int mapped_reads = 1; /* -R flag disables it */
115 int o_direct; /* -Z */
122 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
125 #define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
126 #define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
128 #define fsxread(a,b,c,d) read(a,b,c)
129 #define fsxwrite(a,b,c,d) write(a,b,c)
132 FILE * fsxlogf = NULL;
136 static void *round_up(void *ptr, unsigned long align, unsigned long offset)
138 unsigned long ret = (unsigned long)ptr;
140 ret = ((ret + align - 1) & ~(align - 1));
146 vwarnc(int code, const char *fmt, va_list ap) {
147 fprintf(stderr, "fsx: ");
149 vfprintf(stderr, fmt, ap);
150 fprintf(stderr, ": ");
152 fprintf(stderr, "%s\n", strerror(code));
156 warn(const char * fmt, ...) {
159 vwarnc(errno, fmt, ap);
163 #define BUF_SIZE 1024
169 char buffer[BUF_SIZE];
172 vsnprintf(buffer, BUF_SIZE, fmt, args);
174 fprintf(stdout, buffer);
176 fprintf(fsxlogf, buffer);
182 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
187 log4(int operation, int arg0, int arg1, int arg2)
189 struct log_entry *le;
192 le->operation = operation;
194 le->operation = ~ le->operation;
200 if (logptr >= LOGSIZE)
209 struct log_entry *lp;
210 char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
212 prt("LOG DUMP (%d total operations):\n", logcount);
213 if (logcount < LOGSIZE) {
220 for ( ; count > 0; count--) {
223 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
224 prt("%d(%d mod 256): ", opnum, opnum%256);
226 if ((closeopen = lp->operation < 0))
227 lp->operation = ~ lp->operation;
229 switch (lp->operation) {
231 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
232 lp->args[0], lp->args[0] + lp->args[1] - 1,
234 if (badoff >= lp->args[0] && badoff <
235 lp->args[0] + lp->args[1])
239 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
240 lp->args[0], lp->args[0] + lp->args[1] - 1,
242 if (badoff >= lp->args[0] && badoff <
243 lp->args[0] + lp->args[1])
247 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
248 lp->args[0], lp->args[0] + lp->args[1] - 1,
250 if (badoff >= lp->args[0] &&
251 badoff < lp->args[0] + lp->args[1])
255 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
256 lp->args[0], lp->args[0] + lp->args[1] - 1,
258 if (lp->args[0] > lp->args[2])
260 else if (lp->args[0] + lp->args[1] > lp->args[2])
262 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
263 badoff < lp->args[0] + lp->args[1])
267 down = lp->args[0] < lp->args[1];
268 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
269 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
270 if (badoff >= lp->args[!down] &&
271 badoff < lp->args[!!down])
275 /* 0: offset 1: length 2: where alloced */
276 prt("FALLOCATE %s\tfrom 0x%x to 0x%x",
277 falloc_type[lp->args[2]], lp->args[0], lp->args[0] + lp->args[1]);
278 if (badoff >= lp->args[0] &&
279 badoff < lp->args[0] + lp->args[1])
283 prt("SKIPPED (no operation)");
286 prt("BOGUS LOG ENTRY (operation code = %d)!",
290 prt("\n\t\tCLOSE/OPEN");
300 save_buffer(char *buffer, off_t bufferlength, int fd)
303 ssize_t byteswritten;
305 if (fd <= 0 || bufferlength == 0)
308 if (bufferlength > SSIZE_MAX) {
309 prt("fsx flaw: overflow in save_buffer\n");
313 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
314 if (size_by_seek == (off_t)-1)
315 prterr("save_buffer: lseek eof");
316 else if (bufferlength > size_by_seek) {
317 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
318 (unsigned long long)bufferlength);
319 bufferlength = size_by_seek;
323 ret = lseek(fd, (off_t)0, SEEK_SET);
324 if (ret == (off_t)-1)
325 prterr("save_buffer: lseek 0");
327 byteswritten = write(fd, buffer, (size_t)bufferlength);
328 if (byteswritten != bufferlength) {
329 if (byteswritten == -1)
330 prterr("save_buffer write");
332 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
333 (unsigned)byteswritten,
334 (unsigned long long)bufferlength);
340 report_failure(int status)
346 save_buffer(good_buf, file_size, fsxgoodfd);
347 prt("Correct content saved for comparison\n");
348 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
357 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
358 *(((unsigned char *)(cp)) + 1)))
361 check_buffers(unsigned offset, unsigned size)
369 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
370 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
371 offset, size, fname);
372 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
374 c = good_buf[offset];
378 bad = short_at(&temp_buf[i]);
379 prt("0x%5x\t0x%04x\t0x%04x", offset,
380 short_at(&good_buf[offset]), bad);
381 op = temp_buf[offset & 1 ? i+1 : i];
384 prt("operation# (mod 256) for "
385 "the bad data may be %u\n",
386 ((unsigned)op & 0xff));
388 prt("operation# (mod 256) for "
389 "the bad data unknown, check"
390 " HOLE and EXTEND ops\n");
410 if (fstat(fd, &statbuf)) {
411 prterr("check_size: fstat");
412 statbuf.st_size = -1;
414 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
415 if (file_size != statbuf.st_size || file_size != size_by_seek) {
416 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
417 (unsigned long long)file_size,
418 (unsigned long long)statbuf.st_size,
419 (unsigned long long)size_by_seek);
426 check_trunc_hack(void)
430 ftruncate(fd, (off_t)0);
431 ftruncate(fd, (off_t)100000);
433 if (statbuf.st_size != (off_t)100000) {
434 prt("no extend on truncate! not posix!\n");
441 doflush(unsigned offset, unsigned size)
447 if (o_direct == O_DIRECT)
450 pg_offset = offset & mmap_mask;
451 map_size = pg_offset + size;
453 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
454 MAP_FILE | MAP_SHARED, fd,
455 (off_t)(offset - pg_offset))) == (char *)-1) {
456 prterr("doflush: mmap");
459 if (msync(p, map_size, MS_INVALIDATE) != 0) {
460 prterr("doflush: msync");
463 if (munmap(p, map_size) != 0) {
464 prterr("doflush: munmap");
470 doread(unsigned offset, unsigned size)
475 offset -= offset % readbdy;
477 size -= size % readbdy;
479 if (!quiet && testcalls > simulatedopcount && !o_direct)
480 prt("skipping zero size read\n");
481 log4(OP_SKIPPED, OP_READ, offset, size);
484 if (size + offset > file_size) {
485 if (!quiet && testcalls > simulatedopcount)
486 prt("skipping seek/read past end of file\n");
487 log4(OP_SKIPPED, OP_READ, offset, size);
491 log4(OP_READ, offset, size, 0);
493 if (testcalls <= simulatedopcount)
497 ((progressinterval && testcalls % progressinterval == 0) ||
499 (monitorstart == -1 ||
500 (offset + size > monitorstart &&
501 (monitorend == -1 || offset <= monitorend))))))
502 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
503 offset, offset + size - 1, size);
504 ret = lseek(fd, (off_t)offset, SEEK_SET);
505 if (ret == (off_t)-1) {
506 prterr("doread: lseek");
509 iret = fsxread(fd, temp_buf, size, offset);
512 prterr("doread: read");
514 prt("short read: 0x%x bytes instead of 0x%x\n",
518 check_buffers(offset, size);
523 check_eofpage(char *s, unsigned offset, char *p, int size)
525 unsigned long last_page, should_be_zero;
527 if (offset + size <= (file_size & ~page_mask))
530 * we landed in the last page of the file
531 * test to make sure the VM system provided 0's
532 * beyond the true end of the file mapping
533 * (as required by mmap def in 1996 posix 1003.1)
535 last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
537 for (should_be_zero = last_page + (file_size & page_mask);
538 should_be_zero < last_page + page_size;
540 if (*(char *)should_be_zero) {
541 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
542 s, file_size - 1, should_be_zero & page_mask,
543 short_at(should_be_zero));
550 domapread(unsigned offset, unsigned size)
556 offset -= offset % readbdy;
558 if (!quiet && testcalls > simulatedopcount)
559 prt("skipping zero size read\n");
560 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
563 if (size + offset > file_size) {
564 if (!quiet && testcalls > simulatedopcount)
565 prt("skipping seek/read past end of file\n");
566 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
570 log4(OP_MAPREAD, offset, size, 0);
572 if (testcalls <= simulatedopcount)
576 ((progressinterval && testcalls % progressinterval == 0) ||
578 (monitorstart == -1 ||
579 (offset + size > monitorstart &&
580 (monitorend == -1 || offset <= monitorend))))))
581 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
582 offset, offset + size - 1, size);
584 pg_offset = offset & PAGE_MASK;
585 map_size = pg_offset + size;
587 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
588 (off_t)(offset - pg_offset))) == (char *)-1) {
589 prterr("domapread: mmap");
592 memcpy(temp_buf, p + pg_offset, size);
594 check_eofpage("Read", offset, p, size);
596 if (munmap(p, map_size) != 0) {
597 prterr("domapread: munmap");
601 check_buffers(offset, size);
606 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
609 good_buf[offset] = testcalls % 256;
611 good_buf[offset] += original_buf[offset];
618 dowrite(unsigned offset, unsigned size)
623 offset -= offset % writebdy;
625 size -= size % writebdy;
627 if (!quiet && testcalls > simulatedopcount && !o_direct)
628 prt("skipping zero size write\n");
629 log4(OP_SKIPPED, OP_WRITE, offset, size);
633 log4(OP_WRITE, offset, size, file_size);
635 gendata(original_buf, good_buf, offset, size);
636 if (file_size < offset + size) {
637 if (file_size < offset)
638 memset(good_buf + file_size, '\0', offset - file_size);
639 file_size = offset + size;
641 warn("Lite file size bug in fsx!");
646 if (testcalls <= simulatedopcount)
650 ((progressinterval && testcalls % progressinterval == 0) ||
652 (monitorstart == -1 ||
653 (offset + size > monitorstart &&
654 (monitorend == -1 || offset <= monitorend))))))
655 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
656 offset, offset + size - 1, size);
657 ret = lseek(fd, (off_t)offset, SEEK_SET);
658 if (ret == (off_t)-1) {
659 prterr("dowrite: lseek");
662 iret = fsxwrite(fd, good_buf + offset, size, offset);
665 prterr("dowrite: write");
667 prt("short write: 0x%x bytes instead of 0x%x\n",
673 prt("fsync() failed: %s\n", strerror(errno));
678 doflush(offset, size);
684 domapwrite(unsigned offset, unsigned size)
691 offset -= offset % writebdy;
693 if (!quiet && testcalls > simulatedopcount)
694 prt("skipping zero size write\n");
695 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
698 cur_filesize = file_size;
700 log4(OP_MAPWRITE, offset, size, 0);
702 gendata(original_buf, good_buf, offset, size);
703 if (file_size < offset + size) {
704 if (file_size < offset)
705 memset(good_buf + file_size, '\0', offset - file_size);
706 file_size = offset + size;
708 warn("Lite file size bug in fsx!");
713 if (testcalls <= simulatedopcount)
717 ((progressinterval && testcalls % progressinterval == 0) ||
719 (monitorstart == -1 ||
720 (offset + size > monitorstart &&
721 (monitorend == -1 || offset <= monitorend))))))
722 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
723 offset, offset + size - 1, size);
725 if (file_size > cur_filesize) {
726 if (ftruncate(fd, file_size) == -1) {
727 prterr("domapwrite: ftruncate");
731 pg_offset = offset & PAGE_MASK;
732 map_size = pg_offset + size;
734 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
735 MAP_FILE | MAP_SHARED, fd,
736 (off_t)(offset - pg_offset))) == (char *)-1) {
737 prterr("domapwrite: mmap");
740 memcpy(p + pg_offset, good_buf + offset, size);
741 if (msync(p, map_size, 0) != 0) {
742 prterr("domapwrite: msync");
746 check_eofpage("Write", offset, p, size);
748 if (munmap(p, map_size) != 0) {
749 prterr("domapwrite: munmap");
756 dotruncate(unsigned size)
758 int oldsize = file_size;
760 size -= size % truncbdy;
761 if (size > biggest) {
763 if (!quiet && testcalls > simulatedopcount)
764 prt("truncating to largest ever: 0x%x\n", size);
767 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
769 if (size > file_size)
770 memset(good_buf + file_size, '\0', size - file_size);
773 if (testcalls <= simulatedopcount)
776 if ((progressinterval && testcalls % progressinterval == 0) ||
777 (debug && (monitorstart == -1 || monitorend == -1 ||
778 size <= monitorend)))
779 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
780 if (ftruncate(fd, (off_t)size) == -1) {
781 prt("ftruncate1: %x\n", size);
782 prterr("dotruncate: ftruncate");
788 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
790 dofallocate(unsigned offset, unsigned length)
796 if (!quiet && testcalls > simulatedopcount)
797 prt("skipping zero length fallocate\n");
798 log4(OP_SKIPPED, OP_FALLOCATE, offset, length);
802 keep_size = random() % 2;
804 end_offset = keep_size ? 0 : offset + length;
806 if (end_offset > biggest) {
807 biggest = end_offset;
808 if (!quiet && testcalls > simulatedopcount)
809 prt("fallocating to largest ever: 0x%x\n", end_offset);
814 * 1: allocate past EOF
815 * 2: extending prealloc
816 * 3: interior prealloc
818 log4(OP_FALLOCATE, offset, length, (end_offset > file_size) ? (keep_size ? 1 : 2) : 3);
820 if (end_offset > file_size) {
821 memset(good_buf + file_size, '\0', end_offset - file_size);
822 file_size = end_offset;
825 if (testcalls <= simulatedopcount)
828 if ((progressinterval && testcalls % progressinterval == 0) ||
829 (debug && (monitorstart == -1 || monitorend == -1 ||
830 end_offset <= monitorend)))
831 prt("%lu falloc\tfrom 0x%x to 0x%x\n", testcalls, offset, length);
832 if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
833 prt("fallocate: %x to %x\n", offset, length);
834 prterr("dofallocate: fallocate");
840 dofallocate(unsigned offset, unsigned length)
851 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
852 prterr("writefileimage: lseek");
855 iret = write(fd, good_buf, file_size);
856 if ((off_t)iret != file_size) {
858 prterr("writefileimage: write");
860 prt("short write: 0x%x bytes instead of 0x%llx\n",
861 iret, (unsigned long long)file_size);
864 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
865 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
866 prterr("writefileimage: ftruncate");
875 if (testcalls <= simulatedopcount)
879 prt("%lu close/open\n", testcalls);
881 prterr("docloseopen: close");
884 fd = open(fname, O_RDWR|o_direct, 0);
886 prterr("docloseopen: open");
895 unsigned long offset;
896 unsigned long size = maxoplen;
897 unsigned long rv = random();
898 unsigned long op = rv % (3 + !lite + mapped_writes + fallocate_calls);
900 /* turn off the map read if necessary */
902 if (op == 2 && !mapped_reads)
905 if (simulatedopcount > 0 && testcalls == simulatedopcount)
911 closeopen = (rv >> 3) < (1 << 28) / closeprob;
913 if (debugstart > 0 && testcalls >= debugstart)
916 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
917 prt("%lu...\n", testcalls);
926 * FALLOCATE: op = - 5
928 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
929 dotruncate(random() % maxfilelen);
932 size = random() % (maxoplen+1);
934 if (lite ? 0 : op == 3) {
941 offset %= maxfilelen;
942 if (offset + size > maxfilelen)
943 size = maxfilelen - offset;
944 dofallocate(offset, size);
945 } else if (op == 1 || op == (lite ? 3 : 4)) {
946 /* write / mapwrite */
947 offset %= maxfilelen;
948 if (offset + size > maxfilelen)
949 size = maxfilelen - offset;
951 domapwrite(offset, size);
953 dowrite(offset, size);
960 if (offset + size > file_size)
961 size = file_size - offset;
963 domapread(offset, size);
965 doread(offset, size);
969 if (sizechecks && testcalls > simulatedopcount)
981 prt("signal %d\n", sig);
982 prt("testcalls = %lu\n", testcalls);
990 fprintf(stdout, "usage: %s",
991 "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\
992 -b opnum: beginning operation number (default 1)\n\
993 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
994 -d: debug output for all operations\n\
995 -f flush and invalidate cache after I/O\n\
996 -l flen: the upper bound on file size (default 262144)\n\
997 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
998 -n: no verifications of file size\n\
999 -o oplen: the upper bound on operation size (default 65536)\n\
1000 -p progressinterval: debug output at specified operation interval\n\
1001 -q: quieter operation\n\
1002 -r readbdy: 4096 would make reads page aligned (default 1)\n\
1003 -s style: 1 gives smaller truncates (default 0)\n\
1004 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1005 -w writebdy: 4096 would make writes page aligned (default 1)\n\
1006 -x: preallocate file space before starting, XFS only (default 0)\n\
1007 -y synchronize changes to a file\n"
1010 " -A: Use the AIO system calls\n"
1012 " -D startingop: debug output starting at specified operation\n"
1014 " -F: Do not use fallocate (preallocation) calls\n"
1016 " -L: fsxLite - no file creations & no file size changes\n\
1017 -N numops: total # operations to do (default infinity)\n\
1018 -O: use oplen (see -o flag) for every op (default random)\n\
1019 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1020 -S seed: for random # generator (default 1) 0 gets timestamp\n\
1021 -W: mapped write operations DISabled\n\
1022 -R: read() system calls only (mapped reads disabled)\n\
1023 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1024 fname: this filename is REQUIRED (no default)\n");
1030 getnum(char *s, char **e)
1035 ret = strtol(s, e, 0);
1065 io_context_t io_ctx;
1071 ret = io_queue_init(QSZ, &io_ctx);
1073 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1081 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1083 struct io_event event;
1084 static struct timespec ts;
1085 struct iocb *iocbs[] = { &iocb };
1090 io_prep_pread(&iocb, fd, buf, len, offset);
1092 io_prep_pwrite(&iocb, fd, buf, len, offset);
1097 ret = io_submit(io_ctx, 1, iocbs);
1099 fprintf(stderr, "errcode=%d\n", ret);
1100 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1105 ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1108 fprintf(stderr, "aio_rw: no events available\n");
1110 fprintf(stderr, "errcode=%d\n", -ret);
1111 fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1116 if (len != event.res) {
1118 * The b0rked libaio defines event.res as unsigned.
1119 * However the kernel strucuture has it signed,
1120 * and it's used to pass negated error value.
1121 * Till the library is fixed use the temp var.
1123 res = (long)event.res;
1125 fprintf(stderr, "bad io length: %lu instead of %u\n",
1128 fprintf(stderr, "errcode=%ld\n", -res);
1129 fprintf(stderr, "aio_rw: async io failed: %s\n",
1140 * The caller expects error return in traditional libc
1141 * convention, i.e. -1 and the errno set to error.
1147 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1152 ret = __aio_rw(rw, fd, buf, len, offset);
1155 ret = read(fd, buf, len);
1157 ret = write(fd, buf, len);
1165 main(int argc, char **argv)
1169 char goodfile[1024];
1175 page_size = getpagesize();
1176 page_mask = page_size - 1;
1177 mmap_mask = page_mask;
1180 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1182 while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FLN:OP:RS:WZ"))
1186 simulatedopcount = getnum(optarg, &endp);
1188 fprintf(stdout, "Will begin at operation %ld\n",
1190 if (simulatedopcount == 0)
1192 simulatedopcount -= 1;
1195 closeprob = getnum(optarg, &endp);
1198 "Chance of close/open is 1 in %d\n",
1210 maxfilelen = getnum(optarg, &endp);
1211 if (maxfilelen <= 0)
1215 monitorstart = getnum(optarg, &endp);
1216 if (monitorstart < 0)
1218 if (!endp || *endp++ != ':')
1220 monitorend = getnum(endp, &endp);
1223 if (monitorend == 0)
1224 monitorend = -1; /* aka infinity */
1230 maxoplen = getnum(optarg, &endp);
1235 progressinterval = getnum(optarg, &endp);
1236 if (progressinterval == 0)
1243 readbdy = getnum(optarg, &endp);
1248 style = getnum(optarg, &endp);
1249 if (style < 0 || style > 1)
1253 truncbdy = getnum(optarg, &endp);
1258 writebdy = getnum(optarg, &endp);
1272 debugstart = getnum(optarg, &endp);
1277 fallocate_calls = 0;
1283 numops = getnum(optarg, &endp);
1291 strncpy(goodfile, optarg, sizeof(goodfile));
1292 strcat(goodfile, "/");
1293 strncpy(logfile, optarg, sizeof(logfile));
1294 strcat(logfile, "/");
1300 seed = getnum(optarg, &endp);
1302 seed = time(0) % 10000;
1304 fprintf(stdout, "Seed set to %d\n", seed);
1311 fprintf(stdout, "mapped writes DISABLED\n");
1314 o_direct = O_DIRECT;
1326 signal(SIGHUP, cleanup);
1327 signal(SIGINT, cleanup);
1328 signal(SIGPIPE, cleanup);
1329 signal(SIGALRM, cleanup);
1330 signal(SIGTERM, cleanup);
1331 signal(SIGXCPU, cleanup);
1332 signal(SIGXFSZ, cleanup);
1333 signal(SIGVTALRM, cleanup);
1334 signal(SIGUSR1, cleanup);
1335 signal(SIGUSR2, cleanup);
1337 initstate(seed, state, 256);
1340 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1347 xfs_flock64_t resv = { 0 };
1348 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1349 if (!platform_test_xfs_fd(fd)) {
1351 fprintf(stderr, "main: cannot prealloc, non XFS\n");
1355 resv.l_len = maxfilelen;
1356 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1362 strncat(goodfile, fname, 256);
1363 strcat (goodfile, ".fsxgood");
1364 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1365 if (fsxgoodfd < 0) {
1369 strncat(logfile, fname, 256);
1370 strcat (logfile, ".fsxlog");
1371 fsxlogf = fopen(logfile, "w");
1372 if (fsxlogf == NULL) {
1384 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1385 if (file_size == (off_t)-1) {
1387 warn("main: lseek eof");
1390 ret = lseek(fd, (off_t)0, SEEK_SET);
1391 if (ret == (off_t)-1) {
1393 warn("main: lseek 0");
1397 original_buf = (char *) malloc(maxfilelen);
1398 for (i = 0; i < maxfilelen; i++)
1399 original_buf[i] = random() % 256;
1400 good_buf = (char *) malloc(maxfilelen + writebdy);
1401 good_buf = round_up(good_buf, writebdy, 0);
1402 memset(good_buf, '\0', maxfilelen);
1403 temp_buf = (char *) malloc(maxoplen + readbdy);
1404 temp_buf = round_up(temp_buf, readbdy, 0);
1405 memset(temp_buf, '\0', maxoplen);
1406 if (lite) { /* zero entire existing file */
1409 written = write(fd, good_buf, (size_t)maxfilelen);
1410 if (written != maxfilelen) {
1411 if (written == -1) {
1413 warn("main: error on write");
1415 warn("main: short write, 0x%x bytes instead "
1425 if (!lite && fallocate_calls) {
1426 if (fallocate(fd, 0, 0, 1) && errno == EOPNOTSUPP) {
1428 prt("fsx: main: filesystem does not support fallocate, disabling\n");
1429 fallocate_calls = 0;
1433 #else /* ! FALLOCATE */
1434 fallocate_calls = 0;
1437 while (numops == -1 || numops--)
1444 prt("All operations completed A-OK!\n");