summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2020-04-08 09:34:35 -0600
committerJens Axboe <axboe@kernel.dk>2020-04-08 09:37:42 -0600
commit29896217a80edb511f3d5eb8b766ee422aa6d270 (patch)
tree52a36b48c94634f36a6870a1aab36205fa534167 /test
parent344355ec6619de8f4e64584c9736530b5346e4f4 (diff)
downloadliburing-29896217a80edb511f3d5eb8b766ee422aa6d270.tar.gz
liburing-29896217a80edb511f3d5eb8b766ee422aa6d270.tar.bz2
Add LFS openat test
Heavily based on test case from Dmitry Kadashev <dkadashev@gmail.com> Link: https://lore.kernel.org/io-uring/cc076d44-8cd0-1f19-2e79-45d2f0c5ace3@kernel.dk/T/#m894080c2370f51dd31cba85276d19b2720bc92a1 Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'test')
-rw-r--r--test/Makefile5
-rw-r--r--test/lfs-openat.c91
2 files changed, 94 insertions, 2 deletions
diff --git a/test/Makefile b/test/Makefile
index ef169c5..d9b10de 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -20,7 +20,8 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register
connect 7ad0e4b2f83c-test submit-reuse fallocate open-close \
file-update statx accept-reuse poll-v-poll fadvise madvise \
short-read openat2 probe shared-wq personality eventfd \
- send_recv eventfd-ring across-fork sq-poll-kthread splice
+ send_recv eventfd-ring across-fork sq-poll-kthread splice \
+ lfs-openat
include ../Makefile.quiet
@@ -48,7 +49,7 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \
file-update.c statx.c accept-reuse.c poll-v-poll.c fadvise.c \
madvise.c short-read.c openat2.c probe.c shared-wq.c \
personality.c eventfd.c eventfd-ring.c across-fork.c sq-poll-kthread.c \
- splice.c
+ splice.c lfs-openat.c
test_objs := $(patsubst %.c,%.ol,$(test_srcs))
diff --git a/test/lfs-openat.c b/test/lfs-openat.c
new file mode 100644
index 0000000..d52e215
--- /dev/null
+++ b/test/lfs-openat.c
@@ -0,0 +1,91 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include "liburing.h"
+
+#define DIE(...) do {\
+ fprintf(stderr, __VA_ARGS__);\
+ abort();\
+ } while(0);
+
+static const int RSIZE = 2;
+static const int OPEN_FLAGS = O_RDWR | O_CREAT;
+static const mode_t OPEN_MODE = S_IRUSR | S_IWUSR;
+
+static int open_io_uring(struct io_uring *ring, int dfd, const char *fn)
+{
+ struct io_uring_sqe *sqe;
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ fprintf(stderr, "failed to get sqe\n");
+ return 1;
+ }
+ io_uring_prep_openat(sqe, dfd, fn, OPEN_FLAGS, OPEN_MODE);
+ int ret = io_uring_submit(ring);
+ if (ret < 0) {
+ fprintf(stderr, "failed to submit openat: %s\n", strerror(-ret));
+ return 1;
+ }
+
+ struct io_uring_cqe *cqe;
+ ret = io_uring_wait_cqe(ring, &cqe);
+ int fd = cqe->res;
+ io_uring_cqe_seen(ring, cqe);
+ if (ret < 0) {
+ fprintf(stderr, "wait_cqe failed: %s\n", strerror(-ret));
+ return 1;
+ } else if (fd < 0) {
+ fprintf(stderr, "io_uring openat failed: %s\n", strerror(-fd));
+ return 1;
+ }
+
+ close(fd);
+ return 0;
+}
+
+int prepare_file(int dfd, const char* fn)
+{
+ const char buf[] = "foo";
+ int fd = openat(dfd, fn, OPEN_FLAGS, OPEN_MODE);
+ if (fd < 0) {
+ fprintf(stderr, "prepare/open: %s\n", strerror(errno));
+ return -1;
+ }
+ int res = pwrite(fd, buf, sizeof(buf), 1ull << 32);
+ if (res < 0) {
+ fprintf(stderr, "prepare/pwrite: %s\n", strerror(errno));
+ }
+ close(fd);
+ return res < 0 ? res : 0;
+}
+
+int main(int argc, char *argv[])
+{
+ const char *fn = "io_uring_openat_test";
+ int dfd = open("/tmp", O_RDONLY | O_DIRECTORY);
+ if (dfd < 0) {
+ DIE("open /tmp: %s\n", strerror(errno));
+ }
+ struct io_uring ring;
+ int ret = io_uring_queue_init(RSIZE, &ring, 0);
+ if (ret < 0) {
+ DIE("failed to init io_uring: %s\n", strerror(-ret));
+ }
+
+ if (prepare_file(dfd, fn))
+ return 1;
+
+ ret = open_io_uring(&ring, dfd, fn);
+
+ io_uring_queue_exit(&ring);
+ close(dfd);
+ unlink(fn);
+ return ret;
+}