#ifdef AIO
#include <libaio.h>
#endif
+#ifdef URING
+#include <liburing.h>
+#endif
#include <sys/syscall.h>
#ifndef MAP_FILE
int fsxgoodfd = 0;
int o_direct; /* -Z */
int aio = 0;
+int uring = 0;
int mark_nr = 0;
int page_size;
int page_mask;
int mmap_mask;
-#ifdef AIO
-int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
+int fsx_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
#define READ 0
#define WRITE 1
-#define fsxread(a,b,c,d) aio_rw(READ, a,b,c,d)
-#define fsxwrite(a,b,c,d) aio_rw(WRITE, a,b,c,d)
-#else
-#define fsxread(a,b,c,d) read(a,b,c)
-#define fsxwrite(a,b,c,d) write(a,b,c)
-#endif
+#define fsxread(a,b,c,d) fsx_rw(READ, a,b,c,d)
+#define fsxwrite(a,b,c,d) fsx_rw(WRITE, a,b,c,d)
const char *replayops = NULL;
const char *recordops = NULL;
{
unsigned long ret = (unsigned long)ptr;
- ret = ((ret + align - 1) & ~(align - 1));
+ ret = roundup_64(ret, align);
ret += offset;
return (void *)ret;
}
}
memset(good_buf + offset, '\0', length);
+
+ if (!keep_size && end_offset > file_size) {
+ /*
+ * If there's a gap between the old file size and the offset of
+ * the zero range operation, fill the gap with zeroes.
+ */
+ if (offset > file_size)
+ memset(good_buf + file_size, '\0', offset - file_size);
+
+ file_size = end_offset;
+ }
}
#else
prt("copy range: 0x%x to 0x%x at 0x%x\n", offset,
offset + length, dest);
prt("do_copy_range: asked %u, copied %u??\n",
- nr, olen);
+ olen, nr);
report_failure(161);
} else if (nr > 0)
olen -= nr;
return llabs((unsigned long long)off1 - off0) < size;
}
+static void generate_dest_range(bool bdy_align,
+ unsigned long max_range_end,
+ unsigned long *src_offset,
+ unsigned long *size,
+ unsigned long *dst_offset)
+{
+ int tries = 0;
+
+ TRIM_OFF_LEN(*src_offset, *size, file_size);
+ if (bdy_align) {
+ *src_offset = rounddown_64(*src_offset, readbdy);
+ if (o_direct)
+ *size = rounddown_64(*size, readbdy);
+ } else {
+ *src_offset = rounddown_64(*src_offset, block_size);
+ *size = rounddown_64(*size, block_size);
+ }
+
+ do {
+ if (tries++ >= 30) {
+ *size = 0;
+ break;
+ }
+ *dst_offset = random();
+ TRIM_OFF(*dst_offset, max_range_end);
+ if (bdy_align)
+ *dst_offset = rounddown_64(*dst_offset, writebdy);
+ else
+ *dst_offset = rounddown_64(*dst_offset, block_size);
+ } while (range_overlaps(*src_offset, *dst_offset, *size) ||
+ *dst_offset + *size > max_range_end);
+}
+
int
test(void)
{
keep_size = random() % 2;
break;
case OP_CLONE_RANGE:
- TRIM_OFF_LEN(offset, size, file_size);
- offset = offset & ~(block_size - 1);
- size = size & ~(block_size - 1);
- do {
- offset2 = random();
- TRIM_OFF(offset2, maxfilelen);
- offset2 = offset2 & ~(block_size - 1);
- } while (range_overlaps(offset, offset2, size) ||
- offset2 + size > maxfilelen);
+ generate_dest_range(false, maxfilelen, &offset, &size, &offset2);
break;
case OP_DEDUPE_RANGE:
- {
- int tries = 0;
-
- TRIM_OFF_LEN(offset, size, file_size);
- offset = offset & ~(block_size - 1);
- size = size & ~(block_size - 1);
- do {
- if (tries++ >= 30) {
- size = 0;
- break;
- }
- offset2 = random();
- TRIM_OFF(offset2, file_size);
- offset2 = offset2 & ~(block_size - 1);
- } while (range_overlaps(offset, offset2, size) ||
- offset2 + size > file_size);
- break;
- }
+ generate_dest_range(false, file_size, &offset, &size, &offset2);
+ break;
case OP_COPY_RANGE:
- TRIM_OFF_LEN(offset, size, file_size);
- offset -= offset % readbdy;
- if (o_direct)
- size -= size % readbdy;
- do {
- offset2 = random();
- TRIM_OFF(offset2, maxfilelen);
- offset2 -= offset2 % writebdy;
- } while (range_overlaps(offset, offset2, size) ||
- offset2 + size > maxfilelen);
+ generate_dest_range(true, maxfilelen, &offset, &size, &offset2);
break;
}
do_punch_hole(offset, size);
break;
case OP_ZERO_RANGE:
- TRIM_OFF_LEN(offset, size, file_size);
+ TRIM_OFF_LEN(offset, size, maxfilelen);
do_zero_range(offset, size, keep_size);
break;
case OP_COLLAPSE_RANGE:
TRIM_OFF_LEN(offset, size, file_size - 1);
- offset = offset & ~(block_size - 1);
- size = size & ~(block_size - 1);
+ offset = rounddown_64(offset, block_size);
+ size = rounddown_64(size, block_size);
if (size == 0) {
log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
goto out;
case OP_INSERT_RANGE:
TRIM_OFF(offset, file_size);
TRIM_LEN(file_size, size, maxfilelen);
- offset = offset & ~(block_size - 1);
- size = size & ~(block_size - 1);
+ offset = rounddown_64(offset, block_size);
+ size = rounddown_64(size, block_size);
if (size == 0) {
log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
goto out;
usage(void)
{
fprintf(stdout, "usage: %s",
- "fsx [-dknqxABEFJLOWZ] [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid] [-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\
+ "fsx [-dknqxBEFJLOWZ][-A|-U] [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid] [-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\
-b opnum: beginning operation number (default 1)\n\
-c P: 1 in P chance of file close+open at each op (default infinity)\n\
-d: debug output for all operations\n\
-y synchronize changes to a file\n"
#ifdef AIO
-" -A: Use the AIO system calls\n"
+" -A: Use the AIO system calls, -A excludes -U\n"
#endif
+#ifdef URING
+" -U: Use the IO_URING system calls, -U excludes -A\n"
+ #endif
" -D startingop: debug output starting at specified operation\n"
#ifdef HAVE_LINUX_FALLOC_H
" -F: Do not use fallocate (preallocation) calls\n"
io_context_t io_ctx;
struct iocb iocb;
-int aio_setup()
+int
+aio_setup()
{
int ret;
ret = io_queue_init(QSZ, &io_ctx);
}
int
-__aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
{
struct io_event event;
static struct timespec ts;
errno = -ret;
return -1;
}
+#else
+aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+{
+ fprintf(stderr, "io_rw: need AIO support!\n");
+ exit(111);
+}
+#endif
+
+#ifdef URING
+
+struct io_uring ring;
+#define URING_ENTRIES 1024
+
+int
+uring_setup()
+{
+ int ret;
+
+ ret = io_uring_queue_init(URING_ENTRIES, &ring, 0);
+ if (ret != 0) {
+ fprintf(stderr, "uring_setup: io_uring_queue_init failed: %s\n",
+ strerror(ret));
+ return -1;
+ }
+ return 0;
+}
+
+int
+uring_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+{
+ struct io_uring_sqe *sqe;
+ struct io_uring_cqe *cqe;
+ struct iovec iovec;
+ int ret;
+ int res = 0;
+ char *p = buf;
+ unsigned l = len;
+ unsigned o = offset;
+
+ /*
+ * Due to io_uring tries non-blocking IOs (especially read), that
+ * always cause 'normal' short reading. To avoid this short read
+ * fail, try to loop read/write (escpecilly read) data.
+ */
+ while (l > 0) {
+ sqe = io_uring_get_sqe(&ring);
+ if (!sqe) {
+ fprintf(stderr, "uring_rw: io_uring_get_sqe failed: %s\n",
+ strerror(errno));
+ return -1;
+ }
+
+ iovec.iov_base = p;
+ iovec.iov_len = l;
+ if (rw == READ) {
+ io_uring_prep_readv(sqe, fd, &iovec, 1, o);
+ } else {
+ io_uring_prep_writev(sqe, fd, &iovec, 1, o);
+ }
+
+ ret = io_uring_submit_and_wait(&ring, 1);
+ if (ret != 1) {
+ fprintf(stderr, "errcode=%d\n", -ret);
+ fprintf(stderr, "uring %s: io_uring_submit failed: %s\n",
+ rw == READ ? "read":"write", strerror(-ret));
+ goto uring_error;
+ }
+
+ ret = io_uring_wait_cqe(&ring, &cqe);
+ if (ret != 0) {
+ fprintf(stderr, "errcode=%d\n", -ret);
+ fprintf(stderr, "uring %s: io_uring_wait_cqe failed: %s\n",
+ rw == READ ? "read":"write", strerror(-ret));
+ goto uring_error;
+ }
+
+ ret = cqe->res;
+ io_uring_cqe_seen(&ring, cqe);
+
+ if (ret > 0) {
+ o += ret;
+ l -= ret;
+ p += ret;
+ res += ret;
+ } else if (ret < 0) {
+ fprintf(stderr, "errcode=%d\n", -ret);
+ fprintf(stderr, "uring %s: io_uring failed: %s\n",
+ rw == READ ? "read":"write", strerror(-ret));
+ goto uring_error;
+ } else {
+ fprintf(stderr, "uring %s bad io length: %d instead of %u\n",
+ rw == READ ? "read":"write", res, len);
+ break;
+ }
+ }
+ return res;
+
+ uring_error:
+ /*
+ * The caller expects error return in traditional libc
+ * convention, i.e. -1 and the errno set to error.
+ */
+ errno = -ret;
+ return -1;
+}
+#else
+int
+uring_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+{
+ fprintf(stderr, "io_rw: need IO_URING support!\n");
+ exit(111);
+}
+#endif
-int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
+int
+fsx_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
{
int ret;
if (aio) {
- ret = __aio_rw(rw, fd, buf, len, offset);
+ ret = aio_rw(rw, fd, buf, len, offset);
+ } else if (uring) {
+ ret = uring_rw(rw, fd, buf, len, offset);
} else {
if (rw == READ)
ret = read(fd, buf, len);
return ret;
}
-#endif
-
#define test_fallocate(mode) __test_fallocate(mode, #mode)
int
setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
while ((ch = getopt_long(argc, argv,
- "b:c:dfg:i:j:kl:m:no:p:qr:s:t:w:xyABD:EFJKHzCILN:OP:RS:WXZ",
+ "b:c:dfg:i:j:kl:m:no:p:qr:s:t:w:xyABD:EFJKHzCILN:OP:RS:UWXZ",
longopts, NULL)) != EOF)
switch (ch) {
case 'b':
do_fsync = 1;
break;
case 'A':
- aio = 1;
+ aio = 1;
+ break;
+ case 'U':
+ uring = 1;
break;
case 'D':
debugstart = getnum(optarg, &endp);
randomoplen = 0;
break;
case 'P':
- strncpy(dname, optarg, sizeof(dname));
- strcat(dname, "/");
+ snprintf(dname, sizeof(dname), "%s/", optarg);
dirpath = strlen(dname);
break;
case 'R':
break;
case 255: /* --record-ops */
if (optarg)
- strncpy(opsfile, optarg, sizeof(opsfile));
+ snprintf(opsfile, sizeof(opsfile), "%s", optarg);
recordops = opsfile;
break;
case 256: /* --replay-ops */
if (argc != 1)
usage();
+ if (aio && uring) {
+ fprintf(stderr, "-A and -U shouldn't be used together\n");
+ usage();
+ }
+
if (integrity && !dirpath) {
fprintf(stderr, "option -i <logdev> requires -P <dirpath>\n");
usage();
if (aio)
aio_setup();
#endif
+#ifdef URING
+ if (uring)
+ uring_setup();
+#endif
if (!(o_flags & O_TRUNC)) {
off_t ret;