1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: test io_uring fallocate
4 *
5 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/resource.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <signal.h>
16
17 #include "liburing.h"
18 #include "helpers.h"
19
20 static int no_fallocate;
21
test_fallocate_rlimit(struct io_uring * ring)22 static int test_fallocate_rlimit(struct io_uring *ring)
23 {
24 struct io_uring_cqe *cqe;
25 struct io_uring_sqe *sqe;
26 struct rlimit rlim;
27 char buf[32];
28 int fd, ret;
29
30 if (getrlimit(RLIMIT_FSIZE, &rlim) < 0) {
31 perror("getrlimit");
32 return 1;
33 }
34 rlim.rlim_cur = 64 * 1024;
35 rlim.rlim_max = 64 * 1024;
36 if (setrlimit(RLIMIT_FSIZE, &rlim) < 0) {
37 perror("setrlimit");
38 return 1;
39 }
40
41 sprintf(buf, "./XXXXXX");
42 fd = mkstemp(buf);
43 if (fd < 0) {
44 perror("open");
45 return 1;
46 }
47 unlink(buf);
48
49 sqe = io_uring_get_sqe(ring);
50 if (!sqe) {
51 fprintf(stderr, "get sqe failed\n");
52 goto err;
53 }
54 io_uring_prep_fallocate(sqe, fd, 0, 0, 128*1024);
55
56 ret = io_uring_submit(ring);
57 if (ret <= 0) {
58 fprintf(stderr, "sqe submit failed: %d\n", ret);
59 goto err;
60 }
61
62 ret = io_uring_wait_cqe(ring, &cqe);
63 if (ret < 0) {
64 fprintf(stderr, "wait completion %d\n", ret);
65 goto err;
66 }
67
68 if (cqe->res == -EINVAL) {
69 fprintf(stdout, "Fallocate not supported, skipping\n");
70 no_fallocate = 1;
71 goto skip;
72 } else if (cqe->res != -EFBIG) {
73 fprintf(stderr, "Expected -EFBIG: %d\n", cqe->res);
74 goto err;
75 }
76 io_uring_cqe_seen(ring, cqe);
77 return 0;
78 skip:
79 return T_EXIT_SKIP;
80 err:
81 return 1;
82 }
83
test_fallocate(struct io_uring * ring)84 static int test_fallocate(struct io_uring *ring)
85 {
86 struct io_uring_cqe *cqe;
87 struct io_uring_sqe *sqe;
88 struct stat st;
89 char buf[32];
90 int fd, ret;
91
92 sprintf(buf, "./XXXXXX");
93 fd = mkstemp(buf);
94 if (fd < 0) {
95 perror("open");
96 return 1;
97 }
98 unlink(buf);
99
100 sqe = io_uring_get_sqe(ring);
101 if (!sqe) {
102 fprintf(stderr, "get sqe failed\n");
103 goto err;
104 }
105 io_uring_prep_fallocate(sqe, fd, 0, 0, 128*1024);
106
107 ret = io_uring_submit(ring);
108 if (ret <= 0) {
109 fprintf(stderr, "sqe submit failed: %d\n", ret);
110 goto err;
111 }
112
113 ret = io_uring_wait_cqe(ring, &cqe);
114 if (ret < 0) {
115 fprintf(stderr, "wait completion %d\n", ret);
116 goto err;
117 }
118
119 if (cqe->res == -EINVAL) {
120 fprintf(stdout, "Fallocate not supported, skipping\n");
121 no_fallocate = 1;
122 goto skip;
123 }
124 if (cqe->res) {
125 fprintf(stderr, "cqe->res=%d\n", cqe->res);
126 goto err;
127 }
128 io_uring_cqe_seen(ring, cqe);
129
130 if (fstat(fd, &st) < 0) {
131 perror("stat");
132 goto err;
133 }
134
135 if (st.st_size != 128*1024) {
136 fprintf(stderr, "Size mismatch: %llu\n",
137 (unsigned long long) st.st_size);
138 goto err;
139 }
140
141 return 0;
142 skip:
143 return T_EXIT_SKIP;
144 err:
145 return 1;
146 }
147
test_fallocate_fsync(struct io_uring * ring)148 static int test_fallocate_fsync(struct io_uring *ring)
149 {
150 struct io_uring_cqe *cqe;
151 struct io_uring_sqe *sqe;
152 struct stat st;
153 char buf[32];
154 int fd, ret, i;
155
156 if (no_fallocate)
157 return 0;
158
159 sprintf(buf, "./XXXXXX");
160 fd = mkstemp(buf);
161 if (fd < 0) {
162 perror("open");
163 return 1;
164 }
165 unlink(buf);
166
167 sqe = io_uring_get_sqe(ring);
168 if (!sqe) {
169 fprintf(stderr, "get sqe failed\n");
170 goto err;
171 }
172 io_uring_prep_fallocate(sqe, fd, 0, 0, 128*1024);
173 sqe->flags |= IOSQE_IO_LINK;
174 sqe->user_data = 1;
175
176 sqe = io_uring_get_sqe(ring);
177 if (!sqe) {
178 fprintf(stderr, "get sqe failed\n");
179 goto err;
180 }
181 io_uring_prep_fsync(sqe, fd, 0);
182 sqe->user_data = 2;
183
184 ret = io_uring_submit(ring);
185 if (ret <= 0) {
186 fprintf(stderr, "sqe submit failed: %d\n", ret);
187 goto err;
188 }
189
190 for (i = 0; i < 2; i++) {
191 ret = io_uring_wait_cqe(ring, &cqe);
192 if (ret < 0) {
193 fprintf(stderr, "wait completion %d\n", ret);
194 goto err;
195 }
196 if (cqe->res) {
197 fprintf(stderr, "cqe->res=%d,data=%" PRIu64 "\n", cqe->res,
198 (uint64_t) cqe->user_data);
199 goto err;
200 }
201 io_uring_cqe_seen(ring, cqe);
202 }
203
204 if (fstat(fd, &st) < 0) {
205 perror("stat");
206 goto err;
207 }
208
209 if (st.st_size != 128*1024) {
210 fprintf(stderr, "Size mismatch: %llu\n",
211 (unsigned long long) st.st_size);
212 goto err;
213 }
214
215 return 0;
216 err:
217 return 1;
218 }
219
sig_xfsz(int sig)220 static void sig_xfsz(int sig)
221 {
222 }
223
main(int argc,char * argv[])224 int main(int argc, char *argv[])
225 {
226 struct sigaction act = { };
227 struct io_uring ring;
228 int ret;
229
230 if (argc > 1)
231 return T_EXIT_SKIP;
232
233 act.sa_handler = sig_xfsz;
234 sigaction(SIGXFSZ, &act, NULL);
235
236 ret = io_uring_queue_init(8, &ring, 0);
237 if (ret) {
238 fprintf(stderr, "ring setup failed\n");
239 return T_EXIT_FAIL;
240 }
241
242 ret = test_fallocate(&ring);
243 if (ret) {
244 if (ret != T_EXIT_SKIP) {
245 fprintf(stderr, "test_fallocate failed\n");
246 }
247 return ret;
248 }
249
250 ret = test_fallocate_fsync(&ring);
251 if (ret) {
252 fprintf(stderr, "test_fallocate_fsync failed\n");
253 return ret;
254 }
255
256 ret = test_fallocate_rlimit(&ring);
257 if (ret) {
258 if (ret != T_EXIT_SKIP) {
259 fprintf(stderr, "test_fallocate_rlimit failed\n");
260 }
261 return ret;
262 }
263
264 return T_EXIT_PASS;
265 }
266