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