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