xref: /freebsd/sys/kern/vfs_aio.c (revision cca6d6160f14a6a8dcb1e3caa0759ddbd0f68555)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
4ee877a35SJohn Dyson  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
5ee877a35SJohn Dyson  *
6ee877a35SJohn Dyson  * Redistribution and use in source and binary forms, with or without
7ee877a35SJohn Dyson  * modification, are permitted provided that the following conditions
8ee877a35SJohn Dyson  * are met:
9ee877a35SJohn Dyson  * 1. Redistributions of source code must retain the above copyright
10ee877a35SJohn Dyson  *    notice, this list of conditions and the following disclaimer.
11ee877a35SJohn Dyson  * 2. John S. Dyson's name may not be used to endorse or promote products
12ee877a35SJohn Dyson  *    derived from this software without specific prior written permission.
13ee877a35SJohn Dyson  *
14ee877a35SJohn Dyson  * DISCLAIMER:  This code isn't warranted to do anything useful.  Anything
15ee877a35SJohn Dyson  * bad that happens because of using this software isn't the responsibility
16ee877a35SJohn Dyson  * of the author.  This software is distributed AS-IS.
17ee877a35SJohn Dyson  */
18ee877a35SJohn Dyson 
19ee877a35SJohn Dyson /*
208a6472b7SPeter Dufault  * This file contains support for the POSIX 1003.1B AIO/LIO facility.
21ee877a35SJohn Dyson  */
22ee877a35SJohn Dyson 
23677b542eSDavid E. O'Brien #include <sys/cdefs.h>
24677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
25677b542eSDavid E. O'Brien 
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>
48496ab053SKonstantin Belousov #include <sys/syscallsubr.h>
49bfbbc4aaSJason Evans #include <sys/protosw.h>
5089f6b863SAttilio Rao #include <sys/rwlock.h>
511ce91824SDavid Xu #include <sys/sema.h>
521ce91824SDavid Xu #include <sys/socket.h>
53bfbbc4aaSJason Evans #include <sys/socketvar.h>
5421d56e9cSAlfred Perlstein #include <sys/syscall.h>
55a624e84fSJohn Dyson #include <sys/sysctl.h>
569c20dc99SJohn Baldwin #include <sys/syslog.h>
57ee99e978SBruce Evans #include <sys/sx.h>
581ce91824SDavid Xu #include <sys/taskqueue.h>
59fd3bf775SJohn Dyson #include <sys/vnode.h>
60fd3bf775SJohn Dyson #include <sys/conf.h>
61cb679c38SJonathan Lemon #include <sys/event.h>
6299eee864SDavid Xu #include <sys/mount.h>
63f743d981SAlexander Motin #include <geom/geom.h>
64ee877a35SJohn Dyson 
651ce91824SDavid Xu #include <machine/atomic.h>
661ce91824SDavid Xu 
67ee877a35SJohn Dyson #include <vm/vm.h>
68f743d981SAlexander Motin #include <vm/vm_page.h>
69ee877a35SJohn Dyson #include <vm/vm_extern.h>
702244ea07SJohn Dyson #include <vm/pmap.h>
712244ea07SJohn Dyson #include <vm/vm_map.h>
7299eee864SDavid Xu #include <vm/vm_object.h>
73c897b813SJeff Roberson #include <vm/uma.h>
74ee877a35SJohn Dyson #include <sys/aio.h>
755aaef07cSJohn Dyson 
76eb8e6d52SEivind Eklund /*
77eb8e6d52SEivind Eklund  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
7899eee864SDavid Xu  * overflow. (XXX will be removed soon.)
79eb8e6d52SEivind Eklund  */
8099eee864SDavid Xu static u_long jobrefid;
812244ea07SJohn Dyson 
8299eee864SDavid Xu /*
8399eee864SDavid Xu  * Counter for aio_fsync.
8499eee864SDavid Xu  */
8599eee864SDavid Xu static uint64_t jobseqno;
8699eee864SDavid Xu 
8784af4da6SJohn Dyson #ifndef MAX_AIO_PER_PROC
882244ea07SJohn Dyson #define MAX_AIO_PER_PROC	32
8984af4da6SJohn Dyson #endif
9084af4da6SJohn Dyson 
9184af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE_PER_PROC
92913b9329SAlan Somers #define MAX_AIO_QUEUE_PER_PROC	256
9384af4da6SJohn Dyson #endif
9484af4da6SJohn Dyson 
9584af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE
96913b9329SAlan Somers #define MAX_AIO_QUEUE		1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
9784af4da6SJohn Dyson #endif
9884af4da6SJohn Dyson 
9984af4da6SJohn Dyson #ifndef MAX_BUF_AIO
10084af4da6SJohn Dyson #define MAX_BUF_AIO		16
10184af4da6SJohn Dyson #endif
10284af4da6SJohn Dyson 
103e603be7aSRobert Watson FEATURE(aio, "Asynchronous I/O");
104c45796d5SAlan Somers SYSCTL_DECL(_p1003_1b);
105e603be7aSRobert Watson 
1063858a1f4SJohn Baldwin static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
1079553bc89SMark Johnston static MALLOC_DEFINE(M_AIO, "aio", "structures for asynchronous I/O");
1083858a1f4SJohn Baldwin 
1097029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1100dd6c035SJohn Baldwin     "Async IO management");
111eb8e6d52SEivind Eklund 
112f3215338SJohn Baldwin static int enable_aio_unsafe = 0;
113f3215338SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
114f3215338SJohn Baldwin     "Permit asynchronous IO on all file types, not just known-safe types");
115f3215338SJohn Baldwin 
1169c20dc99SJohn Baldwin static unsigned int unsafe_warningcnt = 1;
1179c20dc99SJohn Baldwin SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
1189c20dc99SJohn Baldwin     &unsafe_warningcnt, 0,
1199c20dc99SJohn Baldwin     "Warnings that will be triggered upon failed IO requests on unsafe files");
1209c20dc99SJohn Baldwin 
121303b270bSEivind Eklund static int max_aio_procs = MAX_AIO_PROCS;
1220dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
12339314b7dSJohn Baldwin     "Maximum number of kernel processes to use for handling async IO ");
124a624e84fSJohn Dyson 
125eb8e6d52SEivind Eklund static int num_aio_procs = 0;
1260dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
12739314b7dSJohn Baldwin     "Number of presently active kernel processes for async IO");
128a624e84fSJohn Dyson 
129eb8e6d52SEivind Eklund /*
130eb8e6d52SEivind Eklund  * The code will adjust the actual number of AIO processes towards this
131eb8e6d52SEivind Eklund  * number when it gets a chance.
132eb8e6d52SEivind Eklund  */
133eb8e6d52SEivind Eklund static int target_aio_procs = TARGET_AIO_PROCS;
134eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
1350dd6c035SJohn Baldwin     0,
1360dd6c035SJohn Baldwin     "Preferred number of ready kernel processes for async IO");
137a624e84fSJohn Dyson 
138eb8e6d52SEivind Eklund static int max_queue_count = MAX_AIO_QUEUE;
139eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
140eb8e6d52SEivind Eklund     "Maximum number of aio requests to queue, globally");
141a624e84fSJohn Dyson 
142eb8e6d52SEivind Eklund static int num_queue_count = 0;
143eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
144eb8e6d52SEivind Eklund     "Number of queued aio requests");
145a624e84fSJohn Dyson 
146eb8e6d52SEivind Eklund static int num_buf_aio = 0;
147eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
148eb8e6d52SEivind Eklund     "Number of aio requests presently handled by the buf subsystem");
149fd3bf775SJohn Dyson 
1508091e52bSJohn Baldwin static int num_unmapped_aio = 0;
1518091e52bSJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
1528091e52bSJohn Baldwin     0,
1538091e52bSJohn Baldwin     "Number of aio requests presently handled by unmapped I/O buffers");
1548091e52bSJohn Baldwin 
15539314b7dSJohn Baldwin /* Number of async I/O processes in the process of being started */
156a9bf5e37SDavid Xu /* XXX This should be local to aio_aqueue() */
157eb8e6d52SEivind Eklund static int num_aio_resv_start = 0;
158fd3bf775SJohn Dyson 
159eb8e6d52SEivind Eklund static int aiod_lifetime;
160eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
161eb8e6d52SEivind Eklund     "Maximum lifetime for idle aiod");
16284af4da6SJohn Dyson 
163eb8e6d52SEivind Eklund static int max_aio_per_proc = MAX_AIO_PER_PROC;
164eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
1650dd6c035SJohn Baldwin     0,
16686bbef43SJohn Baldwin     "Maximum active aio requests per process");
167eb8e6d52SEivind Eklund 
168eb8e6d52SEivind Eklund static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
169eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
170eb8e6d52SEivind Eklund     &max_aio_queue_per_proc, 0,
17186bbef43SJohn Baldwin     "Maximum queued aio requests per process");
172eb8e6d52SEivind Eklund 
173eb8e6d52SEivind Eklund static int max_buf_aio = MAX_BUF_AIO;
174eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
17586bbef43SJohn Baldwin     "Maximum buf aio requests per process");
176eb8e6d52SEivind Eklund 
177913b9329SAlan Somers /*
178913b9329SAlan Somers  * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
179913b9329SAlan Somers  * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
180913b9329SAlan Somers  * vfs.aio.aio_listio_max.
181913b9329SAlan Somers  */
182c45796d5SAlan Somers SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
183913b9329SAlan Somers     CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
184913b9329SAlan Somers     0, "Maximum aio requests for a single lio_listio call");
185c45796d5SAlan Somers 
186399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
1870972628aSDavid Xu typedef struct oaiocb {
1880972628aSDavid Xu 	int	aio_fildes;		/* File descriptor */
1890972628aSDavid Xu 	off_t	aio_offset;		/* File offset for I/O */
1900972628aSDavid Xu 	volatile void *aio_buf;         /* I/O buffer in process space */
1910972628aSDavid Xu 	size_t	aio_nbytes;		/* Number of bytes for I/O */
1920972628aSDavid Xu 	struct	osigevent aio_sigevent;	/* Signal to deliver */
1930972628aSDavid Xu 	int	aio_lio_opcode;		/* LIO opcode */
1940972628aSDavid Xu 	int	aio_reqprio;		/* Request priority -- ignored */
1950972628aSDavid Xu 	struct	__aiocb_private	_aiocb_private;
1960972628aSDavid Xu } oaiocb_t;
197399e8c17SJohn Baldwin #endif
1980972628aSDavid Xu 
1991aa4c324SDavid Xu /*
2005652770dSJohn Baldwin  * Below is a key of locks used to protect each member of struct kaiocb
2011aa4c324SDavid Xu  * aioliojob and kaioinfo and any backends.
2021aa4c324SDavid Xu  *
2031aa4c324SDavid Xu  * * - need not protected
204759ccccaSDavid Xu  * a - locked by kaioinfo lock
2051aa4c324SDavid Xu  * b - locked by backend lock, the backend lock can be null in some cases,
2061aa4c324SDavid Xu  *     for example, BIO belongs to this type, in this case, proc lock is
2071aa4c324SDavid Xu  *     reused.
2081aa4c324SDavid Xu  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
2091aa4c324SDavid Xu  */
2101aa4c324SDavid Xu 
2111aa4c324SDavid Xu /*
212f3215338SJohn Baldwin  * If the routine that services an AIO request blocks while running in an
213f3215338SJohn Baldwin  * AIO kernel process it can starve other I/O requests.  BIO requests
21472bce9ffSAlan Somers  * queued via aio_qbio() complete asynchronously and do not use AIO kernel
215f3215338SJohn Baldwin  * processes at all.  Socket I/O requests use a separate pool of
216f3215338SJohn Baldwin  * kprocs and also force non-blocking I/O.  Other file I/O requests
217f3215338SJohn Baldwin  * use the generic fo_read/fo_write operations which can block.  The
218f3215338SJohn Baldwin  * fsync and mlock operations can also block while executing.  Ideally
219f3215338SJohn Baldwin  * none of these requests would block while executing.
220f3215338SJohn Baldwin  *
221f3215338SJohn Baldwin  * Note that the service routines cannot toggle O_NONBLOCK in the file
222f3215338SJohn Baldwin  * structure directly while handling a request due to races with
223f3215338SJohn Baldwin  * userland threads.
2241aa4c324SDavid Xu  */
2251aa4c324SDavid Xu 
22648dac059SAlan Cox /* jobflags */
227f3215338SJohn Baldwin #define	KAIOCB_QUEUEING		0x01
228f3215338SJohn Baldwin #define	KAIOCB_CANCELLED	0x02
229f3215338SJohn Baldwin #define	KAIOCB_CANCELLING	0x04
2305652770dSJohn Baldwin #define	KAIOCB_CHECKSYNC	0x08
231f3215338SJohn Baldwin #define	KAIOCB_CLEARED		0x10
232f3215338SJohn Baldwin #define	KAIOCB_FINISHED		0x20
23348dac059SAlan Cox 
2342244ea07SJohn Dyson /*
2352244ea07SJohn Dyson  * AIO process info
2362244ea07SJohn Dyson  */
23784af4da6SJohn Dyson #define AIOP_FREE	0x1			/* proc on free queue */
23884af4da6SJohn Dyson 
23939314b7dSJohn Baldwin struct aioproc {
24039314b7dSJohn Baldwin 	int	aioprocflags;			/* (c) AIO proc flags */
24139314b7dSJohn Baldwin 	TAILQ_ENTRY(aioproc) list;		/* (c) list of processes */
24239314b7dSJohn Baldwin 	struct	proc *aioproc;			/* (*) the AIO proc */
2432244ea07SJohn Dyson };
2442244ea07SJohn Dyson 
24584af4da6SJohn Dyson /*
24684af4da6SJohn Dyson  * data-structure for lio signal management
24784af4da6SJohn Dyson  */
2481ce91824SDavid Xu struct aioliojob {
2491aa4c324SDavid Xu 	int	lioj_flags;			/* (a) listio flags */
2505c5005ecSKonstantin Belousov 	int	lioj_count;			/* (a) count of jobs */
2515c5005ecSKonstantin Belousov 	int	lioj_finished_count;		/* (a) count of finished jobs */
2521aa4c324SDavid Xu 	struct	sigevent lioj_signal;		/* (a) signal on all I/O done */
2531aa4c324SDavid Xu 	TAILQ_ENTRY(aioliojob) lioj_list;	/* (a) lio list */
2541aa4c324SDavid Xu 	struct	knlist klist;			/* (a) list of knotes */
2551aa4c324SDavid Xu 	ksiginfo_t lioj_ksi;			/* (a) Realtime signal info */
25684af4da6SJohn Dyson };
2571ce91824SDavid Xu 
25884af4da6SJohn Dyson #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
25984af4da6SJohn Dyson #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
26069cd28daSDoug Ambrisko #define LIOJ_KEVENT_POSTED	0x4	/* kevent triggered */
26184af4da6SJohn Dyson 
26284af4da6SJohn Dyson /*
26384af4da6SJohn Dyson  * per process aio data structure
26484af4da6SJohn Dyson  */
2652244ea07SJohn Dyson struct kaioinfo {
266759ccccaSDavid Xu 	struct	mtx kaio_mtx;		/* the lock to protect this struct */
2671aa4c324SDavid Xu 	int	kaio_flags;		/* (a) per process kaio flags */
2681aa4c324SDavid Xu 	int	kaio_active_count;	/* (c) number of currently used AIOs */
2691aa4c324SDavid Xu 	int	kaio_count;		/* (a) size of AIO queue */
27072bce9ffSAlan Somers 	int	kaio_buffer_count;	/* (a) number of bio buffers */
2715652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_all;	/* (a) all AIOs in a process */
2725652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_done;	/* (a) done queue for process */
2731aa4c324SDavid Xu 	TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
2745652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_jobqueue;	/* (a) job queue for process */
2755652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncqueue;	/* (a) queue for aio_fsync */
276f3215338SJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncready;  /* (a) second q for aio_fsync */
27739314b7dSJohn Baldwin 	struct	task kaio_task;		/* (*) task to kick aio processes */
278f3215338SJohn Baldwin 	struct	task kaio_sync_task;	/* (*) task to schedule fsync jobs */
2792244ea07SJohn Dyson };
2802244ea07SJohn Dyson 
281759ccccaSDavid Xu #define AIO_LOCK(ki)		mtx_lock(&(ki)->kaio_mtx)
282759ccccaSDavid Xu #define AIO_UNLOCK(ki)		mtx_unlock(&(ki)->kaio_mtx)
283759ccccaSDavid Xu #define AIO_LOCK_ASSERT(ki, f)	mtx_assert(&(ki)->kaio_mtx, (f))
284759ccccaSDavid Xu #define AIO_MTX(ki)		(&(ki)->kaio_mtx)
285759ccccaSDavid Xu 
28684af4da6SJohn Dyson #define KAIO_RUNDOWN	0x1	/* process is being run down */
2870dd6c035SJohn Baldwin #define KAIO_WAKEUP	0x2	/* wakeup process when AIO completes */
288fd3bf775SJohn Dyson 
2893858a1f4SJohn Baldwin /*
2903858a1f4SJohn Baldwin  * Operations used to interact with userland aio control blocks.
2913858a1f4SJohn Baldwin  * Different ABIs provide their own operations.
2923858a1f4SJohn Baldwin  */
2933858a1f4SJohn Baldwin struct aiocb_ops {
294022ca2fcSAlan Somers 	int	(*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
2953858a1f4SJohn Baldwin 	long	(*fetch_status)(struct aiocb *ujob);
2963858a1f4SJohn Baldwin 	long	(*fetch_error)(struct aiocb *ujob);
2973858a1f4SJohn Baldwin 	int	(*store_status)(struct aiocb *ujob, long status);
2983858a1f4SJohn Baldwin 	int	(*store_error)(struct aiocb *ujob, long error);
2993858a1f4SJohn Baldwin 	int	(*store_kernelinfo)(struct aiocb *ujob, long jobref);
3003858a1f4SJohn Baldwin 	int	(*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
3013858a1f4SJohn Baldwin };
3023858a1f4SJohn Baldwin 
30339314b7dSJohn Baldwin static TAILQ_HEAD(,aioproc) aio_freeproc;		/* (c) Idle daemons */
3041ce91824SDavid Xu static struct sema aio_newproc_sem;
3051ce91824SDavid Xu static struct mtx aio_job_mtx;
3065652770dSJohn Baldwin static TAILQ_HEAD(,kaiocb) aio_jobs;			/* (c) Async job list */
3071ce91824SDavid Xu static struct unrhdr *aiod_unr;
3082244ea07SJohn Dyson 
309022ca2fcSAlan Somers static void	aio_biocleanup(struct bio *bp);
3106a1162d4SAlexander Leidinger void		aio_init_aioinfo(struct proc *p);
311723d37c0SKonstantin Belousov static int	aio_onceonly(void);
3125652770dSJohn Baldwin static int	aio_free_entry(struct kaiocb *job);
3135652770dSJohn Baldwin static void	aio_process_rw(struct kaiocb *job);
3145652770dSJohn Baldwin static void	aio_process_sync(struct kaiocb *job);
3155652770dSJohn Baldwin static void	aio_process_mlock(struct kaiocb *job);
316f3215338SJohn Baldwin static void	aio_schedule_fsync(void *context, int pending);
3171ce91824SDavid Xu static int	aio_newproc(int *);
3185652770dSJohn Baldwin int		aio_aqueue(struct thread *td, struct aiocb *ujob,
3193858a1f4SJohn Baldwin 		    struct aioliojob *lio, int type, struct aiocb_ops *ops);
320f3215338SJohn Baldwin static int	aio_queue_file(struct file *fp, struct kaiocb *job);
32172bce9ffSAlan Somers static void	aio_biowakeup(struct bio *bp);
32275b8b3b2SJohn Baldwin static void	aio_proc_rundown(void *arg, struct proc *p);
3230dd6c035SJohn Baldwin static void	aio_proc_rundown_exec(void *arg, struct proc *p,
3240dd6c035SJohn Baldwin 		    struct image_params *imgp);
32572bce9ffSAlan Somers static int	aio_qbio(struct proc *p, struct kaiocb *job);
3261ce91824SDavid Xu static void	aio_daemon(void *param);
327f3215338SJohn Baldwin static void	aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
328005ce8e4SJohn Baldwin static bool	aio_clear_cancel_function_locked(struct kaiocb *job);
329dbbccfe9SDavid Xu static int	aio_kick(struct proc *userp);
33099eee864SDavid Xu static void	aio_kick_nowait(struct proc *userp);
33199eee864SDavid Xu static void	aio_kick_helper(void *context, int pending);
33221d56e9cSAlfred Perlstein static int	filt_aioattach(struct knote *kn);
33321d56e9cSAlfred Perlstein static void	filt_aiodetach(struct knote *kn);
33421d56e9cSAlfred Perlstein static int	filt_aio(struct knote *kn, long hint);
33569cd28daSDoug Ambrisko static int	filt_lioattach(struct knote *kn);
33669cd28daSDoug Ambrisko static void	filt_liodetach(struct knote *kn);
33769cd28daSDoug Ambrisko static int	filt_lio(struct knote *kn, long hint);
3382244ea07SJohn Dyson 
339eb8e6d52SEivind Eklund /*
340eb8e6d52SEivind Eklund  * Zones for:
341eb8e6d52SEivind Eklund  * 	kaio	Per process async io info
342eb8e6d52SEivind Eklund  *	aiocb	async io jobs
343eb8e6d52SEivind Eklund  *	aiolio	list io jobs
344eb8e6d52SEivind Eklund  */
3459553bc89SMark Johnston static uma_zone_t kaio_zone, aiocb_zone, aiolio_zone;
346fd3bf775SJohn Dyson 
347eb8e6d52SEivind Eklund /* kqueue filters for aio */
348e76d823bSRobert Watson static struct filterops aio_filtops = {
349e76d823bSRobert Watson 	.f_isfd = 0,
350e76d823bSRobert Watson 	.f_attach = filt_aioattach,
351e76d823bSRobert Watson 	.f_detach = filt_aiodetach,
352e76d823bSRobert Watson 	.f_event = filt_aio,
353e76d823bSRobert Watson };
354e76d823bSRobert Watson static struct filterops lio_filtops = {
355e76d823bSRobert Watson 	.f_isfd = 0,
356e76d823bSRobert Watson 	.f_attach = filt_lioattach,
357e76d823bSRobert Watson 	.f_detach = filt_liodetach,
358e76d823bSRobert Watson 	.f_event = filt_lio
359e76d823bSRobert Watson };
36021d56e9cSAlfred Perlstein 
36175b8b3b2SJohn Baldwin static eventhandler_tag exit_tag, exec_tag;
36275b8b3b2SJohn Baldwin 
363c85650caSJohn Baldwin TASKQUEUE_DEFINE_THREAD(aiod_kick);
3641ce91824SDavid Xu 
365eb8e6d52SEivind Eklund /*
366eb8e6d52SEivind Eklund  * Main operations function for use as a kernel module.
367eb8e6d52SEivind Eklund  */
36821d56e9cSAlfred Perlstein static int
36921d56e9cSAlfred Perlstein aio_modload(struct module *module, int cmd, void *arg)
37021d56e9cSAlfred Perlstein {
37121d56e9cSAlfred Perlstein 	int error = 0;
37221d56e9cSAlfred Perlstein 
37321d56e9cSAlfred Perlstein 	switch (cmd) {
37421d56e9cSAlfred Perlstein 	case MOD_LOAD:
37521d56e9cSAlfred Perlstein 		aio_onceonly();
37621d56e9cSAlfred Perlstein 		break;
37721d56e9cSAlfred Perlstein 	case MOD_SHUTDOWN:
37821d56e9cSAlfred Perlstein 		break;
37921d56e9cSAlfred Perlstein 	default:
380f3215338SJohn Baldwin 		error = EOPNOTSUPP;
38121d56e9cSAlfred Perlstein 		break;
38221d56e9cSAlfred Perlstein 	}
38321d56e9cSAlfred Perlstein 	return (error);
38421d56e9cSAlfred Perlstein }
38521d56e9cSAlfred Perlstein 
38621d56e9cSAlfred Perlstein static moduledata_t aio_mod = {
38721d56e9cSAlfred Perlstein 	"aio",
38821d56e9cSAlfred Perlstein 	&aio_modload,
38921d56e9cSAlfred Perlstein 	NULL
39021d56e9cSAlfred Perlstein };
39121d56e9cSAlfred Perlstein 
392399e8c17SJohn Baldwin DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
39321d56e9cSAlfred Perlstein MODULE_VERSION(aio, 1);
39421d56e9cSAlfred Perlstein 
395fd3bf775SJohn Dyson /*
3962244ea07SJohn Dyson  * Startup initialization
3972244ea07SJohn Dyson  */
398723d37c0SKonstantin Belousov static int
39921d56e9cSAlfred Perlstein aio_onceonly(void)
400fd3bf775SJohn Dyson {
40121d56e9cSAlfred Perlstein 
40275b8b3b2SJohn Baldwin 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
40375b8b3b2SJohn Baldwin 	    EVENTHANDLER_PRI_ANY);
4040dd6c035SJohn Baldwin 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
4050dd6c035SJohn Baldwin 	    NULL, EVENTHANDLER_PRI_ANY);
40621d56e9cSAlfred Perlstein 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
40769cd28daSDoug Ambrisko 	kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
4082244ea07SJohn Dyson 	TAILQ_INIT(&aio_freeproc);
4091ce91824SDavid Xu 	sema_init(&aio_newproc_sem, 0, "aio_new_proc");
4101ce91824SDavid Xu 	mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
4112244ea07SJohn Dyson 	TAILQ_INIT(&aio_jobs);
4121ce91824SDavid Xu 	aiod_unr = new_unrhdr(1, INT_MAX, NULL);
413c897b813SJeff Roberson 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
4149553bc89SMark Johnston 	    NULL, NULL, UMA_ALIGN_PTR, 0);
4155652770dSJohn Baldwin 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
4169553bc89SMark Johnston 	    NULL, NULL, UMA_ALIGN_PTR, 0);
4171ce91824SDavid Xu 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
4189553bc89SMark Johnston 	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
41984af4da6SJohn Dyson 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
420fd3bf775SJohn Dyson 	jobrefid = 1;
421399e8c17SJohn Baldwin 	p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
42286d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
42386d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
424723d37c0SKonstantin Belousov 
425723d37c0SKonstantin Belousov 	return (0);
4262244ea07SJohn Dyson }
4272244ea07SJohn Dyson 
428eb8e6d52SEivind Eklund /*
429bfbbc4aaSJason Evans  * Init the per-process aioinfo structure.  The aioinfo limits are set
430bfbbc4aaSJason Evans  * per-process for user limit (resource) management.
4312244ea07SJohn Dyson  */
4326a1162d4SAlexander Leidinger void
433fd3bf775SJohn Dyson aio_init_aioinfo(struct proc *p)
434fd3bf775SJohn Dyson {
4352244ea07SJohn Dyson 	struct kaioinfo *ki;
436ac41f2efSAlfred Perlstein 
437a163d034SWarner Losh 	ki = uma_zalloc(kaio_zone, M_WAITOK);
4389889bbacSKonstantin Belousov 	mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
43984af4da6SJohn Dyson 	ki->kaio_flags = 0;
4402244ea07SJohn Dyson 	ki->kaio_active_count = 0;
4411ce91824SDavid Xu 	ki->kaio_count = 0;
442fd3bf775SJohn Dyson 	ki->kaio_buffer_count = 0;
4431ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_all);
4441ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_done);
4452244ea07SJohn Dyson 	TAILQ_INIT(&ki->kaio_jobqueue);
44684af4da6SJohn Dyson 	TAILQ_INIT(&ki->kaio_liojoblist);
44799eee864SDavid Xu 	TAILQ_INIT(&ki->kaio_syncqueue);
448f3215338SJohn Baldwin 	TAILQ_INIT(&ki->kaio_syncready);
44999eee864SDavid Xu 	TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
450f3215338SJohn Baldwin 	TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
4513999ebe3SAlan Cox 	PROC_LOCK(p);
4523999ebe3SAlan Cox 	if (p->p_aioinfo == NULL) {
4533999ebe3SAlan Cox 		p->p_aioinfo = ki;
4543999ebe3SAlan Cox 		PROC_UNLOCK(p);
4553999ebe3SAlan Cox 	} else {
4563999ebe3SAlan Cox 		PROC_UNLOCK(p);
457759ccccaSDavid Xu 		mtx_destroy(&ki->kaio_mtx);
4583999ebe3SAlan Cox 		uma_zfree(kaio_zone, ki);
4592244ea07SJohn Dyson 	}
460bfbbc4aaSJason Evans 
46122035f47SOleksandr Tymoshenko 	while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
4621ce91824SDavid Xu 		aio_newproc(NULL);
4632244ea07SJohn Dyson }
4642244ea07SJohn Dyson 
4654c0fb2cfSDavid Xu static int
4666814c2daSKonstantin Belousov aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
4674c0fb2cfSDavid Xu {
468cf7d9a8cSDavid Xu 	struct thread *td;
469cf7d9a8cSDavid Xu 	int error;
470759ccccaSDavid Xu 
471cf7d9a8cSDavid Xu 	error = sigev_findtd(p, sigev, &td);
472cf7d9a8cSDavid Xu 	if (error)
473cf7d9a8cSDavid Xu 		return (error);
4744c0fb2cfSDavid Xu 	if (!KSI_ONQ(ksi)) {
475cf7d9a8cSDavid Xu 		ksiginfo_set_sigev(ksi, sigev);
4764c0fb2cfSDavid Xu 		ksi->ksi_code = SI_ASYNCIO;
4776814c2daSKonstantin Belousov 		ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
478cf7d9a8cSDavid Xu 		tdsendsignal(p, td, ksi->ksi_signo, ksi);
4794c0fb2cfSDavid Xu 	}
480759ccccaSDavid Xu 	PROC_UNLOCK(p);
481cf7d9a8cSDavid Xu 	return (error);
4824c0fb2cfSDavid Xu }
4834c0fb2cfSDavid Xu 
4842244ea07SJohn Dyson /*
485bfbbc4aaSJason Evans  * Free a job entry.  Wait for completion if it is currently active, but don't
486bfbbc4aaSJason Evans  * delay forever.  If we delay, we return a flag that says that we have to
487bfbbc4aaSJason Evans  * restart the queue scan.
4882244ea07SJohn Dyson  */
48988ed460eSAlan Cox static int
4905652770dSJohn Baldwin aio_free_entry(struct kaiocb *job)
491fd3bf775SJohn Dyson {
4922244ea07SJohn Dyson 	struct kaioinfo *ki;
4931ce91824SDavid Xu 	struct aioliojob *lj;
4942244ea07SJohn Dyson 	struct proc *p;
4952244ea07SJohn Dyson 
4965652770dSJohn Baldwin 	p = job->userproc;
4971ce91824SDavid Xu 	MPASS(curproc == p);
4982244ea07SJohn Dyson 	ki = p->p_aioinfo;
4991ce91824SDavid Xu 	MPASS(ki != NULL);
5001ce91824SDavid Xu 
501759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
502f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
503759ccccaSDavid Xu 
5041ce91824SDavid Xu 	atomic_subtract_int(&num_queue_count, 1);
5051ce91824SDavid Xu 
5061ce91824SDavid Xu 	ki->kaio_count--;
5071ce91824SDavid Xu 	MPASS(ki->kaio_count >= 0);
5081ce91824SDavid Xu 
5095652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_done, job, plist);
5105652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_all, job, allist);
51127b8220dSDavid Xu 
5125652770dSJohn Baldwin 	lj = job->lio;
51384af4da6SJohn Dyson 	if (lj) {
5141ce91824SDavid Xu 		lj->lioj_count--;
5151ce91824SDavid Xu 		lj->lioj_finished_count--;
5161ce91824SDavid Xu 
517a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
5181ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
5191ce91824SDavid Xu 			/* lio is going away, we need to destroy any knotes */
5201ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
521759ccccaSDavid Xu 			PROC_LOCK(p);
5221ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
523759ccccaSDavid Xu 			PROC_UNLOCK(p);
5241ce91824SDavid Xu 			uma_zfree(aiolio_zone, lj);
52584af4da6SJohn Dyson 		}
52684af4da6SJohn Dyson 	}
5271ce91824SDavid Xu 
5285652770dSJohn Baldwin 	/* job is going away, we need to destroy any knotes */
5295652770dSJohn Baldwin 	knlist_delete(&job->klist, curthread, 1);
530759ccccaSDavid Xu 	PROC_LOCK(p);
5315652770dSJohn Baldwin 	sigqueue_take(&job->ksi);
532759ccccaSDavid Xu 	PROC_UNLOCK(p);
5331ce91824SDavid Xu 
534759ccccaSDavid Xu 	AIO_UNLOCK(ki);
5352a522eb9SJohn Baldwin 
5362a522eb9SJohn Baldwin 	/*
5372a522eb9SJohn Baldwin 	 * The thread argument here is used to find the owning process
5382a522eb9SJohn Baldwin 	 * and is also passed to fo_close() which may pass it to various
5392a522eb9SJohn Baldwin 	 * places such as devsw close() routines.  Because of that, we
5402a522eb9SJohn Baldwin 	 * need a thread pointer from the process owning the job that is
5412a522eb9SJohn Baldwin 	 * persistent and won't disappear out from under us or move to
5422a522eb9SJohn Baldwin 	 * another process.
5432a522eb9SJohn Baldwin 	 *
5442a522eb9SJohn Baldwin 	 * Currently, all the callers of this function call it to remove
5455652770dSJohn Baldwin 	 * a kaiocb from the current process' job list either via a
5462a522eb9SJohn Baldwin 	 * syscall or due to the current process calling exit() or
5472a522eb9SJohn Baldwin 	 * execve().  Thus, we know that p == curproc.  We also know that
5482a522eb9SJohn Baldwin 	 * curthread can't exit since we are curthread.
5492a522eb9SJohn Baldwin 	 *
5502a522eb9SJohn Baldwin 	 * Therefore, we use curthread as the thread to pass to
5512a522eb9SJohn Baldwin 	 * knlist_delete().  This does mean that it is possible for the
5522a522eb9SJohn Baldwin 	 * thread pointer at close time to differ from the thread pointer
5532a522eb9SJohn Baldwin 	 * at open time, but this is already true of file descriptors in
5542a522eb9SJohn Baldwin 	 * a multithreaded process.
555b40ce416SJulian Elischer 	 */
5565652770dSJohn Baldwin 	if (job->fd_file)
5575652770dSJohn Baldwin 		fdrop(job->fd_file, curthread);
5585652770dSJohn Baldwin 	crfree(job->cred);
559022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
560022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
5615652770dSJohn Baldwin 	uma_zfree(aiocb_zone, job);
562759ccccaSDavid Xu 	AIO_LOCK(ki);
5631ce91824SDavid Xu 
564ac41f2efSAlfred Perlstein 	return (0);
5652244ea07SJohn Dyson }
5662244ea07SJohn Dyson 
567993182e5SAlexander Leidinger static void
5680dd6c035SJohn Baldwin aio_proc_rundown_exec(void *arg, struct proc *p,
5690dd6c035SJohn Baldwin     struct image_params *imgp __unused)
570993182e5SAlexander Leidinger {
571993182e5SAlexander Leidinger    	aio_proc_rundown(arg, p);
572993182e5SAlexander Leidinger }
573993182e5SAlexander Leidinger 
574f3215338SJohn Baldwin static int
575f3215338SJohn Baldwin aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
576f3215338SJohn Baldwin {
577f3215338SJohn Baldwin 	aio_cancel_fn_t *func;
578f3215338SJohn Baldwin 	int cancelled;
579f3215338SJohn Baldwin 
580f3215338SJohn Baldwin 	AIO_LOCK_ASSERT(ki, MA_OWNED);
581f3215338SJohn Baldwin 	if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
582f3215338SJohn Baldwin 		return (0);
583f3215338SJohn Baldwin 	MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
584f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLED;
585f3215338SJohn Baldwin 
586f3215338SJohn Baldwin 	func = job->cancel_fn;
587f3215338SJohn Baldwin 
588f3215338SJohn Baldwin 	/*
589f3215338SJohn Baldwin 	 * If there is no cancel routine, just leave the job marked as
590f3215338SJohn Baldwin 	 * cancelled.  The job should be in active use by a caller who
591f3215338SJohn Baldwin 	 * should complete it normally or when it fails to install a
592f3215338SJohn Baldwin 	 * cancel routine.
593f3215338SJohn Baldwin 	 */
594f3215338SJohn Baldwin 	if (func == NULL)
595f3215338SJohn Baldwin 		return (0);
596f3215338SJohn Baldwin 
597f3215338SJohn Baldwin 	/*
598f3215338SJohn Baldwin 	 * Set the CANCELLING flag so that aio_complete() will defer
599f3215338SJohn Baldwin 	 * completions of this job.  This prevents the job from being
600f3215338SJohn Baldwin 	 * freed out from under the cancel callback.  After the
601f3215338SJohn Baldwin 	 * callback any deferred completion (whether from the callback
602f3215338SJohn Baldwin 	 * or any other source) will be completed.
603f3215338SJohn Baldwin 	 */
604f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLING;
605f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
606f3215338SJohn Baldwin 	func(job);
607f3215338SJohn Baldwin 	AIO_LOCK(ki);
608f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_CANCELLING;
609f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
610f3215338SJohn Baldwin 		cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
611f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
612f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
613f3215338SJohn Baldwin 	} else {
614f3215338SJohn Baldwin 		/*
615f3215338SJohn Baldwin 		 * The cancel callback might have scheduled an
616f3215338SJohn Baldwin 		 * operation to cancel this request, but it is
617f3215338SJohn Baldwin 		 * only counted as cancelled if the request is
618f3215338SJohn Baldwin 		 * cancelled when the callback returns.
619f3215338SJohn Baldwin 		 */
620f3215338SJohn Baldwin 		cancelled = 0;
621f3215338SJohn Baldwin 	}
622f3215338SJohn Baldwin 	return (cancelled);
623f3215338SJohn Baldwin }
624f3215338SJohn Baldwin 
6252244ea07SJohn Dyson /*
6262244ea07SJohn Dyson  * Rundown the jobs for a given process.
6272244ea07SJohn Dyson  */
62821d56e9cSAlfred Perlstein static void
62975b8b3b2SJohn Baldwin aio_proc_rundown(void *arg, struct proc *p)
630fd3bf775SJohn Dyson {
6312244ea07SJohn Dyson 	struct kaioinfo *ki;
6321ce91824SDavid Xu 	struct aioliojob *lj;
6335652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
6342244ea07SJohn Dyson 
6352a522eb9SJohn Baldwin 	KASSERT(curthread->td_proc == p,
6362a522eb9SJohn Baldwin 	    ("%s: called on non-curproc", __func__));
6372244ea07SJohn Dyson 	ki = p->p_aioinfo;
6382244ea07SJohn Dyson 	if (ki == NULL)
6392244ea07SJohn Dyson 		return;
6402244ea07SJohn Dyson 
641759ccccaSDavid Xu 	AIO_LOCK(ki);
64227b8220dSDavid Xu 	ki->kaio_flags |= KAIO_RUNDOWN;
6431ce91824SDavid Xu 
6441ce91824SDavid Xu restart:
645a624e84fSJohn Dyson 
646bfbbc4aaSJason Evans 	/*
6471ce91824SDavid Xu 	 * Try to cancel all pending requests. This code simulates
6481ce91824SDavid Xu 	 * aio_cancel on all pending I/O requests.
649bfbbc4aaSJason Evans 	 */
6505652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
651f3215338SJohn Baldwin 		aio_cancel_job(p, ki, job);
6522244ea07SJohn Dyson 	}
65384af4da6SJohn Dyson 
6541ce91824SDavid Xu 	/* Wait for all running I/O to be finished */
655f3215338SJohn Baldwin 	if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
65684af4da6SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
657759ccccaSDavid Xu 		msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
6581ce91824SDavid Xu 		goto restart;
65984af4da6SJohn Dyson 	}
66084af4da6SJohn Dyson 
6611ce91824SDavid Xu 	/* Free all completed I/O requests. */
6625652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
6635652770dSJohn Baldwin 		aio_free_entry(job);
66484af4da6SJohn Dyson 
6651ce91824SDavid Xu 	while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
666a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
66784af4da6SJohn Dyson 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
6681ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
669759ccccaSDavid Xu 			PROC_LOCK(p);
6701ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
671759ccccaSDavid Xu 			PROC_UNLOCK(p);
672c897b813SJeff Roberson 			uma_zfree(aiolio_zone, lj);
673f4f0ecefSJohn Dyson 		} else {
674a9bf5e37SDavid Xu 			panic("LIO job not cleaned up: C:%d, FC:%d\n",
675a9bf5e37SDavid Xu 			    lj->lioj_count, lj->lioj_finished_count);
67684af4da6SJohn Dyson 		}
677f4f0ecefSJohn Dyson 	}
678759ccccaSDavid Xu 	AIO_UNLOCK(ki);
679c85650caSJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
680f3215338SJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
6815114048bSKonstantin Belousov 	mtx_destroy(&ki->kaio_mtx);
682c897b813SJeff Roberson 	uma_zfree(kaio_zone, ki);
683a624e84fSJohn Dyson 	p->p_aioinfo = NULL;
6842244ea07SJohn Dyson }
6852244ea07SJohn Dyson 
6862244ea07SJohn Dyson /*
687bfbbc4aaSJason Evans  * Select a job to run (called by an AIO daemon).
6882244ea07SJohn Dyson  */
6895652770dSJohn Baldwin static struct kaiocb *
69039314b7dSJohn Baldwin aio_selectjob(struct aioproc *aiop)
691fd3bf775SJohn Dyson {
6925652770dSJohn Baldwin 	struct kaiocb *job;
693bfbbc4aaSJason Evans 	struct kaioinfo *ki;
694bfbbc4aaSJason Evans 	struct proc *userp;
6952244ea07SJohn Dyson 
6961ce91824SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
697f3215338SJohn Baldwin restart:
6985652770dSJohn Baldwin 	TAILQ_FOREACH(job, &aio_jobs, list) {
6995652770dSJohn Baldwin 		userp = job->userproc;
7002244ea07SJohn Dyson 		ki = userp->p_aioinfo;
7012244ea07SJohn Dyson 
70286bbef43SJohn Baldwin 		if (ki->kaio_active_count < max_aio_per_proc) {
7035652770dSJohn Baldwin 			TAILQ_REMOVE(&aio_jobs, job, list);
704f3215338SJohn Baldwin 			if (!aio_clear_cancel_function(job))
705f3215338SJohn Baldwin 				goto restart;
706f3215338SJohn Baldwin 
7071ce91824SDavid Xu 			/* Account for currently active jobs. */
7081ce91824SDavid Xu 			ki->kaio_active_count++;
7091ce91824SDavid Xu 			break;
7101ce91824SDavid Xu 		}
7111ce91824SDavid Xu 	}
7125652770dSJohn Baldwin 	return (job);
7132244ea07SJohn Dyson }
7142244ea07SJohn Dyson 
7152244ea07SJohn Dyson /*
7160dd6c035SJohn Baldwin  * Move all data to a permanent storage device.  This code
717801ac943SThomas Munro  * simulates the fsync and fdatasync syscalls.
71899eee864SDavid Xu  */
71999eee864SDavid Xu static int
720801ac943SThomas Munro aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
72199eee864SDavid Xu {
72299eee864SDavid Xu 	struct mount *mp;
723922bee44SKonstantin Belousov 	vm_object_t obj;
72499eee864SDavid Xu 	int error;
72599eee864SDavid Xu 
726922bee44SKonstantin Belousov 	for (;;) {
727a75d1dddSMateusz Guzik 		error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
728922bee44SKonstantin Belousov 		if (error != 0)
729922bee44SKonstantin Belousov 			break;
730cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
731922bee44SKonstantin Belousov 		obj = vp->v_object;
732922bee44SKonstantin Belousov 		if (obj != NULL) {
733922bee44SKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
734922bee44SKonstantin Belousov 			vm_object_page_clean(obj, 0, 0, 0);
735922bee44SKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
73699eee864SDavid Xu 		}
737801ac943SThomas Munro 		if (op == LIO_DSYNC)
738801ac943SThomas Munro 			error = VOP_FDATASYNC(vp, td);
739801ac943SThomas Munro 		else
74099eee864SDavid Xu 			error = VOP_FSYNC(vp, MNT_WAIT, td);
74199eee864SDavid Xu 
742b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
74399eee864SDavid Xu 		vn_finished_write(mp);
7442933a7caSKonstantin Belousov 		if (error != ERELOOKUP)
745922bee44SKonstantin Belousov 			break;
746922bee44SKonstantin Belousov 	}
74799eee864SDavid Xu 	return (error);
74899eee864SDavid Xu }
74999eee864SDavid Xu 
75099eee864SDavid Xu /*
751f95c13dbSGleb Smirnoff  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
75272bce9ffSAlan Somers  * does the I/O request for the non-bio version of the operations.  The normal
75372bce9ffSAlan Somers  * vn operations are used, and this code should work in all instances for every
75472bce9ffSAlan Somers  * type of file, including pipes, sockets, fifos, and regular files.
7551ce91824SDavid Xu  *
7561aa4c324SDavid Xu  * XXX I don't think it works well for socket, pipe, and fifo.
7572244ea07SJohn Dyson  */
75888ed460eSAlan Cox static void
7595652770dSJohn Baldwin aio_process_rw(struct kaiocb *job)
760fd3bf775SJohn Dyson {
761f8f750c5SRobert Watson 	struct ucred *td_savedcred;
762b40ce416SJulian Elischer 	struct thread *td;
7632244ea07SJohn Dyson 	struct file *fp;
764bb430bc7SJohn Baldwin 	ssize_t cnt;
765b1012d80SJohn Baldwin 	long msgsnd_st, msgsnd_end;
766b1012d80SJohn Baldwin 	long msgrcv_st, msgrcv_end;
767b1012d80SJohn Baldwin 	long oublock_st, oublock_end;
768b1012d80SJohn Baldwin 	long inblock_st, inblock_end;
769022ca2fcSAlan Somers 	int error, opcode;
7702244ea07SJohn Dyson 
7715652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
772022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_READV ||
773022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITE ||
774022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITEV,
7755652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
776f95c13dbSGleb Smirnoff 
777f3215338SJohn Baldwin 	aio_switch_vmspace(job);
778b40ce416SJulian Elischer 	td = curthread;
779f8f750c5SRobert Watson 	td_savedcred = td->td_ucred;
7805652770dSJohn Baldwin 	td->td_ucred = job->cred;
781022ca2fcSAlan Somers 	job->uiop->uio_td = td;
7825652770dSJohn Baldwin 	fp = job->fd_file;
783bfbbc4aaSJason Evans 
784022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
785022ca2fcSAlan Somers 	cnt = job->uiop->uio_resid;
7862244ea07SJohn Dyson 
787b1012d80SJohn Baldwin 	msgrcv_st = td->td_ru.ru_msgrcv;
788b1012d80SJohn Baldwin 	msgsnd_st = td->td_ru.ru_msgsnd;
7891c4bcd05SJeff Roberson 	inblock_st = td->td_ru.ru_inblock;
7901c4bcd05SJeff Roberson 	oublock_st = td->td_ru.ru_oublock;
791b1012d80SJohn Baldwin 
792279d7226SMatthew Dillon 	/*
793a9bf5e37SDavid Xu 	 * aio_aqueue() acquires a reference to the file that is
7949b16adc1SAlan Cox 	 * released in aio_free_entry().
795279d7226SMatthew Dillon 	 */
796022ca2fcSAlan Somers 	if (opcode == LIO_READ || opcode == LIO_READV) {
797022ca2fcSAlan Somers 		if (job->uiop->uio_resid == 0)
7985114048bSKonstantin Belousov 			error = 0;
7995114048bSKonstantin Belousov 		else
800022ca2fcSAlan Somers 			error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
801022ca2fcSAlan Somers 			    td);
8022244ea07SJohn Dyson 	} else {
8036d53aa62SDavid Xu 		if (fp->f_type == DTYPE_VNODE)
8046d53aa62SDavid Xu 			bwillwrite();
805022ca2fcSAlan Somers 		error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
8062244ea07SJohn Dyson 	}
807b1012d80SJohn Baldwin 	msgrcv_end = td->td_ru.ru_msgrcv;
808b1012d80SJohn Baldwin 	msgsnd_end = td->td_ru.ru_msgsnd;
8091c4bcd05SJeff Roberson 	inblock_end = td->td_ru.ru_inblock;
8101c4bcd05SJeff Roberson 	oublock_end = td->td_ru.ru_oublock;
811fd3bf775SJohn Dyson 
812b1012d80SJohn Baldwin 	job->msgrcv = msgrcv_end - msgrcv_st;
813b1012d80SJohn Baldwin 	job->msgsnd = msgsnd_end - msgsnd_st;
814b1012d80SJohn Baldwin 	job->inblock = inblock_end - inblock_st;
815b1012d80SJohn Baldwin 	job->outblock = oublock_end - oublock_st;
8162244ea07SJohn Dyson 
817022ca2fcSAlan Somers 	if (error != 0 && job->uiop->uio_resid != cnt) {
8182244ea07SJohn Dyson 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
8192244ea07SJohn Dyson 			error = 0;
8202247f489SAlan Somers 		if (error == EPIPE && (opcode & LIO_WRITE)) {
8215652770dSJohn Baldwin 			PROC_LOCK(job->userproc);
8225652770dSJohn Baldwin 			kern_psignal(job->userproc, SIGPIPE);
8235652770dSJohn Baldwin 			PROC_UNLOCK(job->userproc);
82419eb87d2SJohn Baldwin 		}
8252244ea07SJohn Dyson 	}
8262244ea07SJohn Dyson 
827022ca2fcSAlan Somers 	cnt -= job->uiop->uio_resid;
828f8f750c5SRobert Watson 	td->td_ucred = td_savedcred;
829f0ec1740SJohn Baldwin 	if (error)
830f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
831f0ec1740SJohn Baldwin 	else
832f0ec1740SJohn Baldwin 		aio_complete(job, cnt, 0);
8332244ea07SJohn Dyson }
8342244ea07SJohn Dyson 
83569cd28daSDoug Ambrisko static void
8365652770dSJohn Baldwin aio_process_sync(struct kaiocb *job)
837f95c13dbSGleb Smirnoff {
838f95c13dbSGleb Smirnoff 	struct thread *td = curthread;
839f95c13dbSGleb Smirnoff 	struct ucred *td_savedcred = td->td_ucred;
8405652770dSJohn Baldwin 	struct file *fp = job->fd_file;
841f95c13dbSGleb Smirnoff 	int error = 0;
842f95c13dbSGleb Smirnoff 
8432247f489SAlan Somers 	KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
8445652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
845f95c13dbSGleb Smirnoff 
8465652770dSJohn Baldwin 	td->td_ucred = job->cred;
847801ac943SThomas Munro 	if (fp->f_vnode != NULL) {
848801ac943SThomas Munro 		error = aio_fsync_vnode(td, fp->f_vnode,
849801ac943SThomas Munro 		    job->uaiocb.aio_lio_opcode);
850801ac943SThomas Munro 	}
851f95c13dbSGleb Smirnoff 	td->td_ucred = td_savedcred;
852f0ec1740SJohn Baldwin 	if (error)
853f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
854f0ec1740SJohn Baldwin 	else
855f0ec1740SJohn Baldwin 		aio_complete(job, 0, 0);
856f95c13dbSGleb Smirnoff }
857f95c13dbSGleb Smirnoff 
858f95c13dbSGleb Smirnoff static void
8595652770dSJohn Baldwin aio_process_mlock(struct kaiocb *job)
8606160e12cSGleb Smirnoff {
8615652770dSJohn Baldwin 	struct aiocb *cb = &job->uaiocb;
8626160e12cSGleb Smirnoff 	int error;
8636160e12cSGleb Smirnoff 
8645652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
8655652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
8666160e12cSGleb Smirnoff 
867f3215338SJohn Baldwin 	aio_switch_vmspace(job);
868496ab053SKonstantin Belousov 	error = kern_mlock(job->userproc, job->cred,
869496ab053SKonstantin Belousov 	    __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
870496ab053SKonstantin Belousov 	aio_complete(job, error != 0 ? -1 : 0, error);
8716160e12cSGleb Smirnoff }
8726160e12cSGleb Smirnoff 
8736160e12cSGleb Smirnoff static void
874f3215338SJohn Baldwin aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
8751ce91824SDavid Xu {
8761ce91824SDavid Xu 	struct aioliojob *lj;
87769cd28daSDoug Ambrisko 	struct kaioinfo *ki;
8785652770dSJohn Baldwin 	struct kaiocb *sjob, *sjobn;
8791ce91824SDavid Xu 	int lj_done;
880f3215338SJohn Baldwin 	bool schedule_fsync;
88169cd28daSDoug Ambrisko 
88269cd28daSDoug Ambrisko 	ki = userp->p_aioinfo;
883759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
8845652770dSJohn Baldwin 	lj = job->lio;
88569cd28daSDoug Ambrisko 	lj_done = 0;
88669cd28daSDoug Ambrisko 	if (lj) {
8871ce91824SDavid Xu 		lj->lioj_finished_count++;
8881ce91824SDavid Xu 		if (lj->lioj_count == lj->lioj_finished_count)
88969cd28daSDoug Ambrisko 			lj_done = 1;
89069cd28daSDoug Ambrisko 	}
8915652770dSJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
892f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
89327b8220dSDavid Xu 
89427b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_RUNDOWN)
89527b8220dSDavid Xu 		goto notification_done;
89627b8220dSDavid Xu 
8975652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
8985652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
8996814c2daSKonstantin Belousov 		aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
9001ce91824SDavid Xu 
9015652770dSJohn Baldwin 	KNOTE_LOCKED(&job->klist, 1);
9021ce91824SDavid Xu 
90369cd28daSDoug Ambrisko 	if (lj_done) {
9041ce91824SDavid Xu 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
90569cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
9061ce91824SDavid Xu 			KNOTE_LOCKED(&lj->klist, 1);
90769cd28daSDoug Ambrisko 		}
9081ce91824SDavid Xu 		if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
90929331656SKonstantin Belousov 		    == LIOJ_SIGNAL &&
91029331656SKonstantin Belousov 		    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
9114c0fb2cfSDavid Xu 		    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
9126814c2daSKonstantin Belousov 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
9136814c2daSKonstantin Belousov 			    true);
91469cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
91569cd28daSDoug Ambrisko 		}
91669cd28daSDoug Ambrisko 	}
91727b8220dSDavid Xu 
91827b8220dSDavid Xu notification_done:
9195652770dSJohn Baldwin 	if (job->jobflags & KAIOCB_CHECKSYNC) {
920f3215338SJohn Baldwin 		schedule_fsync = false;
9215652770dSJohn Baldwin 		TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
922b9a53e16SJohn Baldwin 			if (job->fd_file != sjob->fd_file ||
923b9a53e16SJohn Baldwin 			    job->seqno >= sjob->seqno)
924b9a53e16SJohn Baldwin 				continue;
925b9a53e16SJohn Baldwin 			if (--sjob->pending > 0)
926b9a53e16SJohn Baldwin 				continue;
927b9a53e16SJohn Baldwin 			TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
928005ce8e4SJohn Baldwin 			if (!aio_clear_cancel_function_locked(sjob))
929f3215338SJohn Baldwin 				continue;
930b9a53e16SJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
931f3215338SJohn Baldwin 			schedule_fsync = true;
93299eee864SDavid Xu 		}
933f3215338SJohn Baldwin 		if (schedule_fsync)
934f3215338SJohn Baldwin 			taskqueue_enqueue(taskqueue_aiod_kick,
935f3215338SJohn Baldwin 			    &ki->kaio_sync_task);
93699eee864SDavid Xu 	}
93727b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_WAKEUP) {
93869cd28daSDoug Ambrisko 		ki->kaio_flags &= ~KAIO_WAKEUP;
9391ce91824SDavid Xu 		wakeup(&userp->p_aioinfo);
94069cd28daSDoug Ambrisko 	}
94169cd28daSDoug Ambrisko }
94269cd28daSDoug Ambrisko 
9438a4dc40fSJohn Baldwin static void
944f3215338SJohn Baldwin aio_schedule_fsync(void *context, int pending)
945f3215338SJohn Baldwin {
946f3215338SJohn Baldwin 	struct kaioinfo *ki;
947f3215338SJohn Baldwin 	struct kaiocb *job;
948f3215338SJohn Baldwin 
949f3215338SJohn Baldwin 	ki = context;
950f3215338SJohn Baldwin 	AIO_LOCK(ki);
951f3215338SJohn Baldwin 	while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
952f3215338SJohn Baldwin 		job = TAILQ_FIRST(&ki->kaio_syncready);
953f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncready, job, list);
954f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
955f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
956f3215338SJohn Baldwin 		AIO_LOCK(ki);
957f3215338SJohn Baldwin 	}
958f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
959f3215338SJohn Baldwin }
960f3215338SJohn Baldwin 
961f3215338SJohn Baldwin bool
962f3215338SJohn Baldwin aio_cancel_cleared(struct kaiocb *job)
963f3215338SJohn Baldwin {
964f3215338SJohn Baldwin 
965f3215338SJohn Baldwin 	/*
966f3215338SJohn Baldwin 	 * The caller should hold the same queue lock held when
967f3215338SJohn Baldwin 	 * aio_clear_cancel_function() was called and set this flag
968f3215338SJohn Baldwin 	 * ensuring this check sees an up-to-date value.  However,
969f3215338SJohn Baldwin 	 * there is no way to assert that.
970f3215338SJohn Baldwin 	 */
971f3215338SJohn Baldwin 	return ((job->jobflags & KAIOCB_CLEARED) != 0);
972f3215338SJohn Baldwin }
973f3215338SJohn Baldwin 
974005ce8e4SJohn Baldwin static bool
975005ce8e4SJohn Baldwin aio_clear_cancel_function_locked(struct kaiocb *job)
976005ce8e4SJohn Baldwin {
977005ce8e4SJohn Baldwin 
978005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
979005ce8e4SJohn Baldwin 	MPASS(job->cancel_fn != NULL);
980005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLING) {
981005ce8e4SJohn Baldwin 		job->jobflags |= KAIOCB_CLEARED;
982005ce8e4SJohn Baldwin 		return (false);
983005ce8e4SJohn Baldwin 	}
984005ce8e4SJohn Baldwin 	job->cancel_fn = NULL;
985005ce8e4SJohn Baldwin 	return (true);
986005ce8e4SJohn Baldwin }
987005ce8e4SJohn Baldwin 
988f3215338SJohn Baldwin bool
989f3215338SJohn Baldwin aio_clear_cancel_function(struct kaiocb *job)
990f3215338SJohn Baldwin {
991f3215338SJohn Baldwin 	struct kaioinfo *ki;
992005ce8e4SJohn Baldwin 	bool ret;
993f3215338SJohn Baldwin 
994f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
995f3215338SJohn Baldwin 	AIO_LOCK(ki);
996005ce8e4SJohn Baldwin 	ret = aio_clear_cancel_function_locked(job);
997f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
998005ce8e4SJohn Baldwin 	return (ret);
999f3215338SJohn Baldwin }
1000005ce8e4SJohn Baldwin 
1001005ce8e4SJohn Baldwin static bool
1002005ce8e4SJohn Baldwin aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1003005ce8e4SJohn Baldwin {
1004005ce8e4SJohn Baldwin 
1005005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1006005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLED)
1007005ce8e4SJohn Baldwin 		return (false);
1008005ce8e4SJohn Baldwin 	job->cancel_fn = func;
1009f3215338SJohn Baldwin 	return (true);
1010f3215338SJohn Baldwin }
1011f3215338SJohn Baldwin 
1012f3215338SJohn Baldwin bool
1013f3215338SJohn Baldwin aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1014f3215338SJohn Baldwin {
1015f3215338SJohn Baldwin 	struct kaioinfo *ki;
1016005ce8e4SJohn Baldwin 	bool ret;
1017f3215338SJohn Baldwin 
1018f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1019f3215338SJohn Baldwin 	AIO_LOCK(ki);
1020005ce8e4SJohn Baldwin 	ret = aio_set_cancel_function_locked(job, func);
1021f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1022005ce8e4SJohn Baldwin 	return (ret);
1023f3215338SJohn Baldwin }
1024f3215338SJohn Baldwin 
1025f3215338SJohn Baldwin void
1026f3215338SJohn Baldwin aio_complete(struct kaiocb *job, long status, int error)
1027f3215338SJohn Baldwin {
1028f3215338SJohn Baldwin 	struct kaioinfo *ki;
1029f3215338SJohn Baldwin 	struct proc *userp;
1030f3215338SJohn Baldwin 
1031f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.error = error;
1032f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.status = status;
1033f3215338SJohn Baldwin 
1034f3215338SJohn Baldwin 	userp = job->userproc;
1035f3215338SJohn Baldwin 	ki = userp->p_aioinfo;
1036f3215338SJohn Baldwin 
1037f3215338SJohn Baldwin 	AIO_LOCK(ki);
1038f3215338SJohn Baldwin 	KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1039f3215338SJohn Baldwin 	    ("duplicate aio_complete"));
1040f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_FINISHED;
1041f3215338SJohn Baldwin 	if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1042f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1043f3215338SJohn Baldwin 		aio_bio_done_notify(userp, job);
1044f3215338SJohn Baldwin 	}
1045f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1046f3215338SJohn Baldwin }
1047f3215338SJohn Baldwin 
1048f3215338SJohn Baldwin void
1049f3215338SJohn Baldwin aio_cancel(struct kaiocb *job)
1050f3215338SJohn Baldwin {
1051f3215338SJohn Baldwin 
1052f3215338SJohn Baldwin 	aio_complete(job, -1, ECANCELED);
1053f3215338SJohn Baldwin }
1054f3215338SJohn Baldwin 
1055f3215338SJohn Baldwin void
10565652770dSJohn Baldwin aio_switch_vmspace(struct kaiocb *job)
10578a4dc40fSJohn Baldwin {
10588a4dc40fSJohn Baldwin 
10595652770dSJohn Baldwin 	vmspace_switch_aio(job->userproc->p_vmspace);
10608a4dc40fSJohn Baldwin }
10618a4dc40fSJohn Baldwin 
10622244ea07SJohn Dyson /*
1063f95c13dbSGleb Smirnoff  * The AIO daemon, most of the actual work is done in aio_process_*,
106484af4da6SJohn Dyson  * but the setup (and address space mgmt) is done in this routine.
10652244ea07SJohn Dyson  */
10662244ea07SJohn Dyson static void
10671ce91824SDavid Xu aio_daemon(void *_id)
10682244ea07SJohn Dyson {
10695652770dSJohn Baldwin 	struct kaiocb *job;
107039314b7dSJohn Baldwin 	struct aioproc *aiop;
1071bfbbc4aaSJason Evans 	struct kaioinfo *ki;
1072f3215338SJohn Baldwin 	struct proc *p;
10738a4dc40fSJohn Baldwin 	struct vmspace *myvm;
1074b40ce416SJulian Elischer 	struct thread *td = curthread;
10751ce91824SDavid Xu 	int id = (intptr_t)_id;
10762244ea07SJohn Dyson 
10772244ea07SJohn Dyson 	/*
10788a4dc40fSJohn Baldwin 	 * Grab an extra reference on the daemon's vmspace so that it
10798a4dc40fSJohn Baldwin 	 * doesn't get freed by jobs that switch to a different
10808a4dc40fSJohn Baldwin 	 * vmspace.
10812244ea07SJohn Dyson 	 */
10828a4dc40fSJohn Baldwin 	p = td->td_proc;
10838a4dc40fSJohn Baldwin 	myvm = vmspace_acquire_ref(p);
1084fd3bf775SJohn Dyson 
10858a4dc40fSJohn Baldwin 	KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1086fd3bf775SJohn Dyson 
1087fd3bf775SJohn Dyson 	/*
1088bfbbc4aaSJason Evans 	 * Allocate and ready the aio control info.  There is one aiop structure
1089bfbbc4aaSJason Evans 	 * per daemon.
1090fd3bf775SJohn Dyson 	 */
10919553bc89SMark Johnston 	aiop = malloc(sizeof(*aiop), M_AIO, M_WAITOK);
109239314b7dSJohn Baldwin 	aiop->aioproc = p;
109339314b7dSJohn Baldwin 	aiop->aioprocflags = 0;
1094bfbbc4aaSJason Evans 
1095fd3bf775SJohn Dyson 	/*
1096fd3bf775SJohn Dyson 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1097b40ce416SJulian Elischer 	 * and creating too many daemons.)
1098fd3bf775SJohn Dyson 	 */
10991ce91824SDavid Xu 	sema_post(&aio_newproc_sem);
11002244ea07SJohn Dyson 
11011ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
1102bfbbc4aaSJason Evans 	for (;;) {
1103fd3bf775SJohn Dyson 		/*
1104fd3bf775SJohn Dyson 		 * Take daemon off of free queue
1105fd3bf775SJohn Dyson 		 */
110639314b7dSJohn Baldwin 		if (aiop->aioprocflags & AIOP_FREE) {
11072244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
110839314b7dSJohn Baldwin 			aiop->aioprocflags &= ~AIOP_FREE;
11092244ea07SJohn Dyson 		}
11102244ea07SJohn Dyson 
1111fd3bf775SJohn Dyson 		/*
1112bfbbc4aaSJason Evans 		 * Check for jobs.
1113fd3bf775SJohn Dyson 		 */
11145652770dSJohn Baldwin 		while ((job = aio_selectjob(aiop)) != NULL) {
11151ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11162244ea07SJohn Dyson 
1117f3215338SJohn Baldwin 			ki = job->userproc->p_aioinfo;
1118f3215338SJohn Baldwin 			job->handle_fn(job);
111984af4da6SJohn Dyson 
11209b84335cSDavid Xu 			mtx_lock(&aio_job_mtx);
11219b84335cSDavid Xu 			/* Decrement the active job count. */
11229b84335cSDavid Xu 			ki->kaio_active_count--;
11232244ea07SJohn Dyson 		}
11242244ea07SJohn Dyson 
1125fd3bf775SJohn Dyson 		/*
1126bfbbc4aaSJason Evans 		 * Disconnect from user address space.
1127fd3bf775SJohn Dyson 		 */
11288a4dc40fSJohn Baldwin 		if (p->p_vmspace != myvm) {
11291ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11308a4dc40fSJohn Baldwin 			vmspace_switch_aio(myvm);
11311ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
11321ce91824SDavid Xu 			/*
11331ce91824SDavid Xu 			 * We have to restart to avoid race, we only sleep if
11348a4dc40fSJohn Baldwin 			 * no job can be selected.
11351ce91824SDavid Xu 			 */
11361ce91824SDavid Xu 			continue;
1137fd3bf775SJohn Dyson 		}
1138fd3bf775SJohn Dyson 
11391ce91824SDavid Xu 		mtx_assert(&aio_job_mtx, MA_OWNED);
11401ce91824SDavid Xu 
1141fd3bf775SJohn Dyson 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
114239314b7dSJohn Baldwin 		aiop->aioprocflags |= AIOP_FREE;
1143fd3bf775SJohn Dyson 
1144fd3bf775SJohn Dyson 		/*
1145bfbbc4aaSJason Evans 		 * If daemon is inactive for a long time, allow it to exit,
1146bfbbc4aaSJason Evans 		 * thereby freeing resources.
1147fd3bf775SJohn Dyson 		 */
114839314b7dSJohn Baldwin 		if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
11498a4dc40fSJohn Baldwin 		    aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
115039314b7dSJohn Baldwin 		    (aiop->aioprocflags & AIOP_FREE) &&
11518a4dc40fSJohn Baldwin 		    num_aio_procs > target_aio_procs)
11528a4dc40fSJohn Baldwin 			break;
11538a4dc40fSJohn Baldwin 	}
1154fd3bf775SJohn Dyson 	TAILQ_REMOVE(&aio_freeproc, aiop, list);
115584af4da6SJohn Dyson 	num_aio_procs--;
11561ce91824SDavid Xu 	mtx_unlock(&aio_job_mtx);
11579553bc89SMark Johnston 	free(aiop, M_AIO);
11581ce91824SDavid Xu 	free_unr(aiod_unr, id);
11598a4dc40fSJohn Baldwin 	vmspace_free(myvm);
11608a4dc40fSJohn Baldwin 
11618a4dc40fSJohn Baldwin 	KASSERT(p->p_vmspace == myvm,
11628a4dc40fSJohn Baldwin 	    ("AIOD: bad vmspace for exiting daemon"));
1163f7db0c95SMark Johnston 	KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1164f7db0c95SMark Johnston 	    ("AIOD: bad vm refcnt for exiting daemon: %d",
1165f7db0c95SMark Johnston 	    refcount_load(&myvm->vm_refcnt)));
11663745c395SJulian Elischer 	kproc_exit(0);
1167fd3bf775SJohn Dyson }
11682244ea07SJohn Dyson 
11692244ea07SJohn Dyson /*
1170bfbbc4aaSJason Evans  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1171bfbbc4aaSJason Evans  * AIO daemon modifies its environment itself.
11722244ea07SJohn Dyson  */
11732244ea07SJohn Dyson static int
11741ce91824SDavid Xu aio_newproc(int *start)
1175fd3bf775SJohn Dyson {
11762244ea07SJohn Dyson 	int error;
1177c9a970a7SAlan Cox 	struct proc *p;
11781ce91824SDavid Xu 	int id;
11792244ea07SJohn Dyson 
11801ce91824SDavid Xu 	id = alloc_unr(aiod_unr);
11813745c395SJulian Elischer 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
11821ce91824SDavid Xu 		RFNOWAIT, 0, "aiod%d", id);
11831ce91824SDavid Xu 	if (error == 0) {
1184fd3bf775SJohn Dyson 		/*
11851ce91824SDavid Xu 		 * Wait until daemon is started.
1186fd3bf775SJohn Dyson 		 */
11871ce91824SDavid Xu 		sema_wait(&aio_newproc_sem);
11881ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
118984af4da6SJohn Dyson 		num_aio_procs++;
11901ce91824SDavid Xu 		if (start != NULL)
11917f34b521SDavid Xu 			(*start)--;
11921ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
11931ce91824SDavid Xu 	} else {
11941ce91824SDavid Xu 		free_unr(aiod_unr, id);
11951ce91824SDavid Xu 	}
1196ac41f2efSAlfred Perlstein 	return (error);
11972244ea07SJohn Dyson }
11982244ea07SJohn Dyson 
11992244ea07SJohn Dyson /*
120072bce9ffSAlan Somers  * Try the high-performance, low-overhead bio method for eligible
120188ed460eSAlan Cox  * VCHR devices.  This method doesn't use an aio helper thread, and
120288ed460eSAlan Cox  * thus has very low overhead.
120388ed460eSAlan Cox  *
1204a9bf5e37SDavid Xu  * Assumes that the caller, aio_aqueue(), has incremented the file
120588ed460eSAlan Cox  * structure's reference count, preventing its deallocation for the
120688ed460eSAlan Cox  * duration of this call.
1207fd3bf775SJohn Dyson  */
120888ed460eSAlan Cox static int
120972bce9ffSAlan Somers aio_qbio(struct proc *p, struct kaiocb *job)
1210fd3bf775SJohn Dyson {
1211fd3bf775SJohn Dyson 	struct aiocb *cb;
1212fd3bf775SJohn Dyson 	struct file *fp;
1213f743d981SAlexander Motin 	struct buf *pbuf;
1214fd3bf775SJohn Dyson 	struct vnode *vp;
1215f3215a60SKonstantin Belousov 	struct cdevsw *csw;
1216f3215a60SKonstantin Belousov 	struct cdev *dev;
1217fd3bf775SJohn Dyson 	struct kaioinfo *ki;
1218022ca2fcSAlan Somers 	struct bio **bios = NULL;
1219022ca2fcSAlan Somers 	off_t offset;
1220022ca2fcSAlan Somers 	int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1221f743d981SAlexander Motin 	vm_prot_t prot;
1222022ca2fcSAlan Somers 	bool use_unmapped;
1223fd3bf775SJohn Dyson 
12245652770dSJohn Baldwin 	cb = &job->uaiocb;
12255652770dSJohn Baldwin 	fp = job->fd_file;
1226022ca2fcSAlan Somers 	opcode = cb->aio_lio_opcode;
1227fd3bf775SJohn Dyson 
1228022ca2fcSAlan Somers 	if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1229022ca2fcSAlan Somers 	    opcode == LIO_READ || opcode == LIO_READV))
1230f54c5606SJohn Baldwin 		return (-1);
12316160e12cSGleb Smirnoff 	if (fp == NULL || fp->f_type != DTYPE_VNODE)
1232008626c3SPoul-Henning Kamp 		return (-1);
1233fd3bf775SJohn Dyson 
12343b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
1235f743d981SAlexander Motin 	if (vp->v_type != VCHR)
1236f582ac06SBrian Feldman 		return (-1);
1237ad8de0f2SDavid Xu 	if (vp->v_bufobj.bo_bsize == 0)
1238ad8de0f2SDavid Xu 		return (-1);
1239022ca2fcSAlan Somers 
12402247f489SAlan Somers 	bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1241022ca2fcSAlan Somers 	iovcnt = job->uiop->uio_iovcnt;
1242022ca2fcSAlan Somers 	if (iovcnt > max_buf_aio)
1243008626c3SPoul-Henning Kamp 		return (-1);
1244022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1245022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1246022ca2fcSAlan Somers 			return (-1);
1247022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len > maxphys) {
1248022ca2fcSAlan Somers 			error = -1;
1249022ca2fcSAlan Somers 			return (-1);
1250022ca2fcSAlan Somers 		}
1251022ca2fcSAlan Somers 	}
1252022ca2fcSAlan Somers 	offset = cb->aio_offset;
1253fd3bf775SJohn Dyson 
1254f3215a60SKonstantin Belousov 	ref = 0;
1255f3215a60SKonstantin Belousov 	csw = devvn_refthread(vp, &dev, &ref);
1256f3215a60SKonstantin Belousov 	if (csw == NULL)
1257f3215a60SKonstantin Belousov 		return (ENXIO);
1258f743d981SAlexander Motin 
1259f743d981SAlexander Motin 	if ((csw->d_flags & D_DISK) == 0) {
1260f743d981SAlexander Motin 		error = -1;
1261f743d981SAlexander Motin 		goto unref;
1262f743d981SAlexander Motin 	}
1263022ca2fcSAlan Somers 	if (job->uiop->uio_resid > dev->si_iosize_max) {
1264f3215a60SKonstantin Belousov 		error = -1;
1265f3215a60SKonstantin Belousov 		goto unref;
1266f3215a60SKonstantin Belousov 	}
1267f3215a60SKonstantin Belousov 
1268f743d981SAlexander Motin 	ki = p->p_aioinfo;
1269022ca2fcSAlan Somers 	job->error = 0;
12704d805eacSJohn Baldwin 
1271022ca2fcSAlan Somers 	use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1272022ca2fcSAlan Somers 	if (!use_unmapped) {
1273022ca2fcSAlan Somers 		AIO_LOCK(ki);
1274022ca2fcSAlan Somers 		if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1275022ca2fcSAlan Somers 			AIO_UNLOCK(ki);
1276f54c5606SJohn Baldwin 			error = EAGAIN;
1277f743d981SAlexander Motin 			goto unref;
1278f743d981SAlexander Motin 		}
1279022ca2fcSAlan Somers 		ki->kaio_buffer_count += iovcnt;
1280022ca2fcSAlan Somers 		AIO_UNLOCK(ki);
1281022ca2fcSAlan Somers 	}
12824d805eacSJohn Baldwin 
1283022ca2fcSAlan Somers 	bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1284022ca2fcSAlan Somers 	atomic_store_int(&job->nbio, iovcnt);
1285022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1286022ca2fcSAlan Somers 		struct vm_page** pages;
1287022ca2fcSAlan Somers 		struct bio *bp;
1288022ca2fcSAlan Somers 		void *buf;
1289022ca2fcSAlan Somers 		size_t nbytes;
1290022ca2fcSAlan Somers 		int npages;
1291022ca2fcSAlan Somers 
1292022ca2fcSAlan Somers 		buf = job->uiop->uio_iov[i].iov_base;
1293022ca2fcSAlan Somers 		nbytes = job->uiop->uio_iov[i].iov_len;
1294022ca2fcSAlan Somers 
1295022ca2fcSAlan Somers 		bios[i] = g_alloc_bio();
1296022ca2fcSAlan Somers 		bp = bios[i];
1297022ca2fcSAlan Somers 
1298022ca2fcSAlan Somers 		poff = (vm_offset_t)buf & PAGE_MASK;
1299022ca2fcSAlan Somers 		if (use_unmapped) {
1300022ca2fcSAlan Somers 			pbuf = NULL;
1301022ca2fcSAlan Somers 			pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1302022ca2fcSAlan Somers 			    nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1303022ca2fcSAlan Somers 		} else {
130401206038SAlan Somers 			pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1305f743d981SAlexander Motin 			BUF_KERNPROC(pbuf);
130601206038SAlan Somers 			pages = pbuf->b_pages;
13074d805eacSJohn Baldwin 		}
13081ce91824SDavid Xu 
1309022ca2fcSAlan Somers 		bp->bio_length = nbytes;
1310022ca2fcSAlan Somers 		bp->bio_bcount = nbytes;
131172bce9ffSAlan Somers 		bp->bio_done = aio_biowakeup;
1312022ca2fcSAlan Somers 		bp->bio_offset = offset;
1313022ca2fcSAlan Somers 		bp->bio_cmd = bio_cmd;
1314f743d981SAlexander Motin 		bp->bio_dev = dev;
131501206038SAlan Somers 		bp->bio_caller1 = job;
131601206038SAlan Somers 		bp->bio_caller2 = pbuf;
1317f743d981SAlexander Motin 
1318f743d981SAlexander Motin 		prot = VM_PROT_READ;
1319022ca2fcSAlan Somers 		if (opcode == LIO_READ || opcode == LIO_READV)
1320f743d981SAlexander Motin 			prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
132101206038SAlan Somers 		npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1322022ca2fcSAlan Somers 		    (vm_offset_t)buf, bp->bio_length, prot, pages,
1323cd853791SKonstantin Belousov 		    atop(maxphys) + 1);
132401206038SAlan Somers 		if (npages < 0) {
1325022ca2fcSAlan Somers 			if (pbuf != NULL)
1326022ca2fcSAlan Somers 				uma_zfree(pbuf_zone, pbuf);
1327022ca2fcSAlan Somers 			else
1328022ca2fcSAlan Somers 				free(pages, M_TEMP);
1329f743d981SAlexander Motin 			error = EFAULT;
1330022ca2fcSAlan Somers 			g_destroy_bio(bp);
1331022ca2fcSAlan Somers 			i--;
1332022ca2fcSAlan Somers 			goto destroy_bios;
1333f743d981SAlexander Motin 		}
13344d805eacSJohn Baldwin 		if (pbuf != NULL) {
133501206038SAlan Somers 			pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1336f743d981SAlexander Motin 			bp->bio_data = pbuf->b_data + poff;
133701206038SAlan Somers 			pbuf->b_npages = npages;
1338022ca2fcSAlan Somers 			atomic_add_int(&num_buf_aio, 1);
1339f743d981SAlexander Motin 		} else {
134001206038SAlan Somers 			bp->bio_ma = pages;
134101206038SAlan Somers 			bp->bio_ma_n = npages;
1342f743d981SAlexander Motin 			bp->bio_ma_offset = poff;
1343f743d981SAlexander Motin 			bp->bio_data = unmapped_buf;
1344f743d981SAlexander Motin 			bp->bio_flags |= BIO_UNMAPPED;
13458091e52bSJohn Baldwin 			atomic_add_int(&num_unmapped_aio, 1);
1346f743d981SAlexander Motin 		}
1347f743d981SAlexander Motin 
1348022ca2fcSAlan Somers 		offset += nbytes;
1349022ca2fcSAlan Somers 	}
1350022ca2fcSAlan Somers 
1351bfbbc4aaSJason Evans 	/* Perform transfer. */
1352022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++)
1353022ca2fcSAlan Somers 		csw->d_strategy(bios[i]);
1354022ca2fcSAlan Somers 	free(bios, M_TEMP);
1355022ca2fcSAlan Somers 
1356f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1357ac41f2efSAlfred Perlstein 	return (0);
1358fd3bf775SJohn Dyson 
1359022ca2fcSAlan Somers destroy_bios:
1360022ca2fcSAlan Somers 	for (; i >= 0; i--)
1361022ca2fcSAlan Somers 		aio_biocleanup(bios[i]);
1362022ca2fcSAlan Somers 	free(bios, M_TEMP);
1363f3215a60SKonstantin Belousov unref:
1364f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1365fd3bf775SJohn Dyson 	return (error);
1366fd3bf775SJohn Dyson }
1367fd3bf775SJohn Dyson 
1368399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
13693858a1f4SJohn Baldwin static int
13703858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
13713858a1f4SJohn Baldwin {
13723858a1f4SJohn Baldwin 
13733858a1f4SJohn Baldwin 	/*
13743858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
13753858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
13763858a1f4SJohn Baldwin 	 */
13773858a1f4SJohn Baldwin 	nsig->sigev_notify = osig->sigev_notify;
13783858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
13793858a1f4SJohn Baldwin 	case SIGEV_NONE:
13803858a1f4SJohn Baldwin 		break;
13813858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
13823858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
13833858a1f4SJohn Baldwin 		break;
13843858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
13853858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
13863858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
13873858a1f4SJohn Baldwin 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
13883858a1f4SJohn Baldwin 		break;
13893858a1f4SJohn Baldwin 	default:
13903858a1f4SJohn Baldwin 		return (EINVAL);
13913858a1f4SJohn Baldwin 	}
13923858a1f4SJohn Baldwin 	return (0);
13933858a1f4SJohn Baldwin }
13943858a1f4SJohn Baldwin 
13953858a1f4SJohn Baldwin static int
1396022ca2fcSAlan Somers aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1397022ca2fcSAlan Somers     int type __unused)
13983858a1f4SJohn Baldwin {
13993858a1f4SJohn Baldwin 	struct oaiocb *ojob;
1400022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
14013858a1f4SJohn Baldwin 	int error;
14023858a1f4SJohn Baldwin 
1403022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
1404022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct oaiocb));
14053858a1f4SJohn Baldwin 	if (error)
14063858a1f4SJohn Baldwin 		return (error);
1407022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1408022ca2fcSAlan Somers 	ojob = (struct oaiocb *)kcb;
1409022ca2fcSAlan Somers 	return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
14103858a1f4SJohn Baldwin }
1411399e8c17SJohn Baldwin #endif
14123858a1f4SJohn Baldwin 
14133858a1f4SJohn Baldwin static int
1414022ca2fcSAlan Somers aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
14153858a1f4SJohn Baldwin {
1416022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
1417022ca2fcSAlan Somers 	int error;
14183858a1f4SJohn Baldwin 
1419022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct aiocb));
1420022ca2fcSAlan Somers 	if (error)
1421022ca2fcSAlan Somers 		return (error);
1422f30a1ae8SThomas Munro 	if (type == LIO_NOP)
1423f30a1ae8SThomas Munro 		type = kcb->aio_lio_opcode;
14242247f489SAlan Somers 	if (type & LIO_VECTORED) {
1425022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
1426022ca2fcSAlan Somers 		error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1427022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
1428022ca2fcSAlan Somers 	}
1429022ca2fcSAlan Somers 
1430022ca2fcSAlan Somers 	return (error);
14313858a1f4SJohn Baldwin }
14323858a1f4SJohn Baldwin 
14333858a1f4SJohn Baldwin static long
14343858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob)
14353858a1f4SJohn Baldwin {
14363858a1f4SJohn Baldwin 
14373858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.status));
14383858a1f4SJohn Baldwin }
14393858a1f4SJohn Baldwin 
14403858a1f4SJohn Baldwin static long
14413858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob)
14423858a1f4SJohn Baldwin {
14433858a1f4SJohn Baldwin 
14443858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.error));
14453858a1f4SJohn Baldwin }
14463858a1f4SJohn Baldwin 
14473858a1f4SJohn Baldwin static int
14483858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status)
14493858a1f4SJohn Baldwin {
14503858a1f4SJohn Baldwin 
14513858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.status, status));
14523858a1f4SJohn Baldwin }
14533858a1f4SJohn Baldwin 
14543858a1f4SJohn Baldwin static int
14553858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error)
14563858a1f4SJohn Baldwin {
14573858a1f4SJohn Baldwin 
14583858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.error, error));
14593858a1f4SJohn Baldwin }
14603858a1f4SJohn Baldwin 
14613858a1f4SJohn Baldwin static int
14623858a1f4SJohn Baldwin aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
14633858a1f4SJohn Baldwin {
14643858a1f4SJohn Baldwin 
14653858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
14663858a1f4SJohn Baldwin }
14673858a1f4SJohn Baldwin 
14683858a1f4SJohn Baldwin static int
14693858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
14703858a1f4SJohn Baldwin {
14713858a1f4SJohn Baldwin 
14723858a1f4SJohn Baldwin 	return (suword(ujobp, (long)ujob));
14733858a1f4SJohn Baldwin }
14743858a1f4SJohn Baldwin 
14753858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = {
1476849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin,
14773858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14783858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14793858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14803858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14813858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14823858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14833858a1f4SJohn Baldwin };
14843858a1f4SJohn Baldwin 
1485399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
14863858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = {
1487849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin_old_sigevent,
14883858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14893858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14903858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14913858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14923858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14933858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14943858a1f4SJohn Baldwin };
1495399e8c17SJohn Baldwin #endif
14963858a1f4SJohn Baldwin 
1497bfbbc4aaSJason Evans /*
149872bce9ffSAlan Somers  * Queue a new AIO request.  Choosing either the threaded or direct bio VCHR
1499bfbbc4aaSJason Evans  * technique is done in this code.
15002244ea07SJohn Dyson  */
15016a1162d4SAlexander Leidinger int
15025652770dSJohn Baldwin aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
15033858a1f4SJohn Baldwin     int type, struct aiocb_ops *ops)
1504fd3bf775SJohn Dyson {
1505b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1506022ca2fcSAlan Somers 	struct file *fp = NULL;
1507f3215338SJohn Baldwin 	struct kaiocb *job;
15082244ea07SJohn Dyson 	struct kaioinfo *ki;
1509c6fa9f78SAlan Cox 	struct kevent kev;
15101ce91824SDavid Xu 	int opcode;
15111ce91824SDavid Xu 	int error;
15124db71d27SJohn-Mark Gurney 	int fd, kqfd;
15131ce91824SDavid Xu 	int jid;
1514fde80935SDavid Xu 	u_short evflags;
15152244ea07SJohn Dyson 
1516a9bf5e37SDavid Xu 	if (p->p_aioinfo == NULL)
1517a9bf5e37SDavid Xu 		aio_init_aioinfo(p);
1518a9bf5e37SDavid Xu 
15191ce91824SDavid Xu 	ki = p->p_aioinfo;
15201ce91824SDavid Xu 
15215652770dSJohn Baldwin 	ops->store_status(ujob, -1);
15225652770dSJohn Baldwin 	ops->store_error(ujob, 0);
15235652770dSJohn Baldwin 	ops->store_kernelinfo(ujob, -1);
1524a9bf5e37SDavid Xu 
1525a9bf5e37SDavid Xu 	if (num_queue_count >= max_queue_count ||
152686bbef43SJohn Baldwin 	    ki->kaio_count >= max_aio_queue_per_proc) {
1527022ca2fcSAlan Somers 		error = EAGAIN;
1528022ca2fcSAlan Somers 		goto err1;
1529a9bf5e37SDavid Xu 	}
1530a9bf5e37SDavid Xu 
15315652770dSJohn Baldwin 	job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
15325652770dSJohn Baldwin 	knlist_init_mtx(&job->klist, AIO_MTX(ki));
1533fd3bf775SJohn Dyson 
1534022ca2fcSAlan Somers 	error = ops->aio_copyin(ujob, job, type);
1535022ca2fcSAlan Somers 	if (error)
1536022ca2fcSAlan Somers 		goto err2;
153768d71118SDavid Xu 
1538bb430bc7SJohn Baldwin 	if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1539022ca2fcSAlan Somers 		error = EINVAL;
1540022ca2fcSAlan Somers 		goto err2;
1541434ea137SGleb Smirnoff 	}
1542434ea137SGleb Smirnoff 
15435652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
15445652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
15455652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
15465652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1547022ca2fcSAlan Somers 		error = EINVAL;
1548022ca2fcSAlan Somers 		goto err2;
154968d71118SDavid Xu 	}
155068d71118SDavid Xu 
15515652770dSJohn Baldwin 	if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
15525652770dSJohn Baldwin 	     job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
15535652770dSJohn Baldwin 		!_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1554022ca2fcSAlan Somers 		error = EINVAL;
1555022ca2fcSAlan Somers 		goto err2;
15562f3cf918SAlfred Perlstein 	}
15572244ea07SJohn Dyson 
1558ff1a3078SAlan Somers 	/* Get the opcode. */
1559ff1a3078SAlan Somers 	if (type == LIO_NOP) {
1560ff1a3078SAlan Somers 		switch (job->uaiocb.aio_lio_opcode) {
1561ff1a3078SAlan Somers 		case LIO_WRITE:
1562f30a1ae8SThomas Munro 		case LIO_WRITEV:
1563ff1a3078SAlan Somers 		case LIO_NOP:
1564ff1a3078SAlan Somers 		case LIO_READ:
1565f30a1ae8SThomas Munro 		case LIO_READV:
1566ff1a3078SAlan Somers 			opcode = job->uaiocb.aio_lio_opcode;
1567ff1a3078SAlan Somers 			break;
1568ff1a3078SAlan Somers 		default:
1569ff1a3078SAlan Somers 			error = EINVAL;
1570ff1a3078SAlan Somers 			goto err2;
1571ff1a3078SAlan Somers 		}
1572ff1a3078SAlan Somers 	} else
1573ff1a3078SAlan Somers 		opcode = job->uaiocb.aio_lio_opcode = type;
1574ff1a3078SAlan Somers 
15755652770dSJohn Baldwin 	ksiginfo_init(&job->ksi);
15764c0fb2cfSDavid Xu 
1577bfbbc4aaSJason Evans 	/* Save userspace address of the job info. */
15785652770dSJohn Baldwin 	job->ujob = ujob;
157911783b14SJohn Dyson 
1580a9d2f8d8SRobert Watson 	/*
1581a9d2f8d8SRobert Watson 	 * Validate the opcode and fetch the file object for the specified
1582a9d2f8d8SRobert Watson 	 * file descriptor.
1583a9d2f8d8SRobert Watson 	 *
1584a9d2f8d8SRobert Watson 	 * XXXRW: Moved the opcode validation up here so that we don't
1585a9d2f8d8SRobert Watson 	 * retrieve a file descriptor without knowing what the capabiltity
1586a9d2f8d8SRobert Watson 	 * should be.
1587a9d2f8d8SRobert Watson 	 */
15885652770dSJohn Baldwin 	fd = job->uaiocb.aio_fildes;
15892a522eb9SJohn Baldwin 	switch (opcode) {
15902a522eb9SJohn Baldwin 	case LIO_WRITE:
1591022ca2fcSAlan Somers 	case LIO_WRITEV:
1592cbd92ce6SMatt Macy 		error = fget_write(td, fd, &cap_pwrite_rights, &fp);
15932a522eb9SJohn Baldwin 		break;
15942a522eb9SJohn Baldwin 	case LIO_READ:
1595022ca2fcSAlan Somers 	case LIO_READV:
1596cbd92ce6SMatt Macy 		error = fget_read(td, fd, &cap_pread_rights, &fp);
1597a9d2f8d8SRobert Watson 		break;
1598a9d2f8d8SRobert Watson 	case LIO_SYNC:
1599801ac943SThomas Munro 	case LIO_DSYNC:
1600cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_fsync_rights, &fp);
1601a9d2f8d8SRobert Watson 		break;
16026160e12cSGleb Smirnoff 	case LIO_MLOCK:
16036160e12cSGleb Smirnoff 		break;
1604a9d2f8d8SRobert Watson 	case LIO_NOP:
1605cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_no_rights, &fp);
16062a522eb9SJohn Baldwin 		break;
16072a522eb9SJohn Baldwin 	default:
1608a9d2f8d8SRobert Watson 		error = EINVAL;
16092a522eb9SJohn Baldwin 	}
1610022ca2fcSAlan Somers 	if (error)
1611022ca2fcSAlan Somers 		goto err3;
161299eee864SDavid Xu 
16132247f489SAlan Somers 	if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
161499eee864SDavid Xu 		error = EINVAL;
1615022ca2fcSAlan Somers 		goto err3;
161699eee864SDavid Xu 	}
16172244ea07SJohn Dyson 
1618022ca2fcSAlan Somers 	if ((opcode == LIO_READ || opcode == LIO_READV ||
1619022ca2fcSAlan Somers 	    opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1620711dba24SKonstantin Belousov 	    job->uaiocb.aio_offset < 0 &&
1621711dba24SKonstantin Belousov 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1622ae124fc4SAlan Cox 		error = EINVAL;
1623022ca2fcSAlan Somers 		goto err3;
16242244ea07SJohn Dyson 	}
16251ce91824SDavid Xu 
16268d9ed174SKonstantin Belousov 	if (fp != NULL && fp->f_ops == &path_fileops) {
16278d9ed174SKonstantin Belousov 		error = EBADF;
16288d9ed174SKonstantin Belousov 		goto err3;
16298d9ed174SKonstantin Belousov 	}
16308d9ed174SKonstantin Belousov 
16315652770dSJohn Baldwin 	job->fd_file = fp;
16321ce91824SDavid Xu 
163399eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
163499eee864SDavid Xu 	jid = jobrefid++;
16355652770dSJohn Baldwin 	job->seqno = jobseqno++;
163699eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
16375652770dSJohn Baldwin 	error = ops->store_kernelinfo(ujob, jid);
16381ce91824SDavid Xu 	if (error) {
16391ce91824SDavid Xu 		error = EINVAL;
1640022ca2fcSAlan Somers 		goto err3;
16411ce91824SDavid Xu 	}
16425652770dSJohn Baldwin 	job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
16432244ea07SJohn Dyson 
16442244ea07SJohn Dyson 	if (opcode == LIO_NOP) {
1645a5c0b1c0SAlan Cox 		fdrop(fp, td);
1646022ca2fcSAlan Somers 		MPASS(job->uiop == &job->uio || job->uiop == NULL);
16475652770dSJohn Baldwin 		uma_zfree(aiocb_zone, job);
1648ac41f2efSAlfred Perlstein 		return (0);
16492244ea07SJohn Dyson 	}
16502244ea07SJohn Dyson 
16515652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1652cb679c38SJonathan Lemon 		goto no_kqueue;
16535652770dSJohn Baldwin 	evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1654fde80935SDavid Xu 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1655fde80935SDavid Xu 		error = EINVAL;
1656022ca2fcSAlan Somers 		goto err3;
1657fde80935SDavid Xu 	}
16585652770dSJohn Baldwin 	kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
165936c4960eSMark Johnston 	memset(&kev, 0, sizeof(kev));
16605652770dSJohn Baldwin 	kev.ident = (uintptr_t)job->ujob;
1661cb679c38SJonathan Lemon 	kev.filter = EVFILT_AIO;
1662fde80935SDavid Xu 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
16635652770dSJohn Baldwin 	kev.data = (intptr_t)job;
16645652770dSJohn Baldwin 	kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1665792843c3SMark Johnston 	error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1666f3215338SJohn Baldwin 	if (error)
1667022ca2fcSAlan Somers 		goto err3;
1668f3215338SJohn Baldwin 
1669cb679c38SJonathan Lemon no_kqueue:
1670cb679c38SJonathan Lemon 
16715652770dSJohn Baldwin 	ops->store_error(ujob, EINPROGRESS);
16725652770dSJohn Baldwin 	job->uaiocb._aiocb_private.error = EINPROGRESS;
16735652770dSJohn Baldwin 	job->userproc = p;
16745652770dSJohn Baldwin 	job->cred = crhold(td->td_ucred);
1675f3215338SJohn Baldwin 	job->jobflags = KAIOCB_QUEUEING;
16765652770dSJohn Baldwin 	job->lio = lj;
16772244ea07SJohn Dyson 
16782247f489SAlan Somers 	if (opcode & LIO_VECTORED) {
1679022ca2fcSAlan Somers 		/* Use the uio copied in by aio_copyin */
1680022ca2fcSAlan Somers 		MPASS(job->uiop != &job->uio && job->uiop != NULL);
16812247f489SAlan Somers 	} else {
1682022ca2fcSAlan Somers 		/* Setup the inline uio */
1683022ca2fcSAlan Somers 		job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1684022ca2fcSAlan Somers 		job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1685022ca2fcSAlan Somers 		job->uio.uio_iov = job->iov;
1686022ca2fcSAlan Somers 		job->uio.uio_iovcnt = 1;
1687022ca2fcSAlan Somers 		job->uio.uio_resid = job->uaiocb.aio_nbytes;
1688022ca2fcSAlan Somers 		job->uio.uio_segflg = UIO_USERSPACE;
1689022ca2fcSAlan Somers 		job->uiop = &job->uio;
1690022ca2fcSAlan Somers 	}
16912247f489SAlan Somers 	switch (opcode & (LIO_READ | LIO_WRITE)) {
1692022ca2fcSAlan Somers 	case LIO_READ:
1693022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_READ;
1694022ca2fcSAlan Somers 		break;
1695022ca2fcSAlan Somers 	case LIO_WRITE:
1696022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_WRITE;
1697022ca2fcSAlan Somers 		break;
1698022ca2fcSAlan Somers 	}
1699022ca2fcSAlan Somers 	job->uiop->uio_offset = job->uaiocb.aio_offset;
1700022ca2fcSAlan Somers 	job->uiop->uio_td = td;
1701022ca2fcSAlan Somers 
1702f3215338SJohn Baldwin 	if (opcode == LIO_MLOCK) {
1703f3215338SJohn Baldwin 		aio_schedule(job, aio_process_mlock);
1704f3215338SJohn Baldwin 		error = 0;
1705f3215338SJohn Baldwin 	} else if (fp->f_ops->fo_aio_queue == NULL)
1706f3215338SJohn Baldwin 		error = aio_queue_file(fp, job);
1707f3215338SJohn Baldwin 	else
1708f3215338SJohn Baldwin 		error = fo_aio_queue(fp, job);
1709f3215338SJohn Baldwin 	if (error)
171045c2c7c4SKonstantin Belousov 		goto err4;
1711f3215338SJohn Baldwin 
1712f3215338SJohn Baldwin 	AIO_LOCK(ki);
1713f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_QUEUEING;
1714f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1715f3215338SJohn Baldwin 	ki->kaio_count++;
1716f3215338SJohn Baldwin 	if (lj)
1717f3215338SJohn Baldwin 		lj->lioj_count++;
1718f3215338SJohn Baldwin 	atomic_add_int(&num_queue_count, 1);
1719f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
1720f3215338SJohn Baldwin 		/*
1721f3215338SJohn Baldwin 		 * The queue callback completed the request synchronously.
1722f3215338SJohn Baldwin 		 * The bulk of the completion is deferred in that case
1723f3215338SJohn Baldwin 		 * until this point.
1724f3215338SJohn Baldwin 		 */
1725f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
1726f3215338SJohn Baldwin 	} else
1727f3215338SJohn Baldwin 		TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1728f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1729f3215338SJohn Baldwin 	return (0);
1730f3215338SJohn Baldwin 
173145c2c7c4SKonstantin Belousov err4:
173245c2c7c4SKonstantin Belousov 	crfree(job->cred);
1733022ca2fcSAlan Somers err3:
1734f3215338SJohn Baldwin 	if (fp)
1735f3215338SJohn Baldwin 		fdrop(fp, td);
1736022ca2fcSAlan Somers 	knlist_delete(&job->klist, curthread, 0);
1737022ca2fcSAlan Somers err2:
1738022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
1739022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
1740f3215338SJohn Baldwin 	uma_zfree(aiocb_zone, job);
1741022ca2fcSAlan Somers err1:
1742f3215338SJohn Baldwin 	ops->store_error(ujob, error);
1743f3215338SJohn Baldwin 	return (error);
1744f3215338SJohn Baldwin }
1745f3215338SJohn Baldwin 
1746f3215338SJohn Baldwin static void
1747f3215338SJohn Baldwin aio_cancel_daemon_job(struct kaiocb *job)
1748f3215338SJohn Baldwin {
1749f3215338SJohn Baldwin 
1750f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1751f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1752f3215338SJohn Baldwin 		TAILQ_REMOVE(&aio_jobs, job, list);
1753f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1754f3215338SJohn Baldwin 	aio_cancel(job);
1755f3215338SJohn Baldwin }
1756f3215338SJohn Baldwin 
1757f3215338SJohn Baldwin void
1758f3215338SJohn Baldwin aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1759f3215338SJohn Baldwin {
1760f3215338SJohn Baldwin 
1761f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1762f3215338SJohn Baldwin 	if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1763f3215338SJohn Baldwin 		mtx_unlock(&aio_job_mtx);
1764f3215338SJohn Baldwin 		aio_cancel(job);
1765f3215338SJohn Baldwin 		return;
1766f3215338SJohn Baldwin 	}
1767f3215338SJohn Baldwin 	job->handle_fn = func;
1768f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1769f3215338SJohn Baldwin 	aio_kick_nowait(job->userproc);
1770f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1771f3215338SJohn Baldwin }
1772f3215338SJohn Baldwin 
1773f3215338SJohn Baldwin static void
1774f3215338SJohn Baldwin aio_cancel_sync(struct kaiocb *job)
1775f3215338SJohn Baldwin {
1776f3215338SJohn Baldwin 	struct kaioinfo *ki;
1777f3215338SJohn Baldwin 
1778f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1779005ce8e4SJohn Baldwin 	AIO_LOCK(ki);
1780f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1781f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1782005ce8e4SJohn Baldwin 	AIO_UNLOCK(ki);
1783f3215338SJohn Baldwin 	aio_cancel(job);
1784f3215338SJohn Baldwin }
1785f3215338SJohn Baldwin 
1786f3215338SJohn Baldwin int
1787f3215338SJohn Baldwin aio_queue_file(struct file *fp, struct kaiocb *job)
1788f3215338SJohn Baldwin {
1789f3215338SJohn Baldwin 	struct kaioinfo *ki;
1790f3215338SJohn Baldwin 	struct kaiocb *job2;
17919fe297bbSKonstantin Belousov 	struct vnode *vp;
17929fe297bbSKonstantin Belousov 	struct mount *mp;
1793f54c5606SJohn Baldwin 	int error;
17949fe297bbSKonstantin Belousov 	bool safe;
1795f3215338SJohn Baldwin 
1796f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
179772bce9ffSAlan Somers 	error = aio_qbio(job->userproc, job);
1798f54c5606SJohn Baldwin 	if (error >= 0)
1799f54c5606SJohn Baldwin 		return (error);
18009fe297bbSKonstantin Belousov 	safe = false;
18019fe297bbSKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE) {
18029fe297bbSKonstantin Belousov 		vp = fp->f_vnode;
18039fe297bbSKonstantin Belousov 		if (vp->v_type == VREG || vp->v_type == VDIR) {
18049fe297bbSKonstantin Belousov 			mp = fp->f_vnode->v_mount;
18059fe297bbSKonstantin Belousov 			if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
18069fe297bbSKonstantin Belousov 				safe = true;
18079fe297bbSKonstantin Belousov 		}
18089fe297bbSKonstantin Belousov 	}
18099c20dc99SJohn Baldwin 	if (!(safe || enable_aio_unsafe)) {
18109c20dc99SJohn Baldwin 		counted_warning(&unsafe_warningcnt,
18119c20dc99SJohn Baldwin 		    "is attempting to use unsafe AIO requests");
1812f3215338SJohn Baldwin 		return (EOPNOTSUPP);
18139c20dc99SJohn Baldwin 	}
181484af4da6SJohn Dyson 
18152247f489SAlan Somers 	if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
18167e409184SJohn Baldwin 		aio_schedule(job, aio_process_rw);
18177e409184SJohn Baldwin 		error = 0;
18182247f489SAlan Somers 	} else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1819f3215338SJohn Baldwin 		AIO_LOCK(ki);
18205652770dSJohn Baldwin 		TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
18215652770dSJohn Baldwin 			if (job2->fd_file == job->fd_file &&
18222247f489SAlan Somers 			    ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
18235652770dSJohn Baldwin 			    job2->seqno < job->seqno) {
18245652770dSJohn Baldwin 				job2->jobflags |= KAIOCB_CHECKSYNC;
18255652770dSJohn Baldwin 				job->pending++;
1826dbbccfe9SDavid Xu 			}
1827dbbccfe9SDavid Xu 		}
18285652770dSJohn Baldwin 		if (job->pending != 0) {
1829005ce8e4SJohn Baldwin 			if (!aio_set_cancel_function_locked(job,
1830005ce8e4SJohn Baldwin 				aio_cancel_sync)) {
1831f3215338SJohn Baldwin 				AIO_UNLOCK(ki);
1832f3215338SJohn Baldwin 				aio_cancel(job);
1833f3215338SJohn Baldwin 				return (0);
1834f3215338SJohn Baldwin 			}
18355652770dSJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1836759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1837f3215338SJohn Baldwin 			return (0);
1838dbbccfe9SDavid Xu 		}
1839759ccccaSDavid Xu 		AIO_UNLOCK(ki);
1840f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
1841f3215338SJohn Baldwin 		error = 0;
18422247f489SAlan Somers 	} else {
1843f3215338SJohn Baldwin 		error = EINVAL;
1844f3215338SJohn Baldwin 	}
184599eee864SDavid Xu 	return (error);
184699eee864SDavid Xu }
184799eee864SDavid Xu 
184899eee864SDavid Xu static void
184999eee864SDavid Xu aio_kick_nowait(struct proc *userp)
185099eee864SDavid Xu {
185199eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
185239314b7dSJohn Baldwin 	struct aioproc *aiop;
185399eee864SDavid Xu 
185499eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
185599eee864SDavid Xu 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
185699eee864SDavid Xu 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
185739314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
185839314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18590dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
186086bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1861c85650caSJohn Baldwin 		taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
186299eee864SDavid Xu 	}
186399eee864SDavid Xu }
186499eee864SDavid Xu 
1865dbbccfe9SDavid Xu static int
186699eee864SDavid Xu aio_kick(struct proc *userp)
186799eee864SDavid Xu {
186899eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
186939314b7dSJohn Baldwin 	struct aioproc *aiop;
1870dbbccfe9SDavid Xu 	int error, ret = 0;
187199eee864SDavid Xu 
187299eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
187399eee864SDavid Xu retryproc:
1874d254af07SMatthew Dillon 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
18752244ea07SJohn Dyson 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
187639314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
187739314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18780dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
187986bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1880fd3bf775SJohn Dyson 		num_aio_resv_start++;
18811ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
18821ce91824SDavid Xu 		error = aio_newproc(&num_aio_resv_start);
18831ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
18841ce91824SDavid Xu 		if (error) {
188584af4da6SJohn Dyson 			num_aio_resv_start--;
18862244ea07SJohn Dyson 			goto retryproc;
1887fd3bf775SJohn Dyson 		}
1888dbbccfe9SDavid Xu 	} else {
1889dbbccfe9SDavid Xu 		ret = -1;
18901ce91824SDavid Xu 	}
1891dbbccfe9SDavid Xu 	return (ret);
189299eee864SDavid Xu }
18931ce91824SDavid Xu 
189499eee864SDavid Xu static void
189599eee864SDavid Xu aio_kick_helper(void *context, int pending)
189699eee864SDavid Xu {
189799eee864SDavid Xu 	struct proc *userp = context;
189899eee864SDavid Xu 
189999eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
1900dbbccfe9SDavid Xu 	while (--pending >= 0) {
1901dbbccfe9SDavid Xu 		if (aio_kick(userp))
1902dbbccfe9SDavid Xu 			break;
1903dbbccfe9SDavid Xu 	}
190499eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
19052244ea07SJohn Dyson }
19062244ea07SJohn Dyson 
1907fd3bf775SJohn Dyson /*
1908bfbbc4aaSJason Evans  * Support the aio_return system call, as a side-effect, kernel resources are
1909bfbbc4aaSJason Evans  * released.
19102244ea07SJohn Dyson  */
19113858a1f4SJohn Baldwin static int
19125652770dSJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1913fd3bf775SJohn Dyson {
1914b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19155652770dSJohn Baldwin 	struct kaiocb *job;
19162244ea07SJohn Dyson 	struct kaioinfo *ki;
1917bb430bc7SJohn Baldwin 	long status, error;
19182244ea07SJohn Dyson 
1919c0bf5caaSAlan Cox 	ki = p->p_aioinfo;
1920c0bf5caaSAlan Cox 	if (ki == NULL)
1921ac41f2efSAlfred Perlstein 		return (EINVAL);
1922759ccccaSDavid Xu 	AIO_LOCK(ki);
19235652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_done, plist) {
19245652770dSJohn Baldwin 		if (job->ujob == ujob)
1925c0bf5caaSAlan Cox 			break;
1926c0bf5caaSAlan Cox 	}
19275652770dSJohn Baldwin 	if (job != NULL) {
1928f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
19295652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
19305652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
19311ce91824SDavid Xu 		td->td_retval[0] = status;
1932b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
1933b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
1934b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
1935b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
19365652770dSJohn Baldwin 		aio_free_entry(job);
1937759ccccaSDavid Xu 		AIO_UNLOCK(ki);
19385652770dSJohn Baldwin 		ops->store_error(ujob, error);
19395652770dSJohn Baldwin 		ops->store_status(ujob, status);
194055a122bfSDavid Xu 	} else {
19411ce91824SDavid Xu 		error = EINVAL;
1942759ccccaSDavid Xu 		AIO_UNLOCK(ki);
194355a122bfSDavid Xu 	}
19441ce91824SDavid Xu 	return (error);
19452244ea07SJohn Dyson }
19462244ea07SJohn Dyson 
19473858a1f4SJohn Baldwin int
19488451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap)
19493858a1f4SJohn Baldwin {
19503858a1f4SJohn Baldwin 
19513858a1f4SJohn Baldwin 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
19523858a1f4SJohn Baldwin }
19533858a1f4SJohn Baldwin 
19542244ea07SJohn Dyson /*
1955bfbbc4aaSJason Evans  * Allow a process to wakeup when any of the I/O requests are completed.
19562244ea07SJohn Dyson  */
19573858a1f4SJohn Baldwin static int
19583858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
19593858a1f4SJohn Baldwin     struct timespec *ts)
1960fd3bf775SJohn Dyson {
1961b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19624a11ca4eSPoul-Henning Kamp 	struct timeval atv;
19632244ea07SJohn Dyson 	struct kaioinfo *ki;
19645652770dSJohn Baldwin 	struct kaiocb *firstjob, *job;
19653858a1f4SJohn Baldwin 	int error, i, timo;
19662244ea07SJohn Dyson 
19672244ea07SJohn Dyson 	timo = 0;
19683858a1f4SJohn Baldwin 	if (ts) {
19693858a1f4SJohn Baldwin 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
19702244ea07SJohn Dyson 			return (EINVAL);
19712244ea07SJohn Dyson 
19723858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
19732244ea07SJohn Dyson 		if (itimerfix(&atv))
19742244ea07SJohn Dyson 			return (EINVAL);
1975227ee8a1SPoul-Henning Kamp 		timo = tvtohz(&atv);
19762244ea07SJohn Dyson 	}
19772244ea07SJohn Dyson 
19782244ea07SJohn Dyson 	ki = p->p_aioinfo;
19792244ea07SJohn Dyson 	if (ki == NULL)
1980ac41f2efSAlfred Perlstein 		return (EAGAIN);
19812244ea07SJohn Dyson 
19823858a1f4SJohn Baldwin 	if (njoblist == 0)
1983ac41f2efSAlfred Perlstein 		return (0);
19842244ea07SJohn Dyson 
1985759ccccaSDavid Xu 	AIO_LOCK(ki);
19861ce91824SDavid Xu 	for (;;) {
19875652770dSJohn Baldwin 		firstjob = NULL;
19881ce91824SDavid Xu 		error = 0;
19895652770dSJohn Baldwin 		TAILQ_FOREACH(job, &ki->kaio_all, allist) {
199084af4da6SJohn Dyson 			for (i = 0; i < njoblist; i++) {
19915652770dSJohn Baldwin 				if (job->ujob == ujoblist[i]) {
19925652770dSJohn Baldwin 					if (firstjob == NULL)
19935652770dSJohn Baldwin 						firstjob = job;
1994f3215338SJohn Baldwin 					if (job->jobflags & KAIOCB_FINISHED)
19951ce91824SDavid Xu 						goto RETURN;
199684af4da6SJohn Dyson 				}
199784af4da6SJohn Dyson 			}
199884af4da6SJohn Dyson 		}
19991ce91824SDavid Xu 		/* All tasks were finished. */
20005652770dSJohn Baldwin 		if (firstjob == NULL)
20011ce91824SDavid Xu 			break;
20022244ea07SJohn Dyson 
2003fd3bf775SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
2004759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
20051ce91824SDavid Xu 		    "aiospn", timo);
20061ce91824SDavid Xu 		if (error == ERESTART)
20071ce91824SDavid Xu 			error = EINTR;
20081ce91824SDavid Xu 		if (error)
20091ce91824SDavid Xu 			break;
20102244ea07SJohn Dyson 	}
20111ce91824SDavid Xu RETURN:
2012759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20133858a1f4SJohn Baldwin 	return (error);
20143858a1f4SJohn Baldwin }
20153858a1f4SJohn Baldwin 
20163858a1f4SJohn Baldwin int
20178451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
20183858a1f4SJohn Baldwin {
20193858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
20203858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
20213858a1f4SJohn Baldwin 	int error;
20223858a1f4SJohn Baldwin 
2023913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
20243858a1f4SJohn Baldwin 		return (EINVAL);
20253858a1f4SJohn Baldwin 
20263858a1f4SJohn Baldwin 	if (uap->timeout) {
20273858a1f4SJohn Baldwin 		/* Get timespec struct. */
20283858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
20293858a1f4SJohn Baldwin 			return (error);
20303858a1f4SJohn Baldwin 		tsp = &ts;
20313858a1f4SJohn Baldwin 	} else
20323858a1f4SJohn Baldwin 		tsp = NULL;
20333858a1f4SJohn Baldwin 
20349553bc89SMark Johnston 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
20353858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
20363858a1f4SJohn Baldwin 	if (error == 0)
20373858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
20389553bc89SMark Johnston 	free(ujoblist, M_AIO);
20391ce91824SDavid Xu 	return (error);
20402244ea07SJohn Dyson }
2041ee877a35SJohn Dyson 
2042ee877a35SJohn Dyson /*
204372bce9ffSAlan Somers  * aio_cancel cancels any non-bio aio operations not currently in progress.
2044ee877a35SJohn Dyson  */
2045ee877a35SJohn Dyson int
20468451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2047fd3bf775SJohn Dyson {
2048b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2049dd85920aSJason Evans 	struct kaioinfo *ki;
20505652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
2051dd85920aSJason Evans 	struct file *fp;
20521ce91824SDavid Xu 	int error;
2053dd85920aSJason Evans 	int cancelled = 0;
2054dd85920aSJason Evans 	int notcancelled = 0;
2055dd85920aSJason Evans 	struct vnode *vp;
2056dd85920aSJason Evans 
20572a522eb9SJohn Baldwin 	/* Lookup file object. */
2058cbd92ce6SMatt Macy 	error = fget(td, uap->fd, &cap_no_rights, &fp);
20592a522eb9SJohn Baldwin 	if (error)
20602a522eb9SJohn Baldwin 		return (error);
2061dd85920aSJason Evans 
20621ce91824SDavid Xu 	ki = p->p_aioinfo;
20631ce91824SDavid Xu 	if (ki == NULL)
20641ce91824SDavid Xu 		goto done;
20651ce91824SDavid Xu 
2066dd85920aSJason Evans 	if (fp->f_type == DTYPE_VNODE) {
20673b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
20687ad2a82dSMateusz Guzik 		if (vn_isdisk(vp)) {
20692a522eb9SJohn Baldwin 			fdrop(fp, td);
2070b40ce416SJulian Elischer 			td->td_retval[0] = AIO_NOTCANCELED;
2071ac41f2efSAlfred Perlstein 			return (0);
2072dd85920aSJason Evans 		}
2073dd85920aSJason Evans 	}
2074dd85920aSJason Evans 
2075759ccccaSDavid Xu 	AIO_LOCK(ki);
20765652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
20775652770dSJohn Baldwin 		if ((uap->fd == job->uaiocb.aio_fildes) &&
2078dd85920aSJason Evans 		    ((uap->aiocbp == NULL) ||
20795652770dSJohn Baldwin 		     (uap->aiocbp == job->ujob))) {
2080f3215338SJohn Baldwin 			if (aio_cancel_job(p, ki, job)) {
20811ce91824SDavid Xu 				cancelled++;
2082dd85920aSJason Evans 			} else {
2083dd85920aSJason Evans 				notcancelled++;
2084dd85920aSJason Evans 			}
20851aa4c324SDavid Xu 			if (uap->aiocbp != NULL)
20861aa4c324SDavid Xu 				break;
2087dd85920aSJason Evans 		}
2088dd85920aSJason Evans 	}
2089759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20901ce91824SDavid Xu 
2091ad49abc0SAlan Cox done:
20922a522eb9SJohn Baldwin 	fdrop(fp, td);
20931aa4c324SDavid Xu 
20941aa4c324SDavid Xu 	if (uap->aiocbp != NULL) {
2095dd85920aSJason Evans 		if (cancelled) {
2096b40ce416SJulian Elischer 			td->td_retval[0] = AIO_CANCELED;
2097ac41f2efSAlfred Perlstein 			return (0);
2098dd85920aSJason Evans 		}
20991aa4c324SDavid Xu 	}
21001aa4c324SDavid Xu 
21011aa4c324SDavid Xu 	if (notcancelled) {
21021aa4c324SDavid Xu 		td->td_retval[0] = AIO_NOTCANCELED;
21031aa4c324SDavid Xu 		return (0);
21041aa4c324SDavid Xu 	}
21051aa4c324SDavid Xu 
21061aa4c324SDavid Xu 	if (cancelled) {
21071aa4c324SDavid Xu 		td->td_retval[0] = AIO_CANCELED;
21081aa4c324SDavid Xu 		return (0);
21091aa4c324SDavid Xu 	}
21101aa4c324SDavid Xu 
2111b40ce416SJulian Elischer 	td->td_retval[0] = AIO_ALLDONE;
2112dd85920aSJason Evans 
2113ac41f2efSAlfred Perlstein 	return (0);
2114ee877a35SJohn Dyson }
2115ee877a35SJohn Dyson 
2116ee877a35SJohn Dyson /*
2117873fbcd7SRobert Watson  * aio_error is implemented in the kernel level for compatibility purposes
2118873fbcd7SRobert Watson  * only.  For a user mode async implementation, it would be best to do it in
2119873fbcd7SRobert Watson  * a userland subroutine.
2120ee877a35SJohn Dyson  */
21213858a1f4SJohn Baldwin static int
21225652770dSJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2123fd3bf775SJohn Dyson {
2124b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
21255652770dSJohn Baldwin 	struct kaiocb *job;
21262244ea07SJohn Dyson 	struct kaioinfo *ki;
21271ce91824SDavid Xu 	int status;
2128ee877a35SJohn Dyson 
21292244ea07SJohn Dyson 	ki = p->p_aioinfo;
21301ce91824SDavid Xu 	if (ki == NULL) {
21311ce91824SDavid Xu 		td->td_retval[0] = EINVAL;
21321ce91824SDavid Xu 		return (0);
21331ce91824SDavid Xu 	}
2134ee877a35SJohn Dyson 
2135759ccccaSDavid Xu 	AIO_LOCK(ki);
21365652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_all, allist) {
21375652770dSJohn Baldwin 		if (job->ujob == ujob) {
2138f3215338SJohn Baldwin 			if (job->jobflags & KAIOCB_FINISHED)
21391ce91824SDavid Xu 				td->td_retval[0] =
21405652770dSJohn Baldwin 					job->uaiocb._aiocb_private.error;
21411ce91824SDavid Xu 			else
2142b40ce416SJulian Elischer 				td->td_retval[0] = EINPROGRESS;
2143759ccccaSDavid Xu 			AIO_UNLOCK(ki);
2144ac41f2efSAlfred Perlstein 			return (0);
21452244ea07SJohn Dyson 		}
21462244ea07SJohn Dyson 	}
2147759ccccaSDavid Xu 	AIO_UNLOCK(ki);
214884af4da6SJohn Dyson 
21492244ea07SJohn Dyson 	/*
2150a9bf5e37SDavid Xu 	 * Hack for failure of aio_aqueue.
21512244ea07SJohn Dyson 	 */
21525652770dSJohn Baldwin 	status = ops->fetch_status(ujob);
21531ce91824SDavid Xu 	if (status == -1) {
21545652770dSJohn Baldwin 		td->td_retval[0] = ops->fetch_error(ujob);
21551ce91824SDavid Xu 		return (0);
21561ce91824SDavid Xu 	}
21571ce91824SDavid Xu 
21581ce91824SDavid Xu 	td->td_retval[0] = EINVAL;
21591ce91824SDavid Xu 	return (0);
2160ee877a35SJohn Dyson }
2161ee877a35SJohn Dyson 
21623858a1f4SJohn Baldwin int
21638451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap)
21643858a1f4SJohn Baldwin {
21653858a1f4SJohn Baldwin 
21663858a1f4SJohn Baldwin 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
21673858a1f4SJohn Baldwin }
21683858a1f4SJohn Baldwin 
2169eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */
2170399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2171ee877a35SJohn Dyson int
2172399e8c17SJohn Baldwin freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
21730972628aSDavid Xu {
21740972628aSDavid Xu 
21753858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
21763858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
21770972628aSDavid Xu }
2178399e8c17SJohn Baldwin #endif
21790972628aSDavid Xu 
21800972628aSDavid Xu int
21818451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap)
2182fd3bf775SJohn Dyson {
218321d56e9cSAlfred Perlstein 
21843858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2185ee877a35SJohn Dyson }
2186ee877a35SJohn Dyson 
2187022ca2fcSAlan Somers int
2188022ca2fcSAlan Somers sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2189022ca2fcSAlan Somers {
2190022ca2fcSAlan Somers 
2191022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2192022ca2fcSAlan Somers }
2193022ca2fcSAlan Somers 
2194eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */
2195399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2196ee877a35SJohn Dyson int
2197399e8c17SJohn Baldwin freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
21980972628aSDavid Xu {
21990972628aSDavid Xu 
22003858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
22013858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
22020972628aSDavid Xu }
2203399e8c17SJohn Baldwin #endif
22040972628aSDavid Xu 
22050972628aSDavid Xu int
22068451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap)
2207fd3bf775SJohn Dyson {
220821d56e9cSAlfred Perlstein 
22093858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
22100972628aSDavid Xu }
22110972628aSDavid Xu 
22126160e12cSGleb Smirnoff int
2213022ca2fcSAlan Somers sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2214022ca2fcSAlan Somers {
2215022ca2fcSAlan Somers 
2216022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2217022ca2fcSAlan Somers }
2218022ca2fcSAlan Somers 
2219022ca2fcSAlan Somers int
22206160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
22216160e12cSGleb Smirnoff {
22226160e12cSGleb Smirnoff 
22236160e12cSGleb Smirnoff 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
22246160e12cSGleb Smirnoff }
22256160e12cSGleb Smirnoff 
22260972628aSDavid Xu static int
22273858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
22283858a1f4SJohn Baldwin     struct aiocb **acb_list, int nent, struct sigevent *sig,
22293858a1f4SJohn Baldwin     struct aiocb_ops *ops)
22300972628aSDavid Xu {
2231b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22325652770dSJohn Baldwin 	struct aiocb *job;
22332244ea07SJohn Dyson 	struct kaioinfo *ki;
22341ce91824SDavid Xu 	struct aioliojob *lj;
223569cd28daSDoug Ambrisko 	struct kevent kev;
22361ce91824SDavid Xu 	int error;
223752c09831SAlan Somers 	int nagain, nerror;
2238ee877a35SJohn Dyson 	int i;
2239ee877a35SJohn Dyson 
22403858a1f4SJohn Baldwin 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2241ac41f2efSAlfred Perlstein 		return (EINVAL);
22422244ea07SJohn Dyson 
2243913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
2244ac41f2efSAlfred Perlstein 		return (EINVAL);
22452244ea07SJohn Dyson 
2246bfbbc4aaSJason Evans 	if (p->p_aioinfo == NULL)
22472244ea07SJohn Dyson 		aio_init_aioinfo(p);
22482244ea07SJohn Dyson 
22492244ea07SJohn Dyson 	ki = p->p_aioinfo;
22502244ea07SJohn Dyson 
2251a163d034SWarner Losh 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
225284af4da6SJohn Dyson 	lj->lioj_flags = 0;
22531ce91824SDavid Xu 	lj->lioj_count = 0;
22541ce91824SDavid Xu 	lj->lioj_finished_count = 0;
22552e5f6152SMark Johnston 	lj->lioj_signal.sigev_notify = SIGEV_NONE;
2256d8b0556cSKonstantin Belousov 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
22574c0fb2cfSDavid Xu 	ksiginfo_init(&lj->lioj_ksi);
225869cd28daSDoug Ambrisko 
225984af4da6SJohn Dyson 	/*
2260bfbbc4aaSJason Evans 	 * Setup signal.
226184af4da6SJohn Dyson 	 */
22623858a1f4SJohn Baldwin 	if (sig && (mode == LIO_NOWAIT)) {
22633858a1f4SJohn Baldwin 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
226469cd28daSDoug Ambrisko 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
226569cd28daSDoug Ambrisko 			/* Assume only new style KEVENT */
226636c4960eSMark Johnston 			memset(&kev, 0, sizeof(kev));
226769cd28daSDoug Ambrisko 			kev.filter = EVFILT_LIO;
226869cd28daSDoug Ambrisko 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
22693858a1f4SJohn Baldwin 			kev.ident = (uintptr_t)uacb_list; /* something unique */
227069cd28daSDoug Ambrisko 			kev.data = (intptr_t)lj;
22711ce91824SDavid Xu 			/* pass user defined sigval data */
22721ce91824SDavid Xu 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
22734db71d27SJohn-Mark Gurney 			error = kqfd_register(
2274792843c3SMark Johnston 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2275792843c3SMark Johnston 			    M_WAITOK);
227669cd28daSDoug Ambrisko 			if (error) {
227769cd28daSDoug Ambrisko 				uma_zfree(aiolio_zone, lj);
227869cd28daSDoug Ambrisko 				return (error);
227969cd28daSDoug Ambrisko 			}
22801ce91824SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
22811ce91824SDavid Xu 			;
228268d71118SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
228368d71118SDavid Xu 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
228468d71118SDavid Xu 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
228569cd28daSDoug Ambrisko 					uma_zfree(aiolio_zone, lj);
228669cd28daSDoug Ambrisko 					return EINVAL;
228768d71118SDavid Xu 				}
228884af4da6SJohn Dyson 				lj->lioj_flags |= LIOJ_SIGNAL;
228968d71118SDavid Xu 		} else {
229068d71118SDavid Xu 			uma_zfree(aiolio_zone, lj);
229168d71118SDavid Xu 			return EINVAL;
22924d752b01SAlan Cox 		}
22931ce91824SDavid Xu 	}
229469cd28daSDoug Ambrisko 
2295759ccccaSDavid Xu 	AIO_LOCK(ki);
22962f3cf918SAlfred Perlstein 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
22972244ea07SJohn Dyson 	/*
22981ce91824SDavid Xu 	 * Add extra aiocb count to avoid the lio to be freed
22991ce91824SDavid Xu 	 * by other threads doing aio_waitcomplete or aio_return,
23001ce91824SDavid Xu 	 * and prevent event from being sent until we have queued
23011ce91824SDavid Xu 	 * all tasks.
23021ce91824SDavid Xu 	 */
23031ce91824SDavid Xu 	lj->lioj_count = 1;
2304759ccccaSDavid Xu 	AIO_UNLOCK(ki);
23051ce91824SDavid Xu 
23061ce91824SDavid Xu 	/*
2307bfbbc4aaSJason Evans 	 * Get pointers to the list of I/O requests.
23082244ea07SJohn Dyson 	 */
230952c09831SAlan Somers 	nagain = 0;
2310fd3bf775SJohn Dyson 	nerror = 0;
23113858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++) {
23125652770dSJohn Baldwin 		job = acb_list[i];
23135652770dSJohn Baldwin 		if (job != NULL) {
23145652770dSJohn Baldwin 			error = aio_aqueue(td, job, lj, LIO_NOP, ops);
231552c09831SAlan Somers 			if (error == EAGAIN)
231652c09831SAlan Somers 				nagain++;
231752c09831SAlan Somers 			else if (error != 0)
2318fd3bf775SJohn Dyson 				nerror++;
2319fd3bf775SJohn Dyson 		}
2320fd3bf775SJohn Dyson 	}
23212244ea07SJohn Dyson 
23221ce91824SDavid Xu 	error = 0;
2323759ccccaSDavid Xu 	AIO_LOCK(ki);
23243858a1f4SJohn Baldwin 	if (mode == LIO_WAIT) {
23251ce91824SDavid Xu 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2326fd3bf775SJohn Dyson 			ki->kaio_flags |= KAIO_WAKEUP;
2327759ccccaSDavid Xu 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
23281ce91824SDavid Xu 			    PRIBIO | PCATCH, "aiospn", 0);
23291ce91824SDavid Xu 			if (error == ERESTART)
23301ce91824SDavid Xu 				error = EINTR;
23311ce91824SDavid Xu 			if (error)
23321ce91824SDavid Xu 				break;
23331ce91824SDavid Xu 		}
23341ce91824SDavid Xu 	} else {
23351ce91824SDavid Xu 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
23361ce91824SDavid Xu 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
23371ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
23381ce91824SDavid Xu 				KNOTE_LOCKED(&lj->klist, 1);
23391ce91824SDavid Xu 			}
234029331656SKonstantin Belousov 			if ((lj->lioj_flags & (LIOJ_SIGNAL |
234129331656SKonstantin Belousov 			    LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
234229331656SKonstantin Belousov 			    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
23431ce91824SDavid Xu 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
23446814c2daSKonstantin Belousov 				aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
23456814c2daSKonstantin Belousov 				    lj->lioj_count != 1);
23461ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
23472244ea07SJohn Dyson 			}
23482244ea07SJohn Dyson 		}
23491ce91824SDavid Xu 	}
23501ce91824SDavid Xu 	lj->lioj_count--;
23511ce91824SDavid Xu 	if (lj->lioj_count == 0) {
23521ce91824SDavid Xu 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
23531ce91824SDavid Xu 		knlist_delete(&lj->klist, curthread, 1);
2354759ccccaSDavid Xu 		PROC_LOCK(p);
23551ce91824SDavid Xu 		sigqueue_take(&lj->lioj_ksi);
23561ce91824SDavid Xu 		PROC_UNLOCK(p);
2357759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23581ce91824SDavid Xu 		uma_zfree(aiolio_zone, lj);
23591ce91824SDavid Xu 	} else
2360759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23612244ea07SJohn Dyson 
23621ce91824SDavid Xu 	if (nerror)
23631ce91824SDavid Xu 		return (EIO);
236452c09831SAlan Somers 	else if (nagain)
236552c09831SAlan Somers 		return (EAGAIN);
236652c09831SAlan Somers 	else
23671ce91824SDavid Xu 		return (error);
2368ee877a35SJohn Dyson }
2369fd3bf775SJohn Dyson 
23703858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
2371399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
23723858a1f4SJohn Baldwin int
2373399e8c17SJohn Baldwin freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
23743858a1f4SJohn Baldwin {
23753858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23763858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23773858a1f4SJohn Baldwin 	struct osigevent osig;
23783858a1f4SJohn Baldwin 	int error, nent;
23793858a1f4SJohn Baldwin 
23803858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23813858a1f4SJohn Baldwin 		return (EINVAL);
23823858a1f4SJohn Baldwin 
23833858a1f4SJohn Baldwin 	nent = uap->nent;
2384913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
23853858a1f4SJohn Baldwin 		return (EINVAL);
23863858a1f4SJohn Baldwin 
23873858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23883858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
23893858a1f4SJohn Baldwin 		if (error)
23903858a1f4SJohn Baldwin 			return (error);
23913858a1f4SJohn Baldwin 		error = convert_old_sigevent(&osig, &sig);
23923858a1f4SJohn Baldwin 		if (error)
23933858a1f4SJohn Baldwin 			return (error);
23943858a1f4SJohn Baldwin 		sigp = &sig;
23953858a1f4SJohn Baldwin 	} else
23963858a1f4SJohn Baldwin 		sigp = NULL;
23973858a1f4SJohn Baldwin 
23983858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
23993858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24003858a1f4SJohn Baldwin 	if (error == 0)
24013858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode,
24023858a1f4SJohn Baldwin 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
24033858a1f4SJohn Baldwin 		    &aiocb_ops_osigevent);
24043858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24053858a1f4SJohn Baldwin 	return (error);
24063858a1f4SJohn Baldwin }
2407399e8c17SJohn Baldwin #endif
24083858a1f4SJohn Baldwin 
24093858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
24103858a1f4SJohn Baldwin int
24118451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
24123858a1f4SJohn Baldwin {
24133858a1f4SJohn Baldwin 	struct aiocb **acb_list;
24143858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
24153858a1f4SJohn Baldwin 	int error, nent;
24163858a1f4SJohn Baldwin 
24173858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
24183858a1f4SJohn Baldwin 		return (EINVAL);
24193858a1f4SJohn Baldwin 
24203858a1f4SJohn Baldwin 	nent = uap->nent;
2421913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
24223858a1f4SJohn Baldwin 		return (EINVAL);
24233858a1f4SJohn Baldwin 
24243858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
24253858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig, sizeof(sig));
24263858a1f4SJohn Baldwin 		if (error)
24273858a1f4SJohn Baldwin 			return (error);
24283858a1f4SJohn Baldwin 		sigp = &sig;
24293858a1f4SJohn Baldwin 	} else
24303858a1f4SJohn Baldwin 		sigp = NULL;
24313858a1f4SJohn Baldwin 
24323858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24333858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24343858a1f4SJohn Baldwin 	if (error == 0)
24353858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
24363858a1f4SJohn Baldwin 		    nent, sigp, &aiocb_ops);
24373858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24383858a1f4SJohn Baldwin 	return (error);
24393858a1f4SJohn Baldwin }
24403858a1f4SJohn Baldwin 
2441fd3bf775SJohn Dyson static void
2442022ca2fcSAlan Somers aio_biocleanup(struct bio *bp)
2443fd3bf775SJohn Dyson {
24445652770dSJohn Baldwin 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
244527b8220dSDavid Xu 	struct kaioinfo *ki;
244601206038SAlan Somers 	struct buf *pbuf = (struct buf *)bp->bio_caller2;
24471ce91824SDavid Xu 
2448f743d981SAlexander Motin 	/* Release mapping into kernel space. */
244901206038SAlan Somers 	if (pbuf != NULL) {
245001206038SAlan Somers 		MPASS(pbuf->b_npages <= atop(maxphys) + 1);
245101206038SAlan Somers 		pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
245201206038SAlan Somers 		vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
245301206038SAlan Somers 		uma_zfree(pbuf_zone, pbuf);
2454f743d981SAlexander Motin 		atomic_subtract_int(&num_buf_aio, 1);
2455a9d4fe97SKonstantin Belousov 		ki = job->userproc->p_aioinfo;
2456f3215338SJohn Baldwin 		AIO_LOCK(ki);
2457f3215338SJohn Baldwin 		ki->kaio_buffer_count--;
2458f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
2459cd853791SKonstantin Belousov 	} else {
246001206038SAlan Somers 		MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
246101206038SAlan Somers 		vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
246201206038SAlan Somers 		free(bp->bio_ma, M_TEMP);
24638091e52bSJohn Baldwin 		atomic_subtract_int(&num_unmapped_aio, 1);
2464cd853791SKonstantin Belousov 	}
2465f743d981SAlexander Motin 	g_destroy_bio(bp);
246684af4da6SJohn Dyson }
2467bfbbc4aaSJason Evans 
2468022ca2fcSAlan Somers static void
2469022ca2fcSAlan Somers aio_biowakeup(struct bio *bp)
2470022ca2fcSAlan Somers {
2471022ca2fcSAlan Somers 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2472022ca2fcSAlan Somers 	size_t nbytes;
2473022ca2fcSAlan Somers 	long bcount = bp->bio_bcount;
2474022ca2fcSAlan Somers 	long resid = bp->bio_resid;
2475e9c7ec22SMateusz Guzik 	int opcode, nblks;
2476022ca2fcSAlan Somers 	int bio_error = bp->bio_error;
2477022ca2fcSAlan Somers 	uint16_t flags = bp->bio_flags;
2478022ca2fcSAlan Somers 
2479022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
2480022ca2fcSAlan Somers 
2481022ca2fcSAlan Somers 	aio_biocleanup(bp);
2482022ca2fcSAlan Somers 
2483022ca2fcSAlan Somers 	nbytes = bcount - resid;
2484022ca2fcSAlan Somers 	atomic_add_acq_long(&job->nbytes, nbytes);
2485022ca2fcSAlan Somers 	nblks = btodb(nbytes);
2486*cca6d616SJohn Baldwin 
2487022ca2fcSAlan Somers 	/*
2488022ca2fcSAlan Somers 	 * If multiple bios experienced an error, the job will reflect the
2489022ca2fcSAlan Somers 	 * error of whichever failed bio completed last.
2490022ca2fcSAlan Somers 	 */
2491022ca2fcSAlan Somers 	if (flags & BIO_ERROR)
2492022ca2fcSAlan Somers 		atomic_set_int(&job->error, bio_error);
24932247f489SAlan Somers 	if (opcode & LIO_WRITE)
2494022ca2fcSAlan Somers 		atomic_add_int(&job->outblock, nblks);
2495022ca2fcSAlan Somers 	else
2496022ca2fcSAlan Somers 		atomic_add_int(&job->inblock, nblks);
2497022ca2fcSAlan Somers 
249840734fc5SKeith Reynolds 	if (atomic_fetchadd_int(&job->nbio, -1) == 1) {
2499022ca2fcSAlan Somers 		if (atomic_load_int(&job->error))
2500022ca2fcSAlan Somers 			aio_complete(job, -1, job->error);
2501022ca2fcSAlan Somers 		else
2502022ca2fcSAlan Somers 			aio_complete(job, atomic_load_long(&job->nbytes), 0);
2503022ca2fcSAlan Somers 	}
2504022ca2fcSAlan Somers }
2505022ca2fcSAlan Somers 
2506eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */
25073858a1f4SJohn Baldwin static int
25085652770dSJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
25093858a1f4SJohn Baldwin     struct timespec *ts, struct aiocb_ops *ops)
2510bfbbc4aaSJason Evans {
2511b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2512bfbbc4aaSJason Evans 	struct timeval atv;
2513bfbbc4aaSJason Evans 	struct kaioinfo *ki;
25145652770dSJohn Baldwin 	struct kaiocb *job;
25155652770dSJohn Baldwin 	struct aiocb *ujob;
2516bb430bc7SJohn Baldwin 	long error, status;
2517bb430bc7SJohn Baldwin 	int timo;
2518bfbbc4aaSJason Evans 
25195652770dSJohn Baldwin 	ops->store_aiocb(ujobp, NULL);
2520dd85920aSJason Evans 
252138d68e2dSPawel Jakub Dawidek 	if (ts == NULL) {
2522bfbbc4aaSJason Evans 		timo = 0;
252338d68e2dSPawel Jakub Dawidek 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
252438d68e2dSPawel Jakub Dawidek 		timo = -1;
252538d68e2dSPawel Jakub Dawidek 	} else {
25263858a1f4SJohn Baldwin 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2527bfbbc4aaSJason Evans 			return (EINVAL);
2528bfbbc4aaSJason Evans 
25293858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2530bfbbc4aaSJason Evans 		if (itimerfix(&atv))
2531bfbbc4aaSJason Evans 			return (EINVAL);
2532bfbbc4aaSJason Evans 		timo = tvtohz(&atv);
2533bfbbc4aaSJason Evans 	}
2534bfbbc4aaSJason Evans 
25358213baf0SChristian S.J. Peron 	if (p->p_aioinfo == NULL)
2536323fe565SDavid Xu 		aio_init_aioinfo(p);
25378213baf0SChristian S.J. Peron 	ki = p->p_aioinfo;
2538bfbbc4aaSJason Evans 
25391ce91824SDavid Xu 	error = 0;
25405652770dSJohn Baldwin 	job = NULL;
2541759ccccaSDavid Xu 	AIO_LOCK(ki);
25425652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
254338d68e2dSPawel Jakub Dawidek 		if (timo == -1) {
254438d68e2dSPawel Jakub Dawidek 			error = EWOULDBLOCK;
254538d68e2dSPawel Jakub Dawidek 			break;
254638d68e2dSPawel Jakub Dawidek 		}
25471ce91824SDavid Xu 		ki->kaio_flags |= KAIO_WAKEUP;
2548759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
25491ce91824SDavid Xu 		    "aiowc", timo);
255027b8220dSDavid Xu 		if (timo && error == ERESTART)
25511ce91824SDavid Xu 			error = EINTR;
25521ce91824SDavid Xu 		if (error)
25531ce91824SDavid Xu 			break;
25541ce91824SDavid Xu 	}
25551ce91824SDavid Xu 
25565652770dSJohn Baldwin 	if (job != NULL) {
2557f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
25585652770dSJohn Baldwin 		ujob = job->ujob;
25595652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
25605652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
25611ce91824SDavid Xu 		td->td_retval[0] = status;
2562b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
2563b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
2564b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
2565b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
25665652770dSJohn Baldwin 		aio_free_entry(job);
2567759ccccaSDavid Xu 		AIO_UNLOCK(ki);
25685652770dSJohn Baldwin 		ops->store_aiocb(ujobp, ujob);
25695652770dSJohn Baldwin 		ops->store_error(ujob, error);
25705652770dSJohn Baldwin 		ops->store_status(ujob, status);
25711ce91824SDavid Xu 	} else
2572759ccccaSDavid Xu 		AIO_UNLOCK(ki);
2573bfbbc4aaSJason Evans 
2574ac41f2efSAlfred Perlstein 	return (error);
2575bfbbc4aaSJason Evans }
2576cb679c38SJonathan Lemon 
257799eee864SDavid Xu int
25788451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
25793858a1f4SJohn Baldwin {
25803858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
25813858a1f4SJohn Baldwin 	int error;
25823858a1f4SJohn Baldwin 
25833858a1f4SJohn Baldwin 	if (uap->timeout) {
25843858a1f4SJohn Baldwin 		/* Get timespec struct. */
25853858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts, sizeof(ts));
25863858a1f4SJohn Baldwin 		if (error)
25873858a1f4SJohn Baldwin 			return (error);
25883858a1f4SJohn Baldwin 		tsp = &ts;
25893858a1f4SJohn Baldwin 	} else
25903858a1f4SJohn Baldwin 		tsp = NULL;
25913858a1f4SJohn Baldwin 
25923858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
25933858a1f4SJohn Baldwin }
25943858a1f4SJohn Baldwin 
25953858a1f4SJohn Baldwin static int
25965652770dSJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
25973858a1f4SJohn Baldwin     struct aiocb_ops *ops)
259899eee864SDavid Xu {
2599801ac943SThomas Munro 	int listop;
260099eee864SDavid Xu 
2601801ac943SThomas Munro 	switch (op) {
2602801ac943SThomas Munro 	case O_SYNC:
2603801ac943SThomas Munro 		listop = LIO_SYNC;
2604801ac943SThomas Munro 		break;
2605801ac943SThomas Munro 	case O_DSYNC:
2606801ac943SThomas Munro 		listop = LIO_DSYNC;
2607801ac943SThomas Munro 		break;
2608801ac943SThomas Munro 	default:
260999eee864SDavid Xu 		return (EINVAL);
2610801ac943SThomas Munro 	}
2611801ac943SThomas Munro 
2612801ac943SThomas Munro 	return (aio_aqueue(td, ujob, NULL, listop, ops));
26133858a1f4SJohn Baldwin }
26143858a1f4SJohn Baldwin 
26153858a1f4SJohn Baldwin int
26168451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
26173858a1f4SJohn Baldwin {
26183858a1f4SJohn Baldwin 
26193858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
262099eee864SDavid Xu }
262199eee864SDavid Xu 
2622eb8e6d52SEivind Eklund /* kqueue attach function */
2623cb679c38SJonathan Lemon static int
2624cb679c38SJonathan Lemon filt_aioattach(struct knote *kn)
2625cb679c38SJonathan Lemon {
26262b34e843SKonstantin Belousov 	struct kaiocb *job;
26272b34e843SKonstantin Belousov 
26282b34e843SKonstantin Belousov 	job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2629cb679c38SJonathan Lemon 
2630cb679c38SJonathan Lemon 	/*
26315652770dSJohn Baldwin 	 * The job pointer must be validated before using it, so
2632cb679c38SJonathan Lemon 	 * registration is restricted to the kernel; the user cannot
2633cb679c38SJonathan Lemon 	 * set EV_FLAG1.
2634cb679c38SJonathan Lemon 	 */
2635cb679c38SJonathan Lemon 	if ((kn->kn_flags & EV_FLAG1) == 0)
2636cb679c38SJonathan Lemon 		return (EPERM);
26375652770dSJohn Baldwin 	kn->kn_ptr.p_aio = job;
2638cb679c38SJonathan Lemon 	kn->kn_flags &= ~EV_FLAG1;
2639cb679c38SJonathan Lemon 
26405652770dSJohn Baldwin 	knlist_add(&job->klist, kn, 0);
2641cb679c38SJonathan Lemon 
2642cb679c38SJonathan Lemon 	return (0);
2643cb679c38SJonathan Lemon }
2644cb679c38SJonathan Lemon 
2645eb8e6d52SEivind Eklund /* kqueue detach function */
2646cb679c38SJonathan Lemon static void
2647cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn)
2648cb679c38SJonathan Lemon {
26498e9fc278SDoug Ambrisko 	struct knlist *knl;
2650cb679c38SJonathan Lemon 
26518e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_aio->klist;
26528e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26538e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26548e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26558e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
2656cb679c38SJonathan Lemon }
2657cb679c38SJonathan Lemon 
2658eb8e6d52SEivind Eklund /* kqueue filter function */
2659cb679c38SJonathan Lemon /*ARGSUSED*/
2660cb679c38SJonathan Lemon static int
2661cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint)
2662cb679c38SJonathan Lemon {
26635652770dSJohn Baldwin 	struct kaiocb *job = kn->kn_ptr.p_aio;
2664cb679c38SJonathan Lemon 
26655652770dSJohn Baldwin 	kn->kn_data = job->uaiocb._aiocb_private.error;
2666f3215338SJohn Baldwin 	if (!(job->jobflags & KAIOCB_FINISHED))
2667cb679c38SJonathan Lemon 		return (0);
2668cb679c38SJonathan Lemon 	kn->kn_flags |= EV_EOF;
2669cb679c38SJonathan Lemon 	return (1);
2670cb679c38SJonathan Lemon }
267169cd28daSDoug Ambrisko 
267269cd28daSDoug Ambrisko /* kqueue attach function */
267369cd28daSDoug Ambrisko static int
267469cd28daSDoug Ambrisko filt_lioattach(struct knote *kn)
267569cd28daSDoug Ambrisko {
26762b34e843SKonstantin Belousov 	struct aioliojob *lj;
26772b34e843SKonstantin Belousov 
26782b34e843SKonstantin Belousov 	lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
267969cd28daSDoug Ambrisko 
268069cd28daSDoug Ambrisko 	/*
26811ce91824SDavid Xu 	 * The aioliojob pointer must be validated before using it, so
268269cd28daSDoug Ambrisko 	 * registration is restricted to the kernel; the user cannot
268369cd28daSDoug Ambrisko 	 * set EV_FLAG1.
268469cd28daSDoug Ambrisko 	 */
268569cd28daSDoug Ambrisko 	if ((kn->kn_flags & EV_FLAG1) == 0)
268669cd28daSDoug Ambrisko 		return (EPERM);
2687a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_lio = lj;
268869cd28daSDoug Ambrisko 	kn->kn_flags &= ~EV_FLAG1;
268969cd28daSDoug Ambrisko 
269069cd28daSDoug Ambrisko 	knlist_add(&lj->klist, kn, 0);
269169cd28daSDoug Ambrisko 
269269cd28daSDoug Ambrisko 	return (0);
269369cd28daSDoug Ambrisko }
269469cd28daSDoug Ambrisko 
269569cd28daSDoug Ambrisko /* kqueue detach function */
269669cd28daSDoug Ambrisko static void
269769cd28daSDoug Ambrisko filt_liodetach(struct knote *kn)
269869cd28daSDoug Ambrisko {
26998e9fc278SDoug Ambrisko 	struct knlist *knl;
270069cd28daSDoug Ambrisko 
27018e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_lio->klist;
27028e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
27038e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
27048e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
27058e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
270669cd28daSDoug Ambrisko }
270769cd28daSDoug Ambrisko 
270869cd28daSDoug Ambrisko /* kqueue filter function */
270969cd28daSDoug Ambrisko /*ARGSUSED*/
271069cd28daSDoug Ambrisko static int
271169cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint)
271269cd28daSDoug Ambrisko {
2713a8afa221SJean-Sébastien Pédron 	struct aioliojob * lj = kn->kn_ptr.p_lio;
27141ce91824SDavid Xu 
271569cd28daSDoug Ambrisko 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
271669cd28daSDoug Ambrisko }
27173858a1f4SJohn Baldwin 
2718841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2719399e8c17SJohn Baldwin #include <sys/mount.h>
2720399e8c17SJohn Baldwin #include <sys/socket.h>
272131d1b816SDmitry Chagin #include <sys/sysent.h>
2722399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32.h>
2723399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_proto.h>
2724399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2725399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_syscall.h>
2726399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_util.h>
27273858a1f4SJohn Baldwin 
27283858a1f4SJohn Baldwin struct __aiocb_private32 {
27293858a1f4SJohn Baldwin 	int32_t	status;
27303858a1f4SJohn Baldwin 	int32_t	error;
27313858a1f4SJohn Baldwin 	uint32_t kernelinfo;
27323858a1f4SJohn Baldwin };
27333858a1f4SJohn Baldwin 
2734399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27353858a1f4SJohn Baldwin typedef struct oaiocb32 {
27363858a1f4SJohn Baldwin 	int	aio_fildes;		/* File descriptor */
27373858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27383858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27393858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27403858a1f4SJohn Baldwin 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
27413858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27423858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27433858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27443858a1f4SJohn Baldwin } oaiocb32_t;
2745399e8c17SJohn Baldwin #endif
27463858a1f4SJohn Baldwin 
27473858a1f4SJohn Baldwin typedef struct aiocb32 {
27483858a1f4SJohn Baldwin 	int32_t	aio_fildes;		/* File descriptor */
27493858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27503858a1f4SJohn Baldwin 	uint32_t aio_buf;	/* I/O buffer in process space */
27513858a1f4SJohn Baldwin 	uint32_t aio_nbytes;	/* Number of bytes for I/O */
27523858a1f4SJohn Baldwin 	int	__spare__[2];
27533858a1f4SJohn Baldwin 	uint32_t __spare2__;
27543858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27553858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27563858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27573858a1f4SJohn Baldwin 	struct	sigevent32 aio_sigevent;	/* Signal to deliver */
27583858a1f4SJohn Baldwin } aiocb32_t;
27593858a1f4SJohn Baldwin 
2760399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27613858a1f4SJohn Baldwin static int
27623858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
27633858a1f4SJohn Baldwin {
27643858a1f4SJohn Baldwin 
27653858a1f4SJohn Baldwin 	/*
27663858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
27673858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
27683858a1f4SJohn Baldwin 	 */
27693858a1f4SJohn Baldwin 	CP(*osig, *nsig, sigev_notify);
27703858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
27713858a1f4SJohn Baldwin 	case SIGEV_NONE:
27723858a1f4SJohn Baldwin 		break;
27733858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
27743858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
27753858a1f4SJohn Baldwin 		break;
27763858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
27773858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
27783858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
27793858a1f4SJohn Baldwin 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
27803858a1f4SJohn Baldwin 		break;
27813858a1f4SJohn Baldwin 	default:
27823858a1f4SJohn Baldwin 		return (EINVAL);
27833858a1f4SJohn Baldwin 	}
27843858a1f4SJohn Baldwin 	return (0);
27853858a1f4SJohn Baldwin }
27863858a1f4SJohn Baldwin 
27873858a1f4SJohn Baldwin static int
2788022ca2fcSAlan Somers aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2789022ca2fcSAlan Somers     int type __unused)
27903858a1f4SJohn Baldwin {
27913858a1f4SJohn Baldwin 	struct oaiocb32 job32;
2792022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
27933858a1f4SJohn Baldwin 	int error;
27943858a1f4SJohn Baldwin 
2795022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
27963858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
27973858a1f4SJohn Baldwin 	if (error)
27983858a1f4SJohn Baldwin 		return (error);
27993858a1f4SJohn Baldwin 
2800022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2801022ca2fcSAlan Somers 
2802022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2803022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2804022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, aio_buf);
2805022ca2fcSAlan Somers 	CP(job32, *kcb, aio_nbytes);
2806022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
2807022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2808022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2809022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2810022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
28113858a1f4SJohn Baldwin 	return (convert_old_sigevent32(&job32.aio_sigevent,
2812022ca2fcSAlan Somers 	    &kcb->aio_sigevent));
28133858a1f4SJohn Baldwin }
2814399e8c17SJohn Baldwin #endif
28153858a1f4SJohn Baldwin 
28163858a1f4SJohn Baldwin static int
2817022ca2fcSAlan Somers aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
28183858a1f4SJohn Baldwin {
28193858a1f4SJohn Baldwin 	struct aiocb32 job32;
2820022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
2821022ca2fcSAlan Somers 	struct iovec32 *iov32;
28223858a1f4SJohn Baldwin 	int error;
28233858a1f4SJohn Baldwin 
28243858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
28253858a1f4SJohn Baldwin 	if (error)
28263858a1f4SJohn Baldwin 		return (error);
2827022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2828022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2829022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
28302884918cSMark Johnston 	if (type == LIO_NOP)
28312884918cSMark Johnston 		type = kcb->aio_lio_opcode;
28322247f489SAlan Somers 	if (type & LIO_VECTORED) {
2833022ca2fcSAlan Somers 		iov32 = PTRIN(job32.aio_iov);
2834022ca2fcSAlan Somers 		CP(job32, *kcb, aio_iovcnt);
2835022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
2836022ca2fcSAlan Somers 		error = freebsd32_copyinuio(iov32,
2837022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
2838022ca2fcSAlan Somers 		if (error)
2839022ca2fcSAlan Somers 			return (error);
2840022ca2fcSAlan Somers 	} else {
2841022ca2fcSAlan Somers 		PTRIN_CP(job32, *kcb, aio_buf);
2842022ca2fcSAlan Somers 		CP(job32, *kcb, aio_nbytes);
2843022ca2fcSAlan Somers 	}
2844022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2845022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2846022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2847022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2848022ca2fcSAlan Somers 	error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2849022ca2fcSAlan Somers 
2850022ca2fcSAlan Somers 	return (error);
28513858a1f4SJohn Baldwin }
28523858a1f4SJohn Baldwin 
28533858a1f4SJohn Baldwin static long
28543858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob)
28553858a1f4SJohn Baldwin {
28563858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28573858a1f4SJohn Baldwin 
28583858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28593858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.status));
28603858a1f4SJohn Baldwin }
28613858a1f4SJohn Baldwin 
28623858a1f4SJohn Baldwin static long
28633858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob)
28643858a1f4SJohn Baldwin {
28653858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28663858a1f4SJohn Baldwin 
28673858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28683858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.error));
28693858a1f4SJohn Baldwin }
28703858a1f4SJohn Baldwin 
28713858a1f4SJohn Baldwin static int
28723858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status)
28733858a1f4SJohn Baldwin {
28743858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28753858a1f4SJohn Baldwin 
28763858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28773858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.status, status));
28783858a1f4SJohn Baldwin }
28793858a1f4SJohn Baldwin 
28803858a1f4SJohn Baldwin static int
28813858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error)
28823858a1f4SJohn Baldwin {
28833858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28843858a1f4SJohn Baldwin 
28853858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28863858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.error, error));
28873858a1f4SJohn Baldwin }
28883858a1f4SJohn Baldwin 
28893858a1f4SJohn Baldwin static int
28903858a1f4SJohn Baldwin aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
28913858a1f4SJohn Baldwin {
28923858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28933858a1f4SJohn Baldwin 
28943858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28953858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
28963858a1f4SJohn Baldwin }
28973858a1f4SJohn Baldwin 
28983858a1f4SJohn Baldwin static int
28993858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
29003858a1f4SJohn Baldwin {
29013858a1f4SJohn Baldwin 
29023858a1f4SJohn Baldwin 	return (suword32(ujobp, (long)ujob));
29033858a1f4SJohn Baldwin }
29043858a1f4SJohn Baldwin 
29053858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = {
2906849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin,
29073858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29083858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29093858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29103858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29113858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29123858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29133858a1f4SJohn Baldwin };
29143858a1f4SJohn Baldwin 
2915399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29163858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = {
2917849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin_old_sigevent,
29183858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29193858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29203858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29213858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29223858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29233858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29243858a1f4SJohn Baldwin };
2925399e8c17SJohn Baldwin #endif
29263858a1f4SJohn Baldwin 
29273858a1f4SJohn Baldwin int
29283858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
29293858a1f4SJohn Baldwin {
29303858a1f4SJohn Baldwin 
29313858a1f4SJohn Baldwin 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29323858a1f4SJohn Baldwin }
29333858a1f4SJohn Baldwin 
29343858a1f4SJohn Baldwin int
29353858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
29363858a1f4SJohn Baldwin {
29373858a1f4SJohn Baldwin 	struct timespec32 ts32;
29383858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
29393858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
29403858a1f4SJohn Baldwin 	uint32_t *ujoblist32;
29413858a1f4SJohn Baldwin 	int error, i;
29423858a1f4SJohn Baldwin 
2943913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
29443858a1f4SJohn Baldwin 		return (EINVAL);
29453858a1f4SJohn Baldwin 
29463858a1f4SJohn Baldwin 	if (uap->timeout) {
29473858a1f4SJohn Baldwin 		/* Get timespec struct. */
29483858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
29493858a1f4SJohn Baldwin 			return (error);
29503858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
29513858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
29523858a1f4SJohn Baldwin 		tsp = &ts;
29533858a1f4SJohn Baldwin 	} else
29543858a1f4SJohn Baldwin 		tsp = NULL;
29553858a1f4SJohn Baldwin 
29569553bc89SMark Johnston 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
29573858a1f4SJohn Baldwin 	ujoblist32 = (uint32_t *)ujoblist;
29583858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
29593858a1f4SJohn Baldwin 	    sizeof(ujoblist32[0]));
29603858a1f4SJohn Baldwin 	if (error == 0) {
2961df485bdbSAlan Somers 		for (i = uap->nent - 1; i >= 0; i--)
29623858a1f4SJohn Baldwin 			ujoblist[i] = PTRIN(ujoblist32[i]);
29633858a1f4SJohn Baldwin 
29643858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
29653858a1f4SJohn Baldwin 	}
29669553bc89SMark Johnston 	free(ujoblist, M_AIO);
29673858a1f4SJohn Baldwin 	return (error);
29683858a1f4SJohn Baldwin }
29693858a1f4SJohn Baldwin 
29703858a1f4SJohn Baldwin int
29713858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
29723858a1f4SJohn Baldwin {
29733858a1f4SJohn Baldwin 
29743858a1f4SJohn Baldwin 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29753858a1f4SJohn Baldwin }
29763858a1f4SJohn Baldwin 
2977399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29783858a1f4SJohn Baldwin int
2979399e8c17SJohn Baldwin freebsd6_freebsd32_aio_read(struct thread *td,
2980399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_read_args *uap)
29813858a1f4SJohn Baldwin {
29823858a1f4SJohn Baldwin 
29833858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29843858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29853858a1f4SJohn Baldwin }
2986399e8c17SJohn Baldwin #endif
29873858a1f4SJohn Baldwin 
29883858a1f4SJohn Baldwin int
29893858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
29903858a1f4SJohn Baldwin {
29913858a1f4SJohn Baldwin 
29923858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29933858a1f4SJohn Baldwin 	    &aiocb32_ops));
29943858a1f4SJohn Baldwin }
29953858a1f4SJohn Baldwin 
2996022ca2fcSAlan Somers int
2997022ca2fcSAlan Somers freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
2998022ca2fcSAlan Somers {
2999022ca2fcSAlan Somers 
3000022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
3001022ca2fcSAlan Somers 	    &aiocb32_ops));
3002022ca2fcSAlan Somers }
3003022ca2fcSAlan Somers 
3004399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30053858a1f4SJohn Baldwin int
3006399e8c17SJohn Baldwin freebsd6_freebsd32_aio_write(struct thread *td,
3007399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_write_args *uap)
30083858a1f4SJohn Baldwin {
30093858a1f4SJohn Baldwin 
30103858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30113858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
30123858a1f4SJohn Baldwin }
3013399e8c17SJohn Baldwin #endif
30143858a1f4SJohn Baldwin 
30153858a1f4SJohn Baldwin int
30163858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
30173858a1f4SJohn Baldwin {
30183858a1f4SJohn Baldwin 
30193858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30203858a1f4SJohn Baldwin 	    &aiocb32_ops));
30213858a1f4SJohn Baldwin }
30223858a1f4SJohn Baldwin 
30233858a1f4SJohn Baldwin int
3024022ca2fcSAlan Somers freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3025022ca2fcSAlan Somers {
3026022ca2fcSAlan Somers 
3027022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3028022ca2fcSAlan Somers 	    &aiocb32_ops));
3029022ca2fcSAlan Somers }
3030022ca2fcSAlan Somers 
3031022ca2fcSAlan Somers int
30326160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
30336160e12cSGleb Smirnoff {
30346160e12cSGleb Smirnoff 
30356160e12cSGleb Smirnoff 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
30366160e12cSGleb Smirnoff 	    &aiocb32_ops));
30376160e12cSGleb Smirnoff }
30386160e12cSGleb Smirnoff 
30396160e12cSGleb Smirnoff int
30403858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td,
30413858a1f4SJohn Baldwin     struct freebsd32_aio_waitcomplete_args *uap)
30423858a1f4SJohn Baldwin {
3043e588eeb1SJohn Baldwin 	struct timespec32 ts32;
30443858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
30453858a1f4SJohn Baldwin 	int error;
30463858a1f4SJohn Baldwin 
30473858a1f4SJohn Baldwin 	if (uap->timeout) {
30483858a1f4SJohn Baldwin 		/* Get timespec struct. */
30493858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
30503858a1f4SJohn Baldwin 		if (error)
30513858a1f4SJohn Baldwin 			return (error);
30523858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
30533858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
30543858a1f4SJohn Baldwin 		tsp = &ts;
30553858a1f4SJohn Baldwin 	} else
30563858a1f4SJohn Baldwin 		tsp = NULL;
30573858a1f4SJohn Baldwin 
30583858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
30593858a1f4SJohn Baldwin 	    &aiocb32_ops));
30603858a1f4SJohn Baldwin }
30613858a1f4SJohn Baldwin 
30623858a1f4SJohn Baldwin int
30633858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
30643858a1f4SJohn Baldwin {
30653858a1f4SJohn Baldwin 
30663858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
30673858a1f4SJohn Baldwin 	    &aiocb32_ops));
30683858a1f4SJohn Baldwin }
30693858a1f4SJohn Baldwin 
3070399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30713858a1f4SJohn Baldwin int
3072399e8c17SJohn Baldwin freebsd6_freebsd32_lio_listio(struct thread *td,
3073399e8c17SJohn Baldwin     struct freebsd6_freebsd32_lio_listio_args *uap)
30743858a1f4SJohn Baldwin {
30753858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30763858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30773858a1f4SJohn Baldwin 	struct osigevent32 osig;
30783858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30793858a1f4SJohn Baldwin 	int error, i, nent;
30803858a1f4SJohn Baldwin 
30813858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30823858a1f4SJohn Baldwin 		return (EINVAL);
30833858a1f4SJohn Baldwin 
30843858a1f4SJohn Baldwin 	nent = uap->nent;
3085913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
30863858a1f4SJohn Baldwin 		return (EINVAL);
30873858a1f4SJohn Baldwin 
30883858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30893858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
30903858a1f4SJohn Baldwin 		if (error)
30913858a1f4SJohn Baldwin 			return (error);
30923858a1f4SJohn Baldwin 		error = convert_old_sigevent32(&osig, &sig);
30933858a1f4SJohn Baldwin 		if (error)
30943858a1f4SJohn Baldwin 			return (error);
30953858a1f4SJohn Baldwin 		sigp = &sig;
30963858a1f4SJohn Baldwin 	} else
30973858a1f4SJohn Baldwin 		sigp = NULL;
30983858a1f4SJohn Baldwin 
30993858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31003858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31013858a1f4SJohn Baldwin 	if (error) {
31023858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31033858a1f4SJohn Baldwin 		return (error);
31043858a1f4SJohn Baldwin 	}
31053858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31063858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31073858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31083858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31093858a1f4SJohn Baldwin 
31103858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31113858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31123858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent);
31133858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31143858a1f4SJohn Baldwin 	return (error);
31153858a1f4SJohn Baldwin }
3116399e8c17SJohn Baldwin #endif
31173858a1f4SJohn Baldwin 
31183858a1f4SJohn Baldwin int
31193858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
31203858a1f4SJohn Baldwin {
31213858a1f4SJohn Baldwin 	struct aiocb **acb_list;
31223858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
31233858a1f4SJohn Baldwin 	struct sigevent32 sig32;
31243858a1f4SJohn Baldwin 	uint32_t *acb_list32;
31253858a1f4SJohn Baldwin 	int error, i, nent;
31263858a1f4SJohn Baldwin 
31273858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
31283858a1f4SJohn Baldwin 		return (EINVAL);
31293858a1f4SJohn Baldwin 
31303858a1f4SJohn Baldwin 	nent = uap->nent;
3131913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
31323858a1f4SJohn Baldwin 		return (EINVAL);
31333858a1f4SJohn Baldwin 
31343858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
31353858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig32, sizeof(sig32));
31363858a1f4SJohn Baldwin 		if (error)
31373858a1f4SJohn Baldwin 			return (error);
31383858a1f4SJohn Baldwin 		error = convert_sigevent32(&sig32, &sig);
31393858a1f4SJohn Baldwin 		if (error)
31403858a1f4SJohn Baldwin 			return (error);
31413858a1f4SJohn Baldwin 		sigp = &sig;
31423858a1f4SJohn Baldwin 	} else
31433858a1f4SJohn Baldwin 		sigp = NULL;
31443858a1f4SJohn Baldwin 
31453858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31463858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31473858a1f4SJohn Baldwin 	if (error) {
31483858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31493858a1f4SJohn Baldwin 		return (error);
31503858a1f4SJohn Baldwin 	}
31513858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31523858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31533858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31543858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31553858a1f4SJohn Baldwin 
31563858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31573858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31583858a1f4SJohn Baldwin 	    &aiocb32_ops);
31593858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31603858a1f4SJohn Baldwin 	return (error);
31613858a1f4SJohn Baldwin }
31623858a1f4SJohn Baldwin 
31633858a1f4SJohn Baldwin #endif
3164