blob: c0f9e9cbc98f9d128d2892a832d21553ec907940 [file] [log] [blame]
Jens Axboee5024352020-02-11 20:34:12 -07001/* SPDX-License-Identifier: MIT */
Jens Axboe41d442b2019-12-14 22:44:40 -07002/*
William Dauchy7e3188b2020-01-04 16:23:14 +01003 * Description: run various statx(2) tests
Jens Axboe41d442b2019-12-14 22:44:40 -07004 *
5 */
6#include <errno.h>
7#include <stdio.h>
8#include <unistd.h>
9#include <stdlib.h>
10#include <string.h>
11#include <fcntl.h>
12#include <sys/types.h>
Jens Axboe41d442b2019-12-14 22:44:40 -070013#include <sys/syscall.h>
14#include <linux/stat.h>
15
Zhiqiang Liu97499812021-02-21 22:58:12 +080016#include "helpers.h"
Jens Axboe41d442b2019-12-14 22:44:40 -070017#include "liburing.h"
18
Jens Axboe0abec422020-07-10 07:54:49 -060019#ifdef __NR_statx
Jens Axboe41d442b2019-12-14 22:44:40 -070020static int do_statx(int dfd, const char *path, int flags, unsigned mask,
21 struct statx *statxbuf)
22{
Tobias Klausercb7c6262020-07-09 23:34:52 +020023 return syscall(__NR_statx, dfd, path, flags, mask, statxbuf);
Jens Axboe41d442b2019-12-14 22:44:40 -070024}
Jens Axboe0abec422020-07-10 07:54:49 -060025#else
26static int do_statx(int dfd, const char *path, int flags, unsigned mask,
27 struct statx *statxbuf)
28{
29 errno = ENOSYS;
30 return -1;
31}
32#endif
Jens Axboe41d442b2019-12-14 22:44:40 -070033
Tobias Klausercb7c6262020-07-09 23:34:52 +020034static int statx_syscall_supported(void)
35{
Tobias Klauser126308e2020-07-10 16:27:03 +020036 return errno == ENOSYS ? 0 : -1;
Tobias Klausercb7c6262020-07-09 23:34:52 +020037}
38
Jens Axboe41d442b2019-12-14 22:44:40 -070039static int test_statx(struct io_uring *ring, const char *path)
40{
41 struct io_uring_cqe *cqe;
42 struct io_uring_sqe *sqe;
Tobias Klausercb7c6262020-07-09 23:34:52 +020043 struct statx x1, x2;
Jens Axboe41d442b2019-12-14 22:44:40 -070044 int ret;
45
46 sqe = io_uring_get_sqe(ring);
47 if (!sqe) {
48 fprintf(stderr, "get sqe failed\n");
49 goto err;
50 }
51 io_uring_prep_statx(sqe, -1, path, 0, STATX_ALL, &x1);
52
53 ret = io_uring_submit(ring);
54 if (ret <= 0) {
55 fprintf(stderr, "sqe submit failed: %d\n", ret);
56 goto err;
57 }
58
59 ret = io_uring_wait_cqe(ring, &cqe);
60 if (ret < 0) {
61 fprintf(stderr, "wait completion %d\n", ret);
62 goto err;
63 }
64 ret = cqe->res;
65 io_uring_cqe_seen(ring, cqe);
66 if (ret)
67 return ret;
Jens Axboe41d442b2019-12-14 22:44:40 -070068 ret = do_statx(-1, path, 0, STATX_ALL, &x2);
69 if (ret < 0)
Tobias Klausercb7c6262020-07-09 23:34:52 +020070 return statx_syscall_supported();
Jens Axboe41d442b2019-12-14 22:44:40 -070071 if (memcmp(&x1, &x2, sizeof(x1))) {
72 fprintf(stderr, "Miscompare between io_uring and statx\n");
73 goto err;
74 }
Jens Axboe41d442b2019-12-14 22:44:40 -070075 return 0;
76err:
77 return -1;
78}
79
Jens Axboe06b7fa52020-04-27 10:51:30 -060080static int test_statx_fd(struct io_uring *ring, const char *path)
81{
82 struct io_uring_cqe *cqe;
83 struct io_uring_sqe *sqe;
Tobias Klausercb7c6262020-07-09 23:34:52 +020084 struct statx x1, x2;
Jens Axboe06b7fa52020-04-27 10:51:30 -060085 int ret, fd;
86
87 fd = open(path, O_RDONLY);
88 if (fd < 0) {
89 perror("open");
90 return 1;
91 }
92
93 memset(&x1, 0, sizeof(x1));
94
95 sqe = io_uring_get_sqe(ring);
96 if (!sqe) {
97 fprintf(stderr, "get sqe failed\n");
98 goto err;
99 }
100 io_uring_prep_statx(sqe, fd, "", AT_EMPTY_PATH, STATX_ALL, &x1);
101
102 ret = io_uring_submit(ring);
103 if (ret <= 0) {
104 fprintf(stderr, "sqe submit failed: %d\n", ret);
105 goto err;
106 }
107
108 ret = io_uring_wait_cqe(ring, &cqe);
109 if (ret < 0) {
110 fprintf(stderr, "wait completion %d\n", ret);
111 goto err;
112 }
113 ret = cqe->res;
114 io_uring_cqe_seen(ring, cqe);
115 if (ret)
116 return ret;
Jens Axboe06b7fa52020-04-27 10:51:30 -0600117 memset(&x2, 0, sizeof(x2));
118 ret = do_statx(fd, "", AT_EMPTY_PATH, STATX_ALL, &x2);
119 if (ret < 0)
Tobias Klausercb7c6262020-07-09 23:34:52 +0200120 return statx_syscall_supported();
Jens Axboe06b7fa52020-04-27 10:51:30 -0600121 if (memcmp(&x1, &x2, sizeof(x1))) {
122 fprintf(stderr, "Miscompare between io_uring and statx\n");
123 goto err;
124 }
Jens Axboe06b7fa52020-04-27 10:51:30 -0600125 return 0;
126err:
127 return -1;
128}
129
Jens Axboe41d442b2019-12-14 22:44:40 -0700130int main(int argc, char *argv[])
131{
132 struct io_uring ring;
133 const char *fname;
134 int ret;
135
136 ret = io_uring_queue_init(8, &ring, 0);
137 if (ret) {
138 fprintf(stderr, "ring setup failed\n");
139 return 1;
140 }
141
142 if (argc > 1) {
143 fname = argv[1];
144 } else {
145 fname = "/tmp/.statx";
Jens Axboed84d9012021-02-24 07:12:47 -0700146 t_create_file(fname, 4096);
Jens Axboe41d442b2019-12-14 22:44:40 -0700147 }
148
149 ret = test_statx(&ring, fname);
150 if (ret) {
151 if (ret == -EINVAL) {
152 fprintf(stdout, "statx not supported, skipping\n");
153 goto done;
154 }
155 fprintf(stderr, "test_statx failed: %d\n", ret);
156 goto err;
157 }
Jens Axboe06b7fa52020-04-27 10:51:30 -0600158
159 ret = test_statx_fd(&ring, fname);
160 if (ret) {
161 fprintf(stderr, "test_statx_fd failed: %d\n", ret);
162 goto err;
163 }
Jens Axboe41d442b2019-12-14 22:44:40 -0700164done:
165 if (fname != argv[1])
166 unlink(fname);
167 return 0;
168err:
169 if (fname != argv[1])
170 unlink(fname);
171 return 1;
172}