kernel - Major MPSAFE Infrastructure
[dragonfly.git] / sys / kern / vfs_aio.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997 John S. Dyson. 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. John S. Dyson's name may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
11 *
12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
13 * bad that happens because of using this software isn't the responsibility
14 * of the author. This software is distributed AS-IS.
15 *
16 * $FreeBSD: src/sys/kern/vfs_aio.c,v 1.70.2.28 2003/05/29 06:15:35 alc Exp $
c34665ce 17 * $DragonFly: src/sys/kern/vfs_aio.c,v 1.42 2007/07/20 17:21:52 dillon Exp $
984263bc
MD
18 */
19
20/*
21 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
22 */
23
24#include <sys/param.h>
25#include <sys/systm.h>
26#include <sys/buf.h>
27#include <sys/sysproto.h>
28#include <sys/filedesc.h>
29#include <sys/kernel.h>
30#include <sys/fcntl.h>
31#include <sys/file.h>
32#include <sys/lock.h>
33#include <sys/unistd.h>
34#include <sys/proc.h>
35#include <sys/resourcevar.h>
36#include <sys/signalvar.h>
37#include <sys/protosw.h>
38#include <sys/socketvar.h>
39#include <sys/sysctl.h>
40#include <sys/vnode.h>
41#include <sys/conf.h>
42#include <sys/event.h>
43
44#include <vm/vm.h>
45#include <vm/vm_extern.h>
46#include <vm/pmap.h>
47#include <vm/vm_map.h>
48#include <vm/vm_zone.h>
49#include <sys/aio.h>
684a93c4 50
7b95be2a
MD
51#include <sys/file2.h>
52#include <sys/buf2.h>
ba39e2e0 53#include <sys/sysref2.h>
831f78e5 54#include <sys/thread2.h>
684a93c4 55#include <sys/mplock2.h>
984263bc
MD
56
57#include <machine/limits.h>
58#include "opt_vfs_aio.h"
59
60#ifdef VFS_AIO
61
62/*
63 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
64 * overflow.
65 */
66static long jobrefid;
67
68#define JOBST_NULL 0x0
69#define JOBST_JOBQGLOBAL 0x2
70#define JOBST_JOBRUNNING 0x3
71#define JOBST_JOBFINISHED 0x4
72#define JOBST_JOBQBUF 0x5
73#define JOBST_JOBBFINISHED 0x6
74
75#ifndef MAX_AIO_PER_PROC
76#define MAX_AIO_PER_PROC 32
77#endif
78
79#ifndef MAX_AIO_QUEUE_PER_PROC
80#define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
81#endif
82
83#ifndef MAX_AIO_PROCS
84#define MAX_AIO_PROCS 32
85#endif
86
87#ifndef MAX_AIO_QUEUE
88#define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
89#endif
90
91#ifndef TARGET_AIO_PROCS
92#define TARGET_AIO_PROCS 4
93#endif
94
95#ifndef MAX_BUF_AIO
96#define MAX_BUF_AIO 16
97#endif
98
99#ifndef AIOD_TIMEOUT_DEFAULT
100#define AIOD_TIMEOUT_DEFAULT (10 * hz)
101#endif
102
103#ifndef AIOD_LIFETIME_DEFAULT
104#define AIOD_LIFETIME_DEFAULT (30 * hz)
105#endif
106
107SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
108
109static int max_aio_procs = MAX_AIO_PROCS;
110SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
111 CTLFLAG_RW, &max_aio_procs, 0,
112 "Maximum number of kernel threads to use for handling async IO");
113
114static int num_aio_procs = 0;
115SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
116 CTLFLAG_RD, &num_aio_procs, 0,
117 "Number of presently active kernel threads for async IO");
118
119/*
120 * The code will adjust the actual number of AIO processes towards this
121 * number when it gets a chance.
122 */
123static int target_aio_procs = TARGET_AIO_PROCS;
124SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
125 0, "Preferred number of ready kernel threads for async IO");
126
127static int max_queue_count = MAX_AIO_QUEUE;
128SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
129 "Maximum number of aio requests to queue, globally");
130
131static int num_queue_count = 0;
132SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
133 "Number of queued aio requests");
134
135static int num_buf_aio = 0;
136SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
137 "Number of aio requests presently handled by the buf subsystem");
138
139/* Number of async I/O thread in the process of being started */
140/* XXX This should be local to _aio_aqueue() */
141static int num_aio_resv_start = 0;
142
143static int aiod_timeout;
144SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
145 "Timeout value for synchronous aio operations");
146
147static int aiod_lifetime;
148SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
149 "Maximum lifetime for idle aiod");
150
151static int max_aio_per_proc = MAX_AIO_PER_PROC;
152SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
153 0, "Maximum active aio requests per process (stored in the process)");
154
155static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
156SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
157 &max_aio_queue_per_proc, 0,
158 "Maximum queued aio requests per process (stored in the process)");
159
160static int max_buf_aio = MAX_BUF_AIO;
161SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
162 "Maximum buf aio requests per process (stored in the process)");
163
164/*
165 * AIO process info
166 */
167#define AIOP_FREE 0x1 /* proc on free queue */
168#define AIOP_SCHED 0x2 /* proc explicitly scheduled */
169
170struct aioproclist {
171 int aioprocflags; /* AIO proc flags */
172 TAILQ_ENTRY(aioproclist) list; /* List of processes */
173 struct proc *aioproc; /* The AIO thread */
174};
175
176/*
177 * data-structure for lio signal management
178 */
179struct aio_liojob {
180 int lioj_flags;
181 int lioj_buffer_count;
182 int lioj_buffer_finished_count;
183 int lioj_queue_count;
184 int lioj_queue_finished_count;
185 struct sigevent lioj_signal; /* signal on all I/O done */
186 TAILQ_ENTRY(aio_liojob) lioj_list;
187 struct kaioinfo *lioj_ki;
188};
189#define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
190#define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
191
192/*
193 * per process aio data structure
194 */
195struct kaioinfo {
196 int kaio_flags; /* per process kaio flags */
197 int kaio_maxactive_count; /* maximum number of AIOs */
198 int kaio_active_count; /* number of currently used AIOs */
199 int kaio_qallowed_count; /* maxiumu size of AIO queue */
200 int kaio_queue_count; /* size of AIO queue */
201 int kaio_ballowed_count; /* maximum number of buffers */
202 int kaio_queue_finished_count; /* number of daemon jobs finished */
203 int kaio_buffer_count; /* number of physio buffers */
204 int kaio_buffer_finished_count; /* count of I/O done */
205 struct proc *kaio_p; /* process that uses this kaio block */
206 TAILQ_HEAD(,aio_liojob) kaio_liojoblist; /* list of lio jobs */
207 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* job queue for process */
208 TAILQ_HEAD(,aiocblist) kaio_jobdone; /* done queue for process */
209 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* buffer job queue for process */
210 TAILQ_HEAD(,aiocblist) kaio_bufdone; /* buffer done queue for process */
211 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */
212};
213
214#define KAIO_RUNDOWN 0x1 /* process is being run down */
215#define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
216
217static TAILQ_HEAD(,aioproclist) aio_freeproc, aio_activeproc;
218static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */
219static TAILQ_HEAD(,aiocblist) aio_bufjobs; /* Phys I/O job list */
220static TAILQ_HEAD(,aiocblist) aio_freejobs; /* Pool of free jobs */
221
222static void aio_init_aioinfo(struct proc *p);
223static void aio_onceonly(void *);
224static int aio_free_entry(struct aiocblist *aiocbe);
225static void aio_process(struct aiocblist *aiocbe);
226static int aio_newproc(void);
41c20dac 227static int aio_aqueue(struct aiocb *job, int type);
81b5c339 228static void aio_physwakeup(struct bio *bio);
984263bc
MD
229static int aio_fphysio(struct aiocblist *aiocbe);
230static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
11aec7f4 231static void aio_daemon(void *uproc, struct trapframe *frame);
984263bc
MD
232static void process_signal(void *aioj);
233
234SYSINIT(aio, SI_SUB_VFS, SI_ORDER_ANY, aio_onceonly, NULL);
235
236/*
237 * Zones for:
238 * kaio Per process async io info
239 * aiop async io thread data
240 * aiocb async io jobs
241 * aiol list io job pointer - internal to aio_suspend XXX
242 * aiolio list io jobs
243 */
244static vm_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
245
246/*
247 * Startup initialization
248 */
249static void
250aio_onceonly(void *na)
251{
252 TAILQ_INIT(&aio_freeproc);
253 TAILQ_INIT(&aio_activeproc);
254 TAILQ_INIT(&aio_jobs);
255 TAILQ_INIT(&aio_bufjobs);
256 TAILQ_INIT(&aio_freejobs);
257 kaio_zone = zinit("AIO", sizeof(struct kaioinfo), 0, 0, 1);
258 aiop_zone = zinit("AIOP", sizeof(struct aioproclist), 0, 0, 1);
259 aiocb_zone = zinit("AIOCB", sizeof(struct aiocblist), 0, 0, 1);
260 aiol_zone = zinit("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t), 0, 0, 1);
261 aiolio_zone = zinit("AIOLIO", sizeof(struct aio_liojob), 0, 0, 1);
262 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
263 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
264 jobrefid = 1;
265}
266
267/*
268 * Init the per-process aioinfo structure. The aioinfo limits are set
269 * per-process for user limit (resource) management.
270 */
271static void
272aio_init_aioinfo(struct proc *p)
273{
274 struct kaioinfo *ki;
275 if (p->p_aioinfo == NULL) {
276 ki = zalloc(kaio_zone);
277 p->p_aioinfo = ki;
278 ki->kaio_flags = 0;
279 ki->kaio_maxactive_count = max_aio_per_proc;
280 ki->kaio_active_count = 0;
281 ki->kaio_qallowed_count = max_aio_queue_per_proc;
282 ki->kaio_queue_count = 0;
283 ki->kaio_ballowed_count = max_buf_aio;
284 ki->kaio_buffer_count = 0;
285 ki->kaio_buffer_finished_count = 0;
286 ki->kaio_p = p;
287 TAILQ_INIT(&ki->kaio_jobdone);
288 TAILQ_INIT(&ki->kaio_jobqueue);
289 TAILQ_INIT(&ki->kaio_bufdone);
290 TAILQ_INIT(&ki->kaio_bufqueue);
291 TAILQ_INIT(&ki->kaio_liojoblist);
292 TAILQ_INIT(&ki->kaio_sockqueue);
293 }
294
295 while (num_aio_procs < target_aio_procs)
296 aio_newproc();
297}
298
299/*
300 * Free a job entry. Wait for completion if it is currently active, but don't
301 * delay forever. If we delay, we return a flag that says that we have to
302 * restart the queue scan.
303 */
304static int
305aio_free_entry(struct aiocblist *aiocbe)
306{
307 struct kaioinfo *ki;
308 struct aio_liojob *lj;
309 struct proc *p;
310 int error;
984263bc
MD
311
312 if (aiocbe->jobstate == JOBST_NULL)
313 panic("aio_free_entry: freeing already free job");
314
315 p = aiocbe->userproc;
316 ki = p->p_aioinfo;
317 lj = aiocbe->lio;
318 if (ki == NULL)
319 panic("aio_free_entry: missing p->p_aioinfo");
320
321 while (aiocbe->jobstate == JOBST_JOBRUNNING) {
322 aiocbe->jobflags |= AIOCBLIST_RUNDOWN;
377d4740 323 tsleep(aiocbe, 0, "jobwai", 0);
984263bc
MD
324 }
325 if (aiocbe->bp == NULL) {
326 if (ki->kaio_queue_count <= 0)
327 panic("aio_free_entry: process queue size <= 0");
328 if (num_queue_count <= 0)
329 panic("aio_free_entry: system wide queue size <= 0");
330
331 if (lj) {
332 lj->lioj_queue_count--;
333 if (aiocbe->jobflags & AIOCBLIST_DONE)
334 lj->lioj_queue_finished_count--;
335 }
336 ki->kaio_queue_count--;
337 if (aiocbe->jobflags & AIOCBLIST_DONE)
338 ki->kaio_queue_finished_count--;
339 num_queue_count--;
340 } else {
341 if (lj) {
342 lj->lioj_buffer_count--;
343 if (aiocbe->jobflags & AIOCBLIST_DONE)
344 lj->lioj_buffer_finished_count--;
345 }
346 if (aiocbe->jobflags & AIOCBLIST_DONE)
347 ki->kaio_buffer_finished_count--;
348 ki->kaio_buffer_count--;
349 num_buf_aio--;
350 }
351
352 /* aiocbe is going away, we need to destroy any knotes */
08f2f1bb 353 /* XXX lwp knote wants a thread, but only cares about the process */
5b22f1a7 354 knote_empty(&aiocbe->klist);
984263bc
MD
355
356 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN)
357 && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) {
358 ki->kaio_flags &= ~KAIO_WAKEUP;
359 wakeup(p);
360 }
361
362 if (aiocbe->jobstate == JOBST_JOBQBUF) {
363 if ((error = aio_fphysio(aiocbe)) != 0)
364 return error;
365 if (aiocbe->jobstate != JOBST_JOBBFINISHED)
366 panic("aio_free_entry: invalid physio finish-up state");
e43a034f 367 crit_enter();
984263bc 368 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
e43a034f 369 crit_exit();
984263bc 370 } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) {
e43a034f 371 crit_enter();
984263bc
MD
372 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
373 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
e43a034f 374 crit_exit();
984263bc
MD
375 } else if (aiocbe->jobstate == JOBST_JOBFINISHED)
376 TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist);
377 else if (aiocbe->jobstate == JOBST_JOBBFINISHED) {
e43a034f 378 crit_enter();
984263bc 379 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
e43a034f 380 crit_exit();
984263bc
MD
381 if (aiocbe->bp) {
382 vunmapbuf(aiocbe->bp);
383 relpbuf(aiocbe->bp, NULL);
384 aiocbe->bp = NULL;
385 }
386 }
387 if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) {
388 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
389 zfree(aiolio_zone, lj);
390 }
391 aiocbe->jobstate = JOBST_NULL;
a5eb27b6 392 callout_stop(&aiocbe->timeout);
9f87144f 393 fdrop(aiocbe->fd_file);
984263bc
MD
394 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
395 return 0;
396}
397#endif /* VFS_AIO */
398
399/*
400 * Rundown the jobs for a given process.
401 */
402void
403aio_proc_rundown(struct proc *p)
404{
405#ifndef VFS_AIO
406 return;
407#else
984263bc
MD
408 struct kaioinfo *ki;
409 struct aio_liojob *lj, *ljn;
410 struct aiocblist *aiocbe, *aiocbn;
411 struct file *fp;
412 struct socket *so;
413
414 ki = p->p_aioinfo;
415 if (ki == NULL)
416 return;
417
418 ki->kaio_flags |= LIOJ_SIGNAL_POSTED;
419 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count >
420 ki->kaio_buffer_finished_count)) {
421 ki->kaio_flags |= KAIO_RUNDOWN;
377d4740 422 if (tsleep(p, 0, "kaiowt", aiod_timeout))
984263bc
MD
423 break;
424 }
425
426 /*
427 * Move any aio ops that are waiting on socket I/O to the normal job
428 * queues so they are cleaned up with any others.
429 */
e43a034f 430 crit_enter();
984263bc
MD
431 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe =
432 aiocbn) {
433 aiocbn = TAILQ_NEXT(aiocbe, plist);
434 fp = aiocbe->fd_file;
435 if (fp != NULL) {
436 so = (struct socket *)fp->f_data;
437 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list);
438 if (TAILQ_EMPTY(&so->so_aiojobq)) {
6d49aa6f
MD
439 so->so_snd.ssb_flags &= ~SSB_AIO;
440 so->so_rcv.ssb_flags &= ~SSB_AIO;
984263bc
MD
441 }
442 }
443 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist);
444 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list);
445 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist);
446 }
e43a034f 447 crit_exit();
984263bc
MD
448
449restart1:
450 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) {
451 aiocbn = TAILQ_NEXT(aiocbe, plist);
452 if (aio_free_entry(aiocbe))
453 goto restart1;
454 }
455
456restart2:
457 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe =
458 aiocbn) {
459 aiocbn = TAILQ_NEXT(aiocbe, plist);
460 if (aio_free_entry(aiocbe))
461 goto restart2;
462 }
463
984263bc 464restart3:
e43a034f 465 crit_enter();
984263bc
MD
466 while (TAILQ_FIRST(&ki->kaio_bufqueue)) {
467 ki->kaio_flags |= KAIO_WAKEUP;
377d4740 468 tsleep(p, 0, "aioprn", 0);
e43a034f 469 crit_exit();
984263bc
MD
470 goto restart3;
471 }
e43a034f 472 crit_exit();
984263bc
MD
473
474restart4:
e43a034f 475 crit_enter();
984263bc
MD
476 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) {
477 aiocbn = TAILQ_NEXT(aiocbe, plist);
478 if (aio_free_entry(aiocbe)) {
e43a034f 479 crit_exit();
984263bc
MD
480 goto restart4;
481 }
482 }
e43a034f 483 crit_exit();
984263bc
MD
484
485 /*
486 * If we've slept, jobs might have moved from one queue to another.
487 * Retry rundown if we didn't manage to empty the queues.
488 */
489 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL ||
490 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL ||
491 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL ||
492 TAILQ_FIRST(&ki->kaio_bufdone) != NULL)
493 goto restart1;
494
495 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) {
496 ljn = TAILQ_NEXT(lj, lioj_list);
497 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count ==
498 0)) {
499 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
500 zfree(aiolio_zone, lj);
501 } else {
502#ifdef DIAGNOSTIC
6ea70f76 503 kprintf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, "
984263bc
MD
504 "QF:%d\n", lj->lioj_buffer_count,
505 lj->lioj_buffer_finished_count,
506 lj->lioj_queue_count,
507 lj->lioj_queue_finished_count);
508#endif
509 }
510 }
511
512 zfree(kaio_zone, ki);
513 p->p_aioinfo = NULL;
514#endif /* VFS_AIO */
515}
516
517#ifdef VFS_AIO
518/*
519 * Select a job to run (called by an AIO daemon).
520 */
521static struct aiocblist *
522aio_selectjob(struct aioproclist *aiop)
523{
984263bc
MD
524 struct aiocblist *aiocbe;
525 struct kaioinfo *ki;
526 struct proc *userp;
527
e43a034f 528 crit_enter();
984263bc
MD
529 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe =
530 TAILQ_NEXT(aiocbe, list)) {
531 userp = aiocbe->userproc;
532 ki = userp->p_aioinfo;
533
534 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
535 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
e43a034f 536 crit_exit();
984263bc
MD
537 return aiocbe;
538 }
539 }
e43a034f 540 crit_exit();
984263bc
MD
541
542 return NULL;
543}
544
545/*
546 * The AIO processing activity. This is the code that does the I/O request for
547 * the non-physio version of the operations. The normal vn operations are used,
548 * and this code should work in all instances for every type of file, including
549 * pipes, sockets, fifos, and regular files.
550 */
551static void
552aio_process(struct aiocblist *aiocbe)
553{
7b95be2a 554 struct thread *mytd;
984263bc
MD
555 struct aiocb *cb;
556 struct file *fp;
557 struct uio auio;
558 struct iovec aiov;
559 int cnt;
560 int error;
561 int oublock_st, oublock_end;
562 int inblock_st, inblock_end;
563
7b95be2a 564 mytd = curthread;
984263bc
MD
565 cb = &aiocbe->uaiocb;
566 fp = aiocbe->fd_file;
567
568 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
569 aiov.iov_len = cb->aio_nbytes;
570
571 auio.uio_iov = &aiov;
572 auio.uio_iovcnt = 1;
573 auio.uio_offset = cb->aio_offset;
574 auio.uio_resid = cb->aio_nbytes;
575 cnt = cb->aio_nbytes;
576 auio.uio_segflg = UIO_USERSPACE;
7b95be2a 577 auio.uio_td = mytd;
984263bc 578
fde7ac71
SS
579 inblock_st = mytd->td_lwp->lwp_ru.ru_inblock;
580 oublock_st = mytd->td_lwp->lwp_ru.ru_oublock;
984263bc
MD
581 /*
582 * _aio_aqueue() acquires a reference to the file that is
583 * released in aio_free_entry().
584 */
585 if (cb->aio_lio_opcode == LIO_READ) {
586 auio.uio_rw = UIO_READ;
9ba76b73 587 error = fo_read(fp, &auio, fp->f_cred, O_FOFFSET);
984263bc
MD
588 } else {
589 auio.uio_rw = UIO_WRITE;
9ba76b73 590 error = fo_write(fp, &auio, fp->f_cred, O_FOFFSET);
984263bc 591 }
fde7ac71
SS
592 inblock_end = mytd->td_lwp->lwp_ru.ru_inblock;
593 oublock_end = mytd->td_lwp->lwp_ru.ru_oublock;
984263bc
MD
594
595 aiocbe->inputcharge = inblock_end - inblock_st;
596 aiocbe->outputcharge = oublock_end - oublock_st;
597
598 if ((error) && (auio.uio_resid != cnt)) {
599 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
600 error = 0;
601 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE))
84204577 602 ksignal(aiocbe->userproc, SIGPIPE);
984263bc
MD
603 }
604
605 cnt -= auio.uio_resid;
606 cb->_aiocb_private.error = error;
607 cb->_aiocb_private.status = cnt;
608}
609
610/*
611 * The AIO daemon, most of the actual work is done in aio_process,
612 * but the setup (and address space mgmt) is done in this routine.
8a8d5d85
MD
613 *
614 * The MP lock is held on entry.
984263bc
MD
615 */
616static void
91bd9c1e 617aio_daemon(void *uproc, struct trapframe *frame)
984263bc 618{
984263bc
MD
619 struct aio_liojob *lj;
620 struct aiocb *cb;
621 struct aiocblist *aiocbe;
622 struct aioproclist *aiop;
623 struct kaioinfo *ki;
287ebb09
MD
624 struct proc *mycp, *userp;
625 struct vmspace *curvm;
626 struct lwp *mylwp;
e9a372eb 627 struct ucred *cr;
984263bc 628
287ebb09
MD
629 mylwp = curthread->td_lwp;
630 mycp = mylwp->lwp_proc;
984263bc
MD
631
632 if (mycp->p_textvp) {
633 vrele(mycp->p_textvp);
634 mycp->p_textvp = NULL;
635 }
636
637 /*
638 * Allocate and ready the aio control info. There is one aiop structure
639 * per daemon.
640 */
641 aiop = zalloc(aiop_zone);
642 aiop->aioproc = mycp;
643 aiop->aioprocflags |= AIOP_FREE;
644
e43a034f 645 crit_enter();
984263bc
MD
646
647 /*
648 * Place thread (lightweight process) onto the AIO free thread list.
649 */
650 if (TAILQ_EMPTY(&aio_freeproc))
651 wakeup(&aio_freeproc);
652 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
653
e43a034f 654 crit_exit();
984263bc
MD
655
656 /* Make up a name for the daemon. */
657 strcpy(mycp->p_comm, "aiod");
658
659 /*
660 * Get rid of our current filedescriptors. AIOD's don't need any
661 * filedescriptors, except as temporarily inherited from the client.
662 * Credentials are also cloned, and made equivalent to "root".
663 */
0a4a9c77 664 fdfree(mycp, NULL);
e9a372eb
MD
665 cr = cratom(&mycp->p_ucred);
666 cr->cr_uid = 0;
6eedb489 667 uireplace(&cr->cr_uidinfo, uifind(0));
e9a372eb
MD
668 cr->cr_ngroups = 1;
669 cr->cr_groups[0] = 1;
984263bc
MD
670
671 /* The daemon resides in its own pgrp. */
672 enterpgrp(mycp, mycp->p_pid, 1);
673
674 /* Mark special process type. */
675 mycp->p_flag |= P_SYSTEM | P_KTHREADP;
676
677 /*
678 * Wakeup parent process. (Parent sleeps to keep from blasting away
679 * and creating too many daemons.)
680 */
681 wakeup(mycp);
287ebb09 682 curvm = NULL;
984263bc
MD
683
684 for (;;) {
984263bc
MD
685 /*
686 * Take daemon off of free queue
687 */
688 if (aiop->aioprocflags & AIOP_FREE) {
e43a034f 689 crit_enter();
984263bc
MD
690 TAILQ_REMOVE(&aio_freeproc, aiop, list);
691 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
692 aiop->aioprocflags &= ~AIOP_FREE;
e43a034f 693 crit_exit();
984263bc
MD
694 }
695 aiop->aioprocflags &= ~AIOP_SCHED;
696
697 /*
698 * Check for jobs.
699 */
700 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
701 cb = &aiocbe->uaiocb;
702 userp = aiocbe->userproc;
703
704 aiocbe->jobstate = JOBST_JOBRUNNING;
705
706 /*
707 * Connect to process address space for user program.
708 */
287ebb09
MD
709 if (curvm != userp->p_vmspace) {
710 pmap_setlwpvm(mylwp, userp->p_vmspace);
711 if (curvm)
712 sysref_put(&curvm->vm_sysref);
713 curvm = userp->p_vmspace;
714 sysref_get(&curvm->vm_sysref);
984263bc
MD
715 }
716
717 ki = userp->p_aioinfo;
718 lj = aiocbe->lio;
719
720 /* Account for currently active jobs. */
721 ki->kaio_active_count++;
722
723 /* Do the I/O function. */
724 aio_process(aiocbe);
725
726 /* Decrement the active job count. */
727 ki->kaio_active_count--;
728
729 /*
730 * Increment the completion count for wakeup/signal
731 * comparisons.
732 */
733 aiocbe->jobflags |= AIOCBLIST_DONE;
734 ki->kaio_queue_finished_count++;
735 if (lj)
736 lj->lioj_queue_finished_count++;
737 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
738 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
739 ki->kaio_flags &= ~KAIO_WAKEUP;
740 wakeup(userp);
741 }
742
e43a034f 743 crit_enter();
984263bc
MD
744 if (lj && (lj->lioj_flags &
745 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
746 if ((lj->lioj_queue_finished_count ==
747 lj->lioj_queue_count) &&
748 (lj->lioj_buffer_finished_count ==
749 lj->lioj_buffer_count)) {
84204577 750 ksignal(userp,
984263bc
MD
751 lj->lioj_signal.sigev_signo);
752 lj->lioj_flags |=
753 LIOJ_SIGNAL_POSTED;
754 }
755 }
e43a034f 756 crit_exit();
984263bc
MD
757
758 aiocbe->jobstate = JOBST_JOBFINISHED;
759
e43a034f 760 crit_enter();
984263bc
MD
761 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
762 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist);
e43a034f 763 crit_exit();
984263bc
MD
764 KNOTE(&aiocbe->klist, 0);
765
766 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
767 wakeup(aiocbe);
768 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
769 }
770
771 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
84204577 772 ksignal(userp, cb->aio_sigevent.sigev_signo);
984263bc
MD
773 }
774 }
775
776 /*
777 * Disconnect from user address space.
778 */
287ebb09 779 if (curvm) {
e3161323 780 /* swap our original address space back in */
287ebb09
MD
781 pmap_setlwpvm(mylwp, mycp->p_vmspace);
782 sysref_put(&curvm->vm_sysref);
783 curvm = NULL;
984263bc
MD
784 }
785
786 /*
787 * If we are the first to be put onto the free queue, wakeup
788 * anyone waiting for a daemon.
789 */
e43a034f 790 crit_enter();
984263bc
MD
791 TAILQ_REMOVE(&aio_activeproc, aiop, list);
792 if (TAILQ_EMPTY(&aio_freeproc))
793 wakeup(&aio_freeproc);
794 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
795 aiop->aioprocflags |= AIOP_FREE;
e43a034f 796 crit_exit();
984263bc
MD
797
798 /*
799 * If daemon is inactive for a long time, allow it to exit,
800 * thereby freeing resources.
801 */
802 if (((aiop->aioprocflags & AIOP_SCHED) == 0) && tsleep(mycp,
377d4740 803 0, "aiordy", aiod_lifetime)) {
e43a034f 804 crit_enter();
984263bc
MD
805 if (TAILQ_EMPTY(&aio_jobs)) {
806 if ((aiop->aioprocflags & AIOP_FREE) &&
807 (num_aio_procs > target_aio_procs)) {
808 TAILQ_REMOVE(&aio_freeproc, aiop, list);
e43a034f 809 crit_exit();
984263bc
MD
810 zfree(aiop_zone, aiop);
811 num_aio_procs--;
812#ifdef DIAGNOSTIC
e3161323 813 if (mycp->p_vmspace->vm_sysref.refcnt <= 1) {
6ea70f76 814 kprintf("AIOD: bad vm refcnt for"
984263bc 815 " exiting daemon: %d\n",
e3161323 816 mycp->p_vmspace->vm_sysref.refcnt);
984263bc
MD
817 }
818#endif
7b95be2a 819 exit1(0);
984263bc
MD
820 }
821 }
e43a034f 822 crit_exit();
984263bc
MD
823 }
824 }
825}
826
827/*
828 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
829 * AIO daemon modifies its environment itself.
830 */
831static int
c972a82f 832aio_newproc(void)
984263bc
MD
833{
834 int error;
bb3cd951 835 struct lwp *lp, *nlp;
553ea3c8 836 struct proc *np;
984263bc 837
08f2f1bb 838 lp = &lwp0;
553ea3c8 839 error = fork1(lp, RFPROC|RFMEM|RFNOWAIT, &np);
984263bc
MD
840 if (error)
841 return error;
08f2f1bb 842 nlp = ONLY_LWP_IN_PROC(np);
bb3cd951 843 cpu_set_fork_handler(nlp, aio_daemon, curproc);
553ea3c8 844 start_forked_proc(lp, np);
984263bc
MD
845
846 /*
847 * Wait until daemon is started, but continue on just in case to
848 * handle error conditions.
849 */
377d4740 850 error = tsleep(np, 0, "aiosta", aiod_timeout);
984263bc
MD
851 num_aio_procs++;
852
853 return error;
854}
855
856/*
857 * Try the high-performance, low-overhead physio method for eligible
858 * VCHR devices. This method doesn't use an aio helper thread, and
859 * thus has very low overhead.
860 *
861 * Assumes that the caller, _aio_aqueue(), has incremented the file
862 * structure's reference count, preventing its deallocation for the
863 * duration of this call.
864 */
865static int
866aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
867{
868 int error;
869 struct aiocb *cb;
870 struct file *fp;
871 struct buf *bp;
872 struct vnode *vp;
873 struct kaioinfo *ki;
874 struct aio_liojob *lj;
984263bc
MD
875 int notify;
876
877 cb = &aiocbe->uaiocb;
878 fp = aiocbe->fd_file;
879
880 if (fp->f_type != DTYPE_VNODE)
881 return (-1);
882
883 vp = (struct vnode *)fp->f_data;
884
885 /*
886 * If its not a disk, we don't want to return a positive error.
887 * It causes the aio code to not fall through to try the thread
888 * way when you're talking to a regular file.
889 */
890 if (!vn_isdisk(vp, &error)) {
891 if (error == ENOTBLK)
892 return (-1);
893 else
894 return (error);
895 }
896
897 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys)
898 return (-1);
899
900 if (cb->aio_nbytes >
901 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
902 return (-1);
903
904 ki = p->p_aioinfo;
905 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
906 return (-1);
907
908 ki->kaio_buffer_count++;
909
910 lj = aiocbe->lio;
911 if (lj)
912 lj->lioj_buffer_count++;
913
914 /* Create and build a buffer header for a transfer. */
9a82e536 915 bp = getpbuf_kva(NULL);
984263bc
MD
916 BUF_KERNPROC(bp);
917
918 /*
919 * Get a copy of the kva from the physical buffer.
920 */
81b5c339 921 bp->b_bio1.bio_caller_info1.ptr = p;
984263bc
MD
922 error = 0;
923
10f3fee5
MD
924 bp->b_cmd = (cb->aio_lio_opcode == LIO_WRITE) ?
925 BUF_CMD_WRITE : BUF_CMD_READ;
81b5c339 926 bp->b_bio1.bio_done = aio_physwakeup;
ae8e83e6 927 bp->b_bio1.bio_flags |= BIO_SYNC;
54078292 928 bp->b_bio1.bio_offset = cb->aio_offset;
984263bc
MD
929
930 /* Bring buffer into kernel space. */
3591bbc6 931 if (vmapbuf(bp, __DEVOLATILE(char *, cb->aio_buf), cb->aio_nbytes) < 0) {
984263bc
MD
932 error = EFAULT;
933 goto doerror;
934 }
935
e43a034f
MD
936 crit_enter();
937
984263bc 938 aiocbe->bp = bp;
81b5c339 939 bp->b_bio1.bio_caller_info2.ptr = aiocbe;
984263bc
MD
940 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list);
941 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
942 aiocbe->jobstate = JOBST_JOBQBUF;
943 cb->_aiocb_private.status = cb->aio_nbytes;
944 num_buf_aio++;
945 bp->b_error = 0;
946
e43a034f 947 crit_exit();
984263bc 948
c34665ce
MD
949 /*
950 * Perform the transfer. vn_strategy must be used even though we
951 * know we have a device in order to deal with requests which exceed
952 * device DMA limitations.
953 */
954 vn_strategy(vp, &bp->b_bio1);
984263bc
MD
955
956 notify = 0;
e43a034f 957 crit_enter();
984263bc 958
ae8e83e6 959#if 0
984263bc
MD
960 /*
961 * If we had an error invoking the request, or an error in processing
962 * the request before we have returned, we process it as an error in
963 * transfer. Note that such an I/O error is not indicated immediately,
964 * but is returned using the aio_error mechanism. In this case,
965 * aio_suspend will return immediately.
966 */
967 if (bp->b_error || (bp->b_flags & B_ERROR)) {
968 struct aiocb *job = aiocbe->uuaiocb;
969
970 aiocbe->uaiocb._aiocb_private.status = 0;
971 suword(&job->_aiocb_private.status, 0);
972 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
973 suword(&job->_aiocb_private.error, bp->b_error);
974
975 ki->kaio_buffer_finished_count++;
976
977 if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
978 aiocbe->jobstate = JOBST_JOBBFINISHED;
979 aiocbe->jobflags |= AIOCBLIST_DONE;
980 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
981 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
982 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
983 notify = 1;
984 }
985 }
ae8e83e6 986#endif
e43a034f 987 crit_exit();
984263bc
MD
988 if (notify)
989 KNOTE(&aiocbe->klist, 0);
990 return 0;
991
992doerror:
993 ki->kaio_buffer_count--;
994 if (lj)
995 lj->lioj_buffer_count--;
996 aiocbe->bp = NULL;
997 relpbuf(bp, NULL);
998 return error;
999}
1000
1001/*
1002 * This waits/tests physio completion.
1003 */
1004static int
1005aio_fphysio(struct aiocblist *iocb)
1006{
984263bc
MD
1007 struct buf *bp;
1008 int error;
1009
1010 bp = iocb->bp;
1011
ae8e83e6 1012 error = biowait_timeout(&bp->b_bio1, "physstr", aiod_timeout);
1a493ad9
SW
1013 if (error == EWOULDBLOCK)
1014 return EINPROGRESS;
984263bc
MD
1015
1016 /* Release mapping into kernel space. */
1017 vunmapbuf(bp);
1018 iocb->bp = 0;
1019
1020 error = 0;
1021
1022 /* Check for an error. */
1023 if (bp->b_flags & B_ERROR)
1024 error = bp->b_error;
1025
1026 relpbuf(bp, NULL);
1027 return (error);
1028}
1029#endif /* VFS_AIO */
1030
1031/*
1032 * Wake up aio requests that may be serviceable now.
1033 */
1034void
6d49aa6f 1035aio_swake(struct socket *so, struct signalsockbuf *ssb)
984263bc
MD
1036{
1037#ifndef VFS_AIO
1038 return;
1039#else
1040 struct aiocblist *cb,*cbn;
1041 struct proc *p;
1042 struct kaioinfo *ki = NULL;
1043 int opcode, wakecount = 0;
1044 struct aioproclist *aiop;
1045
6d49aa6f 1046 if (ssb == &so->so_snd) {
984263bc 1047 opcode = LIO_WRITE;
6d49aa6f 1048 so->so_snd.ssb_flags &= ~SSB_AIO;
984263bc
MD
1049 } else {
1050 opcode = LIO_READ;
6d49aa6f 1051 so->so_rcv.ssb_flags &= ~SSB_AIO;
984263bc
MD
1052 }
1053
1054 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1055 cbn = TAILQ_NEXT(cb, list);
1056 if (opcode == cb->uaiocb.aio_lio_opcode) {
1057 p = cb->userproc;
1058 ki = p->p_aioinfo;
1059 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1060 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1061 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1062 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1063 wakecount++;
1064 if (cb->jobstate != JOBST_JOBQGLOBAL)
1065 panic("invalid queue value");
1066 }
1067 }
1068
1069 while (wakecount--) {
1070 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1071 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1072 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1073 aiop->aioprocflags &= ~AIOP_FREE;
1074 wakeup(aiop->aioproc);
1075 }
1076 }
1077#endif /* VFS_AIO */
1078}
1079
1080#ifdef VFS_AIO
1081/*
1082 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1083 * technique is done in this code.
1084 */
1085static int
41c20dac 1086_aio_aqueue(struct aiocb *job, struct aio_liojob *lj, int type)
984263bc 1087{
7b95be2a 1088 struct proc *p = curproc;
984263bc
MD
1089 struct file *fp;
1090 unsigned int fd;
1091 struct socket *so;
984263bc
MD
1092 int error;
1093 int opcode, user_opcode;
1094 struct aiocblist *aiocbe;
1095 struct aioproclist *aiop;
1096 struct kaioinfo *ki;
1097 struct kevent kev;
1098 struct kqueue *kq;
1099 struct file *kq_fp;
3919ced0 1100 int fflags;
984263bc
MD
1101
1102 if ((aiocbe = TAILQ_FIRST(&aio_freejobs)) != NULL)
1103 TAILQ_REMOVE(&aio_freejobs, aiocbe, list);
1104 else
1105 aiocbe = zalloc (aiocb_zone);
1106
1107 aiocbe->inputcharge = 0;
1108 aiocbe->outputcharge = 0;
a5eb27b6 1109 callout_init(&aiocbe->timeout);
984263bc
MD
1110 SLIST_INIT(&aiocbe->klist);
1111
1112 suword(&job->_aiocb_private.status, -1);
1113 suword(&job->_aiocb_private.error, 0);
1114 suword(&job->_aiocb_private.kernelinfo, -1);
1115
1116 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1117 if (error) {
1118 suword(&job->_aiocb_private.error, error);
1119 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1120 return error;
1121 }
1122 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1123 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1124 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1125 return EINVAL;
1126 }
1127
1128 /* Save userspace address of the job info. */
1129 aiocbe->uuaiocb = job;
1130
1131 /* Get the opcode. */
1132 user_opcode = aiocbe->uaiocb.aio_lio_opcode;
1133 if (type != LIO_NOP)
1134 aiocbe->uaiocb.aio_lio_opcode = type;
1135 opcode = aiocbe->uaiocb.aio_lio_opcode;
1136
984263bc
MD
1137 /*
1138 * Range check file descriptor.
1139 */
3919ced0 1140 fflags = (opcode == LIO_WRITE) ? FWRITE : FREAD;
984263bc 1141 fd = aiocbe->uaiocb.aio_fildes;
3919ced0
MD
1142 fp = holdfp(p->p_fd, fd, fflags);
1143 if (fp == NULL) {
984263bc
MD
1144 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1145 if (type == 0)
1146 suword(&job->_aiocb_private.error, EBADF);
1147 return EBADF;
1148 }
1149
3919ced0 1150 aiocbe->fd_file = fp;
984263bc
MD
1151
1152 if (aiocbe->uaiocb.aio_offset == -1LL) {
1153 error = EINVAL;
1154 goto aqueue_fail;
1155 }
1156 error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1157 if (error) {
1158 error = EINVAL;
1159 goto aqueue_fail;
1160 }
1161 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1162 if (jobrefid == LONG_MAX)
1163 jobrefid = 1;
1164 else
1165 jobrefid++;
1166
1167 if (opcode == LIO_NOP) {
9f87144f 1168 fdrop(fp);
984263bc
MD
1169 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1170 if (type == 0) {
1171 suword(&job->_aiocb_private.error, 0);
1172 suword(&job->_aiocb_private.status, 0);
1173 suword(&job->_aiocb_private.kernelinfo, 0);
1174 }
1175 return 0;
1176 }
1177 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1178 if (type == 0)
1179 suword(&job->_aiocb_private.status, 0);
1180 error = EINVAL;
1181 goto aqueue_fail;
1182 }
1183
1184 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1185 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
36934016 1186 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
984263bc
MD
1187 }
1188 else {
1189 /*
1190 * This method for requesting kevent-based notification won't
1191 * work on the alpha, since we're passing in a pointer
1192 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT-
1193 * based method instead.
1194 */
1195 if (user_opcode == LIO_NOP || user_opcode == LIO_READ ||
1196 user_opcode == LIO_WRITE)
1197 goto no_kqueue;
1198
1199 error = copyin((struct kevent *)(uintptr_t)user_opcode,
1200 &kev, sizeof(kev));
1201 if (error)
1202 goto aqueue_fail;
1203 }
3919ced0
MD
1204 kq_fp = holdfp(p->p_fd, (int)kev.ident, -1);
1205 if (kq_fp == NULL || kq_fp->f_type != DTYPE_KQUEUE) {
1206 if (kq_fp) {
1207 fdrop(kq_fp);
1208 kq_fp = NULL;
1209 }
984263bc
MD
1210 error = EBADF;
1211 goto aqueue_fail;
1212 }
1213 kq = (struct kqueue *)kq_fp->f_data;
1214 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1215 kev.filter = EVFILT_AIO;
1216 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1217 kev.data = (intptr_t)aiocbe;
ccafe911 1218 error = kqueue_register(kq, &kev);
3919ced0 1219 fdrop(kq_fp);
984263bc
MD
1220aqueue_fail:
1221 if (error) {
9f87144f 1222 fdrop(fp);
984263bc
MD
1223 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1224 if (type == 0)
1225 suword(&job->_aiocb_private.error, error);
1226 goto done;
1227 }
1228no_kqueue:
1229
1230 suword(&job->_aiocb_private.error, EINPROGRESS);
1231 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1232 aiocbe->userproc = p;
1233 aiocbe->jobflags = 0;
1234 aiocbe->lio = lj;
1235 ki = p->p_aioinfo;
1236
1237 if (fp->f_type == DTYPE_SOCKET) {
1238 /*
1239 * Alternate queueing for socket ops: Reach down into the
1240 * descriptor to get the socket data. Then check to see if the
1241 * socket is ready to be read or written (based on the requested
1242 * operation).
1243 *
1244 * If it is not ready for io, then queue the aiocbe on the
6d49aa6f 1245 * socket, and set the flags so we get a call when ssb_notify()
984263bc
MD
1246 * happens.
1247 */
1248 so = (struct socket *)fp->f_data;
e43a034f 1249 crit_enter();
984263bc
MD
1250 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1251 LIO_WRITE) && (!sowriteable(so)))) {
1252 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1253 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1254 if (opcode == LIO_READ)
6d49aa6f 1255 so->so_rcv.ssb_flags |= SSB_AIO;
984263bc 1256 else
6d49aa6f 1257 so->so_snd.ssb_flags |= SSB_AIO;
984263bc
MD
1258 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1259 ki->kaio_queue_count++;
1260 num_queue_count++;
e43a034f 1261 crit_exit();
984263bc
MD
1262 error = 0;
1263 goto done;
1264 }
e43a034f 1265 crit_exit();
984263bc
MD
1266 }
1267
1268 if ((error = aio_qphysio(p, aiocbe)) == 0)
1269 goto done;
1270 if (error > 0) {
1271 suword(&job->_aiocb_private.status, 0);
1272 aiocbe->uaiocb._aiocb_private.error = error;
1273 suword(&job->_aiocb_private.error, error);
1274 goto done;
1275 }
1276
1277 /* No buffer for daemon I/O. */
1278 aiocbe->bp = NULL;
1279
1280 ki->kaio_queue_count++;
1281 if (lj)
1282 lj->lioj_queue_count++;
e43a034f 1283 crit_enter();
984263bc
MD
1284 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1285 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
e43a034f 1286 crit_exit();
984263bc
MD
1287 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1288
1289 num_queue_count++;
1290 error = 0;
1291
1292 /*
1293 * If we don't have a free AIO process, and we are below our quota, then
1294 * start one. Otherwise, depend on the subsequent I/O completions to
d0d91865 1295 * pick-up this job. If we don't successfully create the new process
984263bc
MD
1296 * (thread) due to resource issues, we return an error for now (EAGAIN),
1297 * which is likely not the correct thing to do.
1298 */
e43a034f 1299 crit_enter();
984263bc
MD
1300retryproc:
1301 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1302 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1303 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1304 aiop->aioprocflags &= ~AIOP_FREE;
1305 wakeup(aiop->aioproc);
1306 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1307 ((ki->kaio_active_count + num_aio_resv_start) <
1308 ki->kaio_maxactive_count)) {
1309 num_aio_resv_start++;
1310 if ((error = aio_newproc()) == 0) {
1311 num_aio_resv_start--;
1312 goto retryproc;
1313 }
1314 num_aio_resv_start--;
1315 }
e43a034f 1316 crit_exit();
984263bc
MD
1317done:
1318 return error;
1319}
1320
1321/*
1322 * This routine queues an AIO request, checking for quotas.
1323 */
1324static int
41c20dac 1325aio_aqueue(struct aiocb *job, int type)
984263bc 1326{
7b95be2a 1327 struct proc *p = curproc;
984263bc
MD
1328 struct kaioinfo *ki;
1329
1330 if (p->p_aioinfo == NULL)
1331 aio_init_aioinfo(p);
1332
1333 if (num_queue_count >= max_queue_count)
1334 return EAGAIN;
1335
1336 ki = p->p_aioinfo;
1337 if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1338 return EAGAIN;
1339
41c20dac 1340 return _aio_aqueue(job, NULL, type);
984263bc
MD
1341}
1342#endif /* VFS_AIO */
1343
1344/*
1345 * Support the aio_return system call, as a side-effect, kernel resources are
1346 * released.
3919ced0
MD
1347 *
1348 * MPALMOSTSAFE
984263bc
MD
1349 */
1350int
753fd850 1351sys_aio_return(struct aio_return_args *uap)
984263bc
MD
1352{
1353#ifndef VFS_AIO
3919ced0 1354 return (ENOSYS);
984263bc 1355#else
41c20dac 1356 struct proc *p = curproc;
fde7ac71 1357 struct lwp *lp = curthread->td_lwp;
984263bc
MD
1358 long jobref;
1359 struct aiocblist *cb, *ncb;
1360 struct aiocb *ujob;
1361 struct kaioinfo *ki;
3919ced0 1362 int error;
984263bc
MD
1363
1364 ki = p->p_aioinfo;
1365 if (ki == NULL)
1366 return EINVAL;
1367
1368 ujob = uap->aiocbp;
1369
1370 jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1371 if (jobref == -1 || jobref == 0)
1372 return EINVAL;
1373
3919ced0 1374 get_mplock();
984263bc
MD
1375 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1376 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1377 jobref) {
1378 if (ujob == cb->uuaiocb) {
c7114eea 1379 uap->sysmsg_result =
984263bc 1380 cb->uaiocb._aiocb_private.status;
3919ced0 1381 } else {
c7114eea 1382 uap->sysmsg_result = EFAULT;
3919ced0 1383 }
984263bc 1384 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
fde7ac71 1385 lp->lwp_ru.ru_oublock += cb->outputcharge;
984263bc
MD
1386 cb->outputcharge = 0;
1387 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
fde7ac71 1388 lp->lwp_ru.ru_inblock += cb->inputcharge;
984263bc
MD
1389 cb->inputcharge = 0;
1390 }
1391 aio_free_entry(cb);
3919ced0
MD
1392 error = 0;
1393 goto done;
984263bc
MD
1394 }
1395 }
e43a034f 1396 crit_enter();
984263bc
MD
1397 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1398 ncb = TAILQ_NEXT(cb, plist);
1399 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1400 == jobref) {
e43a034f 1401 crit_exit();
984263bc 1402 if (ujob == cb->uuaiocb) {
c7114eea 1403 uap->sysmsg_result =
984263bc 1404 cb->uaiocb._aiocb_private.status;
3919ced0 1405 } else {
c7114eea 1406 uap->sysmsg_result = EFAULT;
3919ced0 1407 }
984263bc 1408 aio_free_entry(cb);
3919ced0
MD
1409 error = 0;
1410 goto done;
984263bc
MD
1411 }
1412 }
e43a034f 1413 crit_exit();
3919ced0
MD
1414 error = EINVAL;
1415done:
1416 rel_mplock();
1417 return (error);
984263bc
MD
1418#endif /* VFS_AIO */
1419}
1420
1421/*
1422 * Allow a process to wakeup when any of the I/O requests are completed.
3919ced0
MD
1423 *
1424 * MPALMOSTSAFE
984263bc
MD
1425 */
1426int
753fd850 1427sys_aio_suspend(struct aio_suspend_args *uap)
984263bc
MD
1428{
1429#ifndef VFS_AIO
1430 return ENOSYS;
1431#else
41c20dac 1432 struct proc *p = curproc;
984263bc
MD
1433 struct timeval atv;
1434 struct timespec ts;
1435 struct aiocb *const *cbptr, *cbp;
1436 struct kaioinfo *ki;
1437 struct aiocblist *cb;
1438 int i;
1439 int njoblist;
831f78e5 1440 int error, timo;
984263bc
MD
1441 long *ijoblist;
1442 struct aiocb **ujoblist;
1443
3919ced0 1444 if ((u_int)uap->nent > AIO_LISTIO_MAX)
984263bc
MD
1445 return EINVAL;
1446
1447 timo = 0;
1448 if (uap->timeout) {
1449 /* Get timespec struct. */
1450 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1451 return error;
1452
1453 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1454 return (EINVAL);
1455
1456 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1457 if (itimerfix(&atv))
1458 return (EINVAL);
a94976ad 1459 timo = tvtohz_high(&atv);
984263bc
MD
1460 }
1461
1462 ki = p->p_aioinfo;
1463 if (ki == NULL)
1464 return EAGAIN;
1465
3919ced0
MD
1466 get_mplock();
1467
984263bc
MD
1468 njoblist = 0;
1469 ijoblist = zalloc(aiol_zone);
1470 ujoblist = zalloc(aiol_zone);
1471 cbptr = uap->aiocbp;
1472
1473 for (i = 0; i < uap->nent; i++) {
1474 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1475 if (cbp == 0)
1476 continue;
1477 ujoblist[njoblist] = cbp;
1478 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1479 njoblist++;
1480 }
1481
1482 if (njoblist == 0) {
1483 zfree(aiol_zone, ijoblist);
1484 zfree(aiol_zone, ujoblist);
3919ced0
MD
1485 error = 0;
1486 goto done;
984263bc
MD
1487 }
1488
1489 error = 0;
1490 for (;;) {
1491 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1492 for (i = 0; i < njoblist; i++) {
1493 if (((intptr_t)
1494 cb->uaiocb._aiocb_private.kernelinfo) ==
1495 ijoblist[i]) {
1496 if (ujoblist[i] != cb->uuaiocb)
1497 error = EINVAL;
1498 zfree(aiol_zone, ijoblist);
1499 zfree(aiol_zone, ujoblist);
3919ced0 1500 goto done;
984263bc
MD
1501 }
1502 }
1503 }
1504
e43a034f 1505 crit_enter();
984263bc
MD
1506 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1507 TAILQ_NEXT(cb, plist)) {
1508 for (i = 0; i < njoblist; i++) {
1509 if (((intptr_t)
1510 cb->uaiocb._aiocb_private.kernelinfo) ==
1511 ijoblist[i]) {
e43a034f 1512 crit_exit();
984263bc
MD
1513 if (ujoblist[i] != cb->uuaiocb)
1514 error = EINVAL;
1515 zfree(aiol_zone, ijoblist);
1516 zfree(aiol_zone, ujoblist);
3919ced0 1517 goto done;
984263bc
MD
1518 }
1519 }
1520 }
1521
1522 ki->kaio_flags |= KAIO_WAKEUP;
377d4740 1523 error = tsleep(p, PCATCH, "aiospn", timo);
e43a034f 1524 crit_exit();
984263bc
MD
1525
1526 if (error == ERESTART || error == EINTR) {
1527 zfree(aiol_zone, ijoblist);
1528 zfree(aiol_zone, ujoblist);
3919ced0
MD
1529 error = EINTR;
1530 goto done;
984263bc
MD
1531 } else if (error == EWOULDBLOCK) {
1532 zfree(aiol_zone, ijoblist);
1533 zfree(aiol_zone, ujoblist);
3919ced0
MD
1534 error = EAGAIN;
1535 goto done;
984263bc
MD
1536 }
1537 }
1538
1539/* NOTREACHED */
3919ced0
MD
1540 error = EINVAL;
1541done:
1542 rel_mplock();
1543 return (error);
984263bc
MD
1544#endif /* VFS_AIO */
1545}
1546
1547/*
1548 * aio_cancel cancels any non-physio aio operations not currently in
1549 * progress.
3919ced0
MD
1550 *
1551 * MPALMOSTSAFE
984263bc
MD
1552 */
1553int
753fd850 1554sys_aio_cancel(struct aio_cancel_args *uap)
984263bc
MD
1555{
1556#ifndef VFS_AIO
1557 return ENOSYS;
1558#else
41c20dac 1559 struct proc *p = curproc;
984263bc
MD
1560 struct kaioinfo *ki;
1561 struct aiocblist *cbe, *cbn;
1562 struct file *fp;
984263bc
MD
1563 struct socket *so;
1564 struct proc *po;
e43a034f 1565 int error;
984263bc
MD
1566 int cancelled=0;
1567 int notcancelled=0;
1568 struct vnode *vp;
1569
3919ced0
MD
1570 fp = holdfp(p->p_fd, uap->fd, -1);
1571 if (fp == NULL)
984263bc
MD
1572 return (EBADF);
1573
3919ced0
MD
1574 get_mplock();
1575
984263bc
MD
1576 if (fp->f_type == DTYPE_VNODE) {
1577 vp = (struct vnode *)fp->f_data;
1578
1579 if (vn_isdisk(vp,&error)) {
c7114eea 1580 uap->sysmsg_result = AIO_NOTCANCELED;
3919ced0
MD
1581 error = 0;
1582 goto done2;
984263bc
MD
1583 }
1584 } else if (fp->f_type == DTYPE_SOCKET) {
1585 so = (struct socket *)fp->f_data;
1586
e43a034f 1587 crit_enter();
984263bc
MD
1588
1589 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1590 cbn = TAILQ_NEXT(cbe, list);
1591 if ((uap->aiocbp == NULL) ||
1592 (uap->aiocbp == cbe->uuaiocb) ) {
1593 po = cbe->userproc;
1594 ki = po->p_aioinfo;
1595 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1596 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1597 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1598 if (ki->kaio_flags & KAIO_WAKEUP) {
1599 wakeup(po);
1600 }
1601 cbe->jobstate = JOBST_JOBFINISHED;
1602 cbe->uaiocb._aiocb_private.status=-1;
1603 cbe->uaiocb._aiocb_private.error=ECANCELED;
1604 cancelled++;
1605/* XXX cancelled, knote? */
1606 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1607 SIGEV_SIGNAL)
84204577 1608 ksignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
984263bc
MD
1609 if (uap->aiocbp)
1610 break;
1611 }
1612 }
e43a034f 1613 crit_exit();
984263bc
MD
1614
1615 if ((cancelled) && (uap->aiocbp)) {
c7114eea 1616 uap->sysmsg_result = AIO_CANCELED;
3919ced0
MD
1617 error = 0;
1618 goto done2;
984263bc
MD
1619 }
1620 }
1621 ki=p->p_aioinfo;
1622 if (ki == NULL)
1623 goto done;
e43a034f 1624 crit_enter();
984263bc
MD
1625
1626 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1627 cbn = TAILQ_NEXT(cbe, plist);
1628
1629 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1630 ((uap->aiocbp == NULL ) ||
1631 (uap->aiocbp == cbe->uuaiocb))) {
1632
1633 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1634 TAILQ_REMOVE(&aio_jobs, cbe, list);
1635 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1636 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1637 plist);
1638 cancelled++;
1639 ki->kaio_queue_finished_count++;
1640 cbe->jobstate = JOBST_JOBFINISHED;
1641 cbe->uaiocb._aiocb_private.status = -1;
1642 cbe->uaiocb._aiocb_private.error = ECANCELED;
1643/* XXX cancelled, knote? */
1644 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1645 SIGEV_SIGNAL)
84204577 1646 ksignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
984263bc
MD
1647 } else {
1648 notcancelled++;
1649 }
1650 }
1651 }
e43a034f 1652 crit_exit();
984263bc 1653done:
3919ced0 1654 if (notcancelled)
c7114eea 1655 uap->sysmsg_result = AIO_NOTCANCELED;
3919ced0 1656 else if (cancelled)
c7114eea 1657 uap->sysmsg_result = AIO_CANCELED;
3919ced0
MD
1658 else
1659 uap->sysmsg_result = AIO_ALLDONE;
1660 error = 0;
1661done2:
1662 rel_mplock();
1663 fdrop(fp);
1664 return error;
984263bc
MD
1665#endif /* VFS_AIO */
1666}
1667
1668/*
1669 * aio_error is implemented in the kernel level for compatibility purposes only.
1670 * For a user mode async implementation, it would be best to do it in a userland
1671 * subroutine.
3919ced0
MD
1672 *
1673 * MPALMOSTSAFE
984263bc
MD
1674 */
1675int
753fd850 1676sys_aio_error(struct aio_error_args *uap)
984263bc
MD
1677{
1678#ifndef VFS_AIO
1679 return ENOSYS;
1680#else
41c20dac 1681 struct proc *p = curproc;
984263bc
MD
1682 struct aiocblist *cb;
1683 struct kaioinfo *ki;
1684 long jobref;
3919ced0 1685 int error;
984263bc
MD
1686
1687 ki = p->p_aioinfo;
1688 if (ki == NULL)
1689 return EINVAL;
1690
1691 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1692 if ((jobref == -1) || (jobref == 0))
1693 return EINVAL;
1694
3919ced0
MD
1695 get_mplock();
1696 error = 0;
1697
984263bc
MD
1698 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1699 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1700 jobref) {
c7114eea 1701 uap->sysmsg_result = cb->uaiocb._aiocb_private.error;
3919ced0 1702 goto done;
984263bc
MD
1703 }
1704 }
1705
e43a034f 1706 crit_enter();
984263bc
MD
1707
1708 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1709 plist)) {
1710 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1711 jobref) {
c7114eea 1712 uap->sysmsg_result = EINPROGRESS;
e43a034f 1713 crit_exit();
3919ced0 1714 goto done;
984263bc
MD
1715 }
1716 }
1717
1718 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1719 plist)) {
1720 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1721 jobref) {
c7114eea 1722 uap->sysmsg_result = EINPROGRESS;
e43a034f 1723 crit_exit();
3919ced0 1724 goto done;
984263bc
MD
1725 }
1726 }
e43a034f 1727 crit_exit();
984263bc 1728
e43a034f 1729 crit_enter();
984263bc
MD
1730 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1731 plist)) {
1732 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1733 jobref) {
c7114eea 1734 uap->sysmsg_result = cb->uaiocb._aiocb_private.error;
e43a034f 1735 crit_exit();
3919ced0 1736 goto done;
984263bc
MD
1737 }
1738 }
1739
1740 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1741 plist)) {
1742 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1743 jobref) {
c7114eea 1744 uap->sysmsg_result = EINPROGRESS;
e43a034f 1745 crit_exit();
3919ced0 1746 goto done;
984263bc
MD
1747 }
1748 }
e43a034f 1749 crit_exit();
3919ced0
MD
1750 error = EINVAL;
1751done:
1752 rel_mplock();
1753 return (error);
984263bc
MD
1754#endif /* VFS_AIO */
1755}
1756
3919ced0
MD
1757/*
1758 * syscall - asynchronous read from a file (REALTIME)
1759 *
1760 * MPALMOSTSAFE
1761 */
984263bc 1762int
753fd850 1763sys_aio_read(struct aio_read_args *uap)
984263bc
MD
1764{
1765#ifndef VFS_AIO
1766 return ENOSYS;
1767#else
3919ced0
MD
1768 int error;
1769
1770 get_mplock();
1771 error = aio_aqueue(uap->aiocbp, LIO_READ);
1772 rel_mplock();
1773 return (error);
984263bc
MD
1774#endif /* VFS_AIO */
1775}
1776
3919ced0
MD
1777/*
1778 * syscall - asynchronous write to a file (REALTIME)
1779 *
1780 * MPALMOSTSAFE
1781 */
984263bc 1782int
753fd850 1783sys_aio_write(struct aio_write_args *uap)
984263bc
MD
1784{
1785#ifndef VFS_AIO
1786 return ENOSYS;
1787#else
3919ced0
MD
1788 int error;
1789
1790 get_mplock();
1791 error = aio_aqueue(uap->aiocbp, LIO_WRITE);
1792 rel_mplock();
1793 return (error);
984263bc
MD
1794#endif /* VFS_AIO */
1795}
1796
3919ced0
MD
1797/*
1798 * syscall - XXX undocumented
1799 *
1800 * MPALMOSTSAFE
1801 */
984263bc 1802int
753fd850 1803sys_lio_listio(struct lio_listio_args *uap)
984263bc
MD
1804{
1805#ifndef VFS_AIO
1806 return ENOSYS;
1807#else
41c20dac 1808 struct proc *p = curproc;
fde7ac71 1809 struct lwp *lp = curthread->td_lwp;
984263bc
MD
1810 int nent, nentqueued;
1811 struct aiocb *iocb, * const *cbptr;
1812 struct aiocblist *cb;
1813 struct kaioinfo *ki;
1814 struct aio_liojob *lj;
1815 int error, runningcode;
1816 int nerror;
1817 int i;
984263bc
MD
1818
1819 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1820 return EINVAL;
1821
1822 nent = uap->nent;
1823 if (nent > AIO_LISTIO_MAX)
1824 return EINVAL;
1825
3919ced0
MD
1826 get_mplock();
1827
984263bc
MD
1828 if (p->p_aioinfo == NULL)
1829 aio_init_aioinfo(p);
1830
3919ced0
MD
1831 if ((nent + num_queue_count) > max_queue_count) {
1832 error = EAGAIN;
1833 goto done;
1834 }
984263bc
MD
1835
1836 ki = p->p_aioinfo;
3919ced0
MD
1837 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count) {
1838 error = EAGAIN;
1839 goto done;
1840 }
984263bc
MD
1841
1842 lj = zalloc(aiolio_zone);
3919ced0
MD
1843 if (lj == NULL) {
1844 error = EAGAIN;
1845 goto done;
1846 }
984263bc
MD
1847
1848 lj->lioj_flags = 0;
1849 lj->lioj_buffer_count = 0;
1850 lj->lioj_buffer_finished_count = 0;
1851 lj->lioj_queue_count = 0;
1852 lj->lioj_queue_finished_count = 0;
1853 lj->lioj_ki = ki;
1854
1855 /*
1856 * Setup signal.
1857 */
1858 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1859 error = copyin(uap->sig, &lj->lioj_signal,
1860 sizeof(lj->lioj_signal));
1861 if (error) {
1862 zfree(aiolio_zone, lj);
3919ced0 1863 goto done;
984263bc
MD
1864 }
1865 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1866 zfree(aiolio_zone, lj);
3919ced0
MD
1867 error = EINVAL;
1868 goto done;
984263bc
MD
1869 }
1870 lj->lioj_flags |= LIOJ_SIGNAL;
1871 lj->lioj_flags &= ~LIOJ_SIGNAL_POSTED;
1872 } else
1873 lj->lioj_flags &= ~LIOJ_SIGNAL;
1874
1875 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1876 /*
1877 * Get pointers to the list of I/O requests.
1878 */
1879 nerror = 0;
1880 nentqueued = 0;
1881 cbptr = uap->acb_list;
1882 for (i = 0; i < uap->nent; i++) {
1883 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1884 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
41c20dac 1885 error = _aio_aqueue(iocb, lj, 0);
984263bc
MD
1886 if (error == 0)
1887 nentqueued++;
1888 else
1889 nerror++;
1890 }
1891 }
1892
1893 /*
1894 * If we haven't queued any, then just return error.
1895 */
3919ced0
MD
1896 if (nentqueued == 0) {
1897 error = 0;
1898 goto done;
1899 }
984263bc
MD
1900
1901 /*
1902 * Calculate the appropriate error return.
1903 */
1904 runningcode = 0;
1905 if (nerror)
1906 runningcode = EIO;
1907
1908 if (uap->mode == LIO_WAIT) {
1909 int command, found, jobref;
1910
1911 for (;;) {
1912 found = 0;
1913 for (i = 0; i < uap->nent; i++) {
1914 /*
1915 * Fetch address of the control buf pointer in
1916 * user space.
1917 */
1918 iocb = (struct aiocb *)
1919 (intptr_t)fuword(&cbptr[i]);
1920 if (((intptr_t)iocb == -1) || ((intptr_t)iocb
1921 == 0))
1922 continue;
1923
1924 /*
1925 * Fetch the associated command from user space.
1926 */
1927 command = fuword(&iocb->aio_lio_opcode);
1928 if (command == LIO_NOP) {
1929 found++;
1930 continue;
1931 }
1932
1933 jobref = fuword(&iocb->_aiocb_private.kernelinfo);
1934
1935 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1936 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
1937 == jobref) {
1938 if (cb->uaiocb.aio_lio_opcode
1939 == LIO_WRITE) {
fde7ac71 1940 lp->lwp_ru.ru_oublock +=
984263bc
MD
1941 cb->outputcharge;
1942 cb->outputcharge = 0;
1943 } else if (cb->uaiocb.aio_lio_opcode
1944 == LIO_READ) {
fde7ac71
SS
1945 lp->lwp_ru.ru_inblock +=
1946 cb->inputcharge;
984263bc
MD
1947 cb->inputcharge = 0;
1948 }
1949 found++;
1950 break;
1951 }
1952 }
1953
e43a034f 1954 crit_enter();
984263bc
MD
1955 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
1956 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
1957 == jobref) {
1958 found++;
1959 break;
1960 }
1961 }
e43a034f 1962 crit_exit();
984263bc
MD
1963 }
1964
1965 /*
1966 * If all I/Os have been disposed of, then we can
1967 * return.
1968 */
3919ced0
MD
1969 if (found == nentqueued) {
1970 error = runningcode;
1971 goto done;
1972 }
984263bc
MD
1973
1974 ki->kaio_flags |= KAIO_WAKEUP;
377d4740 1975 error = tsleep(p, PCATCH, "aiospn", 0);
984263bc 1976
3919ced0
MD
1977 if (error == EINTR) {
1978 goto done;
1979 } else if (error == EWOULDBLOCK) {
1980 error = EAGAIN;
1981 goto done;
1982 }
984263bc
MD
1983 }
1984 }
1985
3919ced0
MD
1986 error = runningcode;
1987done:
1988 rel_mplock();
1989 return (error);
984263bc
MD
1990#endif /* VFS_AIO */
1991}
1992
1993#ifdef VFS_AIO
1994/*
1995 * This is a weird hack so that we can post a signal. It is safe to do so from
1996 * a timeout routine, but *not* from an interrupt routine.
1997 */
1998static void
1999process_signal(void *aioj)
2000{
2001 struct aiocblist *aiocbe = aioj;
2002 struct aio_liojob *lj = aiocbe->lio;
2003 struct aiocb *cb = &aiocbe->uaiocb;
2004
2005 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) &&
2006 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) {
84204577 2007 ksignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo);
984263bc
MD
2008 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2009 }
2010
2011 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL)
84204577 2012 ksignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo);
984263bc
MD
2013}
2014
2015/*
2016 * Interrupt handler for physio, performs the necessary process wakeups, and
2017 * signals.
2018 */
2019static void
81b5c339 2020aio_physwakeup(struct bio *bio)
984263bc 2021{
81b5c339 2022 struct buf *bp = bio->bio_buf;
984263bc
MD
2023 struct aiocblist *aiocbe;
2024 struct proc *p;
2025 struct kaioinfo *ki;
2026 struct aio_liojob *lj;
2027
81b5c339 2028 aiocbe = bio->bio_caller_info2.ptr;
77912481 2029 get_mplock();
984263bc 2030
984263bc 2031 if (aiocbe) {
81b5c339 2032 p = bio->bio_caller_info1.ptr;
984263bc
MD
2033
2034 aiocbe->jobstate = JOBST_JOBBFINISHED;
2035 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2036 aiocbe->uaiocb._aiocb_private.error = 0;
2037 aiocbe->jobflags |= AIOCBLIST_DONE;
2038
2039 if (bp->b_flags & B_ERROR)
2040 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2041
2042 lj = aiocbe->lio;
2043 if (lj) {
2044 lj->lioj_buffer_finished_count++;
2045
2046 /*
2047 * wakeup/signal if all of the interrupt jobs are done.
2048 */
2049 if (lj->lioj_buffer_finished_count ==
2050 lj->lioj_buffer_count) {
2051 /*
2052 * Post a signal if it is called for.
2053 */
2054 if ((lj->lioj_flags &
2055 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2056 LIOJ_SIGNAL) {
2057 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
a5eb27b6
MD
2058 callout_reset(&aiocbe->timeout, 0,
2059 process_signal, aiocbe);
984263bc
MD
2060 }
2061 }
2062 }
2063
2064 ki = p->p_aioinfo;
2065 if (ki) {
2066 ki->kaio_buffer_finished_count++;
2067 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
2068 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2069 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2070
2071 KNOTE(&aiocbe->klist, 0);
2072 /* Do the wakeup. */
2073 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2074 ki->kaio_flags &= ~KAIO_WAKEUP;
2075 wakeup(p);
2076 }
2077 }
2078
a5eb27b6
MD
2079 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
2080 callout_reset(&aiocbe->timeout, 0,
2081 process_signal, aiocbe);
2082 }
984263bc 2083 }
ae8e83e6 2084 biodone_sync(bio);
77912481 2085 rel_mplock();
984263bc
MD
2086}
2087#endif /* VFS_AIO */
2088
3919ced0
MD
2089/*
2090 * syscall - wait for the next completion of an aio request
2091 *
2092 * MPALMOSTSAFE
2093 */
984263bc 2094int
753fd850 2095sys_aio_waitcomplete(struct aio_waitcomplete_args *uap)
984263bc
MD
2096{
2097#ifndef VFS_AIO
2098 return ENOSYS;
2099#else
41c20dac 2100 struct proc *p = curproc;
fde7ac71 2101 struct lwp *lp = curthread->td_lwp;
984263bc
MD
2102 struct timeval atv;
2103 struct timespec ts;
2104 struct kaioinfo *ki;
2105 struct aiocblist *cb = NULL;
831f78e5 2106 int error, timo;
984263bc
MD
2107
2108 suword(uap->aiocbp, (int)NULL);
2109
2110 timo = 0;
2111 if (uap->timeout) {
2112 /* Get timespec struct. */
2113 error = copyin(uap->timeout, &ts, sizeof(ts));
2114 if (error)
2115 return error;
2116
2117 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2118 return (EINVAL);
2119
2120 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2121 if (itimerfix(&atv))
2122 return (EINVAL);
a94976ad 2123 timo = tvtohz_high(&atv);
984263bc
MD
2124 }
2125
2126 ki = p->p_aioinfo;
2127 if (ki == NULL)
2128 return EAGAIN;
2129
3919ced0
MD
2130 get_mplock();
2131
984263bc
MD
2132 for (;;) {
2133 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2134 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
c7114eea 2135 uap->sysmsg_result = cb->uaiocb._aiocb_private.status;
984263bc 2136 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
fde7ac71 2137 lp->lwp_ru.ru_oublock +=
984263bc
MD
2138 cb->outputcharge;
2139 cb->outputcharge = 0;
2140 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
fde7ac71 2141 lp->lwp_ru.ru_inblock += cb->inputcharge;
984263bc
MD
2142 cb->inputcharge = 0;
2143 }
2144 aio_free_entry(cb);
3919ced0
MD
2145 error = cb->uaiocb._aiocb_private.error;
2146 break;
984263bc
MD
2147 }
2148
e43a034f 2149 crit_enter();
984263bc 2150 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
e43a034f 2151 crit_exit();
984263bc 2152 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
c7114eea 2153 uap->sysmsg_result = cb->uaiocb._aiocb_private.status;
984263bc 2154 aio_free_entry(cb);
3919ced0
MD
2155 error = cb->uaiocb._aiocb_private.error;
2156 break;
984263bc
MD
2157 }
2158
2159 ki->kaio_flags |= KAIO_WAKEUP;
377d4740 2160 error = tsleep(p, PCATCH, "aiowc", timo);
e43a034f 2161 crit_exit();
984263bc 2162
3919ced0
MD
2163 if (error == ERESTART) {
2164 error = EINTR;
2165 break;
2166 }
2167 if (error < 0)
2168 break;
2169 if (error == EINTR)
2170 break;
2171 if (error == EWOULDBLOCK) {
2172 error = EAGAIN;
2173 break;
2174 }
984263bc 2175 }
3919ced0
MD
2176 rel_mplock();
2177 return (error);
984263bc
MD
2178#endif /* VFS_AIO */
2179}
2180
2181#ifndef VFS_AIO
2182static int
2183filt_aioattach(struct knote *kn)
2184{
2185
2186 return (ENXIO);
2187}
2188
2189struct filterops aio_filtops =
2190 { 0, filt_aioattach, NULL, NULL };
2191
2192#else
2193/* kqueue attach function */
2194static int
2195filt_aioattach(struct knote *kn)
2196{
2197 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2198
2199 /*
2200 * The aiocbe pointer must be validated before using it, so
2201 * registration is restricted to the kernel; the user cannot
2202 * set EV_FLAG1.
2203 */
2204 if ((kn->kn_flags & EV_FLAG1) == 0)
2205 return (EPERM);
2206 kn->kn_flags &= ~EV_FLAG1;
2207
5b22f1a7 2208 knote_insert(&aiocbe->klist, kn);
984263bc
MD
2209
2210 return (0);
2211}
2212
2213/* kqueue detach function */
2214static void
2215filt_aiodetach(struct knote *kn)
2216{
2217 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2218
5b22f1a7 2219 knote_remove(&aiocbe->klist, kn);
984263bc
MD
2220}
2221
2222/* kqueue filter function */
2223/*ARGSUSED*/
2224static int
2225filt_aio(struct knote *kn, long hint)
2226{
2227 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2228
2229 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2230 if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2231 aiocbe->jobstate != JOBST_JOBBFINISHED)
2232 return (0);
2233 kn->kn_flags |= EV_EOF;
2234 return (1);
2235}
2236
2237struct filterops aio_filtops =
2238 { 0, filt_aioattach, filt_aiodetach, filt_aio };
2239#endif /* VFS_AIO */