sys_aio.c revision 1.6.2.2 1 1.6.2.2 mjf /* $NetBSD: sys_aio.c,v 1.6.2.2 2007/07/11 20:10:08 mjf Exp $ */
2 1.6.2.2 mjf
3 1.6.2.2 mjf /*
4 1.6.2.2 mjf * Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
5 1.6.2.2 mjf *
6 1.6.2.2 mjf * Redistribution and use in source and binary forms, with or without
7 1.6.2.2 mjf * modification, are permitted provided that the following conditions
8 1.6.2.2 mjf * are met:
9 1.6.2.2 mjf * 1. Redistributions of source code must retain the above copyright
10 1.6.2.2 mjf * notice, this list of conditions and the following disclaimer.
11 1.6.2.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
12 1.6.2.2 mjf * notice, this list of conditions and the following disclaimer in the
13 1.6.2.2 mjf * documentation and/or other materials provided with the distribution.
14 1.6.2.2 mjf *
15 1.6.2.2 mjf * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 1.6.2.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.6.2.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.6.2.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.6.2.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.6.2.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.6.2.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.6.2.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.6.2.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.6.2.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.6.2.2 mjf * POSSIBILITY OF SUCH DAMAGE.
26 1.6.2.2 mjf */
27 1.6.2.2 mjf
28 1.6.2.2 mjf /*
29 1.6.2.2 mjf * TODO:
30 1.6.2.2 mjf * 1. Additional work for VCHR and maybe VBLK devices.
31 1.6.2.2 mjf * 2. Consider making the job-finding O(n) per one file descriptor.
32 1.6.2.2 mjf */
33 1.6.2.2 mjf
34 1.6.2.2 mjf #include <sys/cdefs.h>
35 1.6.2.2 mjf __KERNEL_RCSID(0, "$NetBSD: sys_aio.c,v 1.6.2.2 2007/07/11 20:10:08 mjf Exp $");
36 1.6.2.2 mjf
37 1.6.2.2 mjf #include "opt_ddb.h"
38 1.6.2.2 mjf
39 1.6.2.2 mjf #include <sys/param.h>
40 1.6.2.2 mjf #include <sys/condvar.h>
41 1.6.2.2 mjf #include <sys/file.h>
42 1.6.2.2 mjf #include <sys/filedesc.h>
43 1.6.2.2 mjf #include <sys/kernel.h>
44 1.6.2.2 mjf #include <sys/kmem.h>
45 1.6.2.2 mjf #include <sys/lwp.h>
46 1.6.2.2 mjf #include <sys/mutex.h>
47 1.6.2.2 mjf #include <sys/pool.h>
48 1.6.2.2 mjf #include <sys/proc.h>
49 1.6.2.2 mjf #include <sys/queue.h>
50 1.6.2.2 mjf #include <sys/signal.h>
51 1.6.2.2 mjf #include <sys/signalvar.h>
52 1.6.2.2 mjf #include <sys/syscallargs.h>
53 1.6.2.2 mjf #include <sys/sysctl.h>
54 1.6.2.2 mjf #include <sys/systm.h>
55 1.6.2.2 mjf #include <sys/types.h>
56 1.6.2.2 mjf #include <sys/vnode.h>
57 1.6.2.2 mjf
58 1.6.2.2 mjf #include <uvm/uvm_extern.h>
59 1.6.2.2 mjf
60 1.6.2.2 mjf /*
61 1.6.2.2 mjf * System-wide limits and counter of AIO operations.
62 1.6.2.2 mjf * XXXSMP: We should spin-lock it, or modify atomically.
63 1.6.2.2 mjf */
64 1.6.2.2 mjf static u_int aio_listio_max = AIO_LISTIO_MAX;
65 1.6.2.2 mjf static u_int aio_max = AIO_MAX;
66 1.6.2.2 mjf static u_int aio_jobs_count;
67 1.6.2.2 mjf
68 1.6.2.2 mjf static struct pool aio_job_pool;
69 1.6.2.2 mjf static struct pool aio_lio_pool;
70 1.6.2.2 mjf
71 1.6.2.2 mjf /* Prototypes */
72 1.6.2.2 mjf void aio_worker(void *);
73 1.6.2.2 mjf static void aio_process(struct aio_job *);
74 1.6.2.2 mjf static void aio_sendsig(struct proc *, struct sigevent *);
75 1.6.2.2 mjf static int aio_enqueue_job(int, void *, struct lio_req *);
76 1.6.2.2 mjf
77 1.6.2.2 mjf /*
78 1.6.2.2 mjf * Initialize the AIO system.
79 1.6.2.2 mjf */
80 1.6.2.2 mjf void
81 1.6.2.2 mjf aio_sysinit(void)
82 1.6.2.2 mjf {
83 1.6.2.2 mjf
84 1.6.2.2 mjf pool_init(&aio_job_pool, sizeof(struct aio_job), 0, 0, 0,
85 1.6.2.2 mjf "aio_jobs_pool", &pool_allocator_nointr, IPL_NONE);
86 1.6.2.2 mjf pool_init(&aio_lio_pool, sizeof(struct lio_req), 0, 0, 0,
87 1.6.2.2 mjf "aio_lio_pool", &pool_allocator_nointr, IPL_NONE);
88 1.6.2.2 mjf }
89 1.6.2.2 mjf
90 1.6.2.2 mjf /*
91 1.6.2.2 mjf * Initialize Asynchronous I/O data structures for the process.
92 1.6.2.2 mjf */
93 1.6.2.2 mjf int
94 1.6.2.2 mjf aio_init(struct proc *p)
95 1.6.2.2 mjf {
96 1.6.2.2 mjf struct aioproc *aio;
97 1.6.2.2 mjf struct lwp *l;
98 1.6.2.2 mjf bool inmem;
99 1.6.2.2 mjf vaddr_t uaddr;
100 1.6.2.2 mjf
101 1.6.2.2 mjf /* Allocate and initialize AIO structure */
102 1.6.2.2 mjf aio = kmem_zalloc(sizeof(struct aioproc), KM_NOSLEEP);
103 1.6.2.2 mjf if (aio == NULL)
104 1.6.2.2 mjf return EAGAIN;
105 1.6.2.2 mjf
106 1.6.2.2 mjf /* Initialize queue and their synchronization structures */
107 1.6.2.2 mjf mutex_init(&aio->aio_mtx, MUTEX_DEFAULT, IPL_NONE);
108 1.6.2.2 mjf cv_init(&aio->aio_worker_cv, "aiowork");
109 1.6.2.2 mjf cv_init(&aio->done_cv, "aiodone");
110 1.6.2.2 mjf TAILQ_INIT(&aio->jobs_queue);
111 1.6.2.2 mjf
112 1.6.2.2 mjf /*
113 1.6.2.2 mjf * Create an AIO worker thread.
114 1.6.2.2 mjf * XXX: Currently, AIO thread is not protected against user's actions.
115 1.6.2.2 mjf */
116 1.6.2.2 mjf inmem = uvm_uarea_alloc(&uaddr);
117 1.6.2.2 mjf if (uaddr == 0) {
118 1.6.2.2 mjf aio_exit(p, aio);
119 1.6.2.2 mjf return EAGAIN;
120 1.6.2.2 mjf }
121 1.6.2.2 mjf if (newlwp(curlwp, p, uaddr, inmem, 0, NULL, 0,
122 1.6.2.2 mjf aio_worker, NULL, &l)) {
123 1.6.2.2 mjf uvm_uarea_free(uaddr);
124 1.6.2.2 mjf aio_exit(p, aio);
125 1.6.2.2 mjf return EAGAIN;
126 1.6.2.2 mjf }
127 1.6.2.2 mjf
128 1.6.2.2 mjf /* Recheck if we are really first */
129 1.6.2.2 mjf mutex_enter(&p->p_mutex);
130 1.6.2.2 mjf if (p->p_aio) {
131 1.6.2.2 mjf mutex_exit(&p->p_mutex);
132 1.6.2.2 mjf aio_exit(p, aio);
133 1.6.2.2 mjf lwp_exit(l);
134 1.6.2.2 mjf return 0;
135 1.6.2.2 mjf }
136 1.6.2.2 mjf p->p_aio = aio;
137 1.6.2.2 mjf mutex_exit(&p->p_mutex);
138 1.6.2.2 mjf
139 1.6.2.2 mjf /* Complete the initialization of thread, and run it */
140 1.6.2.2 mjf mutex_enter(&p->p_smutex);
141 1.6.2.2 mjf aio->aio_worker = l;
142 1.6.2.2 mjf p->p_nrlwps++;
143 1.6.2.2 mjf lwp_lock(l);
144 1.6.2.2 mjf l->l_stat = LSRUN;
145 1.6.2.2 mjf l->l_usrpri = PUSER - 1; /* XXX */
146 1.6.2.2 mjf sched_enqueue(l, false);
147 1.6.2.2 mjf lwp_unlock(l);
148 1.6.2.2 mjf mutex_exit(&p->p_smutex);
149 1.6.2.2 mjf
150 1.6.2.2 mjf return 0;
151 1.6.2.2 mjf }
152 1.6.2.2 mjf
153 1.6.2.2 mjf /*
154 1.6.2.2 mjf * Exit of Asynchronous I/O subsystem of process.
155 1.6.2.2 mjf */
156 1.6.2.2 mjf void
157 1.6.2.2 mjf aio_exit(struct proc *p, struct aioproc *aio)
158 1.6.2.2 mjf {
159 1.6.2.2 mjf struct aio_job *a_job;
160 1.6.2.2 mjf
161 1.6.2.2 mjf if (aio == NULL)
162 1.6.2.2 mjf return;
163 1.6.2.2 mjf
164 1.6.2.2 mjf /* Free AIO queue */
165 1.6.2.2 mjf while (!TAILQ_EMPTY(&aio->jobs_queue)) {
166 1.6.2.2 mjf a_job = TAILQ_FIRST(&aio->jobs_queue);
167 1.6.2.2 mjf TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
168 1.6.2.2 mjf pool_put(&aio_job_pool, a_job);
169 1.6.2.2 mjf aio_jobs_count--; /* XXXSMP */
170 1.6.2.2 mjf }
171 1.6.2.2 mjf
172 1.6.2.2 mjf /* Destroy and free the entire AIO data structure */
173 1.6.2.2 mjf cv_destroy(&aio->aio_worker_cv);
174 1.6.2.2 mjf cv_destroy(&aio->done_cv);
175 1.6.2.2 mjf mutex_destroy(&aio->aio_mtx);
176 1.6.2.2 mjf kmem_free(aio, sizeof(struct aioproc));
177 1.6.2.2 mjf }
178 1.6.2.2 mjf
179 1.6.2.2 mjf /*
180 1.6.2.2 mjf * AIO worker thread and processor.
181 1.6.2.2 mjf */
182 1.6.2.2 mjf void
183 1.6.2.2 mjf aio_worker(void *arg)
184 1.6.2.2 mjf {
185 1.6.2.2 mjf struct proc *p = curlwp->l_proc;
186 1.6.2.2 mjf struct aioproc *aio = p->p_aio;
187 1.6.2.2 mjf struct aio_job *a_job;
188 1.6.2.2 mjf struct lio_req *lio;
189 1.6.2.2 mjf sigset_t oss, nss;
190 1.6.2.2 mjf int error, refcnt;
191 1.6.2.2 mjf
192 1.6.2.2 mjf /*
193 1.6.2.2 mjf * Make an empty signal mask, so it
194 1.6.2.2 mjf * handles only SIGKILL and SIGSTOP.
195 1.6.2.2 mjf */
196 1.6.2.2 mjf sigfillset(&nss);
197 1.6.2.2 mjf mutex_enter(&p->p_smutex);
198 1.6.2.2 mjf error = sigprocmask1(curlwp, SIG_SETMASK, &nss, &oss);
199 1.6.2.2 mjf mutex_exit(&p->p_smutex);
200 1.6.2.2 mjf KASSERT(error == 0);
201 1.6.2.2 mjf
202 1.6.2.2 mjf for (;;) {
203 1.6.2.2 mjf /*
204 1.6.2.2 mjf * Loop for each job in the queue. If there
205 1.6.2.2 mjf * are no jobs then sleep.
206 1.6.2.2 mjf */
207 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
208 1.6.2.2 mjf while ((a_job = TAILQ_FIRST(&aio->jobs_queue)) == NULL) {
209 1.6.2.2 mjf if (cv_wait_sig(&aio->aio_worker_cv, &aio->aio_mtx)) {
210 1.6.2.2 mjf /*
211 1.6.2.2 mjf * Thread was interrupted - check for
212 1.6.2.2 mjf * pending exit or suspend.
213 1.6.2.2 mjf */
214 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
215 1.6.2.2 mjf lwp_userret(curlwp);
216 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
217 1.6.2.2 mjf }
218 1.6.2.2 mjf }
219 1.6.2.2 mjf
220 1.6.2.2 mjf /* Take the job from the queue */
221 1.6.2.2 mjf aio->curjob = a_job;
222 1.6.2.2 mjf TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
223 1.6.2.2 mjf
224 1.6.2.2 mjf aio_jobs_count--; /* XXXSMP */
225 1.6.2.2 mjf aio->jobs_count--;
226 1.6.2.2 mjf
227 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
228 1.6.2.2 mjf
229 1.6.2.2 mjf /* Process an AIO operation */
230 1.6.2.2 mjf aio_process(a_job);
231 1.6.2.2 mjf
232 1.6.2.2 mjf /* Copy data structure back to the user-space */
233 1.6.2.2 mjf (void)copyout(&a_job->aiocbp, a_job->aiocb_uptr,
234 1.6.2.2 mjf sizeof(struct aiocb));
235 1.6.2.2 mjf
236 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
237 1.6.2.2 mjf aio->curjob = NULL;
238 1.6.2.2 mjf
239 1.6.2.2 mjf /* Decrease a reference counter, if there is a LIO structure */
240 1.6.2.2 mjf lio = a_job->lio;
241 1.6.2.2 mjf refcnt = (lio != NULL ? --lio->refcnt : -1);
242 1.6.2.2 mjf
243 1.6.2.2 mjf /* Notify all suspenders */
244 1.6.2.2 mjf cv_broadcast(&aio->done_cv);
245 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
246 1.6.2.2 mjf
247 1.6.2.2 mjf /* Send a signal, if any */
248 1.6.2.2 mjf aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
249 1.6.2.2 mjf
250 1.6.2.2 mjf /* Destroy the LIO structure */
251 1.6.2.2 mjf if (refcnt == 0) {
252 1.6.2.2 mjf aio_sendsig(p, &lio->sig);
253 1.6.2.2 mjf pool_put(&aio_lio_pool, lio);
254 1.6.2.2 mjf }
255 1.6.2.2 mjf
256 1.6.2.2 mjf /* Destroy the the job */
257 1.6.2.2 mjf pool_put(&aio_job_pool, a_job);
258 1.6.2.2 mjf }
259 1.6.2.2 mjf
260 1.6.2.2 mjf /* NOTREACHED */
261 1.6.2.2 mjf }
262 1.6.2.2 mjf
263 1.6.2.2 mjf static void
264 1.6.2.2 mjf aio_process(struct aio_job *a_job)
265 1.6.2.2 mjf {
266 1.6.2.2 mjf struct proc *p = curlwp->l_proc;
267 1.6.2.2 mjf struct aiocb *aiocbp = &a_job->aiocbp;
268 1.6.2.2 mjf struct file *fp;
269 1.6.2.2 mjf struct filedesc *fdp = p->p_fd;
270 1.6.2.2 mjf int fd = aiocbp->aio_fildes;
271 1.6.2.2 mjf int error = 0;
272 1.6.2.2 mjf
273 1.6.2.2 mjf KASSERT(fdp != NULL);
274 1.6.2.2 mjf KASSERT(a_job->aio_op != 0);
275 1.6.2.2 mjf
276 1.6.2.2 mjf if ((a_job->aio_op & (AIO_READ | AIO_WRITE)) != 0) {
277 1.6.2.2 mjf struct iovec aiov;
278 1.6.2.2 mjf struct uio auio;
279 1.6.2.2 mjf
280 1.6.2.2 mjf if (aiocbp->aio_nbytes > SSIZE_MAX) {
281 1.6.2.2 mjf error = EINVAL;
282 1.6.2.2 mjf goto done;
283 1.6.2.2 mjf }
284 1.6.2.2 mjf
285 1.6.2.2 mjf fp = fd_getfile(fdp, fd);
286 1.6.2.2 mjf if (fp == NULL) {
287 1.6.2.2 mjf error = EBADF;
288 1.6.2.2 mjf goto done;
289 1.6.2.2 mjf }
290 1.6.2.2 mjf
291 1.6.2.2 mjf aiov.iov_base = (void *)(uintptr_t)aiocbp->aio_buf;
292 1.6.2.2 mjf aiov.iov_len = aiocbp->aio_nbytes;
293 1.6.2.2 mjf auio.uio_iov = &aiov;
294 1.6.2.2 mjf auio.uio_iovcnt = 1;
295 1.6.2.2 mjf auio.uio_resid = aiocbp->aio_nbytes;
296 1.6.2.2 mjf auio.uio_vmspace = p->p_vmspace;
297 1.6.2.2 mjf
298 1.6.2.2 mjf FILE_USE(fp);
299 1.6.2.2 mjf if (a_job->aio_op & AIO_READ) {
300 1.6.2.2 mjf /*
301 1.6.2.2 mjf * Perform a Read operation
302 1.6.2.2 mjf */
303 1.6.2.2 mjf KASSERT((a_job->aio_op & AIO_WRITE) == 0);
304 1.6.2.2 mjf
305 1.6.2.2 mjf if ((fp->f_flag & FREAD) == 0) {
306 1.6.2.2 mjf FILE_UNUSE(fp, curlwp);
307 1.6.2.2 mjf error = EBADF;
308 1.6.2.2 mjf goto done;
309 1.6.2.2 mjf }
310 1.6.2.2 mjf auio.uio_rw = UIO_READ;
311 1.6.2.2 mjf error = (*fp->f_ops->fo_read)(fp, &aiocbp->aio_offset,
312 1.6.2.2 mjf &auio, fp->f_cred, FOF_UPDATE_OFFSET);
313 1.6.2.2 mjf } else {
314 1.6.2.2 mjf /*
315 1.6.2.2 mjf * Perform a Write operation
316 1.6.2.2 mjf */
317 1.6.2.2 mjf KASSERT(a_job->aio_op & AIO_WRITE);
318 1.6.2.2 mjf
319 1.6.2.2 mjf if ((fp->f_flag & FWRITE) == 0) {
320 1.6.2.2 mjf FILE_UNUSE(fp, curlwp);
321 1.6.2.2 mjf error = EBADF;
322 1.6.2.2 mjf goto done;
323 1.6.2.2 mjf }
324 1.6.2.2 mjf auio.uio_rw = UIO_WRITE;
325 1.6.2.2 mjf error = (*fp->f_ops->fo_write)(fp, &aiocbp->aio_offset,
326 1.6.2.2 mjf &auio, fp->f_cred, FOF_UPDATE_OFFSET);
327 1.6.2.2 mjf }
328 1.6.2.2 mjf FILE_UNUSE(fp, curlwp);
329 1.6.2.2 mjf
330 1.6.2.2 mjf /* Store the result value */
331 1.6.2.2 mjf a_job->aiocbp.aio_nbytes -= auio.uio_resid;
332 1.6.2.2 mjf a_job->aiocbp._retval = (error == 0) ?
333 1.6.2.2 mjf a_job->aiocbp.aio_nbytes : -1;
334 1.6.2.2 mjf
335 1.6.2.2 mjf } else if ((a_job->aio_op & (AIO_SYNC | AIO_DSYNC)) != 0) {
336 1.6.2.2 mjf /*
337 1.6.2.2 mjf * Perform a file Sync operation
338 1.6.2.2 mjf */
339 1.6.2.2 mjf struct vnode *vp;
340 1.6.2.2 mjf
341 1.6.2.2 mjf if ((error = getvnode(fdp, fd, &fp)) != 0)
342 1.6.2.2 mjf goto done;
343 1.6.2.2 mjf
344 1.6.2.2 mjf if ((fp->f_flag & FWRITE) == 0) {
345 1.6.2.2 mjf FILE_UNUSE(fp, curlwp);
346 1.6.2.2 mjf error = EBADF;
347 1.6.2.2 mjf goto done;
348 1.6.2.2 mjf }
349 1.6.2.2 mjf
350 1.6.2.2 mjf vp = (struct vnode *)fp->f_data;
351 1.6.2.2 mjf vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
352 1.6.2.2 mjf if (a_job->aio_op & AIO_DSYNC) {
353 1.6.2.2 mjf error = VOP_FSYNC(vp, fp->f_cred,
354 1.6.2.2 mjf FSYNC_WAIT | FSYNC_DATAONLY, 0, 0, curlwp);
355 1.6.2.2 mjf } else if (a_job->aio_op & AIO_SYNC) {
356 1.6.2.2 mjf error = VOP_FSYNC(vp, fp->f_cred,
357 1.6.2.2 mjf FSYNC_WAIT, 0, 0, curlwp);
358 1.6.2.2 mjf if (error == 0 && bioops.io_fsync != NULL &&
359 1.6.2.2 mjf vp->v_mount &&
360 1.6.2.2 mjf (vp->v_mount->mnt_flag & MNT_SOFTDEP))
361 1.6.2.2 mjf (*bioops.io_fsync)(vp, 0);
362 1.6.2.2 mjf }
363 1.6.2.2 mjf VOP_UNLOCK(vp, 0);
364 1.6.2.2 mjf FILE_UNUSE(fp, curlwp);
365 1.6.2.2 mjf
366 1.6.2.2 mjf /* Store the result value */
367 1.6.2.2 mjf a_job->aiocbp._retval = (error == 0) ? 0 : -1;
368 1.6.2.2 mjf
369 1.6.2.2 mjf } else
370 1.6.2.2 mjf panic("aio_process: invalid operation code\n");
371 1.6.2.2 mjf
372 1.6.2.2 mjf done:
373 1.6.2.2 mjf /* Job is done, set the error, if any */
374 1.6.2.2 mjf a_job->aiocbp._errno = error;
375 1.6.2.2 mjf a_job->aiocbp._state = JOB_DONE;
376 1.6.2.2 mjf }
377 1.6.2.2 mjf
378 1.6.2.2 mjf /*
379 1.6.2.2 mjf * Send AIO signal.
380 1.6.2.2 mjf */
381 1.6.2.2 mjf static void
382 1.6.2.2 mjf aio_sendsig(struct proc *p, struct sigevent *sig)
383 1.6.2.2 mjf {
384 1.6.2.2 mjf ksiginfo_t ksi;
385 1.6.2.2 mjf
386 1.6.2.2 mjf if (sig->sigev_signo == 0 || sig->sigev_notify == SIGEV_NONE)
387 1.6.2.2 mjf return;
388 1.6.2.2 mjf
389 1.6.2.2 mjf KSI_INIT(&ksi);
390 1.6.2.2 mjf ksi.ksi_signo = sig->sigev_signo;
391 1.6.2.2 mjf ksi.ksi_code = SI_ASYNCIO;
392 1.6.2.2 mjf ksi.ksi_value = sig->sigev_value;
393 1.6.2.2 mjf mutex_enter(&proclist_mutex);
394 1.6.2.2 mjf kpsignal(p, &ksi, NULL);
395 1.6.2.2 mjf mutex_exit(&proclist_mutex);
396 1.6.2.2 mjf }
397 1.6.2.2 mjf
398 1.6.2.2 mjf /*
399 1.6.2.2 mjf * Enqueue the job.
400 1.6.2.2 mjf */
401 1.6.2.2 mjf static int
402 1.6.2.2 mjf aio_enqueue_job(int op, void *aiocb_uptr, struct lio_req *lio)
403 1.6.2.2 mjf {
404 1.6.2.2 mjf struct proc *p = curlwp->l_proc;
405 1.6.2.2 mjf struct aioproc *aio;
406 1.6.2.2 mjf struct aio_job *a_job;
407 1.6.2.2 mjf struct aiocb aiocbp;
408 1.6.2.2 mjf struct sigevent *sig;
409 1.6.2.2 mjf int error;
410 1.6.2.2 mjf
411 1.6.2.2 mjf /* Check for the limit */
412 1.6.2.2 mjf if (aio_jobs_count + 1 > aio_max) /* XXXSMP */
413 1.6.2.2 mjf return EAGAIN;
414 1.6.2.2 mjf
415 1.6.2.2 mjf /* Get the data structure from user-space */
416 1.6.2.2 mjf error = copyin(aiocb_uptr, &aiocbp, sizeof(struct aiocb));
417 1.6.2.2 mjf if (error)
418 1.6.2.2 mjf return error;
419 1.6.2.2 mjf
420 1.6.2.2 mjf /* Check if signal is set, and validate it */
421 1.6.2.2 mjf sig = &aiocbp.aio_sigevent;
422 1.6.2.2 mjf if (sig->sigev_signo < 0 || sig->sigev_signo >= NSIG ||
423 1.6.2.2 mjf sig->sigev_notify < SIGEV_NONE || sig->sigev_notify > SIGEV_SA)
424 1.6.2.2 mjf return EINVAL;
425 1.6.2.2 mjf
426 1.6.2.2 mjf /* Buffer and byte count */
427 1.6.2.2 mjf if (((AIO_SYNC | AIO_DSYNC) & op) == 0)
428 1.6.2.2 mjf if (aiocbp.aio_buf == NULL || aiocbp.aio_nbytes > SSIZE_MAX)
429 1.6.2.2 mjf return EINVAL;
430 1.6.2.2 mjf
431 1.6.2.2 mjf /* Check the opcode, if LIO_NOP - simply ignore */
432 1.6.2.2 mjf if (op == AIO_LIO) {
433 1.6.2.2 mjf KASSERT(lio != NULL);
434 1.6.2.2 mjf if (aiocbp.aio_lio_opcode == LIO_WRITE)
435 1.6.2.2 mjf op = AIO_WRITE;
436 1.6.2.2 mjf else if (aiocbp.aio_lio_opcode == LIO_READ)
437 1.6.2.2 mjf op = AIO_READ;
438 1.6.2.2 mjf else
439 1.6.2.2 mjf return (aiocbp.aio_lio_opcode == LIO_NOP) ? 0 : EINVAL;
440 1.6.2.2 mjf } else {
441 1.6.2.2 mjf KASSERT(lio == NULL);
442 1.6.2.2 mjf }
443 1.6.2.2 mjf
444 1.6.2.2 mjf /*
445 1.6.2.2 mjf * Look for already existing job. If found - the job is in-progress.
446 1.6.2.2 mjf * According to POSIX this is invalid, so return the error.
447 1.6.2.2 mjf */
448 1.6.2.2 mjf aio = p->p_aio;
449 1.6.2.2 mjf if (aio) {
450 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
451 1.6.2.2 mjf if (aio->curjob) {
452 1.6.2.2 mjf a_job = aio->curjob;
453 1.6.2.2 mjf if (a_job->aiocb_uptr == aiocb_uptr) {
454 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
455 1.6.2.2 mjf return EINVAL;
456 1.6.2.2 mjf }
457 1.6.2.2 mjf }
458 1.6.2.2 mjf TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
459 1.6.2.2 mjf if (a_job->aiocb_uptr != aiocb_uptr)
460 1.6.2.2 mjf continue;
461 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
462 1.6.2.2 mjf return EINVAL;
463 1.6.2.2 mjf }
464 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
465 1.6.2.2 mjf }
466 1.6.2.2 mjf
467 1.6.2.2 mjf /*
468 1.6.2.2 mjf * Check if AIO structure is initialized, if not - initialize it.
469 1.6.2.2 mjf * In LIO case, we did that already. We will recheck this with
470 1.6.2.2 mjf * the lock in aio_init().
471 1.6.2.2 mjf */
472 1.6.2.2 mjf if (lio == NULL && p->p_aio == NULL)
473 1.6.2.2 mjf if (aio_init(p))
474 1.6.2.2 mjf return EAGAIN;
475 1.6.2.2 mjf aio = p->p_aio;
476 1.6.2.2 mjf
477 1.6.2.2 mjf /*
478 1.6.2.2 mjf * Set the state with errno, and copy data
479 1.6.2.2 mjf * structure back to the user-space.
480 1.6.2.2 mjf */
481 1.6.2.2 mjf aiocbp._state = JOB_WIP;
482 1.6.2.2 mjf aiocbp._errno = EINPROGRESS;
483 1.6.2.2 mjf aiocbp._retval = -1;
484 1.6.2.2 mjf error = copyout(&aiocbp, aiocb_uptr, sizeof(struct aiocb));
485 1.6.2.2 mjf if (error)
486 1.6.2.2 mjf return error;
487 1.6.2.2 mjf
488 1.6.2.2 mjf /* Allocate and initialize a new AIO job */
489 1.6.2.2 mjf a_job = pool_get(&aio_job_pool, PR_WAITOK);
490 1.6.2.2 mjf memset(a_job, 0, sizeof(struct aio_job));
491 1.6.2.2 mjf
492 1.6.2.2 mjf /*
493 1.6.2.2 mjf * Set the data.
494 1.6.2.2 mjf * Store the user-space pointer for searching. Since we
495 1.6.2.2 mjf * are storing only per proc pointers - it is safe.
496 1.6.2.2 mjf */
497 1.6.2.2 mjf memcpy(&a_job->aiocbp, &aiocbp, sizeof(struct aiocb));
498 1.6.2.2 mjf a_job->aiocb_uptr = aiocb_uptr;
499 1.6.2.2 mjf a_job->aio_op |= op;
500 1.6.2.2 mjf a_job->lio = lio;
501 1.6.2.2 mjf
502 1.6.2.2 mjf /*
503 1.6.2.2 mjf * Add the job to the queue, update the counters, and
504 1.6.2.2 mjf * notify the AIO worker thread to handle the job.
505 1.6.2.2 mjf */
506 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
507 1.6.2.2 mjf
508 1.6.2.2 mjf /* Fail, if the limit was reached */
509 1.6.2.2 mjf if (aio->jobs_count >= aio_listio_max) {
510 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
511 1.6.2.2 mjf pool_put(&aio_job_pool, a_job);
512 1.6.2.2 mjf return EAGAIN;
513 1.6.2.2 mjf }
514 1.6.2.2 mjf
515 1.6.2.2 mjf TAILQ_INSERT_TAIL(&aio->jobs_queue, a_job, list);
516 1.6.2.2 mjf aio_jobs_count++; /* XXXSMP */
517 1.6.2.2 mjf aio->jobs_count++;
518 1.6.2.2 mjf if (lio)
519 1.6.2.2 mjf lio->refcnt++;
520 1.6.2.2 mjf cv_signal(&aio->aio_worker_cv);
521 1.6.2.2 mjf
522 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
523 1.6.2.2 mjf
524 1.6.2.2 mjf /*
525 1.6.2.2 mjf * One would handle the errors only with aio_error() function.
526 1.6.2.2 mjf * This way is appropriate according to POSIX.
527 1.6.2.2 mjf */
528 1.6.2.2 mjf return 0;
529 1.6.2.2 mjf }
530 1.6.2.2 mjf
531 1.6.2.2 mjf /*
532 1.6.2.2 mjf * Syscall functions.
533 1.6.2.2 mjf */
534 1.6.2.2 mjf
535 1.6.2.2 mjf int
536 1.6.2.2 mjf sys_aio_cancel(struct lwp *l, void *v, register_t *retval)
537 1.6.2.2 mjf {
538 1.6.2.2 mjf struct sys_aio_cancel_args /* {
539 1.6.2.2 mjf syscallarg(int) fildes;
540 1.6.2.2 mjf syscallarg(struct aiocb *) aiocbp;
541 1.6.2.2 mjf } */ *uap = v;
542 1.6.2.2 mjf struct proc *p = l->l_proc;
543 1.6.2.2 mjf struct aioproc *aio;
544 1.6.2.2 mjf struct aio_job *a_job;
545 1.6.2.2 mjf struct aiocb *aiocbp_ptr;
546 1.6.2.2 mjf struct lio_req *lio;
547 1.6.2.2 mjf struct filedesc *fdp = p->p_fd;
548 1.6.2.2 mjf unsigned int cn, errcnt, fildes;
549 1.6.2.2 mjf
550 1.6.2.2 mjf TAILQ_HEAD(, aio_job) tmp_jobs_list;
551 1.6.2.2 mjf
552 1.6.2.2 mjf /* Check for invalid file descriptor */
553 1.6.2.2 mjf fildes = (unsigned int)SCARG(uap, fildes);
554 1.6.2.2 mjf if (fildes >= fdp->fd_nfiles || fdp->fd_ofiles[fildes] == NULL)
555 1.6.2.2 mjf return EBADF;
556 1.6.2.2 mjf
557 1.6.2.2 mjf /* Check if AIO structure is initialized */
558 1.6.2.2 mjf if (p->p_aio == NULL) {
559 1.6.2.2 mjf *retval = AIO_NOTCANCELED;
560 1.6.2.2 mjf return 0;
561 1.6.2.2 mjf }
562 1.6.2.2 mjf
563 1.6.2.2 mjf aio = p->p_aio;
564 1.6.2.2 mjf aiocbp_ptr = (struct aiocb *)SCARG(uap, aiocbp);
565 1.6.2.2 mjf
566 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
567 1.6.2.2 mjf
568 1.6.2.2 mjf /* Cancel the jobs, and remove them from the queue */
569 1.6.2.2 mjf cn = 0;
570 1.6.2.2 mjf TAILQ_INIT(&tmp_jobs_list);
571 1.6.2.2 mjf TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
572 1.6.2.2 mjf if (aiocbp_ptr) {
573 1.6.2.2 mjf if (aiocbp_ptr != a_job->aiocb_uptr)
574 1.6.2.2 mjf continue;
575 1.6.2.2 mjf if (fildes != a_job->aiocbp.aio_fildes) {
576 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
577 1.6.2.2 mjf return EBADF;
578 1.6.2.2 mjf }
579 1.6.2.2 mjf } else if (a_job->aiocbp.aio_fildes != fildes)
580 1.6.2.2 mjf continue;
581 1.6.2.2 mjf
582 1.6.2.2 mjf TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
583 1.6.2.2 mjf TAILQ_INSERT_TAIL(&tmp_jobs_list, a_job, list);
584 1.6.2.2 mjf
585 1.6.2.2 mjf /* Decrease the counters */
586 1.6.2.2 mjf aio_jobs_count--; /* XXXSMP */
587 1.6.2.2 mjf aio->jobs_count--;
588 1.6.2.2 mjf lio = a_job->lio;
589 1.6.2.2 mjf if (lio != NULL && --lio->refcnt != 0)
590 1.6.2.2 mjf a_job->lio = NULL;
591 1.6.2.2 mjf
592 1.6.2.2 mjf cn++;
593 1.6.2.2 mjf if (aiocbp_ptr)
594 1.6.2.2 mjf break;
595 1.6.2.2 mjf }
596 1.6.2.2 mjf
597 1.6.2.2 mjf /* There are canceled jobs */
598 1.6.2.2 mjf if (cn)
599 1.6.2.2 mjf *retval = AIO_CANCELED;
600 1.6.2.2 mjf
601 1.6.2.2 mjf /* We cannot cancel current job */
602 1.6.2.2 mjf a_job = aio->curjob;
603 1.6.2.2 mjf if (a_job && ((a_job->aiocbp.aio_fildes == fildes) ||
604 1.6.2.2 mjf (a_job->aiocb_uptr == aiocbp_ptr)))
605 1.6.2.2 mjf *retval = AIO_NOTCANCELED;
606 1.6.2.2 mjf
607 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
608 1.6.2.2 mjf
609 1.6.2.2 mjf /* Free the jobs after the lock */
610 1.6.2.2 mjf errcnt = 0;
611 1.6.2.2 mjf while (!TAILQ_EMPTY(&tmp_jobs_list)) {
612 1.6.2.2 mjf a_job = TAILQ_FIRST(&tmp_jobs_list);
613 1.6.2.2 mjf TAILQ_REMOVE(&tmp_jobs_list, a_job, list);
614 1.6.2.2 mjf /* Set the errno and copy structures back to the user-space */
615 1.6.2.2 mjf a_job->aiocbp._errno = ECANCELED;
616 1.6.2.2 mjf a_job->aiocbp._state = JOB_DONE;
617 1.6.2.2 mjf if (copyout(&a_job->aiocbp, a_job->aiocb_uptr,
618 1.6.2.2 mjf sizeof(struct aiocb)))
619 1.6.2.2 mjf errcnt++;
620 1.6.2.2 mjf /* Send a signal if any */
621 1.6.2.2 mjf aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
622 1.6.2.2 mjf if (a_job->lio) {
623 1.6.2.2 mjf lio = a_job->lio;
624 1.6.2.2 mjf aio_sendsig(p, &lio->sig);
625 1.6.2.2 mjf pool_put(&aio_lio_pool, lio);
626 1.6.2.2 mjf }
627 1.6.2.2 mjf pool_put(&aio_job_pool, a_job);
628 1.6.2.2 mjf }
629 1.6.2.2 mjf
630 1.6.2.2 mjf if (errcnt)
631 1.6.2.2 mjf return EFAULT;
632 1.6.2.2 mjf
633 1.6.2.2 mjf /* Set a correct return value */
634 1.6.2.2 mjf if (*retval == 0)
635 1.6.2.2 mjf *retval = AIO_ALLDONE;
636 1.6.2.2 mjf
637 1.6.2.2 mjf return 0;
638 1.6.2.2 mjf }
639 1.6.2.2 mjf
640 1.6.2.2 mjf int
641 1.6.2.2 mjf sys_aio_error(struct lwp *l, void *v, register_t *retval)
642 1.6.2.2 mjf {
643 1.6.2.2 mjf struct sys_aio_error_args /* {
644 1.6.2.2 mjf syscallarg(const struct aiocb *) aiocbp;
645 1.6.2.2 mjf } */ *uap = v;
646 1.6.2.2 mjf struct proc *p = l->l_proc;
647 1.6.2.2 mjf struct aioproc *aio = p->p_aio;
648 1.6.2.2 mjf struct aiocb aiocbp;
649 1.6.2.2 mjf int error;
650 1.6.2.2 mjf
651 1.6.2.2 mjf if (aio == NULL)
652 1.6.2.2 mjf return EINVAL;
653 1.6.2.2 mjf
654 1.6.2.2 mjf error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
655 1.6.2.2 mjf if (error)
656 1.6.2.2 mjf return error;
657 1.6.2.2 mjf
658 1.6.2.2 mjf if (aiocbp._state == JOB_NONE)
659 1.6.2.2 mjf return EINVAL;
660 1.6.2.2 mjf
661 1.6.2.2 mjf *retval = aiocbp._errno;
662 1.6.2.2 mjf
663 1.6.2.2 mjf return 0;
664 1.6.2.2 mjf }
665 1.6.2.2 mjf
666 1.6.2.2 mjf int
667 1.6.2.2 mjf sys_aio_fsync(struct lwp *l, void *v, register_t *retval)
668 1.6.2.2 mjf {
669 1.6.2.2 mjf struct sys_aio_fsync_args /* {
670 1.6.2.2 mjf syscallarg(int) op;
671 1.6.2.2 mjf syscallarg(struct aiocb *) aiocbp;
672 1.6.2.2 mjf } */ *uap = v;
673 1.6.2.2 mjf int op = SCARG(uap, op);
674 1.6.2.2 mjf
675 1.6.2.2 mjf if ((op != O_DSYNC) && (op != O_SYNC))
676 1.6.2.2 mjf return EINVAL;
677 1.6.2.2 mjf
678 1.6.2.2 mjf op = O_DSYNC ? AIO_DSYNC : AIO_SYNC;
679 1.6.2.2 mjf
680 1.6.2.2 mjf return aio_enqueue_job(op, SCARG(uap, aiocbp), NULL);
681 1.6.2.2 mjf }
682 1.6.2.2 mjf
683 1.6.2.2 mjf int
684 1.6.2.2 mjf sys_aio_read(struct lwp *l, void *v, register_t *retval)
685 1.6.2.2 mjf {
686 1.6.2.2 mjf struct sys_aio_read_args /* {
687 1.6.2.2 mjf syscallarg(struct aiocb *) aiocbp;
688 1.6.2.2 mjf } */ *uap = v;
689 1.6.2.2 mjf
690 1.6.2.2 mjf return aio_enqueue_job(AIO_READ, SCARG(uap, aiocbp), NULL);
691 1.6.2.2 mjf }
692 1.6.2.2 mjf
693 1.6.2.2 mjf int
694 1.6.2.2 mjf sys_aio_return(struct lwp *l, void *v, register_t *retval)
695 1.6.2.2 mjf {
696 1.6.2.2 mjf struct sys_aio_return_args /* {
697 1.6.2.2 mjf syscallarg(struct aiocb *) aiocbp;
698 1.6.2.2 mjf } */ *uap = v;
699 1.6.2.2 mjf struct proc *p = l->l_proc;
700 1.6.2.2 mjf struct aioproc *aio = p->p_aio;
701 1.6.2.2 mjf struct aiocb aiocbp;
702 1.6.2.2 mjf int error;
703 1.6.2.2 mjf
704 1.6.2.2 mjf if (aio == NULL)
705 1.6.2.2 mjf return EINVAL;
706 1.6.2.2 mjf
707 1.6.2.2 mjf error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
708 1.6.2.2 mjf if (error)
709 1.6.2.2 mjf return error;
710 1.6.2.2 mjf
711 1.6.2.2 mjf if (aiocbp._errno == EINPROGRESS || aiocbp._state != JOB_DONE)
712 1.6.2.2 mjf return EINVAL;
713 1.6.2.2 mjf
714 1.6.2.2 mjf *retval = aiocbp._retval;
715 1.6.2.2 mjf
716 1.6.2.2 mjf /* Reset the internal variables */
717 1.6.2.2 mjf aiocbp._errno = 0;
718 1.6.2.2 mjf aiocbp._retval = -1;
719 1.6.2.2 mjf aiocbp._state = JOB_NONE;
720 1.6.2.2 mjf error = copyout(&aiocbp, SCARG(uap, aiocbp), sizeof(struct aiocb));
721 1.6.2.2 mjf
722 1.6.2.2 mjf return error;
723 1.6.2.2 mjf }
724 1.6.2.2 mjf
725 1.6.2.2 mjf int
726 1.6.2.2 mjf sys_aio_suspend(struct lwp *l, void *v, register_t *retval)
727 1.6.2.2 mjf {
728 1.6.2.2 mjf struct sys_aio_suspend_args /* {
729 1.6.2.2 mjf syscallarg(const struct aiocb *const[]) list;
730 1.6.2.2 mjf syscallarg(int) nent;
731 1.6.2.2 mjf syscallarg(const struct timespec *) timeout;
732 1.6.2.2 mjf } */ *uap = v;
733 1.6.2.2 mjf struct proc *p = l->l_proc;
734 1.6.2.2 mjf struct aioproc *aio;
735 1.6.2.2 mjf struct aio_job *a_job;
736 1.6.2.2 mjf struct aiocb **aiocbp_list;
737 1.6.2.2 mjf struct timespec ts;
738 1.6.2.2 mjf int i, error, nent, timo;
739 1.6.2.2 mjf
740 1.6.2.2 mjf if (p->p_aio == NULL)
741 1.6.2.2 mjf return EAGAIN;
742 1.6.2.2 mjf aio = p->p_aio;
743 1.6.2.2 mjf
744 1.6.2.2 mjf nent = SCARG(uap, nent);
745 1.6.2.2 mjf if (nent <= 0 || nent > aio_listio_max)
746 1.6.2.2 mjf return EAGAIN;
747 1.6.2.2 mjf
748 1.6.2.2 mjf if (SCARG(uap, timeout)) {
749 1.6.2.2 mjf /* Convert timespec to ticks */
750 1.6.2.2 mjf error = copyin(SCARG(uap, timeout), &ts,
751 1.6.2.2 mjf sizeof(struct timespec));
752 1.6.2.2 mjf if (error)
753 1.6.2.2 mjf return error;
754 1.6.2.2 mjf timo = mstohz((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
755 1.6.2.2 mjf if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
756 1.6.2.2 mjf timo = 1;
757 1.6.2.2 mjf if (timo <= 0)
758 1.6.2.2 mjf return EAGAIN;
759 1.6.2.2 mjf } else
760 1.6.2.2 mjf timo = 0;
761 1.6.2.2 mjf
762 1.6.2.2 mjf /* Get the list from user-space */
763 1.6.2.2 mjf aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
764 1.6.2.2 mjf error = copyin(SCARG(uap, list), aiocbp_list,
765 1.6.2.2 mjf nent * sizeof(struct aiocb));
766 1.6.2.2 mjf if (error) {
767 1.6.2.2 mjf kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
768 1.6.2.2 mjf return error;
769 1.6.2.2 mjf }
770 1.6.2.2 mjf
771 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
772 1.6.2.2 mjf for (;;) {
773 1.6.2.2 mjf
774 1.6.2.2 mjf for (i = 0; i < nent; i++) {
775 1.6.2.2 mjf
776 1.6.2.2 mjf /* Skip NULL entries */
777 1.6.2.2 mjf if (aiocbp_list[i] == NULL)
778 1.6.2.2 mjf continue;
779 1.6.2.2 mjf
780 1.6.2.2 mjf /* Skip current job */
781 1.6.2.2 mjf if (aio->curjob) {
782 1.6.2.2 mjf a_job = aio->curjob;
783 1.6.2.2 mjf if (a_job->aiocb_uptr == aiocbp_list[i])
784 1.6.2.2 mjf continue;
785 1.6.2.2 mjf }
786 1.6.2.2 mjf
787 1.6.2.2 mjf /* Look for a job in the queue */
788 1.6.2.2 mjf TAILQ_FOREACH(a_job, &aio->jobs_queue, list)
789 1.6.2.2 mjf if (a_job->aiocb_uptr == aiocbp_list[i])
790 1.6.2.2 mjf break;
791 1.6.2.2 mjf
792 1.6.2.2 mjf if (a_job == NULL) {
793 1.6.2.2 mjf struct aiocb aiocbp;
794 1.6.2.2 mjf
795 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
796 1.6.2.2 mjf
797 1.6.2.2 mjf error = copyin(aiocbp_list[i], &aiocbp,
798 1.6.2.2 mjf sizeof(struct aiocb));
799 1.6.2.2 mjf if (error == 0 && aiocbp._state != JOB_DONE) {
800 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
801 1.6.2.2 mjf continue;
802 1.6.2.2 mjf }
803 1.6.2.2 mjf
804 1.6.2.2 mjf kmem_free(aiocbp_list,
805 1.6.2.2 mjf nent * sizeof(struct aio_job));
806 1.6.2.2 mjf return error;
807 1.6.2.2 mjf }
808 1.6.2.2 mjf }
809 1.6.2.2 mjf
810 1.6.2.2 mjf /* Wait for a signal or when timeout occurs */
811 1.6.2.2 mjf error = cv_timedwait_sig(&aio->done_cv, &aio->aio_mtx, timo);
812 1.6.2.2 mjf if (error) {
813 1.6.2.2 mjf if (error == EWOULDBLOCK)
814 1.6.2.2 mjf error = EAGAIN;
815 1.6.2.2 mjf break;
816 1.6.2.2 mjf }
817 1.6.2.2 mjf }
818 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
819 1.6.2.2 mjf
820 1.6.2.2 mjf kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
821 1.6.2.2 mjf return error;
822 1.6.2.2 mjf }
823 1.6.2.2 mjf
824 1.6.2.2 mjf int
825 1.6.2.2 mjf sys_aio_write(struct lwp *l, void *v, register_t *retval)
826 1.6.2.2 mjf {
827 1.6.2.2 mjf struct sys_aio_write_args /* {
828 1.6.2.2 mjf syscallarg(struct aiocb *) aiocbp;
829 1.6.2.2 mjf } */ *uap = v;
830 1.6.2.2 mjf
831 1.6.2.2 mjf return aio_enqueue_job(AIO_WRITE, SCARG(uap, aiocbp), NULL);
832 1.6.2.2 mjf }
833 1.6.2.2 mjf
834 1.6.2.2 mjf int
835 1.6.2.2 mjf sys_lio_listio(struct lwp *l, void *v, register_t *retval)
836 1.6.2.2 mjf {
837 1.6.2.2 mjf struct sys_lio_listio_args /* {
838 1.6.2.2 mjf syscallarg(int) mode;
839 1.6.2.2 mjf syscallarg(struct aiocb *const[]) list;
840 1.6.2.2 mjf syscallarg(int) nent;
841 1.6.2.2 mjf syscallarg(struct sigevent *) sig;
842 1.6.2.2 mjf } */ *uap = v;
843 1.6.2.2 mjf struct proc *p = l->l_proc;
844 1.6.2.2 mjf struct aioproc *aio;
845 1.6.2.2 mjf struct aiocb **aiocbp_list;
846 1.6.2.2 mjf struct lio_req *lio;
847 1.6.2.2 mjf int i, error, errcnt, mode, nent;
848 1.6.2.2 mjf
849 1.6.2.2 mjf mode = SCARG(uap, mode);
850 1.6.2.2 mjf nent = SCARG(uap, nent);
851 1.6.2.2 mjf
852 1.6.2.2 mjf /* Check for the limits, and invalid values */
853 1.6.2.2 mjf if (nent < 1 || nent > aio_listio_max)
854 1.6.2.2 mjf return EINVAL;
855 1.6.2.2 mjf if (aio_jobs_count + nent > aio_max) /* XXXSMP */
856 1.6.2.2 mjf return EAGAIN;
857 1.6.2.2 mjf
858 1.6.2.2 mjf /* Check if AIO structure is initialized, if not - initialize it */
859 1.6.2.2 mjf if (p->p_aio == NULL)
860 1.6.2.2 mjf if (aio_init(p))
861 1.6.2.2 mjf return EAGAIN;
862 1.6.2.2 mjf aio = p->p_aio;
863 1.6.2.2 mjf
864 1.6.2.2 mjf /* Create a LIO structure */
865 1.6.2.2 mjf lio = pool_get(&aio_lio_pool, PR_WAITOK);
866 1.6.2.2 mjf lio->refcnt = 1;
867 1.6.2.2 mjf error = 0;
868 1.6.2.2 mjf
869 1.6.2.2 mjf switch (mode) {
870 1.6.2.2 mjf case LIO_WAIT:
871 1.6.2.2 mjf memset(&lio->sig, 0, sizeof(struct sigevent));
872 1.6.2.2 mjf break;
873 1.6.2.2 mjf case LIO_NOWAIT:
874 1.6.2.2 mjf /* Check for signal, validate it */
875 1.6.2.2 mjf if (SCARG(uap, sig)) {
876 1.6.2.2 mjf struct sigevent *sig = &lio->sig;
877 1.6.2.2 mjf
878 1.6.2.2 mjf error = copyin(SCARG(uap, sig), &lio->sig,
879 1.6.2.2 mjf sizeof(struct sigevent));
880 1.6.2.2 mjf if (error == 0 &&
881 1.6.2.2 mjf (sig->sigev_signo < 0 ||
882 1.6.2.2 mjf sig->sigev_signo >= NSIG ||
883 1.6.2.2 mjf sig->sigev_notify < SIGEV_NONE ||
884 1.6.2.2 mjf sig->sigev_notify > SIGEV_SA))
885 1.6.2.2 mjf error = EINVAL;
886 1.6.2.2 mjf } else
887 1.6.2.2 mjf memset(&lio->sig, 0, sizeof(struct sigevent));
888 1.6.2.2 mjf break;
889 1.6.2.2 mjf default:
890 1.6.2.2 mjf error = EINVAL;
891 1.6.2.2 mjf break;
892 1.6.2.2 mjf }
893 1.6.2.2 mjf
894 1.6.2.2 mjf if (error != 0) {
895 1.6.2.2 mjf pool_put(&aio_lio_pool, lio);
896 1.6.2.2 mjf return error;
897 1.6.2.2 mjf }
898 1.6.2.2 mjf
899 1.6.2.2 mjf /* Get the list from user-space */
900 1.6.2.2 mjf aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
901 1.6.2.2 mjf error = copyin(SCARG(uap, list), aiocbp_list,
902 1.6.2.2 mjf nent * sizeof(struct aiocb));
903 1.6.2.2 mjf if (error) {
904 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
905 1.6.2.2 mjf goto err;
906 1.6.2.2 mjf }
907 1.6.2.2 mjf
908 1.6.2.2 mjf /* Enqueue all jobs */
909 1.6.2.2 mjf errcnt = 0;
910 1.6.2.2 mjf for (i = 0; i < nent; i++) {
911 1.6.2.2 mjf error = aio_enqueue_job(AIO_LIO, aiocbp_list[i], lio);
912 1.6.2.2 mjf /*
913 1.6.2.2 mjf * According to POSIX, in such error case it may
914 1.6.2.2 mjf * fail with other I/O operations initiated.
915 1.6.2.2 mjf */
916 1.6.2.2 mjf if (error)
917 1.6.2.2 mjf errcnt++;
918 1.6.2.2 mjf }
919 1.6.2.2 mjf
920 1.6.2.2 mjf mutex_enter(&aio->aio_mtx);
921 1.6.2.2 mjf
922 1.6.2.2 mjf /* Return an error, if any */
923 1.6.2.2 mjf if (errcnt) {
924 1.6.2.2 mjf error = EIO;
925 1.6.2.2 mjf goto err;
926 1.6.2.2 mjf }
927 1.6.2.2 mjf
928 1.6.2.2 mjf if (mode == LIO_WAIT) {
929 1.6.2.2 mjf /*
930 1.6.2.2 mjf * Wait for AIO completion. In such case,
931 1.6.2.2 mjf * the LIO structure will be freed here.
932 1.6.2.2 mjf */
933 1.6.2.2 mjf while (lio->refcnt > 1 && error == 0)
934 1.6.2.2 mjf error = cv_wait_sig(&aio->done_cv, &aio->aio_mtx);
935 1.6.2.2 mjf if (error)
936 1.6.2.2 mjf error = EINTR;
937 1.6.2.2 mjf }
938 1.6.2.2 mjf
939 1.6.2.2 mjf err:
940 1.6.2.2 mjf if (--lio->refcnt != 0)
941 1.6.2.2 mjf lio = NULL;
942 1.6.2.2 mjf mutex_exit(&aio->aio_mtx);
943 1.6.2.2 mjf if (lio != NULL) {
944 1.6.2.2 mjf aio_sendsig(p, &lio->sig);
945 1.6.2.2 mjf pool_put(&aio_lio_pool, lio);
946 1.6.2.2 mjf }
947 1.6.2.2 mjf kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
948 1.6.2.2 mjf return error;
949 1.6.2.2 mjf }
950 1.6.2.2 mjf
951 1.6.2.2 mjf /*
952 1.6.2.2 mjf * SysCtl
953 1.6.2.2 mjf */
954 1.6.2.2 mjf
955 1.6.2.2 mjf static int
956 1.6.2.2 mjf sysctl_aio_listio_max(SYSCTLFN_ARGS)
957 1.6.2.2 mjf {
958 1.6.2.2 mjf struct sysctlnode node;
959 1.6.2.2 mjf int error, newsize;
960 1.6.2.2 mjf
961 1.6.2.2 mjf node = *rnode;
962 1.6.2.2 mjf node.sysctl_data = &newsize;
963 1.6.2.2 mjf
964 1.6.2.2 mjf newsize = aio_listio_max;
965 1.6.2.2 mjf error = sysctl_lookup(SYSCTLFN_CALL(&node));
966 1.6.2.2 mjf if (error || newp == NULL)
967 1.6.2.2 mjf return error;
968 1.6.2.2 mjf
969 1.6.2.2 mjf /* XXXSMP */
970 1.6.2.2 mjf if (newsize < 1 || newsize > aio_max)
971 1.6.2.2 mjf return EINVAL;
972 1.6.2.2 mjf aio_listio_max = newsize;
973 1.6.2.2 mjf
974 1.6.2.2 mjf return 0;
975 1.6.2.2 mjf }
976 1.6.2.2 mjf
977 1.6.2.2 mjf static int
978 1.6.2.2 mjf sysctl_aio_max(SYSCTLFN_ARGS)
979 1.6.2.2 mjf {
980 1.6.2.2 mjf struct sysctlnode node;
981 1.6.2.2 mjf int error, newsize;
982 1.6.2.2 mjf
983 1.6.2.2 mjf node = *rnode;
984 1.6.2.2 mjf node.sysctl_data = &newsize;
985 1.6.2.2 mjf
986 1.6.2.2 mjf newsize = aio_max;
987 1.6.2.2 mjf error = sysctl_lookup(SYSCTLFN_CALL(&node));
988 1.6.2.2 mjf if (error || newp == NULL)
989 1.6.2.2 mjf return error;
990 1.6.2.2 mjf
991 1.6.2.2 mjf /* XXXSMP */
992 1.6.2.2 mjf if (newsize < 1 || newsize < aio_listio_max)
993 1.6.2.2 mjf return EINVAL;
994 1.6.2.2 mjf aio_max = newsize;
995 1.6.2.2 mjf
996 1.6.2.2 mjf return 0;
997 1.6.2.2 mjf }
998 1.6.2.2 mjf
999 1.6.2.2 mjf SYSCTL_SETUP(sysctl_aio_setup, "sysctl aio setup")
1000 1.6.2.2 mjf {
1001 1.6.2.2 mjf
1002 1.6.2.2 mjf sysctl_createv(clog, 0, NULL, NULL,
1003 1.6.2.2 mjf CTLFLAG_PERMANENT,
1004 1.6.2.2 mjf CTLTYPE_NODE, "kern", NULL,
1005 1.6.2.2 mjf NULL, 0, NULL, 0,
1006 1.6.2.2 mjf CTL_KERN, CTL_EOL);
1007 1.6.2.2 mjf sysctl_createv(clog, 0, NULL, NULL,
1008 1.6.2.2 mjf CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
1009 1.6.2.2 mjf CTLTYPE_INT, "posix_aio",
1010 1.6.2.2 mjf SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
1011 1.6.2.2 mjf "Asynchronous I/O option to which the "
1012 1.6.2.2 mjf "system attempts to conform"),
1013 1.6.2.2 mjf NULL, _POSIX_ASYNCHRONOUS_IO, NULL, 0,
1014 1.6.2.2 mjf CTL_KERN, CTL_CREATE, CTL_EOL);
1015 1.6.2.2 mjf sysctl_createv(clog, 0, NULL, NULL,
1016 1.6.2.2 mjf CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1017 1.6.2.2 mjf CTLTYPE_INT, "aio_listio_max",
1018 1.6.2.2 mjf SYSCTL_DESCR("Maximum number of asynchronous I/O "
1019 1.6.2.2 mjf "operations in a single list I/O call"),
1020 1.6.2.2 mjf sysctl_aio_listio_max, 0, &aio_listio_max, 0,
1021 1.6.2.2 mjf CTL_KERN, CTL_CREATE, CTL_EOL);
1022 1.6.2.2 mjf sysctl_createv(clog, 0, NULL, NULL,
1023 1.6.2.2 mjf CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1024 1.6.2.2 mjf CTLTYPE_INT, "aio_max",
1025 1.6.2.2 mjf SYSCTL_DESCR("Maximum number of asynchronous I/O "
1026 1.6.2.2 mjf "operations"),
1027 1.6.2.2 mjf sysctl_aio_max, 0, &aio_max, 0,
1028 1.6.2.2 mjf CTL_KERN, CTL_CREATE, CTL_EOL);
1029 1.6.2.2 mjf }
1030 1.6.2.2 mjf
1031 1.6.2.2 mjf /*
1032 1.6.2.2 mjf * Debugging
1033 1.6.2.2 mjf */
1034 1.6.2.2 mjf #if defined(DDB)
1035 1.6.2.2 mjf void
1036 1.6.2.2 mjf aio_print_jobs(void (*pr)(const char *, ...))
1037 1.6.2.2 mjf {
1038 1.6.2.2 mjf struct proc *p = (curlwp == NULL ? NULL : curlwp->l_proc);
1039 1.6.2.2 mjf struct aioproc *aio;
1040 1.6.2.2 mjf struct aio_job *a_job;
1041 1.6.2.2 mjf struct aiocb *aiocbp;
1042 1.6.2.2 mjf
1043 1.6.2.2 mjf if (p == NULL) {
1044 1.6.2.2 mjf (*pr)("AIO: We are not in the processes right now.\n");
1045 1.6.2.2 mjf return;
1046 1.6.2.2 mjf }
1047 1.6.2.2 mjf
1048 1.6.2.2 mjf aio = p->p_aio;
1049 1.6.2.2 mjf if (aio == NULL) {
1050 1.6.2.2 mjf (*pr)("AIO data is not initialized (PID = %d).\n", p->p_pid);
1051 1.6.2.2 mjf return;
1052 1.6.2.2 mjf }
1053 1.6.2.2 mjf
1054 1.6.2.2 mjf (*pr)("AIO: PID = %d\n", p->p_pid);
1055 1.6.2.2 mjf (*pr)("AIO: Global count of the jobs = %u\n", aio_jobs_count);
1056 1.6.2.2 mjf (*pr)("AIO: Count of the jobs = %u\n", aio->jobs_count);
1057 1.6.2.2 mjf
1058 1.6.2.2 mjf if (aio->curjob) {
1059 1.6.2.2 mjf a_job = aio->curjob;
1060 1.6.2.2 mjf (*pr)("\nAIO current job:\n");
1061 1.6.2.2 mjf (*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
1062 1.6.2.2 mjf a_job->aio_op, a_job->aiocbp._errno,
1063 1.6.2.2 mjf a_job->aiocbp._state, a_job->aiocb_uptr);
1064 1.6.2.2 mjf aiocbp = &a_job->aiocbp;
1065 1.6.2.2 mjf (*pr)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
1066 1.6.2.2 mjf aiocbp->aio_fildes, aiocbp->aio_offset,
1067 1.6.2.2 mjf aiocbp->aio_buf, aiocbp->aio_nbytes);
1068 1.6.2.2 mjf }
1069 1.6.2.2 mjf
1070 1.6.2.2 mjf (*pr)("\nAIO queue:\n");
1071 1.6.2.2 mjf TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
1072 1.6.2.2 mjf (*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
1073 1.6.2.2 mjf a_job->aio_op, a_job->aiocbp._errno,
1074 1.6.2.2 mjf a_job->aiocbp._state, a_job->aiocb_uptr);
1075 1.6.2.2 mjf aiocbp = &a_job->aiocbp;
1076 1.6.2.2 mjf (*pr)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
1077 1.6.2.2 mjf aiocbp->aio_fildes, aiocbp->aio_offset,
1078 1.6.2.2 mjf aiocbp->aio_buf, aiocbp->aio_nbytes);
1079 1.6.2.2 mjf }
1080 1.6.2.2 mjf }
1081 1.6.2.2 mjf #endif /* defined(DDB) */
1082