xref: /freebsd/tests/sys/aio/aio_test.c (revision a4ea52aab6f9d80f8e17b05e7741c7a0361965c3)
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/module.h>
44 #include <sys/resource.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/mdioctl.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 <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <termios.h>
61 #include <unistd.h>
62 
63 #include <atf-c.h>
64 
65 #include "freebsd_test_suite/macros.h"
66 #include "local.h"
67 
68 /*
69  * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
70  * it sizes ac_buffer in the aio_context structure.  It is also the default
71  * size for file I/O.  For other types, we use smaller blocks or we risk
72  * blocking (and we run in a single process/thread so that would be bad).
73  */
74 #define	GLOBAL_MAX	16384
75 
76 #define	BUFFER_MAX	GLOBAL_MAX
77 
78 /*
79  * A completion function will block until the aio has completed, then return
80  * the result of the aio.  errno will be set appropriately.
81  */
82 typedef ssize_t (*completion)(struct aiocb*);
83 
84 struct aio_context {
85 	int		 ac_read_fd, ac_write_fd;
86 	long		 ac_seed;
87 	char		 ac_buffer[GLOBAL_MAX];
88 	int		 ac_buflen;
89 	int		 ac_seconds;
90 };
91 
92 static sem_t		completions;
93 
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 	switch (error) {
162 		case EINPROGRESS:
163 			errno = EINTR;
164 			return (-1);
165 		case 0:
166 			return (aio_return(aio));
167 		default:
168 			return (error);
169 	}
170 }
171 
172 static void
173 sigusr1_handler(int sig __unused)
174 {
175 	ATF_REQUIRE_EQ(0, sem_post(&completions));
176 }
177 
178 static void
179 thr_handler(union sigval sv __unused)
180 {
181 	ATF_REQUIRE_EQ(0, sem_post(&completions));
182 }
183 
184 static ssize_t
185 poll_signaled(struct aiocb *aio)
186 {
187 	int error;
188 
189 	ATF_REQUIRE_EQ(0, sem_wait(&completions));
190 	error = aio_error(aio);
191 	switch (error) {
192 		case EINPROGRESS:
193 			errno = EINTR;
194 			return (-1);
195 		case 0:
196 			return (aio_return(aio));
197 		default:
198 			return (error);
199 	}
200 }
201 
202 /*
203  * Setup a signal handler for signal delivery tests
204  * This isn't thread safe, but it's ok since ATF runs each testcase in a
205  * separate process
206  */
207 static struct sigevent*
208 setup_signal(void)
209 {
210 	static struct sigevent sev;
211 
212 	ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
213 	sev.sigev_notify = SIGEV_SIGNAL;
214 	sev.sigev_signo = SIGUSR1;
215 	ATF_REQUIRE(SIG_ERR != signal(SIGUSR1, sigusr1_handler));
216 	return (&sev);
217 }
218 
219 /*
220  * Setup a thread for thread delivery tests
221  * This isn't thread safe, but it's ok since ATF runs each testcase in a
222  * separate process
223  */
224 static struct sigevent*
225 setup_thread(void)
226 {
227 	static struct sigevent sev;
228 
229 	ATF_REQUIRE_EQ(0, sem_init(&completions, false, 0));
230 	sev.sigev_notify = SIGEV_THREAD;
231 	sev.sigev_notify_function = thr_handler;
232 	sev.sigev_notify_attributes = NULL;
233 	return (&sev);
234 }
235 
236 static ssize_t
237 suspend(struct aiocb *aio)
238 {
239 	const struct aiocb *const iocbs[] = {aio};
240 	int error;
241 
242 	error = aio_suspend(iocbs, 1, NULL);
243 	if (error == 0)
244 		return (aio_return(aio));
245 	else
246 		return (error);
247 }
248 
249 static ssize_t
250 waitcomplete(struct aiocb *aio)
251 {
252 	struct aiocb *aiop;
253 	ssize_t ret;
254 
255 	ret = aio_waitcomplete(&aiop, NULL);
256 	ATF_REQUIRE_EQ(aio, aiop);
257 	return (ret);
258 }
259 
260 /*
261  * Perform a simple write test of our initialized data buffer to the provided
262  * file descriptor.
263  */
264 static void
265 aio_write_test(struct aio_context *ac, completion comp, struct sigevent *sev)
266 {
267 	struct aiocb aio;
268 	ssize_t len;
269 
270 	bzero(&aio, sizeof(aio));
271 	aio.aio_buf = ac->ac_buffer;
272 	aio.aio_nbytes = ac->ac_buflen;
273 	aio.aio_fildes = ac->ac_write_fd;
274 	aio.aio_offset = 0;
275 	if (sev)
276 		aio.aio_sigevent = *sev;
277 
278 	if (aio_write(&aio) < 0)
279 		atf_tc_fail("aio_write failed: %s", strerror(errno));
280 
281 	len = comp(&aio);
282 	if (len < 0)
283 		atf_tc_fail("aio failed: %s", strerror(errno));
284 
285 	if (len != ac->ac_buflen)
286 		atf_tc_fail("aio short write (%jd)", (intmax_t)len);
287 }
288 
289 /*
290  * Perform a simple read test of our initialized data buffer from the
291  * provided file descriptor.
292  */
293 static void
294 aio_read_test(struct aio_context *ac, completion comp, struct sigevent *sev)
295 {
296 	struct aiocb aio;
297 	ssize_t len;
298 
299 	bzero(ac->ac_buffer, ac->ac_buflen);
300 	bzero(&aio, sizeof(aio));
301 	aio.aio_buf = ac->ac_buffer;
302 	aio.aio_nbytes = ac->ac_buflen;
303 	aio.aio_fildes = ac->ac_read_fd;
304 	aio.aio_offset = 0;
305 	if (sev)
306 		aio.aio_sigevent = *sev;
307 
308 	if (aio_read(&aio) < 0)
309 		atf_tc_fail("aio_read failed: %s", strerror(errno));
310 
311 	len = comp(&aio);
312 	if (len < 0)
313 		atf_tc_fail("aio failed: %s", strerror(errno));
314 
315 	ATF_REQUIRE_EQ_MSG(len, ac->ac_buflen,
316 	    "aio short read (%jd)", (intmax_t)len);
317 
318 	if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0)
319 		atf_tc_fail("buffer mismatched");
320 }
321 
322 /*
323  * Series of type-specific tests for AIO.  For now, we just make sure we can
324  * issue a write and then a read to each type.  We assume that once a write
325  * is issued, a read can follow.
326  */
327 
328 /*
329  * Test with a classic file.  Assumes we can create a moderate size temporary
330  * file.
331  */
332 #define	FILE_LEN	GLOBAL_MAX
333 #define	FILE_PATHNAME	"testfile"
334 
335 static void
336 aio_file_test(completion comp, struct sigevent *sev)
337 {
338 	struct aio_context ac;
339 	int fd;
340 
341 	ATF_REQUIRE_KERNEL_MODULE("aio");
342 	ATF_REQUIRE_UNSAFE_AIO();
343 
344 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
345 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
346 
347 	aio_context_init(&ac, fd, fd, FILE_LEN);
348 	aio_write_test(&ac, comp, sev);
349 	aio_read_test(&ac, comp, sev);
350 	close(fd);
351 }
352 
353 ATF_TC_WITHOUT_HEAD(file_poll);
354 ATF_TC_BODY(file_poll, tc)
355 {
356 	aio_file_test(poll, NULL);
357 }
358 
359 ATF_TC_WITHOUT_HEAD(file_signal);
360 ATF_TC_BODY(file_signal, tc)
361 {
362 	aio_file_test(poll_signaled, setup_signal());
363 }
364 
365 ATF_TC_WITHOUT_HEAD(file_suspend);
366 ATF_TC_BODY(file_suspend, tc)
367 {
368 	aio_file_test(suspend, NULL);
369 }
370 
371 ATF_TC_WITHOUT_HEAD(file_thread);
372 ATF_TC_BODY(file_thread, tc)
373 {
374 	aio_file_test(poll_signaled, setup_thread());
375 }
376 
377 ATF_TC_WITHOUT_HEAD(file_waitcomplete);
378 ATF_TC_BODY(file_waitcomplete, tc)
379 {
380 	aio_file_test(waitcomplete, NULL);
381 }
382 
383 #define	FIFO_LEN	256
384 #define	FIFO_PATHNAME	"testfifo"
385 
386 static void
387 aio_fifo_test(completion comp, struct sigevent *sev)
388 {
389 	int error, read_fd = -1, write_fd = -1;
390 	struct aio_context ac;
391 
392 	ATF_REQUIRE_KERNEL_MODULE("aio");
393 	ATF_REQUIRE_UNSAFE_AIO();
394 
395 	ATF_REQUIRE_MSG(mkfifo(FIFO_PATHNAME, 0600) != -1,
396 	    "mkfifo failed: %s", strerror(errno));
397 
398 	read_fd = open(FIFO_PATHNAME, O_RDONLY | O_NONBLOCK);
399 	if (read_fd == -1) {
400 		error = errno;
401 		errno = error;
402 		atf_tc_fail("read_fd open failed: %s",
403 		    strerror(errno));
404 	}
405 
406 	write_fd = open(FIFO_PATHNAME, O_WRONLY);
407 	if (write_fd == -1) {
408 		error = errno;
409 		errno = error;
410 		atf_tc_fail("write_fd open failed: %s",
411 		    strerror(errno));
412 	}
413 
414 	aio_context_init(&ac, read_fd, write_fd, FIFO_LEN);
415 	aio_write_test(&ac, comp, sev);
416 	aio_read_test(&ac, comp, sev);
417 
418 	close(read_fd);
419 	close(write_fd);
420 }
421 
422 ATF_TC_WITHOUT_HEAD(fifo_poll);
423 ATF_TC_BODY(fifo_poll, tc)
424 {
425 	aio_fifo_test(poll, NULL);
426 }
427 
428 ATF_TC_WITHOUT_HEAD(fifo_signal);
429 ATF_TC_BODY(fifo_signal, tc)
430 {
431 	aio_fifo_test(poll_signaled, setup_signal());
432 }
433 
434 ATF_TC_WITHOUT_HEAD(fifo_suspend);
435 ATF_TC_BODY(fifo_suspend, tc)
436 {
437 	aio_fifo_test(suspend, NULL);
438 }
439 
440 ATF_TC_WITHOUT_HEAD(fifo_thread);
441 ATF_TC_BODY(fifo_thread, tc)
442 {
443 	aio_fifo_test(poll_signaled, setup_thread());
444 }
445 
446 ATF_TC_WITHOUT_HEAD(fifo_waitcomplete);
447 ATF_TC_BODY(fifo_waitcomplete, tc)
448 {
449 	aio_fifo_test(waitcomplete, NULL);
450 }
451 
452 #define	UNIX_SOCKETPAIR_LEN	256
453 static void
454 aio_unix_socketpair_test(completion comp, struct sigevent *sev)
455 {
456 	struct aio_context ac;
457 	struct rusage ru_before, ru_after;
458 	int sockets[2];
459 
460 	ATF_REQUIRE_KERNEL_MODULE("aio");
461 
462 	ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != -1,
463 	    "socketpair failed: %s", strerror(errno));
464 
465 	aio_context_init(&ac, sockets[0], sockets[1], UNIX_SOCKETPAIR_LEN);
466 	ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_before) != -1,
467 	    "getrusage failed: %s", strerror(errno));
468 	aio_write_test(&ac, comp, sev);
469 	ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_after) != -1,
470 	    "getrusage failed: %s", strerror(errno));
471 	ATF_REQUIRE(ru_after.ru_msgsnd == ru_before.ru_msgsnd + 1);
472 	ru_before = ru_after;
473 	aio_read_test(&ac, comp, sev);
474 	ATF_REQUIRE_MSG(getrusage(RUSAGE_SELF, &ru_after) != -1,
475 	    "getrusage failed: %s", strerror(errno));
476 	ATF_REQUIRE(ru_after.ru_msgrcv == ru_before.ru_msgrcv + 1);
477 
478 	close(sockets[0]);
479 	close(sockets[1]);
480 }
481 
482 ATF_TC_WITHOUT_HEAD(socket_poll);
483 ATF_TC_BODY(socket_poll, tc)
484 {
485 	aio_unix_socketpair_test(poll, NULL);
486 }
487 
488 ATF_TC_WITHOUT_HEAD(socket_signal);
489 ATF_TC_BODY(socket_signal, tc)
490 {
491 	aio_unix_socketpair_test(poll_signaled, setup_signal());
492 }
493 
494 ATF_TC_WITHOUT_HEAD(socket_suspend);
495 ATF_TC_BODY(socket_suspend, tc)
496 {
497 	aio_unix_socketpair_test(suspend, NULL);
498 }
499 
500 ATF_TC_WITHOUT_HEAD(socket_thread);
501 ATF_TC_BODY(socket_thread, tc)
502 {
503 	aio_unix_socketpair_test(poll_signaled, setup_thread());
504 }
505 
506 ATF_TC_WITHOUT_HEAD(socket_waitcomplete);
507 ATF_TC_BODY(socket_waitcomplete, tc)
508 {
509 	aio_unix_socketpair_test(waitcomplete, NULL);
510 }
511 
512 struct aio_pty_arg {
513 	int	apa_read_fd;
514 	int	apa_write_fd;
515 };
516 
517 #define	PTY_LEN		256
518 static void
519 aio_pty_test(completion comp, struct sigevent *sev)
520 {
521 	struct aio_context ac;
522 	int read_fd, write_fd;
523 	struct termios ts;
524 	int error;
525 
526 	ATF_REQUIRE_KERNEL_MODULE("aio");
527 	ATF_REQUIRE_UNSAFE_AIO();
528 
529 	ATF_REQUIRE_MSG(openpty(&read_fd, &write_fd, NULL, NULL, NULL) == 0,
530 	    "openpty failed: %s", strerror(errno));
531 
532 
533 	if (tcgetattr(write_fd, &ts) < 0) {
534 		error = errno;
535 		errno = error;
536 		atf_tc_fail("tcgetattr failed: %s", strerror(errno));
537 	}
538 	cfmakeraw(&ts);
539 	if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
540 		error = errno;
541 		errno = error;
542 		atf_tc_fail("tcsetattr failed: %s", strerror(errno));
543 	}
544 	aio_context_init(&ac, read_fd, write_fd, PTY_LEN);
545 
546 	aio_write_test(&ac, comp, sev);
547 	aio_read_test(&ac, comp, sev);
548 
549 	close(read_fd);
550 	close(write_fd);
551 }
552 
553 ATF_TC_WITHOUT_HEAD(pty_poll);
554 ATF_TC_BODY(pty_poll, tc)
555 {
556 	aio_pty_test(poll, NULL);
557 }
558 
559 ATF_TC_WITHOUT_HEAD(pty_signal);
560 ATF_TC_BODY(pty_signal, tc)
561 {
562 	aio_pty_test(poll_signaled, setup_signal());
563 }
564 
565 ATF_TC_WITHOUT_HEAD(pty_suspend);
566 ATF_TC_BODY(pty_suspend, tc)
567 {
568 	aio_pty_test(suspend, NULL);
569 }
570 
571 ATF_TC_WITHOUT_HEAD(pty_thread);
572 ATF_TC_BODY(pty_thread, tc)
573 {
574 	aio_pty_test(poll_signaled, setup_thread());
575 }
576 
577 ATF_TC_WITHOUT_HEAD(pty_waitcomplete);
578 ATF_TC_BODY(pty_waitcomplete, tc)
579 {
580 	aio_pty_test(waitcomplete, NULL);
581 }
582 
583 #define	PIPE_LEN	256
584 static void
585 aio_pipe_test(completion comp, struct sigevent *sev)
586 {
587 	struct aio_context ac;
588 	int pipes[2];
589 
590 	ATF_REQUIRE_KERNEL_MODULE("aio");
591 	ATF_REQUIRE_UNSAFE_AIO();
592 
593 	ATF_REQUIRE_MSG(pipe(pipes) != -1,
594 	    "pipe failed: %s", strerror(errno));
595 
596 	aio_context_init(&ac, pipes[0], pipes[1], PIPE_LEN);
597 	aio_write_test(&ac, comp, sev);
598 	aio_read_test(&ac, comp, sev);
599 
600 	close(pipes[0]);
601 	close(pipes[1]);
602 }
603 
604 ATF_TC_WITHOUT_HEAD(pipe_poll);
605 ATF_TC_BODY(pipe_poll, tc)
606 {
607 	aio_pipe_test(poll, NULL);
608 }
609 
610 ATF_TC_WITHOUT_HEAD(pipe_signal);
611 ATF_TC_BODY(pipe_signal, tc)
612 {
613 	aio_pipe_test(poll_signaled, setup_signal());
614 }
615 
616 ATF_TC_WITHOUT_HEAD(pipe_suspend);
617 ATF_TC_BODY(pipe_suspend, tc)
618 {
619 	aio_pipe_test(suspend, NULL);
620 }
621 
622 ATF_TC_WITHOUT_HEAD(pipe_thread);
623 ATF_TC_BODY(pipe_thread, tc)
624 {
625 	aio_pipe_test(poll_signaled, setup_thread());
626 }
627 
628 ATF_TC_WITHOUT_HEAD(pipe_waitcomplete);
629 ATF_TC_BODY(pipe_waitcomplete, tc)
630 {
631 	aio_pipe_test(waitcomplete, NULL);
632 }
633 
634 #define	MD_LEN		GLOBAL_MAX
635 #define	MDUNIT_LINK	"mdunit_link"
636 
637 static void
638 aio_md_cleanup(void)
639 {
640 	struct md_ioctl mdio;
641 	int mdctl_fd, error, n, unit;
642 	char buf[80];
643 
644 	mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
645 	ATF_REQUIRE(mdctl_fd >= 0);
646 	n = readlink(MDUNIT_LINK, buf, sizeof(buf));
647 	if (n > 0) {
648 		if (sscanf(buf, "%d", &unit) == 1 && unit >= 0) {
649 			bzero(&mdio, sizeof(mdio));
650 			mdio.md_version = MDIOVERSION;
651 			mdio.md_unit = unit;
652 			if (ioctl(mdctl_fd, MDIOCDETACH, &mdio) == -1) {
653 				error = errno;
654 				close(mdctl_fd);
655 				errno = error;
656 				atf_tc_fail("ioctl MDIOCDETACH failed: %s",
657 				    strerror(errno));
658 			}
659 		}
660 	}
661 
662 	close(mdctl_fd);
663 }
664 
665 static void
666 aio_md_test(completion comp, struct sigevent *sev)
667 {
668 	int error, fd, mdctl_fd, unit;
669 	char pathname[PATH_MAX];
670 	struct aio_context ac;
671 	struct md_ioctl mdio;
672 	char buf[80];
673 
674 	ATF_REQUIRE_KERNEL_MODULE("aio");
675 	ATF_REQUIRE_UNSAFE_AIO();
676 
677 	mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
678 	ATF_REQUIRE_MSG(mdctl_fd != -1,
679 	    "opening /dev/%s failed: %s", MDCTL_NAME, strerror(errno));
680 
681 	bzero(&mdio, sizeof(mdio));
682 	mdio.md_version = MDIOVERSION;
683 	mdio.md_type = MD_MALLOC;
684 	mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
685 	mdio.md_mediasize = GLOBAL_MAX;
686 	mdio.md_sectorsize = 512;
687 
688 	if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
689 		error = errno;
690 		errno = error;
691 		atf_tc_fail("ioctl MDIOCATTACH failed: %s", strerror(errno));
692 	}
693 	close(mdctl_fd);
694 
695 	/* Store the md unit number in a symlink for future cleanup */
696 	unit = mdio.md_unit;
697 	snprintf(buf, sizeof(buf), "%d", unit);
698 	ATF_REQUIRE_EQ(0, symlink(buf, MDUNIT_LINK));
699 	snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
700 	fd = open(pathname, O_RDWR);
701 	ATF_REQUIRE_MSG(fd != -1,
702 	    "opening %s failed: %s", pathname, strerror(errno));
703 
704 	aio_context_init(&ac, fd, fd, MD_LEN);
705 	aio_write_test(&ac, comp, sev);
706 	aio_read_test(&ac, comp, sev);
707 
708 	close(fd);
709 }
710 
711 ATF_TC_WITH_CLEANUP(md_poll);
712 ATF_TC_HEAD(md_poll, tc)
713 {
714 
715 	atf_tc_set_md_var(tc, "require.user", "root");
716 }
717 ATF_TC_BODY(md_poll, tc)
718 {
719 	aio_md_test(poll, NULL);
720 }
721 ATF_TC_CLEANUP(md_poll, tc)
722 {
723 	aio_md_cleanup();
724 }
725 
726 ATF_TC_WITH_CLEANUP(md_signal);
727 ATF_TC_HEAD(md_signal, tc)
728 {
729 
730 	atf_tc_set_md_var(tc, "require.user", "root");
731 }
732 ATF_TC_BODY(md_signal, tc)
733 {
734 	aio_md_test(poll_signaled, setup_signal());
735 }
736 ATF_TC_CLEANUP(md_signal, tc)
737 {
738 	aio_md_cleanup();
739 }
740 
741 ATF_TC_WITH_CLEANUP(md_suspend);
742 ATF_TC_HEAD(md_suspend, tc)
743 {
744 
745 	atf_tc_set_md_var(tc, "require.user", "root");
746 }
747 ATF_TC_BODY(md_suspend, tc)
748 {
749 	aio_md_test(suspend, NULL);
750 }
751 ATF_TC_CLEANUP(md_suspend, tc)
752 {
753 	aio_md_cleanup();
754 }
755 
756 ATF_TC_WITH_CLEANUP(md_thread);
757 ATF_TC_HEAD(md_thread, tc)
758 {
759 
760 	atf_tc_set_md_var(tc, "require.user", "root");
761 }
762 ATF_TC_BODY(md_thread, tc)
763 {
764 	aio_md_test(poll_signaled, setup_thread());
765 }
766 ATF_TC_CLEANUP(md_thread, tc)
767 {
768 	aio_md_cleanup();
769 }
770 
771 ATF_TC_WITH_CLEANUP(md_waitcomplete);
772 ATF_TC_HEAD(md_waitcomplete, tc)
773 {
774 
775 	atf_tc_set_md_var(tc, "require.user", "root");
776 }
777 ATF_TC_BODY(md_waitcomplete, tc)
778 {
779 	aio_md_test(waitcomplete, NULL);
780 }
781 ATF_TC_CLEANUP(md_waitcomplete, tc)
782 {
783 	aio_md_cleanup();
784 }
785 
786 ATF_TC_WITHOUT_HEAD(aio_large_read_test);
787 ATF_TC_BODY(aio_large_read_test, tc)
788 {
789 	struct aiocb cb, *cbp;
790 	ssize_t nread;
791 	size_t len;
792 	int fd;
793 #ifdef __LP64__
794 	int clamped;
795 #endif
796 
797 	ATF_REQUIRE_KERNEL_MODULE("aio");
798 	ATF_REQUIRE_UNSAFE_AIO();
799 
800 #ifdef __LP64__
801 	len = sizeof(clamped);
802 	if (sysctlbyname("debug.iosize_max_clamp", &clamped, &len, NULL, 0) ==
803 	    -1)
804 		atf_libc_error(errno, "Failed to read debug.iosize_max_clamp");
805 #endif
806 
807 	/* Determine the maximum supported read(2) size. */
808 	len = SSIZE_MAX;
809 #ifdef __LP64__
810 	if (clamped)
811 		len = INT_MAX;
812 #endif
813 
814 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
815 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
816 
817 	unlink(FILE_PATHNAME);
818 
819 	memset(&cb, 0, sizeof(cb));
820 	cb.aio_nbytes = len;
821 	cb.aio_fildes = fd;
822 	cb.aio_buf = NULL;
823 	if (aio_read(&cb) == -1)
824 		atf_tc_fail("aio_read() of maximum read size failed: %s",
825 		    strerror(errno));
826 
827 	nread = aio_waitcomplete(&cbp, NULL);
828 	if (nread == -1)
829 		atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
830 	if (nread != 0)
831 		atf_tc_fail("aio_read() from empty file returned data: %zd",
832 		    nread);
833 
834 	memset(&cb, 0, sizeof(cb));
835 	cb.aio_nbytes = len + 1;
836 	cb.aio_fildes = fd;
837 	cb.aio_buf = NULL;
838 	if (aio_read(&cb) == -1) {
839 		if (errno == EINVAL)
840 			goto finished;
841 		atf_tc_fail("aio_read() of too large read size failed: %s",
842 		    strerror(errno));
843 	}
844 
845 	nread = aio_waitcomplete(&cbp, NULL);
846 	if (nread == -1) {
847 		if (errno == EINVAL)
848 			goto finished;
849 		atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
850 	}
851 	atf_tc_fail("aio_read() of too large read size returned: %zd", nread);
852 
853 finished:
854 	close(fd);
855 }
856 
857 /*
858  * This tests for a bug where arriving socket data can wakeup multiple
859  * AIO read requests resulting in an uncancellable request.
860  */
861 ATF_TC_WITHOUT_HEAD(aio_socket_two_reads);
862 ATF_TC_BODY(aio_socket_two_reads, tc)
863 {
864 	struct ioreq {
865 		struct aiocb iocb;
866 		char buffer[1024];
867 	} ioreq[2];
868 	struct aiocb *iocb;
869 	unsigned i;
870 	int s[2];
871 	char c;
872 
873 	ATF_REQUIRE_KERNEL_MODULE("aio");
874 #if __FreeBSD_version < 1100101
875 	aft_tc_skip("kernel version %d is too old (%d required)",
876 	    __FreeBSD_version, 1100101);
877 #endif
878 
879 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
880 
881 	/* Queue two read requests. */
882 	memset(&ioreq, 0, sizeof(ioreq));
883 	for (i = 0; i < nitems(ioreq); i++) {
884 		ioreq[i].iocb.aio_nbytes = sizeof(ioreq[i].buffer);
885 		ioreq[i].iocb.aio_fildes = s[0];
886 		ioreq[i].iocb.aio_buf = ioreq[i].buffer;
887 		ATF_REQUIRE(aio_read(&ioreq[i].iocb) == 0);
888 	}
889 
890 	/* Send a single byte.  This should complete one request. */
891 	c = 0xc3;
892 	ATF_REQUIRE(write(s[1], &c, sizeof(c)) == 1);
893 
894 	ATF_REQUIRE(aio_waitcomplete(&iocb, NULL) == 1);
895 
896 	/* Determine which request completed and verify the data was read. */
897 	if (iocb == &ioreq[0].iocb)
898 		i = 0;
899 	else
900 		i = 1;
901 	ATF_REQUIRE(ioreq[i].buffer[0] == c);
902 
903 	i ^= 1;
904 
905 	/*
906 	 * Try to cancel the other request.  On broken systems this
907 	 * will fail and the process will hang on exit.
908 	 */
909 	ATF_REQUIRE(aio_error(&ioreq[i].iocb) == EINPROGRESS);
910 	ATF_REQUIRE(aio_cancel(s[0], &ioreq[i].iocb) == AIO_CANCELED);
911 
912 	close(s[1]);
913 	close(s[0]);
914 }
915 
916 /*
917  * This test ensures that aio_write() on a blocking socket of a "large"
918  * buffer does not return a short completion.
919  */
920 ATF_TC_WITHOUT_HEAD(aio_socket_blocking_short_write);
921 ATF_TC_BODY(aio_socket_blocking_short_write, tc)
922 {
923 	struct aiocb iocb, *iocbp;
924 	char *buffer[2];
925 	ssize_t done;
926 	int buffer_size, sb_size;
927 	socklen_t len;
928 	int s[2];
929 
930 	ATF_REQUIRE_KERNEL_MODULE("aio");
931 
932 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
933 
934 	len = sizeof(sb_size);
935 	ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
936 	    -1);
937 	ATF_REQUIRE(len == sizeof(sb_size));
938 	buffer_size = sb_size;
939 
940 	ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
941 	    -1);
942 	ATF_REQUIRE(len == sizeof(sb_size));
943 	if (sb_size > buffer_size)
944 		buffer_size = sb_size;
945 
946 	/*
947 	 * Use twice the size of the MAX(receive buffer, send buffer)
948 	 * to ensure that the write is split up into multiple writes
949 	 * internally.
950 	 */
951 	buffer_size *= 2;
952 
953 	buffer[0] = malloc(buffer_size);
954 	ATF_REQUIRE(buffer[0] != NULL);
955 	buffer[1] = malloc(buffer_size);
956 	ATF_REQUIRE(buffer[1] != NULL);
957 
958 	srandomdev();
959 	aio_fill_buffer(buffer[1], buffer_size, random());
960 
961 	memset(&iocb, 0, sizeof(iocb));
962 	iocb.aio_fildes = s[1];
963 	iocb.aio_buf = buffer[1];
964 	iocb.aio_nbytes = buffer_size;
965 	ATF_REQUIRE(aio_write(&iocb) == 0);
966 
967 	done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
968 	ATF_REQUIRE(done == buffer_size);
969 
970 	done = aio_waitcomplete(&iocbp, NULL);
971 	ATF_REQUIRE(iocbp == &iocb);
972 	ATF_REQUIRE(done == buffer_size);
973 
974 	ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
975 
976 	close(s[1]);
977 	close(s[0]);
978 }
979 
980 /*
981  * This test verifies that cancelling a partially completed socket write
982  * returns a short write rather than ECANCELED.
983  */
984 ATF_TC_WITHOUT_HEAD(aio_socket_short_write_cancel);
985 ATF_TC_BODY(aio_socket_short_write_cancel, tc)
986 {
987 	struct aiocb iocb, *iocbp;
988 	char *buffer[2];
989 	ssize_t done;
990 	int buffer_size, sb_size;
991 	socklen_t len;
992 	int s[2];
993 
994 	ATF_REQUIRE_KERNEL_MODULE("aio");
995 
996 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
997 
998 	len = sizeof(sb_size);
999 	ATF_REQUIRE(getsockopt(s[0], SOL_SOCKET, SO_RCVBUF, &sb_size, &len) !=
1000 	    -1);
1001 	ATF_REQUIRE(len == sizeof(sb_size));
1002 	buffer_size = sb_size;
1003 
1004 	ATF_REQUIRE(getsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &sb_size, &len) !=
1005 	    -1);
1006 	ATF_REQUIRE(len == sizeof(sb_size));
1007 	if (sb_size > buffer_size)
1008 		buffer_size = sb_size;
1009 
1010 	/*
1011 	 * Use three times the size of the MAX(receive buffer, send
1012 	 * buffer) for the write to ensure that the write is split up
1013 	 * into multiple writes internally.  The recv() ensures that
1014 	 * the write has partially completed, but a remaining size of
1015 	 * two buffers should ensure that the write has not completed
1016 	 * fully when it is cancelled.
1017 	 */
1018 	buffer[0] = malloc(buffer_size);
1019 	ATF_REQUIRE(buffer[0] != NULL);
1020 	buffer[1] = malloc(buffer_size * 3);
1021 	ATF_REQUIRE(buffer[1] != NULL);
1022 
1023 	srandomdev();
1024 	aio_fill_buffer(buffer[1], buffer_size * 3, random());
1025 
1026 	memset(&iocb, 0, sizeof(iocb));
1027 	iocb.aio_fildes = s[1];
1028 	iocb.aio_buf = buffer[1];
1029 	iocb.aio_nbytes = buffer_size * 3;
1030 	ATF_REQUIRE(aio_write(&iocb) == 0);
1031 
1032 	done = recv(s[0], buffer[0], buffer_size, MSG_WAITALL);
1033 	ATF_REQUIRE(done == buffer_size);
1034 
1035 	ATF_REQUIRE(aio_error(&iocb) == EINPROGRESS);
1036 	ATF_REQUIRE(aio_cancel(s[1], &iocb) == AIO_NOTCANCELED);
1037 
1038 	done = aio_waitcomplete(&iocbp, NULL);
1039 	ATF_REQUIRE(iocbp == &iocb);
1040 	ATF_REQUIRE(done >= buffer_size && done <= buffer_size * 2);
1041 
1042 	ATF_REQUIRE(memcmp(buffer[0], buffer[1], buffer_size) == 0);
1043 
1044 	close(s[1]);
1045 	close(s[0]);
1046 }
1047 
1048 /*
1049  * This test just performs a basic test of aio_fsync().
1050  */
1051 ATF_TC_WITHOUT_HEAD(aio_fsync_test);
1052 ATF_TC_BODY(aio_fsync_test, tc)
1053 {
1054 	struct aiocb synccb, *iocbp;
1055 	struct {
1056 		struct aiocb iocb;
1057 		bool done;
1058 		char *buffer;
1059 	} buffers[16];
1060 	struct stat sb;
1061 	ssize_t rval;
1062 	unsigned i;
1063 	int fd;
1064 
1065 	ATF_REQUIRE_KERNEL_MODULE("aio");
1066 	ATF_REQUIRE_UNSAFE_AIO();
1067 
1068 	fd = open(FILE_PATHNAME, O_RDWR | O_CREAT, 0600);
1069 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
1070 	unlink(FILE_PATHNAME);
1071 
1072 	ATF_REQUIRE(fstat(fd, &sb) == 0);
1073 	ATF_REQUIRE(sb.st_blksize != 0);
1074 	ATF_REQUIRE(ftruncate(fd, sb.st_blksize * nitems(buffers)) == 0);
1075 
1076 	/*
1077 	 * Queue several asynchronous write requests.  Hopefully this
1078 	 * forces the aio_fsync() request to be deferred.  There is no
1079 	 * reliable way to guarantee that however.
1080 	 */
1081 	srandomdev();
1082 	for (i = 0; i < nitems(buffers); i++) {
1083 		buffers[i].done = false;
1084 		memset(&buffers[i].iocb, 0, sizeof(buffers[i].iocb));
1085 		buffers[i].buffer = malloc(sb.st_blksize);
1086 		aio_fill_buffer(buffers[i].buffer, sb.st_blksize, random());
1087 		buffers[i].iocb.aio_fildes = fd;
1088 		buffers[i].iocb.aio_buf = buffers[i].buffer;
1089 		buffers[i].iocb.aio_nbytes = sb.st_blksize;
1090 		buffers[i].iocb.aio_offset = sb.st_blksize * i;
1091 		ATF_REQUIRE(aio_write(&buffers[i].iocb) == 0);
1092 	}
1093 
1094 	/* Queue the aio_fsync request. */
1095 	memset(&synccb, 0, sizeof(synccb));
1096 	synccb.aio_fildes = fd;
1097 	ATF_REQUIRE(aio_fsync(O_SYNC, &synccb) == 0);
1098 
1099 	/* Wait for requests to complete. */
1100 	for (;;) {
1101 	next:
1102 		rval = aio_waitcomplete(&iocbp, NULL);
1103 		ATF_REQUIRE(iocbp != NULL);
1104 		if (iocbp == &synccb) {
1105 			ATF_REQUIRE(rval == 0);
1106 			break;
1107 		}
1108 
1109 		for (i = 0; i < nitems(buffers); i++) {
1110 			if (iocbp == &buffers[i].iocb) {
1111 				ATF_REQUIRE(buffers[i].done == false);
1112 				ATF_REQUIRE(rval == sb.st_blksize);
1113 				buffers[i].done = true;
1114 				goto next;
1115 			}
1116 		}
1117 
1118 		ATF_REQUIRE_MSG(false, "unmatched AIO request");
1119 	}
1120 
1121 	for (i = 0; i < nitems(buffers); i++)
1122 		ATF_REQUIRE_MSG(buffers[i].done,
1123 		    "AIO request %u did not complete", i);
1124 
1125 	close(fd);
1126 }
1127 
1128 ATF_TP_ADD_TCS(tp)
1129 {
1130 
1131 	ATF_TP_ADD_TC(tp, file_poll);
1132 	ATF_TP_ADD_TC(tp, file_signal);
1133 	ATF_TP_ADD_TC(tp, file_suspend);
1134 	ATF_TP_ADD_TC(tp, file_thread);
1135 	ATF_TP_ADD_TC(tp, file_waitcomplete);
1136 	ATF_TP_ADD_TC(tp, fifo_poll);
1137 	ATF_TP_ADD_TC(tp, fifo_signal);
1138 	ATF_TP_ADD_TC(tp, fifo_suspend);
1139 	ATF_TP_ADD_TC(tp, fifo_thread);
1140 	ATF_TP_ADD_TC(tp, fifo_waitcomplete);
1141 	ATF_TP_ADD_TC(tp, socket_poll);
1142 	ATF_TP_ADD_TC(tp, socket_signal);
1143 	ATF_TP_ADD_TC(tp, socket_suspend);
1144 	ATF_TP_ADD_TC(tp, socket_thread);
1145 	ATF_TP_ADD_TC(tp, socket_waitcomplete);
1146 	ATF_TP_ADD_TC(tp, pty_poll);
1147 	ATF_TP_ADD_TC(tp, pty_signal);
1148 	ATF_TP_ADD_TC(tp, pty_suspend);
1149 	ATF_TP_ADD_TC(tp, pty_thread);
1150 	ATF_TP_ADD_TC(tp, pty_waitcomplete);
1151 	ATF_TP_ADD_TC(tp, pipe_poll);
1152 	ATF_TP_ADD_TC(tp, pipe_signal);
1153 	ATF_TP_ADD_TC(tp, pipe_suspend);
1154 	ATF_TP_ADD_TC(tp, pipe_thread);
1155 	ATF_TP_ADD_TC(tp, pipe_waitcomplete);
1156 	ATF_TP_ADD_TC(tp, md_poll);
1157 	ATF_TP_ADD_TC(tp, md_signal);
1158 	ATF_TP_ADD_TC(tp, md_suspend);
1159 	ATF_TP_ADD_TC(tp, md_thread);
1160 	ATF_TP_ADD_TC(tp, md_waitcomplete);
1161 	ATF_TP_ADD_TC(tp, aio_fsync_test);
1162 	ATF_TP_ADD_TC(tp, aio_large_read_test);
1163 	ATF_TP_ADD_TC(tp, aio_socket_two_reads);
1164 	ATF_TP_ADD_TC(tp, aio_socket_blocking_short_write);
1165 	ATF_TP_ADD_TC(tp, aio_socket_short_write_cancel);
1166 
1167 	return (atf_no_error());
1168 }
1169