Import of POSIX Asynchronous I/O.
Seems to be quite stable. Some work still left to do. Please note, that syscalls are not yet MP-safe, because of the file and vnode subsystems. Reviewed by: <tech-kern>, <ad>
This commit is contained in:
parent
906b9af2a7
commit
67d703cf25
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: mi,v 1.1020 2007/04/16 19:17:29 pooka Exp $
|
||||
# $NetBSD: mi,v 1.1021 2007/04/30 14:44:28 rmind Exp $
|
||||
./etc/mtree/set.comp comp-sys-root
|
||||
./usr/bin/addr2line comp-debug-bin bfd
|
||||
./usr/bin/ar comp-util-bin bfd
|
||||
|
@ -72,6 +72,7 @@
|
|||
./usr/bin/yacc comp-c-bin
|
||||
./usr/include/a.out.h comp-c-include
|
||||
./usr/include/adosfs/adosfs.h comp-c-include
|
||||
./usr/include/aio.h comp-c-include
|
||||
./usr/include/altq/altq.h comp-c-include
|
||||
./usr/include/altq/altq_afmap.h comp-c-include
|
||||
./usr/include/altq/altq_blue.h comp-c-include
|
||||
|
@ -1739,6 +1740,7 @@
|
|||
./usr/include/struct.h comp-c-include
|
||||
./usr/include/sys/acct.h comp-c-include
|
||||
./usr/include/sys/agpio.h comp-c-include
|
||||
./usr/include/sys/aio.h comp-c-include
|
||||
./usr/include/sys/ansi.h comp-c-include
|
||||
./usr/include/sys/ataio.h comp-c-include
|
||||
./usr/include/sys/audioio.h comp-c-include
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: Makefile,v 1.126 2007/02/09 22:13:31 ad Exp $
|
||||
# $NetBSD: Makefile,v 1.127 2007/04/30 14:44:29 rmind Exp $
|
||||
# @(#)Makefile 8.2 (Berkeley) 1/4/94
|
||||
|
||||
# Doing a make includes builds /usr/include
|
||||
|
@ -7,9 +7,9 @@ NOOBJ= # defined
|
|||
|
||||
# Missing: mp.h
|
||||
|
||||
INCS= a.out.h ar.h assert.h bitstring.h bm.h cpio.h ctype.h db.h dirent.h \
|
||||
disktab.h dlfcn.h err.h errno.h fmtmsg.h fnmatch.h fstab.h fts.h \
|
||||
ftw.h getopt.h glob.h grp.h ifaddrs.h iconv.h \
|
||||
INCS= a.out.h aio.h ar.h assert.h bitstring.h bm.h cpio.h ctype.h db.h \
|
||||
dirent.h disktab.h dlfcn.h err.h errno.h fmtmsg.h fnmatch.h fstab.h \
|
||||
fts.h ftw.h getopt.h glob.h grp.h ifaddrs.h iconv.h \
|
||||
inttypes.h iso646.h kvm.h langinfo.h libgen.h \
|
||||
limits.h link.h link_aout.h link_elf.h locale.h \
|
||||
login_cap.h lwp.h malloc.h math.h md2.h \
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/* $NetBSD: aio.h,v 1.1 2007/04/30 14:44:29 rmind Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _AIO_H_
|
||||
#define _AIO_H_
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <sys/signal.h>
|
||||
|
||||
/*
|
||||
* NOTE: Asynchronous I/O block structure and
|
||||
* constants are defined in sys/aio.h header.
|
||||
*/
|
||||
#include <sys/aio.h>
|
||||
|
||||
/* Prototypes */
|
||||
int aio_cancel(int, struct aiocb *);
|
||||
int aio_error(const struct aiocb *);
|
||||
int aio_fsync(int, struct aiocb *);
|
||||
int aio_read(struct aiocb *);
|
||||
ssize_t aio_return(struct aiocb *);
|
||||
int aio_suspend(const struct aiocb *const[], int, const struct timespec *);
|
||||
int aio_write(struct aiocb *);
|
||||
int lio_listio(int, struct aiocb *const[], int, struct sigevent *);
|
||||
|
||||
#endif /* _AIO_H_ */
|
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: Makefile,v 1.1 2003/01/24 01:52:43 thorpej Exp $
|
||||
# $NetBSD: Makefile,v 1.2 2007/04/30 14:44:29 rmind Exp $
|
||||
#
|
||||
|
||||
WARNS= 2
|
||||
|
@ -13,4 +13,20 @@ MLINKS+= sem_open.3 sem_close.3
|
|||
MLINKS+= sem_open.3 sem_unlink.3
|
||||
MLINKS+= sem_wait.3 sem_trywait.3
|
||||
|
||||
.include <bsd.own.mk>
|
||||
|
||||
.if exists(${NETBSDSRCDIR}/lib/libc/arch/${MACHINE_ARCH})
|
||||
ARCHDIR= ${NETBSDSRCDIR}/lib/libc/arch/${MACHINE_ARCH}
|
||||
.elif exists(${NETBSDSRCDIR}/lib/libc/arch/${MACHINE_CPU})
|
||||
ARCHDIR= ${NETBSDSRCDIR}/lib/libc/arch/${MACHINE_CPU}
|
||||
.else
|
||||
.BEGIN:
|
||||
@echo no ARCHDIR for ${MACHINE_ARCH} nor ${MACHINE_CPU}
|
||||
@false
|
||||
.endif
|
||||
|
||||
AFLAGS+=-I${ARCHDIR}
|
||||
|
||||
.include "${.CURDIR}/sys/Makefile.inc"
|
||||
|
||||
.include <bsd.lib.mk>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
# $NetBSD: Makefile.inc,v 1.1 2007/04/30 14:44:29 rmind Exp $
|
||||
|
||||
.PATH: ${.CURDIR}/sys
|
||||
|
||||
ASM= aio_cancel.S aio_error.S aio_fsync.S aio_read.S aio_return.S \
|
||||
aio_suspend.S aio_write.S lio_listio.S
|
||||
|
||||
SRCS+= ${ASM}
|
||||
CLEANFILES+= ${PSEUDO}
|
||||
|
||||
ASMDEPS= ${.CURDIR}/sys/Makefile.inc ${ARCHDIR}/SYS.h \
|
||||
${DESTDIR}/usr/include/sys/syscall.h
|
||||
|
||||
${ASM}: ${ASMDEPS}
|
||||
${_MKTARGET_CREATE}
|
||||
printf '#include "SYS.h"\nRSYSCALL(${.PREFIX})\n' >${.TARGET}
|
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: files,v 1.837 2007/04/14 19:33:29 tnn Exp $
|
||||
# $NetBSD: files,v 1.838 2007/04/30 14:44:29 rmind Exp $
|
||||
|
||||
# @(#)files.newconf 7.5 (Berkeley) 5/10/93
|
||||
|
||||
|
@ -1383,6 +1383,7 @@ file kern/uipc_socket.c
|
|||
file kern/uipc_socket2.c
|
||||
file kern/uipc_syscalls.c
|
||||
file kern/uipc_usrreq.c
|
||||
file kern/vfs_aio.c
|
||||
file kern/vfs_bio.c
|
||||
file kern/vfs_cache.c
|
||||
file kern/vfs_getcwd.c
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: db_command.c,v 1.95 2007/02/22 06:41:00 thorpej Exp $ */
|
||||
/* $NetBSD: db_command.c,v 1.96 2007/04/30 14:44:30 rmind Exp $ */
|
||||
|
||||
/*
|
||||
* Mach Operating System
|
||||
|
@ -31,7 +31,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.95 2007/02/22 06:41:00 thorpej Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.96 2007/04/30 14:44:30 rmind Exp $");
|
||||
|
||||
#include "opt_ddb.h"
|
||||
#include "opt_kgdb.h"
|
||||
|
@ -140,6 +140,7 @@ static const struct db_command db_show_all_cmds[] = {
|
|||
};
|
||||
|
||||
static const struct db_command db_show_cmds[] = {
|
||||
{ "aio_jobs", db_show_aio_jobs, 0, NULL },
|
||||
{ "all", NULL, 0, db_show_all_cmds },
|
||||
#if defined(INET) && (NARP > 0)
|
||||
{ "arptab", db_show_arptab, 0, NULL },
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: db_interface.h,v 1.19 2007/02/22 04:38:06 matt Exp $ */
|
||||
/* $NetBSD: db_interface.h,v 1.20 2007/04/30 14:44:30 rmind Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1995 The NetBSD Foundation, Inc.
|
||||
|
@ -65,6 +65,9 @@ void db_dmesg(db_expr_t, bool, db_expr_t, const char *);
|
|||
/* netinet/if_arp.c */
|
||||
void db_show_arptab(db_expr_t, bool, db_expr_t, const char *);
|
||||
|
||||
/* kern/vfs_aio.c */
|
||||
void db_show_aio_jobs(db_expr_t, bool, db_expr_t, const char *);
|
||||
|
||||
/*
|
||||
* This is used in several places to determine which printf format
|
||||
* string is appropriate for displaying a variable of type db_expr_t.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: db_xxx.c,v 1.42 2007/02/22 04:38:06 matt Exp $ */
|
||||
/* $NetBSD: db_xxx.c,v 1.43 2007/04/30 14:44:30 rmind Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1982, 1986, 1989, 1991, 1993
|
||||
|
@ -39,7 +39,7 @@
|
|||
#include "opt_kgdb.h"
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.42 2007/02/22 04:38:06 matt Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.43 2007/04/30 14:44:30 rmind Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
|
@ -114,6 +114,13 @@ db_kgdb_cmd(db_expr_t addr, bool haddr,
|
|||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
db_show_aio_jobs(db_expr_t addr, bool haddr,
|
||||
db_expr_t count, const char *modif)
|
||||
{
|
||||
aio_print_jobs(db_printf);
|
||||
}
|
||||
|
||||
void
|
||||
db_show_all_procs(db_expr_t addr, bool haddr,
|
||||
db_expr_t count, const char *modif)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: kern_exit.c,v 1.173 2007/03/29 17:35:17 ad Exp $ */
|
||||
/* $NetBSD: kern_exit.c,v 1.174 2007/04/30 14:44:30 rmind Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1998, 1999, 2006, 2007 The NetBSD Foundation, Inc.
|
||||
|
@ -74,7 +74,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.173 2007/03/29 17:35:17 ad Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.174 2007/04/30 14:44:30 rmind Exp $");
|
||||
|
||||
#include "opt_ktrace.h"
|
||||
#include "opt_perfctrs.h"
|
||||
|
@ -82,6 +82,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.173 2007/03/29 17:35:17 ad Exp $");
|
|||
#include "opt_sysv.h"
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/aio.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/tty.h>
|
||||
|
@ -235,6 +236,9 @@ exit1(struct lwp *l, int rv)
|
|||
} else
|
||||
mutex_exit(&p->p_smutex);
|
||||
|
||||
/* Destroy all AIO works */
|
||||
aio_exit(p);
|
||||
|
||||
/*
|
||||
* Drain all remaining references that procfs, ptrace and others may
|
||||
* have on the process.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: kern_fork.c,v 1.137 2007/03/13 00:35:44 ad Exp $ */
|
||||
/* $NetBSD: kern_fork.c,v 1.138 2007/04/30 14:44:30 rmind Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1999, 2001, 2004 The NetBSD Foundation, Inc.
|
||||
|
@ -76,7 +76,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.137 2007/03/13 00:35:44 ad Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.138 2007/04/30 14:44:30 rmind Exp $");
|
||||
|
||||
#include "opt_ktrace.h"
|
||||
#include "opt_systrace.h"
|
||||
|
@ -375,6 +375,7 @@ fork1(struct lwp *l1, int flags, int exitsig, void *stack, size_t stacksize,
|
|||
p2->p_pptr = parent;
|
||||
LIST_INIT(&p2->p_children);
|
||||
|
||||
p2->p_aio = NULL;
|
||||
|
||||
#ifdef KTRACE
|
||||
/*
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
$NetBSD: syscalls.master,v 1.168 2007/03/06 16:09:21 drochner Exp $
|
||||
$NetBSD: syscalls.master,v 1.169 2007/04/30 14:44:30 rmind Exp $
|
||||
|
||||
; @(#)syscalls.master 8.2 (Berkeley) 1/13/94
|
||||
|
||||
|
@ -793,3 +793,15 @@
|
|||
size_t fh_size, struct statvfs *buf, int flags); }
|
||||
398 STD { int sys___fhstat40(const void *fhp, \
|
||||
size_t fh_size, struct stat *sb); }
|
||||
|
||||
; Asynchronous I/O system calls
|
||||
399 STD { int sys_aio_cancel(int fildes, struct aiocb *aiocbp); }
|
||||
400 STD { int sys_aio_error(const struct aiocb *aiocbp); }
|
||||
401 STD { int sys_aio_fsync(int op, struct aiocb *aiocbp); }
|
||||
402 STD { int sys_aio_read(struct aiocb *aiocbp); }
|
||||
403 STD { int sys_aio_return(struct aiocb *aiocbp); }
|
||||
404 STD { int sys_aio_suspend(const struct aiocb *const *list, \
|
||||
int nent, const struct timespec *timeout); }
|
||||
405 STD { int sys_aio_write(struct aiocb *aiocbp); }
|
||||
406 STD { int sys_lio_listio(int mode, struct aiocb *const *list, \
|
||||
int nent, struct sigevent *sig); }
|
||||
|
|
|
@ -0,0 +1,993 @@
|
|||
/* $NetBSD: vfs_aio.c,v 1.1 2007/04/30 14:44:30 rmind Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* 1. Additional work for VCHR and maybe VBLK devices.
|
||||
* 2. Consider making the job-finding O(n) per one file descriptor.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: vfs_aio.c,v 1.1 2007/04/30 14:44:30 rmind Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
|
||||
#include <sys/condvar.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/filedesc.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/kmem.h>
|
||||
#include <sys/lwp.h>
|
||||
#include <sys/mutex.h>
|
||||
#include <sys/pool.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/signal.h>
|
||||
#include <sys/signalvar.h>
|
||||
#include <sys/syscallargs.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/vnode.h>
|
||||
|
||||
#include <uvm/uvm_extern.h>
|
||||
|
||||
/*
|
||||
* System-wide counter of AIO operations.
|
||||
* XXXSMP: We should spin-lock it, or modify atomically.
|
||||
*/
|
||||
static unsigned long aio_jobs_count = 0;
|
||||
|
||||
/* Prototypes */
|
||||
void aio_worker(void *);
|
||||
static void aio_process(struct aio_job *);
|
||||
static void aio_sendsig(struct proc *, struct sigevent *);
|
||||
static int aio_enqueue_job(int, void *, struct lio_req *);
|
||||
|
||||
/*
|
||||
* Initialize Asynchronous I/O data structures for the process.
|
||||
*/
|
||||
int
|
||||
aio_init(struct proc *p)
|
||||
{
|
||||
struct aioproc *aio;
|
||||
struct lwp *l;
|
||||
bool inmem;
|
||||
vaddr_t uaddr;
|
||||
|
||||
/* Allocate and initialize AIO structure */
|
||||
aio = kmem_zalloc(sizeof(struct aioproc), KM_NOSLEEP);
|
||||
if (aio == NULL)
|
||||
return EAGAIN;
|
||||
|
||||
/* Recheck if we are really first */
|
||||
mutex_enter(&p->p_mutex);
|
||||
if (p->p_aio) {
|
||||
mutex_exit(&p->p_mutex);
|
||||
kmem_free(aio, sizeof(struct aioproc));
|
||||
return 0;
|
||||
}
|
||||
p->p_aio = aio;
|
||||
|
||||
/* Initialize pools, queue and their synchronization structures */
|
||||
pool_init(&aio->jobs_pool, sizeof(struct aio_job), 0, 0, 0,
|
||||
"aio_jobs_pool", &pool_allocator_nointr, IPL_NONE);
|
||||
pool_init(&aio->lio_pool, sizeof(struct lio_req), 0, 0, 0,
|
||||
"aio_lio_pool", &pool_allocator_nointr, IPL_NONE);
|
||||
mutex_init(&aio->aio_mtx, MUTEX_DEFAULT, IPL_NONE);
|
||||
cv_init(&aio->aio_worker_cv, "aio_worker_cv");
|
||||
cv_init(&aio->done_cv, "aio_done_cv");
|
||||
TAILQ_INIT(&aio->jobs_queue);
|
||||
|
||||
/* It is safe to leave this window without AIO worker set */
|
||||
mutex_exit(&p->p_mutex);
|
||||
|
||||
/*
|
||||
* Create an AIO worker thread.
|
||||
* XXX: Currently, AIO thread is not protected against user's actions.
|
||||
*/
|
||||
inmem = uvm_uarea_alloc(&uaddr);
|
||||
if (uaddr == 0) {
|
||||
aio_exit(p);
|
||||
return EAGAIN;
|
||||
}
|
||||
if (newlwp(curlwp, p, uaddr, inmem, 0, NULL, 0,
|
||||
aio_worker, NULL, &l))
|
||||
{
|
||||
uvm_uarea_free(uaddr);
|
||||
aio_exit(p);
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
/* Complete the initialization of thread, and run it */
|
||||
mutex_enter(&p->p_smutex);
|
||||
aio->aio_worker = l;
|
||||
p->p_nrlwps++;
|
||||
lwp_lock(l);
|
||||
l->l_stat = LSRUN;
|
||||
l->l_usrpri = PRIBIO;
|
||||
setrunqueue(l);
|
||||
lwp_unlock(l);
|
||||
mutex_exit(&p->p_smutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Exit of Asynchronous I/O subsystem of process.
|
||||
*/
|
||||
void
|
||||
aio_exit(struct proc *p)
|
||||
{
|
||||
struct aio_job *a_job;
|
||||
|
||||
if (p->p_aio == NULL)
|
||||
return;
|
||||
|
||||
KASSERT(p->p_aio->aio_worker == NULL);
|
||||
|
||||
/* Free AIO queue */
|
||||
while (!TAILQ_EMPTY(&p->p_aio->jobs_queue)) {
|
||||
a_job = TAILQ_FIRST(&p->p_aio->jobs_queue);
|
||||
TAILQ_REMOVE(&p->p_aio->jobs_queue, a_job, list);
|
||||
pool_put(&p->p_aio->jobs_pool, a_job);
|
||||
aio_jobs_count--; /* XXXSMP */
|
||||
}
|
||||
|
||||
/* Destroy and free the entire AIO data structure */
|
||||
cv_destroy(&p->p_aio->aio_worker_cv);
|
||||
cv_destroy(&p->p_aio->done_cv);
|
||||
mutex_destroy(&p->p_aio->aio_mtx);
|
||||
pool_destroy(&p->p_aio->jobs_pool);
|
||||
pool_destroy(&p->p_aio->lio_pool);
|
||||
kmem_free(p->p_aio, sizeof(struct aioproc));
|
||||
p->p_aio = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* AIO worker thread and processor.
|
||||
*/
|
||||
void
|
||||
aio_worker(void *arg)
|
||||
{
|
||||
struct proc *p = curlwp->l_proc;
|
||||
struct aioproc *aio = p->p_aio;
|
||||
struct aio_job *a_job;
|
||||
struct lio_req *lio;
|
||||
sigset_t oss, nss;
|
||||
int error;
|
||||
|
||||
/*
|
||||
* Make an empty signal mask, so it
|
||||
* handles only SIGKILL and SIGSTOP.
|
||||
*/
|
||||
sigfillset(&nss);
|
||||
mutex_enter(&p->p_smutex);
|
||||
error = sigprocmask1(curlwp, SIG_SETMASK, &nss, &oss);
|
||||
KASSERT(error == 0);
|
||||
mutex_exit(&p->p_smutex);
|
||||
|
||||
for (;;) {
|
||||
/*
|
||||
* Loop for each job in the queue. If there
|
||||
* are no jobs - sleep and wait for the signal.
|
||||
*/
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
while ((a_job = TAILQ_FIRST(&aio->jobs_queue)) == NULL) {
|
||||
if (cv_wait_sig(&aio->aio_worker_cv,
|
||||
&aio->aio_mtx) == EINTR) {
|
||||
/*
|
||||
* Thread was interrupted by the
|
||||
* signal - check for exit.
|
||||
*/
|
||||
if (curlwp->l_flag & (LW_WEXIT | LW_WCORE))
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* Take the job from the queue */
|
||||
aio->curjob = a_job;
|
||||
TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
|
||||
|
||||
aio_jobs_count--; /* XXXSMP */
|
||||
aio->jobs_count--;
|
||||
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
/* Process an AIO operation */
|
||||
aio_process(a_job);
|
||||
|
||||
/* Copy data structure back to the user-space */
|
||||
(void)copyout(&a_job->aiocbp, a_job->aiocb_uptr,
|
||||
sizeof(struct aiocb));
|
||||
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
aio->curjob = NULL;
|
||||
/* Decrease a reference counter, if there is a LIO structure */
|
||||
lio = a_job->lio;
|
||||
if (lio) {
|
||||
lio->refcnt--;
|
||||
if (lio->refcnt || lio->dofree == false)
|
||||
lio = NULL;
|
||||
}
|
||||
/* Notify all suspenders */
|
||||
cv_broadcast(&aio->done_cv);
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
/* Send a signal, if any */
|
||||
aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
|
||||
|
||||
/* Destroy the LIO structure */
|
||||
if (lio) {
|
||||
aio_sendsig(p, &lio->sig);
|
||||
if (lio->dofree == true)
|
||||
pool_put(&aio->lio_pool, lio);
|
||||
}
|
||||
|
||||
/* Destroy the the job */
|
||||
pool_put(&aio->jobs_pool, a_job);
|
||||
}
|
||||
|
||||
exit:
|
||||
/*
|
||||
* Destroy oneself, the rest will be cared.
|
||||
*/
|
||||
aio->aio_worker = NULL;
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
/* Restore the old signal mask */
|
||||
mutex_enter(&p->p_smutex);
|
||||
error = sigprocmask1(curlwp, SIG_SETMASK, &oss, NULL);
|
||||
KASSERT(error == 0);
|
||||
mutex_exit(&p->p_smutex);
|
||||
}
|
||||
|
||||
static void
|
||||
aio_process(struct aio_job *a_job)
|
||||
{
|
||||
struct proc *p = curlwp->l_proc;
|
||||
struct aiocb *aiocbp = &a_job->aiocbp;
|
||||
struct file *fp;
|
||||
struct filedesc *fdp = p->p_fd;
|
||||
int fd = aiocbp->aio_fildes;
|
||||
int error = 0;
|
||||
|
||||
KASSERT(fdp != NULL);
|
||||
KASSERT(a_job->aio_op != 0);
|
||||
|
||||
if ((a_job->aio_op & AIO_READ) || (a_job->aio_op & AIO_WRITE)) {
|
||||
struct iovec aiov;
|
||||
struct uio auio;
|
||||
|
||||
fp = fd_getfile(fdp, fd);
|
||||
if (fp == NULL) {
|
||||
error = EBADF;
|
||||
goto done;
|
||||
}
|
||||
|
||||
aiov.iov_base = (void *)(uintptr_t)aiocbp->aio_buf;
|
||||
aiov.iov_len = aiocbp->aio_nbytes;
|
||||
auio.uio_iov = &aiov;
|
||||
auio.uio_iovcnt = 1;
|
||||
auio.uio_resid = aiocbp->aio_nbytes;
|
||||
auio.uio_vmspace = p->p_vmspace;
|
||||
|
||||
if (auio.uio_resid > SSIZE_MAX) {
|
||||
error = EINVAL;
|
||||
goto done;
|
||||
}
|
||||
|
||||
FILE_USE(fp);
|
||||
if (a_job->aio_op & AIO_READ) {
|
||||
/*
|
||||
* Perform a Read operation
|
||||
*/
|
||||
KASSERT((a_job->aio_op & AIO_WRITE) == 0);
|
||||
|
||||
if ((fp->f_flag & FREAD) == 0) {
|
||||
FILE_UNUSE(fp, curlwp);
|
||||
error = EBADF;
|
||||
goto done;
|
||||
}
|
||||
auio.uio_rw = UIO_READ;
|
||||
error = (*fp->f_ops->fo_read)(fp, &aiocbp->aio_offset,
|
||||
&auio, fp->f_cred, FOF_UPDATE_OFFSET);
|
||||
} else {
|
||||
/*
|
||||
* Perform a Write operation
|
||||
*/
|
||||
KASSERT(a_job->aio_op & AIO_WRITE);
|
||||
|
||||
if ((fp->f_flag & FWRITE) == 0) {
|
||||
FILE_UNUSE(fp, curlwp);
|
||||
error = EBADF;
|
||||
goto done;
|
||||
}
|
||||
auio.uio_rw = UIO_WRITE;
|
||||
error = (*fp->f_ops->fo_write)(fp, &aiocbp->aio_offset,
|
||||
&auio, fp->f_cred, FOF_UPDATE_OFFSET);
|
||||
}
|
||||
FILE_UNUSE(fp, curlwp);
|
||||
|
||||
/* Store the result value */
|
||||
a_job->aiocbp.aio_nbytes -= auio.uio_resid;
|
||||
a_job->aiocbp._retval = (error == 0) ?
|
||||
a_job->aiocbp.aio_nbytes : -1;
|
||||
|
||||
} else if((a_job->aio_op & AIO_SYNC) || (a_job->aio_op & AIO_DSYNC)) {
|
||||
/*
|
||||
* Perform a file Sync operation
|
||||
*/
|
||||
struct vnode *vp;
|
||||
|
||||
if ((error = getvnode(fdp, fd, &fp)) != 0)
|
||||
goto done;
|
||||
|
||||
if ((fp->f_flag & FWRITE) == 0) {
|
||||
FILE_UNUSE(fp, curlwp);
|
||||
error = EBADF;
|
||||
goto done;
|
||||
}
|
||||
|
||||
vp = (struct vnode *)fp->f_data;
|
||||
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
||||
if (a_job->aio_op & AIO_DSYNC) {
|
||||
error = VOP_FSYNC(vp, fp->f_cred,
|
||||
FSYNC_WAIT | FSYNC_DATAONLY, 0, 0, curlwp);
|
||||
} else if (a_job->aio_op & AIO_SYNC) {
|
||||
error = VOP_FSYNC(vp, fp->f_cred,
|
||||
FSYNC_WAIT, 0, 0, curlwp);
|
||||
if (error == 0 && bioops.io_fsync != NULL &&
|
||||
vp->v_mount &&
|
||||
(vp->v_mount->mnt_flag & MNT_SOFTDEP))
|
||||
(*bioops.io_fsync)(vp, 0);
|
||||
}
|
||||
VOP_UNLOCK(vp, 0);
|
||||
FILE_UNUSE(fp, curlwp);
|
||||
|
||||
/* Store the result value */
|
||||
a_job->aiocbp._retval = (error == 0) ? 0 : -1;
|
||||
|
||||
} else
|
||||
panic("aio_process: invalid operation code\n");
|
||||
|
||||
done:
|
||||
/* Job is done, set the error, if any */
|
||||
a_job->aiocbp._errno = error;
|
||||
a_job->aiocbp._state = JOB_DONE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Send AIO signal.
|
||||
*/
|
||||
static void
|
||||
aio_sendsig(struct proc *p, struct sigevent *sig)
|
||||
{
|
||||
ksiginfo_t ksi;
|
||||
|
||||
if (sig->sigev_signo == 0 || sig->sigev_notify == SIGEV_NONE)
|
||||
return;
|
||||
|
||||
KSI_INIT(&ksi);
|
||||
ksi.ksi_signo = sig->sigev_signo;
|
||||
ksi.ksi_code = SI_ASYNCIO;
|
||||
ksi.ksi_sigval = sig->sigev_value;
|
||||
mutex_enter(&proclist_mutex);
|
||||
kpsignal(p, &ksi, NULL);
|
||||
mutex_exit(&proclist_mutex);
|
||||
}
|
||||
|
||||
/*
|
||||
* Enqueue the job.
|
||||
*/
|
||||
static int
|
||||
aio_enqueue_job(int op, void *aiocb_uptr, struct lio_req *lio)
|
||||
{
|
||||
struct proc *p = curlwp->l_proc;
|
||||
struct aioproc *aio;
|
||||
struct aio_job *a_job;
|
||||
struct aiocb aiocbp;
|
||||
struct sigevent *sig;
|
||||
int error;
|
||||
|
||||
/* Check for the limit */
|
||||
if (aio_jobs_count + 1 > AIO_MAX) /* XXXSMP */
|
||||
return EAGAIN;
|
||||
|
||||
/* Get the data structure from user-space */
|
||||
error = copyin(aiocb_uptr, &aiocbp, sizeof(struct aiocb));
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
/* Check if signal is set, and validate it */
|
||||
sig = &aiocbp.aio_sigevent;
|
||||
if (sig->sigev_signo < 0 || sig->sigev_signo >= NSIG ||
|
||||
sig->sigev_notify < SIGEV_NONE || sig->sigev_notify > SIGEV_SA)
|
||||
return EINVAL;
|
||||
|
||||
/* Buffer and byte count */
|
||||
if (((AIO_SYNC | AIO_DSYNC) & op) == 0)
|
||||
if (aiocbp.aio_buf == NULL || aiocbp.aio_nbytes > SSIZE_MAX)
|
||||
return EINVAL;
|
||||
|
||||
/* Check the opcode, if LIO_NOP - simply ignore */
|
||||
if (op == AIO_LIO) {
|
||||
KASSERT(lio != NULL);
|
||||
if (aiocbp.aio_lio_opcode == LIO_WRITE)
|
||||
op = AIO_WRITE;
|
||||
else if (aiocbp.aio_lio_opcode == LIO_READ)
|
||||
op = AIO_READ;
|
||||
else
|
||||
return (aiocbp.aio_lio_opcode == LIO_NOP) ? 0 : EINVAL;
|
||||
} else {
|
||||
KASSERT(lio == NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Look for already existing job. If found - the job is in-progress.
|
||||
* According to POSIX this is invalid, so return the error.
|
||||
*/
|
||||
aio = p->p_aio;
|
||||
if (aio) {
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
if (aio->curjob) {
|
||||
a_job = aio->curjob;
|
||||
if (a_job->aiocb_uptr == aiocb_uptr) {
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
|
||||
if (a_job->aiocb_uptr != aiocb_uptr)
|
||||
continue;
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
return EINVAL;
|
||||
}
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if AIO structure is initialized, if not - initialize it.
|
||||
* In LIO case, we did that already. We will recheck this with
|
||||
* the lock in aio_init().
|
||||
*/
|
||||
if (lio == NULL && p->p_aio == NULL)
|
||||
if (aio_init(p))
|
||||
return EAGAIN;
|
||||
aio = p->p_aio;
|
||||
|
||||
/*
|
||||
* Set the state with errno, and copy data
|
||||
* structure back to the user-space.
|
||||
*/
|
||||
aiocbp._state = JOB_WIP;
|
||||
aiocbp._errno = EINPROGRESS;
|
||||
aiocbp._retval = -1;
|
||||
error = copyout(&aiocbp, aiocb_uptr, sizeof(struct aiocb));
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
/* Allocate and initialize a new AIO job */
|
||||
a_job = pool_get(&aio->jobs_pool, PR_WAITOK);
|
||||
memset(a_job, 0, sizeof(struct aio_job));
|
||||
|
||||
/*
|
||||
* Set the data.
|
||||
* Store the user-space pointer for searching. Since we
|
||||
* are storing only per proc pointers - it is safe.
|
||||
*/
|
||||
memcpy(&a_job->aiocbp, &aiocbp, sizeof(struct aiocb));
|
||||
a_job->aiocb_uptr = aiocb_uptr;
|
||||
a_job->aio_op |= op;
|
||||
a_job->lio = lio;
|
||||
|
||||
/*
|
||||
* Add the job to the queue, update the counters, and
|
||||
* notify the AIO worker thread to handle the job.
|
||||
*/
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
|
||||
/* Fail, if the limit was reached */
|
||||
if (aio->jobs_count >= AIO_LISTIO_MAX) {
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
pool_put(&aio->jobs_pool, a_job);
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
TAILQ_INSERT_TAIL(&aio->jobs_queue, a_job, list);
|
||||
aio_jobs_count++; /* XXXSMP */
|
||||
aio->jobs_count++;
|
||||
if (lio)
|
||||
lio->refcnt++;
|
||||
cv_signal(&aio->aio_worker_cv);
|
||||
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
/*
|
||||
* One would handle the errors only with aio_error() function.
|
||||
* This way is appropriate according to POSIX.
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Syscall functions.
|
||||
*/
|
||||
|
||||
int
|
||||
sys_aio_cancel(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_cancel_args /* {
|
||||
syscallarg(int) fildes;
|
||||
syscallarg(struct aiocb *) aiocbp;
|
||||
} */ *uap = v;
|
||||
struct proc *p = l->l_proc;
|
||||
struct aioproc *aio;
|
||||
struct aio_job *a_job;
|
||||
struct aiocb *aiocbp_ptr;
|
||||
struct lio_req *lio;
|
||||
struct filedesc *fdp = p->p_fd;
|
||||
unsigned int cn, error, fildes;
|
||||
|
||||
TAILQ_HEAD(, aio_job) tmp_jobs_list;
|
||||
|
||||
/* Check for invalid file descriptor */
|
||||
fildes = (unsigned int)SCARG(uap, fildes);
|
||||
if (fildes >= fdp->fd_nfiles || fdp->fd_ofiles[fildes] == NULL)
|
||||
return EBADF;
|
||||
|
||||
/* Check if AIO structure is initialized */
|
||||
if (p->p_aio == NULL) {
|
||||
*retval = AIO_NOTCANCELED;
|
||||
return 0;
|
||||
}
|
||||
|
||||
aio = p->p_aio;
|
||||
aiocbp_ptr = (struct aiocb *)SCARG(uap, aiocbp);
|
||||
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
|
||||
/* Cancel the jobs, and remove them from the queue */
|
||||
cn = 0;
|
||||
TAILQ_INIT(&tmp_jobs_list);
|
||||
TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
|
||||
if (aiocbp_ptr) {
|
||||
if (aiocbp_ptr != a_job->aiocb_uptr)
|
||||
continue;
|
||||
if (fildes != a_job->aiocbp.aio_fildes) {
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
return EBADF;
|
||||
}
|
||||
} else if (a_job->aiocbp.aio_fildes != fildes)
|
||||
continue;
|
||||
|
||||
/* Set the errno and copy structures back to the user-space */
|
||||
a_job->aiocbp._errno = ECANCELED;
|
||||
a_job->aiocbp._state = JOB_DONE;
|
||||
error = copyout(&a_job->aiocbp, a_job->aiocb_uptr,
|
||||
sizeof(struct aiocb));
|
||||
if (error) {
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
return error;
|
||||
}
|
||||
|
||||
TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
|
||||
TAILQ_INSERT_TAIL(&tmp_jobs_list, a_job, list);
|
||||
|
||||
/* Decrease the counters */
|
||||
aio_jobs_count--; /* XXXSMP */
|
||||
aio->jobs_count--;
|
||||
lio = a_job->lio;
|
||||
if (lio) {
|
||||
lio->refcnt--;
|
||||
if (lio->refcnt || lio->dofree == false)
|
||||
a_job->lio = NULL;
|
||||
}
|
||||
|
||||
cn++;
|
||||
if (aiocbp_ptr)
|
||||
break;
|
||||
}
|
||||
|
||||
/* There are canceled jobs */
|
||||
if (cn)
|
||||
*retval = AIO_CANCELED;
|
||||
|
||||
/* We cannot cancel current job */
|
||||
a_job = aio->curjob;
|
||||
if (a_job && ((a_job->aiocbp.aio_fildes == fildes) ||
|
||||
(a_job->aiocb_uptr == aiocbp_ptr)))
|
||||
*retval = AIO_NOTCANCELED;
|
||||
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
/* Free the jobs after the lock */
|
||||
while (!TAILQ_EMPTY(&tmp_jobs_list)) {
|
||||
a_job = TAILQ_FIRST(&tmp_jobs_list);
|
||||
TAILQ_REMOVE(&tmp_jobs_list, a_job, list);
|
||||
/* Send a signal if any */
|
||||
aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
|
||||
if (a_job->lio)
|
||||
pool_put(&aio->lio_pool, a_job->lio);
|
||||
pool_put(&aio->jobs_pool, a_job);
|
||||
}
|
||||
|
||||
/* Set a correct return value */
|
||||
if (*retval == 0)
|
||||
*retval = AIO_ALLDONE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sys_aio_error(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_error_args /* {
|
||||
syscallarg(const struct aiocb *) aiocbp;
|
||||
} */ *uap = v;
|
||||
struct proc *p = l->l_proc;
|
||||
struct aioproc *aio = p->p_aio;
|
||||
struct aiocb aiocbp;
|
||||
int error;
|
||||
|
||||
if (aio == NULL)
|
||||
return EINVAL;
|
||||
|
||||
error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
if (aiocbp._state == JOB_NONE)
|
||||
return EINVAL;
|
||||
|
||||
*retval = aiocbp._errno;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sys_aio_fsync(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_fsync_args /* {
|
||||
syscallarg(int) op;
|
||||
syscallarg(struct aiocb *) aiocbp;
|
||||
} */ *uap = v;
|
||||
int op = SCARG(uap, op);
|
||||
|
||||
if ((op != O_DSYNC) && (op != O_SYNC))
|
||||
return EINVAL;
|
||||
|
||||
op = O_DSYNC ? AIO_DSYNC : AIO_SYNC;
|
||||
|
||||
return aio_enqueue_job(op, SCARG(uap, aiocbp), NULL);
|
||||
}
|
||||
|
||||
int
|
||||
sys_aio_read(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_read_args /* {
|
||||
syscallarg(struct aiocb *) aiocbp;
|
||||
} */ *uap = v;
|
||||
|
||||
return aio_enqueue_job(AIO_READ, SCARG(uap, aiocbp), NULL);
|
||||
}
|
||||
|
||||
int
|
||||
sys_aio_return(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_return_args /* {
|
||||
syscallarg(struct aiocb *) aiocbp;
|
||||
} */ *uap = v;
|
||||
struct proc *p = l->l_proc;
|
||||
struct aioproc *aio = p->p_aio;
|
||||
struct aiocb aiocbp;
|
||||
int error;
|
||||
|
||||
if (aio == NULL)
|
||||
return EINVAL;
|
||||
|
||||
error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
if (aiocbp._errno == EINPROGRESS || aiocbp._state != JOB_DONE)
|
||||
return EINVAL;
|
||||
|
||||
*retval = aiocbp._retval;
|
||||
|
||||
/* Reset the internal variables */
|
||||
aiocbp._errno = 0;
|
||||
aiocbp._retval = -1;
|
||||
aiocbp._state = JOB_NONE;
|
||||
error = copyout(&aiocbp, SCARG(uap, aiocbp), sizeof(struct aiocb));
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
int
|
||||
sys_aio_suspend(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_suspend_args /* {
|
||||
syscallarg(const struct aiocb *const[]) list;
|
||||
syscallarg(int) nent;
|
||||
syscallarg(const struct timespec *) timeout;
|
||||
} */ *uap = v;
|
||||
struct proc *p = l->l_proc;
|
||||
struct aioproc *aio;
|
||||
struct aio_job *a_job;
|
||||
struct aiocb **aiocbp_list;
|
||||
struct timespec ts;
|
||||
int i, error, nent, timo;
|
||||
|
||||
if (p->p_aio == NULL)
|
||||
return EAGAIN;
|
||||
aio = p->p_aio;
|
||||
|
||||
nent = SCARG(uap, nent);
|
||||
if (nent <= 0 || nent > AIO_LISTIO_MAX)
|
||||
return EAGAIN;
|
||||
|
||||
if (SCARG(uap, timeout)) {
|
||||
/* Convert timespec to ticks */
|
||||
error = copyin(SCARG(uap, timeout), &ts,
|
||||
sizeof(struct timespec));
|
||||
if (error)
|
||||
return error;
|
||||
timo = mstohz((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
|
||||
if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
|
||||
timo = 1;
|
||||
if (timo <= 0)
|
||||
return EAGAIN;
|
||||
} else
|
||||
timo = 0;
|
||||
|
||||
/* Get the list from user-space */
|
||||
aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
|
||||
error = copyin(SCARG(uap, list), aiocbp_list,
|
||||
nent * sizeof(struct aiocb));
|
||||
if (error) {
|
||||
kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
|
||||
return error;
|
||||
}
|
||||
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
for (;;) {
|
||||
|
||||
for (i = 0; i < nent; i++) {
|
||||
|
||||
/* Skip NULL entries */
|
||||
if (aiocbp_list[i] == NULL)
|
||||
continue;
|
||||
|
||||
/* Skip current job */
|
||||
if (aio->curjob) {
|
||||
a_job = aio->curjob;
|
||||
if (a_job->aiocb_uptr == aiocbp_list[i])
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Look for a job in the queue */
|
||||
TAILQ_FOREACH(a_job, &aio->jobs_queue, list)
|
||||
if (a_job->aiocb_uptr == aiocbp_list[i])
|
||||
break;
|
||||
|
||||
if (a_job == NULL) {
|
||||
struct aiocb aiocbp;
|
||||
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
error = copyin(aiocbp_list[i], &aiocbp,
|
||||
sizeof(struct aiocb));
|
||||
if (error == 0 && aiocbp._state != JOB_DONE) {
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
continue;
|
||||
}
|
||||
|
||||
kmem_free(aiocbp_list,
|
||||
nent * sizeof(struct aio_job));
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for a signal or when timeout occurs */
|
||||
error = cv_timedwait_sig(&aio->done_cv, &aio->aio_mtx, timo);
|
||||
if (error) {
|
||||
if (error == EWOULDBLOCK)
|
||||
error = EAGAIN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
|
||||
kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
|
||||
return error;
|
||||
}
|
||||
|
||||
int
|
||||
sys_aio_write(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_aio_write_args /* {
|
||||
syscallarg(struct aiocb *) aiocbp;
|
||||
} */ *uap = v;
|
||||
|
||||
return aio_enqueue_job(AIO_WRITE, SCARG(uap, aiocbp), NULL);
|
||||
}
|
||||
|
||||
int
|
||||
sys_lio_listio(struct lwp *l, void *v, register_t *retval)
|
||||
{
|
||||
struct sys_lio_listio_args /* {
|
||||
syscallarg(int) mode;
|
||||
syscallarg(struct aiocb *const[]) list;
|
||||
syscallarg(int) nent;
|
||||
syscallarg(struct sigevent *) sig;
|
||||
} */ *uap = v;
|
||||
struct proc *p = l->l_proc;
|
||||
struct aioproc *aio;
|
||||
struct aiocb **aiocbp_list;
|
||||
struct lio_req *lio;
|
||||
struct sigevent sig;
|
||||
int i, error, errcnt, mode, nent;
|
||||
|
||||
mode = SCARG(uap, mode);
|
||||
nent = SCARG(uap, nent);
|
||||
|
||||
/* Check for the limits, and invalid values */
|
||||
if (nent < 1 || nent > AIO_LISTIO_MAX)
|
||||
return EINVAL;
|
||||
if (aio_jobs_count + nent > AIO_MAX) /* XXXSMP */
|
||||
return EAGAIN;
|
||||
if (mode != LIO_NOWAIT && mode != LIO_WAIT)
|
||||
return EINVAL;
|
||||
|
||||
/* Check for signal, validate it */
|
||||
if (mode == LIO_NOWAIT && SCARG(uap, sig)) {
|
||||
error = copyin(SCARG(uap, sig), &sig, sizeof(struct sigevent));
|
||||
if (error)
|
||||
return error;
|
||||
if (sig.sigev_signo < 0 || sig.sigev_signo >= NSIG ||
|
||||
sig.sigev_notify < SIGEV_NONE ||
|
||||
sig.sigev_notify > SIGEV_SA)
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Check if AIO structure is initialized, if not - initialize it */
|
||||
if (p->p_aio == NULL)
|
||||
if (aio_init(p))
|
||||
return EAGAIN;
|
||||
aio = p->p_aio;
|
||||
|
||||
/* Create a LIO structure */
|
||||
lio = pool_get(&aio->lio_pool, PR_WAITOK);
|
||||
if (SCARG(uap, sig))
|
||||
memcpy(&lio->sig, &sig, sizeof(struct sigevent));
|
||||
else
|
||||
memset(&lio->sig, 0, sizeof(struct sigevent));
|
||||
lio->dofree = (mode == LIO_WAIT) ? false : true;
|
||||
lio->refcnt = 1; /* XXX: Hack */
|
||||
|
||||
/* Get the list from user-space */
|
||||
aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
|
||||
error = copyin(SCARG(uap, list), aiocbp_list,
|
||||
nent * sizeof(struct aiocb));
|
||||
if (error)
|
||||
goto err;
|
||||
|
||||
/* Enqueue all jobs */
|
||||
errcnt = 0;
|
||||
for (i = 0; i < nent; i++) {
|
||||
if (i == (nent - 1)) /* XXX: Hack */
|
||||
lio->refcnt--;
|
||||
error = aio_enqueue_job(AIO_LIO, aiocbp_list[i], lio);
|
||||
/*
|
||||
* According to POSIX, in such error case it may
|
||||
* fail with other I/O operations initiated.
|
||||
*/
|
||||
if (error)
|
||||
errcnt++;
|
||||
}
|
||||
|
||||
/* Return an error, if any */
|
||||
if (errcnt) {
|
||||
error = EIO;
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (mode == LIO_WAIT) {
|
||||
/*
|
||||
* Wait for AIO completion. In such case,
|
||||
* the LIO structure will be freed here.
|
||||
*/
|
||||
error = 0;
|
||||
mutex_enter(&aio->aio_mtx);
|
||||
while (lio->refcnt || error)
|
||||
error = cv_wait_sig(&aio->done_cv, &aio->aio_mtx);
|
||||
mutex_exit(&aio->aio_mtx);
|
||||
if (error)
|
||||
error = EINTR;
|
||||
}
|
||||
|
||||
err:
|
||||
kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
|
||||
if (mode == LIO_WAIT) {
|
||||
KASSERT(lio != NULL);
|
||||
pool_put(&aio->lio_pool, lio);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*
|
||||
* Debugging
|
||||
*/
|
||||
#if defined(DDB)
|
||||
void
|
||||
aio_print_jobs(void (*pr)(const char *, ...))
|
||||
{
|
||||
struct proc *p = (curlwp == NULL ? NULL : curlwp->l_proc);
|
||||
struct aioproc *aio;
|
||||
struct aio_job *a_job;
|
||||
struct aiocb *aiocbp;
|
||||
|
||||
if (p == NULL) {
|
||||
(*pr)("AIO: We are not in the processes right now.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
aio = p->p_aio;
|
||||
if (aio == NULL) {
|
||||
(*pr)("AIO data is not initialized (PID = %d).\n", p->p_pid);
|
||||
return;
|
||||
}
|
||||
|
||||
(*pr)("AIO: PID = %d\n", p->p_pid);
|
||||
(*pr)("AIO: Global count of the jobs = %u\n", aio_jobs_count);
|
||||
(*pr)("AIO: Count of the jobs = %u\n", aio->jobs_count);
|
||||
|
||||
if (aio->curjob) {
|
||||
a_job = aio->curjob;
|
||||
(*pr)("\nAIO current job:\n");
|
||||
(*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
|
||||
a_job->aio_op, a_job->aiocbp._errno,
|
||||
a_job->aiocbp._state, a_job->aiocb_uptr);
|
||||
aiocbp = &a_job->aiocbp;
|
||||
(*pr)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
|
||||
aiocbp->aio_fildes, aiocbp->aio_offset,
|
||||
aiocbp->aio_buf, aiocbp->aio_nbytes);
|
||||
}
|
||||
|
||||
(*pr)("\nAIO queue:\n");
|
||||
TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
|
||||
(*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
|
||||
a_job->aio_op, a_job->aiocbp._errno,
|
||||
a_job->aiocbp._state, a_job->aiocb_uptr);
|
||||
aiocbp = &a_job->aiocbp;
|
||||
(*pr)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
|
||||
aiocbp->aio_fildes, aiocbp->aio_offset,
|
||||
aiocbp->aio_buf, aiocbp->aio_nbytes);
|
||||
}
|
||||
}
|
||||
#endif /* defined(DDB) */
|
|
@ -1,8 +1,8 @@
|
|||
# $NetBSD: Makefile,v 1.93 2007/03/12 16:31:54 ad Exp $
|
||||
# $NetBSD: Makefile,v 1.94 2007/04/30 14:44:31 rmind Exp $
|
||||
|
||||
INCSDIR= /usr/include/sys
|
||||
|
||||
INCS= acct.h agpio.h ansi.h ataio.h audioio.h \
|
||||
INCS= acct.h agpio.h aio.h ansi.h ataio.h audioio.h \
|
||||
bootblock.h bswap.h buf.h \
|
||||
callout.h cdefs.h cdefs_aout.h \
|
||||
cdefs_elf.h cdio.h chio.h clockctl.h condvar.h conf.h core.h \
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
/* $NetBSD: aio.h,v 1.1 2007/04/30 14:44:31 rmind Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _SYS_AIO_H_
|
||||
#define _SYS_AIO_H_
|
||||
|
||||
/* Returned by aio_cancel() */
|
||||
#define AIO_CANCELED 0x1
|
||||
#define AIO_NOTCANCELED 0x2
|
||||
#define AIO_ALLDONE 0x3
|
||||
|
||||
/* LIO opcodes */
|
||||
#define LIO_NOP 0x0
|
||||
#define LIO_WRITE 0x1
|
||||
#define LIO_READ 0x2
|
||||
|
||||
/* LIO modes */
|
||||
#define LIO_NOWAIT 0x0
|
||||
#define LIO_WAIT 0x1
|
||||
|
||||
/*
|
||||
* Asynchronous I/O structure.
|
||||
* Defined in the Base Definitions volume of IEEE Std 1003.1-2001 .
|
||||
*/
|
||||
struct aiocb {
|
||||
int aio_fildes; /* File descriptor */
|
||||
off_t aio_offset; /* File offset */
|
||||
volatile void *aio_buf; /* I/O buffer in process space */
|
||||
size_t aio_nbytes; /* Length of transfer */
|
||||
int aio_reqprio; /* Request priority offset */
|
||||
struct sigevent aio_sigevent; /* Signal to deliver */
|
||||
int aio_lio_opcode; /* LIO opcode */
|
||||
|
||||
/* Internal kernel variables */
|
||||
char _state; /* State of the job */
|
||||
int _errno; /* Error value */
|
||||
ssize_t _retval; /* Return value */
|
||||
};
|
||||
|
||||
/* Internal kernel data */
|
||||
#ifdef _KERNEL
|
||||
|
||||
/* Maximal number of allowed AIO operations */
|
||||
/* XXX: These will be changeable via sysctl */
|
||||
#define AIO_LISTIO_MAX 512
|
||||
#define AIO_MAX AIO_LISTIO_MAX * 16
|
||||
|
||||
#include <sys/condvar.h>
|
||||
#include <sys/lwp.h>
|
||||
#include <sys/mutex.h>
|
||||
#include <sys/pool.h>
|
||||
#include <sys/queue.h>
|
||||
|
||||
/* Operations (as flags) */
|
||||
#define AIO_LIO 0x00
|
||||
#define AIO_READ 0x01
|
||||
#define AIO_WRITE 0x02
|
||||
#define AIO_SYNC 0x04
|
||||
#define AIO_DSYNC 0x08
|
||||
|
||||
/* Job states */
|
||||
#define JOB_NONE 0x0
|
||||
#define JOB_WIP 0x1
|
||||
#define JOB_DONE 0x2
|
||||
|
||||
/* Structure of AIO job */
|
||||
struct aio_job {
|
||||
int aio_op; /* Operation code */
|
||||
struct aiocb aiocbp; /* AIO data structure */
|
||||
void *aiocb_uptr; /* User-space pointer for identification of job */
|
||||
TAILQ_ENTRY(aio_job) list;
|
||||
struct lio_req *lio;
|
||||
};
|
||||
|
||||
/* LIO structure */
|
||||
struct lio_req {
|
||||
u_int refcnt; /* Reference counter */
|
||||
struct sigevent sig; /* Signal of lio_listio() calls */
|
||||
bool dofree; /* Worker will free the structure */
|
||||
};
|
||||
|
||||
/* Structure of AIO data for process */
|
||||
struct aioproc {
|
||||
kmutex_t aio_mtx; /* Protects the entire structure */
|
||||
kcondvar_t aio_worker_cv; /* Signals on a new job */
|
||||
kcondvar_t done_cv; /* Signals when the job is done */
|
||||
struct aio_job *curjob; /* Currently processing AIO job */
|
||||
struct pool jobs_pool; /* Jobs pool */
|
||||
unsigned int jobs_count; /* Count of the jobs */
|
||||
TAILQ_HEAD(, aio_job) jobs_queue; /* Queue of the AIO jobs */
|
||||
struct pool lio_pool; /* List of LIO operations */
|
||||
struct lwp *aio_worker; /* AIO worker thread */
|
||||
};
|
||||
|
||||
/* Prototypes */
|
||||
int aio_init(struct proc *);
|
||||
void aio_exit(struct proc *);
|
||||
#if defined(DDB)
|
||||
void aio_print_jobs(void (*pr)(const char *, ...));
|
||||
#endif
|
||||
|
||||
#endif /* _KERNEL */
|
||||
|
||||
#endif /* _SYS_AIO_H_ */
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: proc.h,v 1.244 2007/03/11 21:36:48 ad Exp $ */
|
||||
/* $NetBSD: proc.h,v 1.245 2007/04/30 14:44:31 rmind Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
|
||||
|
@ -82,6 +82,7 @@
|
|||
#endif
|
||||
|
||||
#include <machine/proc.h> /* Machine-dependent proc substruct */
|
||||
#include <sys/aio.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/rwlock.h>
|
||||
#include <sys/mutex.h>
|
||||
|
@ -231,6 +232,7 @@ struct proc {
|
|||
struct plimit *p_limit; /* Process limits */
|
||||
struct vmspace *p_vmspace; /* Address space */
|
||||
struct sigacts *p_sigacts; /* Process sigactions */
|
||||
struct aioproc *p_aio; /* p: Asynchronous I/O data */
|
||||
|
||||
specificdata_reference
|
||||
p_specdataref; /* subsystem proc-specific data */
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: unistd.h,v 1.35 2006/08/14 18:17:48 rpaulo Exp $ */
|
||||
/* $NetBSD: unistd.h,v 1.36 2007/04/30 14:44:31 rmind Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
|
@ -61,6 +61,8 @@
|
|||
#define _POSIX2_VERSION 199212L
|
||||
|
||||
/* execution-time symbolic constants */
|
||||
/* asynchronous I/O is available */
|
||||
#define _POSIX_ASYNCHRONOUS_IO 200112L
|
||||
/* chown requires appropriate privileges */
|
||||
#define _POSIX_CHOWN_RESTRICTED 1
|
||||
/* clock selection */
|
||||
|
|
Loading…
Reference in New Issue