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