Optimize lwkt_rwlock.c a bit
[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 $
7d0bac62 17 * $DragonFly: src/sys/kern/vfs_aio.c,v 1.3 2003/06/22 04:30:42 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);
221static int aio_aqueue(struct proc *p, struct aiocb *job, int type);
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;
318 tsleep(aiocbe, PRIBIO, "jobwai", 0);
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;
417 if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout))
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;
467 tsleep(p, PRIBIO, "aioprn", 0);
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.
613 */
614static void
615aio_daemon(void *uproc)
616{
617 int s;
618 struct aio_liojob *lj;
619 struct aiocb *cb;
620 struct aiocblist *aiocbe;
621 struct aioproclist *aiop;
622 struct kaioinfo *ki;
623 struct proc *curcp, *mycp, *userp;
624 struct vmspace *myvm, *tmpvm;
625
626 /*
627 * Local copies of curproc (cp) and vmspace (myvm)
628 */
629 mycp = curproc;
630 myvm = mycp->p_vmspace;
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
645 s = splnet();
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
654 splx(s);
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 */
664 fdfree(mycp);
665 mycp->p_fd = NULL;
666 mycp->p_ucred = crcopy(mycp->p_ucred);
667 mycp->p_ucred->cr_uid = 0;
668 uifree(mycp->p_ucred->cr_uidinfo);
669 mycp->p_ucred->cr_uidinfo = uifind(0);
670 mycp->p_ucred->cr_ngroups = 1;
671 mycp->p_ucred->cr_groups[0] = 1;
672
673 /* The daemon resides in its own pgrp. */
674 enterpgrp(mycp, mycp->p_pid, 1);
675
676 /* Mark special process type. */
677 mycp->p_flag |= P_SYSTEM | P_KTHREADP;
678
679 /*
680 * Wakeup parent process. (Parent sleeps to keep from blasting away
681 * and creating too many daemons.)
682 */
683 wakeup(mycp);
684
685 for (;;) {
686 /*
687 * curcp is the current daemon process context.
688 * userp is the current user process context.
689 */
690 curcp = mycp;
691
692 /*
693 * Take daemon off of free queue
694 */
695 if (aiop->aioprocflags & AIOP_FREE) {
696 s = splnet();
697 TAILQ_REMOVE(&aio_freeproc, aiop, list);
698 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
699 aiop->aioprocflags &= ~AIOP_FREE;
700 splx(s);
701 }
702 aiop->aioprocflags &= ~AIOP_SCHED;
703
704 /*
705 * Check for jobs.
706 */
707 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
708 cb = &aiocbe->uaiocb;
709 userp = aiocbe->userproc;
710
711 aiocbe->jobstate = JOBST_JOBRUNNING;
712
713 /*
714 * Connect to process address space for user program.
715 */
716 if (userp != curcp) {
717 /*
718 * Save the current address space that we are
719 * connected to.
720 */
721 tmpvm = mycp->p_vmspace;
722
723 /*
724 * Point to the new user address space, and
725 * refer to it.
726 */
727 mycp->p_vmspace = userp->p_vmspace;
728 mycp->p_vmspace->vm_refcnt++;
729
730 /* Activate the new mapping. */
731 pmap_activate(mycp);
732
733 /*
734 * If the old address space wasn't the daemons
735 * own address space, then we need to remove the
736 * daemon's reference from the other process
737 * that it was acting on behalf of.
738 */
739 if (tmpvm != myvm) {
740 vmspace_free(tmpvm);
741 }
742 curcp = userp;
743 }
744
745 ki = userp->p_aioinfo;
746 lj = aiocbe->lio;
747
748 /* Account for currently active jobs. */
749 ki->kaio_active_count++;
750
751 /* Do the I/O function. */
752 aio_process(aiocbe);
753
754 /* Decrement the active job count. */
755 ki->kaio_active_count--;
756
757 /*
758 * Increment the completion count for wakeup/signal
759 * comparisons.
760 */
761 aiocbe->jobflags |= AIOCBLIST_DONE;
762 ki->kaio_queue_finished_count++;
763 if (lj)
764 lj->lioj_queue_finished_count++;
765 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
766 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
767 ki->kaio_flags &= ~KAIO_WAKEUP;
768 wakeup(userp);
769 }
770
771 s = splbio();
772 if (lj && (lj->lioj_flags &
773 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
774 if ((lj->lioj_queue_finished_count ==
775 lj->lioj_queue_count) &&
776 (lj->lioj_buffer_finished_count ==
777 lj->lioj_buffer_count)) {
778 psignal(userp,
779 lj->lioj_signal.sigev_signo);
780 lj->lioj_flags |=
781 LIOJ_SIGNAL_POSTED;
782 }
783 }
784 splx(s);
785
786 aiocbe->jobstate = JOBST_JOBFINISHED;
787
788 s = splnet();
789 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
790 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist);
791 splx(s);
792 KNOTE(&aiocbe->klist, 0);
793
794 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
795 wakeup(aiocbe);
796 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
797 }
798
799 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
800 psignal(userp, cb->aio_sigevent.sigev_signo);
801 }
802 }
803
804 /*
805 * Disconnect from user address space.
806 */
807 if (curcp != mycp) {
808 /* Get the user address space to disconnect from. */
809 tmpvm = mycp->p_vmspace;
810
811 /* Get original address space for daemon. */
812 mycp->p_vmspace = myvm;
813
814 /* Activate the daemon's address space. */
815 pmap_activate(mycp);
816#ifdef DIAGNOSTIC
817 if (tmpvm == myvm) {
818 printf("AIOD: vmspace problem -- %d\n",
819 mycp->p_pid);
820 }
821#endif
822 /* Remove our vmspace reference. */
823 vmspace_free(tmpvm);
824
825 curcp = mycp;
826 }
827
828 /*
829 * If we are the first to be put onto the free queue, wakeup
830 * anyone waiting for a daemon.
831 */
832 s = splnet();
833 TAILQ_REMOVE(&aio_activeproc, aiop, list);
834 if (TAILQ_EMPTY(&aio_freeproc))
835 wakeup(&aio_freeproc);
836 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
837 aiop->aioprocflags |= AIOP_FREE;
838 splx(s);
839
840 /*
841 * If daemon is inactive for a long time, allow it to exit,
842 * thereby freeing resources.
843 */
844 if (((aiop->aioprocflags & AIOP_SCHED) == 0) && tsleep(mycp,
845 PRIBIO, "aiordy", aiod_lifetime)) {
846 s = splnet();
847 if (TAILQ_EMPTY(&aio_jobs)) {
848 if ((aiop->aioprocflags & AIOP_FREE) &&
849 (num_aio_procs > target_aio_procs)) {
850 TAILQ_REMOVE(&aio_freeproc, aiop, list);
851 splx(s);
852 zfree(aiop_zone, aiop);
853 num_aio_procs--;
854#ifdef DIAGNOSTIC
855 if (mycp->p_vmspace->vm_refcnt <= 1) {
856 printf("AIOD: bad vm refcnt for"
857 " exiting daemon: %d\n",
858 mycp->p_vmspace->vm_refcnt);
859 }
860#endif
861 exit1(mycp, 0);
862 }
863 }
864 splx(s);
865 }
866 }
867}
868
869/*
870 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
871 * AIO daemon modifies its environment itself.
872 */
873static int
874aio_newproc()
875{
876 int error;
877 struct proc *p, *np;
878
879 p = &proc0;
880 error = fork1(p, RFPROC|RFMEM|RFNOWAIT, &np);
881 if (error)
882 return error;
883 cpu_set_fork_handler(np, aio_daemon, curproc);
7d0bac62 884 start_forked_proc(p, np);
984263bc
MD
885
886 /*
887 * Wait until daemon is started, but continue on just in case to
888 * handle error conditions.
889 */
890 error = tsleep(np, PZERO, "aiosta", aiod_timeout);
891 num_aio_procs++;
892
893 return error;
894}
895
896/*
897 * Try the high-performance, low-overhead physio method for eligible
898 * VCHR devices. This method doesn't use an aio helper thread, and
899 * thus has very low overhead.
900 *
901 * Assumes that the caller, _aio_aqueue(), has incremented the file
902 * structure's reference count, preventing its deallocation for the
903 * duration of this call.
904 */
905static int
906aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
907{
908 int error;
909 struct aiocb *cb;
910 struct file *fp;
911 struct buf *bp;
912 struct vnode *vp;
913 struct kaioinfo *ki;
914 struct aio_liojob *lj;
915 int s;
916 int notify;
917
918 cb = &aiocbe->uaiocb;
919 fp = aiocbe->fd_file;
920
921 if (fp->f_type != DTYPE_VNODE)
922 return (-1);
923
924 vp = (struct vnode *)fp->f_data;
925
926 /*
927 * If its not a disk, we don't want to return a positive error.
928 * It causes the aio code to not fall through to try the thread
929 * way when you're talking to a regular file.
930 */
931 if (!vn_isdisk(vp, &error)) {
932 if (error == ENOTBLK)
933 return (-1);
934 else
935 return (error);
936 }
937
938 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys)
939 return (-1);
940
941 if (cb->aio_nbytes >
942 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
943 return (-1);
944
945 ki = p->p_aioinfo;
946 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
947 return (-1);
948
949 ki->kaio_buffer_count++;
950
951 lj = aiocbe->lio;
952 if (lj)
953 lj->lioj_buffer_count++;
954
955 /* Create and build a buffer header for a transfer. */
956 bp = (struct buf *)getpbuf(NULL);
957 BUF_KERNPROC(bp);
958
959 /*
960 * Get a copy of the kva from the physical buffer.
961 */
962 bp->b_caller1 = p;
963 bp->b_dev = vp->v_rdev;
964 error = 0;
965
966 bp->b_bcount = cb->aio_nbytes;
967 bp->b_bufsize = cb->aio_nbytes;
968 bp->b_flags = B_PHYS | B_CALL | (cb->aio_lio_opcode == LIO_WRITE ?
969 B_WRITE : B_READ);
970 bp->b_iodone = aio_physwakeup;
971 bp->b_saveaddr = bp->b_data;
972 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
973 bp->b_blkno = btodb(cb->aio_offset);
974
975 /* Bring buffer into kernel space. */
976 if (vmapbuf(bp) < 0) {
977 error = EFAULT;
978 goto doerror;
979 }
980
981 s = splbio();
982 aiocbe->bp = bp;
983 bp->b_spc = (void *)aiocbe;
984 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list);
985 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
986 aiocbe->jobstate = JOBST_JOBQBUF;
987 cb->_aiocb_private.status = cb->aio_nbytes;
988 num_buf_aio++;
989 bp->b_error = 0;
990
991 splx(s);
992
993 /* Perform transfer. */
994 BUF_STRATEGY(bp, 0);
995
996 notify = 0;
997 s = splbio();
998
999 /*
1000 * If we had an error invoking the request, or an error in processing
1001 * the request before we have returned, we process it as an error in
1002 * transfer. Note that such an I/O error is not indicated immediately,
1003 * but is returned using the aio_error mechanism. In this case,
1004 * aio_suspend will return immediately.
1005 */
1006 if (bp->b_error || (bp->b_flags & B_ERROR)) {
1007 struct aiocb *job = aiocbe->uuaiocb;
1008
1009 aiocbe->uaiocb._aiocb_private.status = 0;
1010 suword(&job->_aiocb_private.status, 0);
1011 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
1012 suword(&job->_aiocb_private.error, bp->b_error);
1013
1014 ki->kaio_buffer_finished_count++;
1015
1016 if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
1017 aiocbe->jobstate = JOBST_JOBBFINISHED;
1018 aiocbe->jobflags |= AIOCBLIST_DONE;
1019 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
1020 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1021 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
1022 notify = 1;
1023 }
1024 }
1025 splx(s);
1026 if (notify)
1027 KNOTE(&aiocbe->klist, 0);
1028 return 0;
1029
1030doerror:
1031 ki->kaio_buffer_count--;
1032 if (lj)
1033 lj->lioj_buffer_count--;
1034 aiocbe->bp = NULL;
1035 relpbuf(bp, NULL);
1036 return error;
1037}
1038
1039/*
1040 * This waits/tests physio completion.
1041 */
1042static int
1043aio_fphysio(struct aiocblist *iocb)
1044{
1045 int s;
1046 struct buf *bp;
1047 int error;
1048
1049 bp = iocb->bp;
1050
1051 s = splbio();
1052 while ((bp->b_flags & B_DONE) == 0) {
1053 if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) {
1054 if ((bp->b_flags & B_DONE) == 0) {
1055 splx(s);
1056 return EINPROGRESS;
1057 } else
1058 break;
1059 }
1060 }
1061 splx(s);
1062
1063 /* Release mapping into kernel space. */
1064 vunmapbuf(bp);
1065 iocb->bp = 0;
1066
1067 error = 0;
1068
1069 /* Check for an error. */
1070 if (bp->b_flags & B_ERROR)
1071 error = bp->b_error;
1072
1073 relpbuf(bp, NULL);
1074 return (error);
1075}
1076#endif /* VFS_AIO */
1077
1078/*
1079 * Wake up aio requests that may be serviceable now.
1080 */
1081void
1082aio_swake(struct socket *so, struct sockbuf *sb)
1083{
1084#ifndef VFS_AIO
1085 return;
1086#else
1087 struct aiocblist *cb,*cbn;
1088 struct proc *p;
1089 struct kaioinfo *ki = NULL;
1090 int opcode, wakecount = 0;
1091 struct aioproclist *aiop;
1092
1093 if (sb == &so->so_snd) {
1094 opcode = LIO_WRITE;
1095 so->so_snd.sb_flags &= ~SB_AIO;
1096 } else {
1097 opcode = LIO_READ;
1098 so->so_rcv.sb_flags &= ~SB_AIO;
1099 }
1100
1101 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1102 cbn = TAILQ_NEXT(cb, list);
1103 if (opcode == cb->uaiocb.aio_lio_opcode) {
1104 p = cb->userproc;
1105 ki = p->p_aioinfo;
1106 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1107 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1108 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1109 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1110 wakecount++;
1111 if (cb->jobstate != JOBST_JOBQGLOBAL)
1112 panic("invalid queue value");
1113 }
1114 }
1115
1116 while (wakecount--) {
1117 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1118 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1119 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1120 aiop->aioprocflags &= ~AIOP_FREE;
1121 wakeup(aiop->aioproc);
1122 }
1123 }
1124#endif /* VFS_AIO */
1125}
1126
1127#ifdef VFS_AIO
1128/*
1129 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1130 * technique is done in this code.
1131 */
1132static int
1133_aio_aqueue(struct proc *p, struct aiocb *job, struct aio_liojob *lj, int type)
1134{
1135 struct filedesc *fdp;
1136 struct file *fp;
1137 unsigned int fd;
1138 struct socket *so;
1139 int s;
1140 int error;
1141 int opcode, user_opcode;
1142 struct aiocblist *aiocbe;
1143 struct aioproclist *aiop;
1144 struct kaioinfo *ki;
1145 struct kevent kev;
1146 struct kqueue *kq;
1147 struct file *kq_fp;
1148
1149 if ((aiocbe = TAILQ_FIRST(&aio_freejobs)) != NULL)
1150 TAILQ_REMOVE(&aio_freejobs, aiocbe, list);
1151 else
1152 aiocbe = zalloc (aiocb_zone);
1153
1154 aiocbe->inputcharge = 0;
1155 aiocbe->outputcharge = 0;
1156 callout_handle_init(&aiocbe->timeouthandle);
1157 SLIST_INIT(&aiocbe->klist);
1158
1159 suword(&job->_aiocb_private.status, -1);
1160 suword(&job->_aiocb_private.error, 0);
1161 suword(&job->_aiocb_private.kernelinfo, -1);
1162
1163 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1164 if (error) {
1165 suword(&job->_aiocb_private.error, error);
1166 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1167 return error;
1168 }
1169 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1170 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1171 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1172 return EINVAL;
1173 }
1174
1175 /* Save userspace address of the job info. */
1176 aiocbe->uuaiocb = job;
1177
1178 /* Get the opcode. */
1179 user_opcode = aiocbe->uaiocb.aio_lio_opcode;
1180 if (type != LIO_NOP)
1181 aiocbe->uaiocb.aio_lio_opcode = type;
1182 opcode = aiocbe->uaiocb.aio_lio_opcode;
1183
1184 /* Get the fd info for process. */
1185 fdp = p->p_fd;
1186
1187 /*
1188 * Range check file descriptor.
1189 */
1190 fd = aiocbe->uaiocb.aio_fildes;
1191 if (fd >= fdp->fd_nfiles) {
1192 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1193 if (type == 0)
1194 suword(&job->_aiocb_private.error, EBADF);
1195 return EBADF;
1196 }
1197
1198 fp = aiocbe->fd_file = fdp->fd_ofiles[fd];
1199 if ((fp == NULL) || ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) ==
1200 0))) {
1201 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1202 if (type == 0)
1203 suword(&job->_aiocb_private.error, EBADF);
1204 return EBADF;
1205 }
1206 fhold(fp);
1207
1208 if (aiocbe->uaiocb.aio_offset == -1LL) {
1209 error = EINVAL;
1210 goto aqueue_fail;
1211 }
1212 error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1213 if (error) {
1214 error = EINVAL;
1215 goto aqueue_fail;
1216 }
1217 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1218 if (jobrefid == LONG_MAX)
1219 jobrefid = 1;
1220 else
1221 jobrefid++;
1222
1223 if (opcode == LIO_NOP) {
1224 fdrop(fp, p);
1225 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1226 if (type == 0) {
1227 suword(&job->_aiocb_private.error, 0);
1228 suword(&job->_aiocb_private.status, 0);
1229 suword(&job->_aiocb_private.kernelinfo, 0);
1230 }
1231 return 0;
1232 }
1233 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1234 if (type == 0)
1235 suword(&job->_aiocb_private.status, 0);
1236 error = EINVAL;
1237 goto aqueue_fail;
1238 }
1239
1240 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1241 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1242 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr;
1243 }
1244 else {
1245 /*
1246 * This method for requesting kevent-based notification won't
1247 * work on the alpha, since we're passing in a pointer
1248 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT-
1249 * based method instead.
1250 */
1251 if (user_opcode == LIO_NOP || user_opcode == LIO_READ ||
1252 user_opcode == LIO_WRITE)
1253 goto no_kqueue;
1254
1255 error = copyin((struct kevent *)(uintptr_t)user_opcode,
1256 &kev, sizeof(kev));
1257 if (error)
1258 goto aqueue_fail;
1259 }
1260 if ((u_int)kev.ident >= fdp->fd_nfiles ||
1261 (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL ||
1262 (kq_fp->f_type != DTYPE_KQUEUE)) {
1263 error = EBADF;
1264 goto aqueue_fail;
1265 }
1266 kq = (struct kqueue *)kq_fp->f_data;
1267 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1268 kev.filter = EVFILT_AIO;
1269 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1270 kev.data = (intptr_t)aiocbe;
1271 error = kqueue_register(kq, &kev, p);
1272aqueue_fail:
1273 if (error) {
1274 fdrop(fp, p);
1275 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1276 if (type == 0)
1277 suword(&job->_aiocb_private.error, error);
1278 goto done;
1279 }
1280no_kqueue:
1281
1282 suword(&job->_aiocb_private.error, EINPROGRESS);
1283 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1284 aiocbe->userproc = p;
1285 aiocbe->jobflags = 0;
1286 aiocbe->lio = lj;
1287 ki = p->p_aioinfo;
1288
1289 if (fp->f_type == DTYPE_SOCKET) {
1290 /*
1291 * Alternate queueing for socket ops: Reach down into the
1292 * descriptor to get the socket data. Then check to see if the
1293 * socket is ready to be read or written (based on the requested
1294 * operation).
1295 *
1296 * If it is not ready for io, then queue the aiocbe on the
1297 * socket, and set the flags so we get a call when sbnotify()
1298 * happens.
1299 */
1300 so = (struct socket *)fp->f_data;
1301 s = splnet();
1302 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1303 LIO_WRITE) && (!sowriteable(so)))) {
1304 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1305 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1306 if (opcode == LIO_READ)
1307 so->so_rcv.sb_flags |= SB_AIO;
1308 else
1309 so->so_snd.sb_flags |= SB_AIO;
1310 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1311 ki->kaio_queue_count++;
1312 num_queue_count++;
1313 splx(s);
1314 error = 0;
1315 goto done;
1316 }
1317 splx(s);
1318 }
1319
1320 if ((error = aio_qphysio(p, aiocbe)) == 0)
1321 goto done;
1322 if (error > 0) {
1323 suword(&job->_aiocb_private.status, 0);
1324 aiocbe->uaiocb._aiocb_private.error = error;
1325 suword(&job->_aiocb_private.error, error);
1326 goto done;
1327 }
1328
1329 /* No buffer for daemon I/O. */
1330 aiocbe->bp = NULL;
1331
1332 ki->kaio_queue_count++;
1333 if (lj)
1334 lj->lioj_queue_count++;
1335 s = splnet();
1336 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1337 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1338 splx(s);
1339 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1340
1341 num_queue_count++;
1342 error = 0;
1343
1344 /*
1345 * If we don't have a free AIO process, and we are below our quota, then
1346 * start one. Otherwise, depend on the subsequent I/O completions to
1347 * pick-up this job. If we don't sucessfully create the new process
1348 * (thread) due to resource issues, we return an error for now (EAGAIN),
1349 * which is likely not the correct thing to do.
1350 */
1351 s = splnet();
1352retryproc:
1353 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1354 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1355 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1356 aiop->aioprocflags &= ~AIOP_FREE;
1357 wakeup(aiop->aioproc);
1358 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1359 ((ki->kaio_active_count + num_aio_resv_start) <
1360 ki->kaio_maxactive_count)) {
1361 num_aio_resv_start++;
1362 if ((error = aio_newproc()) == 0) {
1363 num_aio_resv_start--;
1364 goto retryproc;
1365 }
1366 num_aio_resv_start--;
1367 }
1368 splx(s);
1369done:
1370 return error;
1371}
1372
1373/*
1374 * This routine queues an AIO request, checking for quotas.
1375 */
1376static int
1377aio_aqueue(struct proc *p, struct aiocb *job, int type)
1378{
1379 struct kaioinfo *ki;
1380
1381 if (p->p_aioinfo == NULL)
1382 aio_init_aioinfo(p);
1383
1384 if (num_queue_count >= max_queue_count)
1385 return EAGAIN;
1386
1387 ki = p->p_aioinfo;
1388 if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1389 return EAGAIN;
1390
1391 return _aio_aqueue(p, job, NULL, type);
1392}
1393#endif /* VFS_AIO */
1394
1395/*
1396 * Support the aio_return system call, as a side-effect, kernel resources are
1397 * released.
1398 */
1399int
1400aio_return(struct proc *p, struct aio_return_args *uap)
1401{
1402#ifndef VFS_AIO
1403 return ENOSYS;
1404#else
1405 int s;
1406 long jobref;
1407 struct aiocblist *cb, *ncb;
1408 struct aiocb *ujob;
1409 struct kaioinfo *ki;
1410
1411 ki = p->p_aioinfo;
1412 if (ki == NULL)
1413 return EINVAL;
1414
1415 ujob = uap->aiocbp;
1416
1417 jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1418 if (jobref == -1 || jobref == 0)
1419 return EINVAL;
1420
1421 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1422 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1423 jobref) {
1424 if (ujob == cb->uuaiocb) {
1425 p->p_retval[0] =
1426 cb->uaiocb._aiocb_private.status;
1427 } else
1428 p->p_retval[0] = EFAULT;
1429 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1430 p->p_stats->p_ru.ru_oublock +=
1431 cb->outputcharge;
1432 cb->outputcharge = 0;
1433 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1434 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1435 cb->inputcharge = 0;
1436 }
1437 aio_free_entry(cb);
1438 return 0;
1439 }
1440 }
1441 s = splbio();
1442 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1443 ncb = TAILQ_NEXT(cb, plist);
1444 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1445 == jobref) {
1446 splx(s);
1447 if (ujob == cb->uuaiocb) {
1448 p->p_retval[0] =
1449 cb->uaiocb._aiocb_private.status;
1450 } else
1451 p->p_retval[0] = EFAULT;
1452 aio_free_entry(cb);
1453 return 0;
1454 }
1455 }
1456 splx(s);
1457
1458 return (EINVAL);
1459#endif /* VFS_AIO */
1460}
1461
1462/*
1463 * Allow a process to wakeup when any of the I/O requests are completed.
1464 */
1465int
1466aio_suspend(struct proc *p, struct aio_suspend_args *uap)
1467{
1468#ifndef VFS_AIO
1469 return ENOSYS;
1470#else
1471 struct timeval atv;
1472 struct timespec ts;
1473 struct aiocb *const *cbptr, *cbp;
1474 struct kaioinfo *ki;
1475 struct aiocblist *cb;
1476 int i;
1477 int njoblist;
1478 int error, s, timo;
1479 long *ijoblist;
1480 struct aiocb **ujoblist;
1481
1482 if (uap->nent > AIO_LISTIO_MAX)
1483 return EINVAL;
1484
1485 timo = 0;
1486 if (uap->timeout) {
1487 /* Get timespec struct. */
1488 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1489 return error;
1490
1491 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1492 return (EINVAL);
1493
1494 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1495 if (itimerfix(&atv))
1496 return (EINVAL);
1497 timo = tvtohz(&atv);
1498 }
1499
1500 ki = p->p_aioinfo;
1501 if (ki == NULL)
1502 return EAGAIN;
1503
1504 njoblist = 0;
1505 ijoblist = zalloc(aiol_zone);
1506 ujoblist = zalloc(aiol_zone);
1507 cbptr = uap->aiocbp;
1508
1509 for (i = 0; i < uap->nent; i++) {
1510 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1511 if (cbp == 0)
1512 continue;
1513 ujoblist[njoblist] = cbp;
1514 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1515 njoblist++;
1516 }
1517
1518 if (njoblist == 0) {
1519 zfree(aiol_zone, ijoblist);
1520 zfree(aiol_zone, ujoblist);
1521 return 0;
1522 }
1523
1524 error = 0;
1525 for (;;) {
1526 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1527 for (i = 0; i < njoblist; i++) {
1528 if (((intptr_t)
1529 cb->uaiocb._aiocb_private.kernelinfo) ==
1530 ijoblist[i]) {
1531 if (ujoblist[i] != cb->uuaiocb)
1532 error = EINVAL;
1533 zfree(aiol_zone, ijoblist);
1534 zfree(aiol_zone, ujoblist);
1535 return error;
1536 }
1537 }
1538 }
1539
1540 s = splbio();
1541 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1542 TAILQ_NEXT(cb, plist)) {
1543 for (i = 0; i < njoblist; i++) {
1544 if (((intptr_t)
1545 cb->uaiocb._aiocb_private.kernelinfo) ==
1546 ijoblist[i]) {
1547 splx(s);
1548 if (ujoblist[i] != cb->uuaiocb)
1549 error = EINVAL;
1550 zfree(aiol_zone, ijoblist);
1551 zfree(aiol_zone, ujoblist);
1552 return error;
1553 }
1554 }
1555 }
1556
1557 ki->kaio_flags |= KAIO_WAKEUP;
1558 error = tsleep(p, PRIBIO | PCATCH, "aiospn", timo);
1559 splx(s);
1560
1561 if (error == ERESTART || error == EINTR) {
1562 zfree(aiol_zone, ijoblist);
1563 zfree(aiol_zone, ujoblist);
1564 return EINTR;
1565 } else if (error == EWOULDBLOCK) {
1566 zfree(aiol_zone, ijoblist);
1567 zfree(aiol_zone, ujoblist);
1568 return EAGAIN;
1569 }
1570 }
1571
1572/* NOTREACHED */
1573 return EINVAL;
1574#endif /* VFS_AIO */
1575}
1576
1577/*
1578 * aio_cancel cancels any non-physio aio operations not currently in
1579 * progress.
1580 */
1581int
1582aio_cancel(struct proc *p, struct aio_cancel_args *uap)
1583{
1584#ifndef VFS_AIO
1585 return ENOSYS;
1586#else
1587 struct kaioinfo *ki;
1588 struct aiocblist *cbe, *cbn;
1589 struct file *fp;
1590 struct filedesc *fdp;
1591 struct socket *so;
1592 struct proc *po;
1593 int s,error;
1594 int cancelled=0;
1595 int notcancelled=0;
1596 struct vnode *vp;
1597
1598 fdp = p->p_fd;
1599 if ((u_int)uap->fd >= fdp->fd_nfiles ||
1600 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1601 return (EBADF);
1602
1603 if (fp->f_type == DTYPE_VNODE) {
1604 vp = (struct vnode *)fp->f_data;
1605
1606 if (vn_isdisk(vp,&error)) {
1607 p->p_retval[0] = AIO_NOTCANCELED;
1608 return 0;
1609 }
1610 } else if (fp->f_type == DTYPE_SOCKET) {
1611 so = (struct socket *)fp->f_data;
1612
1613 s = splnet();
1614
1615 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1616 cbn = TAILQ_NEXT(cbe, list);
1617 if ((uap->aiocbp == NULL) ||
1618 (uap->aiocbp == cbe->uuaiocb) ) {
1619 po = cbe->userproc;
1620 ki = po->p_aioinfo;
1621 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1622 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1623 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1624 if (ki->kaio_flags & KAIO_WAKEUP) {
1625 wakeup(po);
1626 }
1627 cbe->jobstate = JOBST_JOBFINISHED;
1628 cbe->uaiocb._aiocb_private.status=-1;
1629 cbe->uaiocb._aiocb_private.error=ECANCELED;
1630 cancelled++;
1631/* XXX cancelled, knote? */
1632 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1633 SIGEV_SIGNAL)
1634 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1635 if (uap->aiocbp)
1636 break;
1637 }
1638 }
1639 splx(s);
1640
1641 if ((cancelled) && (uap->aiocbp)) {
1642 p->p_retval[0] = AIO_CANCELED;
1643 return 0;
1644 }
1645 }
1646 ki=p->p_aioinfo;
1647 if (ki == NULL)
1648 goto done;
1649 s = splnet();
1650
1651 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1652 cbn = TAILQ_NEXT(cbe, plist);
1653
1654 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1655 ((uap->aiocbp == NULL ) ||
1656 (uap->aiocbp == cbe->uuaiocb))) {
1657
1658 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1659 TAILQ_REMOVE(&aio_jobs, cbe, list);
1660 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1661 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1662 plist);
1663 cancelled++;
1664 ki->kaio_queue_finished_count++;
1665 cbe->jobstate = JOBST_JOBFINISHED;
1666 cbe->uaiocb._aiocb_private.status = -1;
1667 cbe->uaiocb._aiocb_private.error = ECANCELED;
1668/* XXX cancelled, knote? */
1669 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1670 SIGEV_SIGNAL)
1671 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1672 } else {
1673 notcancelled++;
1674 }
1675 }
1676 }
1677 splx(s);
1678done:
1679 if (notcancelled) {
1680 p->p_retval[0] = AIO_NOTCANCELED;
1681 return 0;
1682 }
1683 if (cancelled) {
1684 p->p_retval[0] = AIO_CANCELED;
1685 return 0;
1686 }
1687 p->p_retval[0] = AIO_ALLDONE;
1688
1689 return 0;
1690#endif /* VFS_AIO */
1691}
1692
1693/*
1694 * aio_error is implemented in the kernel level for compatibility purposes only.
1695 * For a user mode async implementation, it would be best to do it in a userland
1696 * subroutine.
1697 */
1698int
1699aio_error(struct proc *p, struct aio_error_args *uap)
1700{
1701#ifndef VFS_AIO
1702 return ENOSYS;
1703#else
1704 int s;
1705 struct aiocblist *cb;
1706 struct kaioinfo *ki;
1707 long jobref;
1708
1709 ki = p->p_aioinfo;
1710 if (ki == NULL)
1711 return EINVAL;
1712
1713 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1714 if ((jobref == -1) || (jobref == 0))
1715 return EINVAL;
1716
1717 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1718 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1719 jobref) {
1720 p->p_retval[0] = cb->uaiocb._aiocb_private.error;
1721 return 0;
1722 }
1723 }
1724
1725 s = splnet();
1726
1727 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1728 plist)) {
1729 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1730 jobref) {
1731 p->p_retval[0] = EINPROGRESS;
1732 splx(s);
1733 return 0;
1734 }
1735 }
1736
1737 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1738 plist)) {
1739 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1740 jobref) {
1741 p->p_retval[0] = EINPROGRESS;
1742 splx(s);
1743 return 0;
1744 }
1745 }
1746 splx(s);
1747
1748 s = splbio();
1749 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1750 plist)) {
1751 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1752 jobref) {
1753 p->p_retval[0] = cb->uaiocb._aiocb_private.error;
1754 splx(s);
1755 return 0;
1756 }
1757 }
1758
1759 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1760 plist)) {
1761 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1762 jobref) {
1763 p->p_retval[0] = EINPROGRESS;
1764 splx(s);
1765 return 0;
1766 }
1767 }
1768 splx(s);
1769
1770#if (0)
1771 /*
1772 * Hack for lio.
1773 */
1774 status = fuword(&uap->aiocbp->_aiocb_private.status);
1775 if (status == -1)
1776 return fuword(&uap->aiocbp->_aiocb_private.error);
1777#endif
1778 return EINVAL;
1779#endif /* VFS_AIO */
1780}
1781
1782/* syscall - asynchronous read from a file (REALTIME) */
1783int
1784aio_read(struct proc *p, struct aio_read_args *uap)
1785{
1786#ifndef VFS_AIO
1787 return ENOSYS;
1788#else
1789 return aio_aqueue(p, uap->aiocbp, LIO_READ);
1790#endif /* VFS_AIO */
1791}
1792
1793/* syscall - asynchronous write to a file (REALTIME) */
1794int
1795aio_write(struct proc *p, struct aio_write_args *uap)
1796{
1797#ifndef VFS_AIO
1798 return ENOSYS;
1799#else
1800 return aio_aqueue(p, uap->aiocbp, LIO_WRITE);
1801#endif /* VFS_AIO */
1802}
1803
1804/* syscall - XXX undocumented */
1805int
1806lio_listio(struct proc *p, struct lio_listio_args *uap)
1807{
1808#ifndef VFS_AIO
1809 return ENOSYS;
1810#else
1811 int nent, nentqueued;
1812 struct aiocb *iocb, * const *cbptr;
1813 struct aiocblist *cb;
1814 struct kaioinfo *ki;
1815 struct aio_liojob *lj;
1816 int error, runningcode;
1817 int nerror;
1818 int i;
1819 int s;
1820
1821 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1822 return EINVAL;
1823
1824 nent = uap->nent;
1825 if (nent > AIO_LISTIO_MAX)
1826 return EINVAL;
1827
1828 if (p->p_aioinfo == NULL)
1829 aio_init_aioinfo(p);
1830
1831 if ((nent + num_queue_count) > max_queue_count)
1832 return EAGAIN;
1833
1834 ki = p->p_aioinfo;
1835 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count)
1836 return EAGAIN;
1837
1838 lj = zalloc(aiolio_zone);
1839 if (!lj)
1840 return EAGAIN;
1841
1842 lj->lioj_flags = 0;
1843 lj->lioj_buffer_count = 0;
1844 lj->lioj_buffer_finished_count = 0;
1845 lj->lioj_queue_count = 0;
1846 lj->lioj_queue_finished_count = 0;
1847 lj->lioj_ki = ki;
1848
1849 /*
1850 * Setup signal.
1851 */
1852 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1853 error = copyin(uap->sig, &lj->lioj_signal,
1854 sizeof(lj->lioj_signal));
1855 if (error) {
1856 zfree(aiolio_zone, lj);
1857 return error;
1858 }
1859 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1860 zfree(aiolio_zone, lj);
1861 return EINVAL;
1862 }
1863 lj->lioj_flags |= LIOJ_SIGNAL;
1864 lj->lioj_flags &= ~LIOJ_SIGNAL_POSTED;
1865 } else
1866 lj->lioj_flags &= ~LIOJ_SIGNAL;
1867
1868 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1869 /*
1870 * Get pointers to the list of I/O requests.
1871 */
1872 nerror = 0;
1873 nentqueued = 0;
1874 cbptr = uap->acb_list;
1875 for (i = 0; i < uap->nent; i++) {
1876 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1877 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1878 error = _aio_aqueue(p, iocb, lj, 0);
1879 if (error == 0)
1880 nentqueued++;
1881 else
1882 nerror++;
1883 }
1884 }
1885
1886 /*
1887 * If we haven't queued any, then just return error.
1888 */
1889 if (nentqueued == 0)
1890 return 0;
1891
1892 /*
1893 * Calculate the appropriate error return.
1894 */
1895 runningcode = 0;
1896 if (nerror)
1897 runningcode = EIO;
1898
1899 if (uap->mode == LIO_WAIT) {
1900 int command, found, jobref;
1901
1902 for (;;) {
1903 found = 0;
1904 for (i = 0; i < uap->nent; i++) {
1905 /*
1906 * Fetch address of the control buf pointer in
1907 * user space.
1908 */
1909 iocb = (struct aiocb *)
1910 (intptr_t)fuword(&cbptr[i]);
1911 if (((intptr_t)iocb == -1) || ((intptr_t)iocb
1912 == 0))
1913 continue;
1914
1915 /*
1916 * Fetch the associated command from user space.
1917 */
1918 command = fuword(&iocb->aio_lio_opcode);
1919 if (command == LIO_NOP) {
1920 found++;
1921 continue;
1922 }
1923
1924 jobref = fuword(&iocb->_aiocb_private.kernelinfo);
1925
1926 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1927 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
1928 == jobref) {
1929 if (cb->uaiocb.aio_lio_opcode
1930 == LIO_WRITE) {
1931 p->p_stats->p_ru.ru_oublock
1932 +=
1933 cb->outputcharge;
1934 cb->outputcharge = 0;
1935 } else if (cb->uaiocb.aio_lio_opcode
1936 == LIO_READ) {
1937 p->p_stats->p_ru.ru_inblock
1938 += cb->inputcharge;
1939 cb->inputcharge = 0;
1940 }
1941 found++;
1942 break;
1943 }
1944 }
1945
1946 s = splbio();
1947 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
1948 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
1949 == jobref) {
1950 found++;
1951 break;
1952 }
1953 }
1954 splx(s);
1955 }
1956
1957 /*
1958 * If all I/Os have been disposed of, then we can
1959 * return.
1960 */
1961 if (found == nentqueued)
1962 return runningcode;
1963
1964 ki->kaio_flags |= KAIO_WAKEUP;
1965 error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0);
1966
1967 if (error == EINTR)
1968 return EINTR;
1969 else if (error == EWOULDBLOCK)
1970 return EAGAIN;
1971 }
1972 }
1973
1974 return runningcode;
1975#endif /* VFS_AIO */
1976}
1977
1978#ifdef VFS_AIO
1979/*
1980 * This is a weird hack so that we can post a signal. It is safe to do so from
1981 * a timeout routine, but *not* from an interrupt routine.
1982 */
1983static void
1984process_signal(void *aioj)
1985{
1986 struct aiocblist *aiocbe = aioj;
1987 struct aio_liojob *lj = aiocbe->lio;
1988 struct aiocb *cb = &aiocbe->uaiocb;
1989
1990 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) &&
1991 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) {
1992 psignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo);
1993 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1994 }
1995
1996 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL)
1997 psignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo);
1998}
1999
2000/*
2001 * Interrupt handler for physio, performs the necessary process wakeups, and
2002 * signals.
2003 */
2004static void
2005aio_physwakeup(struct buf *bp)
2006{
2007 struct aiocblist *aiocbe;
2008 struct proc *p;
2009 struct kaioinfo *ki;
2010 struct aio_liojob *lj;
2011
2012 wakeup(bp);
2013
2014 aiocbe = (struct aiocblist *)bp->b_spc;
2015 if (aiocbe) {
2016 p = bp->b_caller1;
2017
2018 aiocbe->jobstate = JOBST_JOBBFINISHED;
2019 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2020 aiocbe->uaiocb._aiocb_private.error = 0;
2021 aiocbe->jobflags |= AIOCBLIST_DONE;
2022
2023 if (bp->b_flags & B_ERROR)
2024 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2025
2026 lj = aiocbe->lio;
2027 if (lj) {
2028 lj->lioj_buffer_finished_count++;
2029
2030 /*
2031 * wakeup/signal if all of the interrupt jobs are done.
2032 */
2033 if (lj->lioj_buffer_finished_count ==
2034 lj->lioj_buffer_count) {
2035 /*
2036 * Post a signal if it is called for.
2037 */
2038 if ((lj->lioj_flags &
2039 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2040 LIOJ_SIGNAL) {
2041 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2042 aiocbe->timeouthandle =
2043 timeout(process_signal,
2044 aiocbe, 0);
2045 }
2046 }
2047 }
2048
2049 ki = p->p_aioinfo;
2050 if (ki) {
2051 ki->kaio_buffer_finished_count++;
2052 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
2053 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2054 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2055
2056 KNOTE(&aiocbe->klist, 0);
2057 /* Do the wakeup. */
2058 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2059 ki->kaio_flags &= ~KAIO_WAKEUP;
2060 wakeup(p);
2061 }
2062 }
2063
2064 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL)
2065 aiocbe->timeouthandle =
2066 timeout(process_signal, aiocbe, 0);
2067 }
2068}
2069#endif /* VFS_AIO */
2070
2071/* syscall - wait for the next completion of an aio request */
2072int
2073aio_waitcomplete(struct proc *p, struct aio_waitcomplete_args *uap)
2074{
2075#ifndef VFS_AIO
2076 return ENOSYS;
2077#else
2078 struct timeval atv;
2079 struct timespec ts;
2080 struct kaioinfo *ki;
2081 struct aiocblist *cb = NULL;
2082 int error, s, timo;
2083
2084 suword(uap->aiocbp, (int)NULL);
2085
2086 timo = 0;
2087 if (uap->timeout) {
2088 /* Get timespec struct. */
2089 error = copyin(uap->timeout, &ts, sizeof(ts));
2090 if (error)
2091 return error;
2092
2093 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2094 return (EINVAL);
2095
2096 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2097 if (itimerfix(&atv))
2098 return (EINVAL);
2099 timo = tvtohz(&atv);
2100 }
2101
2102 ki = p->p_aioinfo;
2103 if (ki == NULL)
2104 return EAGAIN;
2105
2106 for (;;) {
2107 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2108 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2109 p->p_retval[0] = cb->uaiocb._aiocb_private.status;
2110 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2111 p->p_stats->p_ru.ru_oublock +=
2112 cb->outputcharge;
2113 cb->outputcharge = 0;
2114 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2115 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2116 cb->inputcharge = 0;
2117 }
2118 aio_free_entry(cb);
2119 return cb->uaiocb._aiocb_private.error;
2120 }
2121
2122 s = splbio();
2123 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
2124 splx(s);
2125 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2126 p->p_retval[0] = cb->uaiocb._aiocb_private.status;
2127 aio_free_entry(cb);
2128 return cb->uaiocb._aiocb_private.error;
2129 }
2130
2131 ki->kaio_flags |= KAIO_WAKEUP;
2132 error = tsleep(p, PRIBIO | PCATCH, "aiowc", timo);
2133 splx(s);
2134
2135 if (error == ERESTART)
2136 return EINTR;
2137 else if (error < 0)
2138 return error;
2139 else if (error == EINTR)
2140 return EINTR;
2141 else if (error == EWOULDBLOCK)
2142 return EAGAIN;
2143 }
2144#endif /* VFS_AIO */
2145}
2146
2147#ifndef VFS_AIO
2148static int
2149filt_aioattach(struct knote *kn)
2150{
2151
2152 return (ENXIO);
2153}
2154
2155struct filterops aio_filtops =
2156 { 0, filt_aioattach, NULL, NULL };
2157
2158#else
2159/* kqueue attach function */
2160static int
2161filt_aioattach(struct knote *kn)
2162{
2163 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2164
2165 /*
2166 * The aiocbe pointer must be validated before using it, so
2167 * registration is restricted to the kernel; the user cannot
2168 * set EV_FLAG1.
2169 */
2170 if ((kn->kn_flags & EV_FLAG1) == 0)
2171 return (EPERM);
2172 kn->kn_flags &= ~EV_FLAG1;
2173
2174 SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext);
2175
2176 return (0);
2177}
2178
2179/* kqueue detach function */
2180static void
2181filt_aiodetach(struct knote *kn)
2182{
2183 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2184
2185 SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext);
2186}
2187
2188/* kqueue filter function */
2189/*ARGSUSED*/
2190static int
2191filt_aio(struct knote *kn, long hint)
2192{
2193 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2194
2195 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2196 if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2197 aiocbe->jobstate != JOBST_JOBBFINISHED)
2198 return (0);
2199 kn->kn_flags |= EV_EOF;
2200 return (1);
2201}
2202
2203struct filterops aio_filtops =
2204 { 0, filt_aioattach, filt_aiodetach, filt_aio };
2205#endif /* VFS_AIO */