xref: /freebsd/sys/kern/vfs_aio.c (revision 38d68e2d42b76c891ded3b4657d04705cd3b35b6)
19454b2d8SWarner Losh /*-
2ee877a35SJohn Dyson  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
3ee877a35SJohn Dyson  *
4ee877a35SJohn Dyson  * Redistribution and use in source and binary forms, with or without
5ee877a35SJohn Dyson  * modification, are permitted provided that the following conditions
6ee877a35SJohn Dyson  * are met:
7ee877a35SJohn Dyson  * 1. Redistributions of source code must retain the above copyright
8ee877a35SJohn Dyson  *    notice, this list of conditions and the following disclaimer.
9ee877a35SJohn Dyson  * 2. John S. Dyson's name may not be used to endorse or promote products
10ee877a35SJohn Dyson  *    derived from this software without specific prior written permission.
11ee877a35SJohn Dyson  *
12ee877a35SJohn Dyson  * DISCLAIMER:  This code isn't warranted to do anything useful.  Anything
13ee877a35SJohn Dyson  * bad that happens because of using this software isn't the responsibility
14ee877a35SJohn Dyson  * of the author.  This software is distributed AS-IS.
15ee877a35SJohn Dyson  */
16ee877a35SJohn Dyson 
17ee877a35SJohn Dyson /*
188a6472b7SPeter Dufault  * This file contains support for the POSIX 1003.1B AIO/LIO facility.
19ee877a35SJohn Dyson  */
20ee877a35SJohn Dyson 
21677b542eSDavid E. O'Brien #include <sys/cdefs.h>
22677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
23677b542eSDavid E. O'Brien 
243858a1f4SJohn Baldwin #include "opt_compat.h"
253858a1f4SJohn Baldwin 
26ee877a35SJohn Dyson #include <sys/param.h>
27ee877a35SJohn Dyson #include <sys/systm.h>
28f591779bSSeigo Tanimura #include <sys/malloc.h>
299626b608SPoul-Henning Kamp #include <sys/bio.h>
30a5c9bce7SBruce Evans #include <sys/buf.h>
314a144410SRobert Watson #include <sys/capsicum.h>
3275b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
33ee877a35SJohn Dyson #include <sys/sysproto.h>
34ee877a35SJohn Dyson #include <sys/filedesc.h>
35ee877a35SJohn Dyson #include <sys/kernel.h>
3677409fe1SPoul-Henning Kamp #include <sys/module.h>
37c9a970a7SAlan Cox #include <sys/kthread.h>
38ee877a35SJohn Dyson #include <sys/fcntl.h>
39ee877a35SJohn Dyson #include <sys/file.h>
40104a9b7eSAlexander Kabaev #include <sys/limits.h>
41fdebd4f0SBruce Evans #include <sys/lock.h>
4235e0e5b3SJohn Baldwin #include <sys/mutex.h>
43ee877a35SJohn Dyson #include <sys/unistd.h>
446aeb05d7STom Rhodes #include <sys/posix4.h>
45ee877a35SJohn Dyson #include <sys/proc.h>
462d2f8ae7SBruce Evans #include <sys/resourcevar.h>
47ee877a35SJohn Dyson #include <sys/signalvar.h>
48bfbbc4aaSJason Evans #include <sys/protosw.h>
4989f6b863SAttilio Rao #include <sys/rwlock.h>
501ce91824SDavid Xu #include <sys/sema.h>
511ce91824SDavid Xu #include <sys/socket.h>
52bfbbc4aaSJason Evans #include <sys/socketvar.h>
5321d56e9cSAlfred Perlstein #include <sys/syscall.h>
5421d56e9cSAlfred Perlstein #include <sys/sysent.h>
55a624e84fSJohn Dyson #include <sys/sysctl.h>
56ee99e978SBruce Evans #include <sys/sx.h>
571ce91824SDavid Xu #include <sys/taskqueue.h>
58fd3bf775SJohn Dyson #include <sys/vnode.h>
59fd3bf775SJohn Dyson #include <sys/conf.h>
60cb679c38SJonathan Lemon #include <sys/event.h>
6199eee864SDavid Xu #include <sys/mount.h>
62f743d981SAlexander Motin #include <geom/geom.h>
63ee877a35SJohn Dyson 
641ce91824SDavid Xu #include <machine/atomic.h>
651ce91824SDavid Xu 
66ee877a35SJohn Dyson #include <vm/vm.h>
67f743d981SAlexander Motin #include <vm/vm_page.h>
68ee877a35SJohn Dyson #include <vm/vm_extern.h>
692244ea07SJohn Dyson #include <vm/pmap.h>
702244ea07SJohn Dyson #include <vm/vm_map.h>
7199eee864SDavid Xu #include <vm/vm_object.h>
72c897b813SJeff Roberson #include <vm/uma.h>
73ee877a35SJohn Dyson #include <sys/aio.h>
745aaef07cSJohn Dyson 
75dd85920aSJason Evans #include "opt_vfs_aio.h"
76ee877a35SJohn Dyson 
77eb8e6d52SEivind Eklund /*
78eb8e6d52SEivind Eklund  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
7999eee864SDavid Xu  * overflow. (XXX will be removed soon.)
80eb8e6d52SEivind Eklund  */
8199eee864SDavid Xu static u_long jobrefid;
822244ea07SJohn Dyson 
8399eee864SDavid Xu /*
8499eee864SDavid Xu  * Counter for aio_fsync.
8599eee864SDavid Xu  */
8699eee864SDavid Xu static uint64_t jobseqno;
8799eee864SDavid Xu 
8899eee864SDavid Xu #define JOBST_NULL		0
8999eee864SDavid Xu #define JOBST_JOBQSOCK		1
9099eee864SDavid Xu #define JOBST_JOBQGLOBAL	2
9199eee864SDavid Xu #define JOBST_JOBRUNNING	3
9299eee864SDavid Xu #define JOBST_JOBFINISHED	4
9399eee864SDavid Xu #define JOBST_JOBQBUF		5
9499eee864SDavid Xu #define JOBST_JOBQSYNC		6
952244ea07SJohn Dyson 
9684af4da6SJohn Dyson #ifndef MAX_AIO_PER_PROC
972244ea07SJohn Dyson #define MAX_AIO_PER_PROC	32
9884af4da6SJohn Dyson #endif
9984af4da6SJohn Dyson 
10084af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE_PER_PROC
1012244ea07SJohn Dyson #define MAX_AIO_QUEUE_PER_PROC	256 /* Bigger than AIO_LISTIO_MAX */
10284af4da6SJohn Dyson #endif
10384af4da6SJohn Dyson 
10484af4da6SJohn Dyson #ifndef MAX_AIO_PROCS
105fd3bf775SJohn Dyson #define MAX_AIO_PROCS		32
10684af4da6SJohn Dyson #endif
10784af4da6SJohn Dyson 
10884af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE
1092244ea07SJohn Dyson #define	MAX_AIO_QUEUE		1024 /* Bigger than AIO_LISTIO_MAX */
11084af4da6SJohn Dyson #endif
11184af4da6SJohn Dyson 
11284af4da6SJohn Dyson #ifndef TARGET_AIO_PROCS
113bfbbc4aaSJason Evans #define TARGET_AIO_PROCS	4
11484af4da6SJohn Dyson #endif
11584af4da6SJohn Dyson 
11684af4da6SJohn Dyson #ifndef MAX_BUF_AIO
11784af4da6SJohn Dyson #define MAX_BUF_AIO		16
11884af4da6SJohn Dyson #endif
11984af4da6SJohn Dyson 
12084af4da6SJohn Dyson #ifndef AIOD_TIMEOUT_DEFAULT
12184af4da6SJohn Dyson #define	AIOD_TIMEOUT_DEFAULT	(10 * hz)
12284af4da6SJohn Dyson #endif
12384af4da6SJohn Dyson 
12484af4da6SJohn Dyson #ifndef AIOD_LIFETIME_DEFAULT
12584af4da6SJohn Dyson #define AIOD_LIFETIME_DEFAULT	(30 * hz)
12684af4da6SJohn Dyson #endif
1272244ea07SJohn Dyson 
128e603be7aSRobert Watson FEATURE(aio, "Asynchronous I/O");
129e603be7aSRobert Watson 
1303858a1f4SJohn Baldwin static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
1313858a1f4SJohn Baldwin 
1325ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
133eb8e6d52SEivind Eklund 
134303b270bSEivind Eklund static int max_aio_procs = MAX_AIO_PROCS;
135a624e84fSJohn Dyson SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
136eb8e6d52SEivind Eklund 	CTLFLAG_RW, &max_aio_procs, 0,
137eb8e6d52SEivind Eklund 	"Maximum number of kernel threads to use for handling async IO ");
138a624e84fSJohn Dyson 
139eb8e6d52SEivind Eklund static int num_aio_procs = 0;
140a624e84fSJohn Dyson SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
141eb8e6d52SEivind Eklund 	CTLFLAG_RD, &num_aio_procs, 0,
142eb8e6d52SEivind Eklund 	"Number of presently active kernel threads for async IO");
143a624e84fSJohn Dyson 
144eb8e6d52SEivind Eklund /*
145eb8e6d52SEivind Eklund  * The code will adjust the actual number of AIO processes towards this
146eb8e6d52SEivind Eklund  * number when it gets a chance.
147eb8e6d52SEivind Eklund  */
148eb8e6d52SEivind Eklund static int target_aio_procs = TARGET_AIO_PROCS;
149eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
150eb8e6d52SEivind Eklund 	0, "Preferred number of ready kernel threads for async IO");
151a624e84fSJohn Dyson 
152eb8e6d52SEivind Eklund static int max_queue_count = MAX_AIO_QUEUE;
153eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
154eb8e6d52SEivind Eklund     "Maximum number of aio requests to queue, globally");
155a624e84fSJohn Dyson 
156eb8e6d52SEivind Eklund static int num_queue_count = 0;
157eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
158eb8e6d52SEivind Eklund     "Number of queued aio requests");
159a624e84fSJohn Dyson 
160eb8e6d52SEivind Eklund static int num_buf_aio = 0;
161eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
162eb8e6d52SEivind Eklund     "Number of aio requests presently handled by the buf subsystem");
163fd3bf775SJohn Dyson 
164eb8e6d52SEivind Eklund /* Number of async I/O thread in the process of being started */
165a9bf5e37SDavid Xu /* XXX This should be local to aio_aqueue() */
166eb8e6d52SEivind Eklund static int num_aio_resv_start = 0;
167fd3bf775SJohn Dyson 
168eb8e6d52SEivind Eklund static int aiod_timeout;
169eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
170eb8e6d52SEivind Eklund     "Timeout value for synchronous aio operations");
17184af4da6SJohn Dyson 
172eb8e6d52SEivind Eklund static int aiod_lifetime;
173eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
174eb8e6d52SEivind Eklund     "Maximum lifetime for idle aiod");
17584af4da6SJohn Dyson 
176eb8e6d52SEivind Eklund static int unloadable = 0;
17721d56e9cSAlfred Perlstein SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
17821d56e9cSAlfred Perlstein     "Allow unload of aio (not recommended)");
17921d56e9cSAlfred Perlstein 
180eb8e6d52SEivind Eklund 
181eb8e6d52SEivind Eklund static int max_aio_per_proc = MAX_AIO_PER_PROC;
182eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
183eb8e6d52SEivind Eklund     0, "Maximum active aio requests per process (stored in the process)");
184eb8e6d52SEivind Eklund 
185eb8e6d52SEivind Eklund static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
186eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
187eb8e6d52SEivind Eklund     &max_aio_queue_per_proc, 0,
188eb8e6d52SEivind Eklund     "Maximum queued aio requests per process (stored in the process)");
189eb8e6d52SEivind Eklund 
190eb8e6d52SEivind Eklund static int max_buf_aio = MAX_BUF_AIO;
191eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
192eb8e6d52SEivind Eklund     "Maximum buf aio requests per process (stored in the process)");
193eb8e6d52SEivind Eklund 
1940972628aSDavid Xu typedef struct oaiocb {
1950972628aSDavid Xu 	int	aio_fildes;		/* File descriptor */
1960972628aSDavid Xu 	off_t	aio_offset;		/* File offset for I/O */
1970972628aSDavid Xu 	volatile void *aio_buf;         /* I/O buffer in process space */
1980972628aSDavid Xu 	size_t	aio_nbytes;		/* Number of bytes for I/O */
1990972628aSDavid Xu 	struct	osigevent aio_sigevent;	/* Signal to deliver */
2000972628aSDavid Xu 	int	aio_lio_opcode;		/* LIO opcode */
2010972628aSDavid Xu 	int	aio_reqprio;		/* Request priority -- ignored */
2020972628aSDavid Xu 	struct	__aiocb_private	_aiocb_private;
2030972628aSDavid Xu } oaiocb_t;
2040972628aSDavid Xu 
2051aa4c324SDavid Xu /*
2061aa4c324SDavid Xu  * Below is a key of locks used to protect each member of struct aiocblist
2071aa4c324SDavid Xu  * aioliojob and kaioinfo and any backends.
2081aa4c324SDavid Xu  *
2091aa4c324SDavid Xu  * * - need not protected
210759ccccaSDavid Xu  * a - locked by kaioinfo lock
2111aa4c324SDavid Xu  * b - locked by backend lock, the backend lock can be null in some cases,
2121aa4c324SDavid Xu  *     for example, BIO belongs to this type, in this case, proc lock is
2131aa4c324SDavid Xu  *     reused.
2141aa4c324SDavid Xu  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
2151aa4c324SDavid Xu  */
2161aa4c324SDavid Xu 
2171aa4c324SDavid Xu /*
2181aa4c324SDavid Xu  * Current, there is only two backends: BIO and generic file I/O.
2191aa4c324SDavid Xu  * socket I/O is served by generic file I/O, this is not a good idea, since
2201aa4c324SDavid Xu  * disk file I/O and any other types without O_NONBLOCK flag can block daemon
2211aa4c324SDavid Xu  * threads, if there is no thread to serve socket I/O, the socket I/O will be
2221aa4c324SDavid Xu  * delayed too long or starved, we should create some threads dedicated to
2231aa4c324SDavid Xu  * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
2241aa4c324SDavid Xu  * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
2251aa4c324SDavid Xu  * structure is not safe because there is race between userland and aio
2261aa4c324SDavid Xu  * daemons.
2271aa4c324SDavid Xu  */
2281aa4c324SDavid Xu 
22948dac059SAlan Cox struct aiocblist {
2301aa4c324SDavid Xu 	TAILQ_ENTRY(aiocblist) list;	/* (b) internal list of for backend */
2311aa4c324SDavid Xu 	TAILQ_ENTRY(aiocblist) plist;	/* (a) list of jobs for each backend */
2321aa4c324SDavid Xu 	TAILQ_ENTRY(aiocblist) allist;  /* (a) list of all jobs in proc */
2331aa4c324SDavid Xu 	int	jobflags;		/* (a) job flags */
2341aa4c324SDavid Xu 	int	jobstate;		/* (b) job state */
2351aa4c324SDavid Xu 	int	inputcharge;		/* (*) input blockes */
2361aa4c324SDavid Xu 	int	outputcharge;		/* (*) output blockes */
237f743d981SAlexander Motin 	struct	bio *bp;		/* (*) BIO backend BIO pointer */
238f743d981SAlexander Motin 	struct	buf *pbuf;		/* (*) BIO backend buffer pointer */
239f743d981SAlexander Motin 	struct	vm_page *pages[btoc(MAXPHYS)+1]; /* BIO backend pages */
240f743d981SAlexander Motin 	int	npages;			/* BIO backend number of pages */
2411aa4c324SDavid Xu 	struct	proc *userproc;		/* (*) user process */
2421aa4c324SDavid Xu 	struct  ucred *cred;		/* (*) active credential when created */
2431aa4c324SDavid Xu 	struct	file *fd_file;		/* (*) pointer to file structure */
2441aa4c324SDavid Xu 	struct	aioliojob *lio;		/* (*) optional lio job */
2451aa4c324SDavid Xu 	struct	aiocb *uuaiocb;		/* (*) pointer in userspace of aiocb */
2461aa4c324SDavid Xu 	struct	knlist klist;		/* (a) list of knotes */
2471aa4c324SDavid Xu 	struct	aiocb uaiocb;		/* (*) kernel I/O control block */
2481aa4c324SDavid Xu 	ksiginfo_t ksi;			/* (a) realtime signal info */
24999eee864SDavid Xu 	uint64_t seqno;			/* (*) job number */
25099eee864SDavid Xu 	int	pending;		/* (a) number of pending I/O, aio_fsync only */
25148dac059SAlan Cox };
25248dac059SAlan Cox 
25348dac059SAlan Cox /* jobflags */
25499eee864SDavid Xu #define AIOCBLIST_DONE		0x01
25599eee864SDavid Xu #define AIOCBLIST_BUFDONE	0x02
2561ce91824SDavid Xu #define AIOCBLIST_RUNDOWN	0x04
25799eee864SDavid Xu #define AIOCBLIST_CHECKSYNC	0x08
25848dac059SAlan Cox 
2592244ea07SJohn Dyson /*
2602244ea07SJohn Dyson  * AIO process info
2612244ea07SJohn Dyson  */
26284af4da6SJohn Dyson #define AIOP_FREE	0x1			/* proc on free queue */
26384af4da6SJohn Dyson 
264b40ce416SJulian Elischer struct aiothreadlist {
2651aa4c324SDavid Xu 	int aiothreadflags;			/* (c) AIO proc flags */
2661aa4c324SDavid Xu 	TAILQ_ENTRY(aiothreadlist) list;	/* (c) list of processes */
2671aa4c324SDavid Xu 	struct thread *aiothread;		/* (*) the AIO thread */
2682244ea07SJohn Dyson };
2692244ea07SJohn Dyson 
27084af4da6SJohn Dyson /*
27184af4da6SJohn Dyson  * data-structure for lio signal management
27284af4da6SJohn Dyson  */
2731ce91824SDavid Xu struct aioliojob {
2741aa4c324SDavid Xu 	int	lioj_flags;			/* (a) listio flags */
2751aa4c324SDavid Xu 	int	lioj_count;			/* (a) listio flags */
2761aa4c324SDavid Xu 	int	lioj_finished_count;		/* (a) listio flags */
2771aa4c324SDavid Xu 	struct	sigevent lioj_signal;		/* (a) signal on all I/O done */
2781aa4c324SDavid Xu 	TAILQ_ENTRY(aioliojob) lioj_list;	/* (a) lio list */
2791aa4c324SDavid Xu 	struct  knlist klist;			/* (a) list of knotes */
2801aa4c324SDavid Xu 	ksiginfo_t lioj_ksi;			/* (a) Realtime signal info */
28184af4da6SJohn Dyson };
2821ce91824SDavid Xu 
28384af4da6SJohn Dyson #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
28484af4da6SJohn Dyson #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
28569cd28daSDoug Ambrisko #define LIOJ_KEVENT_POSTED	0x4	/* kevent triggered */
28684af4da6SJohn Dyson 
28784af4da6SJohn Dyson /*
28884af4da6SJohn Dyson  * per process aio data structure
28984af4da6SJohn Dyson  */
2902244ea07SJohn Dyson struct kaioinfo {
291759ccccaSDavid Xu 	struct mtx	kaio_mtx;	/* the lock to protect this struct */
2921aa4c324SDavid Xu 	int	kaio_flags;		/* (a) per process kaio flags */
2931aa4c324SDavid Xu 	int	kaio_maxactive_count;	/* (*) maximum number of AIOs */
2941aa4c324SDavid Xu 	int	kaio_active_count;	/* (c) number of currently used AIOs */
2951aa4c324SDavid Xu 	int	kaio_qallowed_count;	/* (*) maxiumu size of AIO queue */
2961aa4c324SDavid Xu 	int	kaio_count;		/* (a) size of AIO queue */
2971aa4c324SDavid Xu 	int	kaio_ballowed_count;	/* (*) maximum number of buffers */
2981aa4c324SDavid Xu 	int	kaio_buffer_count;	/* (a) number of physio buffers */
2991aa4c324SDavid Xu 	TAILQ_HEAD(,aiocblist) kaio_all;	/* (a) all AIOs in the process */
3001aa4c324SDavid Xu 	TAILQ_HEAD(,aiocblist) kaio_done;	/* (a) done queue for process */
3011aa4c324SDavid Xu 	TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
3021aa4c324SDavid Xu 	TAILQ_HEAD(,aiocblist) kaio_jobqueue;	/* (a) job queue for process */
3031aa4c324SDavid Xu 	TAILQ_HEAD(,aiocblist) kaio_bufqueue;	/* (a) buffer job queue for process */
3041aa4c324SDavid Xu 	TAILQ_HEAD(,aiocblist) kaio_sockqueue;  /* (a) queue for aios waiting on sockets,
30599eee864SDavid Xu 						 *  NOT USED YET.
3061aa4c324SDavid Xu 						 */
30799eee864SDavid Xu 	TAILQ_HEAD(,aiocblist) kaio_syncqueue;	/* (a) queue for aio_fsync */
30899eee864SDavid Xu 	struct	task	kaio_task;	/* (*) task to kick aio threads */
3092244ea07SJohn Dyson };
3102244ea07SJohn Dyson 
311759ccccaSDavid Xu #define AIO_LOCK(ki)		mtx_lock(&(ki)->kaio_mtx)
312759ccccaSDavid Xu #define AIO_UNLOCK(ki)		mtx_unlock(&(ki)->kaio_mtx)
313759ccccaSDavid Xu #define AIO_LOCK_ASSERT(ki, f)	mtx_assert(&(ki)->kaio_mtx, (f))
314759ccccaSDavid Xu #define AIO_MTX(ki)		(&(ki)->kaio_mtx)
315759ccccaSDavid Xu 
31684af4da6SJohn Dyson #define KAIO_RUNDOWN	0x1	/* process is being run down */
317bfbbc4aaSJason Evans #define KAIO_WAKEUP	0x2	/* wakeup process when there is a significant event */
318fd3bf775SJohn Dyson 
3193858a1f4SJohn Baldwin /*
3203858a1f4SJohn Baldwin  * Operations used to interact with userland aio control blocks.
3213858a1f4SJohn Baldwin  * Different ABIs provide their own operations.
3223858a1f4SJohn Baldwin  */
3233858a1f4SJohn Baldwin struct aiocb_ops {
3243858a1f4SJohn Baldwin 	int	(*copyin)(struct aiocb *ujob, struct aiocb *kjob);
3253858a1f4SJohn Baldwin 	long	(*fetch_status)(struct aiocb *ujob);
3263858a1f4SJohn Baldwin 	long	(*fetch_error)(struct aiocb *ujob);
3273858a1f4SJohn Baldwin 	int	(*store_status)(struct aiocb *ujob, long status);
3283858a1f4SJohn Baldwin 	int	(*store_error)(struct aiocb *ujob, long error);
3293858a1f4SJohn Baldwin 	int	(*store_kernelinfo)(struct aiocb *ujob, long jobref);
3303858a1f4SJohn Baldwin 	int	(*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
3313858a1f4SJohn Baldwin };
3323858a1f4SJohn Baldwin 
3331aa4c324SDavid Xu static TAILQ_HEAD(,aiothreadlist) aio_freeproc;		/* (c) Idle daemons */
3341ce91824SDavid Xu static struct sema aio_newproc_sem;
3351ce91824SDavid Xu static struct mtx aio_job_mtx;
3361ce91824SDavid Xu static struct mtx aio_sock_mtx;
3371aa4c324SDavid Xu static TAILQ_HEAD(,aiocblist) aio_jobs;			/* (c) Async job list */
3381ce91824SDavid Xu static struct unrhdr *aiod_unr;
3392244ea07SJohn Dyson 
3406a1162d4SAlexander Leidinger void		aio_init_aioinfo(struct proc *p);
341723d37c0SKonstantin Belousov static int	aio_onceonly(void);
342fd3bf775SJohn Dyson static int	aio_free_entry(struct aiocblist *aiocbe);
343f95c13dbSGleb Smirnoff static void	aio_process_rw(struct aiocblist *aiocbe);
344f95c13dbSGleb Smirnoff static void	aio_process_sync(struct aiocblist *aiocbe);
3456160e12cSGleb Smirnoff static void	aio_process_mlock(struct aiocblist *aiocbe);
3461ce91824SDavid Xu static int	aio_newproc(int *);
3476a1162d4SAlexander Leidinger int		aio_aqueue(struct thread *td, struct aiocb *job,
3483858a1f4SJohn Baldwin 			struct aioliojob *lio, int type, struct aiocb_ops *ops);
349f743d981SAlexander Motin static void	aio_physwakeup(struct bio *bp);
35075b8b3b2SJohn Baldwin static void	aio_proc_rundown(void *arg, struct proc *p);
351993182e5SAlexander Leidinger static void	aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
352fd3bf775SJohn Dyson static int	aio_qphysio(struct proc *p, struct aiocblist *iocb);
3531ce91824SDavid Xu static void	aio_daemon(void *param);
35448dac059SAlan Cox static void	aio_swake_cb(struct socket *, struct sockbuf *);
35521d56e9cSAlfred Perlstein static int	aio_unload(void);
35699eee864SDavid Xu static void	aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
35799eee864SDavid Xu #define DONE_BUF	1
35899eee864SDavid Xu #define DONE_QUEUE	2
359dbbccfe9SDavid Xu static int	aio_kick(struct proc *userp);
36099eee864SDavid Xu static void	aio_kick_nowait(struct proc *userp);
36199eee864SDavid Xu static void	aio_kick_helper(void *context, int pending);
36221d56e9cSAlfred Perlstein static int	filt_aioattach(struct knote *kn);
36321d56e9cSAlfred Perlstein static void	filt_aiodetach(struct knote *kn);
36421d56e9cSAlfred Perlstein static int	filt_aio(struct knote *kn, long hint);
36569cd28daSDoug Ambrisko static int	filt_lioattach(struct knote *kn);
36669cd28daSDoug Ambrisko static void	filt_liodetach(struct knote *kn);
36769cd28daSDoug Ambrisko static int	filt_lio(struct knote *kn, long hint);
3682244ea07SJohn Dyson 
369eb8e6d52SEivind Eklund /*
370eb8e6d52SEivind Eklund  * Zones for:
371eb8e6d52SEivind Eklund  * 	kaio	Per process async io info
372eb8e6d52SEivind Eklund  *	aiop	async io thread data
373eb8e6d52SEivind Eklund  *	aiocb	async io jobs
374eb8e6d52SEivind Eklund  *	aiol	list io job pointer - internal to aio_suspend XXX
375eb8e6d52SEivind Eklund  *	aiolio	list io jobs
376eb8e6d52SEivind Eklund  */
377c897b813SJeff Roberson static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
378fd3bf775SJohn Dyson 
379eb8e6d52SEivind Eklund /* kqueue filters for aio */
380e76d823bSRobert Watson static struct filterops aio_filtops = {
381e76d823bSRobert Watson 	.f_isfd = 0,
382e76d823bSRobert Watson 	.f_attach = filt_aioattach,
383e76d823bSRobert Watson 	.f_detach = filt_aiodetach,
384e76d823bSRobert Watson 	.f_event = filt_aio,
385e76d823bSRobert Watson };
386e76d823bSRobert Watson static struct filterops lio_filtops = {
387e76d823bSRobert Watson 	.f_isfd = 0,
388e76d823bSRobert Watson 	.f_attach = filt_lioattach,
389e76d823bSRobert Watson 	.f_detach = filt_liodetach,
390e76d823bSRobert Watson 	.f_event = filt_lio
391e76d823bSRobert Watson };
39221d56e9cSAlfred Perlstein 
39375b8b3b2SJohn Baldwin static eventhandler_tag exit_tag, exec_tag;
39475b8b3b2SJohn Baldwin 
3951ce91824SDavid Xu TASKQUEUE_DEFINE_THREAD(aiod_bio);
3961ce91824SDavid Xu 
397eb8e6d52SEivind Eklund /*
398eb8e6d52SEivind Eklund  * Main operations function for use as a kernel module.
399eb8e6d52SEivind Eklund  */
40021d56e9cSAlfred Perlstein static int
40121d56e9cSAlfred Perlstein aio_modload(struct module *module, int cmd, void *arg)
40221d56e9cSAlfred Perlstein {
40321d56e9cSAlfred Perlstein 	int error = 0;
40421d56e9cSAlfred Perlstein 
40521d56e9cSAlfred Perlstein 	switch (cmd) {
40621d56e9cSAlfred Perlstein 	case MOD_LOAD:
40721d56e9cSAlfred Perlstein 		aio_onceonly();
40821d56e9cSAlfred Perlstein 		break;
40921d56e9cSAlfred Perlstein 	case MOD_UNLOAD:
41021d56e9cSAlfred Perlstein 		error = aio_unload();
41121d56e9cSAlfred Perlstein 		break;
41221d56e9cSAlfred Perlstein 	case MOD_SHUTDOWN:
41321d56e9cSAlfred Perlstein 		break;
41421d56e9cSAlfred Perlstein 	default:
41521d56e9cSAlfred Perlstein 		error = EINVAL;
41621d56e9cSAlfred Perlstein 		break;
41721d56e9cSAlfred Perlstein 	}
41821d56e9cSAlfred Perlstein 	return (error);
41921d56e9cSAlfred Perlstein }
42021d56e9cSAlfred Perlstein 
42121d56e9cSAlfred Perlstein static moduledata_t aio_mod = {
42221d56e9cSAlfred Perlstein 	"aio",
42321d56e9cSAlfred Perlstein 	&aio_modload,
42421d56e9cSAlfred Perlstein 	NULL
42521d56e9cSAlfred Perlstein };
42621d56e9cSAlfred Perlstein 
427723d37c0SKonstantin Belousov static struct syscall_helper_data aio_syscalls[] = {
428723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_cancel),
429723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_error),
430723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_fsync),
4316160e12cSGleb Smirnoff 	SYSCALL_INIT_HELPER(aio_mlock),
432723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_read),
433723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_return),
434723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_suspend),
435723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_waitcomplete),
436723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(aio_write),
437723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(lio_listio),
438723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(oaio_read),
439723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(oaio_write),
440723d37c0SKonstantin Belousov 	SYSCALL_INIT_HELPER(olio_listio),
441723d37c0SKonstantin Belousov 	SYSCALL_INIT_LAST
442723d37c0SKonstantin Belousov };
443723d37c0SKonstantin Belousov 
444723d37c0SKonstantin Belousov #ifdef COMPAT_FREEBSD32
445723d37c0SKonstantin Belousov #include <sys/mount.h>
446723d37c0SKonstantin Belousov #include <sys/socket.h>
447723d37c0SKonstantin Belousov #include <compat/freebsd32/freebsd32.h>
448723d37c0SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h>
449723d37c0SKonstantin Belousov #include <compat/freebsd32/freebsd32_signal.h>
450723d37c0SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h>
451723d37c0SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
452723d37c0SKonstantin Belousov 
453723d37c0SKonstantin Belousov static struct syscall_helper_data aio32_syscalls[] = {
454723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_return),
455723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
456723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
457723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_error),
458723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
4596160e12cSGleb Smirnoff 	SYSCALL32_INIT_HELPER(freebsd32_aio_mlock),
460723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_read),
461723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_write),
462723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
463723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
464723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
465723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
466723d37c0SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
467723d37c0SKonstantin Belousov 	SYSCALL_INIT_LAST
468723d37c0SKonstantin Belousov };
469723d37c0SKonstantin Belousov #endif
47021d56e9cSAlfred Perlstein 
47121d56e9cSAlfred Perlstein DECLARE_MODULE(aio, aio_mod,
47221d56e9cSAlfred Perlstein 	SI_SUB_VFS, SI_ORDER_ANY);
47321d56e9cSAlfred Perlstein MODULE_VERSION(aio, 1);
47421d56e9cSAlfred Perlstein 
475fd3bf775SJohn Dyson /*
4762244ea07SJohn Dyson  * Startup initialization
4772244ea07SJohn Dyson  */
478723d37c0SKonstantin Belousov static int
47921d56e9cSAlfred Perlstein aio_onceonly(void)
480fd3bf775SJohn Dyson {
481723d37c0SKonstantin Belousov 	int error;
48221d56e9cSAlfred Perlstein 
48321d56e9cSAlfred Perlstein 	/* XXX: should probably just use so->callback */
48421d56e9cSAlfred Perlstein 	aio_swake = &aio_swake_cb;
48575b8b3b2SJohn Baldwin 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
48675b8b3b2SJohn Baldwin 	    EVENTHANDLER_PRI_ANY);
487993182e5SAlexander Leidinger 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
48875b8b3b2SJohn Baldwin 	    EVENTHANDLER_PRI_ANY);
48921d56e9cSAlfred Perlstein 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
49069cd28daSDoug Ambrisko 	kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
4912244ea07SJohn Dyson 	TAILQ_INIT(&aio_freeproc);
4921ce91824SDavid Xu 	sema_init(&aio_newproc_sem, 0, "aio_new_proc");
4931ce91824SDavid Xu 	mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
4941ce91824SDavid Xu 	mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
4952244ea07SJohn Dyson 	TAILQ_INIT(&aio_jobs);
4961ce91824SDavid Xu 	aiod_unr = new_unrhdr(1, INT_MAX, NULL);
497c897b813SJeff Roberson 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
498c897b813SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
499c897b813SJeff Roberson 	aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
500c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
501c897b813SJeff Roberson 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
502c897b813SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
503c897b813SJeff Roberson 	aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
504c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
5051ce91824SDavid Xu 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
506c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
50784af4da6SJohn Dyson 	aiod_timeout = AIOD_TIMEOUT_DEFAULT;
50884af4da6SJohn Dyson 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
509fd3bf775SJohn Dyson 	jobrefid = 1;
510c7047e52SGarrett Wollman 	async_io_version = _POSIX_VERSION;
511c844abc9SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
51286d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
51386d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
514723d37c0SKonstantin Belousov 
515e015b1abSMateusz Guzik 	error = syscall_helper_register(aio_syscalls, SY_THR_STATIC_KLD);
516723d37c0SKonstantin Belousov 	if (error)
517723d37c0SKonstantin Belousov 		return (error);
518723d37c0SKonstantin Belousov #ifdef COMPAT_FREEBSD32
519e015b1abSMateusz Guzik 	error = syscall32_helper_register(aio32_syscalls, SY_THR_STATIC_KLD);
520723d37c0SKonstantin Belousov 	if (error)
521723d37c0SKonstantin Belousov 		return (error);
522723d37c0SKonstantin Belousov #endif
523723d37c0SKonstantin Belousov 	return (0);
5242244ea07SJohn Dyson }
5252244ea07SJohn Dyson 
526eb8e6d52SEivind Eklund /*
527eb8e6d52SEivind Eklund  * Callback for unload of AIO when used as a module.
528eb8e6d52SEivind Eklund  */
52921d56e9cSAlfred Perlstein static int
53021d56e9cSAlfred Perlstein aio_unload(void)
53121d56e9cSAlfred Perlstein {
532ad3b9257SJohn-Mark Gurney 	int error;
53321d56e9cSAlfred Perlstein 
53421d56e9cSAlfred Perlstein 	/*
53521d56e9cSAlfred Perlstein 	 * XXX: no unloads by default, it's too dangerous.
53621d56e9cSAlfred Perlstein 	 * perhaps we could do it if locked out callers and then
53721d56e9cSAlfred Perlstein 	 * did an aio_proc_rundown() on each process.
5382a522eb9SJohn Baldwin 	 *
5392a522eb9SJohn Baldwin 	 * jhb: aio_proc_rundown() needs to run on curproc though,
5402a522eb9SJohn Baldwin 	 * so I don't think that would fly.
54121d56e9cSAlfred Perlstein 	 */
54221d56e9cSAlfred Perlstein 	if (!unloadable)
54321d56e9cSAlfred Perlstein 		return (EOPNOTSUPP);
54421d56e9cSAlfred Perlstein 
545723d37c0SKonstantin Belousov #ifdef COMPAT_FREEBSD32
546723d37c0SKonstantin Belousov 	syscall32_helper_unregister(aio32_syscalls);
547723d37c0SKonstantin Belousov #endif
548723d37c0SKonstantin Belousov 	syscall_helper_unregister(aio_syscalls);
549723d37c0SKonstantin Belousov 
550ad3b9257SJohn-Mark Gurney 	error = kqueue_del_filteropts(EVFILT_AIO);
551ad3b9257SJohn-Mark Gurney 	if (error)
552ad3b9257SJohn-Mark Gurney 		return error;
553bd793be3SDavid Xu 	error = kqueue_del_filteropts(EVFILT_LIO);
554bd793be3SDavid Xu 	if (error)
555bd793be3SDavid Xu 		return error;
556c7047e52SGarrett Wollman 	async_io_version = 0;
55721d56e9cSAlfred Perlstein 	aio_swake = NULL;
5581ce91824SDavid Xu 	taskqueue_free(taskqueue_aiod_bio);
5591ce91824SDavid Xu 	delete_unrhdr(aiod_unr);
560bd793be3SDavid Xu 	uma_zdestroy(kaio_zone);
561bd793be3SDavid Xu 	uma_zdestroy(aiop_zone);
562bd793be3SDavid Xu 	uma_zdestroy(aiocb_zone);
563bd793be3SDavid Xu 	uma_zdestroy(aiol_zone);
564bd793be3SDavid Xu 	uma_zdestroy(aiolio_zone);
56575b8b3b2SJohn Baldwin 	EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
56675b8b3b2SJohn Baldwin 	EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
5671ce91824SDavid Xu 	mtx_destroy(&aio_job_mtx);
5681ce91824SDavid Xu 	mtx_destroy(&aio_sock_mtx);
5691ce91824SDavid Xu 	sema_destroy(&aio_newproc_sem);
570f51c1e89SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
571f51c1e89SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
572f51c1e89SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
57321d56e9cSAlfred Perlstein 	return (0);
57421d56e9cSAlfred Perlstein }
57521d56e9cSAlfred Perlstein 
5762244ea07SJohn Dyson /*
577bfbbc4aaSJason Evans  * Init the per-process aioinfo structure.  The aioinfo limits are set
578bfbbc4aaSJason Evans  * per-process for user limit (resource) management.
5792244ea07SJohn Dyson  */
5806a1162d4SAlexander Leidinger void
581fd3bf775SJohn Dyson aio_init_aioinfo(struct proc *p)
582fd3bf775SJohn Dyson {
5832244ea07SJohn Dyson 	struct kaioinfo *ki;
584ac41f2efSAlfred Perlstein 
585a163d034SWarner Losh 	ki = uma_zalloc(kaio_zone, M_WAITOK);
5869889bbacSKonstantin Belousov 	mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
58784af4da6SJohn Dyson 	ki->kaio_flags = 0;
588a624e84fSJohn Dyson 	ki->kaio_maxactive_count = max_aio_per_proc;
5892244ea07SJohn Dyson 	ki->kaio_active_count = 0;
590a624e84fSJohn Dyson 	ki->kaio_qallowed_count = max_aio_queue_per_proc;
5911ce91824SDavid Xu 	ki->kaio_count = 0;
59284af4da6SJohn Dyson 	ki->kaio_ballowed_count = max_buf_aio;
593fd3bf775SJohn Dyson 	ki->kaio_buffer_count = 0;
5941ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_all);
5951ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_done);
5962244ea07SJohn Dyson 	TAILQ_INIT(&ki->kaio_jobqueue);
597fd3bf775SJohn Dyson 	TAILQ_INIT(&ki->kaio_bufqueue);
59884af4da6SJohn Dyson 	TAILQ_INIT(&ki->kaio_liojoblist);
599bfbbc4aaSJason Evans 	TAILQ_INIT(&ki->kaio_sockqueue);
60099eee864SDavid Xu 	TAILQ_INIT(&ki->kaio_syncqueue);
60199eee864SDavid Xu 	TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
6023999ebe3SAlan Cox 	PROC_LOCK(p);
6033999ebe3SAlan Cox 	if (p->p_aioinfo == NULL) {
6043999ebe3SAlan Cox 		p->p_aioinfo = ki;
6053999ebe3SAlan Cox 		PROC_UNLOCK(p);
6063999ebe3SAlan Cox 	} else {
6073999ebe3SAlan Cox 		PROC_UNLOCK(p);
608759ccccaSDavid Xu 		mtx_destroy(&ki->kaio_mtx);
6093999ebe3SAlan Cox 		uma_zfree(kaio_zone, ki);
6102244ea07SJohn Dyson 	}
611bfbbc4aaSJason Evans 
61222035f47SOleksandr Tymoshenko 	while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
6131ce91824SDavid Xu 		aio_newproc(NULL);
6142244ea07SJohn Dyson }
6152244ea07SJohn Dyson 
6164c0fb2cfSDavid Xu static int
6174c0fb2cfSDavid Xu aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
6184c0fb2cfSDavid Xu {
619cf7d9a8cSDavid Xu 	struct thread *td;
620cf7d9a8cSDavid Xu 	int error;
621759ccccaSDavid Xu 
622cf7d9a8cSDavid Xu 	error = sigev_findtd(p, sigev, &td);
623cf7d9a8cSDavid Xu 	if (error)
624cf7d9a8cSDavid Xu 		return (error);
6254c0fb2cfSDavid Xu 	if (!KSI_ONQ(ksi)) {
626cf7d9a8cSDavid Xu 		ksiginfo_set_sigev(ksi, sigev);
6274c0fb2cfSDavid Xu 		ksi->ksi_code = SI_ASYNCIO;
6284c0fb2cfSDavid Xu 		ksi->ksi_flags |= KSI_EXT | KSI_INS;
629cf7d9a8cSDavid Xu 		tdsendsignal(p, td, ksi->ksi_signo, ksi);
6304c0fb2cfSDavid Xu 	}
631759ccccaSDavid Xu 	PROC_UNLOCK(p);
632cf7d9a8cSDavid Xu 	return (error);
6334c0fb2cfSDavid Xu }
6344c0fb2cfSDavid Xu 
6352244ea07SJohn Dyson /*
636bfbbc4aaSJason Evans  * Free a job entry.  Wait for completion if it is currently active, but don't
637bfbbc4aaSJason Evans  * delay forever.  If we delay, we return a flag that says that we have to
638bfbbc4aaSJason Evans  * restart the queue scan.
6392244ea07SJohn Dyson  */
64088ed460eSAlan Cox static int
641fd3bf775SJohn Dyson aio_free_entry(struct aiocblist *aiocbe)
642fd3bf775SJohn Dyson {
6432244ea07SJohn Dyson 	struct kaioinfo *ki;
6441ce91824SDavid Xu 	struct aioliojob *lj;
6452244ea07SJohn Dyson 	struct proc *p;
6462244ea07SJohn Dyson 
6472244ea07SJohn Dyson 	p = aiocbe->userproc;
6481ce91824SDavid Xu 	MPASS(curproc == p);
6492244ea07SJohn Dyson 	ki = p->p_aioinfo;
6501ce91824SDavid Xu 	MPASS(ki != NULL);
6511ce91824SDavid Xu 
652759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
653759ccccaSDavid Xu 	MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
654759ccccaSDavid Xu 
6551ce91824SDavid Xu 	atomic_subtract_int(&num_queue_count, 1);
6561ce91824SDavid Xu 
6571ce91824SDavid Xu 	ki->kaio_count--;
6581ce91824SDavid Xu 	MPASS(ki->kaio_count >= 0);
6591ce91824SDavid Xu 
66027b8220dSDavid Xu 	TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
66127b8220dSDavid Xu 	TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
66227b8220dSDavid Xu 
66384af4da6SJohn Dyson 	lj = aiocbe->lio;
66484af4da6SJohn Dyson 	if (lj) {
6651ce91824SDavid Xu 		lj->lioj_count--;
6661ce91824SDavid Xu 		lj->lioj_finished_count--;
6671ce91824SDavid Xu 
668a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
6691ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
6701ce91824SDavid Xu 			/* lio is going away, we need to destroy any knotes */
6711ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
672759ccccaSDavid Xu 			PROC_LOCK(p);
6731ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
674759ccccaSDavid Xu 			PROC_UNLOCK(p);
6751ce91824SDavid Xu 			uma_zfree(aiolio_zone, lj);
67684af4da6SJohn Dyson 		}
67784af4da6SJohn Dyson 	}
6781ce91824SDavid Xu 
679cb679c38SJonathan Lemon 	/* aiocbe is going away, we need to destroy any knotes */
6801ce91824SDavid Xu 	knlist_delete(&aiocbe->klist, curthread, 1);
681759ccccaSDavid Xu 	PROC_LOCK(p);
6821ce91824SDavid Xu 	sigqueue_take(&aiocbe->ksi);
683759ccccaSDavid Xu 	PROC_UNLOCK(p);
6841ce91824SDavid Xu 
6851ce91824SDavid Xu 	MPASS(aiocbe->bp == NULL);
6861ce91824SDavid Xu 	aiocbe->jobstate = JOBST_NULL;
687759ccccaSDavid Xu 	AIO_UNLOCK(ki);
6882a522eb9SJohn Baldwin 
6892a522eb9SJohn Baldwin 	/*
6902a522eb9SJohn Baldwin 	 * The thread argument here is used to find the owning process
6912a522eb9SJohn Baldwin 	 * and is also passed to fo_close() which may pass it to various
6922a522eb9SJohn Baldwin 	 * places such as devsw close() routines.  Because of that, we
6932a522eb9SJohn Baldwin 	 * need a thread pointer from the process owning the job that is
6942a522eb9SJohn Baldwin 	 * persistent and won't disappear out from under us or move to
6952a522eb9SJohn Baldwin 	 * another process.
6962a522eb9SJohn Baldwin 	 *
6972a522eb9SJohn Baldwin 	 * Currently, all the callers of this function call it to remove
6982a522eb9SJohn Baldwin 	 * an aiocblist from the current process' job list either via a
6992a522eb9SJohn Baldwin 	 * syscall or due to the current process calling exit() or
7002a522eb9SJohn Baldwin 	 * execve().  Thus, we know that p == curproc.  We also know that
7012a522eb9SJohn Baldwin 	 * curthread can't exit since we are curthread.
7022a522eb9SJohn Baldwin 	 *
7032a522eb9SJohn Baldwin 	 * Therefore, we use curthread as the thread to pass to
7042a522eb9SJohn Baldwin 	 * knlist_delete().  This does mean that it is possible for the
7052a522eb9SJohn Baldwin 	 * thread pointer at close time to differ from the thread pointer
7062a522eb9SJohn Baldwin 	 * at open time, but this is already true of file descriptors in
7072a522eb9SJohn Baldwin 	 * a multithreaded process.
708b40ce416SJulian Elischer 	 */
7096160e12cSGleb Smirnoff 	if (aiocbe->fd_file)
710a5c0b1c0SAlan Cox 		fdrop(aiocbe->fd_file, curthread);
711f8f750c5SRobert Watson 	crfree(aiocbe->cred);
712c897b813SJeff Roberson 	uma_zfree(aiocb_zone, aiocbe);
713759ccccaSDavid Xu 	AIO_LOCK(ki);
7141ce91824SDavid Xu 
715ac41f2efSAlfred Perlstein 	return (0);
7162244ea07SJohn Dyson }
7172244ea07SJohn Dyson 
718993182e5SAlexander Leidinger static void
719993182e5SAlexander Leidinger aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
720993182e5SAlexander Leidinger {
721993182e5SAlexander Leidinger    	aio_proc_rundown(arg, p);
722993182e5SAlexander Leidinger }
723993182e5SAlexander Leidinger 
7242244ea07SJohn Dyson /*
7252244ea07SJohn Dyson  * Rundown the jobs for a given process.
7262244ea07SJohn Dyson  */
72721d56e9cSAlfred Perlstein static void
72875b8b3b2SJohn Baldwin aio_proc_rundown(void *arg, struct proc *p)
729fd3bf775SJohn Dyson {
7302244ea07SJohn Dyson 	struct kaioinfo *ki;
7311ce91824SDavid Xu 	struct aioliojob *lj;
7321ce91824SDavid Xu 	struct aiocblist *cbe, *cbn;
733bfbbc4aaSJason Evans 	struct file *fp;
734bfbbc4aaSJason Evans 	struct socket *so;
7351aa4c324SDavid Xu 	int remove;
7362244ea07SJohn Dyson 
7372a522eb9SJohn Baldwin 	KASSERT(curthread->td_proc == p,
7382a522eb9SJohn Baldwin 	    ("%s: called on non-curproc", __func__));
7392244ea07SJohn Dyson 	ki = p->p_aioinfo;
7402244ea07SJohn Dyson 	if (ki == NULL)
7412244ea07SJohn Dyson 		return;
7422244ea07SJohn Dyson 
743759ccccaSDavid Xu 	AIO_LOCK(ki);
74427b8220dSDavid Xu 	ki->kaio_flags |= KAIO_RUNDOWN;
7451ce91824SDavid Xu 
7461ce91824SDavid Xu restart:
747a624e84fSJohn Dyson 
748bfbbc4aaSJason Evans 	/*
7491ce91824SDavid Xu 	 * Try to cancel all pending requests. This code simulates
7501ce91824SDavid Xu 	 * aio_cancel on all pending I/O requests.
751bfbbc4aaSJason Evans 	 */
7521ce91824SDavid Xu 	TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
7531aa4c324SDavid Xu 		remove = 0;
7541ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
7551ce91824SDavid Xu 		if (cbe->jobstate == JOBST_JOBQGLOBAL) {
7561ce91824SDavid Xu 			TAILQ_REMOVE(&aio_jobs, cbe, list);
7571aa4c324SDavid Xu 			remove = 1;
7581aa4c324SDavid Xu 		} else if (cbe->jobstate == JOBST_JOBQSOCK) {
7591aa4c324SDavid Xu 			fp = cbe->fd_file;
7601aa4c324SDavid Xu 			MPASS(fp->f_type == DTYPE_SOCKET);
7611aa4c324SDavid Xu 			so = fp->f_data;
7621aa4c324SDavid Xu 			TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
7631aa4c324SDavid Xu 			remove = 1;
76499eee864SDavid Xu 		} else if (cbe->jobstate == JOBST_JOBQSYNC) {
76599eee864SDavid Xu 			TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
76699eee864SDavid Xu 			remove = 1;
7671aa4c324SDavid Xu 		}
7681ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
7691aa4c324SDavid Xu 
7701aa4c324SDavid Xu 		if (remove) {
7711ce91824SDavid Xu 			cbe->jobstate = JOBST_JOBFINISHED;
7721ce91824SDavid Xu 			cbe->uaiocb._aiocb_private.status = -1;
7731ce91824SDavid Xu 			cbe->uaiocb._aiocb_private.error = ECANCELED;
7741ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
7751ce91824SDavid Xu 			aio_bio_done_notify(p, cbe, DONE_QUEUE);
7761ce91824SDavid Xu 		}
7772244ea07SJohn Dyson 	}
77884af4da6SJohn Dyson 
7791ce91824SDavid Xu 	/* Wait for all running I/O to be finished */
7801ce91824SDavid Xu 	if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
7811ce91824SDavid Xu 	    TAILQ_FIRST(&ki->kaio_jobqueue)) {
78284af4da6SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
783759ccccaSDavid Xu 		msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
7841ce91824SDavid Xu 		goto restart;
78584af4da6SJohn Dyson 	}
78684af4da6SJohn Dyson 
7871ce91824SDavid Xu 	/* Free all completed I/O requests. */
7881ce91824SDavid Xu 	while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
7891ce91824SDavid Xu 		aio_free_entry(cbe);
79084af4da6SJohn Dyson 
7911ce91824SDavid Xu 	while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
792a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
79384af4da6SJohn Dyson 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
7941ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
795759ccccaSDavid Xu 			PROC_LOCK(p);
7961ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
797759ccccaSDavid Xu 			PROC_UNLOCK(p);
798c897b813SJeff Roberson 			uma_zfree(aiolio_zone, lj);
799f4f0ecefSJohn Dyson 		} else {
800a9bf5e37SDavid Xu 			panic("LIO job not cleaned up: C:%d, FC:%d\n",
801a9bf5e37SDavid Xu 			    lj->lioj_count, lj->lioj_finished_count);
80284af4da6SJohn Dyson 		}
803f4f0ecefSJohn Dyson 	}
804759ccccaSDavid Xu 	AIO_UNLOCK(ki);
80599eee864SDavid Xu 	taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
8065114048bSKonstantin Belousov 	mtx_destroy(&ki->kaio_mtx);
807c897b813SJeff Roberson 	uma_zfree(kaio_zone, ki);
808a624e84fSJohn Dyson 	p->p_aioinfo = NULL;
8092244ea07SJohn Dyson }
8102244ea07SJohn Dyson 
8112244ea07SJohn Dyson /*
812bfbbc4aaSJason Evans  * Select a job to run (called by an AIO daemon).
8132244ea07SJohn Dyson  */
8142244ea07SJohn Dyson static struct aiocblist *
815b40ce416SJulian Elischer aio_selectjob(struct aiothreadlist *aiop)
816fd3bf775SJohn Dyson {
8172244ea07SJohn Dyson 	struct aiocblist *aiocbe;
818bfbbc4aaSJason Evans 	struct kaioinfo *ki;
819bfbbc4aaSJason Evans 	struct proc *userp;
8202244ea07SJohn Dyson 
8211ce91824SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
8222a522eb9SJohn Baldwin 	TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
8232244ea07SJohn Dyson 		userp = aiocbe->userproc;
8242244ea07SJohn Dyson 		ki = userp->p_aioinfo;
8252244ea07SJohn Dyson 
8262244ea07SJohn Dyson 		if (ki->kaio_active_count < ki->kaio_maxactive_count) {
8272244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_jobs, aiocbe, list);
8281ce91824SDavid Xu 			/* Account for currently active jobs. */
8291ce91824SDavid Xu 			ki->kaio_active_count++;
8301ce91824SDavid Xu 			aiocbe->jobstate = JOBST_JOBRUNNING;
8311ce91824SDavid Xu 			break;
8321ce91824SDavid Xu 		}
8331ce91824SDavid Xu 	}
834ac41f2efSAlfred Perlstein 	return (aiocbe);
8352244ea07SJohn Dyson }
8362244ea07SJohn Dyson 
8372244ea07SJohn Dyson /*
83899eee864SDavid Xu  *  Move all data to a permanent storage device, this code
83999eee864SDavid Xu  *  simulates fsync syscall.
84099eee864SDavid Xu  */
84199eee864SDavid Xu static int
84299eee864SDavid Xu aio_fsync_vnode(struct thread *td, struct vnode *vp)
84399eee864SDavid Xu {
84499eee864SDavid Xu 	struct mount *mp;
84599eee864SDavid Xu 	int error;
84699eee864SDavid Xu 
84799eee864SDavid Xu 	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
84899eee864SDavid Xu 		goto drop;
849cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
85099eee864SDavid Xu 	if (vp->v_object != NULL) {
85189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(vp->v_object);
85299eee864SDavid Xu 		vm_object_page_clean(vp->v_object, 0, 0, 0);
85389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(vp->v_object);
85499eee864SDavid Xu 	}
85599eee864SDavid Xu 	error = VOP_FSYNC(vp, MNT_WAIT, td);
85699eee864SDavid Xu 
85722db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
85899eee864SDavid Xu 	vn_finished_write(mp);
85999eee864SDavid Xu drop:
86099eee864SDavid Xu 	return (error);
86199eee864SDavid Xu }
86299eee864SDavid Xu 
86399eee864SDavid Xu /*
864f95c13dbSGleb Smirnoff  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
865f95c13dbSGleb Smirnoff  * does the I/O request for the non-physio version of the operations.  The
866f95c13dbSGleb Smirnoff  * normal vn operations are used, and this code should work in all instances
867f95c13dbSGleb Smirnoff  * for every type of file, including pipes, sockets, fifos, and regular files.
8681ce91824SDavid Xu  *
8691aa4c324SDavid Xu  * XXX I don't think it works well for socket, pipe, and fifo.
8702244ea07SJohn Dyson  */
87188ed460eSAlan Cox static void
872f95c13dbSGleb Smirnoff aio_process_rw(struct aiocblist *aiocbe)
873fd3bf775SJohn Dyson {
874f8f750c5SRobert Watson 	struct ucred *td_savedcred;
875b40ce416SJulian Elischer 	struct thread *td;
8762244ea07SJohn Dyson 	struct aiocb *cb;
8772244ea07SJohn Dyson 	struct file *fp;
8781ce91824SDavid Xu 	struct socket *so;
8792244ea07SJohn Dyson 	struct uio auio;
8802244ea07SJohn Dyson 	struct iovec aiov;
8812244ea07SJohn Dyson 	int cnt;
8822244ea07SJohn Dyson 	int error;
883fd3bf775SJohn Dyson 	int oublock_st, oublock_end;
884fd3bf775SJohn Dyson 	int inblock_st, inblock_end;
8852244ea07SJohn Dyson 
886f95c13dbSGleb Smirnoff 	KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_READ ||
887f95c13dbSGleb Smirnoff 	    aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE,
888f95c13dbSGleb Smirnoff 	    ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
889f95c13dbSGleb Smirnoff 
890b40ce416SJulian Elischer 	td = curthread;
891f8f750c5SRobert Watson 	td_savedcred = td->td_ucred;
892f8f750c5SRobert Watson 	td->td_ucred = aiocbe->cred;
8932244ea07SJohn Dyson 	cb = &aiocbe->uaiocb;
89400e73160SAlan Cox 	fp = aiocbe->fd_file;
895bfbbc4aaSJason Evans 
89691369fc7SAlan Cox 	aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
8972244ea07SJohn Dyson 	aiov.iov_len = cb->aio_nbytes;
8982244ea07SJohn Dyson 
8992244ea07SJohn Dyson 	auio.uio_iov = &aiov;
9002244ea07SJohn Dyson 	auio.uio_iovcnt = 1;
9019b16adc1SAlan Cox 	auio.uio_offset = cb->aio_offset;
9022244ea07SJohn Dyson 	auio.uio_resid = cb->aio_nbytes;
9032244ea07SJohn Dyson 	cnt = cb->aio_nbytes;
9042244ea07SJohn Dyson 	auio.uio_segflg = UIO_USERSPACE;
905b40ce416SJulian Elischer 	auio.uio_td = td;
9062244ea07SJohn Dyson 
9071c4bcd05SJeff Roberson 	inblock_st = td->td_ru.ru_inblock;
9081c4bcd05SJeff Roberson 	oublock_st = td->td_ru.ru_oublock;
909279d7226SMatthew Dillon 	/*
910a9bf5e37SDavid Xu 	 * aio_aqueue() acquires a reference to the file that is
9119b16adc1SAlan Cox 	 * released in aio_free_entry().
912279d7226SMatthew Dillon 	 */
9132244ea07SJohn Dyson 	if (cb->aio_lio_opcode == LIO_READ) {
9142244ea07SJohn Dyson 		auio.uio_rw = UIO_READ;
9155114048bSKonstantin Belousov 		if (auio.uio_resid == 0)
9165114048bSKonstantin Belousov 			error = 0;
9175114048bSKonstantin Belousov 		else
918b40ce416SJulian Elischer 			error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
9192244ea07SJohn Dyson 	} else {
9206d53aa62SDavid Xu 		if (fp->f_type == DTYPE_VNODE)
9216d53aa62SDavid Xu 			bwillwrite();
9222244ea07SJohn Dyson 		auio.uio_rw = UIO_WRITE;
923b40ce416SJulian Elischer 		error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
9242244ea07SJohn Dyson 	}
9251c4bcd05SJeff Roberson 	inblock_end = td->td_ru.ru_inblock;
9261c4bcd05SJeff Roberson 	oublock_end = td->td_ru.ru_oublock;
927fd3bf775SJohn Dyson 
928fd3bf775SJohn Dyson 	aiocbe->inputcharge = inblock_end - inblock_st;
929fd3bf775SJohn Dyson 	aiocbe->outputcharge = oublock_end - oublock_st;
9302244ea07SJohn Dyson 
931bfbbc4aaSJason Evans 	if ((error) && (auio.uio_resid != cnt)) {
9322244ea07SJohn Dyson 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
9332244ea07SJohn Dyson 			error = 0;
93419eb87d2SJohn Baldwin 		if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
9351ce91824SDavid Xu 			int sigpipe = 1;
9361ce91824SDavid Xu 			if (fp->f_type == DTYPE_SOCKET) {
9371ce91824SDavid Xu 				so = fp->f_data;
9381ce91824SDavid Xu 				if (so->so_options & SO_NOSIGPIPE)
9391ce91824SDavid Xu 					sigpipe = 0;
9401ce91824SDavid Xu 			}
9411ce91824SDavid Xu 			if (sigpipe) {
9429b16adc1SAlan Cox 				PROC_LOCK(aiocbe->userproc);
9438451d0ddSKip Macy 				kern_psignal(aiocbe->userproc, SIGPIPE);
9449b16adc1SAlan Cox 				PROC_UNLOCK(aiocbe->userproc);
94519eb87d2SJohn Baldwin 			}
9462244ea07SJohn Dyson 		}
9471ce91824SDavid Xu 	}
9482244ea07SJohn Dyson 
9492244ea07SJohn Dyson 	cnt -= auio.uio_resid;
9502244ea07SJohn Dyson 	cb->_aiocb_private.error = error;
9512244ea07SJohn Dyson 	cb->_aiocb_private.status = cnt;
952f8f750c5SRobert Watson 	td->td_ucred = td_savedcred;
9532244ea07SJohn Dyson }
9542244ea07SJohn Dyson 
95569cd28daSDoug Ambrisko static void
956f95c13dbSGleb Smirnoff aio_process_sync(struct aiocblist *aiocbe)
957f95c13dbSGleb Smirnoff {
958f95c13dbSGleb Smirnoff 	struct thread *td = curthread;
959f95c13dbSGleb Smirnoff 	struct ucred *td_savedcred = td->td_ucred;
960f95c13dbSGleb Smirnoff 	struct aiocb *cb = &aiocbe->uaiocb;
961f95c13dbSGleb Smirnoff 	struct file *fp = aiocbe->fd_file;
962f95c13dbSGleb Smirnoff 	int error = 0;
963f95c13dbSGleb Smirnoff 
964f95c13dbSGleb Smirnoff 	KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_SYNC,
965f95c13dbSGleb Smirnoff 	    ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
966f95c13dbSGleb Smirnoff 
967f95c13dbSGleb Smirnoff 	td->td_ucred = aiocbe->cred;
968f95c13dbSGleb Smirnoff 	if (fp->f_vnode != NULL)
969f95c13dbSGleb Smirnoff 		error = aio_fsync_vnode(td, fp->f_vnode);
970f95c13dbSGleb Smirnoff 	cb->_aiocb_private.error = error;
971f95c13dbSGleb Smirnoff 	cb->_aiocb_private.status = 0;
972f95c13dbSGleb Smirnoff 	td->td_ucred = td_savedcred;
973f95c13dbSGleb Smirnoff }
974f95c13dbSGleb Smirnoff 
975f95c13dbSGleb Smirnoff static void
9766160e12cSGleb Smirnoff aio_process_mlock(struct aiocblist *aiocbe)
9776160e12cSGleb Smirnoff {
9786160e12cSGleb Smirnoff 	struct aiocb *cb = &aiocbe->uaiocb;
9796160e12cSGleb Smirnoff 	int error;
9806160e12cSGleb Smirnoff 
9816160e12cSGleb Smirnoff 	KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_MLOCK,
9826160e12cSGleb Smirnoff 	    ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
9836160e12cSGleb Smirnoff 
9846160e12cSGleb Smirnoff 	error = vm_mlock(aiocbe->userproc, aiocbe->cred,
9856160e12cSGleb Smirnoff 	    __DEVOLATILE(void *, cb->aio_buf), cb->aio_nbytes);
9866160e12cSGleb Smirnoff 	cb->_aiocb_private.error = error;
9876160e12cSGleb Smirnoff 	cb->_aiocb_private.status = 0;
9886160e12cSGleb Smirnoff }
9896160e12cSGleb Smirnoff 
9906160e12cSGleb Smirnoff static void
9911ce91824SDavid Xu aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
9921ce91824SDavid Xu {
9931ce91824SDavid Xu 	struct aioliojob *lj;
99469cd28daSDoug Ambrisko 	struct kaioinfo *ki;
99599eee864SDavid Xu 	struct aiocblist *scb, *scbn;
9961ce91824SDavid Xu 	int lj_done;
99769cd28daSDoug Ambrisko 
99869cd28daSDoug Ambrisko 	ki = userp->p_aioinfo;
999759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
100069cd28daSDoug Ambrisko 	lj = aiocbe->lio;
100169cd28daSDoug Ambrisko 	lj_done = 0;
100269cd28daSDoug Ambrisko 	if (lj) {
10031ce91824SDavid Xu 		lj->lioj_finished_count++;
10041ce91824SDavid Xu 		if (lj->lioj_count == lj->lioj_finished_count)
100569cd28daSDoug Ambrisko 			lj_done = 1;
100669cd28daSDoug Ambrisko 	}
100769cd28daSDoug Ambrisko 	if (type == DONE_QUEUE) {
10081ce91824SDavid Xu 		aiocbe->jobflags |= AIOCBLIST_DONE;
100969cd28daSDoug Ambrisko 	} else {
10101ce91824SDavid Xu 		aiocbe->jobflags |= AIOCBLIST_BUFDONE;
101169cd28daSDoug Ambrisko 	}
10121ce91824SDavid Xu 	TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
10131ce91824SDavid Xu 	aiocbe->jobstate = JOBST_JOBFINISHED;
101427b8220dSDavid Xu 
101527b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_RUNDOWN)
101627b8220dSDavid Xu 		goto notification_done;
101727b8220dSDavid Xu 
10181ce91824SDavid Xu 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
10191ce91824SDavid Xu 	    aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
10201ce91824SDavid Xu 		aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
10211ce91824SDavid Xu 
10221ce91824SDavid Xu 	KNOTE_LOCKED(&aiocbe->klist, 1);
10231ce91824SDavid Xu 
102469cd28daSDoug Ambrisko 	if (lj_done) {
10251ce91824SDavid Xu 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
102669cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
10271ce91824SDavid Xu 			KNOTE_LOCKED(&lj->klist, 1);
102869cd28daSDoug Ambrisko 		}
10291ce91824SDavid Xu 		if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
103069cd28daSDoug Ambrisko 		    == LIOJ_SIGNAL
10314c0fb2cfSDavid Xu 		    && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
10324c0fb2cfSDavid Xu 		        lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
10334c0fb2cfSDavid Xu 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
103469cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
103569cd28daSDoug Ambrisko 		}
103669cd28daSDoug Ambrisko 	}
103727b8220dSDavid Xu 
103827b8220dSDavid Xu notification_done:
103999eee864SDavid Xu 	if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
104099eee864SDavid Xu 		TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
1041dbbccfe9SDavid Xu 			if (aiocbe->fd_file == scb->fd_file &&
104299eee864SDavid Xu 			    aiocbe->seqno < scb->seqno) {
104399eee864SDavid Xu 				if (--scb->pending == 0) {
104499eee864SDavid Xu 					mtx_lock(&aio_job_mtx);
104599eee864SDavid Xu 					scb->jobstate = JOBST_JOBQGLOBAL;
104699eee864SDavid Xu 					TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
104799eee864SDavid Xu 					TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
104899eee864SDavid Xu 					aio_kick_nowait(userp);
104999eee864SDavid Xu 					mtx_unlock(&aio_job_mtx);
105099eee864SDavid Xu 				}
105199eee864SDavid Xu 			}
105299eee864SDavid Xu 		}
105399eee864SDavid Xu 	}
105427b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_WAKEUP) {
105569cd28daSDoug Ambrisko 		ki->kaio_flags &= ~KAIO_WAKEUP;
10561ce91824SDavid Xu 		wakeup(&userp->p_aioinfo);
105769cd28daSDoug Ambrisko 	}
105869cd28daSDoug Ambrisko }
105969cd28daSDoug Ambrisko 
10602244ea07SJohn Dyson /*
1061f95c13dbSGleb Smirnoff  * The AIO daemon, most of the actual work is done in aio_process_*,
106284af4da6SJohn Dyson  * but the setup (and address space mgmt) is done in this routine.
10632244ea07SJohn Dyson  */
10642244ea07SJohn Dyson static void
10651ce91824SDavid Xu aio_daemon(void *_id)
10662244ea07SJohn Dyson {
1067bfbbc4aaSJason Evans 	struct aiocblist *aiocbe;
1068b40ce416SJulian Elischer 	struct aiothreadlist *aiop;
1069bfbbc4aaSJason Evans 	struct kaioinfo *ki;
1070bfbbc4aaSJason Evans 	struct proc *curcp, *mycp, *userp;
1071bfbbc4aaSJason Evans 	struct vmspace *myvm, *tmpvm;
1072b40ce416SJulian Elischer 	struct thread *td = curthread;
10731ce91824SDavid Xu 	int id = (intptr_t)_id;
10742244ea07SJohn Dyson 
10752244ea07SJohn Dyson 	/*
1076fd3bf775SJohn Dyson 	 * Local copies of curproc (cp) and vmspace (myvm)
10772244ea07SJohn Dyson 	 */
1078b40ce416SJulian Elischer 	mycp = td->td_proc;
1079fd3bf775SJohn Dyson 	myvm = mycp->p_vmspace;
1080fd3bf775SJohn Dyson 
1081cd4ed3b5SJohn Baldwin 	KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1082fd3bf775SJohn Dyson 
1083fd3bf775SJohn Dyson 	/*
1084bfbbc4aaSJason Evans 	 * Allocate and ready the aio control info.  There is one aiop structure
1085bfbbc4aaSJason Evans 	 * per daemon.
1086fd3bf775SJohn Dyson 	 */
1087a163d034SWarner Losh 	aiop = uma_zalloc(aiop_zone, M_WAITOK);
1088b40ce416SJulian Elischer 	aiop->aiothread = td;
108999eee864SDavid Xu 	aiop->aiothreadflags = 0;
1090bfbbc4aaSJason Evans 
1091e6bdc05fSDavid Xu 	/* The daemon resides in its own pgrp. */
10928451d0ddSKip Macy 	sys_setsid(td, NULL);
1093fd3bf775SJohn Dyson 
1094fd3bf775SJohn Dyson 	/*
1095fd3bf775SJohn Dyson 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1096b40ce416SJulian Elischer 	 * and creating too many daemons.)
1097fd3bf775SJohn Dyson 	 */
10981ce91824SDavid Xu 	sema_post(&aio_newproc_sem);
10992244ea07SJohn Dyson 
11001ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
1101bfbbc4aaSJason Evans 	for (;;) {
1102fd3bf775SJohn Dyson 		/*
1103fd3bf775SJohn Dyson 		 * curcp is the current daemon process context.
1104fd3bf775SJohn Dyson 		 * userp is the current user process context.
1105fd3bf775SJohn Dyson 		 */
1106fd3bf775SJohn Dyson 		curcp = mycp;
1107c4860686SJohn Dyson 
1108fd3bf775SJohn Dyson 		/*
1109fd3bf775SJohn Dyson 		 * Take daemon off of free queue
1110fd3bf775SJohn Dyson 		 */
1111b40ce416SJulian Elischer 		if (aiop->aiothreadflags & AIOP_FREE) {
11122244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
1113b40ce416SJulian Elischer 			aiop->aiothreadflags &= ~AIOP_FREE;
11142244ea07SJohn Dyson 		}
11152244ea07SJohn Dyson 
1116fd3bf775SJohn Dyson 		/*
1117bfbbc4aaSJason Evans 		 * Check for jobs.
1118fd3bf775SJohn Dyson 		 */
1119d254af07SMatthew Dillon 		while ((aiocbe = aio_selectjob(aiop)) != NULL) {
11201ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11212244ea07SJohn Dyson 			userp = aiocbe->userproc;
11222244ea07SJohn Dyson 
1123fd3bf775SJohn Dyson 			/*
1124bfbbc4aaSJason Evans 			 * Connect to process address space for user program.
1125fd3bf775SJohn Dyson 			 */
1126fd3bf775SJohn Dyson 			if (userp != curcp) {
1127fd3bf775SJohn Dyson 				/*
1128bfbbc4aaSJason Evans 				 * Save the current address space that we are
1129bfbbc4aaSJason Evans 				 * connected to.
1130fd3bf775SJohn Dyson 				 */
1131fd3bf775SJohn Dyson 				tmpvm = mycp->p_vmspace;
1132bfbbc4aaSJason Evans 
1133fd3bf775SJohn Dyson 				/*
1134bfbbc4aaSJason Evans 				 * Point to the new user address space, and
1135bfbbc4aaSJason Evans 				 * refer to it.
1136fd3bf775SJohn Dyson 				 */
1137fd3bf775SJohn Dyson 				mycp->p_vmspace = userp->p_vmspace;
11381a276a3fSAlan Cox 				atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1139bfbbc4aaSJason Evans 
1140bfbbc4aaSJason Evans 				/* Activate the new mapping. */
1141079b7badSJulian Elischer 				pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1142bfbbc4aaSJason Evans 
1143fd3bf775SJohn Dyson 				/*
1144bfbbc4aaSJason Evans 				 * If the old address space wasn't the daemons
1145bfbbc4aaSJason Evans 				 * own address space, then we need to remove the
1146bfbbc4aaSJason Evans 				 * daemon's reference from the other process
1147bfbbc4aaSJason Evans 				 * that it was acting on behalf of.
1148fd3bf775SJohn Dyson 				 */
11492244ea07SJohn Dyson 				if (tmpvm != myvm) {
11502244ea07SJohn Dyson 					vmspace_free(tmpvm);
11512244ea07SJohn Dyson 				}
1152fd3bf775SJohn Dyson 				curcp = userp;
11532244ea07SJohn Dyson 			}
11542244ea07SJohn Dyson 
1155fd3bf775SJohn Dyson 			ki = userp->p_aioinfo;
115684af4da6SJohn Dyson 
1157bfbbc4aaSJason Evans 			/* Do the I/O function. */
1158f95c13dbSGleb Smirnoff 			switch(aiocbe->uaiocb.aio_lio_opcode) {
1159f95c13dbSGleb Smirnoff 			case LIO_READ:
1160f95c13dbSGleb Smirnoff 			case LIO_WRITE:
1161f95c13dbSGleb Smirnoff 				aio_process_rw(aiocbe);
1162f95c13dbSGleb Smirnoff 				break;
1163f95c13dbSGleb Smirnoff 			case LIO_SYNC:
1164f95c13dbSGleb Smirnoff 				aio_process_sync(aiocbe);
1165f95c13dbSGleb Smirnoff 				break;
11666160e12cSGleb Smirnoff 			case LIO_MLOCK:
11676160e12cSGleb Smirnoff 				aio_process_mlock(aiocbe);
11686160e12cSGleb Smirnoff 				break;
1169f95c13dbSGleb Smirnoff 			}
117084af4da6SJohn Dyson 
11719b84335cSDavid Xu 			mtx_lock(&aio_job_mtx);
11729b84335cSDavid Xu 			/* Decrement the active job count. */
11739b84335cSDavid Xu 			ki->kaio_active_count--;
11749b84335cSDavid Xu 			mtx_unlock(&aio_job_mtx);
11759b84335cSDavid Xu 
1176759ccccaSDavid Xu 			AIO_LOCK(ki);
11771ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
117869cd28daSDoug Ambrisko 			aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1179759ccccaSDavid Xu 			AIO_UNLOCK(ki);
11801ce91824SDavid Xu 
11811ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
11822244ea07SJohn Dyson 		}
11832244ea07SJohn Dyson 
1184fd3bf775SJohn Dyson 		/*
1185bfbbc4aaSJason Evans 		 * Disconnect from user address space.
1186fd3bf775SJohn Dyson 		 */
1187fd3bf775SJohn Dyson 		if (curcp != mycp) {
11881ce91824SDavid Xu 
11891ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11901ce91824SDavid Xu 
1191bfbbc4aaSJason Evans 			/* Get the user address space to disconnect from. */
1192fd3bf775SJohn Dyson 			tmpvm = mycp->p_vmspace;
1193bfbbc4aaSJason Evans 
1194bfbbc4aaSJason Evans 			/* Get original address space for daemon. */
1195fd3bf775SJohn Dyson 			mycp->p_vmspace = myvm;
1196bfbbc4aaSJason Evans 
1197bfbbc4aaSJason Evans 			/* Activate the daemon's address space. */
1198079b7badSJulian Elischer 			pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1199bfbbc4aaSJason Evans #ifdef DIAGNOSTIC
1200bfbbc4aaSJason Evans 			if (tmpvm == myvm) {
1201bfbbc4aaSJason Evans 				printf("AIOD: vmspace problem -- %d\n",
1202bfbbc4aaSJason Evans 				    mycp->p_pid);
1203bfbbc4aaSJason Evans 			}
120411783b14SJohn Dyson #endif
1205bfbbc4aaSJason Evans 			/* Remove our vmspace reference. */
12062244ea07SJohn Dyson 			vmspace_free(tmpvm);
1207bfbbc4aaSJason Evans 
1208fd3bf775SJohn Dyson 			curcp = mycp;
12091ce91824SDavid Xu 
12101ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
12111ce91824SDavid Xu 			/*
12121ce91824SDavid Xu 			 * We have to restart to avoid race, we only sleep if
12131ce91824SDavid Xu 			 * no job can be selected, that should be
12141ce91824SDavid Xu 			 * curcp == mycp.
12151ce91824SDavid Xu 			 */
12161ce91824SDavid Xu 			continue;
1217fd3bf775SJohn Dyson 		}
1218fd3bf775SJohn Dyson 
12191ce91824SDavid Xu 		mtx_assert(&aio_job_mtx, MA_OWNED);
12201ce91824SDavid Xu 
1221fd3bf775SJohn Dyson 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1222b40ce416SJulian Elischer 		aiop->aiothreadflags |= AIOP_FREE;
1223fd3bf775SJohn Dyson 
1224fd3bf775SJohn Dyson 		/*
1225bfbbc4aaSJason Evans 		 * If daemon is inactive for a long time, allow it to exit,
1226bfbbc4aaSJason Evans 		 * thereby freeing resources.
1227fd3bf775SJohn Dyson 		 */
12281ce91824SDavid Xu 		if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
12291ce91824SDavid Xu 		    aiod_lifetime)) {
1230c3869e4bSAlan Cox 			if (TAILQ_EMPTY(&aio_jobs)) {
1231b40ce416SJulian Elischer 				if ((aiop->aiothreadflags & AIOP_FREE) &&
123284af4da6SJohn Dyson 				    (num_aio_procs > target_aio_procs)) {
1233fd3bf775SJohn Dyson 					TAILQ_REMOVE(&aio_freeproc, aiop, list);
123484af4da6SJohn Dyson 					num_aio_procs--;
12351ce91824SDavid Xu 					mtx_unlock(&aio_job_mtx);
12361ce91824SDavid Xu 					uma_zfree(aiop_zone, aiop);
12371ce91824SDavid Xu 					free_unr(aiod_unr, id);
1238bfbbc4aaSJason Evans #ifdef DIAGNOSTIC
1239bfbbc4aaSJason Evans 					if (mycp->p_vmspace->vm_refcnt <= 1) {
1240bfbbc4aaSJason Evans 						printf("AIOD: bad vm refcnt for"
1241bfbbc4aaSJason Evans 						    " exiting daemon: %d\n",
1242fd3bf775SJohn Dyson 						    mycp->p_vmspace->vm_refcnt);
1243bfbbc4aaSJason Evans 					}
124411783b14SJohn Dyson #endif
12453745c395SJulian Elischer 					kproc_exit(0);
1246fd3bf775SJohn Dyson 				}
12472244ea07SJohn Dyson 			}
12482244ea07SJohn Dyson 		}
12492244ea07SJohn Dyson 	}
12501ce91824SDavid Xu 	mtx_unlock(&aio_job_mtx);
12511ce91824SDavid Xu 	panic("shouldn't be here\n");
12521ce91824SDavid Xu }
12532244ea07SJohn Dyson 
12542244ea07SJohn Dyson /*
1255bfbbc4aaSJason Evans  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1256bfbbc4aaSJason Evans  * AIO daemon modifies its environment itself.
12572244ea07SJohn Dyson  */
12582244ea07SJohn Dyson static int
12591ce91824SDavid Xu aio_newproc(int *start)
1260fd3bf775SJohn Dyson {
12612244ea07SJohn Dyson 	int error;
1262c9a970a7SAlan Cox 	struct proc *p;
12631ce91824SDavid Xu 	int id;
12642244ea07SJohn Dyson 
12651ce91824SDavid Xu 	id = alloc_unr(aiod_unr);
12663745c395SJulian Elischer 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
12671ce91824SDavid Xu 		RFNOWAIT, 0, "aiod%d", id);
12681ce91824SDavid Xu 	if (error == 0) {
1269fd3bf775SJohn Dyson 		/*
12701ce91824SDavid Xu 		 * Wait until daemon is started.
1271fd3bf775SJohn Dyson 		 */
12721ce91824SDavid Xu 		sema_wait(&aio_newproc_sem);
12731ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
127484af4da6SJohn Dyson 		num_aio_procs++;
12751ce91824SDavid Xu 		if (start != NULL)
12767f34b521SDavid Xu 			(*start)--;
12771ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
12781ce91824SDavid Xu 	} else {
12791ce91824SDavid Xu 		free_unr(aiod_unr, id);
12801ce91824SDavid Xu 	}
1281ac41f2efSAlfred Perlstein 	return (error);
12822244ea07SJohn Dyson }
12832244ea07SJohn Dyson 
12842244ea07SJohn Dyson /*
128588ed460eSAlan Cox  * Try the high-performance, low-overhead physio method for eligible
128688ed460eSAlan Cox  * VCHR devices.  This method doesn't use an aio helper thread, and
128788ed460eSAlan Cox  * thus has very low overhead.
128888ed460eSAlan Cox  *
1289a9bf5e37SDavid Xu  * Assumes that the caller, aio_aqueue(), has incremented the file
129088ed460eSAlan Cox  * structure's reference count, preventing its deallocation for the
129188ed460eSAlan Cox  * duration of this call.
1292fd3bf775SJohn Dyson  */
129388ed460eSAlan Cox static int
1294bfbbc4aaSJason Evans aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1295fd3bf775SJohn Dyson {
1296fd3bf775SJohn Dyson 	struct aiocb *cb;
1297fd3bf775SJohn Dyson 	struct file *fp;
1298f743d981SAlexander Motin 	struct bio *bp;
1299f743d981SAlexander Motin 	struct buf *pbuf;
1300fd3bf775SJohn Dyson 	struct vnode *vp;
1301f3215a60SKonstantin Belousov 	struct cdevsw *csw;
1302f3215a60SKonstantin Belousov 	struct cdev *dev;
1303fd3bf775SJohn Dyson 	struct kaioinfo *ki;
13041ce91824SDavid Xu 	struct aioliojob *lj;
1305f743d981SAlexander Motin 	int error, ref, unmap, poff;
1306f743d981SAlexander Motin 	vm_prot_t prot;
1307fd3bf775SJohn Dyson 
130884af4da6SJohn Dyson 	cb = &aiocbe->uaiocb;
13099fbd7ccfSAlan Cox 	fp = aiocbe->fd_file;
1310fd3bf775SJohn Dyson 
13116160e12cSGleb Smirnoff 	if (fp == NULL || fp->f_type != DTYPE_VNODE)
1312008626c3SPoul-Henning Kamp 		return (-1);
1313fd3bf775SJohn Dyson 
13143b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
1315f743d981SAlexander Motin 	if (vp->v_type != VCHR)
1316f582ac06SBrian Feldman 		return (-1);
1317ad8de0f2SDavid Xu 	if (vp->v_bufobj.bo_bsize == 0)
1318ad8de0f2SDavid Xu 		return (-1);
13195d9d81e7SPoul-Henning Kamp 	if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1320008626c3SPoul-Henning Kamp 		return (-1);
1321fd3bf775SJohn Dyson 
1322f3215a60SKonstantin Belousov 	ref = 0;
1323f3215a60SKonstantin Belousov 	csw = devvn_refthread(vp, &dev, &ref);
1324f3215a60SKonstantin Belousov 	if (csw == NULL)
1325f3215a60SKonstantin Belousov 		return (ENXIO);
1326f743d981SAlexander Motin 
1327f743d981SAlexander Motin 	if ((csw->d_flags & D_DISK) == 0) {
1328f743d981SAlexander Motin 		error = -1;
1329f743d981SAlexander Motin 		goto unref;
1330f743d981SAlexander Motin 	}
1331f3215a60SKonstantin Belousov 	if (cb->aio_nbytes > dev->si_iosize_max) {
1332f3215a60SKonstantin Belousov 		error = -1;
1333f3215a60SKonstantin Belousov 		goto unref;
1334f3215a60SKonstantin Belousov 	}
1335f3215a60SKonstantin Belousov 
1336f743d981SAlexander Motin 	ki = p->p_aioinfo;
1337f743d981SAlexander Motin 	poff = (vm_offset_t)cb->aio_buf & PAGE_MASK;
1338f743d981SAlexander Motin 	unmap = ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed);
1339f743d981SAlexander Motin 	if (unmap) {
1340f743d981SAlexander Motin 		if (cb->aio_nbytes > MAXPHYS) {
1341f743d981SAlexander Motin 			error = -1;
1342f743d981SAlexander Motin 			goto unref;
1343f743d981SAlexander Motin 		}
1344f743d981SAlexander Motin 	} else {
1345f743d981SAlexander Motin 		if (cb->aio_nbytes > MAXPHYS - poff) {
1346f743d981SAlexander Motin 			error = -1;
1347f743d981SAlexander Motin 			goto unref;
1348f743d981SAlexander Motin 		}
1349f743d981SAlexander Motin 		if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) {
1350f743d981SAlexander Motin 			error = -1;
1351f743d981SAlexander Motin 			goto unref;
1352f743d981SAlexander Motin 		}
1353f743d981SAlexander Motin 	}
1354f743d981SAlexander Motin 	aiocbe->bp = bp = g_alloc_bio();
1355f743d981SAlexander Motin 	if (!unmap) {
1356f743d981SAlexander Motin 		aiocbe->pbuf = pbuf = (struct buf *)getpbuf(NULL);
1357f743d981SAlexander Motin 		BUF_KERNPROC(pbuf);
1358f743d981SAlexander Motin 	}
1359fd3bf775SJohn Dyson 
1360759ccccaSDavid Xu 	AIO_LOCK(ki);
13611ce91824SDavid Xu 	ki->kaio_count++;
1362f743d981SAlexander Motin 	if (!unmap)
13631ce91824SDavid Xu 		ki->kaio_buffer_count++;
13641ce91824SDavid Xu 	lj = aiocbe->lio;
13651ce91824SDavid Xu 	if (lj)
13661ce91824SDavid Xu 		lj->lioj_count++;
136784af4da6SJohn Dyson 	TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
13681ce91824SDavid Xu 	TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1369fd3bf775SJohn Dyson 	aiocbe->jobstate = JOBST_JOBQBUF;
137084af4da6SJohn Dyson 	cb->_aiocb_private.status = cb->aio_nbytes;
1371759ccccaSDavid Xu 	AIO_UNLOCK(ki);
13721ce91824SDavid Xu 
1373f743d981SAlexander Motin 	bp->bio_length = cb->aio_nbytes;
1374f743d981SAlexander Motin 	bp->bio_bcount = cb->aio_nbytes;
1375f743d981SAlexander Motin 	bp->bio_done = aio_physwakeup;
1376f743d981SAlexander Motin 	bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1377f743d981SAlexander Motin 	bp->bio_offset = cb->aio_offset;
1378f743d981SAlexander Motin 	bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1379f743d981SAlexander Motin 	bp->bio_dev = dev;
1380f743d981SAlexander Motin 	bp->bio_caller1 = (void *)aiocbe;
1381f743d981SAlexander Motin 
1382f743d981SAlexander Motin 	prot = VM_PROT_READ;
1383f743d981SAlexander Motin 	if (cb->aio_lio_opcode == LIO_READ)
1384f743d981SAlexander Motin 		prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
1385f743d981SAlexander Motin 	if ((aiocbe->npages = vm_fault_quick_hold_pages(
1386f743d981SAlexander Motin 	    &curproc->p_vmspace->vm_map,
1387f743d981SAlexander Motin 	    (vm_offset_t)bp->bio_data, bp->bio_length, prot, aiocbe->pages,
1388f743d981SAlexander Motin 	    sizeof(aiocbe->pages)/sizeof(aiocbe->pages[0]))) < 0) {
1389f743d981SAlexander Motin 		error = EFAULT;
1390f743d981SAlexander Motin 		goto doerror;
1391f743d981SAlexander Motin 	}
1392f743d981SAlexander Motin 	if (!unmap) {
1393f743d981SAlexander Motin 		pmap_qenter((vm_offset_t)pbuf->b_data,
1394f743d981SAlexander Motin 		    aiocbe->pages, aiocbe->npages);
1395f743d981SAlexander Motin 		bp->bio_data = pbuf->b_data + poff;
1396f743d981SAlexander Motin 	} else {
1397f743d981SAlexander Motin 		bp->bio_ma = aiocbe->pages;
1398f743d981SAlexander Motin 		bp->bio_ma_n = aiocbe->npages;
1399f743d981SAlexander Motin 		bp->bio_ma_offset = poff;
1400f743d981SAlexander Motin 		bp->bio_data = unmapped_buf;
1401f743d981SAlexander Motin 		bp->bio_flags |= BIO_UNMAPPED;
1402f743d981SAlexander Motin 	}
1403f743d981SAlexander Motin 
14041ce91824SDavid Xu 	atomic_add_int(&num_queue_count, 1);
1405f743d981SAlexander Motin 	if (!unmap)
14061ce91824SDavid Xu 		atomic_add_int(&num_buf_aio, 1);
14071ce91824SDavid Xu 
1408bfbbc4aaSJason Evans 	/* Perform transfer. */
1409f743d981SAlexander Motin 	csw->d_strategy(bp);
1410f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1411ac41f2efSAlfred Perlstein 	return (0);
1412fd3bf775SJohn Dyson 
1413fd3bf775SJohn Dyson doerror:
1414759ccccaSDavid Xu 	AIO_LOCK(ki);
1415f743d981SAlexander Motin 	aiocbe->jobstate = JOBST_NULL;
1416f743d981SAlexander Motin 	TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1417f743d981SAlexander Motin 	TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
14181ce91824SDavid Xu 	ki->kaio_count--;
1419f743d981SAlexander Motin 	if (!unmap)
1420fd3bf775SJohn Dyson 		ki->kaio_buffer_count--;
1421bfbbc4aaSJason Evans 	if (lj)
14221ce91824SDavid Xu 		lj->lioj_count--;
1423759ccccaSDavid Xu 	AIO_UNLOCK(ki);
1424f743d981SAlexander Motin 	if (pbuf) {
1425f743d981SAlexander Motin 		relpbuf(pbuf, NULL);
1426f743d981SAlexander Motin 		aiocbe->pbuf = NULL;
1427f743d981SAlexander Motin 	}
1428f743d981SAlexander Motin 	g_destroy_bio(bp);
1429f743d981SAlexander Motin 	aiocbe->bp = NULL;
1430f3215a60SKonstantin Belousov unref:
1431f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1432fd3bf775SJohn Dyson 	return (error);
1433fd3bf775SJohn Dyson }
1434fd3bf775SJohn Dyson 
1435fd3bf775SJohn Dyson /*
1436bfbbc4aaSJason Evans  * Wake up aio requests that may be serviceable now.
1437bfbbc4aaSJason Evans  */
143848dac059SAlan Cox static void
143921d56e9cSAlfred Perlstein aio_swake_cb(struct socket *so, struct sockbuf *sb)
1440bfbbc4aaSJason Evans {
1441bfbbc4aaSJason Evans 	struct aiocblist *cb, *cbn;
144299eee864SDavid Xu 	int opcode;
1443bfbbc4aaSJason Evans 
144474fb0ba7SJohn Baldwin 	SOCKBUF_LOCK_ASSERT(sb);
14451aa4c324SDavid Xu 	if (sb == &so->so_snd)
1446bfbbc4aaSJason Evans 		opcode = LIO_WRITE;
14471aa4c324SDavid Xu 	else
1448bfbbc4aaSJason Evans 		opcode = LIO_READ;
1449bfbbc4aaSJason Evans 
14501aa4c324SDavid Xu 	sb->sb_flags &= ~SB_AIO;
14511aa4c324SDavid Xu 	mtx_lock(&aio_job_mtx);
14522a522eb9SJohn Baldwin 	TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1453bfbbc4aaSJason Evans 		if (opcode == cb->uaiocb.aio_lio_opcode) {
14548c0d9af5SDavid Xu 			if (cb->jobstate != JOBST_JOBQSOCK)
14551ce91824SDavid Xu 				panic("invalid queue value");
14561aa4c324SDavid Xu 			/* XXX
14571aa4c324SDavid Xu 			 * We don't have actual sockets backend yet,
14581aa4c324SDavid Xu 			 * so we simply move the requests to the generic
14591aa4c324SDavid Xu 			 * file I/O backend.
14601ce91824SDavid Xu 			 */
14611aa4c324SDavid Xu 			TAILQ_REMOVE(&so->so_aiojobq, cb, list);
14621ce91824SDavid Xu 			TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
146399eee864SDavid Xu 			aio_kick_nowait(cb->userproc);
1464bfbbc4aaSJason Evans 		}
1465bfbbc4aaSJason Evans 	}
14661aa4c324SDavid Xu 	mtx_unlock(&aio_job_mtx);
1467bfbbc4aaSJason Evans }
1468bfbbc4aaSJason Evans 
14693858a1f4SJohn Baldwin static int
14703858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
14713858a1f4SJohn Baldwin {
14723858a1f4SJohn Baldwin 
14733858a1f4SJohn Baldwin 	/*
14743858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
14753858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
14763858a1f4SJohn Baldwin 	 */
14773858a1f4SJohn Baldwin 	nsig->sigev_notify = osig->sigev_notify;
14783858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
14793858a1f4SJohn Baldwin 	case SIGEV_NONE:
14803858a1f4SJohn Baldwin 		break;
14813858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
14823858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
14833858a1f4SJohn Baldwin 		break;
14843858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
14853858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
14863858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
14873858a1f4SJohn Baldwin 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
14883858a1f4SJohn Baldwin 		break;
14893858a1f4SJohn Baldwin 	default:
14903858a1f4SJohn Baldwin 		return (EINVAL);
14913858a1f4SJohn Baldwin 	}
14923858a1f4SJohn Baldwin 	return (0);
14933858a1f4SJohn Baldwin }
14943858a1f4SJohn Baldwin 
14953858a1f4SJohn Baldwin static int
14963858a1f4SJohn Baldwin aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
14973858a1f4SJohn Baldwin {
14983858a1f4SJohn Baldwin 	struct oaiocb *ojob;
14993858a1f4SJohn Baldwin 	int error;
15003858a1f4SJohn Baldwin 
15013858a1f4SJohn Baldwin 	bzero(kjob, sizeof(struct aiocb));
15023858a1f4SJohn Baldwin 	error = copyin(ujob, kjob, sizeof(struct oaiocb));
15033858a1f4SJohn Baldwin 	if (error)
15043858a1f4SJohn Baldwin 		return (error);
15053858a1f4SJohn Baldwin 	ojob = (struct oaiocb *)kjob;
15063858a1f4SJohn Baldwin 	return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
15073858a1f4SJohn Baldwin }
15083858a1f4SJohn Baldwin 
15093858a1f4SJohn Baldwin static int
15103858a1f4SJohn Baldwin aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
15113858a1f4SJohn Baldwin {
15123858a1f4SJohn Baldwin 
15133858a1f4SJohn Baldwin 	return (copyin(ujob, kjob, sizeof(struct aiocb)));
15143858a1f4SJohn Baldwin }
15153858a1f4SJohn Baldwin 
15163858a1f4SJohn Baldwin static long
15173858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob)
15183858a1f4SJohn Baldwin {
15193858a1f4SJohn Baldwin 
15203858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.status));
15213858a1f4SJohn Baldwin }
15223858a1f4SJohn Baldwin 
15233858a1f4SJohn Baldwin static long
15243858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob)
15253858a1f4SJohn Baldwin {
15263858a1f4SJohn Baldwin 
15273858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.error));
15283858a1f4SJohn Baldwin }
15293858a1f4SJohn Baldwin 
15303858a1f4SJohn Baldwin static int
15313858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status)
15323858a1f4SJohn Baldwin {
15333858a1f4SJohn Baldwin 
15343858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.status, status));
15353858a1f4SJohn Baldwin }
15363858a1f4SJohn Baldwin 
15373858a1f4SJohn Baldwin static int
15383858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error)
15393858a1f4SJohn Baldwin {
15403858a1f4SJohn Baldwin 
15413858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.error, error));
15423858a1f4SJohn Baldwin }
15433858a1f4SJohn Baldwin 
15443858a1f4SJohn Baldwin static int
15453858a1f4SJohn Baldwin aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
15463858a1f4SJohn Baldwin {
15473858a1f4SJohn Baldwin 
15483858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
15493858a1f4SJohn Baldwin }
15503858a1f4SJohn Baldwin 
15513858a1f4SJohn Baldwin static int
15523858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
15533858a1f4SJohn Baldwin {
15543858a1f4SJohn Baldwin 
15553858a1f4SJohn Baldwin 	return (suword(ujobp, (long)ujob));
15563858a1f4SJohn Baldwin }
15573858a1f4SJohn Baldwin 
15583858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = {
15593858a1f4SJohn Baldwin 	.copyin = aiocb_copyin,
15603858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
15613858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
15623858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
15633858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
15643858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
15653858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
15663858a1f4SJohn Baldwin };
15673858a1f4SJohn Baldwin 
15683858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = {
15693858a1f4SJohn Baldwin 	.copyin = aiocb_copyin_old_sigevent,
15703858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
15713858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
15723858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
15733858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
15743858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
15753858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
15763858a1f4SJohn Baldwin };
15773858a1f4SJohn Baldwin 
1578bfbbc4aaSJason Evans /*
1579bfbbc4aaSJason Evans  * Queue a new AIO request.  Choosing either the threaded or direct physio VCHR
1580bfbbc4aaSJason Evans  * technique is done in this code.
15812244ea07SJohn Dyson  */
15826a1162d4SAlexander Leidinger int
1583a9bf5e37SDavid Xu aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
15843858a1f4SJohn Baldwin 	int type, struct aiocb_ops *ops)
1585fd3bf775SJohn Dyson {
1586b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
15877008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
15882244ea07SJohn Dyson 	struct file *fp;
1589bfbbc4aaSJason Evans 	struct socket *so;
1590dbbccfe9SDavid Xu 	struct aiocblist *aiocbe, *cb;
15912244ea07SJohn Dyson 	struct kaioinfo *ki;
1592c6fa9f78SAlan Cox 	struct kevent kev;
1593576c004fSAlfred Perlstein 	struct sockbuf *sb;
15941ce91824SDavid Xu 	int opcode;
15951ce91824SDavid Xu 	int error;
15964db71d27SJohn-Mark Gurney 	int fd, kqfd;
15971ce91824SDavid Xu 	int jid;
1598fde80935SDavid Xu 	u_short evflags;
15992244ea07SJohn Dyson 
1600a9bf5e37SDavid Xu 	if (p->p_aioinfo == NULL)
1601a9bf5e37SDavid Xu 		aio_init_aioinfo(p);
1602a9bf5e37SDavid Xu 
16031ce91824SDavid Xu 	ki = p->p_aioinfo;
16041ce91824SDavid Xu 
16053858a1f4SJohn Baldwin 	ops->store_status(job, -1);
16063858a1f4SJohn Baldwin 	ops->store_error(job, 0);
16073858a1f4SJohn Baldwin 	ops->store_kernelinfo(job, -1);
1608a9bf5e37SDavid Xu 
1609a9bf5e37SDavid Xu 	if (num_queue_count >= max_queue_count ||
1610a9bf5e37SDavid Xu 	    ki->kaio_count >= ki->kaio_qallowed_count) {
16113858a1f4SJohn Baldwin 		ops->store_error(job, EAGAIN);
1612a9bf5e37SDavid Xu 		return (EAGAIN);
1613a9bf5e37SDavid Xu 	}
1614a9bf5e37SDavid Xu 
16151ce91824SDavid Xu 	aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1616d8b0556cSKonstantin Belousov 	knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1617fd3bf775SJohn Dyson 
16183858a1f4SJohn Baldwin 	error = ops->copyin(job, &aiocbe->uaiocb);
16192244ea07SJohn Dyson 	if (error) {
16203858a1f4SJohn Baldwin 		ops->store_error(job, error);
1621c897b813SJeff Roberson 		uma_zfree(aiocb_zone, aiocbe);
1622ac41f2efSAlfred Perlstein 		return (error);
16232244ea07SJohn Dyson 	}
162468d71118SDavid Xu 
1625434ea137SGleb Smirnoff 	/* XXX: aio_nbytes is later casted to signed types. */
162694fce847SGleb Smirnoff 	if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1627434ea137SGleb Smirnoff 		uma_zfree(aiocb_zone, aiocbe);
1628434ea137SGleb Smirnoff 		return (EINVAL);
1629434ea137SGleb Smirnoff 	}
1630434ea137SGleb Smirnoff 
163168d71118SDavid Xu 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
163268d71118SDavid Xu 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
163368d71118SDavid Xu 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
163468d71118SDavid Xu 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
16353858a1f4SJohn Baldwin 		ops->store_error(job, EINVAL);
163668d71118SDavid Xu 		uma_zfree(aiocb_zone, aiocbe);
163768d71118SDavid Xu 		return (EINVAL);
163868d71118SDavid Xu 	}
163968d71118SDavid Xu 
16404c0fb2cfSDavid Xu 	if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
16414c0fb2cfSDavid Xu 	     aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
16422f3cf918SAlfred Perlstein 		!_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1643c897b813SJeff Roberson 		uma_zfree(aiocb_zone, aiocbe);
1644ac41f2efSAlfred Perlstein 		return (EINVAL);
16452f3cf918SAlfred Perlstein 	}
16462244ea07SJohn Dyson 
16474c0fb2cfSDavid Xu 	ksiginfo_init(&aiocbe->ksi);
16484c0fb2cfSDavid Xu 
1649bfbbc4aaSJason Evans 	/* Save userspace address of the job info. */
165011783b14SJohn Dyson 	aiocbe->uuaiocb = job;
165111783b14SJohn Dyson 
1652bfbbc4aaSJason Evans 	/* Get the opcode. */
1653bfbbc4aaSJason Evans 	if (type != LIO_NOP)
1654a624e84fSJohn Dyson 		aiocbe->uaiocb.aio_lio_opcode = type;
1655a624e84fSJohn Dyson 	opcode = aiocbe->uaiocb.aio_lio_opcode;
16562244ea07SJohn Dyson 
1657a9d2f8d8SRobert Watson 	/*
1658a9d2f8d8SRobert Watson 	 * Validate the opcode and fetch the file object for the specified
1659a9d2f8d8SRobert Watson 	 * file descriptor.
1660a9d2f8d8SRobert Watson 	 *
1661a9d2f8d8SRobert Watson 	 * XXXRW: Moved the opcode validation up here so that we don't
1662a9d2f8d8SRobert Watson 	 * retrieve a file descriptor without knowing what the capabiltity
1663a9d2f8d8SRobert Watson 	 * should be.
1664a9d2f8d8SRobert Watson 	 */
16652244ea07SJohn Dyson 	fd = aiocbe->uaiocb.aio_fildes;
16662a522eb9SJohn Baldwin 	switch (opcode) {
16672a522eb9SJohn Baldwin 	case LIO_WRITE:
16687008be5bSPawel Jakub Dawidek 		error = fget_write(td, fd,
16697008be5bSPawel Jakub Dawidek 		    cap_rights_init(&rights, CAP_PWRITE), &fp);
16702a522eb9SJohn Baldwin 		break;
16712a522eb9SJohn Baldwin 	case LIO_READ:
16727008be5bSPawel Jakub Dawidek 		error = fget_read(td, fd,
16737008be5bSPawel Jakub Dawidek 		    cap_rights_init(&rights, CAP_PREAD), &fp);
1674a9d2f8d8SRobert Watson 		break;
1675a9d2f8d8SRobert Watson 	case LIO_SYNC:
16767008be5bSPawel Jakub Dawidek 		error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1677a9d2f8d8SRobert Watson 		break;
16786160e12cSGleb Smirnoff 	case LIO_MLOCK:
16796160e12cSGleb Smirnoff 		fp = NULL;
16806160e12cSGleb Smirnoff 		break;
1681a9d2f8d8SRobert Watson 	case LIO_NOP:
16827008be5bSPawel Jakub Dawidek 		error = fget(td, fd, cap_rights_init(&rights), &fp);
16832a522eb9SJohn Baldwin 		break;
16842a522eb9SJohn Baldwin 	default:
1685a9d2f8d8SRobert Watson 		error = EINVAL;
16862a522eb9SJohn Baldwin 	}
16872a522eb9SJohn Baldwin 	if (error) {
1688c897b813SJeff Roberson 		uma_zfree(aiocb_zone, aiocbe);
16893858a1f4SJohn Baldwin 		ops->store_error(job, error);
1690af56abaaSJohn Baldwin 		return (error);
16912244ea07SJohn Dyson 	}
169299eee864SDavid Xu 
169399eee864SDavid Xu 	if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
169499eee864SDavid Xu 		error = EINVAL;
169599eee864SDavid Xu 		goto aqueue_fail;
169699eee864SDavid Xu 	}
16972244ea07SJohn Dyson 
1698dbbccfe9SDavid Xu 	if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1699ae124fc4SAlan Cox 		error = EINVAL;
1700ae124fc4SAlan Cox 		goto aqueue_fail;
17012244ea07SJohn Dyson 	}
17021ce91824SDavid Xu 
170399eee864SDavid Xu 	aiocbe->fd_file = fp;
17041ce91824SDavid Xu 
170599eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
170699eee864SDavid Xu 	jid = jobrefid++;
170799eee864SDavid Xu 	aiocbe->seqno = jobseqno++;
170899eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
17093858a1f4SJohn Baldwin 	error = ops->store_kernelinfo(job, jid);
17101ce91824SDavid Xu 	if (error) {
17111ce91824SDavid Xu 		error = EINVAL;
17121ce91824SDavid Xu 		goto aqueue_fail;
17131ce91824SDavid Xu 	}
17141ce91824SDavid Xu 	aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
17152244ea07SJohn Dyson 
17162244ea07SJohn Dyson 	if (opcode == LIO_NOP) {
1717a5c0b1c0SAlan Cox 		fdrop(fp, td);
1718c897b813SJeff Roberson 		uma_zfree(aiocb_zone, aiocbe);
1719ac41f2efSAlfred Perlstein 		return (0);
17202244ea07SJohn Dyson 	}
17212244ea07SJohn Dyson 
17224db71d27SJohn-Mark Gurney 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1723cb679c38SJonathan Lemon 		goto no_kqueue;
1724fde80935SDavid Xu 	evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1725fde80935SDavid Xu 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1726fde80935SDavid Xu 		error = EINVAL;
1727fde80935SDavid Xu 		goto aqueue_fail;
1728fde80935SDavid Xu 	}
17294db71d27SJohn-Mark Gurney 	kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1730b46f1c55SAlan Cox 	kev.ident = (uintptr_t)aiocbe->uuaiocb;
1731cb679c38SJonathan Lemon 	kev.filter = EVFILT_AIO;
1732fde80935SDavid Xu 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1733b46f1c55SAlan Cox 	kev.data = (intptr_t)aiocbe;
17341ce91824SDavid Xu 	kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
17354db71d27SJohn-Mark Gurney 	error = kqfd_register(kqfd, &kev, td, 1);
1736cb679c38SJonathan Lemon aqueue_fail:
1737cb679c38SJonathan Lemon 	if (error) {
17386160e12cSGleb Smirnoff 		if (fp)
1739a5c0b1c0SAlan Cox 			fdrop(fp, td);
1740c897b813SJeff Roberson 		uma_zfree(aiocb_zone, aiocbe);
17413858a1f4SJohn Baldwin 		ops->store_error(job, error);
1742279d7226SMatthew Dillon 		goto done;
1743cb679c38SJonathan Lemon 	}
1744cb679c38SJonathan Lemon no_kqueue:
1745cb679c38SJonathan Lemon 
17463858a1f4SJohn Baldwin 	ops->store_error(job, EINPROGRESS);
1747fd3bf775SJohn Dyson 	aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
17482244ea07SJohn Dyson 	aiocbe->userproc = p;
1749f8f750c5SRobert Watson 	aiocbe->cred = crhold(td->td_ucred);
17502244ea07SJohn Dyson 	aiocbe->jobflags = 0;
175184af4da6SJohn Dyson 	aiocbe->lio = lj;
17522244ea07SJohn Dyson 
175399eee864SDavid Xu 	if (opcode == LIO_SYNC)
175499eee864SDavid Xu 		goto queueit;
175599eee864SDavid Xu 
17566160e12cSGleb Smirnoff 	if (fp && fp->f_type == DTYPE_SOCKET) {
1757bfbbc4aaSJason Evans 		/*
1758bfbbc4aaSJason Evans 		 * Alternate queueing for socket ops: Reach down into the
1759bfbbc4aaSJason Evans 		 * descriptor to get the socket data.  Then check to see if the
1760bfbbc4aaSJason Evans 		 * socket is ready to be read or written (based on the requested
1761bfbbc4aaSJason Evans 		 * operation).
1762bfbbc4aaSJason Evans 		 *
1763bfbbc4aaSJason Evans 		 * If it is not ready for io, then queue the aiocbe on the
1764bfbbc4aaSJason Evans 		 * socket, and set the flags so we get a call when sbnotify()
1765bfbbc4aaSJason Evans 		 * happens.
1766576c004fSAlfred Perlstein 		 *
1767576c004fSAlfred Perlstein 		 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1768576c004fSAlfred Perlstein 		 * and unlock the snd sockbuf for no reason.
1769bfbbc4aaSJason Evans 		 */
177048e3128bSMatthew Dillon 		so = fp->f_data;
1771576c004fSAlfred Perlstein 		sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1772576c004fSAlfred Perlstein 		SOCKBUF_LOCK(sb);
1773bfbbc4aaSJason Evans 		if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1774bfbbc4aaSJason Evans 		    LIO_WRITE) && (!sowriteable(so)))) {
1775576c004fSAlfred Perlstein 			sb->sb_flags |= SB_AIO;
17761aa4c324SDavid Xu 
17771aa4c324SDavid Xu 			mtx_lock(&aio_job_mtx);
17781aa4c324SDavid Xu 			TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
17791aa4c324SDavid Xu 			mtx_unlock(&aio_job_mtx);
17801aa4c324SDavid Xu 
1781759ccccaSDavid Xu 			AIO_LOCK(ki);
17821ce91824SDavid Xu 			TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
17831aa4c324SDavid Xu 			TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
17841ce91824SDavid Xu 			aiocbe->jobstate = JOBST_JOBQSOCK;
17851ce91824SDavid Xu 			ki->kaio_count++;
17861ce91824SDavid Xu 			if (lj)
17871ce91824SDavid Xu 				lj->lioj_count++;
1788759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1789576c004fSAlfred Perlstein 			SOCKBUF_UNLOCK(sb);
17901ce91824SDavid Xu 			atomic_add_int(&num_queue_count, 1);
1791279d7226SMatthew Dillon 			error = 0;
1792279d7226SMatthew Dillon 			goto done;
1793bfbbc4aaSJason Evans 		}
1794576c004fSAlfred Perlstein 		SOCKBUF_UNLOCK(sb);
1795bfbbc4aaSJason Evans 	}
1796bfbbc4aaSJason Evans 
1797bfbbc4aaSJason Evans 	if ((error = aio_qphysio(p, aiocbe)) == 0)
1798279d7226SMatthew Dillon 		goto done;
17991ce91824SDavid Xu #if 0
1800279d7226SMatthew Dillon 	if (error > 0) {
1801fd3bf775SJohn Dyson 		aiocbe->uaiocb._aiocb_private.error = error;
18023858a1f4SJohn Baldwin 		ops->store_error(job, error);
1803279d7226SMatthew Dillon 		goto done;
1804fd3bf775SJohn Dyson 	}
18051ce91824SDavid Xu #endif
180699eee864SDavid Xu queueit:
180799eee864SDavid Xu 	atomic_add_int(&num_queue_count, 1);
180884af4da6SJohn Dyson 
1809759ccccaSDavid Xu 	AIO_LOCK(ki);
18101ce91824SDavid Xu 	ki->kaio_count++;
1811bfbbc4aaSJason Evans 	if (lj)
18121ce91824SDavid Xu 		lj->lioj_count++;
1813fd3bf775SJohn Dyson 	TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
18141ce91824SDavid Xu 	TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
181599eee864SDavid Xu 	if (opcode == LIO_SYNC) {
1816dbbccfe9SDavid Xu 		TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1817dbbccfe9SDavid Xu 			if (cb->fd_file == aiocbe->fd_file &&
1818dbbccfe9SDavid Xu 			    cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1819dbbccfe9SDavid Xu 			    cb->seqno < aiocbe->seqno) {
1820dbbccfe9SDavid Xu 				cb->jobflags |= AIOCBLIST_CHECKSYNC;
1821dbbccfe9SDavid Xu 				aiocbe->pending++;
1822dbbccfe9SDavid Xu 			}
1823dbbccfe9SDavid Xu 		}
1824dbbccfe9SDavid Xu 		TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1825dbbccfe9SDavid Xu 			if (cb->fd_file == aiocbe->fd_file &&
1826dbbccfe9SDavid Xu 			    cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1827dbbccfe9SDavid Xu 			    cb->seqno < aiocbe->seqno) {
1828dbbccfe9SDavid Xu 				cb->jobflags |= AIOCBLIST_CHECKSYNC;
1829dbbccfe9SDavid Xu 				aiocbe->pending++;
1830dbbccfe9SDavid Xu 			}
1831dbbccfe9SDavid Xu 		}
1832dbbccfe9SDavid Xu 		if (aiocbe->pending != 0) {
183399eee864SDavid Xu 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
183499eee864SDavid Xu 			aiocbe->jobstate = JOBST_JOBQSYNC;
1835759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1836dbbccfe9SDavid Xu 			goto done;
1837dbbccfe9SDavid Xu 		}
1838dbbccfe9SDavid Xu 	}
18391ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
18401ce91824SDavid Xu 	TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
18411ce91824SDavid Xu 	aiocbe->jobstate = JOBST_JOBQGLOBAL;
184299eee864SDavid Xu 	aio_kick_nowait(p);
184399eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
1844759ccccaSDavid Xu 	AIO_UNLOCK(ki);
18451ce91824SDavid Xu 	error = 0;
184699eee864SDavid Xu done:
184799eee864SDavid Xu 	return (error);
184899eee864SDavid Xu }
184999eee864SDavid Xu 
185099eee864SDavid Xu static void
185199eee864SDavid Xu aio_kick_nowait(struct proc *userp)
185299eee864SDavid Xu {
185399eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
185499eee864SDavid Xu 	struct aiothreadlist *aiop;
185599eee864SDavid Xu 
185699eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
185799eee864SDavid Xu 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
185899eee864SDavid Xu 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
185999eee864SDavid Xu 		aiop->aiothreadflags &= ~AIOP_FREE;
186099eee864SDavid Xu 		wakeup(aiop->aiothread);
1861dbbccfe9SDavid Xu 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1862dbbccfe9SDavid Xu 	    ((ki->kaio_active_count + num_aio_resv_start) <
1863dbbccfe9SDavid Xu 	    ki->kaio_maxactive_count)) {
186499eee864SDavid Xu 		taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
186599eee864SDavid Xu 	}
186699eee864SDavid Xu }
186799eee864SDavid Xu 
1868dbbccfe9SDavid Xu static int
186999eee864SDavid Xu aio_kick(struct proc *userp)
187099eee864SDavid Xu {
187199eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
187299eee864SDavid Xu 	struct aiothreadlist *aiop;
1873dbbccfe9SDavid Xu 	int error, ret = 0;
187499eee864SDavid Xu 
187599eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
187699eee864SDavid Xu retryproc:
1877d254af07SMatthew Dillon 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
18782244ea07SJohn Dyson 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
1879b40ce416SJulian Elischer 		aiop->aiothreadflags &= ~AIOP_FREE;
1880b40ce416SJulian Elischer 		wakeup(aiop->aiothread);
1881fd3bf775SJohn Dyson 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1882fd3bf775SJohn Dyson 	    ((ki->kaio_active_count + num_aio_resv_start) <
1883fd3bf775SJohn Dyson 	    ki->kaio_maxactive_count)) {
1884fd3bf775SJohn Dyson 		num_aio_resv_start++;
18851ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
18861ce91824SDavid Xu 		error = aio_newproc(&num_aio_resv_start);
18871ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
18881ce91824SDavid Xu 		if (error) {
188984af4da6SJohn Dyson 			num_aio_resv_start--;
18902244ea07SJohn Dyson 			goto retryproc;
1891fd3bf775SJohn Dyson 		}
1892dbbccfe9SDavid Xu 	} else {
1893dbbccfe9SDavid Xu 		ret = -1;
18941ce91824SDavid Xu 	}
1895dbbccfe9SDavid Xu 	return (ret);
189699eee864SDavid Xu }
18971ce91824SDavid Xu 
189899eee864SDavid Xu static void
189999eee864SDavid Xu aio_kick_helper(void *context, int pending)
190099eee864SDavid Xu {
190199eee864SDavid Xu 	struct proc *userp = context;
190299eee864SDavid Xu 
190399eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
1904dbbccfe9SDavid Xu 	while (--pending >= 0) {
1905dbbccfe9SDavid Xu 		if (aio_kick(userp))
1906dbbccfe9SDavid Xu 			break;
1907dbbccfe9SDavid Xu 	}
190899eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
19092244ea07SJohn Dyson }
19102244ea07SJohn Dyson 
1911fd3bf775SJohn Dyson /*
1912bfbbc4aaSJason Evans  * Support the aio_return system call, as a side-effect, kernel resources are
1913bfbbc4aaSJason Evans  * released.
19142244ea07SJohn Dyson  */
19153858a1f4SJohn Baldwin static int
19163858a1f4SJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1917fd3bf775SJohn Dyson {
1918b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19191ce91824SDavid Xu 	struct aiocblist *cb;
19202244ea07SJohn Dyson 	struct kaioinfo *ki;
19211ce91824SDavid Xu 	int status, error;
19222244ea07SJohn Dyson 
1923c0bf5caaSAlan Cox 	ki = p->p_aioinfo;
1924c0bf5caaSAlan Cox 	if (ki == NULL)
1925ac41f2efSAlfred Perlstein 		return (EINVAL);
1926759ccccaSDavid Xu 	AIO_LOCK(ki);
19271ce91824SDavid Xu 	TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
19281ce91824SDavid Xu 		if (cb->uuaiocb == uaiocb)
1929c0bf5caaSAlan Cox 			break;
1930c0bf5caaSAlan Cox 	}
1931c0bf5caaSAlan Cox 	if (cb != NULL) {
19321ce91824SDavid Xu 		MPASS(cb->jobstate == JOBST_JOBFINISHED);
19331ce91824SDavid Xu 		status = cb->uaiocb._aiocb_private.status;
19341ce91824SDavid Xu 		error = cb->uaiocb._aiocb_private.error;
19351ce91824SDavid Xu 		td->td_retval[0] = status;
193669cd28daSDoug Ambrisko 		if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
19371c4bcd05SJeff Roberson 			td->td_ru.ru_oublock += cb->outputcharge;
193869cd28daSDoug Ambrisko 			cb->outputcharge = 0;
193969cd28daSDoug Ambrisko 		} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
19401c4bcd05SJeff Roberson 			td->td_ru.ru_inblock += cb->inputcharge;
194169cd28daSDoug Ambrisko 			cb->inputcharge = 0;
194269cd28daSDoug Ambrisko 		}
194384af4da6SJohn Dyson 		aio_free_entry(cb);
1944759ccccaSDavid Xu 		AIO_UNLOCK(ki);
19453858a1f4SJohn Baldwin 		ops->store_error(uaiocb, error);
19463858a1f4SJohn Baldwin 		ops->store_status(uaiocb, status);
194755a122bfSDavid Xu 	} else {
19481ce91824SDavid Xu 		error = EINVAL;
1949759ccccaSDavid Xu 		AIO_UNLOCK(ki);
195055a122bfSDavid Xu 	}
19511ce91824SDavid Xu 	return (error);
19522244ea07SJohn Dyson }
19532244ea07SJohn Dyson 
19543858a1f4SJohn Baldwin int
19558451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap)
19563858a1f4SJohn Baldwin {
19573858a1f4SJohn Baldwin 
19583858a1f4SJohn Baldwin 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
19593858a1f4SJohn Baldwin }
19603858a1f4SJohn Baldwin 
19612244ea07SJohn Dyson /*
1962bfbbc4aaSJason Evans  * Allow a process to wakeup when any of the I/O requests are completed.
19632244ea07SJohn Dyson  */
19643858a1f4SJohn Baldwin static int
19653858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
19663858a1f4SJohn Baldwin     struct timespec *ts)
1967fd3bf775SJohn Dyson {
1968b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19694a11ca4eSPoul-Henning Kamp 	struct timeval atv;
19702244ea07SJohn Dyson 	struct kaioinfo *ki;
19711ce91824SDavid Xu 	struct aiocblist *cb, *cbfirst;
19723858a1f4SJohn Baldwin 	int error, i, timo;
19732244ea07SJohn Dyson 
19742244ea07SJohn Dyson 	timo = 0;
19753858a1f4SJohn Baldwin 	if (ts) {
19763858a1f4SJohn Baldwin 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
19772244ea07SJohn Dyson 			return (EINVAL);
19782244ea07SJohn Dyson 
19793858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
19802244ea07SJohn Dyson 		if (itimerfix(&atv))
19812244ea07SJohn Dyson 			return (EINVAL);
1982227ee8a1SPoul-Henning Kamp 		timo = tvtohz(&atv);
19832244ea07SJohn Dyson 	}
19842244ea07SJohn Dyson 
19852244ea07SJohn Dyson 	ki = p->p_aioinfo;
19862244ea07SJohn Dyson 	if (ki == NULL)
1987ac41f2efSAlfred Perlstein 		return (EAGAIN);
19882244ea07SJohn Dyson 
19893858a1f4SJohn Baldwin 	if (njoblist == 0)
1990ac41f2efSAlfred Perlstein 		return (0);
19912244ea07SJohn Dyson 
1992759ccccaSDavid Xu 	AIO_LOCK(ki);
19931ce91824SDavid Xu 	for (;;) {
19941ce91824SDavid Xu 		cbfirst = NULL;
19951ce91824SDavid Xu 		error = 0;
19961ce91824SDavid Xu 		TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
199784af4da6SJohn Dyson 			for (i = 0; i < njoblist; i++) {
19981ce91824SDavid Xu 				if (cb->uuaiocb == ujoblist[i]) {
19991ce91824SDavid Xu 					if (cbfirst == NULL)
20001ce91824SDavid Xu 						cbfirst = cb;
20011ce91824SDavid Xu 					if (cb->jobstate == JOBST_JOBFINISHED)
20021ce91824SDavid Xu 						goto RETURN;
200384af4da6SJohn Dyson 				}
200484af4da6SJohn Dyson 			}
200584af4da6SJohn Dyson 		}
20061ce91824SDavid Xu 		/* All tasks were finished. */
20071ce91824SDavid Xu 		if (cbfirst == NULL)
20081ce91824SDavid Xu 			break;
20092244ea07SJohn Dyson 
2010fd3bf775SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
2011759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
20121ce91824SDavid Xu 		    "aiospn", timo);
20131ce91824SDavid Xu 		if (error == ERESTART)
20141ce91824SDavid Xu 			error = EINTR;
20151ce91824SDavid Xu 		if (error)
20161ce91824SDavid Xu 			break;
20172244ea07SJohn Dyson 	}
20181ce91824SDavid Xu RETURN:
2019759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20203858a1f4SJohn Baldwin 	return (error);
20213858a1f4SJohn Baldwin }
20223858a1f4SJohn Baldwin 
20233858a1f4SJohn Baldwin int
20248451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
20253858a1f4SJohn Baldwin {
20263858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
20273858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
20283858a1f4SJohn Baldwin 	int error;
20293858a1f4SJohn Baldwin 
20303858a1f4SJohn Baldwin 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
20313858a1f4SJohn Baldwin 		return (EINVAL);
20323858a1f4SJohn Baldwin 
20333858a1f4SJohn Baldwin 	if (uap->timeout) {
20343858a1f4SJohn Baldwin 		/* Get timespec struct. */
20353858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
20363858a1f4SJohn Baldwin 			return (error);
20373858a1f4SJohn Baldwin 		tsp = &ts;
20383858a1f4SJohn Baldwin 	} else
20393858a1f4SJohn Baldwin 		tsp = NULL;
20403858a1f4SJohn Baldwin 
20413858a1f4SJohn Baldwin 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
20423858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
20433858a1f4SJohn Baldwin 	if (error == 0)
20443858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
20451ce91824SDavid Xu 	uma_zfree(aiol_zone, ujoblist);
20461ce91824SDavid Xu 	return (error);
20472244ea07SJohn Dyson }
2048ee877a35SJohn Dyson 
2049ee877a35SJohn Dyson /*
2050dd85920aSJason Evans  * aio_cancel cancels any non-physio aio operations not currently in
2051dd85920aSJason Evans  * progress.
2052ee877a35SJohn Dyson  */
2053ee877a35SJohn Dyson int
20548451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2055fd3bf775SJohn Dyson {
2056b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2057dd85920aSJason Evans 	struct kaioinfo *ki;
2058dd85920aSJason Evans 	struct aiocblist *cbe, *cbn;
2059dd85920aSJason Evans 	struct file *fp;
2060dd85920aSJason Evans 	struct socket *so;
2061f131759fSMateusz Guzik 	cap_rights_t rights;
20621ce91824SDavid Xu 	int error;
20631aa4c324SDavid Xu 	int remove;
2064dd85920aSJason Evans 	int cancelled = 0;
2065dd85920aSJason Evans 	int notcancelled = 0;
2066dd85920aSJason Evans 	struct vnode *vp;
2067dd85920aSJason Evans 
20682a522eb9SJohn Baldwin 	/* Lookup file object. */
2069f131759fSMateusz Guzik 	error = fget(td, uap->fd, cap_rights_init(&rights), &fp);
20702a522eb9SJohn Baldwin 	if (error)
20712a522eb9SJohn Baldwin 		return (error);
2072dd85920aSJason Evans 
20731ce91824SDavid Xu 	ki = p->p_aioinfo;
20741ce91824SDavid Xu 	if (ki == NULL)
20751ce91824SDavid Xu 		goto done;
20761ce91824SDavid Xu 
2077dd85920aSJason Evans 	if (fp->f_type == DTYPE_VNODE) {
20783b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
2079dd85920aSJason Evans 		if (vn_isdisk(vp, &error)) {
20802a522eb9SJohn Baldwin 			fdrop(fp, td);
2081b40ce416SJulian Elischer 			td->td_retval[0] = AIO_NOTCANCELED;
2082ac41f2efSAlfred Perlstein 			return (0);
2083dd85920aSJason Evans 		}
2084dd85920aSJason Evans 	}
2085dd85920aSJason Evans 
2086759ccccaSDavid Xu 	AIO_LOCK(ki);
20872a522eb9SJohn Baldwin 	TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2088dd85920aSJason Evans 		if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2089dd85920aSJason Evans 		    ((uap->aiocbp == NULL) ||
2090dd85920aSJason Evans 		     (uap->aiocbp == cbe->uuaiocb))) {
20911aa4c324SDavid Xu 			remove = 0;
20921aa4c324SDavid Xu 
20931ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
2094dd85920aSJason Evans 			if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2095dd85920aSJason Evans 				TAILQ_REMOVE(&aio_jobs, cbe, list);
20961aa4c324SDavid Xu 				remove = 1;
20971aa4c324SDavid Xu 			} else if (cbe->jobstate == JOBST_JOBQSOCK) {
20981aa4c324SDavid Xu 				MPASS(fp->f_type == DTYPE_SOCKET);
20991aa4c324SDavid Xu 				so = fp->f_data;
21001aa4c324SDavid Xu 				TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
21011aa4c324SDavid Xu 				remove = 1;
210299eee864SDavid Xu 			} else if (cbe->jobstate == JOBST_JOBQSYNC) {
210399eee864SDavid Xu 				TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
210499eee864SDavid Xu 				remove = 1;
21051aa4c324SDavid Xu 			}
21061ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
21071aa4c324SDavid Xu 
21081aa4c324SDavid Xu 			if (remove) {
21091ce91824SDavid Xu 				TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2110dd85920aSJason Evans 				cbe->uaiocb._aiocb_private.status = -1;
2111dd85920aSJason Evans 				cbe->uaiocb._aiocb_private.error = ECANCELED;
21121ce91824SDavid Xu 				aio_bio_done_notify(p, cbe, DONE_QUEUE);
21131ce91824SDavid Xu 				cancelled++;
2114dd85920aSJason Evans 			} else {
2115dd85920aSJason Evans 				notcancelled++;
2116dd85920aSJason Evans 			}
21171aa4c324SDavid Xu 			if (uap->aiocbp != NULL)
21181aa4c324SDavid Xu 				break;
2119dd85920aSJason Evans 		}
2120dd85920aSJason Evans 	}
2121759ccccaSDavid Xu 	AIO_UNLOCK(ki);
21221ce91824SDavid Xu 
2123ad49abc0SAlan Cox done:
21242a522eb9SJohn Baldwin 	fdrop(fp, td);
21251aa4c324SDavid Xu 
21261aa4c324SDavid Xu 	if (uap->aiocbp != NULL) {
2127dd85920aSJason Evans 		if (cancelled) {
2128b40ce416SJulian Elischer 			td->td_retval[0] = AIO_CANCELED;
2129ac41f2efSAlfred Perlstein 			return (0);
2130dd85920aSJason Evans 		}
21311aa4c324SDavid Xu 	}
21321aa4c324SDavid Xu 
21331aa4c324SDavid Xu 	if (notcancelled) {
21341aa4c324SDavid Xu 		td->td_retval[0] = AIO_NOTCANCELED;
21351aa4c324SDavid Xu 		return (0);
21361aa4c324SDavid Xu 	}
21371aa4c324SDavid Xu 
21381aa4c324SDavid Xu 	if (cancelled) {
21391aa4c324SDavid Xu 		td->td_retval[0] = AIO_CANCELED;
21401aa4c324SDavid Xu 		return (0);
21411aa4c324SDavid Xu 	}
21421aa4c324SDavid Xu 
2143b40ce416SJulian Elischer 	td->td_retval[0] = AIO_ALLDONE;
2144dd85920aSJason Evans 
2145ac41f2efSAlfred Perlstein 	return (0);
2146ee877a35SJohn Dyson }
2147ee877a35SJohn Dyson 
2148ee877a35SJohn Dyson /*
2149873fbcd7SRobert Watson  * aio_error is implemented in the kernel level for compatibility purposes
2150873fbcd7SRobert Watson  * only.  For a user mode async implementation, it would be best to do it in
2151873fbcd7SRobert Watson  * a userland subroutine.
2152ee877a35SJohn Dyson  */
21533858a1f4SJohn Baldwin static int
21543858a1f4SJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2155fd3bf775SJohn Dyson {
2156b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
21572244ea07SJohn Dyson 	struct aiocblist *cb;
21582244ea07SJohn Dyson 	struct kaioinfo *ki;
21591ce91824SDavid Xu 	int status;
2160ee877a35SJohn Dyson 
21612244ea07SJohn Dyson 	ki = p->p_aioinfo;
21621ce91824SDavid Xu 	if (ki == NULL) {
21631ce91824SDavid Xu 		td->td_retval[0] = EINVAL;
21641ce91824SDavid Xu 		return (0);
21651ce91824SDavid Xu 	}
2166ee877a35SJohn Dyson 
2167759ccccaSDavid Xu 	AIO_LOCK(ki);
21681ce91824SDavid Xu 	TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
21693858a1f4SJohn Baldwin 		if (cb->uuaiocb == aiocbp) {
21701ce91824SDavid Xu 			if (cb->jobstate == JOBST_JOBFINISHED)
21711ce91824SDavid Xu 				td->td_retval[0] =
21721ce91824SDavid Xu 					cb->uaiocb._aiocb_private.error;
21731ce91824SDavid Xu 			else
2174b40ce416SJulian Elischer 				td->td_retval[0] = EINPROGRESS;
2175759ccccaSDavid Xu 			AIO_UNLOCK(ki);
2176ac41f2efSAlfred Perlstein 			return (0);
21772244ea07SJohn Dyson 		}
21782244ea07SJohn Dyson 	}
2179759ccccaSDavid Xu 	AIO_UNLOCK(ki);
218084af4da6SJohn Dyson 
21812244ea07SJohn Dyson 	/*
2182a9bf5e37SDavid Xu 	 * Hack for failure of aio_aqueue.
21832244ea07SJohn Dyson 	 */
21843858a1f4SJohn Baldwin 	status = ops->fetch_status(aiocbp);
21851ce91824SDavid Xu 	if (status == -1) {
21863858a1f4SJohn Baldwin 		td->td_retval[0] = ops->fetch_error(aiocbp);
21871ce91824SDavid Xu 		return (0);
21881ce91824SDavid Xu 	}
21891ce91824SDavid Xu 
21901ce91824SDavid Xu 	td->td_retval[0] = EINVAL;
21911ce91824SDavid Xu 	return (0);
2192ee877a35SJohn Dyson }
2193ee877a35SJohn Dyson 
21943858a1f4SJohn Baldwin int
21958451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap)
21963858a1f4SJohn Baldwin {
21973858a1f4SJohn Baldwin 
21983858a1f4SJohn Baldwin 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
21993858a1f4SJohn Baldwin }
22003858a1f4SJohn Baldwin 
2201eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */
2202ee877a35SJohn Dyson int
22038451d0ddSKip Macy sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
22040972628aSDavid Xu {
22050972628aSDavid Xu 
22063858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
22073858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
22080972628aSDavid Xu }
22090972628aSDavid Xu 
22100972628aSDavid Xu int
22118451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap)
2212fd3bf775SJohn Dyson {
221321d56e9cSAlfred Perlstein 
22143858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2215ee877a35SJohn Dyson }
2216ee877a35SJohn Dyson 
2217eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */
2218ee877a35SJohn Dyson int
22198451d0ddSKip Macy sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
22200972628aSDavid Xu {
22210972628aSDavid Xu 
22223858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
22233858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
22240972628aSDavid Xu }
22250972628aSDavid Xu 
22260972628aSDavid Xu int
22278451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap)
2228fd3bf775SJohn Dyson {
222921d56e9cSAlfred Perlstein 
22303858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
22310972628aSDavid Xu }
22320972628aSDavid Xu 
22336160e12cSGleb Smirnoff int
22346160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
22356160e12cSGleb Smirnoff {
22366160e12cSGleb Smirnoff 
22376160e12cSGleb Smirnoff 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
22386160e12cSGleb Smirnoff }
22396160e12cSGleb Smirnoff 
22400972628aSDavid Xu static int
22413858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
22423858a1f4SJohn Baldwin     struct aiocb **acb_list, int nent, struct sigevent *sig,
22433858a1f4SJohn Baldwin     struct aiocb_ops *ops)
22440972628aSDavid Xu {
2245b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22463858a1f4SJohn Baldwin 	struct aiocb *iocb;
22472244ea07SJohn Dyson 	struct kaioinfo *ki;
22481ce91824SDavid Xu 	struct aioliojob *lj;
224969cd28daSDoug Ambrisko 	struct kevent kev;
22501ce91824SDavid Xu 	int error;
2251fd3bf775SJohn Dyson 	int nerror;
2252ee877a35SJohn Dyson 	int i;
2253ee877a35SJohn Dyson 
22543858a1f4SJohn Baldwin 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2255ac41f2efSAlfred Perlstein 		return (EINVAL);
22562244ea07SJohn Dyson 
2257ae3b195fSTim J. Robbins 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2258ac41f2efSAlfred Perlstein 		return (EINVAL);
22592244ea07SJohn Dyson 
2260bfbbc4aaSJason Evans 	if (p->p_aioinfo == NULL)
22612244ea07SJohn Dyson 		aio_init_aioinfo(p);
22622244ea07SJohn Dyson 
22632244ea07SJohn Dyson 	ki = p->p_aioinfo;
22642244ea07SJohn Dyson 
2265a163d034SWarner Losh 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
226684af4da6SJohn Dyson 	lj->lioj_flags = 0;
22671ce91824SDavid Xu 	lj->lioj_count = 0;
22681ce91824SDavid Xu 	lj->lioj_finished_count = 0;
2269d8b0556cSKonstantin Belousov 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
22704c0fb2cfSDavid Xu 	ksiginfo_init(&lj->lioj_ksi);
227169cd28daSDoug Ambrisko 
227284af4da6SJohn Dyson 	/*
2273bfbbc4aaSJason Evans 	 * Setup signal.
227484af4da6SJohn Dyson 	 */
22753858a1f4SJohn Baldwin 	if (sig && (mode == LIO_NOWAIT)) {
22763858a1f4SJohn Baldwin 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
227769cd28daSDoug Ambrisko 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
227869cd28daSDoug Ambrisko 			/* Assume only new style KEVENT */
227969cd28daSDoug Ambrisko 			kev.filter = EVFILT_LIO;
228069cd28daSDoug Ambrisko 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
22813858a1f4SJohn Baldwin 			kev.ident = (uintptr_t)uacb_list; /* something unique */
228269cd28daSDoug Ambrisko 			kev.data = (intptr_t)lj;
22831ce91824SDavid Xu 			/* pass user defined sigval data */
22841ce91824SDavid Xu 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
22854db71d27SJohn-Mark Gurney 			error = kqfd_register(
22864db71d27SJohn-Mark Gurney 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
228769cd28daSDoug Ambrisko 			if (error) {
228869cd28daSDoug Ambrisko 				uma_zfree(aiolio_zone, lj);
228969cd28daSDoug Ambrisko 				return (error);
229069cd28daSDoug Ambrisko 			}
22911ce91824SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
22921ce91824SDavid Xu 			;
229368d71118SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
229468d71118SDavid Xu 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
229568d71118SDavid Xu 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
229669cd28daSDoug Ambrisko 					uma_zfree(aiolio_zone, lj);
229769cd28daSDoug Ambrisko 					return EINVAL;
229868d71118SDavid Xu 				}
229984af4da6SJohn Dyson 				lj->lioj_flags |= LIOJ_SIGNAL;
230068d71118SDavid Xu 		} else {
230168d71118SDavid Xu 			uma_zfree(aiolio_zone, lj);
230268d71118SDavid Xu 			return EINVAL;
23034d752b01SAlan Cox 		}
23041ce91824SDavid Xu 	}
230569cd28daSDoug Ambrisko 
2306759ccccaSDavid Xu 	AIO_LOCK(ki);
23072f3cf918SAlfred Perlstein 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
23082244ea07SJohn Dyson 	/*
23091ce91824SDavid Xu 	 * Add extra aiocb count to avoid the lio to be freed
23101ce91824SDavid Xu 	 * by other threads doing aio_waitcomplete or aio_return,
23111ce91824SDavid Xu 	 * and prevent event from being sent until we have queued
23121ce91824SDavid Xu 	 * all tasks.
23131ce91824SDavid Xu 	 */
23141ce91824SDavid Xu 	lj->lioj_count = 1;
2315759ccccaSDavid Xu 	AIO_UNLOCK(ki);
23161ce91824SDavid Xu 
23171ce91824SDavid Xu 	/*
2318bfbbc4aaSJason Evans 	 * Get pointers to the list of I/O requests.
23192244ea07SJohn Dyson 	 */
2320fd3bf775SJohn Dyson 	nerror = 0;
23213858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++) {
23223858a1f4SJohn Baldwin 		iocb = acb_list[i];
23233858a1f4SJohn Baldwin 		if (iocb != NULL) {
23243858a1f4SJohn Baldwin 			error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
23251ce91824SDavid Xu 			if (error != 0)
2326fd3bf775SJohn Dyson 				nerror++;
2327fd3bf775SJohn Dyson 		}
2328fd3bf775SJohn Dyson 	}
23292244ea07SJohn Dyson 
23301ce91824SDavid Xu 	error = 0;
2331759ccccaSDavid Xu 	AIO_LOCK(ki);
23323858a1f4SJohn Baldwin 	if (mode == LIO_WAIT) {
23331ce91824SDavid Xu 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2334fd3bf775SJohn Dyson 			ki->kaio_flags |= KAIO_WAKEUP;
2335759ccccaSDavid Xu 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
23361ce91824SDavid Xu 			    PRIBIO | PCATCH, "aiospn", 0);
23371ce91824SDavid Xu 			if (error == ERESTART)
23381ce91824SDavid Xu 				error = EINTR;
23391ce91824SDavid Xu 			if (error)
23401ce91824SDavid Xu 				break;
23411ce91824SDavid Xu 		}
23421ce91824SDavid Xu 	} else {
23431ce91824SDavid Xu 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
23441ce91824SDavid Xu 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
23451ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
23461ce91824SDavid Xu 				KNOTE_LOCKED(&lj->klist, 1);
23471ce91824SDavid Xu 			}
23481ce91824SDavid Xu 			if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
23491ce91824SDavid Xu 			    == LIOJ_SIGNAL
23501ce91824SDavid Xu 			    && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
23511ce91824SDavid Xu 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
23521ce91824SDavid Xu 				aio_sendsig(p, &lj->lioj_signal,
23531ce91824SDavid Xu 					    &lj->lioj_ksi);
23541ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
23552244ea07SJohn Dyson 			}
23562244ea07SJohn Dyson 		}
23571ce91824SDavid Xu 	}
23581ce91824SDavid Xu 	lj->lioj_count--;
23591ce91824SDavid Xu 	if (lj->lioj_count == 0) {
23601ce91824SDavid Xu 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
23611ce91824SDavid Xu 		knlist_delete(&lj->klist, curthread, 1);
2362759ccccaSDavid Xu 		PROC_LOCK(p);
23631ce91824SDavid Xu 		sigqueue_take(&lj->lioj_ksi);
23641ce91824SDavid Xu 		PROC_UNLOCK(p);
2365759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23661ce91824SDavid Xu 		uma_zfree(aiolio_zone, lj);
23671ce91824SDavid Xu 	} else
2368759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23692244ea07SJohn Dyson 
23701ce91824SDavid Xu 	if (nerror)
23711ce91824SDavid Xu 		return (EIO);
23721ce91824SDavid Xu 	return (error);
2373ee877a35SJohn Dyson }
2374fd3bf775SJohn Dyson 
23753858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
23763858a1f4SJohn Baldwin int
23778451d0ddSKip Macy sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
23783858a1f4SJohn Baldwin {
23793858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23803858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23813858a1f4SJohn Baldwin 	struct osigevent osig;
23823858a1f4SJohn Baldwin 	int error, nent;
23833858a1f4SJohn Baldwin 
23843858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23853858a1f4SJohn Baldwin 		return (EINVAL);
23863858a1f4SJohn Baldwin 
23873858a1f4SJohn Baldwin 	nent = uap->nent;
23883858a1f4SJohn Baldwin 	if (nent < 0 || nent > AIO_LISTIO_MAX)
23893858a1f4SJohn Baldwin 		return (EINVAL);
23903858a1f4SJohn Baldwin 
23913858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23923858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
23933858a1f4SJohn Baldwin 		if (error)
23943858a1f4SJohn Baldwin 			return (error);
23953858a1f4SJohn Baldwin 		error = convert_old_sigevent(&osig, &sig);
23963858a1f4SJohn Baldwin 		if (error)
23973858a1f4SJohn Baldwin 			return (error);
23983858a1f4SJohn Baldwin 		sigp = &sig;
23993858a1f4SJohn Baldwin 	} else
24003858a1f4SJohn Baldwin 		sigp = NULL;
24013858a1f4SJohn Baldwin 
24023858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24033858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24043858a1f4SJohn Baldwin 	if (error == 0)
24053858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode,
24063858a1f4SJohn Baldwin 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
24073858a1f4SJohn Baldwin 		    &aiocb_ops_osigevent);
24083858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24093858a1f4SJohn Baldwin 	return (error);
24103858a1f4SJohn Baldwin }
24113858a1f4SJohn Baldwin 
24123858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
24133858a1f4SJohn Baldwin int
24148451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
24153858a1f4SJohn Baldwin {
24163858a1f4SJohn Baldwin 	struct aiocb **acb_list;
24173858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
24183858a1f4SJohn Baldwin 	int error, nent;
24193858a1f4SJohn Baldwin 
24203858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
24213858a1f4SJohn Baldwin 		return (EINVAL);
24223858a1f4SJohn Baldwin 
24233858a1f4SJohn Baldwin 	nent = uap->nent;
24243858a1f4SJohn Baldwin 	if (nent < 0 || nent > AIO_LISTIO_MAX)
24253858a1f4SJohn Baldwin 		return (EINVAL);
24263858a1f4SJohn Baldwin 
24273858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
24283858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig, sizeof(sig));
24293858a1f4SJohn Baldwin 		if (error)
24303858a1f4SJohn Baldwin 			return (error);
24313858a1f4SJohn Baldwin 		sigp = &sig;
24323858a1f4SJohn Baldwin 	} else
24333858a1f4SJohn Baldwin 		sigp = NULL;
24343858a1f4SJohn Baldwin 
24353858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24363858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24373858a1f4SJohn Baldwin 	if (error == 0)
24383858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
24393858a1f4SJohn Baldwin 		    nent, sigp, &aiocb_ops);
24403858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24413858a1f4SJohn Baldwin 	return (error);
24423858a1f4SJohn Baldwin }
24433858a1f4SJohn Baldwin 
2444fd3bf775SJohn Dyson static void
2445f743d981SAlexander Motin aio_physwakeup(struct bio *bp)
2446fd3bf775SJohn Dyson {
2447f743d981SAlexander Motin 	struct aiocblist *aiocbe = (struct aiocblist *)bp->bio_caller1;
24481ce91824SDavid Xu 	struct proc *userp;
244927b8220dSDavid Xu 	struct kaioinfo *ki;
24501ce91824SDavid Xu 	int nblks;
24511ce91824SDavid Xu 
2452f743d981SAlexander Motin 	/* Release mapping into kernel space. */
2453f743d981SAlexander Motin 	if (aiocbe->pbuf) {
2454f743d981SAlexander Motin 		pmap_qremove((vm_offset_t)aiocbe->pbuf->b_data, aiocbe->npages);
2455f743d981SAlexander Motin 		relpbuf(aiocbe->pbuf, NULL);
2456f743d981SAlexander Motin 		aiocbe->pbuf = NULL;
2457f743d981SAlexander Motin 		atomic_subtract_int(&num_buf_aio, 1);
2458f743d981SAlexander Motin 	}
2459f743d981SAlexander Motin 	vm_page_unhold_pages(aiocbe->pages, aiocbe->npages);
2460f743d981SAlexander Motin 
24611ce91824SDavid Xu 	bp = aiocbe->bp;
2462f743d981SAlexander Motin 	aiocbe->bp = NULL;
24631ce91824SDavid Xu 	userp = aiocbe->userproc;
246427b8220dSDavid Xu 	ki = userp->p_aioinfo;
2465759ccccaSDavid Xu 	AIO_LOCK(ki);
2466f743d981SAlexander Motin 	aiocbe->uaiocb._aiocb_private.status -= bp->bio_resid;
246784af4da6SJohn Dyson 	aiocbe->uaiocb._aiocb_private.error = 0;
2468f743d981SAlexander Motin 	if (bp->bio_flags & BIO_ERROR)
2469f743d981SAlexander Motin 		aiocbe->uaiocb._aiocb_private.error = bp->bio_error;
24701ce91824SDavid Xu 	nblks = btodb(aiocbe->uaiocb.aio_nbytes);
24711ce91824SDavid Xu 	if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
24721ce91824SDavid Xu 		aiocbe->outputcharge += nblks;
24731ce91824SDavid Xu 	else
24741ce91824SDavid Xu 		aiocbe->inputcharge += nblks;
24751ce91824SDavid Xu 	TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
247627b8220dSDavid Xu 	ki->kaio_buffer_count--;
247769cd28daSDoug Ambrisko 	aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2478759ccccaSDavid Xu 	AIO_UNLOCK(ki);
24791ce91824SDavid Xu 
2480f743d981SAlexander Motin 	g_destroy_bio(bp);
248184af4da6SJohn Dyson }
2482bfbbc4aaSJason Evans 
2483eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */
24843858a1f4SJohn Baldwin static int
24853858a1f4SJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
24863858a1f4SJohn Baldwin     struct timespec *ts, struct aiocb_ops *ops)
2487bfbbc4aaSJason Evans {
2488b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2489bfbbc4aaSJason Evans 	struct timeval atv;
2490bfbbc4aaSJason Evans 	struct kaioinfo *ki;
24911ce91824SDavid Xu 	struct aiocblist *cb;
24921ce91824SDavid Xu 	struct aiocb *uuaiocb;
24931ce91824SDavid Xu 	int error, status, timo;
2494bfbbc4aaSJason Evans 
24953858a1f4SJohn Baldwin 	ops->store_aiocb(aiocbp, NULL);
2496dd85920aSJason Evans 
2497*38d68e2dSPawel Jakub Dawidek 	if (ts == NULL) {
2498bfbbc4aaSJason Evans 		timo = 0;
2499*38d68e2dSPawel Jakub Dawidek 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2500*38d68e2dSPawel Jakub Dawidek 		timo = -1;
2501*38d68e2dSPawel Jakub Dawidek 	} else {
25023858a1f4SJohn Baldwin 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2503bfbbc4aaSJason Evans 			return (EINVAL);
2504bfbbc4aaSJason Evans 
25053858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2506bfbbc4aaSJason Evans 		if (itimerfix(&atv))
2507bfbbc4aaSJason Evans 			return (EINVAL);
2508bfbbc4aaSJason Evans 		timo = tvtohz(&atv);
2509bfbbc4aaSJason Evans 	}
2510bfbbc4aaSJason Evans 
25118213baf0SChristian S.J. Peron 	if (p->p_aioinfo == NULL)
2512323fe565SDavid Xu 		aio_init_aioinfo(p);
25138213baf0SChristian S.J. Peron 	ki = p->p_aioinfo;
2514bfbbc4aaSJason Evans 
25151ce91824SDavid Xu 	error = 0;
25161ce91824SDavid Xu 	cb = NULL;
2517759ccccaSDavid Xu 	AIO_LOCK(ki);
25181ce91824SDavid Xu 	while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2519*38d68e2dSPawel Jakub Dawidek 		if (timo == -1) {
2520*38d68e2dSPawel Jakub Dawidek 			error = EWOULDBLOCK;
2521*38d68e2dSPawel Jakub Dawidek 			break;
2522*38d68e2dSPawel Jakub Dawidek 		}
25231ce91824SDavid Xu 		ki->kaio_flags |= KAIO_WAKEUP;
2524759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
25251ce91824SDavid Xu 		    "aiowc", timo);
252627b8220dSDavid Xu 		if (timo && error == ERESTART)
25271ce91824SDavid Xu 			error = EINTR;
25281ce91824SDavid Xu 		if (error)
25291ce91824SDavid Xu 			break;
25301ce91824SDavid Xu 	}
25311ce91824SDavid Xu 
25321ce91824SDavid Xu 	if (cb != NULL) {
25331ce91824SDavid Xu 		MPASS(cb->jobstate == JOBST_JOBFINISHED);
25341ce91824SDavid Xu 		uuaiocb = cb->uuaiocb;
25351ce91824SDavid Xu 		status = cb->uaiocb._aiocb_private.status;
25361ce91824SDavid Xu 		error = cb->uaiocb._aiocb_private.error;
25371ce91824SDavid Xu 		td->td_retval[0] = status;
2538bfbbc4aaSJason Evans 		if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
25391c4bcd05SJeff Roberson 			td->td_ru.ru_oublock += cb->outputcharge;
2540bfbbc4aaSJason Evans 			cb->outputcharge = 0;
2541bfbbc4aaSJason Evans 		} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
25421c4bcd05SJeff Roberson 			td->td_ru.ru_inblock += cb->inputcharge;
2543bfbbc4aaSJason Evans 			cb->inputcharge = 0;
2544bfbbc4aaSJason Evans 		}
2545bfbbc4aaSJason Evans 		aio_free_entry(cb);
2546759ccccaSDavid Xu 		AIO_UNLOCK(ki);
25473858a1f4SJohn Baldwin 		ops->store_aiocb(aiocbp, uuaiocb);
25483858a1f4SJohn Baldwin 		ops->store_error(uuaiocb, error);
25493858a1f4SJohn Baldwin 		ops->store_status(uuaiocb, status);
25501ce91824SDavid Xu 	} else
2551759ccccaSDavid Xu 		AIO_UNLOCK(ki);
2552bfbbc4aaSJason Evans 
2553ac41f2efSAlfred Perlstein 	return (error);
2554bfbbc4aaSJason Evans }
2555cb679c38SJonathan Lemon 
255699eee864SDavid Xu int
25578451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
25583858a1f4SJohn Baldwin {
25593858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
25603858a1f4SJohn Baldwin 	int error;
25613858a1f4SJohn Baldwin 
25623858a1f4SJohn Baldwin 	if (uap->timeout) {
25633858a1f4SJohn Baldwin 		/* Get timespec struct. */
25643858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts, sizeof(ts));
25653858a1f4SJohn Baldwin 		if (error)
25663858a1f4SJohn Baldwin 			return (error);
25673858a1f4SJohn Baldwin 		tsp = &ts;
25683858a1f4SJohn Baldwin 	} else
25693858a1f4SJohn Baldwin 		tsp = NULL;
25703858a1f4SJohn Baldwin 
25713858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
25723858a1f4SJohn Baldwin }
25733858a1f4SJohn Baldwin 
25743858a1f4SJohn Baldwin static int
25753858a1f4SJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
25763858a1f4SJohn Baldwin     struct aiocb_ops *ops)
257799eee864SDavid Xu {
257899eee864SDavid Xu 	struct proc *p = td->td_proc;
257999eee864SDavid Xu 	struct kaioinfo *ki;
258099eee864SDavid Xu 
25813858a1f4SJohn Baldwin 	if (op != O_SYNC) /* XXX lack of O_DSYNC */
258299eee864SDavid Xu 		return (EINVAL);
258399eee864SDavid Xu 	ki = p->p_aioinfo;
258499eee864SDavid Xu 	if (ki == NULL)
258599eee864SDavid Xu 		aio_init_aioinfo(p);
25863858a1f4SJohn Baldwin 	return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
25873858a1f4SJohn Baldwin }
25883858a1f4SJohn Baldwin 
25893858a1f4SJohn Baldwin int
25908451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
25913858a1f4SJohn Baldwin {
25923858a1f4SJohn Baldwin 
25933858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
259499eee864SDavid Xu }
259599eee864SDavid Xu 
2596eb8e6d52SEivind Eklund /* kqueue attach function */
2597cb679c38SJonathan Lemon static int
2598cb679c38SJonathan Lemon filt_aioattach(struct knote *kn)
2599cb679c38SJonathan Lemon {
2600b46f1c55SAlan Cox 	struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2601cb679c38SJonathan Lemon 
2602cb679c38SJonathan Lemon 	/*
2603cb679c38SJonathan Lemon 	 * The aiocbe pointer must be validated before using it, so
2604cb679c38SJonathan Lemon 	 * registration is restricted to the kernel; the user cannot
2605cb679c38SJonathan Lemon 	 * set EV_FLAG1.
2606cb679c38SJonathan Lemon 	 */
2607cb679c38SJonathan Lemon 	if ((kn->kn_flags & EV_FLAG1) == 0)
2608cb679c38SJonathan Lemon 		return (EPERM);
2609a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_aio = aiocbe;
2610cb679c38SJonathan Lemon 	kn->kn_flags &= ~EV_FLAG1;
2611cb679c38SJonathan Lemon 
2612ad3b9257SJohn-Mark Gurney 	knlist_add(&aiocbe->klist, kn, 0);
2613cb679c38SJonathan Lemon 
2614cb679c38SJonathan Lemon 	return (0);
2615cb679c38SJonathan Lemon }
2616cb679c38SJonathan Lemon 
2617eb8e6d52SEivind Eklund /* kqueue detach function */
2618cb679c38SJonathan Lemon static void
2619cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn)
2620cb679c38SJonathan Lemon {
26218e9fc278SDoug Ambrisko 	struct knlist *knl;
2622cb679c38SJonathan Lemon 
26238e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_aio->klist;
26248e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26258e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26268e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26278e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
2628cb679c38SJonathan Lemon }
2629cb679c38SJonathan Lemon 
2630eb8e6d52SEivind Eklund /* kqueue filter function */
2631cb679c38SJonathan Lemon /*ARGSUSED*/
2632cb679c38SJonathan Lemon static int
2633cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint)
2634cb679c38SJonathan Lemon {
2635a8afa221SJean-Sébastien Pédron 	struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2636cb679c38SJonathan Lemon 
263791369fc7SAlan Cox 	kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
26381ce91824SDavid Xu 	if (aiocbe->jobstate != JOBST_JOBFINISHED)
2639cb679c38SJonathan Lemon 		return (0);
2640cb679c38SJonathan Lemon 	kn->kn_flags |= EV_EOF;
2641cb679c38SJonathan Lemon 	return (1);
2642cb679c38SJonathan Lemon }
264369cd28daSDoug Ambrisko 
264469cd28daSDoug Ambrisko /* kqueue attach function */
264569cd28daSDoug Ambrisko static int
264669cd28daSDoug Ambrisko filt_lioattach(struct knote *kn)
264769cd28daSDoug Ambrisko {
26481ce91824SDavid Xu 	struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
264969cd28daSDoug Ambrisko 
265069cd28daSDoug Ambrisko 	/*
26511ce91824SDavid Xu 	 * The aioliojob pointer must be validated before using it, so
265269cd28daSDoug Ambrisko 	 * registration is restricted to the kernel; the user cannot
265369cd28daSDoug Ambrisko 	 * set EV_FLAG1.
265469cd28daSDoug Ambrisko 	 */
265569cd28daSDoug Ambrisko 	if ((kn->kn_flags & EV_FLAG1) == 0)
265669cd28daSDoug Ambrisko 		return (EPERM);
2657a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_lio = lj;
265869cd28daSDoug Ambrisko 	kn->kn_flags &= ~EV_FLAG1;
265969cd28daSDoug Ambrisko 
266069cd28daSDoug Ambrisko 	knlist_add(&lj->klist, kn, 0);
266169cd28daSDoug Ambrisko 
266269cd28daSDoug Ambrisko 	return (0);
266369cd28daSDoug Ambrisko }
266469cd28daSDoug Ambrisko 
266569cd28daSDoug Ambrisko /* kqueue detach function */
266669cd28daSDoug Ambrisko static void
266769cd28daSDoug Ambrisko filt_liodetach(struct knote *kn)
266869cd28daSDoug Ambrisko {
26698e9fc278SDoug Ambrisko 	struct knlist *knl;
267069cd28daSDoug Ambrisko 
26718e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_lio->klist;
26728e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26738e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26748e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26758e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
267669cd28daSDoug Ambrisko }
267769cd28daSDoug Ambrisko 
267869cd28daSDoug Ambrisko /* kqueue filter function */
267969cd28daSDoug Ambrisko /*ARGSUSED*/
268069cd28daSDoug Ambrisko static int
268169cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint)
268269cd28daSDoug Ambrisko {
2683a8afa221SJean-Sébastien Pédron 	struct aioliojob * lj = kn->kn_ptr.p_lio;
26841ce91824SDavid Xu 
268569cd28daSDoug Ambrisko 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
268669cd28daSDoug Ambrisko }
26873858a1f4SJohn Baldwin 
2688841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
26893858a1f4SJohn Baldwin 
26903858a1f4SJohn Baldwin struct __aiocb_private32 {
26913858a1f4SJohn Baldwin 	int32_t	status;
26923858a1f4SJohn Baldwin 	int32_t	error;
26933858a1f4SJohn Baldwin 	uint32_t kernelinfo;
26943858a1f4SJohn Baldwin };
26953858a1f4SJohn Baldwin 
26963858a1f4SJohn Baldwin typedef struct oaiocb32 {
26973858a1f4SJohn Baldwin 	int	aio_fildes;		/* File descriptor */
26983858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
26993858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27003858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27013858a1f4SJohn Baldwin 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
27023858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27033858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27043858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27053858a1f4SJohn Baldwin } oaiocb32_t;
27063858a1f4SJohn Baldwin 
27073858a1f4SJohn Baldwin typedef struct aiocb32 {
27083858a1f4SJohn Baldwin 	int32_t	aio_fildes;		/* File descriptor */
27093858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27103858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27113858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27123858a1f4SJohn Baldwin 	int	__spare__[2];
27133858a1f4SJohn Baldwin 	uint32_t __spare2__;
27143858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27153858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27163858a1f4SJohn Baldwin 	struct __aiocb_private32 _aiocb_private;
27173858a1f4SJohn Baldwin 	struct sigevent32 aio_sigevent;	/* Signal to deliver */
27183858a1f4SJohn Baldwin } aiocb32_t;
27193858a1f4SJohn Baldwin 
27203858a1f4SJohn Baldwin static int
27213858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
27223858a1f4SJohn Baldwin {
27233858a1f4SJohn Baldwin 
27243858a1f4SJohn Baldwin 	/*
27253858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
27263858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
27273858a1f4SJohn Baldwin 	 */
27283858a1f4SJohn Baldwin 	CP(*osig, *nsig, sigev_notify);
27293858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
27303858a1f4SJohn Baldwin 	case SIGEV_NONE:
27313858a1f4SJohn Baldwin 		break;
27323858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
27333858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
27343858a1f4SJohn Baldwin 		break;
27353858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
27363858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
27373858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
27383858a1f4SJohn Baldwin 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
27393858a1f4SJohn Baldwin 		break;
27403858a1f4SJohn Baldwin 	default:
27413858a1f4SJohn Baldwin 		return (EINVAL);
27423858a1f4SJohn Baldwin 	}
27433858a1f4SJohn Baldwin 	return (0);
27443858a1f4SJohn Baldwin }
27453858a1f4SJohn Baldwin 
27463858a1f4SJohn Baldwin static int
27473858a1f4SJohn Baldwin aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
27483858a1f4SJohn Baldwin {
27493858a1f4SJohn Baldwin 	struct oaiocb32 job32;
27503858a1f4SJohn Baldwin 	int error;
27513858a1f4SJohn Baldwin 
27523858a1f4SJohn Baldwin 	bzero(kjob, sizeof(struct aiocb));
27533858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
27543858a1f4SJohn Baldwin 	if (error)
27553858a1f4SJohn Baldwin 		return (error);
27563858a1f4SJohn Baldwin 
27573858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_fildes);
27583858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_offset);
27593858a1f4SJohn Baldwin 	PTRIN_CP(job32, *kjob, aio_buf);
27603858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_nbytes);
27613858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_lio_opcode);
27623858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_reqprio);
27633858a1f4SJohn Baldwin 	CP(job32, *kjob, _aiocb_private.status);
27643858a1f4SJohn Baldwin 	CP(job32, *kjob, _aiocb_private.error);
27653858a1f4SJohn Baldwin 	PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
27663858a1f4SJohn Baldwin 	return (convert_old_sigevent32(&job32.aio_sigevent,
27673858a1f4SJohn Baldwin 	    &kjob->aio_sigevent));
27683858a1f4SJohn Baldwin }
27693858a1f4SJohn Baldwin 
27703858a1f4SJohn Baldwin static int
27713858a1f4SJohn Baldwin aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
27723858a1f4SJohn Baldwin {
27733858a1f4SJohn Baldwin 	struct aiocb32 job32;
27743858a1f4SJohn Baldwin 	int error;
27753858a1f4SJohn Baldwin 
27763858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
27773858a1f4SJohn Baldwin 	if (error)
27783858a1f4SJohn Baldwin 		return (error);
27793858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_fildes);
27803858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_offset);
27813858a1f4SJohn Baldwin 	PTRIN_CP(job32, *kjob, aio_buf);
27823858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_nbytes);
27833858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_lio_opcode);
27843858a1f4SJohn Baldwin 	CP(job32, *kjob, aio_reqprio);
27853858a1f4SJohn Baldwin 	CP(job32, *kjob, _aiocb_private.status);
27863858a1f4SJohn Baldwin 	CP(job32, *kjob, _aiocb_private.error);
27873858a1f4SJohn Baldwin 	PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
27883858a1f4SJohn Baldwin 	return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
27893858a1f4SJohn Baldwin }
27903858a1f4SJohn Baldwin 
27913858a1f4SJohn Baldwin static long
27923858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob)
27933858a1f4SJohn Baldwin {
27943858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
27953858a1f4SJohn Baldwin 
27963858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
27973858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.status));
27983858a1f4SJohn Baldwin }
27993858a1f4SJohn Baldwin 
28003858a1f4SJohn Baldwin static long
28013858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob)
28023858a1f4SJohn Baldwin {
28033858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28043858a1f4SJohn Baldwin 
28053858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28063858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.error));
28073858a1f4SJohn Baldwin }
28083858a1f4SJohn Baldwin 
28093858a1f4SJohn Baldwin static int
28103858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status)
28113858a1f4SJohn Baldwin {
28123858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28133858a1f4SJohn Baldwin 
28143858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28153858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.status, status));
28163858a1f4SJohn Baldwin }
28173858a1f4SJohn Baldwin 
28183858a1f4SJohn Baldwin static int
28193858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error)
28203858a1f4SJohn Baldwin {
28213858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28223858a1f4SJohn Baldwin 
28233858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28243858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.error, error));
28253858a1f4SJohn Baldwin }
28263858a1f4SJohn Baldwin 
28273858a1f4SJohn Baldwin static int
28283858a1f4SJohn Baldwin aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
28293858a1f4SJohn Baldwin {
28303858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28313858a1f4SJohn Baldwin 
28323858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28333858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
28343858a1f4SJohn Baldwin }
28353858a1f4SJohn Baldwin 
28363858a1f4SJohn Baldwin static int
28373858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
28383858a1f4SJohn Baldwin {
28393858a1f4SJohn Baldwin 
28403858a1f4SJohn Baldwin 	return (suword32(ujobp, (long)ujob));
28413858a1f4SJohn Baldwin }
28423858a1f4SJohn Baldwin 
28433858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = {
28443858a1f4SJohn Baldwin 	.copyin = aiocb32_copyin,
28453858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
28463858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
28473858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
28483858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
28493858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
28503858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
28513858a1f4SJohn Baldwin };
28523858a1f4SJohn Baldwin 
28533858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = {
28543858a1f4SJohn Baldwin 	.copyin = aiocb32_copyin_old_sigevent,
28553858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
28563858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
28573858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
28583858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
28593858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
28603858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
28613858a1f4SJohn Baldwin };
28623858a1f4SJohn Baldwin 
28633858a1f4SJohn Baldwin int
28643858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
28653858a1f4SJohn Baldwin {
28663858a1f4SJohn Baldwin 
28673858a1f4SJohn Baldwin 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
28683858a1f4SJohn Baldwin }
28693858a1f4SJohn Baldwin 
28703858a1f4SJohn Baldwin int
28713858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
28723858a1f4SJohn Baldwin {
28733858a1f4SJohn Baldwin 	struct timespec32 ts32;
28743858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
28753858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
28763858a1f4SJohn Baldwin 	uint32_t *ujoblist32;
28773858a1f4SJohn Baldwin 	int error, i;
28783858a1f4SJohn Baldwin 
28793858a1f4SJohn Baldwin 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
28803858a1f4SJohn Baldwin 		return (EINVAL);
28813858a1f4SJohn Baldwin 
28823858a1f4SJohn Baldwin 	if (uap->timeout) {
28833858a1f4SJohn Baldwin 		/* Get timespec struct. */
28843858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
28853858a1f4SJohn Baldwin 			return (error);
28863858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
28873858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
28883858a1f4SJohn Baldwin 		tsp = &ts;
28893858a1f4SJohn Baldwin 	} else
28903858a1f4SJohn Baldwin 		tsp = NULL;
28913858a1f4SJohn Baldwin 
28923858a1f4SJohn Baldwin 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
28933858a1f4SJohn Baldwin 	ujoblist32 = (uint32_t *)ujoblist;
28943858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
28953858a1f4SJohn Baldwin 	    sizeof(ujoblist32[0]));
28963858a1f4SJohn Baldwin 	if (error == 0) {
28973858a1f4SJohn Baldwin 		for (i = uap->nent; i > 0; i--)
28983858a1f4SJohn Baldwin 			ujoblist[i] = PTRIN(ujoblist32[i]);
28993858a1f4SJohn Baldwin 
29003858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
29013858a1f4SJohn Baldwin 	}
29023858a1f4SJohn Baldwin 	uma_zfree(aiol_zone, ujoblist);
29033858a1f4SJohn Baldwin 	return (error);
29043858a1f4SJohn Baldwin }
29053858a1f4SJohn Baldwin 
29063858a1f4SJohn Baldwin int
29073858a1f4SJohn Baldwin freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
29083858a1f4SJohn Baldwin {
29093858a1f4SJohn Baldwin 
29108451d0ddSKip Macy 	return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
29113858a1f4SJohn Baldwin }
29123858a1f4SJohn Baldwin 
29133858a1f4SJohn Baldwin int
29143858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
29153858a1f4SJohn Baldwin {
29163858a1f4SJohn Baldwin 
29173858a1f4SJohn Baldwin 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29183858a1f4SJohn Baldwin }
29193858a1f4SJohn Baldwin 
29203858a1f4SJohn Baldwin int
29213858a1f4SJohn Baldwin freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
29223858a1f4SJohn Baldwin {
29233858a1f4SJohn Baldwin 
29243858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29253858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29263858a1f4SJohn Baldwin }
29273858a1f4SJohn Baldwin 
29283858a1f4SJohn Baldwin int
29293858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
29303858a1f4SJohn Baldwin {
29313858a1f4SJohn Baldwin 
29323858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29333858a1f4SJohn Baldwin 	    &aiocb32_ops));
29343858a1f4SJohn Baldwin }
29353858a1f4SJohn Baldwin 
29363858a1f4SJohn Baldwin int
29373858a1f4SJohn Baldwin freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
29383858a1f4SJohn Baldwin {
29393858a1f4SJohn Baldwin 
29403858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
29413858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29423858a1f4SJohn Baldwin }
29433858a1f4SJohn Baldwin 
29443858a1f4SJohn Baldwin int
29453858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
29463858a1f4SJohn Baldwin {
29473858a1f4SJohn Baldwin 
29483858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
29493858a1f4SJohn Baldwin 	    &aiocb32_ops));
29503858a1f4SJohn Baldwin }
29513858a1f4SJohn Baldwin 
29523858a1f4SJohn Baldwin int
29536160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
29546160e12cSGleb Smirnoff {
29556160e12cSGleb Smirnoff 
29566160e12cSGleb Smirnoff 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
29576160e12cSGleb Smirnoff 	    &aiocb32_ops));
29586160e12cSGleb Smirnoff }
29596160e12cSGleb Smirnoff 
29606160e12cSGleb Smirnoff int
29613858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td,
29623858a1f4SJohn Baldwin     struct freebsd32_aio_waitcomplete_args *uap)
29633858a1f4SJohn Baldwin {
2964e588eeb1SJohn Baldwin 	struct timespec32 ts32;
29653858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
29663858a1f4SJohn Baldwin 	int error;
29673858a1f4SJohn Baldwin 
29683858a1f4SJohn Baldwin 	if (uap->timeout) {
29693858a1f4SJohn Baldwin 		/* Get timespec struct. */
29703858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
29713858a1f4SJohn Baldwin 		if (error)
29723858a1f4SJohn Baldwin 			return (error);
29733858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
29743858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
29753858a1f4SJohn Baldwin 		tsp = &ts;
29763858a1f4SJohn Baldwin 	} else
29773858a1f4SJohn Baldwin 		tsp = NULL;
29783858a1f4SJohn Baldwin 
29793858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
29803858a1f4SJohn Baldwin 	    &aiocb32_ops));
29813858a1f4SJohn Baldwin }
29823858a1f4SJohn Baldwin 
29833858a1f4SJohn Baldwin int
29843858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
29853858a1f4SJohn Baldwin {
29863858a1f4SJohn Baldwin 
29873858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
29883858a1f4SJohn Baldwin 	    &aiocb32_ops));
29893858a1f4SJohn Baldwin }
29903858a1f4SJohn Baldwin 
29913858a1f4SJohn Baldwin int
29923858a1f4SJohn Baldwin freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
29933858a1f4SJohn Baldwin {
29943858a1f4SJohn Baldwin 	struct aiocb **acb_list;
29953858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
29963858a1f4SJohn Baldwin 	struct osigevent32 osig;
29973858a1f4SJohn Baldwin 	uint32_t *acb_list32;
29983858a1f4SJohn Baldwin 	int error, i, nent;
29993858a1f4SJohn Baldwin 
30003858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30013858a1f4SJohn Baldwin 		return (EINVAL);
30023858a1f4SJohn Baldwin 
30033858a1f4SJohn Baldwin 	nent = uap->nent;
30043858a1f4SJohn Baldwin 	if (nent < 0 || nent > AIO_LISTIO_MAX)
30053858a1f4SJohn Baldwin 		return (EINVAL);
30063858a1f4SJohn Baldwin 
30073858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30083858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
30093858a1f4SJohn Baldwin 		if (error)
30103858a1f4SJohn Baldwin 			return (error);
30113858a1f4SJohn Baldwin 		error = convert_old_sigevent32(&osig, &sig);
30123858a1f4SJohn Baldwin 		if (error)
30133858a1f4SJohn Baldwin 			return (error);
30143858a1f4SJohn Baldwin 		sigp = &sig;
30153858a1f4SJohn Baldwin 	} else
30163858a1f4SJohn Baldwin 		sigp = NULL;
30173858a1f4SJohn Baldwin 
30183858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
30193858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
30203858a1f4SJohn Baldwin 	if (error) {
30213858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
30223858a1f4SJohn Baldwin 		return (error);
30233858a1f4SJohn Baldwin 	}
30243858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
30253858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
30263858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
30273858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
30283858a1f4SJohn Baldwin 
30293858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
30303858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
30313858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent);
30323858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
30333858a1f4SJohn Baldwin 	return (error);
30343858a1f4SJohn Baldwin }
30353858a1f4SJohn Baldwin 
30363858a1f4SJohn Baldwin int
30373858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
30383858a1f4SJohn Baldwin {
30393858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30403858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30413858a1f4SJohn Baldwin 	struct sigevent32 sig32;
30423858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30433858a1f4SJohn Baldwin 	int error, i, nent;
30443858a1f4SJohn Baldwin 
30453858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30463858a1f4SJohn Baldwin 		return (EINVAL);
30473858a1f4SJohn Baldwin 
30483858a1f4SJohn Baldwin 	nent = uap->nent;
30493858a1f4SJohn Baldwin 	if (nent < 0 || nent > AIO_LISTIO_MAX)
30503858a1f4SJohn Baldwin 		return (EINVAL);
30513858a1f4SJohn Baldwin 
30523858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30533858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig32, sizeof(sig32));
30543858a1f4SJohn Baldwin 		if (error)
30553858a1f4SJohn Baldwin 			return (error);
30563858a1f4SJohn Baldwin 		error = convert_sigevent32(&sig32, &sig);
30573858a1f4SJohn Baldwin 		if (error)
30583858a1f4SJohn Baldwin 			return (error);
30593858a1f4SJohn Baldwin 		sigp = &sig;
30603858a1f4SJohn Baldwin 	} else
30613858a1f4SJohn Baldwin 		sigp = NULL;
30623858a1f4SJohn Baldwin 
30633858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
30643858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
30653858a1f4SJohn Baldwin 	if (error) {
30663858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
30673858a1f4SJohn Baldwin 		return (error);
30683858a1f4SJohn Baldwin 	}
30693858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
30703858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
30713858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
30723858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
30733858a1f4SJohn Baldwin 
30743858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
30753858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
30763858a1f4SJohn Baldwin 	    &aiocb32_ops);
30773858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
30783858a1f4SJohn Baldwin 	return (error);
30793858a1f4SJohn Baldwin }
30803858a1f4SJohn Baldwin 
30813858a1f4SJohn Baldwin #endif
3082