1/*-
2 * Copyright (C) 2005 IronPort Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28/*
29 * Note: it is a good idea to run this against a physical drive to
30 * exercise the physio fast path (ie. lio_kqueue_test /dev/<something safe>)
31 */
32
33#include <sys/types.h>
34#include <sys/event.h>
35#include <sys/time.h>
36#include <aio.h>
37#include <fcntl.h>
38#include <err.h>
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include "freebsd_test_suite/macros.h"
46#include "local.h"
47
48#define PATH_TEMPLATE   "aio.XXXXXXXXXX"
49
50#define LIO_MAX 5
51#define MAX_IOCBS_PER_LIO	64
52#define MAX_IOCBS (LIO_MAX * MAX_IOCBS_PER_LIO)
53#define MAX_RUNS 300
54
55int
56main(int argc, char *argv[])
57{
58	int fd;
59	struct aiocb *iocb[MAX_IOCBS];
60	struct aiocb **lio[LIO_MAX], **kq_lio;
61	int i, result, run, error, j, k, max_queue_per_proc;
62	int max_iocbs, iocbs_per_lio;
63	size_t max_queue_per_proc_size;
64	char buffer[32768];
65	int kq;
66	struct kevent ke, kq_returned;
67	struct timespec ts;
68	struct sigevent sig;
69	time_t time1, time2;
70	char *file, pathname[sizeof(PATH_TEMPLATE)];
71	int tmp_file = 0, failed = 0;
72
73	PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
74	PLAIN_REQUIRE_UNSAFE_AIO(0);
75
76	max_queue_per_proc_size = sizeof(max_queue_per_proc);
77	if (sysctlbyname("vfs.aio.max_aio_queue_per_proc",
78	    &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0) != 0)
79		err(1, "sysctlbyname");
80	iocbs_per_lio = max_queue_per_proc / LIO_MAX;
81	max_iocbs = LIO_MAX * iocbs_per_lio;
82
83	kq = kqueue();
84	if (kq < 0)
85		err(1, "kqeueue(2) failed");
86
87	if (argc == 1) {
88		strcpy(pathname, PATH_TEMPLATE);
89		fd = mkstemp(pathname);
90		file = pathname;
91		tmp_file = 1;
92	} else {
93		file = argv[1];
94		fd = open(file, O_RDWR|O_CREAT, 0666);
95        }
96	if (fd < 0)
97		err(1, "can't open %s", argv[1]);
98
99#ifdef DEBUG
100	printf("Hello kq %d fd %d\n", kq, fd);
101#endif
102
103	for (run = 0; run < MAX_RUNS; run++) {
104#ifdef DEBUG
105		printf("Run %d\n", run);
106#endif
107		for (j = 0; j < LIO_MAX; j++) {
108			lio[j] =
109			    malloc(sizeof(struct aiocb *) * iocbs_per_lio);
110			for (i = 0; i < iocbs_per_lio; i++) {
111				k = (iocbs_per_lio * j) + i;
112				lio[j][i] = iocb[k] =
113				    calloc(1, sizeof(struct aiocb));
114				iocb[k]->aio_nbytes = sizeof(buffer);
115				iocb[k]->aio_buf = buffer;
116				iocb[k]->aio_fildes = fd;
117				iocb[k]->aio_offset
118				    = iocb[k]->aio_nbytes * k * (run + 1);
119
120#ifdef DEBUG
121				printf("hello iocb[k] %jd\n",
122				       (intmax_t)iocb[k]->aio_offset);
123#endif
124				iocb[k]->aio_lio_opcode = LIO_WRITE;
125			}
126			sig.sigev_notify_kqueue = kq;
127			sig.sigev_value.sival_ptr = lio[j];
128			sig.sigev_notify = SIGEV_KEVENT;
129			time(&time1);
130			result = lio_listio(LIO_NOWAIT, lio[j],
131					    iocbs_per_lio, &sig);
132			error = errno;
133			time(&time2);
134#ifdef DEBUG
135			printf("Time %jd %jd %jd result -> %d\n",
136			    (intmax_t)time1, (intmax_t)time2,
137			    (intmax_t)time2-time1, result);
138#endif
139			if (result != 0) {
140			        errno = error;
141				err(1, "FAIL: Result %d iteration %d\n",
142				    result, j);
143			}
144#ifdef DEBUG
145			printf("write %d is at %p\n", j, lio[j]);
146#endif
147		}
148
149		for (i = 0; i < LIO_MAX; i++) {
150			for (j = LIO_MAX - 1; j >=0; j--) {
151				if (lio[j])
152					break;
153			}
154
155			for (;;) {
156				bzero(&ke, sizeof(ke));
157				bzero(&kq_returned, sizeof(ke));
158				ts.tv_sec = 0;
159				ts.tv_nsec = 1;
160#ifdef DEBUG
161				printf("FOO lio %d -> %p\n", j, lio[j]);
162#endif
163				EV_SET(&ke, (uintptr_t)lio[j],
164				       EVFILT_LIO, EV_ONESHOT, 0, 0, iocb[j]);
165				result = kevent(kq, NULL, 0,
166						&kq_returned, 1, &ts);
167				error = errno;
168				if (result < 0) {
169					perror("kevent error: ");
170				}
171				kq_lio = kq_returned.udata;
172#ifdef DEBUG
173				printf("kevent %d %d errno %d return.ident %p "
174				       "return.data %p return.udata %p %p\n",
175				       i, result, error,
176				       (void*)kq_returned.ident,
177				       (void*)kq_returned.data,
178				       kq_returned.udata,
179				       lio[j]);
180#endif
181
182				if (kq_lio)
183					break;
184#ifdef DEBUG
185				printf("Try again\n");
186#endif
187			}
188
189#ifdef DEBUG
190			printf("lio %p\n", lio);
191#endif
192
193			for (j = 0; j < LIO_MAX; j++) {
194				if (lio[j] == kq_lio)
195					break;
196			}
197			if (j == LIO_MAX)
198				errx(1, "FAIL: ");
199
200#ifdef DEBUG
201			printf("Error Result for %d is %d\n", j, result);
202#endif
203			if (result < 0) {
204				printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
205				failed++;
206			} else
207				printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
208			for (k = 0; k < max_iocbs / LIO_MAX; k++) {
209				result = aio_return(kq_lio[k]);
210#ifdef DEBUG
211				printf("Return Result for %d %d is %d\n", j, k, result);
212#endif
213				if (result != sizeof(buffer)) {
214					printf("FAIL: run %d, operation %d sub-opt %d  result %d (errno=%d) should be %zu\n",
215					   run, LIO_MAX - i -1, k, result, errno, sizeof(buffer));
216				} else {
217					printf("PASS: run %d, operation %d sub-opt %d  result %d\n",
218					   run, LIO_MAX - i -1, k, result);
219				}
220			}
221#ifdef DEBUG
222			printf("\n");
223#endif
224
225			for (k = 0; k < max_iocbs / LIO_MAX; k++)
226				free(lio[j][k]);
227			free(lio[j]);
228			lio[j] = NULL;
229		}
230	}
231#ifdef DEBUG
232	printf("Done\n");
233#endif
234
235	if (tmp_file)
236		unlink(pathname);
237
238	if (failed)
239		errx(1, "FAIL: %d testcases failed", failed);
240	else
241		errx(0, "PASS: All\n");
242
243}
244