xref: /freebsd/tests/sys/aio/lio_test.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*-
2  * Copyright (c) 2017 Spectra Logic Corp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #define _WANT_ALL_LIO_OPCODES
30 
31 #include <sys/param.h>
32 #include <sys/event.h>
33 #include <sys/uio.h>
34 
35 #include <aio.h>
36 #include <fcntl.h>
37 #include <semaphore.h>
38 #include <stdlib.h>
39 
40 #include <atf-c.h>
41 
42 #include "local.h"
43 #include "freebsd_test_suite/macros.h"
44 
45 static sem_t completions;
46 
47 
48 static void
49 handler(int sig __unused)
50 {
51 	ATF_REQUIRE_EQ(0, sem_post(&completions));
52 }
53 
54 static void
55 thr_handler(union sigval sv __unused)
56 {
57 	ATF_REQUIRE_EQ(0, sem_post(&completions));
58 }
59 
60 /*
61  * If lio_listio is unable to enqueue any requests at all, it should return
62  * EAGAIN.
63  */
64 ATF_TC_WITHOUT_HEAD(lio_listio_eagain_kevent);
65 ATF_TC_BODY(lio_listio_eagain_kevent, tc)
66 {
67 	int fd, i, j, kq, max_queue_per_proc, ios_per_call;
68 	size_t max_queue_per_proc_size;
69 	struct aiocb *aiocbs[2];
70 	struct aiocb **list[2];
71 	struct sigevent sev[2];
72 	char *buffer;
73 	const char *path="tempfile";
74 	void *udata[2];
75 
76 	ATF_REQUIRE_KERNEL_MODULE("aio");
77 	ATF_REQUIRE_UNSAFE_AIO();
78 
79 	max_queue_per_proc_size = sizeof(max_queue_per_proc);
80 	ATF_REQUIRE_EQ(sysctlbyname("vfs.aio.max_aio_queue_per_proc",
81 	    &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0), 0);
82 	ios_per_call = max_queue_per_proc;
83 
84 	fd = open(path, O_RDWR|O_CREAT, 0666);
85 	ATF_REQUIRE(fd >= 0);
86 
87 	kq = kqueue();
88 	ATF_REQUIRE(kq > 0);
89 
90 	buffer = calloc(1, 4096);
91 	ATF_REQUIRE(buffer != NULL);
92 
93 	/*
94 	 * Call lio_listio twice, each with the maximum number of operations.
95 	 * The first call should succeed and the second should fail.
96 	 */
97 	for (i = 0; i < 2; i++) {
98 		aiocbs[i] = calloc(ios_per_call, sizeof(struct aiocb));
99 		ATF_REQUIRE(aiocbs[i] != NULL);
100 		list[i] = calloc(ios_per_call, sizeof(struct aiocb*));
101 		ATF_REQUIRE(list[i] != NULL);
102 		udata[i] = (void*)((caddr_t)0xdead0000 + i);
103 		sev[i].sigev_notify = SIGEV_KEVENT;
104 		sev[i].sigev_notify_kqueue = kq;
105 		sev[i].sigev_value.sival_ptr = udata[i];
106 		for (j = 0; j < ios_per_call; j++) {
107 			aiocbs[i][j].aio_fildes = fd;
108 			aiocbs[i][j].aio_offset = (i * ios_per_call + j) * 4096;
109 			aiocbs[i][j].aio_buf = buffer;
110 			aiocbs[i][j].aio_nbytes = 4096;
111 			aiocbs[i][j].aio_lio_opcode = LIO_WRITE;
112 			list[i][j] = &aiocbs[i][j];
113 		}
114 	}
115 
116 	ATF_REQUIRE_EQ(0, lio_listio(LIO_NOWAIT, list[0], ios_per_call, &sev[0]));
117 	ATF_REQUIRE_EQ(-1, lio_listio(LIO_NOWAIT, list[1], ios_per_call, &sev[1]));
118 	/*
119 	 * The second lio_listio call should fail with EAGAIN.  Bad timing may
120 	 * mean that some requests did get enqueued, but the result should
121 	 * still be EAGAIN.
122 	 */
123 	ATF_REQUIRE_EQ(errno, EAGAIN);
124 }
125 
126 
127 /* With LIO_WAIT, an empty lio_listio should return immediately */
128 ATF_TC_WITHOUT_HEAD(lio_listio_empty_wait);
129 ATF_TC_BODY(lio_listio_empty_wait, tc)
130 {
131 	struct aiocb *list = NULL;
132 
133 	ATF_REQUIRE_EQ(0, lio_listio(LIO_WAIT, &list, 0, NULL));
134 }
135 
136 /* With LIO_NOWAIT, an empty lio_listio should return immediately */
137 ATF_TC_WITHOUT_HEAD(lio_listio_empty_nowait);
138 ATF_TC_BODY(lio_listio_empty_nowait, tc)
139 {
140 	struct aiocb *list = NULL;
141 
142 	ATF_REQUIRE_EQ(0, lio_listio(LIO_NOWAIT, &list, 0, NULL));
143 }
144 
145 /*
146  * With LIO_NOWAIT, an empty lio_listio should send completion notification
147  * immediately
148  */
149 ATF_TC_WITHOUT_HEAD(lio_listio_empty_nowait_kevent);
150 ATF_TC_BODY(lio_listio_empty_nowait_kevent, tc)
151 {
152 	struct aiocb *list = NULL;
153 	struct sigevent sev;
154 	struct kevent kq_returned;
155 	int kq, result;
156 	void *udata = (void*)0xdeadbeefdeadbeef;
157 
158 	atf_tc_expect_timeout("Bug 251515 - lio_listio(2) never sends"
159 			" kevent if nent==0");
160 	kq = kqueue();
161 	ATF_REQUIRE(kq > 0);
162 	sev.sigev_notify = SIGEV_KEVENT;
163 	sev.sigev_notify_kqueue = kq;
164 	sev.sigev_value.sival_ptr = udata;
165 	ATF_REQUIRE_EQ(0, lio_listio(LIO_NOWAIT, &list, 0, &sev));
166 	result = kevent(kq, NULL, 0, &kq_returned, 1, NULL);
167 	ATF_REQUIRE_MSG(result == 1, "Never got completion notification");
168 	ATF_REQUIRE_EQ((uintptr_t)list, kq_returned.ident);
169 	ATF_REQUIRE_EQ(EVFILT_LIO, kq_returned.filter);
170 	ATF_REQUIRE_EQ(udata, kq_returned.udata);
171 }
172 
173 /*
174  * With LIO_NOWAIT, an empty lio_listio should send completion notification
175  * immediately
176  */
177 ATF_TC_WITHOUT_HEAD(lio_listio_empty_nowait_signal);
178 ATF_TC_BODY(lio_listio_empty_nowait_signal, tc)
179 {
180 	struct aiocb *list = NULL;
181 	struct sigevent sev;
182 
183 	ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
184 	sev.sigev_notify = SIGEV_SIGNAL;
185 	sev.sigev_signo = SIGUSR1;
186 	ATF_REQUIRE(SIG_ERR != signal(SIGUSR1, handler));
187 	ATF_REQUIRE_EQ(0, lio_listio(LIO_NOWAIT, &list, 0, &sev));
188 	ATF_REQUIRE_EQ(0, sem_wait(&completions));
189 	ATF_REQUIRE_EQ(0, sem_destroy(&completions));
190 }
191 
192 /*
193  * With LIO_NOWAIT, an empty lio_listio should send completion notification
194  * immediately
195  */
196 ATF_TC_WITHOUT_HEAD(lio_listio_empty_nowait_thread);
197 ATF_TC_BODY(lio_listio_empty_nowait_thread, tc)
198 {
199 	struct aiocb *list = NULL;
200 	struct sigevent sev;
201 
202 	ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
203 	bzero(&sev, sizeof(sev));
204 	sev.sigev_notify = SIGEV_THREAD;
205 	sev.sigev_notify_function = thr_handler;
206 	sev.sigev_notify_attributes = NULL;
207 	ATF_REQUIRE_MSG(0 == lio_listio(LIO_NOWAIT, &list, 0, &sev),
208 	    "lio_listio: %s", strerror(errno));
209 	ATF_REQUIRE_EQ(0, sem_wait(&completions));
210 	ATF_REQUIRE_EQ(0, sem_destroy(&completions));
211 }
212 
213 /*
214  * A simple check that the allowed operations work.
215  */
216 ATF_TC_WITHOUT_HEAD(lio_listio_opcodes);
217 ATF_TC_BODY(lio_listio_opcodes, tc)
218 {
219 	struct aiocb write_cb, read_cb, writev_cb, readv_cb;
220 	struct aiocb *list[] = {&write_cb, &read_cb, &writev_cb, &readv_cb};
221 	struct iovec writev_iov[2];
222 	struct iovec readv_iov[2];
223 	char buffer[6];
224 	int fd;
225 
226 	fd = open("testfile", O_CREAT | O_RDWR, 0666);
227 	ATF_REQUIRE_MSG(fd >= 0, "open: %s", strerror(errno));
228 
229 	/* We start with numbers in a file and letters in memory... */
230 	ATF_CHECK_EQ(6, write(fd, "123456", 6));
231 	memcpy(buffer, "abcdef", 6);
232 
233 	/* a -> 1 */
234 	bzero(&write_cb, sizeof(write_cb));
235 	write_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
236 	write_cb.aio_fildes = fd;
237 	write_cb.aio_lio_opcode = LIO_WRITE;
238 	write_cb.aio_buf = &buffer[0];
239 	write_cb.aio_nbytes = 1;
240 	write_cb.aio_offset = 0;
241 
242 	/* b <- 2 */
243 	bzero(&read_cb, sizeof(read_cb));
244 	read_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
245 	read_cb.aio_fildes = fd;
246 	read_cb.aio_lio_opcode = LIO_READ;
247 	read_cb.aio_buf = &buffer[1];
248 	read_cb.aio_nbytes = 1;
249 	read_cb.aio_offset = 1;
250 
251 	/* d -> 3, c -> 4 */
252 	writev_iov[0].iov_base = &buffer[3];
253 	writev_iov[0].iov_len = 1;
254 	writev_iov[1].iov_base = &buffer[2];
255 	writev_iov[1].iov_len = 1;
256 	bzero(&writev_cb, sizeof(writev_cb));
257 	writev_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
258 	writev_cb.aio_fildes = fd;
259 	writev_cb.aio_lio_opcode = LIO_WRITEV;
260 	writev_cb.aio_iov = &writev_iov;
261 	writev_cb.aio_iovcnt = 2;
262 	writev_cb.aio_offset = 2;
263 
264 	/* f <- 5, e <- 6 */
265 	readv_iov[0].iov_base = &buffer[5];
266 	readv_iov[0].iov_len = 1;
267 	readv_iov[1].iov_base = &buffer[4];
268 	readv_iov[1].iov_len = 1;
269 	bzero(&readv_cb, sizeof(readv_cb));
270 	readv_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
271 	readv_cb.aio_fildes = fd;
272 	readv_cb.aio_lio_opcode = LIO_READV;
273 	readv_cb.aio_iov = &readv_iov;
274 	readv_cb.aio_iovcnt = 2;
275 	readv_cb.aio_offset = 4;
276 
277 	ATF_CHECK_EQ(0, lio_listio(LIO_WAIT, list, nitems(list), NULL));
278 	ATF_CHECK_EQ(0, aio_error(&write_cb));
279 	ATF_CHECK_EQ(1, aio_return(&write_cb));
280 	ATF_CHECK_EQ(0, aio_error(&read_cb));
281 	ATF_CHECK_EQ(1, aio_return(&read_cb));
282 	ATF_CHECK_EQ(0, aio_error(&writev_cb));
283 	ATF_CHECK_EQ(2, aio_return(&writev_cb));
284 	ATF_CHECK_EQ(0, aio_error(&readv_cb));
285 	ATF_CHECK_EQ(2, aio_return(&readv_cb));
286 
287 	ATF_CHECK_EQ(0, memcmp(buffer, "a2cd65", 6));
288 	ATF_CHECK_EQ(6, pread(fd, buffer, 6, 0));
289 	ATF_CHECK_EQ(0, memcmp(buffer, "a2dc56", 6));
290 
291 	close(fd);
292 }
293 
294 
295 /*
296  * Only select opcodes are allowed with lio_listio
297  */
298 ATF_TC_WITHOUT_HEAD(lio_listio_invalid_opcode);
299 ATF_TC_BODY(lio_listio_invalid_opcode, tc)
300 {
301 	struct aiocb sync_cb, mlock_cb;
302 	struct aiocb *list[] = {&sync_cb, &mlock_cb};
303 	int fd;
304 
305 	fd = open("testfile", O_CREAT | O_RDWR, 0666);
306 	ATF_REQUIRE_MSG(fd >= 0, "open: %s", strerror(errno));
307 
308 	bzero(&sync_cb, sizeof(sync_cb));
309 	sync_cb.aio_fildes = fd;
310 	sync_cb.aio_lio_opcode = LIO_SYNC;
311 
312 	bzero(&mlock_cb, sizeof(mlock_cb));
313 	mlock_cb.aio_lio_opcode = LIO_MLOCK;
314 
315 	ATF_CHECK_ERRNO(EIO, lio_listio(LIO_WAIT, list, nitems(list), NULL));
316 	ATF_CHECK_EQ(EINVAL, aio_error(&sync_cb));
317 	ATF_CHECK_ERRNO(EINVAL, aio_return(&sync_cb) < 0);
318 	ATF_CHECK_EQ(EINVAL, aio_error(&mlock_cb));
319 	ATF_CHECK_ERRNO(EINVAL, aio_return(&mlock_cb) < 0);
320 
321 	close(fd);
322 }
323 
324 
325 ATF_TP_ADD_TCS(tp)
326 {
327 
328 	ATF_TP_ADD_TC(tp, lio_listio_eagain_kevent);
329 	ATF_TP_ADD_TC(tp, lio_listio_empty_nowait);
330 	ATF_TP_ADD_TC(tp, lio_listio_empty_nowait_kevent);
331 	ATF_TP_ADD_TC(tp, lio_listio_empty_nowait_signal);
332 	ATF_TP_ADD_TC(tp, lio_listio_empty_nowait_thread);
333 	ATF_TP_ADD_TC(tp, lio_listio_empty_wait);
334 	ATF_TP_ADD_TC(tp, lio_listio_opcodes);
335 	ATF_TP_ADD_TC(tp, lio_listio_invalid_opcode);
336 
337 	return (atf_no_error());
338 }
339