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