blob: a1d97b2d941ed090019fe1a7d364ef7c0f6dcf2d [file] [log] [blame]
Jens Axboe41d442b2019-12-14 22:44:40 -07001/*
William Dauchy7e3188b2020-01-04 16:23:14 +01002 * Description: run various statx(2) tests
Jens Axboe41d442b2019-12-14 22:44:40 -07003 *
4 */
5#include <errno.h>
6#include <stdio.h>
7#include <unistd.h>
8#include <stdlib.h>
9#include <string.h>
10#include <fcntl.h>
11#include <sys/types.h>
Jens Axboe41d442b2019-12-14 22:44:40 -070012#include <sys/syscall.h>
13#include <linux/stat.h>
14
15#include "liburing.h"
16
17#if defined(__x86_64)
18static int do_statx(int dfd, const char *path, int flags, unsigned mask,
19 struct statx *statxbuf)
20{
21 return syscall(332, dfd, path, flags, mask, statxbuf);
22}
23#endif
24
25static int create_file(const char *file, size_t size)
26{
27 ssize_t ret;
28 char *buf;
29 int fd;
30
31 buf = malloc(size);
32 memset(buf, 0xaa, size);
33
34 fd = open(file, O_WRONLY | O_CREAT, 0644);
35 if (fd < 0) {
36 perror("open file");
37 return 1;
38 }
39 ret = write(fd, buf, size);
40 close(fd);
41 return ret != size;
42}
43
44static int test_statx(struct io_uring *ring, const char *path)
45{
46 struct io_uring_cqe *cqe;
47 struct io_uring_sqe *sqe;
48 struct statx x1;
49#if defined(__x86_64)
50 struct statx x2;
51#endif
52 int ret;
53
54 sqe = io_uring_get_sqe(ring);
55 if (!sqe) {
56 fprintf(stderr, "get sqe failed\n");
57 goto err;
58 }
59 io_uring_prep_statx(sqe, -1, path, 0, STATX_ALL, &x1);
60
61 ret = io_uring_submit(ring);
62 if (ret <= 0) {
63 fprintf(stderr, "sqe submit failed: %d\n", ret);
64 goto err;
65 }
66
67 ret = io_uring_wait_cqe(ring, &cqe);
68 if (ret < 0) {
69 fprintf(stderr, "wait completion %d\n", ret);
70 goto err;
71 }
72 ret = cqe->res;
73 io_uring_cqe_seen(ring, cqe);
74 if (ret)
75 return ret;
76#if defined(__x86_64)
77 ret = do_statx(-1, path, 0, STATX_ALL, &x2);
78 if (ret < 0)
79 return -1;
80 if (memcmp(&x1, &x2, sizeof(x1))) {
81 fprintf(stderr, "Miscompare between io_uring and statx\n");
82 goto err;
83 }
84#endif
85 return 0;
86err:
87 return -1;
88}
89
90int main(int argc, char *argv[])
91{
92 struct io_uring ring;
93 const char *fname;
94 int ret;
95
96 ret = io_uring_queue_init(8, &ring, 0);
97 if (ret) {
98 fprintf(stderr, "ring setup failed\n");
99 return 1;
100 }
101
102 if (argc > 1) {
103 fname = argv[1];
104 } else {
105 fname = "/tmp/.statx";
106 if (create_file(fname, 4096)) {
107 fprintf(stderr, "file create failed\n");
108 return 1;
109 }
110 }
111
112 ret = test_statx(&ring, fname);
113 if (ret) {
114 if (ret == -EINVAL) {
115 fprintf(stdout, "statx not supported, skipping\n");
116 goto done;
117 }
118 fprintf(stderr, "test_statx failed: %d\n", ret);
119 goto err;
120 }
121done:
122 if (fname != argv[1])
123 unlink(fname);
124 return 0;
125err:
126 if (fname != argv[1])
127 unlink(fname);
128 return 1;
129}