xref: /freebsd/tests/sys/aio/aio_test.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*-
2  * Copyright (c) 2004 Robert N. M. Watson
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 /*
30  * Regression test to do some very basic AIO exercising on several types of
31  * file descriptors.  Currently, the tests consist of initializing a fixed
32  * size buffer with pseudo-random data, writing it to one fd using AIO, then
33  * reading it from a second descriptor using AIO.  For some targets, the same
34  * fd is used for write and read (i.e., file, md device), but for others the
35  * operation is performed on a peer (pty, socket, fifo, etc).  For each file
36  * descriptor type, several completion methods are tested.  This test program
37  * does not attempt to exercise error cases or more subtle asynchronous
38  * behavior, just make sure that the basic operations work on some basic object
39  * types.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/mdioctl.h>
44 #include <sys/module.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/un.h>
49 
50 #include <aio.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <libutil.h>
55 #include <limits.h>
56 #include <semaphore.h>
57 #include <signal.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <termios.h>
63 #include <unistd.h>
64 
65 #include <atf-c.h>
66 
67 #include "freebsd_test_suite/macros.h"
68 #include "local.h"
69 
70 /*
71  * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
72  * it sizes ac_buffer in the aio_context structure.  It is also the default
73  * size for file I/O.  For other types, we use smaller blocks or we risk
74  * blocking (and we run in a single process/thread so that would be bad).
75  */
76 #define	GLOBAL_MAX	16384
77 
78 #define	BUFFER_MAX	GLOBAL_MAX
79 
80 /*
81  * A completion function will block until the aio has completed, then return
82  * the result of the aio.  errno will be set appropriately.
83  */
84 typedef ssize_t (*completion)(struct aiocb*);
85 
86 struct aio_context {
87 	int		 ac_read_fd, ac_write_fd;
88 	long		 ac_seed;
89 	char		 ac_buffer[GLOBAL_MAX];
90 	int		 ac_buflen;
91 	int		 ac_seconds;
92 };
93 
94 static sem_t		completions;
95 
96 
97 /*
98  * Fill a buffer given a seed that can be fed into srandom() to initialize
99  * the PRNG in a repeatable manner.
100  */
101 static void
102 aio_fill_buffer(char *buffer, int len, long seed)
103 {
104 	char ch;
105 	int i;
106 
107 	srandom(seed);
108 	for (i = 0; i < len; i++) {
109 		ch = random() & 0xff;
110 		buffer[i] = ch;
111 	}
112 }
113 
114 /*
115  * Test that a buffer matches a given seed.  See aio_fill_buffer().  Return
116  * (1) on a match, (0) on a mismatch.
117  */
118 static int
119 aio_test_buffer(char *buffer, int len, long seed)
120 {
121 	char ch;
122 	int i;
123 
124 	srandom(seed);
125 	for (i = 0; i < len; i++) {
126 		ch = random() & 0xff;
127 		if (buffer[i] != ch)
128 			return (0);
129 	}
130 	return (1);
131 }
132 
133 /*
134  * Initialize a testing context given the file descriptors provided by the
135  * test setup.
136  */
137 static void
138 aio_context_init(struct aio_context *ac, int read_fd,
139     int write_fd, int buflen)
140 {
141 
142 	ATF_REQUIRE_MSG(buflen <= BUFFER_MAX,
143 	    "aio_context_init: buffer too large (%d > %d)",
144 	    buflen, BUFFER_MAX);
145 	bzero(ac, sizeof(*ac));
146 	ac->ac_read_fd = read_fd;
147 	ac->ac_write_fd = write_fd;
148 	ac->ac_buflen = buflen;
149 	srandomdev();
150 	ac->ac_seed = random();
151 	aio_fill_buffer(ac->ac_buffer, buflen, ac->ac_seed);
152 	ATF_REQUIRE_MSG(aio_test_buffer(ac->ac_buffer, buflen,
153 	    ac->ac_seed) != 0, "aio_test_buffer: internal error");
154 }
155 
156 static ssize_t
157 poll(struct aiocb *aio)
158 {
159 	int error;
160 
161 	while ((error = aio_error(aio)) == EINPROGRESS)
162 		usleep(25000);
163 	if (error)
164 		return (error);
165 	else
166 		return (aio_return(aio));
167 }
168 
169 static void
170 sigusr1_handler(int sig __unused)
171 {
172 	ATF_REQUIRE_EQ(0, sem_post(&completions));
173 }
174 
175 static void
176 thr_handler(union sigval sv __unused)
177 {
178 	ATF_REQUIRE_EQ(0, sem_post(&completions));
179 }
180 
181 static ssize_t
182 poll_signaled(struct aiocb *aio)
183 {
184 	int error;
185 
186 	ATF_REQUIRE_EQ(0, sem_wait(&completions));
187 	error = aio_error(aio);
188 	switch (error) {
189 		case EINPROGRESS:
190 			errno = EINTR;
191 			return (-1);
192 		case 0:
193 			return (aio_return(aio));
194 		default:
195 			return (error);
196 	}
197 }
198 
199 /*
200  * Setup a signal handler for signal delivery tests
201  * This isn't thread safe, but it's ok since ATF runs each testcase in a
202  * separate process
203  */
204 static struct sigevent*
205 setup_signal(void)
206 {
207 	static struct sigevent sev;
208 
209 	ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
210 	sev.sigev_notify = SIGEV_SIGNAL;
211 	sev.sigev_signo = SIGUSR1;
212 	ATF_REQUIRE(SIG_ERR != signal(SIGUSR1, sigusr1_handler));
213 	return (&sev);
214 }
215 
216 /*
217  * Setup a thread for thread delivery tests
218  * This isn't thread safe, but it's ok since ATF runs each testcase in a
219  * separate process
220  */
221 static struct sigevent*
222 setup_thread(void)
223 {
224 	static struct sigevent sev;
225 
226 	ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
227 	sev.sigev_notify = SIGEV_THREAD;
228 	sev.sigev_notify_function = thr_handler;
229 	sev.sigev_notify_attributes = NULL;
230 	return (&sev);
231 }
232 
233 static ssize_t
234 suspend(struct aiocb *aio)
235 {
236 	const struct aiocb *const iocbs[] = {aio};
237 	int error;
238 
239 	error = aio_suspend(iocbs, 1, NULL);
240 	if (error == 0)
241 		return (aio_return(aio));
242 	else
243 		return (error);
244 }
245 
246 static ssize_t
247 waitcomplete(struct aiocb *aio)
248 {
249 	struct aiocb *aiop;
250 	ssize_t ret;
251 
252 	ret = aio_waitcomplete(&aiop, NULL);
253 	ATF_REQUIRE_EQ(aio, aiop);
254 	return (ret);
255 }
256 
257 /*
258  * Perform a simple write test of our initialized data buffer to the provided
259  * file descriptor.
260  */
261 static void
262 aio_write_test(struct aio_context *ac, completion comp, struct sigevent *sev)
263 {
264 	struct aiocb aio;
265 	ssize_t len;
266 
267 	bzero(&aio, sizeof(aio));
268 	aio.aio_buf = ac->ac_buffer;
269 	aio.aio_nbytes = ac->ac_buflen;
270 	aio.aio_fildes = ac->ac_write_fd;
271 	aio.aio_offset = 0;
272 	if (sev)
273 		aio.aio_sigevent = *sev;
274 
275 	if (aio_write(&aio) < 0)
276 		atf_tc_fail("aio_write failed: %s", strerror(errno));
277 
278 	len = comp(&aio);
279 	if (len < 0)
280 		atf_tc_fail("aio failed: %s", strerror(errno));
281 
282 	if (len != ac->ac_buflen)
283 		atf_tc_fail("aio short write (%jd)", (intmax_t)len);
284 }
285 
286 /*
287  * Perform a vectored I/O test of our initialized data buffer to the provided
288  * file descriptor.
289  *
290  * To vectorize the linear buffer, chop it up into two pieces of dissimilar
291  * size, and swap their offsets.
292  */
293 static void
294 aio_writev_test(struct aio_context *ac, completion comp, struct sigevent *sev)
295 {
296 	struct aiocb aio;
297 	struct iovec iov[2];
298 	size_t len0, len1;
299 	ssize_t len;
300 
301 	bzero(&aio, sizeof(aio));
302 
303 	aio.aio_fildes = ac->ac_write_fd;
304 	aio.aio_offset = 0;
305 	len0 = ac->ac_buflen * 3 / 4;
306 	len1 = ac->ac_buflen / 4;
307 	iov[0].iov_base = ac->ac_buffer + len1;
308 	iov[0].iov_len = len0;
309 	iov[1].iov_base = ac->ac_buffer;
310 	iov[1].iov_len = len1;
311 	aio.aio_iov = iov;
312 	aio.aio_iovcnt = 2;
313 	if (sev)
314 		aio.aio_sigevent = *sev;
315 
316 	if (aio_writev(&aio) < 0)
317 		atf_tc_fail("aio_writev failed: %s", strerror(errno));
318 
319 	len = comp(&aio);
320 	if (len < 0)
321 		atf_tc_fail("aio failed: %s", strerror(errno));
322 
323 	if (len != ac->ac_buflen)
324 		atf_tc_fail("aio short write (%jd)", (intmax_t)len);
325 }
326 
327 /*
328  * Perform a simple read test of our initialized data buffer from the
329  * provided file descriptor.
330  */
331 static void
332 aio_read_test(struct aio_context *ac, completion comp, struct sigevent *sev)
333 {
334 	struct aiocb aio;
335 	ssize_t len;
336 
337 	bzero(ac->ac_buffer, ac->ac_buflen);
338 	bzero(&aio, sizeof(aio));
339 	aio.aio_buf = ac->ac_buffer;
340 	aio.aio_nbytes = ac->ac_buflen;
341 	aio.aio_fildes = ac->ac_read_fd;
342 	aio.aio_offset = 0;
343 	if (sev)
344 		aio.aio_sigevent = *sev;
345 
346 	if (aio_read(&aio) < 0)
347 		atf_tc_fail("aio_read failed: %s", strerror(errno));
348 
349 	len = comp(&aio);
350 	if (len < 0)
351 		atf_tc_fail("aio failed: %s", strerror(errno));
352 
353 	ATF_REQUIRE_EQ_MSG(len, ac->ac_buflen,
354 	    "aio short read (%jd)", (intmax_t)len);
355 
356 	if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0)
357 		atf_tc_fail("buffer mismatched");
358 }
359 
360 static void
361 aio_readv_test(struct aio_context *ac, completion comp, struct sigevent *sev)
362 {
363 	struct aiocb aio;
364 	struct iovec iov[2];
365 	size_t len0, len1;
366 	ssize_t len;
367 
368 	bzero(ac->ac_buffer, ac->ac_buflen);
369 	bzero(&aio, sizeof(aio));
370 	aio.aio_fildes = ac->ac_read_fd;
371 	aio.aio_offset = 0;
372 	len0 = ac->ac_buflen * 3 / 4;
373 	len1 = ac->ac_buflen / 4;
374 	iov[0].iov_base = ac->ac_buffer + len1;
375 	iov[0].iov_len = len0;
376 	iov[1].iov_base = ac->ac_buffer;
377 	iov[1].iov_len = len1;
378 	aio.aio_iov = iov;
379 	aio.aio_iovcnt = 2;
380 	if (sev)
381 		aio.aio_sigevent = *sev;
382 
383 	if (aio_readv(&aio) < 0)
384 		atf_tc_fail("aio_read failed: %s", strerror(errno));
385 
386 	len = comp(&aio);
387 	if (len < 0)
388 		atf_tc_fail("aio failed: %s", strerror(errno));
389 
390 	ATF_REQUIRE_EQ_MSG(len, ac->ac_buflen,
391 	    "aio short read (%jd)", (intmax_t)len);
392 
393 	if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0)
394 		atf_tc_fail("buffer mismatched");
395 }
396 
397 /*
398  * Series of type-specific tests for AIO.  For now, we just make sure we can
399  * issue a write and then a read to each type.  We assume that once a write
400  * is issued, a read can follow.
401  */
402 
403 /*
404  * Test with a classic file.  Assumes we can create a moderate size temporary
405  * file.
406  */
407 #define	FILE_LEN	GLOBAL_MAX
408 #define	FILE_PATHNAME	"testfile"
409 
410 static void
411 aio_file_test(completion comp, struct sigevent *sev, bool vectored)
412 {
413 	struct aio_context ac;
414 	int fd;
415 
416 	ATF_REQUIRE_KERNEL_MODULE("aio");
417 	ATF_REQUIRE_UNSAFE_AIO();
418 
419 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
420 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
421 
422 	aio_context_init(&ac, fd, fd, FILE_LEN);
423 	if (vectored) {
424 		aio_writev_test(&ac, comp, sev);
425 		aio_readv_test(&ac, comp, sev);
426 	} else {
427 		aio_write_test(&ac, comp, sev);
428 		aio_read_test(&ac, comp, sev);
429 	}
430 	close(fd);
431 }
432 
433 ATF_TC_WITHOUT_HEAD(file_poll);
434 ATF_TC_BODY(file_poll, tc)
435 {
436 	aio_file_test(poll, NULL, false);
437 }
438 
439 ATF_TC_WITHOUT_HEAD(file_signal);
440 ATF_TC_BODY(file_signal, tc)
441 {
442 	aio_file_test(poll_signaled, setup_signal(), false);
443 }
444 
445 ATF_TC_WITHOUT_HEAD(file_suspend);
446 ATF_TC_BODY(file_suspend, tc)
447 {
448 	aio_file_test(suspend, NULL, false);
449 }
450 
451 ATF_TC_WITHOUT_HEAD(file_thread);
452 ATF_TC_BODY(file_thread, tc)
453 {
454 	aio_file_test(poll_signaled, setup_thread(), false);
455 }
456 
457 ATF_TC_WITHOUT_HEAD(file_waitcomplete);
458 ATF_TC_BODY(file_waitcomplete, tc)
459 {
460 	aio_file_test(waitcomplete, NULL, false);
461 }
462 
463 #define	FIFO_LEN	256
464 #define	FIFO_PATHNAME	"testfifo"
465 
466 static void
467 aio_fifo_test(completion comp, struct sigevent *sev)
468 {
469 	int error, read_fd = -1, write_fd = -1;
470 	struct aio_context ac;
471 
472 	ATF_REQUIRE_KERNEL_MODULE("aio");
473 	ATF_REQUIRE_UNSAFE_AIO();
474 
475 	ATF_REQUIRE_MSG(mkfifo(FIFO_PATHNAME, 0600) != -1,
476 	    "mkfifo failed: %s", strerror(errno));
477 
478 	read_fd = open(FIFO_PATHNAME, O_RDONLY | O_NONBLOCK);
479 	if (read_fd == -1) {
480 		error = errno;
481 		errno = error;
482 		atf_tc_fail("read_fd open failed: %s",
483 		    strerror(errno));
484 	}
485 
486 	write_fd = open(FIFO_PATHNAME, O_WRONLY);
487 	if (write_fd == -1) {
488 		error = errno;
489 		errno = error;
490 		atf_tc_fail("write_fd open failed: %s",
491 		    strerror(errno));
492 	}
493 
494 	aio_context_init(&ac, read_fd, write_fd, FIFO_LEN);
495 	aio_write_test(&ac, comp, sev);
496 	aio_read_test(&ac, comp, sev);
497 
498 	close(read_fd);
499 	close(write_fd);
500 }
501 
502 ATF_TC_WITHOUT_HEAD(fifo_poll);
503 ATF_TC_BODY(fifo_poll, tc)
504 {
505 	aio_fifo_test(poll, NULL);
506 }
507 
508 ATF_TC_WITHOUT_HEAD(fifo_signal);
509 ATF_TC_BODY(fifo_signal, tc)
510 {
511 	aio_fifo_test(poll_signaled, setup_signal());
512 }
513 
514 ATF_TC_WITHOUT_HEAD(fifo_suspend);
515 ATF_TC_BODY(fifo_suspend, tc)
516 {
517 	aio_fifo_test(suspend, NULL);
518 }
519 
520 ATF_TC_WITHOUT_HEAD(fifo_thread);
521 ATF_TC_BODY(fifo_thread, tc)
522 {
523 	aio_fifo_test(poll_signaled, setup_thread());
524 }
525 
526 ATF_TC_WITHOUT_HEAD(fifo_waitcomplete);
527 ATF_TC_BODY(fifo_waitcomplete, tc)
528 {
529 	aio_fifo_test(waitcomplete, NULL);
530 }
531 
532 #define	UNIX_SOCKETPAIR_LEN	256
533 static void
534 aio_unix_socketpair_test(completion comp, struct sigevent *sev, bool vectored)
535 {
536 	struct aio_context ac;
537 	struct rusage ru_before, ru_after;
538 	int sockets[2];
539 
540 	ATF_REQUIRE_KERNEL_MODULE("aio");
541 
542 	ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != -1,
543 	    "socketpair failed: %s", strerror(errno));
544 
545 	aio_context_init(&ac, sockets[0], sockets[1], UNIX_SOCKETPAIR_LEN);
546 	ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_before) != -1,
547 	    "getrusage failed: %s", strerror(errno));
548 	if (vectored) {
549 		aio_writev_test(&ac, comp, sev);
550 		aio_readv_test(&ac, comp, sev);
551 	} else {
552 		aio_write_test(&ac, comp, sev);
553 		aio_read_test(&ac, comp, sev);
554 	}
555 	ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_after) != -1,
556 	    "getrusage failed: %s", strerror(errno));
557 	ATF_REQUIRE(ru_after.ru_msgsnd == ru_before.ru_msgsnd + 1);
558 	ATF_REQUIRE(ru_after.ru_msgrcv == ru_before.ru_msgrcv + 1);
559 
560 	close(sockets[0]);
561 	close(sockets[1]);
562 }
563 
564 ATF_TC_WITHOUT_HEAD(socket_poll);
565 ATF_TC_BODY(socket_poll, tc)
566 {
567 	aio_unix_socketpair_test(poll, NULL, false);
568 }
569 
570 ATF_TC_WITHOUT_HEAD(socket_signal);
571 ATF_TC_BODY(socket_signal, tc)
572 {
573 	aio_unix_socketpair_test(poll_signaled, setup_signal(), false);
574 }
575 
576 ATF_TC_WITHOUT_HEAD(socket_suspend);
577 ATF_TC_BODY(socket_suspend, tc)
578 {
579 	aio_unix_socketpair_test(suspend, NULL, false);
580 }
581 
582 ATF_TC_WITHOUT_HEAD(socket_thread);
583 ATF_TC_BODY(socket_thread, tc)
584 {
585 	aio_unix_socketpair_test(poll_signaled, setup_thread(), false);
586 }
587 
588 ATF_TC_WITHOUT_HEAD(socket_waitcomplete);
589 ATF_TC_BODY(socket_waitcomplete, tc)
590 {
591 	aio_unix_socketpair_test(waitcomplete, NULL, false);
592 }
593 
594 struct aio_pty_arg {
595 	int	apa_read_fd;
596 	int	apa_write_fd;
597 };
598 
599 #define	PTY_LEN		256
600 static void
601 aio_pty_test(completion comp, struct sigevent *sev)
602 {
603 	struct aio_context ac;
604 	int read_fd, write_fd;
605 	struct termios ts;
606 	int error;
607 
608 	ATF_REQUIRE_KERNEL_MODULE("aio");
609 	ATF_REQUIRE_UNSAFE_AIO();
610 
611 	ATF_REQUIRE_MSG(openpty(&read_fd, &write_fd, NULL, NULL, NULL) == 0,
612 	    "openpty failed: %s", strerror(errno));
613 
614 
615 	if (tcgetattr(write_fd, &ts) < 0) {
616 		error = errno;
617 		errno = error;
618 		atf_tc_fail("tcgetattr failed: %s", strerror(errno));
619 	}
620 	cfmakeraw(&ts);
621 	if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
622 		error = errno;
623 		errno = error;
624 		atf_tc_fail("tcsetattr failed: %s", strerror(errno));
625 	}
626 	aio_context_init(&ac, read_fd, write_fd, PTY_LEN);
627 
628 	aio_write_test(&ac, comp, sev);
629 	aio_read_test(&ac, comp, sev);
630 
631 	close(read_fd);
632 	close(write_fd);
633 }
634 
635 ATF_TC_WITHOUT_HEAD(pty_poll);
636 ATF_TC_BODY(pty_poll, tc)
637 {
638 	aio_pty_test(poll, NULL);
639 }
640 
641 ATF_TC_WITHOUT_HEAD(pty_signal);
642 ATF_TC_BODY(pty_signal, tc)
643 {
644 	aio_pty_test(poll_signaled, setup_signal());
645 }
646 
647 ATF_TC_WITHOUT_HEAD(pty_suspend);
648 ATF_TC_BODY(pty_suspend, tc)
649 {
650 	aio_pty_test(suspend, NULL);
651 }
652 
653 ATF_TC_WITHOUT_HEAD(pty_thread);
654 ATF_TC_BODY(pty_thread, tc)
655 {
656 	aio_pty_test(poll_signaled, setup_thread());
657 }
658 
659 ATF_TC_WITHOUT_HEAD(pty_waitcomplete);
660 ATF_TC_BODY(pty_waitcomplete, tc)
661 {
662 	aio_pty_test(waitcomplete, NULL);
663 }
664 
665 #define	PIPE_LEN	256
666 static void
667 aio_pipe_test(completion comp, struct sigevent *sev)
668 {
669 	struct aio_context ac;
670 	int pipes[2];
671 
672 	ATF_REQUIRE_KERNEL_MODULE("aio");
673 	ATF_REQUIRE_UNSAFE_AIO();
674 
675 	ATF_REQUIRE_MSG(pipe(pipes) != -1,
676 	    "pipe failed: %s", strerror(errno));
677 
678 	aio_context_init(&ac, pipes[0], pipes[1], PIPE_LEN);
679 	aio_write_test(&ac, comp, sev);
680 	aio_read_test(&ac, comp, sev);
681 
682 	close(pipes[0]);
683 	close(pipes[1]);
684 }
685 
686 ATF_TC_WITHOUT_HEAD(pipe_poll);
687 ATF_TC_BODY(pipe_poll, tc)
688 {
689 	aio_pipe_test(poll, NULL);
690 }
691 
692 ATF_TC_WITHOUT_HEAD(pipe_signal);
693 ATF_TC_BODY(pipe_signal, tc)
694 {
695 	aio_pipe_test(poll_signaled, setup_signal());
696 }
697 
698 ATF_TC_WITHOUT_HEAD(pipe_suspend);
699 ATF_TC_BODY(pipe_suspend, tc)
700 {
701 	aio_pipe_test(suspend, NULL);
702 }
703 
704 ATF_TC_WITHOUT_HEAD(pipe_thread);
705 ATF_TC_BODY(pipe_thread, tc)
706 {
707 	aio_pipe_test(poll_signaled, setup_thread());
708 }
709 
710 ATF_TC_WITHOUT_HEAD(pipe_waitcomplete);
711 ATF_TC_BODY(pipe_waitcomplete, tc)
712 {
713 	aio_pipe_test(waitcomplete, NULL);
714 }
715 
716 #define	MD_LEN		GLOBAL_MAX
717 #define	MDUNIT_LINK	"mdunit_link"
718 
719 static int
720 aio_md_setup(void)
721 {
722 	int error, fd, mdctl_fd, unit;
723 	char pathname[PATH_MAX];
724 	struct md_ioctl mdio;
725 	char buf[80];
726 
727 	ATF_REQUIRE_KERNEL_MODULE("aio");
728 
729 	mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
730 	ATF_REQUIRE_MSG(mdctl_fd != -1,
731 	    "opening /dev/%s failed: %s", MDCTL_NAME, strerror(errno));
732 
733 	bzero(&mdio, sizeof(mdio));
734 	mdio.md_version = MDIOVERSION;
735 	mdio.md_type = MD_MALLOC;
736 	mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
737 	mdio.md_mediasize = GLOBAL_MAX;
738 	mdio.md_sectorsize = 512;
739 	strlcpy(buf, __func__, sizeof(buf));
740 	mdio.md_label = buf;
741 
742 	if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
743 		error = errno;
744 		errno = error;
745 		atf_tc_fail("ioctl MDIOCATTACH failed: %s", strerror(errno));
746 	}
747 	close(mdctl_fd);
748 
749 	/* Store the md unit number in a symlink for future cleanup */
750 	unit = mdio.md_unit;
751 	snprintf(buf, sizeof(buf), "%d", unit);
752 	ATF_REQUIRE_EQ(0, symlink(buf, MDUNIT_LINK));
753 	snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
754 	fd = open(pathname, O_RDWR);
755 	ATF_REQUIRE_MSG(fd != -1,
756 	    "opening %s failed: %s", pathname, strerror(errno));
757 
758 	return (fd);
759 }
760 
761 static void
762 aio_md_cleanup(void)
763 {
764 	struct md_ioctl mdio;
765 	int mdctl_fd, n, unit;
766 	char buf[80];
767 
768 	mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
769 	if (mdctl_fd < 0) {
770 		fprintf(stderr, "opening /dev/%s failed: %s\n", MDCTL_NAME,
771 		    strerror(errno));
772 		return;
773 	}
774 	n = readlink(MDUNIT_LINK, buf, sizeof(buf) - 1);
775 	if (n > 0) {
776 		buf[n] = '\0';
777 		if (sscanf(buf, "%d", &unit) == 1 && unit >= 0) {
778 			bzero(&mdio, sizeof(mdio));
779 			mdio.md_version = MDIOVERSION;
780 			mdio.md_unit = unit;
781 			if (ioctl(mdctl_fd, MDIOCDETACH, &mdio) == -1) {
782 				fprintf(stderr,
783 				    "ioctl MDIOCDETACH unit %d failed: %s\n",
784 				    unit, strerror(errno));
785 			}
786 		}
787 	}
788 
789 	close(mdctl_fd);
790 }
791 
792 static void
793 aio_md_test(completion comp, struct sigevent *sev, bool vectored)
794 {
795 	struct aio_context ac;
796 	int fd;
797 
798 	fd = aio_md_setup();
799 	aio_context_init(&ac, fd, fd, MD_LEN);
800 	if (vectored) {
801 		aio_writev_test(&ac, comp, sev);
802 		aio_readv_test(&ac, comp, sev);
803 	} else {
804 		aio_write_test(&ac, comp, sev);
805 		aio_read_test(&ac, comp, sev);
806 	}
807 
808 	close(fd);
809 }
810 
811 ATF_TC_WITH_CLEANUP(md_poll);
812 ATF_TC_HEAD(md_poll, tc)
813 {
814 
815 	atf_tc_set_md_var(tc, "require.user", "root");
816 }
817 ATF_TC_BODY(md_poll, tc)
818 {
819 	aio_md_test(poll, NULL, false);
820 }
821 ATF_TC_CLEANUP(md_poll, tc)
822 {
823 	aio_md_cleanup();
824 }
825 
826 ATF_TC_WITH_CLEANUP(md_signal);
827 ATF_TC_HEAD(md_signal, tc)
828 {
829 
830 	atf_tc_set_md_var(tc, "require.user", "root");
831 }
832 ATF_TC_BODY(md_signal, tc)
833 {
834 	aio_md_test(poll_signaled, setup_signal(), false);
835 }
836 ATF_TC_CLEANUP(md_signal, tc)
837 {
838 	aio_md_cleanup();
839 }
840 
841 ATF_TC_WITH_CLEANUP(md_suspend);
842 ATF_TC_HEAD(md_suspend, tc)
843 {
844 
845 	atf_tc_set_md_var(tc, "require.user", "root");
846 }
847 ATF_TC_BODY(md_suspend, tc)
848 {
849 	aio_md_test(suspend, NULL, false);
850 }
851 ATF_TC_CLEANUP(md_suspend, tc)
852 {
853 	aio_md_cleanup();
854 }
855 
856 ATF_TC_WITH_CLEANUP(md_thread);
857 ATF_TC_HEAD(md_thread, tc)
858 {
859 
860 	atf_tc_set_md_var(tc, "require.user", "root");
861 }
862 ATF_TC_BODY(md_thread, tc)
863 {
864 	aio_md_test(poll_signaled, setup_thread(), false);
865 }
866 ATF_TC_CLEANUP(md_thread, tc)
867 {
868 	aio_md_cleanup();
869 }
870 
871 ATF_TC_WITH_CLEANUP(md_waitcomplete);
872 ATF_TC_HEAD(md_waitcomplete, tc)
873 {
874 
875 	atf_tc_set_md_var(tc, "require.user", "root");
876 }
877 ATF_TC_BODY(md_waitcomplete, tc)
878 {
879 	aio_md_test(waitcomplete, NULL, false);
880 }
881 ATF_TC_CLEANUP(md_waitcomplete, tc)
882 {
883 	aio_md_cleanup();
884 }
885 
886 #define	ZVOL_VDEV_PATHNAME	"test_vdev"
887 #define POOL_SIZE		(1 << 28)	/* 256 MB */
888 #define ZVOL_SIZE		"64m"
889 #define POOL_NAME		"aio_testpool"
890 #define ZVOL_NAME		"aio_testvol"
891 
892 static int
893 aio_zvol_setup(void)
894 {
895 	FILE *pidfile;
896 	int fd;
897 	pid_t pid;
898 	char pool_name[80];
899 	char cmd[160];
900 	char zvol_name[160];
901 	char devname[160];
902 
903 	ATF_REQUIRE_KERNEL_MODULE("aio");
904 	ATF_REQUIRE_KERNEL_MODULE("zfs");
905 
906 	fd = open(ZVOL_VDEV_PATHNAME, O_RDWR | O_CREAT, 0600);
907 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
908 	ATF_REQUIRE_EQ_MSG(0,
909 	    ftruncate(fd, POOL_SIZE), "ftruncate failed: %s", strerror(errno));
910 	close(fd);
911 
912 	pid = getpid();
913 	pidfile = fopen("pidfile", "w");
914 	ATF_REQUIRE_MSG(NULL != pidfile, "fopen: %s", strerror(errno));
915 	fprintf(pidfile, "%d", pid);
916 	fclose(pidfile);
917 
918 	snprintf(pool_name, sizeof(pool_name), POOL_NAME ".%d", pid);
919 	snprintf(zvol_name, sizeof(zvol_name), "%s/" ZVOL_NAME, pool_name);
920 	snprintf(cmd, sizeof(cmd), "zpool create %s $PWD/" ZVOL_VDEV_PATHNAME,
921 	    pool_name);
922 	ATF_REQUIRE_EQ_MSG(0, system(cmd),
923 	    "zpool create failed: %s", strerror(errno));
924 	snprintf(cmd, sizeof(cmd),
925 	    "zfs create -o volblocksize=8192 -o volmode=dev -V "
926 		ZVOL_SIZE " %s", zvol_name);
927 	ATF_REQUIRE_EQ_MSG(0, system(cmd),
928 	    "zfs create failed: %s", strerror(errno));
929 
930 	snprintf(devname, sizeof(devname), "/dev/zvol/%s", zvol_name);
931 	do {
932 		fd = open(devname, O_RDWR);
933 	} while (fd == -1 && errno == EINTR) ;
934 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
935 	return (fd);
936 }
937 
938 static void
939 aio_zvol_cleanup(void)
940 {
941 	FILE *pidfile;
942 	pid_t testpid;
943 	char cmd[160];
944 
945 	pidfile = fopen("pidfile", "r");
946 	if (pidfile == NULL && errno == ENOENT) {
947 		/* Setup probably failed */
948 		return;
949 	}
950 	ATF_REQUIRE_MSG(NULL != pidfile, "fopen: %s", strerror(errno));
951 	ATF_REQUIRE_EQ(1, fscanf(pidfile, "%d", &testpid));
952 	fclose(pidfile);
953 
954 	snprintf(cmd, sizeof(cmd), "zpool destroy " POOL_NAME ".%d", testpid);
955 	system(cmd);
956 }
957 
958 
959 ATF_TC_WITHOUT_HEAD(aio_large_read_test);
960 ATF_TC_BODY(aio_large_read_test, tc)
961 {
962 	struct aiocb cb, *cbp;
963 	ssize_t nread;
964 	size_t len;
965 	int fd;
966 #ifdef __LP64__
967 	int clamped;
968 #endif
969 
970 	ATF_REQUIRE_KERNEL_MODULE("aio");
971 	ATF_REQUIRE_UNSAFE_AIO();
972 
973 #ifdef __LP64__
974 	len = sizeof(clamped);
975 	if (sysctlbyname("debug.iosize_max_clamp", &clamped, &len, NULL, 0) ==
976 	    -1)
977 		atf_libc_error(errno, "Failed to read debug.iosize_max_clamp");
978 #endif
979 
980 	/* Determine the maximum supported read(2) size. */
981 	len = SSIZE_MAX;
982 #ifdef __LP64__
983 	if (clamped)
984 		len = INT_MAX;
985 #endif
986 
987 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
988 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
989 
990 	unlink(FILE_PATHNAME);
991 
992 	memset(&cb, 0, sizeof(cb));
993 	cb.aio_nbytes = len;
994 	cb.aio_fildes = fd;
995 	cb.aio_buf = NULL;
996 	if (aio_read(&cb) == -1)
997 		atf_tc_fail("aio_read() of maximum read size failed: %s",
998 		    strerror(errno));
999 
1000 	nread = aio_waitcomplete(&cbp, NULL);
1001 	if (nread == -1)
1002 		atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
1003 	if (nread != 0)
1004 		atf_tc_fail("aio_read() from empty file returned data: %zd",
1005 		    nread);
1006 
1007 	memset(&cb, 0, sizeof(cb));
1008 	cb.aio_nbytes = len + 1;
1009 	cb.aio_fildes = fd;
1010 	cb.aio_buf = NULL;
1011 	if (aio_read(&cb) == -1) {
1012 		if (errno == EINVAL)
1013 			goto finished;
1014 		atf_tc_fail("aio_read() of too large read size failed: %s",
1015 		    strerror(errno));
1016 	}
1017 
1018 	nread = aio_waitcomplete(&cbp, NULL);
1019 	if (nread == -1) {
1020 		if (errno == EINVAL)
1021 			goto finished;
1022 		atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
1023 	}
1024 	atf_tc_fail("aio_read() of too large read size returned: %zd", nread);
1025 
1026 finished:
1027 	close(fd);
1028 }
1029 
1030 /*
1031  * This tests for a bug where arriving socket data can wakeup multiple
1032  * AIO read requests resulting in an uncancellable request.
1033  */
1034 ATF_TC_WITHOUT_HEAD(aio_socket_two_reads);
1035 ATF_TC_BODY(aio_socket_two_reads, tc)
1036 {
1037 	struct ioreq {
1038 		struct aiocb iocb;
1039 		char buffer[1024];
1040 	} ioreq[2];
1041 	struct aiocb *iocb;
1042 	unsigned i;
1043 	int s[2];
1044 	char c;
1045 
1046 	ATF_REQUIRE_KERNEL_MODULE("aio");
1047 #if __FreeBSD_version < 1100101
1048 	aft_tc_skip("kernel version %d is too old (%d required)",
1049 	    __FreeBSD_version, 1100101);
1050 #endif
1051 
1052 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
1053 
1054 	/* Queue two read requests. */
1055 	memset(&ioreq, 0, sizeof(ioreq));
1056 	for (i = 0; i < nitems(ioreq); i++) {
1057 		ioreq[i].iocb.aio_nbytes = sizeof(ioreq[i].buffer);
1058 		ioreq[i].iocb.aio_fildes = s[0];
1059 		ioreq[i].iocb.aio_buf = ioreq[i].buffer;
1060 		ATF_REQUIRE(aio_read(&ioreq[i].iocb) == 0);
1061 	}
1062 
1063 	/* Send a single byte.  This should complete one request. */
1064 	c = 0xc3;
1065 	ATF_REQUIRE(write(s[1], &c, sizeof(c)) == 1);
1066 
1067 	ATF_REQUIRE(aio_waitcomplete(&iocb, NULL) == 1);
1068 
1069 	/* Determine which request completed and verify the data was read. */
1070 	if (iocb == &ioreq[0].iocb)
1071 		i = 0;
1072 	else
1073 		i = 1;
1074 	ATF_REQUIRE(ioreq[i].buffer[0] == c);
1075 
1076 	i ^= 1;
1077 
1078 	/*
1079 	 * Try to cancel the other request.  On broken systems this
1080 	 * will fail and the process will hang on exit.
1081 	 */
1082 	ATF_REQUIRE(aio_error(&ioreq[i].iocb) == EINPROGRESS);
1083 	ATF_REQUIRE(aio_cancel(s[0], &ioreq[i].iocb) == AIO_CANCELED);
1084 
1085 	close(s[1]);
1086 	close(s[0]);
1087 }
1088 
1089 static void
1090 aio_socket_blocking_short_write_test(bool vectored)
1091 {
1092 	struct aiocb iocb, *iocbp;
1093 	struct iovec iov[2];
1094 	char *buffer[2];
1095 	ssize_t done, r;
1096 	int buffer_size, sb_size;
1097 	socklen_t len;
1098 	int s[2];
1099 
1100 	ATF_REQUIRE_KERNEL_MODULE("aio");
1101 
1102 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
1103 
1104 	len = sizeof(sb_size);
1105 	ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
1106 	    -1);
1107 	ATF_REQUIRE(len == sizeof(sb_size));
1108 	buffer_size = sb_size;
1109 
1110 	ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
1111 	    -1);
1112 	ATF_REQUIRE(len == sizeof(sb_size));
1113 	if (sb_size > buffer_size)
1114 		buffer_size = sb_size;
1115 
1116 	/*
1117 	 * Use twice the size of the MAX(receive buffer, send buffer)
1118 	 * to ensure that the write is split up into multiple writes
1119 	 * internally.
1120 	 */
1121 	buffer_size *= 2;
1122 
1123 	buffer[0] = malloc(buffer_size);
1124 	ATF_REQUIRE(buffer[0] != NULL);
1125 	buffer[1] = malloc(buffer_size);
1126 	ATF_REQUIRE(buffer[1] != NULL);
1127 
1128 	srandomdev();
1129 	aio_fill_buffer(buffer[1], buffer_size, random());
1130 
1131 	memset(&iocb, 0, sizeof(iocb));
1132 	iocb.aio_fildes = s[1];
1133 	if (vectored) {
1134 		iov[0].iov_base = buffer[1];
1135 		iov[0].iov_len = buffer_size / 2 + 1;
1136 		iov[1].iov_base = buffer[1] + buffer_size / 2 + 1;
1137 		iov[1].iov_len = buffer_size / 2 - 1;
1138 		iocb.aio_iov = iov;
1139 		iocb.aio_iovcnt = 2;
1140 		r = aio_writev(&iocb);
1141 		ATF_CHECK_EQ_MSG(0, r, "aio_writev returned %zd", r);
1142 	} else {
1143 		iocb.aio_buf = buffer[1];
1144 		iocb.aio_nbytes = buffer_size;
1145 		r = aio_write(&iocb);
1146 		ATF_CHECK_EQ_MSG(0, r, "aio_writev returned %zd", r);
1147 	}
1148 
1149 	done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
1150 	ATF_REQUIRE(done == buffer_size);
1151 
1152 	done = aio_waitcomplete(&iocbp, NULL);
1153 	ATF_REQUIRE(iocbp == &iocb);
1154 	ATF_REQUIRE(done == buffer_size);
1155 
1156 	ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
1157 
1158 	close(s[1]);
1159 	close(s[0]);
1160 }
1161 
1162 /*
1163  * This test ensures that aio_write() on a blocking socket of a "large"
1164  * buffer does not return a short completion.
1165  */
1166 ATF_TC_WITHOUT_HEAD(aio_socket_blocking_short_write);
1167 ATF_TC_BODY(aio_socket_blocking_short_write, tc)
1168 {
1169 	aio_socket_blocking_short_write_test(false);
1170 }
1171 
1172 /*
1173  * Like aio_socket_blocking_short_write, but also tests that partially
1174  * completed vectored sends can be retried correctly.
1175  */
1176 ATF_TC_WITHOUT_HEAD(aio_socket_blocking_short_write_vectored);
1177 ATF_TC_BODY(aio_socket_blocking_short_write_vectored, tc)
1178 {
1179 	aio_socket_blocking_short_write_test(true);
1180 }
1181 
1182 /*
1183  * Verify that AIO requests fail when applied to a listening socket.
1184  */
1185 ATF_TC_WITHOUT_HEAD(aio_socket_listen_fail);
1186 ATF_TC_BODY(aio_socket_listen_fail, tc)
1187 {
1188 	struct aiocb iocb;
1189 	struct sockaddr_un sun;
1190 	char buf[16];
1191 	int s;
1192 
1193 	s = socket(AF_LOCAL, SOCK_STREAM, 0);
1194 	ATF_REQUIRE(s != -1);
1195 
1196 	memset(&sun, 0, sizeof(sun));
1197 	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", "listen.XXXXXX");
1198 	mktemp(sun.sun_path);
1199 	sun.sun_family = AF_LOCAL;
1200 	sun.sun_len = SUN_LEN(&sun);
1201 
1202 	ATF_REQUIRE(bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) == 0);
1203 	ATF_REQUIRE(listen(s, 5) == 0);
1204 
1205 	memset(buf, 0, sizeof(buf));
1206 	memset(&iocb, 0, sizeof(iocb));
1207 	iocb.aio_fildes = s;
1208 	iocb.aio_buf = buf;
1209 	iocb.aio_nbytes = sizeof(buf);
1210 
1211 	ATF_REQUIRE_ERRNO(EINVAL, aio_read(&iocb) == -1);
1212 	ATF_REQUIRE_ERRNO(EINVAL, aio_write(&iocb) == -1);
1213 
1214 	ATF_REQUIRE(unlink(sun.sun_path) == 0);
1215 	close(s);
1216 }
1217 
1218 /*
1219  * Verify that listen(2) fails if a socket has pending AIO requests.
1220  */
1221 ATF_TC_WITHOUT_HEAD(aio_socket_listen_pending);
1222 ATF_TC_BODY(aio_socket_listen_pending, tc)
1223 {
1224 	struct aiocb iocb;
1225 	struct sockaddr_un sun;
1226 	char buf[16];
1227 	int s;
1228 
1229 	s = socket(AF_LOCAL, SOCK_STREAM, 0);
1230 	ATF_REQUIRE(s != -1);
1231 
1232 	memset(&sun, 0, sizeof(sun));
1233 	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", "listen.XXXXXX");
1234 	mktemp(sun.sun_path);
1235 	sun.sun_family = AF_LOCAL;
1236 	sun.sun_len = SUN_LEN(&sun);
1237 
1238 	ATF_REQUIRE(bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) == 0);
1239 
1240 	memset(buf, 0, sizeof(buf));
1241 	memset(&iocb, 0, sizeof(iocb));
1242 	iocb.aio_fildes = s;
1243 	iocb.aio_buf = buf;
1244 	iocb.aio_nbytes = sizeof(buf);
1245 	ATF_REQUIRE(aio_read(&iocb) == 0);
1246 
1247 	ATF_REQUIRE_ERRNO(EINVAL, listen(s, 5) == -1);
1248 
1249 	ATF_REQUIRE(aio_cancel(s, &iocb) != -1);
1250 
1251 	ATF_REQUIRE(unlink(sun.sun_path) == 0);
1252 	close(s);
1253 }
1254 
1255 /*
1256  * This test verifies that cancelling a partially completed socket write
1257  * returns a short write rather than ECANCELED.
1258  */
1259 ATF_TC_WITHOUT_HEAD(aio_socket_short_write_cancel);
1260 ATF_TC_BODY(aio_socket_short_write_cancel, tc)
1261 {
1262 	struct aiocb iocb, *iocbp;
1263 	char *buffer[2];
1264 	ssize_t done;
1265 	int buffer_size, sb_size;
1266 	socklen_t len;
1267 	int s[2];
1268 
1269 	ATF_REQUIRE_KERNEL_MODULE("aio");
1270 
1271 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
1272 
1273 	len = sizeof(sb_size);
1274 	ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
1275 	    -1);
1276 	ATF_REQUIRE(len == sizeof(sb_size));
1277 	buffer_size = sb_size;
1278 
1279 	ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
1280 	    -1);
1281 	ATF_REQUIRE(len == sizeof(sb_size));
1282 	if (sb_size > buffer_size)
1283 		buffer_size = sb_size;
1284 
1285 	/*
1286 	 * Use three times the size of the MAX(receive buffer, send
1287 	 * buffer) for the write to ensure that the write is split up
1288 	 * into multiple writes internally.  The recv() ensures that
1289 	 * the write has partially completed, but a remaining size of
1290 	 * two buffers should ensure that the write has not completed
1291 	 * fully when it is cancelled.
1292 	 */
1293 	buffer[0] = malloc(buffer_size);
1294 	ATF_REQUIRE(buffer[0] != NULL);
1295 	buffer[1] = malloc(buffer_size * 3);
1296 	ATF_REQUIRE(buffer[1] != NULL);
1297 
1298 	srandomdev();
1299 	aio_fill_buffer(buffer[1], buffer_size * 3, random());
1300 
1301 	memset(&iocb, 0, sizeof(iocb));
1302 	iocb.aio_fildes = s[1];
1303 	iocb.aio_buf = buffer[1];
1304 	iocb.aio_nbytes = buffer_size * 3;
1305 	ATF_REQUIRE(aio_write(&iocb) == 0);
1306 
1307 	done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
1308 	ATF_REQUIRE(done == buffer_size);
1309 
1310 	ATF_REQUIRE(aio_error(&iocb) == EINPROGRESS);
1311 	ATF_REQUIRE(aio_cancel(s[1], &iocb) == AIO_NOTCANCELED);
1312 
1313 	done = aio_waitcomplete(&iocbp, NULL);
1314 	ATF_REQUIRE(iocbp == &iocb);
1315 	ATF_REQUIRE(done >= buffer_size && done <= buffer_size * 2);
1316 
1317 	ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
1318 
1319 	close(s[1]);
1320 	close(s[0]);
1321 }
1322 
1323 /*
1324  * Test handling of aio_read() and aio_write() on shut-down sockets.
1325  */
1326 ATF_TC_WITHOUT_HEAD(aio_socket_shutdown);
1327 ATF_TC_BODY(aio_socket_shutdown, tc)
1328 {
1329 	struct aiocb iocb;
1330 	sigset_t set;
1331 	char *buffer;
1332 	ssize_t len;
1333 	size_t bsz;
1334 	int error, s[2];
1335 
1336 	ATF_REQUIRE_KERNEL_MODULE("aio");
1337 
1338 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
1339 
1340 	bsz = 1024;
1341 	buffer = malloc(bsz);
1342 	memset(buffer, 0, bsz);
1343 
1344 	/* Put some data in s[0]'s recv buffer. */
1345 	ATF_REQUIRE(send(s[1], buffer, bsz, 0) == (ssize_t)bsz);
1346 
1347 	/* No more reading from s[0]. */
1348 	ATF_REQUIRE(shutdown(s[0], SHUT_RD) != -1);
1349 
1350 	ATF_REQUIRE(buffer != NULL);
1351 
1352 	memset(&iocb, 0, sizeof(iocb));
1353 	iocb.aio_fildes = s[0];
1354 	iocb.aio_buf = buffer;
1355 	iocb.aio_nbytes = bsz;
1356 	ATF_REQUIRE(aio_read(&iocb) == 0);
1357 
1358 	/* Expect to see zero bytes, analogous to recv(2). */
1359 	while ((error = aio_error(&iocb)) == EINPROGRESS)
1360 		usleep(25000);
1361 	ATF_REQUIRE_MSG(error == 0, "aio_error() returned %d", error);
1362 	len = aio_return(&iocb);
1363 	ATF_REQUIRE_MSG(len == 0, "read job returned %zd bytes", len);
1364 
1365 	/* No more writing to s[1]. */
1366 	ATF_REQUIRE(shutdown(s[1], SHUT_WR) != -1);
1367 
1368 	/* Block SIGPIPE so that we can detect the error in-band. */
1369 	sigemptyset(&set);
1370 	sigaddset(&set, SIGPIPE);
1371 	ATF_REQUIRE(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
1372 
1373 	memset(&iocb, 0, sizeof(iocb));
1374 	iocb.aio_fildes = s[1];
1375 	iocb.aio_buf = buffer;
1376 	iocb.aio_nbytes = bsz;
1377 	ATF_REQUIRE(aio_write(&iocb) == 0);
1378 
1379 	/* Expect an error, analogous to send(2). */
1380 	while ((error = aio_error(&iocb)) == EINPROGRESS)
1381 		usleep(25000);
1382 	ATF_REQUIRE_MSG(error == EPIPE, "aio_error() returned %d", error);
1383 
1384 	ATF_REQUIRE(close(s[0]) != -1);
1385 	ATF_REQUIRE(close(s[1]) != -1);
1386 	free(buffer);
1387 }
1388 
1389 /*
1390  * test aio_fsync's behavior with bad inputs
1391  */
1392 ATF_TC_WITHOUT_HEAD(aio_fsync_errors);
1393 ATF_TC_BODY(aio_fsync_errors, tc)
1394 {
1395 	int fd;
1396 	struct aiocb iocb;
1397 
1398 	ATF_REQUIRE_KERNEL_MODULE("aio");
1399 	ATF_REQUIRE_UNSAFE_AIO();
1400 
1401 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
1402 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1403 	unlink(FILE_PATHNAME);
1404 
1405 	/* aio_fsync should return EINVAL unless op is O_SYNC or O_DSYNC */
1406 	memset(&iocb, 0, sizeof(iocb));
1407 	iocb.aio_fildes = fd;
1408 	ATF_CHECK_EQ(-1, aio_fsync(666, &iocb));
1409 	ATF_CHECK_EQ(EINVAL, errno);
1410 
1411 	/* aio_fsync should return EBADF if fd is not a valid descriptor */
1412 	memset(&iocb, 0, sizeof(iocb));
1413 	iocb.aio_fildes = 666;
1414 	ATF_CHECK_EQ(-1, aio_fsync(O_SYNC, &iocb));
1415 	ATF_CHECK_EQ(EBADF, errno);
1416 
1417 	/* aio_fsync should return EINVAL if sigev_notify is invalid */
1418 	memset(&iocb, 0, sizeof(iocb));
1419 	iocb.aio_fildes = fd;
1420 	iocb.aio_sigevent.sigev_notify = 666;
1421 	ATF_CHECK_EQ(-1, aio_fsync(666, &iocb));
1422 	ATF_CHECK_EQ(EINVAL, errno);
1423 }
1424 
1425 /*
1426  * This test just performs a basic test of aio_fsync().
1427  */
1428 static void
1429 aio_fsync_test(int op)
1430 {
1431 	struct aiocb synccb, *iocbp;
1432 	struct {
1433 		struct aiocb iocb;
1434 		bool done;
1435 		char *buffer;
1436 	} buffers[16];
1437 	struct stat sb;
1438 	ssize_t rval;
1439 	unsigned i;
1440 	int fd;
1441 
1442 	ATF_REQUIRE_KERNEL_MODULE("aio");
1443 	ATF_REQUIRE_UNSAFE_AIO();
1444 
1445 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
1446 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1447 	unlink(FILE_PATHNAME);
1448 
1449 	ATF_REQUIRE(fstat(fd, &sb) == 0);
1450 	ATF_REQUIRE(sb.st_blksize != 0);
1451 	ATF_REQUIRE(ftruncate(fd, sb.st_blksize * nitems(buffers)) == 0);
1452 
1453 	/*
1454 	 * Queue several asynchronous write requests.  Hopefully this
1455 	 * forces the aio_fsync() request to be deferred.  There is no
1456 	 * reliable way to guarantee that however.
1457 	 */
1458 	srandomdev();
1459 	for (i = 0; i < nitems(buffers); i++) {
1460 		buffers[i].done = false;
1461 		memset(&buffers[i].iocb, 0, sizeof(buffers[i].iocb));
1462 		buffers[i].buffer = malloc(sb.st_blksize);
1463 		aio_fill_buffer(buffers[i].buffer, sb.st_blksize, random());
1464 		buffers[i].iocb.aio_fildes = fd;
1465 		buffers[i].iocb.aio_buf = buffers[i].buffer;
1466 		buffers[i].iocb.aio_nbytes = sb.st_blksize;
1467 		buffers[i].iocb.aio_offset = sb.st_blksize * i;
1468 		ATF_REQUIRE(aio_write(&buffers[i].iocb) == 0);
1469 	}
1470 
1471 	/* Queue the aio_fsync request. */
1472 	memset(&synccb, 0, sizeof(synccb));
1473 	synccb.aio_fildes = fd;
1474 	ATF_REQUIRE(aio_fsync(op, &synccb) == 0);
1475 
1476 	/* Wait for requests to complete. */
1477 	for (;;) {
1478 	next:
1479 		rval = aio_waitcomplete(&iocbp, NULL);
1480 		ATF_REQUIRE(iocbp != NULL);
1481 		if (iocbp == &synccb) {
1482 			ATF_REQUIRE(rval == 0);
1483 			break;
1484 		}
1485 
1486 		for (i = 0; i < nitems(buffers); i++) {
1487 			if (iocbp == &buffers[i].iocb) {
1488 				ATF_REQUIRE(buffers[i].done == false);
1489 				ATF_REQUIRE(rval == sb.st_blksize);
1490 				buffers[i].done = true;
1491 				goto next;
1492 			}
1493 		}
1494 
1495 		ATF_REQUIRE_MSG(false, "unmatched AIO request");
1496 	}
1497 
1498 	for (i = 0; i < nitems(buffers); i++)
1499 		ATF_REQUIRE_MSG(buffers[i].done,
1500 		    "AIO request %u did not complete", i);
1501 
1502 	close(fd);
1503 }
1504 
1505 ATF_TC_WITHOUT_HEAD(aio_fsync_sync_test);
1506 ATF_TC_BODY(aio_fsync_sync_test, tc)
1507 {
1508 	aio_fsync_test(O_SYNC);
1509 }
1510 
1511 ATF_TC_WITHOUT_HEAD(aio_fsync_dsync_test);
1512 ATF_TC_BODY(aio_fsync_dsync_test, tc)
1513 {
1514 	aio_fsync_test(O_DSYNC);
1515 }
1516 
1517 /*
1518  * We shouldn't be able to DoS the system by setting iov_len to an insane
1519  * value
1520  */
1521 ATF_TC_WITHOUT_HEAD(aio_writev_dos_iov_len);
1522 ATF_TC_BODY(aio_writev_dos_iov_len, tc)
1523 {
1524 	struct aiocb aio;
1525 	const struct aiocb *const iocbs[] = {&aio};
1526 	const char *wbuf = "Hello, world!";
1527 	struct iovec iov[1];
1528 	ssize_t len, r;
1529 	int fd;
1530 
1531 	ATF_REQUIRE_KERNEL_MODULE("aio");
1532 	ATF_REQUIRE_UNSAFE_AIO();
1533 
1534 	fd = open("testfile", O_RDWR | O_CREAT, 0600);
1535 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1536 
1537 	len = strlen(wbuf);
1538 	iov[0].iov_base = __DECONST(void*, wbuf);
1539 	iov[0].iov_len = 1 << 30;
1540 	bzero(&aio, sizeof(aio));
1541 	aio.aio_fildes = fd;
1542 	aio.aio_offset = 0;
1543 	aio.aio_iov = iov;
1544 	aio.aio_iovcnt = 1;
1545 
1546 	r = aio_writev(&aio);
1547 	ATF_CHECK_EQ_MSG(0, r, "aio_writev returned %zd", r);
1548 	ATF_REQUIRE_EQ(0, aio_suspend(iocbs, 1, NULL));
1549 	r = aio_return(&aio);
1550 	ATF_CHECK_EQ_MSG(-1, r, "aio_return returned %zd", r);
1551 	ATF_CHECK_MSG(errno == EFAULT || errno == EINVAL,
1552 	    "aio_writev: %s", strerror(errno));
1553 
1554 	close(fd);
1555 }
1556 
1557 /*
1558  * We shouldn't be able to DoS the system by setting aio_iovcnt to an insane
1559  * value
1560  */
1561 ATF_TC_WITHOUT_HEAD(aio_writev_dos_iovcnt);
1562 ATF_TC_BODY(aio_writev_dos_iovcnt, tc)
1563 {
1564 	struct aiocb aio;
1565 	const char *wbuf = "Hello, world!";
1566 	struct iovec iov[1];
1567 	ssize_t len;
1568 	int fd;
1569 
1570 	ATF_REQUIRE_KERNEL_MODULE("aio");
1571 	ATF_REQUIRE_UNSAFE_AIO();
1572 
1573 	fd = open("testfile", O_RDWR | O_CREAT, 0600);
1574 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1575 
1576 	len = strlen(wbuf);
1577 	iov[0].iov_base = __DECONST(void*, wbuf);
1578 	iov[0].iov_len = len;
1579 	bzero(&aio, sizeof(aio));
1580 	aio.aio_fildes = fd;
1581 	aio.aio_offset = 0;
1582 	aio.aio_iov = iov;
1583 	aio.aio_iovcnt = 1 << 30;
1584 
1585 	ATF_REQUIRE_EQ(-1, aio_writev(&aio));
1586 	ATF_CHECK_EQ(EINVAL, errno);
1587 
1588 	close(fd);
1589 }
1590 
1591 ATF_TC_WITH_CLEANUP(aio_writev_efault);
1592 ATF_TC_HEAD(aio_writev_efault, tc)
1593 {
1594 	atf_tc_set_md_var(tc, "descr",
1595 	    "Vectored AIO should gracefully handle invalid addresses");
1596 	atf_tc_set_md_var(tc, "require.user", "root");
1597 }
1598 ATF_TC_BODY(aio_writev_efault, tc)
1599 {
1600 	struct aiocb aio;
1601 	ssize_t buflen;
1602 	char *buffer;
1603 	struct iovec iov[2];
1604 	long seed;
1605 	int fd;
1606 
1607 	ATF_REQUIRE_KERNEL_MODULE("aio");
1608 	ATF_REQUIRE_UNSAFE_AIO();
1609 
1610 	fd = aio_md_setup();
1611 
1612 	seed = random();
1613 	buflen = 4096;
1614 	buffer = malloc(buflen);
1615 	aio_fill_buffer(buffer, buflen, seed);
1616 	iov[0].iov_base = buffer;
1617 	iov[0].iov_len = buflen;
1618 	iov[1].iov_base = (void*)-1;	/* Invalid! */
1619 	iov[1].iov_len = buflen;
1620 	bzero(&aio, sizeof(aio));
1621 	aio.aio_fildes = fd;
1622 	aio.aio_offset = 0;
1623 	aio.aio_iov = iov;
1624 	aio.aio_iovcnt = nitems(iov);
1625 
1626 	ATF_REQUIRE_EQ(-1, aio_writev(&aio));
1627 	ATF_CHECK_EQ(EFAULT, errno);
1628 
1629 	close(fd);
1630 }
1631 ATF_TC_CLEANUP(aio_writev_efault, tc)
1632 {
1633 	aio_md_cleanup();
1634 }
1635 
1636 ATF_TC_WITHOUT_HEAD(aio_writev_empty_file_poll);
1637 ATF_TC_BODY(aio_writev_empty_file_poll, tc)
1638 {
1639 	struct aiocb aio;
1640 	int fd;
1641 
1642 	ATF_REQUIRE_KERNEL_MODULE("aio");
1643 	ATF_REQUIRE_UNSAFE_AIO();
1644 
1645 	fd = open("testfile", O_RDWR | O_CREAT, 0600);
1646 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1647 
1648 	bzero(&aio, sizeof(aio));
1649 	aio.aio_fildes = fd;
1650 	aio.aio_offset = 0;
1651 	aio.aio_iovcnt = 0;
1652 
1653 	ATF_REQUIRE_EQ(0, aio_writev(&aio));
1654 	ATF_REQUIRE_EQ(0, suspend(&aio));
1655 
1656 	close(fd);
1657 }
1658 
1659 ATF_TC_WITHOUT_HEAD(aio_writev_empty_file_signal);
1660 ATF_TC_BODY(aio_writev_empty_file_signal, tc)
1661 {
1662 	struct aiocb aio;
1663 	int fd;
1664 
1665 	ATF_REQUIRE_KERNEL_MODULE("aio");
1666 	ATF_REQUIRE_UNSAFE_AIO();
1667 
1668 	fd = open("testfile", O_RDWR | O_CREAT, 0600);
1669 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1670 
1671 	bzero(&aio, sizeof(aio));
1672 	aio.aio_fildes = fd;
1673 	aio.aio_offset = 0;
1674 	aio.aio_iovcnt = 0;
1675 	aio.aio_sigevent = *setup_signal();
1676 
1677 	ATF_REQUIRE_EQ(0, aio_writev(&aio));
1678 	ATF_REQUIRE_EQ(0, poll_signaled(&aio));
1679 
1680 	close(fd);
1681 }
1682 
1683 // aio_writev and aio_readv should still work even if the iovcnt is greater
1684 // than the number of buffered AIO operations permitted per process.
1685 ATF_TC_WITH_CLEANUP(vectored_big_iovcnt);
1686 ATF_TC_HEAD(vectored_big_iovcnt, tc)
1687 {
1688 	atf_tc_set_md_var(tc, "descr",
1689 	    "Vectored AIO should still work even if the iovcnt is greater than "
1690 	    "the number of buffered AIO operations permitted by the process");
1691 	atf_tc_set_md_var(tc, "require.user", "root");
1692 }
1693 ATF_TC_BODY(vectored_big_iovcnt, tc)
1694 {
1695 	struct aiocb aio;
1696 	struct iovec *iov;
1697 	ssize_t len, buflen;
1698 	char *buffer;
1699 	const char *oid = "vfs.aio.max_buf_aio";
1700 	long seed;
1701 	int max_buf_aio;
1702 	int fd, i;
1703 	ssize_t sysctl_len = sizeof(max_buf_aio);
1704 
1705 	ATF_REQUIRE_KERNEL_MODULE("aio");
1706 	ATF_REQUIRE_UNSAFE_AIO();
1707 
1708 	if (sysctlbyname(oid, &max_buf_aio, &sysctl_len, NULL, 0) == -1)
1709 		atf_libc_error(errno, "Failed to read %s", oid);
1710 
1711 	seed = random();
1712 	buflen = 512 * (max_buf_aio + 1);
1713 	buffer = malloc(buflen);
1714 	aio_fill_buffer(buffer, buflen, seed);
1715 	iov = calloc(max_buf_aio + 1, sizeof(struct iovec));
1716 
1717 	fd = aio_md_setup();
1718 
1719 	bzero(&aio, sizeof(aio));
1720 	aio.aio_fildes = fd;
1721 	aio.aio_offset = 0;
1722 	for (i = 0; i < max_buf_aio + 1; i++) {
1723 		iov[i].iov_base = &buffer[i * 512];
1724 		iov[i].iov_len = 512;
1725 	}
1726 	aio.aio_iov = iov;
1727 	aio.aio_iovcnt = max_buf_aio + 1;
1728 
1729 	if (aio_writev(&aio) < 0)
1730 		atf_tc_fail("aio_writev failed: %s", strerror(errno));
1731 
1732 	len = poll(&aio);
1733 	if (len < 0)
1734 		atf_tc_fail("aio failed: %s", strerror(errno));
1735 
1736 	if (len != buflen)
1737 		atf_tc_fail("aio short write (%jd)", (intmax_t)len);
1738 
1739 	bzero(&aio, sizeof(aio));
1740 	aio.aio_fildes = fd;
1741 	aio.aio_offset = 0;
1742 	aio.aio_iov = iov;
1743 	aio.aio_iovcnt = max_buf_aio + 1;
1744 
1745 	if (aio_readv(&aio) < 0)
1746 		atf_tc_fail("aio_readv failed: %s", strerror(errno));
1747 
1748 	len = poll(&aio);
1749 	if (len < 0)
1750 		atf_tc_fail("aio failed: %s", strerror(errno));
1751 
1752 	if (len != buflen)
1753 		atf_tc_fail("aio short read (%jd)", (intmax_t)len);
1754 
1755 	if (aio_test_buffer(buffer, buflen, seed) == 0)
1756 		atf_tc_fail("buffer mismatched");
1757 
1758 	close(fd);
1759 }
1760 ATF_TC_CLEANUP(vectored_big_iovcnt, tc)
1761 {
1762 	aio_md_cleanup();
1763 }
1764 
1765 ATF_TC_WITHOUT_HEAD(vectored_file_poll);
1766 ATF_TC_BODY(vectored_file_poll, tc)
1767 {
1768 	aio_file_test(poll, NULL, true);
1769 }
1770 
1771 ATF_TC_WITHOUT_HEAD(vectored_thread);
1772 ATF_TC_BODY(vectored_thread, tc)
1773 {
1774 	aio_file_test(poll_signaled, setup_thread(), true);
1775 }
1776 
1777 ATF_TC_WITH_CLEANUP(vectored_md_poll);
1778 ATF_TC_HEAD(vectored_md_poll, tc)
1779 {
1780 	atf_tc_set_md_var(tc, "require.user", "root");
1781 }
1782 ATF_TC_BODY(vectored_md_poll, tc)
1783 {
1784 	aio_md_test(poll, NULL, true);
1785 }
1786 ATF_TC_CLEANUP(vectored_md_poll, tc)
1787 {
1788 	aio_md_cleanup();
1789 }
1790 
1791 ATF_TC_WITHOUT_HEAD(vectored_socket_poll);
1792 ATF_TC_BODY(vectored_socket_poll, tc)
1793 {
1794 	aio_unix_socketpair_test(poll, NULL, true);
1795 }
1796 
1797 // aio_writev and aio_readv should still work even if the iov contains elements
1798 // that aren't a multiple of the device's sector size, and even if the total
1799 // amount if I/O _is_ a multiple of the device's sector size.
1800 ATF_TC_WITH_CLEANUP(vectored_unaligned);
1801 ATF_TC_HEAD(vectored_unaligned, tc)
1802 {
1803 	atf_tc_set_md_var(tc, "descr",
1804 	    "Vectored AIO should still work even if the iov contains elements "
1805 	    "that aren't a multiple of the sector size.");
1806 	atf_tc_set_md_var(tc, "require.user", "root");
1807 }
1808 ATF_TC_BODY(vectored_unaligned, tc)
1809 {
1810 	struct aio_context ac;
1811 	struct aiocb aio;
1812 	struct iovec iov[3];
1813 	ssize_t len, total_len;
1814 	int fd;
1815 
1816 	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
1817 		atf_tc_skip("https://bugs.freebsd.org/258766");
1818 
1819 	ATF_REQUIRE_KERNEL_MODULE("aio");
1820 	ATF_REQUIRE_UNSAFE_AIO();
1821 
1822 	/*
1823 	 * Use a zvol with volmode=dev, so it will allow .d_write with
1824 	 * unaligned uio.  geom devices use physio, which doesn't allow that.
1825 	 */
1826 	fd = aio_zvol_setup();
1827 	aio_context_init(&ac, fd, fd, FILE_LEN);
1828 
1829 	/* Break the buffer into 3 parts:
1830 	 * * A 4kB part, aligned to 4kB
1831 	 * * Two other parts that add up to 4kB:
1832 	 *   - 256B
1833 	 *   - 4kB - 256B
1834 	 */
1835 	iov[0].iov_base = ac.ac_buffer;
1836 	iov[0].iov_len = 4096;
1837 	iov[1].iov_base = (void*)((uintptr_t)iov[0].iov_base + iov[0].iov_len);
1838 	iov[1].iov_len = 256;
1839 	iov[2].iov_base = (void*)((uintptr_t)iov[1].iov_base + iov[1].iov_len);
1840 	iov[2].iov_len = 4096 - iov[1].iov_len;
1841 	total_len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
1842 	bzero(&aio, sizeof(aio));
1843 	aio.aio_fildes = ac.ac_write_fd;
1844 	aio.aio_offset = 0;
1845 	aio.aio_iov = iov;
1846 	aio.aio_iovcnt = 3;
1847 
1848 	if (aio_writev(&aio) < 0)
1849 		atf_tc_fail("aio_writev failed: %s", strerror(errno));
1850 
1851 	len = poll(&aio);
1852 	if (len < 0)
1853 		atf_tc_fail("aio failed: %s", strerror(errno));
1854 
1855 	if (len != total_len)
1856 		atf_tc_fail("aio short write (%jd)", (intmax_t)len);
1857 
1858 	bzero(&aio, sizeof(aio));
1859 	aio.aio_fildes = ac.ac_read_fd;
1860 	aio.aio_offset = 0;
1861 	aio.aio_iov = iov;
1862 	aio.aio_iovcnt = 3;
1863 
1864 	if (aio_readv(&aio) < 0)
1865 		atf_tc_fail("aio_readv failed: %s", strerror(errno));
1866 	len = poll(&aio);
1867 
1868 	ATF_REQUIRE_MSG(aio_test_buffer(ac.ac_buffer, total_len,
1869 	    ac.ac_seed) != 0, "aio_test_buffer: internal error");
1870 
1871 	close(fd);
1872 }
1873 ATF_TC_CLEANUP(vectored_unaligned, tc)
1874 {
1875 	aio_zvol_cleanup();
1876 }
1877 
1878 static void
1879 aio_zvol_test(completion comp, struct sigevent *sev, bool vectored)
1880 {
1881 	struct aio_context ac;
1882 	int fd;
1883 
1884 	fd = aio_zvol_setup();
1885 	aio_context_init(&ac, fd, fd, MD_LEN);
1886 	if (vectored) {
1887 		aio_writev_test(&ac, comp, sev);
1888 		aio_readv_test(&ac, comp, sev);
1889 	} else {
1890 		aio_write_test(&ac, comp, sev);
1891 		aio_read_test(&ac, comp, sev);
1892 	}
1893 
1894 	close(fd);
1895 }
1896 
1897 /*
1898  * Note that unlike md, the zvol is not a geom device, does not allow unmapped
1899  * buffers, and does not use physio.
1900  */
1901 ATF_TC_WITH_CLEANUP(vectored_zvol_poll);
1902 ATF_TC_HEAD(vectored_zvol_poll, tc)
1903 {
1904 	atf_tc_set_md_var(tc, "require.user", "root");
1905 }
1906 ATF_TC_BODY(vectored_zvol_poll, tc)
1907 {
1908 	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
1909 		atf_tc_skip("https://bugs.freebsd.org/258766");
1910 	aio_zvol_test(poll, NULL, true);
1911 }
1912 ATF_TC_CLEANUP(vectored_zvol_poll, tc)
1913 {
1914 	aio_zvol_cleanup();
1915 }
1916 
1917 ATF_TP_ADD_TCS(tp)
1918 {
1919 
1920 	ATF_TP_ADD_TC(tp, file_poll);
1921 	ATF_TP_ADD_TC(tp, file_signal);
1922 	ATF_TP_ADD_TC(tp, file_suspend);
1923 	ATF_TP_ADD_TC(tp, file_thread);
1924 	ATF_TP_ADD_TC(tp, file_waitcomplete);
1925 	ATF_TP_ADD_TC(tp, fifo_poll);
1926 	ATF_TP_ADD_TC(tp, fifo_signal);
1927 	ATF_TP_ADD_TC(tp, fifo_suspend);
1928 	ATF_TP_ADD_TC(tp, fifo_thread);
1929 	ATF_TP_ADD_TC(tp, fifo_waitcomplete);
1930 	ATF_TP_ADD_TC(tp, socket_poll);
1931 	ATF_TP_ADD_TC(tp, socket_signal);
1932 	ATF_TP_ADD_TC(tp, socket_suspend);
1933 	ATF_TP_ADD_TC(tp, socket_thread);
1934 	ATF_TP_ADD_TC(tp, socket_waitcomplete);
1935 	ATF_TP_ADD_TC(tp, pty_poll);
1936 	ATF_TP_ADD_TC(tp, pty_signal);
1937 	ATF_TP_ADD_TC(tp, pty_suspend);
1938 	ATF_TP_ADD_TC(tp, pty_thread);
1939 	ATF_TP_ADD_TC(tp, pty_waitcomplete);
1940 	ATF_TP_ADD_TC(tp, pipe_poll);
1941 	ATF_TP_ADD_TC(tp, pipe_signal);
1942 	ATF_TP_ADD_TC(tp, pipe_suspend);
1943 	ATF_TP_ADD_TC(tp, pipe_thread);
1944 	ATF_TP_ADD_TC(tp, pipe_waitcomplete);
1945 	ATF_TP_ADD_TC(tp, md_poll);
1946 	ATF_TP_ADD_TC(tp, md_signal);
1947 	ATF_TP_ADD_TC(tp, md_suspend);
1948 	ATF_TP_ADD_TC(tp, md_thread);
1949 	ATF_TP_ADD_TC(tp, md_waitcomplete);
1950 	ATF_TP_ADD_TC(tp, aio_fsync_errors);
1951 	ATF_TP_ADD_TC(tp, aio_fsync_sync_test);
1952 	ATF_TP_ADD_TC(tp, aio_fsync_dsync_test);
1953 	ATF_TP_ADD_TC(tp, aio_large_read_test);
1954 	ATF_TP_ADD_TC(tp, aio_socket_two_reads);
1955 	ATF_TP_ADD_TC(tp, aio_socket_blocking_short_write);
1956 	ATF_TP_ADD_TC(tp, aio_socket_blocking_short_write_vectored);
1957 	ATF_TP_ADD_TC(tp, aio_socket_listen_fail);
1958 	ATF_TP_ADD_TC(tp, aio_socket_listen_pending);
1959 	ATF_TP_ADD_TC(tp, aio_socket_short_write_cancel);
1960 	ATF_TP_ADD_TC(tp, aio_socket_shutdown);
1961 	ATF_TP_ADD_TC(tp, aio_writev_dos_iov_len);
1962 	ATF_TP_ADD_TC(tp, aio_writev_dos_iovcnt);
1963 	ATF_TP_ADD_TC(tp, aio_writev_efault);
1964 	ATF_TP_ADD_TC(tp, aio_writev_empty_file_poll);
1965 	ATF_TP_ADD_TC(tp, aio_writev_empty_file_signal);
1966 	ATF_TP_ADD_TC(tp, vectored_big_iovcnt);
1967 	ATF_TP_ADD_TC(tp, vectored_file_poll);
1968 	ATF_TP_ADD_TC(tp, vectored_md_poll);
1969 	ATF_TP_ADD_TC(tp, vectored_zvol_poll);
1970 	ATF_TP_ADD_TC(tp, vectored_unaligned);
1971 	ATF_TP_ADD_TC(tp, vectored_socket_poll);
1972 	ATF_TP_ADD_TC(tp, vectored_thread);
1973 
1974 	return (atf_no_error());
1975 }
1976