xref: /freebsd/sys/kern/vfs_aio.c (revision 45c2c7c484de7747014492b17ff89e323ee66496)
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>
5521d56e9cSAlfred Perlstein #include <sys/sysent.h>
56a624e84fSJohn Dyson #include <sys/sysctl.h>
579c20dc99SJohn Baldwin #include <sys/syslog.h>
58ee99e978SBruce Evans #include <sys/sx.h>
591ce91824SDavid Xu #include <sys/taskqueue.h>
60fd3bf775SJohn Dyson #include <sys/vnode.h>
61fd3bf775SJohn Dyson #include <sys/conf.h>
62cb679c38SJonathan Lemon #include <sys/event.h>
6399eee864SDavid Xu #include <sys/mount.h>
64f743d981SAlexander Motin #include <geom/geom.h>
65ee877a35SJohn Dyson 
661ce91824SDavid Xu #include <machine/atomic.h>
671ce91824SDavid Xu 
68ee877a35SJohn Dyson #include <vm/vm.h>
69f743d981SAlexander Motin #include <vm/vm_page.h>
70ee877a35SJohn Dyson #include <vm/vm_extern.h>
712244ea07SJohn Dyson #include <vm/pmap.h>
722244ea07SJohn Dyson #include <vm/vm_map.h>
7399eee864SDavid Xu #include <vm/vm_object.h>
74c897b813SJeff Roberson #include <vm/uma.h>
75ee877a35SJohn Dyson #include <sys/aio.h>
765aaef07cSJohn Dyson 
77eb8e6d52SEivind Eklund /*
78eb8e6d52SEivind Eklund  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
7999eee864SDavid Xu  * overflow. (XXX will be removed soon.)
80eb8e6d52SEivind Eklund  */
8199eee864SDavid Xu static u_long jobrefid;
822244ea07SJohn Dyson 
8399eee864SDavid Xu /*
8499eee864SDavid Xu  * Counter for aio_fsync.
8599eee864SDavid Xu  */
8699eee864SDavid Xu static uint64_t jobseqno;
8799eee864SDavid Xu 
8884af4da6SJohn Dyson #ifndef MAX_AIO_PER_PROC
892244ea07SJohn Dyson #define MAX_AIO_PER_PROC	32
9084af4da6SJohn Dyson #endif
9184af4da6SJohn Dyson 
9284af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE_PER_PROC
93913b9329SAlan Somers #define MAX_AIO_QUEUE_PER_PROC	256
9484af4da6SJohn Dyson #endif
9584af4da6SJohn Dyson 
9684af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE
97913b9329SAlan Somers #define MAX_AIO_QUEUE		1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
9884af4da6SJohn Dyson #endif
9984af4da6SJohn Dyson 
10084af4da6SJohn Dyson #ifndef MAX_BUF_AIO
10184af4da6SJohn Dyson #define MAX_BUF_AIO		16
10284af4da6SJohn Dyson #endif
10384af4da6SJohn Dyson 
104e603be7aSRobert Watson FEATURE(aio, "Asynchronous I/O");
105c45796d5SAlan Somers SYSCTL_DECL(_p1003_1b);
106e603be7aSRobert Watson 
1073858a1f4SJohn Baldwin static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
108913b9329SAlan Somers static MALLOC_DEFINE(M_AIOS, "aios", "aio_suspend aio control block list");
1093858a1f4SJohn Baldwin 
1107029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1110dd6c035SJohn Baldwin     "Async IO management");
112eb8e6d52SEivind Eklund 
113f3215338SJohn Baldwin static int enable_aio_unsafe = 0;
114f3215338SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
115f3215338SJohn Baldwin     "Permit asynchronous IO on all file types, not just known-safe types");
116f3215338SJohn Baldwin 
1179c20dc99SJohn Baldwin static unsigned int unsafe_warningcnt = 1;
1189c20dc99SJohn Baldwin SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
1199c20dc99SJohn Baldwin     &unsafe_warningcnt, 0,
1209c20dc99SJohn Baldwin     "Warnings that will be triggered upon failed IO requests on unsafe files");
1219c20dc99SJohn Baldwin 
122303b270bSEivind Eklund static int max_aio_procs = MAX_AIO_PROCS;
1230dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
12439314b7dSJohn Baldwin     "Maximum number of kernel processes to use for handling async IO ");
125a624e84fSJohn Dyson 
126eb8e6d52SEivind Eklund static int num_aio_procs = 0;
1270dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
12839314b7dSJohn Baldwin     "Number of presently active kernel processes for async IO");
129a624e84fSJohn Dyson 
130eb8e6d52SEivind Eklund /*
131eb8e6d52SEivind Eklund  * The code will adjust the actual number of AIO processes towards this
132eb8e6d52SEivind Eklund  * number when it gets a chance.
133eb8e6d52SEivind Eklund  */
134eb8e6d52SEivind Eklund static int target_aio_procs = TARGET_AIO_PROCS;
135eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
1360dd6c035SJohn Baldwin     0,
1370dd6c035SJohn Baldwin     "Preferred number of ready kernel processes for async IO");
138a624e84fSJohn Dyson 
139eb8e6d52SEivind Eklund static int max_queue_count = MAX_AIO_QUEUE;
140eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
141eb8e6d52SEivind Eklund     "Maximum number of aio requests to queue, globally");
142a624e84fSJohn Dyson 
143eb8e6d52SEivind Eklund static int num_queue_count = 0;
144eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
145eb8e6d52SEivind Eklund     "Number of queued aio requests");
146a624e84fSJohn Dyson 
147eb8e6d52SEivind Eklund static int num_buf_aio = 0;
148eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
149eb8e6d52SEivind Eklund     "Number of aio requests presently handled by the buf subsystem");
150fd3bf775SJohn Dyson 
1518091e52bSJohn Baldwin static int num_unmapped_aio = 0;
1528091e52bSJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
1538091e52bSJohn Baldwin     0,
1548091e52bSJohn Baldwin     "Number of aio requests presently handled by unmapped I/O buffers");
1558091e52bSJohn Baldwin 
15639314b7dSJohn Baldwin /* Number of async I/O processes in the process of being started */
157a9bf5e37SDavid Xu /* XXX This should be local to aio_aqueue() */
158eb8e6d52SEivind Eklund static int num_aio_resv_start = 0;
159fd3bf775SJohn Dyson 
160eb8e6d52SEivind Eklund static int aiod_lifetime;
161eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
162eb8e6d52SEivind Eklund     "Maximum lifetime for idle aiod");
16384af4da6SJohn Dyson 
164eb8e6d52SEivind Eklund static int max_aio_per_proc = MAX_AIO_PER_PROC;
165eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
1660dd6c035SJohn Baldwin     0,
16786bbef43SJohn Baldwin     "Maximum active aio requests per process");
168eb8e6d52SEivind Eklund 
169eb8e6d52SEivind Eklund static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
170eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
171eb8e6d52SEivind Eklund     &max_aio_queue_per_proc, 0,
17286bbef43SJohn Baldwin     "Maximum queued aio requests per process");
173eb8e6d52SEivind Eklund 
174eb8e6d52SEivind Eklund static int max_buf_aio = MAX_BUF_AIO;
175eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
17686bbef43SJohn Baldwin     "Maximum buf aio requests per process");
177eb8e6d52SEivind Eklund 
178913b9329SAlan Somers /*
179913b9329SAlan Somers  * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
180913b9329SAlan Somers  * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
181913b9329SAlan Somers  * vfs.aio.aio_listio_max.
182913b9329SAlan Somers  */
183c45796d5SAlan Somers SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
184913b9329SAlan Somers     CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
185913b9329SAlan Somers     0, "Maximum aio requests for a single lio_listio call");
186c45796d5SAlan Somers 
187399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
1880972628aSDavid Xu typedef struct oaiocb {
1890972628aSDavid Xu 	int	aio_fildes;		/* File descriptor */
1900972628aSDavid Xu 	off_t	aio_offset;		/* File offset for I/O */
1910972628aSDavid Xu 	volatile void *aio_buf;         /* I/O buffer in process space */
1920972628aSDavid Xu 	size_t	aio_nbytes;		/* Number of bytes for I/O */
1930972628aSDavid Xu 	struct	osigevent aio_sigevent;	/* Signal to deliver */
1940972628aSDavid Xu 	int	aio_lio_opcode;		/* LIO opcode */
1950972628aSDavid Xu 	int	aio_reqprio;		/* Request priority -- ignored */
1960972628aSDavid Xu 	struct	__aiocb_private	_aiocb_private;
1970972628aSDavid Xu } oaiocb_t;
198399e8c17SJohn Baldwin #endif
1990972628aSDavid Xu 
2001aa4c324SDavid Xu /*
2015652770dSJohn Baldwin  * Below is a key of locks used to protect each member of struct kaiocb
2021aa4c324SDavid Xu  * aioliojob and kaioinfo and any backends.
2031aa4c324SDavid Xu  *
2041aa4c324SDavid Xu  * * - need not protected
205759ccccaSDavid Xu  * a - locked by kaioinfo lock
2061aa4c324SDavid Xu  * b - locked by backend lock, the backend lock can be null in some cases,
2071aa4c324SDavid Xu  *     for example, BIO belongs to this type, in this case, proc lock is
2081aa4c324SDavid Xu  *     reused.
2091aa4c324SDavid Xu  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
2101aa4c324SDavid Xu  */
2111aa4c324SDavid Xu 
2121aa4c324SDavid Xu /*
213f3215338SJohn Baldwin  * If the routine that services an AIO request blocks while running in an
214f3215338SJohn Baldwin  * AIO kernel process it can starve other I/O requests.  BIO requests
21572bce9ffSAlan Somers  * queued via aio_qbio() complete asynchronously and do not use AIO kernel
216f3215338SJohn Baldwin  * processes at all.  Socket I/O requests use a separate pool of
217f3215338SJohn Baldwin  * kprocs and also force non-blocking I/O.  Other file I/O requests
218f3215338SJohn Baldwin  * use the generic fo_read/fo_write operations which can block.  The
219f3215338SJohn Baldwin  * fsync and mlock operations can also block while executing.  Ideally
220f3215338SJohn Baldwin  * none of these requests would block while executing.
221f3215338SJohn Baldwin  *
222f3215338SJohn Baldwin  * Note that the service routines cannot toggle O_NONBLOCK in the file
223f3215338SJohn Baldwin  * structure directly while handling a request due to races with
224f3215338SJohn Baldwin  * userland threads.
2251aa4c324SDavid Xu  */
2261aa4c324SDavid Xu 
22748dac059SAlan Cox /* jobflags */
228f3215338SJohn Baldwin #define	KAIOCB_QUEUEING		0x01
229f3215338SJohn Baldwin #define	KAIOCB_CANCELLED	0x02
230f3215338SJohn Baldwin #define	KAIOCB_CANCELLING	0x04
2315652770dSJohn Baldwin #define	KAIOCB_CHECKSYNC	0x08
232f3215338SJohn Baldwin #define	KAIOCB_CLEARED		0x10
233f3215338SJohn Baldwin #define	KAIOCB_FINISHED		0x20
23448dac059SAlan Cox 
2352244ea07SJohn Dyson /*
2362244ea07SJohn Dyson  * AIO process info
2372244ea07SJohn Dyson  */
23884af4da6SJohn Dyson #define AIOP_FREE	0x1			/* proc on free queue */
23984af4da6SJohn Dyson 
24039314b7dSJohn Baldwin struct aioproc {
24139314b7dSJohn Baldwin 	int	aioprocflags;			/* (c) AIO proc flags */
24239314b7dSJohn Baldwin 	TAILQ_ENTRY(aioproc) list;		/* (c) list of processes */
24339314b7dSJohn Baldwin 	struct	proc *aioproc;			/* (*) the AIO proc */
2442244ea07SJohn Dyson };
2452244ea07SJohn Dyson 
24684af4da6SJohn Dyson /*
24784af4da6SJohn Dyson  * data-structure for lio signal management
24884af4da6SJohn Dyson  */
2491ce91824SDavid Xu struct aioliojob {
2501aa4c324SDavid Xu 	int	lioj_flags;			/* (a) listio flags */
2515c5005ecSKonstantin Belousov 	int	lioj_count;			/* (a) count of jobs */
2525c5005ecSKonstantin Belousov 	int	lioj_finished_count;		/* (a) count of finished jobs */
2531aa4c324SDavid Xu 	struct	sigevent lioj_signal;		/* (a) signal on all I/O done */
2541aa4c324SDavid Xu 	TAILQ_ENTRY(aioliojob) lioj_list;	/* (a) lio list */
2551aa4c324SDavid Xu 	struct	knlist klist;			/* (a) list of knotes */
2561aa4c324SDavid Xu 	ksiginfo_t lioj_ksi;			/* (a) Realtime signal info */
25784af4da6SJohn Dyson };
2581ce91824SDavid Xu 
25984af4da6SJohn Dyson #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
26084af4da6SJohn Dyson #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
26169cd28daSDoug Ambrisko #define LIOJ_KEVENT_POSTED	0x4	/* kevent triggered */
26284af4da6SJohn Dyson 
26384af4da6SJohn Dyson /*
26484af4da6SJohn Dyson  * per process aio data structure
26584af4da6SJohn Dyson  */
2662244ea07SJohn Dyson struct kaioinfo {
267759ccccaSDavid Xu 	struct	mtx kaio_mtx;		/* the lock to protect this struct */
2681aa4c324SDavid Xu 	int	kaio_flags;		/* (a) per process kaio flags */
2691aa4c324SDavid Xu 	int	kaio_active_count;	/* (c) number of currently used AIOs */
2701aa4c324SDavid Xu 	int	kaio_count;		/* (a) size of AIO queue */
27172bce9ffSAlan Somers 	int	kaio_buffer_count;	/* (a) number of bio buffers */
2725652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_all;	/* (a) all AIOs in a process */
2735652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_done;	/* (a) done queue for process */
2741aa4c324SDavid Xu 	TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
2755652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_jobqueue;	/* (a) job queue for process */
2765652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncqueue;	/* (a) queue for aio_fsync */
277f3215338SJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncready;  /* (a) second q for aio_fsync */
27839314b7dSJohn Baldwin 	struct	task kaio_task;		/* (*) task to kick aio processes */
279f3215338SJohn Baldwin 	struct	task kaio_sync_task;	/* (*) task to schedule fsync jobs */
2802244ea07SJohn Dyson };
2812244ea07SJohn Dyson 
282759ccccaSDavid Xu #define AIO_LOCK(ki)		mtx_lock(&(ki)->kaio_mtx)
283759ccccaSDavid Xu #define AIO_UNLOCK(ki)		mtx_unlock(&(ki)->kaio_mtx)
284759ccccaSDavid Xu #define AIO_LOCK_ASSERT(ki, f)	mtx_assert(&(ki)->kaio_mtx, (f))
285759ccccaSDavid Xu #define AIO_MTX(ki)		(&(ki)->kaio_mtx)
286759ccccaSDavid Xu 
28784af4da6SJohn Dyson #define KAIO_RUNDOWN	0x1	/* process is being run down */
2880dd6c035SJohn Baldwin #define KAIO_WAKEUP	0x2	/* wakeup process when AIO completes */
289fd3bf775SJohn Dyson 
2903858a1f4SJohn Baldwin /*
2913858a1f4SJohn Baldwin  * Operations used to interact with userland aio control blocks.
2923858a1f4SJohn Baldwin  * Different ABIs provide their own operations.
2933858a1f4SJohn Baldwin  */
2943858a1f4SJohn Baldwin struct aiocb_ops {
295022ca2fcSAlan Somers 	int	(*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
2963858a1f4SJohn Baldwin 	long	(*fetch_status)(struct aiocb *ujob);
2973858a1f4SJohn Baldwin 	long	(*fetch_error)(struct aiocb *ujob);
2983858a1f4SJohn Baldwin 	int	(*store_status)(struct aiocb *ujob, long status);
2993858a1f4SJohn Baldwin 	int	(*store_error)(struct aiocb *ujob, long error);
3003858a1f4SJohn Baldwin 	int	(*store_kernelinfo)(struct aiocb *ujob, long jobref);
3013858a1f4SJohn Baldwin 	int	(*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
3023858a1f4SJohn Baldwin };
3033858a1f4SJohn Baldwin 
30439314b7dSJohn Baldwin static TAILQ_HEAD(,aioproc) aio_freeproc;		/* (c) Idle daemons */
3051ce91824SDavid Xu static struct sema aio_newproc_sem;
3061ce91824SDavid Xu static struct mtx aio_job_mtx;
3075652770dSJohn Baldwin static TAILQ_HEAD(,kaiocb) aio_jobs;			/* (c) Async job list */
3081ce91824SDavid Xu static struct unrhdr *aiod_unr;
3092244ea07SJohn Dyson 
310022ca2fcSAlan Somers static void	aio_biocleanup(struct bio *bp);
3116a1162d4SAlexander Leidinger void		aio_init_aioinfo(struct proc *p);
312723d37c0SKonstantin Belousov static int	aio_onceonly(void);
3135652770dSJohn Baldwin static int	aio_free_entry(struct kaiocb *job);
3145652770dSJohn Baldwin static void	aio_process_rw(struct kaiocb *job);
3155652770dSJohn Baldwin static void	aio_process_sync(struct kaiocb *job);
3165652770dSJohn Baldwin static void	aio_process_mlock(struct kaiocb *job);
317f3215338SJohn Baldwin static void	aio_schedule_fsync(void *context, int pending);
3181ce91824SDavid Xu static int	aio_newproc(int *);
3195652770dSJohn Baldwin int		aio_aqueue(struct thread *td, struct aiocb *ujob,
3203858a1f4SJohn Baldwin 		    struct aioliojob *lio, int type, struct aiocb_ops *ops);
321f3215338SJohn Baldwin static int	aio_queue_file(struct file *fp, struct kaiocb *job);
32272bce9ffSAlan Somers static void	aio_biowakeup(struct bio *bp);
32375b8b3b2SJohn Baldwin static void	aio_proc_rundown(void *arg, struct proc *p);
3240dd6c035SJohn Baldwin static void	aio_proc_rundown_exec(void *arg, struct proc *p,
3250dd6c035SJohn Baldwin 		    struct image_params *imgp);
32672bce9ffSAlan Somers static int	aio_qbio(struct proc *p, struct kaiocb *job);
3271ce91824SDavid Xu static void	aio_daemon(void *param);
328f3215338SJohn Baldwin static void	aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
329005ce8e4SJohn Baldwin static bool	aio_clear_cancel_function_locked(struct kaiocb *job);
330dbbccfe9SDavid Xu static int	aio_kick(struct proc *userp);
33199eee864SDavid Xu static void	aio_kick_nowait(struct proc *userp);
33299eee864SDavid Xu static void	aio_kick_helper(void *context, int pending);
33321d56e9cSAlfred Perlstein static int	filt_aioattach(struct knote *kn);
33421d56e9cSAlfred Perlstein static void	filt_aiodetach(struct knote *kn);
33521d56e9cSAlfred Perlstein static int	filt_aio(struct knote *kn, long hint);
33669cd28daSDoug Ambrisko static int	filt_lioattach(struct knote *kn);
33769cd28daSDoug Ambrisko static void	filt_liodetach(struct knote *kn);
33869cd28daSDoug Ambrisko static int	filt_lio(struct knote *kn, long hint);
3392244ea07SJohn Dyson 
340eb8e6d52SEivind Eklund /*
341eb8e6d52SEivind Eklund  * Zones for:
342eb8e6d52SEivind Eklund  * 	kaio	Per process async io info
34339314b7dSJohn Baldwin  *	aiop	async io process data
344eb8e6d52SEivind Eklund  *	aiocb	async io jobs
345eb8e6d52SEivind Eklund  *	aiolio	list io jobs
346eb8e6d52SEivind Eklund  */
347913b9329SAlan Somers static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiolio_zone;
348fd3bf775SJohn Dyson 
349eb8e6d52SEivind Eklund /* kqueue filters for aio */
350e76d823bSRobert Watson static struct filterops aio_filtops = {
351e76d823bSRobert Watson 	.f_isfd = 0,
352e76d823bSRobert Watson 	.f_attach = filt_aioattach,
353e76d823bSRobert Watson 	.f_detach = filt_aiodetach,
354e76d823bSRobert Watson 	.f_event = filt_aio,
355e76d823bSRobert Watson };
356e76d823bSRobert Watson static struct filterops lio_filtops = {
357e76d823bSRobert Watson 	.f_isfd = 0,
358e76d823bSRobert Watson 	.f_attach = filt_lioattach,
359e76d823bSRobert Watson 	.f_detach = filt_liodetach,
360e76d823bSRobert Watson 	.f_event = filt_lio
361e76d823bSRobert Watson };
36221d56e9cSAlfred Perlstein 
36375b8b3b2SJohn Baldwin static eventhandler_tag exit_tag, exec_tag;
36475b8b3b2SJohn Baldwin 
365c85650caSJohn Baldwin TASKQUEUE_DEFINE_THREAD(aiod_kick);
3661ce91824SDavid Xu 
367eb8e6d52SEivind Eklund /*
368eb8e6d52SEivind Eklund  * Main operations function for use as a kernel module.
369eb8e6d52SEivind Eklund  */
37021d56e9cSAlfred Perlstein static int
37121d56e9cSAlfred Perlstein aio_modload(struct module *module, int cmd, void *arg)
37221d56e9cSAlfred Perlstein {
37321d56e9cSAlfred Perlstein 	int error = 0;
37421d56e9cSAlfred Perlstein 
37521d56e9cSAlfred Perlstein 	switch (cmd) {
37621d56e9cSAlfred Perlstein 	case MOD_LOAD:
37721d56e9cSAlfred Perlstein 		aio_onceonly();
37821d56e9cSAlfred Perlstein 		break;
37921d56e9cSAlfred Perlstein 	case MOD_SHUTDOWN:
38021d56e9cSAlfred Perlstein 		break;
38121d56e9cSAlfred Perlstein 	default:
382f3215338SJohn Baldwin 		error = EOPNOTSUPP;
38321d56e9cSAlfred Perlstein 		break;
38421d56e9cSAlfred Perlstein 	}
38521d56e9cSAlfred Perlstein 	return (error);
38621d56e9cSAlfred Perlstein }
38721d56e9cSAlfred Perlstein 
38821d56e9cSAlfred Perlstein static moduledata_t aio_mod = {
38921d56e9cSAlfred Perlstein 	"aio",
39021d56e9cSAlfred Perlstein 	&aio_modload,
39121d56e9cSAlfred Perlstein 	NULL
39221d56e9cSAlfred Perlstein };
39321d56e9cSAlfred Perlstein 
394399e8c17SJohn Baldwin DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
39521d56e9cSAlfred Perlstein MODULE_VERSION(aio, 1);
39621d56e9cSAlfred Perlstein 
397fd3bf775SJohn Dyson /*
3982244ea07SJohn Dyson  * Startup initialization
3992244ea07SJohn Dyson  */
400723d37c0SKonstantin Belousov static int
40121d56e9cSAlfred Perlstein aio_onceonly(void)
402fd3bf775SJohn Dyson {
40321d56e9cSAlfred Perlstein 
40475b8b3b2SJohn Baldwin 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
40575b8b3b2SJohn Baldwin 	    EVENTHANDLER_PRI_ANY);
4060dd6c035SJohn Baldwin 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
4070dd6c035SJohn Baldwin 	    NULL, EVENTHANDLER_PRI_ANY);
40821d56e9cSAlfred Perlstein 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
40969cd28daSDoug Ambrisko 	kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
4102244ea07SJohn Dyson 	TAILQ_INIT(&aio_freeproc);
4111ce91824SDavid Xu 	sema_init(&aio_newproc_sem, 0, "aio_new_proc");
4121ce91824SDavid Xu 	mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
4132244ea07SJohn Dyson 	TAILQ_INIT(&aio_jobs);
4141ce91824SDavid Xu 	aiod_unr = new_unrhdr(1, INT_MAX, NULL);
415c897b813SJeff Roberson 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
416c897b813SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
41739314b7dSJohn Baldwin 	aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
418c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
4195652770dSJohn Baldwin 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
420c897b813SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
4211ce91824SDavid Xu 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
422c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
42384af4da6SJohn Dyson 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
424fd3bf775SJohn Dyson 	jobrefid = 1;
425399e8c17SJohn Baldwin 	p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
42686d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
42786d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
428723d37c0SKonstantin Belousov 
429723d37c0SKonstantin Belousov 	return (0);
4302244ea07SJohn Dyson }
4312244ea07SJohn Dyson 
432eb8e6d52SEivind Eklund /*
433bfbbc4aaSJason Evans  * Init the per-process aioinfo structure.  The aioinfo limits are set
434bfbbc4aaSJason Evans  * per-process for user limit (resource) management.
4352244ea07SJohn Dyson  */
4366a1162d4SAlexander Leidinger void
437fd3bf775SJohn Dyson aio_init_aioinfo(struct proc *p)
438fd3bf775SJohn Dyson {
4392244ea07SJohn Dyson 	struct kaioinfo *ki;
440ac41f2efSAlfred Perlstein 
441a163d034SWarner Losh 	ki = uma_zalloc(kaio_zone, M_WAITOK);
4429889bbacSKonstantin Belousov 	mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
44384af4da6SJohn Dyson 	ki->kaio_flags = 0;
4442244ea07SJohn Dyson 	ki->kaio_active_count = 0;
4451ce91824SDavid Xu 	ki->kaio_count = 0;
446fd3bf775SJohn Dyson 	ki->kaio_buffer_count = 0;
4471ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_all);
4481ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_done);
4492244ea07SJohn Dyson 	TAILQ_INIT(&ki->kaio_jobqueue);
45084af4da6SJohn Dyson 	TAILQ_INIT(&ki->kaio_liojoblist);
45199eee864SDavid Xu 	TAILQ_INIT(&ki->kaio_syncqueue);
452f3215338SJohn Baldwin 	TAILQ_INIT(&ki->kaio_syncready);
45399eee864SDavid Xu 	TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
454f3215338SJohn Baldwin 	TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
4553999ebe3SAlan Cox 	PROC_LOCK(p);
4563999ebe3SAlan Cox 	if (p->p_aioinfo == NULL) {
4573999ebe3SAlan Cox 		p->p_aioinfo = ki;
4583999ebe3SAlan Cox 		PROC_UNLOCK(p);
4593999ebe3SAlan Cox 	} else {
4603999ebe3SAlan Cox 		PROC_UNLOCK(p);
461759ccccaSDavid Xu 		mtx_destroy(&ki->kaio_mtx);
4623999ebe3SAlan Cox 		uma_zfree(kaio_zone, ki);
4632244ea07SJohn Dyson 	}
464bfbbc4aaSJason Evans 
46522035f47SOleksandr Tymoshenko 	while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
4661ce91824SDavid Xu 		aio_newproc(NULL);
4672244ea07SJohn Dyson }
4682244ea07SJohn Dyson 
4694c0fb2cfSDavid Xu static int
4706814c2daSKonstantin Belousov aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
4714c0fb2cfSDavid Xu {
472cf7d9a8cSDavid Xu 	struct thread *td;
473cf7d9a8cSDavid Xu 	int error;
474759ccccaSDavid Xu 
475cf7d9a8cSDavid Xu 	error = sigev_findtd(p, sigev, &td);
476cf7d9a8cSDavid Xu 	if (error)
477cf7d9a8cSDavid Xu 		return (error);
4784c0fb2cfSDavid Xu 	if (!KSI_ONQ(ksi)) {
479cf7d9a8cSDavid Xu 		ksiginfo_set_sigev(ksi, sigev);
4804c0fb2cfSDavid Xu 		ksi->ksi_code = SI_ASYNCIO;
4816814c2daSKonstantin Belousov 		ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
482cf7d9a8cSDavid Xu 		tdsendsignal(p, td, ksi->ksi_signo, ksi);
4834c0fb2cfSDavid Xu 	}
484759ccccaSDavid Xu 	PROC_UNLOCK(p);
485cf7d9a8cSDavid Xu 	return (error);
4864c0fb2cfSDavid Xu }
4874c0fb2cfSDavid Xu 
4882244ea07SJohn Dyson /*
489bfbbc4aaSJason Evans  * Free a job entry.  Wait for completion if it is currently active, but don't
490bfbbc4aaSJason Evans  * delay forever.  If we delay, we return a flag that says that we have to
491bfbbc4aaSJason Evans  * restart the queue scan.
4922244ea07SJohn Dyson  */
49388ed460eSAlan Cox static int
4945652770dSJohn Baldwin aio_free_entry(struct kaiocb *job)
495fd3bf775SJohn Dyson {
4962244ea07SJohn Dyson 	struct kaioinfo *ki;
4971ce91824SDavid Xu 	struct aioliojob *lj;
4982244ea07SJohn Dyson 	struct proc *p;
4992244ea07SJohn Dyson 
5005652770dSJohn Baldwin 	p = job->userproc;
5011ce91824SDavid Xu 	MPASS(curproc == p);
5022244ea07SJohn Dyson 	ki = p->p_aioinfo;
5031ce91824SDavid Xu 	MPASS(ki != NULL);
5041ce91824SDavid Xu 
505759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
506f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
507759ccccaSDavid Xu 
5081ce91824SDavid Xu 	atomic_subtract_int(&num_queue_count, 1);
5091ce91824SDavid Xu 
5101ce91824SDavid Xu 	ki->kaio_count--;
5111ce91824SDavid Xu 	MPASS(ki->kaio_count >= 0);
5121ce91824SDavid Xu 
5135652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_done, job, plist);
5145652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_all, job, allist);
51527b8220dSDavid Xu 
5165652770dSJohn Baldwin 	lj = job->lio;
51784af4da6SJohn Dyson 	if (lj) {
5181ce91824SDavid Xu 		lj->lioj_count--;
5191ce91824SDavid Xu 		lj->lioj_finished_count--;
5201ce91824SDavid Xu 
521a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
5221ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
5231ce91824SDavid Xu 			/* lio is going away, we need to destroy any knotes */
5241ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
525759ccccaSDavid Xu 			PROC_LOCK(p);
5261ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
527759ccccaSDavid Xu 			PROC_UNLOCK(p);
5281ce91824SDavid Xu 			uma_zfree(aiolio_zone, lj);
52984af4da6SJohn Dyson 		}
53084af4da6SJohn Dyson 	}
5311ce91824SDavid Xu 
5325652770dSJohn Baldwin 	/* job is going away, we need to destroy any knotes */
5335652770dSJohn Baldwin 	knlist_delete(&job->klist, curthread, 1);
534759ccccaSDavid Xu 	PROC_LOCK(p);
5355652770dSJohn Baldwin 	sigqueue_take(&job->ksi);
536759ccccaSDavid Xu 	PROC_UNLOCK(p);
5371ce91824SDavid Xu 
538759ccccaSDavid Xu 	AIO_UNLOCK(ki);
5392a522eb9SJohn Baldwin 
5402a522eb9SJohn Baldwin 	/*
5412a522eb9SJohn Baldwin 	 * The thread argument here is used to find the owning process
5422a522eb9SJohn Baldwin 	 * and is also passed to fo_close() which may pass it to various
5432a522eb9SJohn Baldwin 	 * places such as devsw close() routines.  Because of that, we
5442a522eb9SJohn Baldwin 	 * need a thread pointer from the process owning the job that is
5452a522eb9SJohn Baldwin 	 * persistent and won't disappear out from under us or move to
5462a522eb9SJohn Baldwin 	 * another process.
5472a522eb9SJohn Baldwin 	 *
5482a522eb9SJohn Baldwin 	 * Currently, all the callers of this function call it to remove
5495652770dSJohn Baldwin 	 * a kaiocb from the current process' job list either via a
5502a522eb9SJohn Baldwin 	 * syscall or due to the current process calling exit() or
5512a522eb9SJohn Baldwin 	 * execve().  Thus, we know that p == curproc.  We also know that
5522a522eb9SJohn Baldwin 	 * curthread can't exit since we are curthread.
5532a522eb9SJohn Baldwin 	 *
5542a522eb9SJohn Baldwin 	 * Therefore, we use curthread as the thread to pass to
5552a522eb9SJohn Baldwin 	 * knlist_delete().  This does mean that it is possible for the
5562a522eb9SJohn Baldwin 	 * thread pointer at close time to differ from the thread pointer
5572a522eb9SJohn Baldwin 	 * at open time, but this is already true of file descriptors in
5582a522eb9SJohn Baldwin 	 * a multithreaded process.
559b40ce416SJulian Elischer 	 */
5605652770dSJohn Baldwin 	if (job->fd_file)
5615652770dSJohn Baldwin 		fdrop(job->fd_file, curthread);
5625652770dSJohn Baldwin 	crfree(job->cred);
563022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
564022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
5655652770dSJohn Baldwin 	uma_zfree(aiocb_zone, job);
566759ccccaSDavid Xu 	AIO_LOCK(ki);
5671ce91824SDavid Xu 
568ac41f2efSAlfred Perlstein 	return (0);
5692244ea07SJohn Dyson }
5702244ea07SJohn Dyson 
571993182e5SAlexander Leidinger static void
5720dd6c035SJohn Baldwin aio_proc_rundown_exec(void *arg, struct proc *p,
5730dd6c035SJohn Baldwin     struct image_params *imgp __unused)
574993182e5SAlexander Leidinger {
575993182e5SAlexander Leidinger    	aio_proc_rundown(arg, p);
576993182e5SAlexander Leidinger }
577993182e5SAlexander Leidinger 
578f3215338SJohn Baldwin static int
579f3215338SJohn Baldwin aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
580f3215338SJohn Baldwin {
581f3215338SJohn Baldwin 	aio_cancel_fn_t *func;
582f3215338SJohn Baldwin 	int cancelled;
583f3215338SJohn Baldwin 
584f3215338SJohn Baldwin 	AIO_LOCK_ASSERT(ki, MA_OWNED);
585f3215338SJohn Baldwin 	if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
586f3215338SJohn Baldwin 		return (0);
587f3215338SJohn Baldwin 	MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
588f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLED;
589f3215338SJohn Baldwin 
590f3215338SJohn Baldwin 	func = job->cancel_fn;
591f3215338SJohn Baldwin 
592f3215338SJohn Baldwin 	/*
593f3215338SJohn Baldwin 	 * If there is no cancel routine, just leave the job marked as
594f3215338SJohn Baldwin 	 * cancelled.  The job should be in active use by a caller who
595f3215338SJohn Baldwin 	 * should complete it normally or when it fails to install a
596f3215338SJohn Baldwin 	 * cancel routine.
597f3215338SJohn Baldwin 	 */
598f3215338SJohn Baldwin 	if (func == NULL)
599f3215338SJohn Baldwin 		return (0);
600f3215338SJohn Baldwin 
601f3215338SJohn Baldwin 	/*
602f3215338SJohn Baldwin 	 * Set the CANCELLING flag so that aio_complete() will defer
603f3215338SJohn Baldwin 	 * completions of this job.  This prevents the job from being
604f3215338SJohn Baldwin 	 * freed out from under the cancel callback.  After the
605f3215338SJohn Baldwin 	 * callback any deferred completion (whether from the callback
606f3215338SJohn Baldwin 	 * or any other source) will be completed.
607f3215338SJohn Baldwin 	 */
608f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLING;
609f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
610f3215338SJohn Baldwin 	func(job);
611f3215338SJohn Baldwin 	AIO_LOCK(ki);
612f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_CANCELLING;
613f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
614f3215338SJohn Baldwin 		cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
615f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
616f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
617f3215338SJohn Baldwin 	} else {
618f3215338SJohn Baldwin 		/*
619f3215338SJohn Baldwin 		 * The cancel callback might have scheduled an
620f3215338SJohn Baldwin 		 * operation to cancel this request, but it is
621f3215338SJohn Baldwin 		 * only counted as cancelled if the request is
622f3215338SJohn Baldwin 		 * cancelled when the callback returns.
623f3215338SJohn Baldwin 		 */
624f3215338SJohn Baldwin 		cancelled = 0;
625f3215338SJohn Baldwin 	}
626f3215338SJohn Baldwin 	return (cancelled);
627f3215338SJohn Baldwin }
628f3215338SJohn Baldwin 
6292244ea07SJohn Dyson /*
6302244ea07SJohn Dyson  * Rundown the jobs for a given process.
6312244ea07SJohn Dyson  */
63221d56e9cSAlfred Perlstein static void
63375b8b3b2SJohn Baldwin aio_proc_rundown(void *arg, struct proc *p)
634fd3bf775SJohn Dyson {
6352244ea07SJohn Dyson 	struct kaioinfo *ki;
6361ce91824SDavid Xu 	struct aioliojob *lj;
6375652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
6382244ea07SJohn Dyson 
6392a522eb9SJohn Baldwin 	KASSERT(curthread->td_proc == p,
6402a522eb9SJohn Baldwin 	    ("%s: called on non-curproc", __func__));
6412244ea07SJohn Dyson 	ki = p->p_aioinfo;
6422244ea07SJohn Dyson 	if (ki == NULL)
6432244ea07SJohn Dyson 		return;
6442244ea07SJohn Dyson 
645759ccccaSDavid Xu 	AIO_LOCK(ki);
64627b8220dSDavid Xu 	ki->kaio_flags |= KAIO_RUNDOWN;
6471ce91824SDavid Xu 
6481ce91824SDavid Xu restart:
649a624e84fSJohn Dyson 
650bfbbc4aaSJason Evans 	/*
6511ce91824SDavid Xu 	 * Try to cancel all pending requests. This code simulates
6521ce91824SDavid Xu 	 * aio_cancel on all pending I/O requests.
653bfbbc4aaSJason Evans 	 */
6545652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
655f3215338SJohn Baldwin 		aio_cancel_job(p, ki, job);
6562244ea07SJohn Dyson 	}
65784af4da6SJohn Dyson 
6581ce91824SDavid Xu 	/* Wait for all running I/O to be finished */
659f3215338SJohn Baldwin 	if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
66084af4da6SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
661759ccccaSDavid Xu 		msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
6621ce91824SDavid Xu 		goto restart;
66384af4da6SJohn Dyson 	}
66484af4da6SJohn Dyson 
6651ce91824SDavid Xu 	/* Free all completed I/O requests. */
6665652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
6675652770dSJohn Baldwin 		aio_free_entry(job);
66884af4da6SJohn Dyson 
6691ce91824SDavid Xu 	while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
670a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
67184af4da6SJohn Dyson 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
6721ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
673759ccccaSDavid Xu 			PROC_LOCK(p);
6741ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
675759ccccaSDavid Xu 			PROC_UNLOCK(p);
676c897b813SJeff Roberson 			uma_zfree(aiolio_zone, lj);
677f4f0ecefSJohn Dyson 		} else {
678a9bf5e37SDavid Xu 			panic("LIO job not cleaned up: C:%d, FC:%d\n",
679a9bf5e37SDavid Xu 			    lj->lioj_count, lj->lioj_finished_count);
68084af4da6SJohn Dyson 		}
681f4f0ecefSJohn Dyson 	}
682759ccccaSDavid Xu 	AIO_UNLOCK(ki);
683c85650caSJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
684f3215338SJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
6855114048bSKonstantin Belousov 	mtx_destroy(&ki->kaio_mtx);
686c897b813SJeff Roberson 	uma_zfree(kaio_zone, ki);
687a624e84fSJohn Dyson 	p->p_aioinfo = NULL;
6882244ea07SJohn Dyson }
6892244ea07SJohn Dyson 
6902244ea07SJohn Dyson /*
691bfbbc4aaSJason Evans  * Select a job to run (called by an AIO daemon).
6922244ea07SJohn Dyson  */
6935652770dSJohn Baldwin static struct kaiocb *
69439314b7dSJohn Baldwin aio_selectjob(struct aioproc *aiop)
695fd3bf775SJohn Dyson {
6965652770dSJohn Baldwin 	struct kaiocb *job;
697bfbbc4aaSJason Evans 	struct kaioinfo *ki;
698bfbbc4aaSJason Evans 	struct proc *userp;
6992244ea07SJohn Dyson 
7001ce91824SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
701f3215338SJohn Baldwin restart:
7025652770dSJohn Baldwin 	TAILQ_FOREACH(job, &aio_jobs, list) {
7035652770dSJohn Baldwin 		userp = job->userproc;
7042244ea07SJohn Dyson 		ki = userp->p_aioinfo;
7052244ea07SJohn Dyson 
70686bbef43SJohn Baldwin 		if (ki->kaio_active_count < max_aio_per_proc) {
7075652770dSJohn Baldwin 			TAILQ_REMOVE(&aio_jobs, job, list);
708f3215338SJohn Baldwin 			if (!aio_clear_cancel_function(job))
709f3215338SJohn Baldwin 				goto restart;
710f3215338SJohn Baldwin 
7111ce91824SDavid Xu 			/* Account for currently active jobs. */
7121ce91824SDavid Xu 			ki->kaio_active_count++;
7131ce91824SDavid Xu 			break;
7141ce91824SDavid Xu 		}
7151ce91824SDavid Xu 	}
7165652770dSJohn Baldwin 	return (job);
7172244ea07SJohn Dyson }
7182244ea07SJohn Dyson 
7192244ea07SJohn Dyson /*
7200dd6c035SJohn Baldwin  * Move all data to a permanent storage device.  This code
721801ac943SThomas Munro  * simulates the fsync and fdatasync syscalls.
72299eee864SDavid Xu  */
72399eee864SDavid Xu static int
724801ac943SThomas Munro aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
72599eee864SDavid Xu {
72699eee864SDavid Xu 	struct mount *mp;
727922bee44SKonstantin Belousov 	vm_object_t obj;
72899eee864SDavid Xu 	int error;
72999eee864SDavid Xu 
730922bee44SKonstantin Belousov 	for (;;) {
731922bee44SKonstantin Belousov 		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
732922bee44SKonstantin Belousov 		if (error != 0)
733922bee44SKonstantin Belousov 			break;
734cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
735922bee44SKonstantin Belousov 		obj = vp->v_object;
736922bee44SKonstantin Belousov 		if (obj != NULL) {
737922bee44SKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
738922bee44SKonstantin Belousov 			vm_object_page_clean(obj, 0, 0, 0);
739922bee44SKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
74099eee864SDavid Xu 		}
741801ac943SThomas Munro 		if (op == LIO_DSYNC)
742801ac943SThomas Munro 			error = VOP_FDATASYNC(vp, td);
743801ac943SThomas Munro 		else
74499eee864SDavid Xu 			error = VOP_FSYNC(vp, MNT_WAIT, td);
74599eee864SDavid Xu 
746b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
74799eee864SDavid Xu 		vn_finished_write(mp);
7482933a7caSKonstantin Belousov 		if (error != ERELOOKUP)
749922bee44SKonstantin Belousov 			break;
750922bee44SKonstantin Belousov 	}
75199eee864SDavid Xu 	return (error);
75299eee864SDavid Xu }
75399eee864SDavid Xu 
75499eee864SDavid Xu /*
755f95c13dbSGleb Smirnoff  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
75672bce9ffSAlan Somers  * does the I/O request for the non-bio version of the operations.  The normal
75772bce9ffSAlan Somers  * vn operations are used, and this code should work in all instances for every
75872bce9ffSAlan Somers  * type of file, including pipes, sockets, fifos, and regular files.
7591ce91824SDavid Xu  *
7601aa4c324SDavid Xu  * XXX I don't think it works well for socket, pipe, and fifo.
7612244ea07SJohn Dyson  */
76288ed460eSAlan Cox static void
7635652770dSJohn Baldwin aio_process_rw(struct kaiocb *job)
764fd3bf775SJohn Dyson {
765f8f750c5SRobert Watson 	struct ucred *td_savedcred;
766b40ce416SJulian Elischer 	struct thread *td;
7672244ea07SJohn Dyson 	struct aiocb *cb;
7682244ea07SJohn Dyson 	struct file *fp;
769bb430bc7SJohn Baldwin 	ssize_t cnt;
770b1012d80SJohn Baldwin 	long msgsnd_st, msgsnd_end;
771b1012d80SJohn Baldwin 	long msgrcv_st, msgrcv_end;
772b1012d80SJohn Baldwin 	long oublock_st, oublock_end;
773b1012d80SJohn Baldwin 	long inblock_st, inblock_end;
774022ca2fcSAlan Somers 	int error, opcode;
7752244ea07SJohn Dyson 
7765652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
777022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_READV ||
778022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITE ||
779022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITEV,
7805652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
781f95c13dbSGleb Smirnoff 
782f3215338SJohn Baldwin 	aio_switch_vmspace(job);
783b40ce416SJulian Elischer 	td = curthread;
784f8f750c5SRobert Watson 	td_savedcred = td->td_ucred;
7855652770dSJohn Baldwin 	td->td_ucred = job->cred;
786022ca2fcSAlan Somers 	job->uiop->uio_td = td;
7875652770dSJohn Baldwin 	cb = &job->uaiocb;
7885652770dSJohn Baldwin 	fp = job->fd_file;
789bfbbc4aaSJason Evans 
790022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
791022ca2fcSAlan Somers 	cnt = job->uiop->uio_resid;
7922244ea07SJohn Dyson 
793b1012d80SJohn Baldwin 	msgrcv_st = td->td_ru.ru_msgrcv;
794b1012d80SJohn Baldwin 	msgsnd_st = td->td_ru.ru_msgsnd;
7951c4bcd05SJeff Roberson 	inblock_st = td->td_ru.ru_inblock;
7961c4bcd05SJeff Roberson 	oublock_st = td->td_ru.ru_oublock;
797b1012d80SJohn Baldwin 
798279d7226SMatthew Dillon 	/*
799a9bf5e37SDavid Xu 	 * aio_aqueue() acquires a reference to the file that is
8009b16adc1SAlan Cox 	 * released in aio_free_entry().
801279d7226SMatthew Dillon 	 */
802022ca2fcSAlan Somers 	if (opcode == LIO_READ || opcode == LIO_READV) {
803022ca2fcSAlan Somers 		if (job->uiop->uio_resid == 0)
8045114048bSKonstantin Belousov 			error = 0;
8055114048bSKonstantin Belousov 		else
806022ca2fcSAlan Somers 			error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
807022ca2fcSAlan Somers 			    td);
8082244ea07SJohn Dyson 	} else {
8096d53aa62SDavid Xu 		if (fp->f_type == DTYPE_VNODE)
8106d53aa62SDavid Xu 			bwillwrite();
811022ca2fcSAlan Somers 		error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
8122244ea07SJohn Dyson 	}
813b1012d80SJohn Baldwin 	msgrcv_end = td->td_ru.ru_msgrcv;
814b1012d80SJohn Baldwin 	msgsnd_end = td->td_ru.ru_msgsnd;
8151c4bcd05SJeff Roberson 	inblock_end = td->td_ru.ru_inblock;
8161c4bcd05SJeff Roberson 	oublock_end = td->td_ru.ru_oublock;
817fd3bf775SJohn Dyson 
818b1012d80SJohn Baldwin 	job->msgrcv = msgrcv_end - msgrcv_st;
819b1012d80SJohn Baldwin 	job->msgsnd = msgsnd_end - msgsnd_st;
820b1012d80SJohn Baldwin 	job->inblock = inblock_end - inblock_st;
821b1012d80SJohn Baldwin 	job->outblock = oublock_end - oublock_st;
8222244ea07SJohn Dyson 
823022ca2fcSAlan Somers 	if (error != 0 && job->uiop->uio_resid != cnt) {
8242244ea07SJohn Dyson 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
8252244ea07SJohn Dyson 			error = 0;
8262247f489SAlan Somers 		if (error == EPIPE && (opcode & LIO_WRITE)) {
8275652770dSJohn Baldwin 			PROC_LOCK(job->userproc);
8285652770dSJohn Baldwin 			kern_psignal(job->userproc, SIGPIPE);
8295652770dSJohn Baldwin 			PROC_UNLOCK(job->userproc);
83019eb87d2SJohn Baldwin 		}
8312244ea07SJohn Dyson 	}
8322244ea07SJohn Dyson 
833022ca2fcSAlan Somers 	cnt -= job->uiop->uio_resid;
834f8f750c5SRobert Watson 	td->td_ucred = td_savedcred;
835f0ec1740SJohn Baldwin 	if (error)
836f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
837f0ec1740SJohn Baldwin 	else
838f0ec1740SJohn Baldwin 		aio_complete(job, cnt, 0);
8392244ea07SJohn Dyson }
8402244ea07SJohn Dyson 
84169cd28daSDoug Ambrisko static void
8425652770dSJohn Baldwin aio_process_sync(struct kaiocb *job)
843f95c13dbSGleb Smirnoff {
844f95c13dbSGleb Smirnoff 	struct thread *td = curthread;
845f95c13dbSGleb Smirnoff 	struct ucred *td_savedcred = td->td_ucred;
8465652770dSJohn Baldwin 	struct file *fp = job->fd_file;
847f95c13dbSGleb Smirnoff 	int error = 0;
848f95c13dbSGleb Smirnoff 
8492247f489SAlan Somers 	KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
8505652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
851f95c13dbSGleb Smirnoff 
8525652770dSJohn Baldwin 	td->td_ucred = job->cred;
853801ac943SThomas Munro 	if (fp->f_vnode != NULL) {
854801ac943SThomas Munro 		error = aio_fsync_vnode(td, fp->f_vnode,
855801ac943SThomas Munro 		    job->uaiocb.aio_lio_opcode);
856801ac943SThomas Munro 	}
857f95c13dbSGleb Smirnoff 	td->td_ucred = td_savedcred;
858f0ec1740SJohn Baldwin 	if (error)
859f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
860f0ec1740SJohn Baldwin 	else
861f0ec1740SJohn Baldwin 		aio_complete(job, 0, 0);
862f95c13dbSGleb Smirnoff }
863f95c13dbSGleb Smirnoff 
864f95c13dbSGleb Smirnoff static void
8655652770dSJohn Baldwin aio_process_mlock(struct kaiocb *job)
8666160e12cSGleb Smirnoff {
8675652770dSJohn Baldwin 	struct aiocb *cb = &job->uaiocb;
8686160e12cSGleb Smirnoff 	int error;
8696160e12cSGleb Smirnoff 
8705652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
8715652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
8726160e12cSGleb Smirnoff 
873f3215338SJohn Baldwin 	aio_switch_vmspace(job);
874496ab053SKonstantin Belousov 	error = kern_mlock(job->userproc, job->cred,
875496ab053SKonstantin Belousov 	    __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
876496ab053SKonstantin Belousov 	aio_complete(job, error != 0 ? -1 : 0, error);
8776160e12cSGleb Smirnoff }
8786160e12cSGleb Smirnoff 
8796160e12cSGleb Smirnoff static void
880f3215338SJohn Baldwin aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
8811ce91824SDavid Xu {
8821ce91824SDavid Xu 	struct aioliojob *lj;
88369cd28daSDoug Ambrisko 	struct kaioinfo *ki;
8845652770dSJohn Baldwin 	struct kaiocb *sjob, *sjobn;
8851ce91824SDavid Xu 	int lj_done;
886f3215338SJohn Baldwin 	bool schedule_fsync;
88769cd28daSDoug Ambrisko 
88869cd28daSDoug Ambrisko 	ki = userp->p_aioinfo;
889759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
8905652770dSJohn Baldwin 	lj = job->lio;
89169cd28daSDoug Ambrisko 	lj_done = 0;
89269cd28daSDoug Ambrisko 	if (lj) {
8931ce91824SDavid Xu 		lj->lioj_finished_count++;
8941ce91824SDavid Xu 		if (lj->lioj_count == lj->lioj_finished_count)
89569cd28daSDoug Ambrisko 			lj_done = 1;
89669cd28daSDoug Ambrisko 	}
8975652770dSJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
898f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
89927b8220dSDavid Xu 
90027b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_RUNDOWN)
90127b8220dSDavid Xu 		goto notification_done;
90227b8220dSDavid Xu 
9035652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
9045652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
9056814c2daSKonstantin Belousov 		aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
9061ce91824SDavid Xu 
9075652770dSJohn Baldwin 	KNOTE_LOCKED(&job->klist, 1);
9081ce91824SDavid Xu 
90969cd28daSDoug Ambrisko 	if (lj_done) {
9101ce91824SDavid Xu 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
91169cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
9121ce91824SDavid Xu 			KNOTE_LOCKED(&lj->klist, 1);
91369cd28daSDoug Ambrisko 		}
9141ce91824SDavid Xu 		if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
91529331656SKonstantin Belousov 		    == LIOJ_SIGNAL &&
91629331656SKonstantin Belousov 		    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
9174c0fb2cfSDavid Xu 		    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
9186814c2daSKonstantin Belousov 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
9196814c2daSKonstantin Belousov 			    true);
92069cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
92169cd28daSDoug Ambrisko 		}
92269cd28daSDoug Ambrisko 	}
92327b8220dSDavid Xu 
92427b8220dSDavid Xu notification_done:
9255652770dSJohn Baldwin 	if (job->jobflags & KAIOCB_CHECKSYNC) {
926f3215338SJohn Baldwin 		schedule_fsync = false;
9275652770dSJohn Baldwin 		TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
928b9a53e16SJohn Baldwin 			if (job->fd_file != sjob->fd_file ||
929b9a53e16SJohn Baldwin 			    job->seqno >= sjob->seqno)
930b9a53e16SJohn Baldwin 				continue;
931b9a53e16SJohn Baldwin 			if (--sjob->pending > 0)
932b9a53e16SJohn Baldwin 				continue;
933b9a53e16SJohn Baldwin 			TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
934005ce8e4SJohn Baldwin 			if (!aio_clear_cancel_function_locked(sjob))
935f3215338SJohn Baldwin 				continue;
936b9a53e16SJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
937f3215338SJohn Baldwin 			schedule_fsync = true;
93899eee864SDavid Xu 		}
939f3215338SJohn Baldwin 		if (schedule_fsync)
940f3215338SJohn Baldwin 			taskqueue_enqueue(taskqueue_aiod_kick,
941f3215338SJohn Baldwin 			    &ki->kaio_sync_task);
94299eee864SDavid Xu 	}
94327b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_WAKEUP) {
94469cd28daSDoug Ambrisko 		ki->kaio_flags &= ~KAIO_WAKEUP;
9451ce91824SDavid Xu 		wakeup(&userp->p_aioinfo);
94669cd28daSDoug Ambrisko 	}
94769cd28daSDoug Ambrisko }
94869cd28daSDoug Ambrisko 
9498a4dc40fSJohn Baldwin static void
950f3215338SJohn Baldwin aio_schedule_fsync(void *context, int pending)
951f3215338SJohn Baldwin {
952f3215338SJohn Baldwin 	struct kaioinfo *ki;
953f3215338SJohn Baldwin 	struct kaiocb *job;
954f3215338SJohn Baldwin 
955f3215338SJohn Baldwin 	ki = context;
956f3215338SJohn Baldwin 	AIO_LOCK(ki);
957f3215338SJohn Baldwin 	while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
958f3215338SJohn Baldwin 		job = TAILQ_FIRST(&ki->kaio_syncready);
959f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncready, job, list);
960f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
961f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
962f3215338SJohn Baldwin 		AIO_LOCK(ki);
963f3215338SJohn Baldwin 	}
964f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
965f3215338SJohn Baldwin }
966f3215338SJohn Baldwin 
967f3215338SJohn Baldwin bool
968f3215338SJohn Baldwin aio_cancel_cleared(struct kaiocb *job)
969f3215338SJohn Baldwin {
970f3215338SJohn Baldwin 
971f3215338SJohn Baldwin 	/*
972f3215338SJohn Baldwin 	 * The caller should hold the same queue lock held when
973f3215338SJohn Baldwin 	 * aio_clear_cancel_function() was called and set this flag
974f3215338SJohn Baldwin 	 * ensuring this check sees an up-to-date value.  However,
975f3215338SJohn Baldwin 	 * there is no way to assert that.
976f3215338SJohn Baldwin 	 */
977f3215338SJohn Baldwin 	return ((job->jobflags & KAIOCB_CLEARED) != 0);
978f3215338SJohn Baldwin }
979f3215338SJohn Baldwin 
980005ce8e4SJohn Baldwin static bool
981005ce8e4SJohn Baldwin aio_clear_cancel_function_locked(struct kaiocb *job)
982005ce8e4SJohn Baldwin {
983005ce8e4SJohn Baldwin 
984005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
985005ce8e4SJohn Baldwin 	MPASS(job->cancel_fn != NULL);
986005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLING) {
987005ce8e4SJohn Baldwin 		job->jobflags |= KAIOCB_CLEARED;
988005ce8e4SJohn Baldwin 		return (false);
989005ce8e4SJohn Baldwin 	}
990005ce8e4SJohn Baldwin 	job->cancel_fn = NULL;
991005ce8e4SJohn Baldwin 	return (true);
992005ce8e4SJohn Baldwin }
993005ce8e4SJohn Baldwin 
994f3215338SJohn Baldwin bool
995f3215338SJohn Baldwin aio_clear_cancel_function(struct kaiocb *job)
996f3215338SJohn Baldwin {
997f3215338SJohn Baldwin 	struct kaioinfo *ki;
998005ce8e4SJohn Baldwin 	bool ret;
999f3215338SJohn Baldwin 
1000f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1001f3215338SJohn Baldwin 	AIO_LOCK(ki);
1002005ce8e4SJohn Baldwin 	ret = aio_clear_cancel_function_locked(job);
1003f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1004005ce8e4SJohn Baldwin 	return (ret);
1005f3215338SJohn Baldwin }
1006005ce8e4SJohn Baldwin 
1007005ce8e4SJohn Baldwin static bool
1008005ce8e4SJohn Baldwin aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1009005ce8e4SJohn Baldwin {
1010005ce8e4SJohn Baldwin 
1011005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1012005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLED)
1013005ce8e4SJohn Baldwin 		return (false);
1014005ce8e4SJohn Baldwin 	job->cancel_fn = func;
1015f3215338SJohn Baldwin 	return (true);
1016f3215338SJohn Baldwin }
1017f3215338SJohn Baldwin 
1018f3215338SJohn Baldwin bool
1019f3215338SJohn Baldwin aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1020f3215338SJohn Baldwin {
1021f3215338SJohn Baldwin 	struct kaioinfo *ki;
1022005ce8e4SJohn Baldwin 	bool ret;
1023f3215338SJohn Baldwin 
1024f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1025f3215338SJohn Baldwin 	AIO_LOCK(ki);
1026005ce8e4SJohn Baldwin 	ret = aio_set_cancel_function_locked(job, func);
1027f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1028005ce8e4SJohn Baldwin 	return (ret);
1029f3215338SJohn Baldwin }
1030f3215338SJohn Baldwin 
1031f3215338SJohn Baldwin void
1032f3215338SJohn Baldwin aio_complete(struct kaiocb *job, long status, int error)
1033f3215338SJohn Baldwin {
1034f3215338SJohn Baldwin 	struct kaioinfo *ki;
1035f3215338SJohn Baldwin 	struct proc *userp;
1036f3215338SJohn Baldwin 
1037f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.error = error;
1038f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.status = status;
1039f3215338SJohn Baldwin 
1040f3215338SJohn Baldwin 	userp = job->userproc;
1041f3215338SJohn Baldwin 	ki = userp->p_aioinfo;
1042f3215338SJohn Baldwin 
1043f3215338SJohn Baldwin 	AIO_LOCK(ki);
1044f3215338SJohn Baldwin 	KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1045f3215338SJohn Baldwin 	    ("duplicate aio_complete"));
1046f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_FINISHED;
1047f3215338SJohn Baldwin 	if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1048f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1049f3215338SJohn Baldwin 		aio_bio_done_notify(userp, job);
1050f3215338SJohn Baldwin 	}
1051f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1052f3215338SJohn Baldwin }
1053f3215338SJohn Baldwin 
1054f3215338SJohn Baldwin void
1055f3215338SJohn Baldwin aio_cancel(struct kaiocb *job)
1056f3215338SJohn Baldwin {
1057f3215338SJohn Baldwin 
1058f3215338SJohn Baldwin 	aio_complete(job, -1, ECANCELED);
1059f3215338SJohn Baldwin }
1060f3215338SJohn Baldwin 
1061f3215338SJohn Baldwin void
10625652770dSJohn Baldwin aio_switch_vmspace(struct kaiocb *job)
10638a4dc40fSJohn Baldwin {
10648a4dc40fSJohn Baldwin 
10655652770dSJohn Baldwin 	vmspace_switch_aio(job->userproc->p_vmspace);
10668a4dc40fSJohn Baldwin }
10678a4dc40fSJohn Baldwin 
10682244ea07SJohn Dyson /*
1069f95c13dbSGleb Smirnoff  * The AIO daemon, most of the actual work is done in aio_process_*,
107084af4da6SJohn Dyson  * but the setup (and address space mgmt) is done in this routine.
10712244ea07SJohn Dyson  */
10722244ea07SJohn Dyson static void
10731ce91824SDavid Xu aio_daemon(void *_id)
10742244ea07SJohn Dyson {
10755652770dSJohn Baldwin 	struct kaiocb *job;
107639314b7dSJohn Baldwin 	struct aioproc *aiop;
1077bfbbc4aaSJason Evans 	struct kaioinfo *ki;
1078f3215338SJohn Baldwin 	struct proc *p;
10798a4dc40fSJohn Baldwin 	struct vmspace *myvm;
1080b40ce416SJulian Elischer 	struct thread *td = curthread;
10811ce91824SDavid Xu 	int id = (intptr_t)_id;
10822244ea07SJohn Dyson 
10832244ea07SJohn Dyson 	/*
10848a4dc40fSJohn Baldwin 	 * Grab an extra reference on the daemon's vmspace so that it
10858a4dc40fSJohn Baldwin 	 * doesn't get freed by jobs that switch to a different
10868a4dc40fSJohn Baldwin 	 * vmspace.
10872244ea07SJohn Dyson 	 */
10888a4dc40fSJohn Baldwin 	p = td->td_proc;
10898a4dc40fSJohn Baldwin 	myvm = vmspace_acquire_ref(p);
1090fd3bf775SJohn Dyson 
10918a4dc40fSJohn Baldwin 	KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1092fd3bf775SJohn Dyson 
1093fd3bf775SJohn Dyson 	/*
1094bfbbc4aaSJason Evans 	 * Allocate and ready the aio control info.  There is one aiop structure
1095bfbbc4aaSJason Evans 	 * per daemon.
1096fd3bf775SJohn Dyson 	 */
1097a163d034SWarner Losh 	aiop = uma_zalloc(aiop_zone, M_WAITOK);
109839314b7dSJohn Baldwin 	aiop->aioproc = p;
109939314b7dSJohn Baldwin 	aiop->aioprocflags = 0;
1100bfbbc4aaSJason Evans 
1101fd3bf775SJohn Dyson 	/*
1102fd3bf775SJohn Dyson 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1103b40ce416SJulian Elischer 	 * and creating too many daemons.)
1104fd3bf775SJohn Dyson 	 */
11051ce91824SDavid Xu 	sema_post(&aio_newproc_sem);
11062244ea07SJohn Dyson 
11071ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
1108bfbbc4aaSJason Evans 	for (;;) {
1109fd3bf775SJohn Dyson 		/*
1110fd3bf775SJohn Dyson 		 * Take daemon off of free queue
1111fd3bf775SJohn Dyson 		 */
111239314b7dSJohn Baldwin 		if (aiop->aioprocflags & AIOP_FREE) {
11132244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
111439314b7dSJohn Baldwin 			aiop->aioprocflags &= ~AIOP_FREE;
11152244ea07SJohn Dyson 		}
11162244ea07SJohn Dyson 
1117fd3bf775SJohn Dyson 		/*
1118bfbbc4aaSJason Evans 		 * Check for jobs.
1119fd3bf775SJohn Dyson 		 */
11205652770dSJohn Baldwin 		while ((job = aio_selectjob(aiop)) != NULL) {
11211ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11222244ea07SJohn Dyson 
1123f3215338SJohn Baldwin 			ki = job->userproc->p_aioinfo;
1124f3215338SJohn Baldwin 			job->handle_fn(job);
112584af4da6SJohn Dyson 
11269b84335cSDavid Xu 			mtx_lock(&aio_job_mtx);
11279b84335cSDavid Xu 			/* Decrement the active job count. */
11289b84335cSDavid Xu 			ki->kaio_active_count--;
11292244ea07SJohn Dyson 		}
11302244ea07SJohn Dyson 
1131fd3bf775SJohn Dyson 		/*
1132bfbbc4aaSJason Evans 		 * Disconnect from user address space.
1133fd3bf775SJohn Dyson 		 */
11348a4dc40fSJohn Baldwin 		if (p->p_vmspace != myvm) {
11351ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11368a4dc40fSJohn Baldwin 			vmspace_switch_aio(myvm);
11371ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
11381ce91824SDavid Xu 			/*
11391ce91824SDavid Xu 			 * We have to restart to avoid race, we only sleep if
11408a4dc40fSJohn Baldwin 			 * no job can be selected.
11411ce91824SDavid Xu 			 */
11421ce91824SDavid Xu 			continue;
1143fd3bf775SJohn Dyson 		}
1144fd3bf775SJohn Dyson 
11451ce91824SDavid Xu 		mtx_assert(&aio_job_mtx, MA_OWNED);
11461ce91824SDavid Xu 
1147fd3bf775SJohn Dyson 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
114839314b7dSJohn Baldwin 		aiop->aioprocflags |= AIOP_FREE;
1149fd3bf775SJohn Dyson 
1150fd3bf775SJohn Dyson 		/*
1151bfbbc4aaSJason Evans 		 * If daemon is inactive for a long time, allow it to exit,
1152bfbbc4aaSJason Evans 		 * thereby freeing resources.
1153fd3bf775SJohn Dyson 		 */
115439314b7dSJohn Baldwin 		if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
11558a4dc40fSJohn Baldwin 		    aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
115639314b7dSJohn Baldwin 		    (aiop->aioprocflags & AIOP_FREE) &&
11578a4dc40fSJohn Baldwin 		    num_aio_procs > target_aio_procs)
11588a4dc40fSJohn Baldwin 			break;
11598a4dc40fSJohn Baldwin 	}
1160fd3bf775SJohn Dyson 	TAILQ_REMOVE(&aio_freeproc, aiop, list);
116184af4da6SJohn Dyson 	num_aio_procs--;
11621ce91824SDavid Xu 	mtx_unlock(&aio_job_mtx);
11631ce91824SDavid Xu 	uma_zfree(aiop_zone, aiop);
11641ce91824SDavid Xu 	free_unr(aiod_unr, id);
11658a4dc40fSJohn Baldwin 	vmspace_free(myvm);
11668a4dc40fSJohn Baldwin 
11678a4dc40fSJohn Baldwin 	KASSERT(p->p_vmspace == myvm,
11688a4dc40fSJohn Baldwin 	    ("AIOD: bad vmspace for exiting daemon"));
1169f7db0c95SMark Johnston 	KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1170f7db0c95SMark Johnston 	    ("AIOD: bad vm refcnt for exiting daemon: %d",
1171f7db0c95SMark Johnston 	    refcount_load(&myvm->vm_refcnt)));
11723745c395SJulian Elischer 	kproc_exit(0);
1173fd3bf775SJohn Dyson }
11742244ea07SJohn Dyson 
11752244ea07SJohn Dyson /*
1176bfbbc4aaSJason Evans  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1177bfbbc4aaSJason Evans  * AIO daemon modifies its environment itself.
11782244ea07SJohn Dyson  */
11792244ea07SJohn Dyson static int
11801ce91824SDavid Xu aio_newproc(int *start)
1181fd3bf775SJohn Dyson {
11822244ea07SJohn Dyson 	int error;
1183c9a970a7SAlan Cox 	struct proc *p;
11841ce91824SDavid Xu 	int id;
11852244ea07SJohn Dyson 
11861ce91824SDavid Xu 	id = alloc_unr(aiod_unr);
11873745c395SJulian Elischer 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
11881ce91824SDavid Xu 		RFNOWAIT, 0, "aiod%d", id);
11891ce91824SDavid Xu 	if (error == 0) {
1190fd3bf775SJohn Dyson 		/*
11911ce91824SDavid Xu 		 * Wait until daemon is started.
1192fd3bf775SJohn Dyson 		 */
11931ce91824SDavid Xu 		sema_wait(&aio_newproc_sem);
11941ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
119584af4da6SJohn Dyson 		num_aio_procs++;
11961ce91824SDavid Xu 		if (start != NULL)
11977f34b521SDavid Xu 			(*start)--;
11981ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
11991ce91824SDavid Xu 	} else {
12001ce91824SDavid Xu 		free_unr(aiod_unr, id);
12011ce91824SDavid Xu 	}
1202ac41f2efSAlfred Perlstein 	return (error);
12032244ea07SJohn Dyson }
12042244ea07SJohn Dyson 
12052244ea07SJohn Dyson /*
120672bce9ffSAlan Somers  * Try the high-performance, low-overhead bio method for eligible
120788ed460eSAlan Cox  * VCHR devices.  This method doesn't use an aio helper thread, and
120888ed460eSAlan Cox  * thus has very low overhead.
120988ed460eSAlan Cox  *
1210a9bf5e37SDavid Xu  * Assumes that the caller, aio_aqueue(), has incremented the file
121188ed460eSAlan Cox  * structure's reference count, preventing its deallocation for the
121288ed460eSAlan Cox  * duration of this call.
1213fd3bf775SJohn Dyson  */
121488ed460eSAlan Cox static int
121572bce9ffSAlan Somers aio_qbio(struct proc *p, struct kaiocb *job)
1216fd3bf775SJohn Dyson {
1217fd3bf775SJohn Dyson 	struct aiocb *cb;
1218fd3bf775SJohn Dyson 	struct file *fp;
1219f743d981SAlexander Motin 	struct buf *pbuf;
1220fd3bf775SJohn Dyson 	struct vnode *vp;
1221f3215a60SKonstantin Belousov 	struct cdevsw *csw;
1222f3215a60SKonstantin Belousov 	struct cdev *dev;
1223fd3bf775SJohn Dyson 	struct kaioinfo *ki;
1224022ca2fcSAlan Somers 	struct bio **bios = NULL;
1225022ca2fcSAlan Somers 	off_t offset;
1226022ca2fcSAlan Somers 	int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1227f743d981SAlexander Motin 	vm_prot_t prot;
1228022ca2fcSAlan Somers 	bool use_unmapped;
1229fd3bf775SJohn Dyson 
12305652770dSJohn Baldwin 	cb = &job->uaiocb;
12315652770dSJohn Baldwin 	fp = job->fd_file;
1232022ca2fcSAlan Somers 	opcode = cb->aio_lio_opcode;
1233fd3bf775SJohn Dyson 
1234022ca2fcSAlan Somers 	if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1235022ca2fcSAlan Somers 	    opcode == LIO_READ || opcode == LIO_READV))
1236f54c5606SJohn Baldwin 		return (-1);
12376160e12cSGleb Smirnoff 	if (fp == NULL || fp->f_type != DTYPE_VNODE)
1238008626c3SPoul-Henning Kamp 		return (-1);
1239fd3bf775SJohn Dyson 
12403b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
1241f743d981SAlexander Motin 	if (vp->v_type != VCHR)
1242f582ac06SBrian Feldman 		return (-1);
1243ad8de0f2SDavid Xu 	if (vp->v_bufobj.bo_bsize == 0)
1244ad8de0f2SDavid Xu 		return (-1);
1245022ca2fcSAlan Somers 
12462247f489SAlan Somers 	bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1247022ca2fcSAlan Somers 	iovcnt = job->uiop->uio_iovcnt;
1248022ca2fcSAlan Somers 	if (iovcnt > max_buf_aio)
1249008626c3SPoul-Henning Kamp 		return (-1);
1250022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1251022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1252022ca2fcSAlan Somers 			return (-1);
1253022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len > maxphys) {
1254022ca2fcSAlan Somers 			error = -1;
1255022ca2fcSAlan Somers 			return (-1);
1256022ca2fcSAlan Somers 		}
1257022ca2fcSAlan Somers 	}
1258022ca2fcSAlan Somers 	offset = cb->aio_offset;
1259fd3bf775SJohn Dyson 
1260f3215a60SKonstantin Belousov 	ref = 0;
1261f3215a60SKonstantin Belousov 	csw = devvn_refthread(vp, &dev, &ref);
1262f3215a60SKonstantin Belousov 	if (csw == NULL)
1263f3215a60SKonstantin Belousov 		return (ENXIO);
1264f743d981SAlexander Motin 
1265f743d981SAlexander Motin 	if ((csw->d_flags & D_DISK) == 0) {
1266f743d981SAlexander Motin 		error = -1;
1267f743d981SAlexander Motin 		goto unref;
1268f743d981SAlexander Motin 	}
1269022ca2fcSAlan Somers 	if (job->uiop->uio_resid > dev->si_iosize_max) {
1270f3215a60SKonstantin Belousov 		error = -1;
1271f3215a60SKonstantin Belousov 		goto unref;
1272f3215a60SKonstantin Belousov 	}
1273f3215a60SKonstantin Belousov 
1274f743d981SAlexander Motin 	ki = p->p_aioinfo;
1275022ca2fcSAlan Somers 	job->error = 0;
12764d805eacSJohn Baldwin 
1277022ca2fcSAlan Somers 	use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1278022ca2fcSAlan Somers 	if (!use_unmapped) {
1279022ca2fcSAlan Somers 		AIO_LOCK(ki);
1280022ca2fcSAlan Somers 		if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1281022ca2fcSAlan Somers 			AIO_UNLOCK(ki);
1282f54c5606SJohn Baldwin 			error = EAGAIN;
1283f743d981SAlexander Motin 			goto unref;
1284f743d981SAlexander Motin 		}
1285022ca2fcSAlan Somers 		ki->kaio_buffer_count += iovcnt;
1286022ca2fcSAlan Somers 		AIO_UNLOCK(ki);
1287022ca2fcSAlan Somers 	}
12884d805eacSJohn Baldwin 
1289022ca2fcSAlan Somers 	bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1290022ca2fcSAlan Somers 	atomic_store_int(&job->nbio, iovcnt);
1291022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1292022ca2fcSAlan Somers 		struct vm_page** pages;
1293022ca2fcSAlan Somers 		struct bio *bp;
1294022ca2fcSAlan Somers 		void *buf;
1295022ca2fcSAlan Somers 		size_t nbytes;
1296022ca2fcSAlan Somers 		int npages;
1297022ca2fcSAlan Somers 
1298022ca2fcSAlan Somers 		buf = job->uiop->uio_iov[i].iov_base;
1299022ca2fcSAlan Somers 		nbytes = job->uiop->uio_iov[i].iov_len;
1300022ca2fcSAlan Somers 
1301022ca2fcSAlan Somers 		bios[i] = g_alloc_bio();
1302022ca2fcSAlan Somers 		bp = bios[i];
1303022ca2fcSAlan Somers 
1304022ca2fcSAlan Somers 		poff = (vm_offset_t)buf & PAGE_MASK;
1305022ca2fcSAlan Somers 		if (use_unmapped) {
1306022ca2fcSAlan Somers 			pbuf = NULL;
1307022ca2fcSAlan Somers 			pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1308022ca2fcSAlan Somers 			    nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1309022ca2fcSAlan Somers 		} else {
131001206038SAlan Somers 			pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1311f743d981SAlexander Motin 			BUF_KERNPROC(pbuf);
131201206038SAlan Somers 			pages = pbuf->b_pages;
13134d805eacSJohn Baldwin 		}
13141ce91824SDavid Xu 
1315022ca2fcSAlan Somers 		bp->bio_length = nbytes;
1316022ca2fcSAlan Somers 		bp->bio_bcount = nbytes;
131772bce9ffSAlan Somers 		bp->bio_done = aio_biowakeup;
1318022ca2fcSAlan Somers 		bp->bio_offset = offset;
1319022ca2fcSAlan Somers 		bp->bio_cmd = bio_cmd;
1320f743d981SAlexander Motin 		bp->bio_dev = dev;
132101206038SAlan Somers 		bp->bio_caller1 = job;
132201206038SAlan Somers 		bp->bio_caller2 = pbuf;
1323f743d981SAlexander Motin 
1324f743d981SAlexander Motin 		prot = VM_PROT_READ;
1325022ca2fcSAlan Somers 		if (opcode == LIO_READ || opcode == LIO_READV)
1326f743d981SAlexander Motin 			prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
132701206038SAlan Somers 		npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1328022ca2fcSAlan Somers 		    (vm_offset_t)buf, bp->bio_length, prot, pages,
1329cd853791SKonstantin Belousov 		    atop(maxphys) + 1);
133001206038SAlan Somers 		if (npages < 0) {
1331022ca2fcSAlan Somers 			if (pbuf != NULL)
1332022ca2fcSAlan Somers 				uma_zfree(pbuf_zone, pbuf);
1333022ca2fcSAlan Somers 			else
1334022ca2fcSAlan Somers 				free(pages, M_TEMP);
1335f743d981SAlexander Motin 			error = EFAULT;
1336022ca2fcSAlan Somers 			g_destroy_bio(bp);
1337022ca2fcSAlan Somers 			i--;
1338022ca2fcSAlan Somers 			goto destroy_bios;
1339f743d981SAlexander Motin 		}
13404d805eacSJohn Baldwin 		if (pbuf != NULL) {
134101206038SAlan Somers 			pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1342f743d981SAlexander Motin 			bp->bio_data = pbuf->b_data + poff;
134301206038SAlan Somers 			pbuf->b_npages = npages;
1344022ca2fcSAlan Somers 			atomic_add_int(&num_buf_aio, 1);
1345f743d981SAlexander Motin 		} else {
134601206038SAlan Somers 			bp->bio_ma = pages;
134701206038SAlan Somers 			bp->bio_ma_n = npages;
1348f743d981SAlexander Motin 			bp->bio_ma_offset = poff;
1349f743d981SAlexander Motin 			bp->bio_data = unmapped_buf;
1350f743d981SAlexander Motin 			bp->bio_flags |= BIO_UNMAPPED;
13518091e52bSJohn Baldwin 			atomic_add_int(&num_unmapped_aio, 1);
1352f743d981SAlexander Motin 		}
1353f743d981SAlexander Motin 
1354022ca2fcSAlan Somers 		offset += nbytes;
1355022ca2fcSAlan Somers 	}
1356022ca2fcSAlan Somers 
1357bfbbc4aaSJason Evans 	/* Perform transfer. */
1358022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++)
1359022ca2fcSAlan Somers 		csw->d_strategy(bios[i]);
1360022ca2fcSAlan Somers 	free(bios, M_TEMP);
1361022ca2fcSAlan Somers 
1362f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1363ac41f2efSAlfred Perlstein 	return (0);
1364fd3bf775SJohn Dyson 
1365022ca2fcSAlan Somers destroy_bios:
1366022ca2fcSAlan Somers 	for (; i >= 0; i--)
1367022ca2fcSAlan Somers 		aio_biocleanup(bios[i]);
1368022ca2fcSAlan Somers 	free(bios, M_TEMP);
1369f3215a60SKonstantin Belousov unref:
1370f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1371fd3bf775SJohn Dyson 	return (error);
1372fd3bf775SJohn Dyson }
1373fd3bf775SJohn Dyson 
1374399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
13753858a1f4SJohn Baldwin static int
13763858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
13773858a1f4SJohn Baldwin {
13783858a1f4SJohn Baldwin 
13793858a1f4SJohn Baldwin 	/*
13803858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
13813858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
13823858a1f4SJohn Baldwin 	 */
13833858a1f4SJohn Baldwin 	nsig->sigev_notify = osig->sigev_notify;
13843858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
13853858a1f4SJohn Baldwin 	case SIGEV_NONE:
13863858a1f4SJohn Baldwin 		break;
13873858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
13883858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
13893858a1f4SJohn Baldwin 		break;
13903858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
13913858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
13923858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
13933858a1f4SJohn Baldwin 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
13943858a1f4SJohn Baldwin 		break;
13953858a1f4SJohn Baldwin 	default:
13963858a1f4SJohn Baldwin 		return (EINVAL);
13973858a1f4SJohn Baldwin 	}
13983858a1f4SJohn Baldwin 	return (0);
13993858a1f4SJohn Baldwin }
14003858a1f4SJohn Baldwin 
14013858a1f4SJohn Baldwin static int
1402022ca2fcSAlan Somers aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1403022ca2fcSAlan Somers     int type __unused)
14043858a1f4SJohn Baldwin {
14053858a1f4SJohn Baldwin 	struct oaiocb *ojob;
1406022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
14073858a1f4SJohn Baldwin 	int error;
14083858a1f4SJohn Baldwin 
1409022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
1410022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct oaiocb));
14113858a1f4SJohn Baldwin 	if (error)
14123858a1f4SJohn Baldwin 		return (error);
1413022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1414022ca2fcSAlan Somers 	ojob = (struct oaiocb *)kcb;
1415022ca2fcSAlan Somers 	return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
14163858a1f4SJohn Baldwin }
1417399e8c17SJohn Baldwin #endif
14183858a1f4SJohn Baldwin 
14193858a1f4SJohn Baldwin static int
1420022ca2fcSAlan Somers aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
14213858a1f4SJohn Baldwin {
1422022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
1423022ca2fcSAlan Somers 	int error;
14243858a1f4SJohn Baldwin 
1425022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct aiocb));
1426022ca2fcSAlan Somers 	if (error)
1427022ca2fcSAlan Somers 		return (error);
1428f30a1ae8SThomas Munro 	if (type == LIO_NOP)
1429f30a1ae8SThomas Munro 		type = kcb->aio_lio_opcode;
14302247f489SAlan Somers 	if (type & LIO_VECTORED) {
1431022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
1432022ca2fcSAlan Somers 		error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1433022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
1434022ca2fcSAlan Somers 	}
1435022ca2fcSAlan Somers 
1436022ca2fcSAlan Somers 	return (error);
14373858a1f4SJohn Baldwin }
14383858a1f4SJohn Baldwin 
14393858a1f4SJohn Baldwin static long
14403858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob)
14413858a1f4SJohn Baldwin {
14423858a1f4SJohn Baldwin 
14433858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.status));
14443858a1f4SJohn Baldwin }
14453858a1f4SJohn Baldwin 
14463858a1f4SJohn Baldwin static long
14473858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob)
14483858a1f4SJohn Baldwin {
14493858a1f4SJohn Baldwin 
14503858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.error));
14513858a1f4SJohn Baldwin }
14523858a1f4SJohn Baldwin 
14533858a1f4SJohn Baldwin static int
14543858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status)
14553858a1f4SJohn Baldwin {
14563858a1f4SJohn Baldwin 
14573858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.status, status));
14583858a1f4SJohn Baldwin }
14593858a1f4SJohn Baldwin 
14603858a1f4SJohn Baldwin static int
14613858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error)
14623858a1f4SJohn Baldwin {
14633858a1f4SJohn Baldwin 
14643858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.error, error));
14653858a1f4SJohn Baldwin }
14663858a1f4SJohn Baldwin 
14673858a1f4SJohn Baldwin static int
14683858a1f4SJohn Baldwin aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
14693858a1f4SJohn Baldwin {
14703858a1f4SJohn Baldwin 
14713858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
14723858a1f4SJohn Baldwin }
14733858a1f4SJohn Baldwin 
14743858a1f4SJohn Baldwin static int
14753858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
14763858a1f4SJohn Baldwin {
14773858a1f4SJohn Baldwin 
14783858a1f4SJohn Baldwin 	return (suword(ujobp, (long)ujob));
14793858a1f4SJohn Baldwin }
14803858a1f4SJohn Baldwin 
14813858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = {
1482849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin,
14833858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14843858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14853858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14863858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14873858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14883858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14893858a1f4SJohn Baldwin };
14903858a1f4SJohn Baldwin 
1491399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
14923858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = {
1493849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin_old_sigevent,
14943858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14953858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14963858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14973858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14983858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14993858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
15003858a1f4SJohn Baldwin };
1501399e8c17SJohn Baldwin #endif
15023858a1f4SJohn Baldwin 
1503bfbbc4aaSJason Evans /*
150472bce9ffSAlan Somers  * Queue a new AIO request.  Choosing either the threaded or direct bio VCHR
1505bfbbc4aaSJason Evans  * technique is done in this code.
15062244ea07SJohn Dyson  */
15076a1162d4SAlexander Leidinger int
15085652770dSJohn Baldwin aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
15093858a1f4SJohn Baldwin     int type, struct aiocb_ops *ops)
1510fd3bf775SJohn Dyson {
1511b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1512022ca2fcSAlan Somers 	struct file *fp = NULL;
1513f3215338SJohn Baldwin 	struct kaiocb *job;
15142244ea07SJohn Dyson 	struct kaioinfo *ki;
1515c6fa9f78SAlan Cox 	struct kevent kev;
15161ce91824SDavid Xu 	int opcode;
15171ce91824SDavid Xu 	int error;
15184db71d27SJohn-Mark Gurney 	int fd, kqfd;
15191ce91824SDavid Xu 	int jid;
1520fde80935SDavid Xu 	u_short evflags;
15212244ea07SJohn Dyson 
1522a9bf5e37SDavid Xu 	if (p->p_aioinfo == NULL)
1523a9bf5e37SDavid Xu 		aio_init_aioinfo(p);
1524a9bf5e37SDavid Xu 
15251ce91824SDavid Xu 	ki = p->p_aioinfo;
15261ce91824SDavid Xu 
15275652770dSJohn Baldwin 	ops->store_status(ujob, -1);
15285652770dSJohn Baldwin 	ops->store_error(ujob, 0);
15295652770dSJohn Baldwin 	ops->store_kernelinfo(ujob, -1);
1530a9bf5e37SDavid Xu 
1531a9bf5e37SDavid Xu 	if (num_queue_count >= max_queue_count ||
153286bbef43SJohn Baldwin 	    ki->kaio_count >= max_aio_queue_per_proc) {
1533022ca2fcSAlan Somers 		error = EAGAIN;
1534022ca2fcSAlan Somers 		goto err1;
1535a9bf5e37SDavid Xu 	}
1536a9bf5e37SDavid Xu 
15375652770dSJohn Baldwin 	job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
15385652770dSJohn Baldwin 	knlist_init_mtx(&job->klist, AIO_MTX(ki));
1539fd3bf775SJohn Dyson 
1540022ca2fcSAlan Somers 	error = ops->aio_copyin(ujob, job, type);
1541022ca2fcSAlan Somers 	if (error)
1542022ca2fcSAlan Somers 		goto err2;
154368d71118SDavid Xu 
1544bb430bc7SJohn Baldwin 	if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1545022ca2fcSAlan Somers 		error = EINVAL;
1546022ca2fcSAlan Somers 		goto err2;
1547434ea137SGleb Smirnoff 	}
1548434ea137SGleb Smirnoff 
15495652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
15505652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
15515652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
15525652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1553022ca2fcSAlan Somers 		error = EINVAL;
1554022ca2fcSAlan Somers 		goto err2;
155568d71118SDavid Xu 	}
155668d71118SDavid Xu 
15575652770dSJohn Baldwin 	if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
15585652770dSJohn Baldwin 	     job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
15595652770dSJohn Baldwin 		!_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1560022ca2fcSAlan Somers 		error = EINVAL;
1561022ca2fcSAlan Somers 		goto err2;
15622f3cf918SAlfred Perlstein 	}
15632244ea07SJohn Dyson 
1564ff1a3078SAlan Somers 	/* Get the opcode. */
1565ff1a3078SAlan Somers 	if (type == LIO_NOP) {
1566ff1a3078SAlan Somers 		switch (job->uaiocb.aio_lio_opcode) {
1567ff1a3078SAlan Somers 		case LIO_WRITE:
1568f30a1ae8SThomas Munro 		case LIO_WRITEV:
1569ff1a3078SAlan Somers 		case LIO_NOP:
1570ff1a3078SAlan Somers 		case LIO_READ:
1571f30a1ae8SThomas Munro 		case LIO_READV:
1572ff1a3078SAlan Somers 			opcode = job->uaiocb.aio_lio_opcode;
1573ff1a3078SAlan Somers 			break;
1574ff1a3078SAlan Somers 		default:
1575ff1a3078SAlan Somers 			error = EINVAL;
1576ff1a3078SAlan Somers 			goto err2;
1577ff1a3078SAlan Somers 		}
1578ff1a3078SAlan Somers 	} else
1579ff1a3078SAlan Somers 		opcode = job->uaiocb.aio_lio_opcode = type;
1580ff1a3078SAlan Somers 
15815652770dSJohn Baldwin 	ksiginfo_init(&job->ksi);
15824c0fb2cfSDavid Xu 
1583bfbbc4aaSJason Evans 	/* Save userspace address of the job info. */
15845652770dSJohn Baldwin 	job->ujob = ujob;
158511783b14SJohn Dyson 
1586a9d2f8d8SRobert Watson 	/*
1587a9d2f8d8SRobert Watson 	 * Validate the opcode and fetch the file object for the specified
1588a9d2f8d8SRobert Watson 	 * file descriptor.
1589a9d2f8d8SRobert Watson 	 *
1590a9d2f8d8SRobert Watson 	 * XXXRW: Moved the opcode validation up here so that we don't
1591a9d2f8d8SRobert Watson 	 * retrieve a file descriptor without knowing what the capabiltity
1592a9d2f8d8SRobert Watson 	 * should be.
1593a9d2f8d8SRobert Watson 	 */
15945652770dSJohn Baldwin 	fd = job->uaiocb.aio_fildes;
15952a522eb9SJohn Baldwin 	switch (opcode) {
15962a522eb9SJohn Baldwin 	case LIO_WRITE:
1597022ca2fcSAlan Somers 	case LIO_WRITEV:
1598cbd92ce6SMatt Macy 		error = fget_write(td, fd, &cap_pwrite_rights, &fp);
15992a522eb9SJohn Baldwin 		break;
16002a522eb9SJohn Baldwin 	case LIO_READ:
1601022ca2fcSAlan Somers 	case LIO_READV:
1602cbd92ce6SMatt Macy 		error = fget_read(td, fd, &cap_pread_rights, &fp);
1603a9d2f8d8SRobert Watson 		break;
1604a9d2f8d8SRobert Watson 	case LIO_SYNC:
1605801ac943SThomas Munro 	case LIO_DSYNC:
1606cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_fsync_rights, &fp);
1607a9d2f8d8SRobert Watson 		break;
16086160e12cSGleb Smirnoff 	case LIO_MLOCK:
16096160e12cSGleb Smirnoff 		break;
1610a9d2f8d8SRobert Watson 	case LIO_NOP:
1611cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_no_rights, &fp);
16122a522eb9SJohn Baldwin 		break;
16132a522eb9SJohn Baldwin 	default:
1614a9d2f8d8SRobert Watson 		error = EINVAL;
16152a522eb9SJohn Baldwin 	}
1616022ca2fcSAlan Somers 	if (error)
1617022ca2fcSAlan Somers 		goto err3;
161899eee864SDavid Xu 
16192247f489SAlan Somers 	if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
162099eee864SDavid Xu 		error = EINVAL;
1621022ca2fcSAlan Somers 		goto err3;
162299eee864SDavid Xu 	}
16232244ea07SJohn Dyson 
1624022ca2fcSAlan Somers 	if ((opcode == LIO_READ || opcode == LIO_READV ||
1625022ca2fcSAlan Somers 	    opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1626711dba24SKonstantin Belousov 	    job->uaiocb.aio_offset < 0 &&
1627711dba24SKonstantin Belousov 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1628ae124fc4SAlan Cox 		error = EINVAL;
1629022ca2fcSAlan Somers 		goto err3;
16302244ea07SJohn Dyson 	}
16311ce91824SDavid Xu 
16328d9ed174SKonstantin Belousov 	if (fp != NULL && fp->f_ops == &path_fileops) {
16338d9ed174SKonstantin Belousov 		error = EBADF;
16348d9ed174SKonstantin Belousov 		goto err3;
16358d9ed174SKonstantin Belousov 	}
16368d9ed174SKonstantin Belousov 
16375652770dSJohn Baldwin 	job->fd_file = fp;
16381ce91824SDavid Xu 
163999eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
164099eee864SDavid Xu 	jid = jobrefid++;
16415652770dSJohn Baldwin 	job->seqno = jobseqno++;
164299eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
16435652770dSJohn Baldwin 	error = ops->store_kernelinfo(ujob, jid);
16441ce91824SDavid Xu 	if (error) {
16451ce91824SDavid Xu 		error = EINVAL;
1646022ca2fcSAlan Somers 		goto err3;
16471ce91824SDavid Xu 	}
16485652770dSJohn Baldwin 	job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
16492244ea07SJohn Dyson 
16502244ea07SJohn Dyson 	if (opcode == LIO_NOP) {
1651a5c0b1c0SAlan Cox 		fdrop(fp, td);
1652022ca2fcSAlan Somers 		MPASS(job->uiop == &job->uio || job->uiop == NULL);
16535652770dSJohn Baldwin 		uma_zfree(aiocb_zone, job);
1654ac41f2efSAlfred Perlstein 		return (0);
16552244ea07SJohn Dyson 	}
16562244ea07SJohn Dyson 
16575652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1658cb679c38SJonathan Lemon 		goto no_kqueue;
16595652770dSJohn Baldwin 	evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1660fde80935SDavid Xu 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1661fde80935SDavid Xu 		error = EINVAL;
1662022ca2fcSAlan Somers 		goto err3;
1663fde80935SDavid Xu 	}
16645652770dSJohn Baldwin 	kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
166536c4960eSMark Johnston 	memset(&kev, 0, sizeof(kev));
16665652770dSJohn Baldwin 	kev.ident = (uintptr_t)job->ujob;
1667cb679c38SJonathan Lemon 	kev.filter = EVFILT_AIO;
1668fde80935SDavid Xu 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
16695652770dSJohn Baldwin 	kev.data = (intptr_t)job;
16705652770dSJohn Baldwin 	kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1671792843c3SMark Johnston 	error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1672f3215338SJohn Baldwin 	if (error)
1673022ca2fcSAlan Somers 		goto err3;
1674f3215338SJohn Baldwin 
1675cb679c38SJonathan Lemon no_kqueue:
1676cb679c38SJonathan Lemon 
16775652770dSJohn Baldwin 	ops->store_error(ujob, EINPROGRESS);
16785652770dSJohn Baldwin 	job->uaiocb._aiocb_private.error = EINPROGRESS;
16795652770dSJohn Baldwin 	job->userproc = p;
16805652770dSJohn Baldwin 	job->cred = crhold(td->td_ucred);
1681f3215338SJohn Baldwin 	job->jobflags = KAIOCB_QUEUEING;
16825652770dSJohn Baldwin 	job->lio = lj;
16832244ea07SJohn Dyson 
16842247f489SAlan Somers 	if (opcode & LIO_VECTORED) {
1685022ca2fcSAlan Somers 		/* Use the uio copied in by aio_copyin */
1686022ca2fcSAlan Somers 		MPASS(job->uiop != &job->uio && job->uiop != NULL);
16872247f489SAlan Somers 	} else {
1688022ca2fcSAlan Somers 		/* Setup the inline uio */
1689022ca2fcSAlan Somers 		job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1690022ca2fcSAlan Somers 		job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1691022ca2fcSAlan Somers 		job->uio.uio_iov = job->iov;
1692022ca2fcSAlan Somers 		job->uio.uio_iovcnt = 1;
1693022ca2fcSAlan Somers 		job->uio.uio_resid = job->uaiocb.aio_nbytes;
1694022ca2fcSAlan Somers 		job->uio.uio_segflg = UIO_USERSPACE;
1695022ca2fcSAlan Somers 		job->uiop = &job->uio;
1696022ca2fcSAlan Somers 	}
16972247f489SAlan Somers 	switch (opcode & (LIO_READ | LIO_WRITE)) {
1698022ca2fcSAlan Somers 	case LIO_READ:
1699022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_READ;
1700022ca2fcSAlan Somers 		break;
1701022ca2fcSAlan Somers 	case LIO_WRITE:
1702022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_WRITE;
1703022ca2fcSAlan Somers 		break;
1704022ca2fcSAlan Somers 	}
1705022ca2fcSAlan Somers 	job->uiop->uio_offset = job->uaiocb.aio_offset;
1706022ca2fcSAlan Somers 	job->uiop->uio_td = td;
1707022ca2fcSAlan Somers 
1708f3215338SJohn Baldwin 	if (opcode == LIO_MLOCK) {
1709f3215338SJohn Baldwin 		aio_schedule(job, aio_process_mlock);
1710f3215338SJohn Baldwin 		error = 0;
1711f3215338SJohn Baldwin 	} else if (fp->f_ops->fo_aio_queue == NULL)
1712f3215338SJohn Baldwin 		error = aio_queue_file(fp, job);
1713f3215338SJohn Baldwin 	else
1714f3215338SJohn Baldwin 		error = fo_aio_queue(fp, job);
1715f3215338SJohn Baldwin 	if (error)
1716*45c2c7c4SKonstantin Belousov 		goto err4;
1717f3215338SJohn Baldwin 
1718f3215338SJohn Baldwin 	AIO_LOCK(ki);
1719f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_QUEUEING;
1720f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1721f3215338SJohn Baldwin 	ki->kaio_count++;
1722f3215338SJohn Baldwin 	if (lj)
1723f3215338SJohn Baldwin 		lj->lioj_count++;
1724f3215338SJohn Baldwin 	atomic_add_int(&num_queue_count, 1);
1725f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
1726f3215338SJohn Baldwin 		/*
1727f3215338SJohn Baldwin 		 * The queue callback completed the request synchronously.
1728f3215338SJohn Baldwin 		 * The bulk of the completion is deferred in that case
1729f3215338SJohn Baldwin 		 * until this point.
1730f3215338SJohn Baldwin 		 */
1731f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
1732f3215338SJohn Baldwin 	} else
1733f3215338SJohn Baldwin 		TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1734f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1735f3215338SJohn Baldwin 	return (0);
1736f3215338SJohn Baldwin 
1737*45c2c7c4SKonstantin Belousov err4:
1738*45c2c7c4SKonstantin Belousov 	crfree(job->cred);
1739022ca2fcSAlan Somers err3:
1740f3215338SJohn Baldwin 	if (fp)
1741f3215338SJohn Baldwin 		fdrop(fp, td);
1742022ca2fcSAlan Somers 	knlist_delete(&job->klist, curthread, 0);
1743022ca2fcSAlan Somers err2:
1744022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
1745022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
1746f3215338SJohn Baldwin 	uma_zfree(aiocb_zone, job);
1747022ca2fcSAlan Somers err1:
1748f3215338SJohn Baldwin 	ops->store_error(ujob, error);
1749f3215338SJohn Baldwin 	return (error);
1750f3215338SJohn Baldwin }
1751f3215338SJohn Baldwin 
1752f3215338SJohn Baldwin static void
1753f3215338SJohn Baldwin aio_cancel_daemon_job(struct kaiocb *job)
1754f3215338SJohn Baldwin {
1755f3215338SJohn Baldwin 
1756f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1757f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1758f3215338SJohn Baldwin 		TAILQ_REMOVE(&aio_jobs, job, list);
1759f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1760f3215338SJohn Baldwin 	aio_cancel(job);
1761f3215338SJohn Baldwin }
1762f3215338SJohn Baldwin 
1763f3215338SJohn Baldwin void
1764f3215338SJohn Baldwin aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1765f3215338SJohn Baldwin {
1766f3215338SJohn Baldwin 
1767f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1768f3215338SJohn Baldwin 	if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1769f3215338SJohn Baldwin 		mtx_unlock(&aio_job_mtx);
1770f3215338SJohn Baldwin 		aio_cancel(job);
1771f3215338SJohn Baldwin 		return;
1772f3215338SJohn Baldwin 	}
1773f3215338SJohn Baldwin 	job->handle_fn = func;
1774f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1775f3215338SJohn Baldwin 	aio_kick_nowait(job->userproc);
1776f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1777f3215338SJohn Baldwin }
1778f3215338SJohn Baldwin 
1779f3215338SJohn Baldwin static void
1780f3215338SJohn Baldwin aio_cancel_sync(struct kaiocb *job)
1781f3215338SJohn Baldwin {
1782f3215338SJohn Baldwin 	struct kaioinfo *ki;
1783f3215338SJohn Baldwin 
1784f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1785005ce8e4SJohn Baldwin 	AIO_LOCK(ki);
1786f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1787f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1788005ce8e4SJohn Baldwin 	AIO_UNLOCK(ki);
1789f3215338SJohn Baldwin 	aio_cancel(job);
1790f3215338SJohn Baldwin }
1791f3215338SJohn Baldwin 
1792f3215338SJohn Baldwin int
1793f3215338SJohn Baldwin aio_queue_file(struct file *fp, struct kaiocb *job)
1794f3215338SJohn Baldwin {
1795f3215338SJohn Baldwin 	struct kaioinfo *ki;
1796f3215338SJohn Baldwin 	struct kaiocb *job2;
17979fe297bbSKonstantin Belousov 	struct vnode *vp;
17989fe297bbSKonstantin Belousov 	struct mount *mp;
1799f54c5606SJohn Baldwin 	int error;
18009fe297bbSKonstantin Belousov 	bool safe;
1801f3215338SJohn Baldwin 
1802f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
180372bce9ffSAlan Somers 	error = aio_qbio(job->userproc, job);
1804f54c5606SJohn Baldwin 	if (error >= 0)
1805f54c5606SJohn Baldwin 		return (error);
18069fe297bbSKonstantin Belousov 	safe = false;
18079fe297bbSKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE) {
18089fe297bbSKonstantin Belousov 		vp = fp->f_vnode;
18099fe297bbSKonstantin Belousov 		if (vp->v_type == VREG || vp->v_type == VDIR) {
18109fe297bbSKonstantin Belousov 			mp = fp->f_vnode->v_mount;
18119fe297bbSKonstantin Belousov 			if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
18129fe297bbSKonstantin Belousov 				safe = true;
18139fe297bbSKonstantin Belousov 		}
18149fe297bbSKonstantin Belousov 	}
18159c20dc99SJohn Baldwin 	if (!(safe || enable_aio_unsafe)) {
18169c20dc99SJohn Baldwin 		counted_warning(&unsafe_warningcnt,
18179c20dc99SJohn Baldwin 		    "is attempting to use unsafe AIO requests");
1818f3215338SJohn Baldwin 		return (EOPNOTSUPP);
18199c20dc99SJohn Baldwin 	}
182084af4da6SJohn Dyson 
18212247f489SAlan Somers 	if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
18227e409184SJohn Baldwin 		aio_schedule(job, aio_process_rw);
18237e409184SJohn Baldwin 		error = 0;
18242247f489SAlan Somers 	} else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1825f3215338SJohn Baldwin 		AIO_LOCK(ki);
18265652770dSJohn Baldwin 		TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
18275652770dSJohn Baldwin 			if (job2->fd_file == job->fd_file &&
18282247f489SAlan Somers 			    ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
18295652770dSJohn Baldwin 			    job2->seqno < job->seqno) {
18305652770dSJohn Baldwin 				job2->jobflags |= KAIOCB_CHECKSYNC;
18315652770dSJohn Baldwin 				job->pending++;
1832dbbccfe9SDavid Xu 			}
1833dbbccfe9SDavid Xu 		}
18345652770dSJohn Baldwin 		if (job->pending != 0) {
1835005ce8e4SJohn Baldwin 			if (!aio_set_cancel_function_locked(job,
1836005ce8e4SJohn Baldwin 				aio_cancel_sync)) {
1837f3215338SJohn Baldwin 				AIO_UNLOCK(ki);
1838f3215338SJohn Baldwin 				aio_cancel(job);
1839f3215338SJohn Baldwin 				return (0);
1840f3215338SJohn Baldwin 			}
18415652770dSJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1842759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1843f3215338SJohn Baldwin 			return (0);
1844dbbccfe9SDavid Xu 		}
1845759ccccaSDavid Xu 		AIO_UNLOCK(ki);
1846f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
1847f3215338SJohn Baldwin 		error = 0;
18482247f489SAlan Somers 	} else {
1849f3215338SJohn Baldwin 		error = EINVAL;
1850f3215338SJohn Baldwin 	}
185199eee864SDavid Xu 	return (error);
185299eee864SDavid Xu }
185399eee864SDavid Xu 
185499eee864SDavid Xu static void
185599eee864SDavid Xu aio_kick_nowait(struct proc *userp)
185699eee864SDavid Xu {
185799eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
185839314b7dSJohn Baldwin 	struct aioproc *aiop;
185999eee864SDavid Xu 
186099eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
186199eee864SDavid Xu 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
186299eee864SDavid Xu 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
186339314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
186439314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18650dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
186686bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1867c85650caSJohn Baldwin 		taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
186899eee864SDavid Xu 	}
186999eee864SDavid Xu }
187099eee864SDavid Xu 
1871dbbccfe9SDavid Xu static int
187299eee864SDavid Xu aio_kick(struct proc *userp)
187399eee864SDavid Xu {
187499eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
187539314b7dSJohn Baldwin 	struct aioproc *aiop;
1876dbbccfe9SDavid Xu 	int error, ret = 0;
187799eee864SDavid Xu 
187899eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
187999eee864SDavid Xu retryproc:
1880d254af07SMatthew Dillon 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
18812244ea07SJohn Dyson 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
188239314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
188339314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18840dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
188586bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1886fd3bf775SJohn Dyson 		num_aio_resv_start++;
18871ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
18881ce91824SDavid Xu 		error = aio_newproc(&num_aio_resv_start);
18891ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
18901ce91824SDavid Xu 		if (error) {
189184af4da6SJohn Dyson 			num_aio_resv_start--;
18922244ea07SJohn Dyson 			goto retryproc;
1893fd3bf775SJohn Dyson 		}
1894dbbccfe9SDavid Xu 	} else {
1895dbbccfe9SDavid Xu 		ret = -1;
18961ce91824SDavid Xu 	}
1897dbbccfe9SDavid Xu 	return (ret);
189899eee864SDavid Xu }
18991ce91824SDavid Xu 
190099eee864SDavid Xu static void
190199eee864SDavid Xu aio_kick_helper(void *context, int pending)
190299eee864SDavid Xu {
190399eee864SDavid Xu 	struct proc *userp = context;
190499eee864SDavid Xu 
190599eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
1906dbbccfe9SDavid Xu 	while (--pending >= 0) {
1907dbbccfe9SDavid Xu 		if (aio_kick(userp))
1908dbbccfe9SDavid Xu 			break;
1909dbbccfe9SDavid Xu 	}
191099eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
19112244ea07SJohn Dyson }
19122244ea07SJohn Dyson 
1913fd3bf775SJohn Dyson /*
1914bfbbc4aaSJason Evans  * Support the aio_return system call, as a side-effect, kernel resources are
1915bfbbc4aaSJason Evans  * released.
19162244ea07SJohn Dyson  */
19173858a1f4SJohn Baldwin static int
19185652770dSJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1919fd3bf775SJohn Dyson {
1920b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19215652770dSJohn Baldwin 	struct kaiocb *job;
19222244ea07SJohn Dyson 	struct kaioinfo *ki;
1923bb430bc7SJohn Baldwin 	long status, error;
19242244ea07SJohn Dyson 
1925c0bf5caaSAlan Cox 	ki = p->p_aioinfo;
1926c0bf5caaSAlan Cox 	if (ki == NULL)
1927ac41f2efSAlfred Perlstein 		return (EINVAL);
1928759ccccaSDavid Xu 	AIO_LOCK(ki);
19295652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_done, plist) {
19305652770dSJohn Baldwin 		if (job->ujob == ujob)
1931c0bf5caaSAlan Cox 			break;
1932c0bf5caaSAlan Cox 	}
19335652770dSJohn Baldwin 	if (job != NULL) {
1934f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
19355652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
19365652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
19371ce91824SDavid Xu 		td->td_retval[0] = status;
1938b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
1939b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
1940b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
1941b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
19425652770dSJohn Baldwin 		aio_free_entry(job);
1943759ccccaSDavid Xu 		AIO_UNLOCK(ki);
19445652770dSJohn Baldwin 		ops->store_error(ujob, error);
19455652770dSJohn Baldwin 		ops->store_status(ujob, status);
194655a122bfSDavid Xu 	} else {
19471ce91824SDavid Xu 		error = EINVAL;
1948759ccccaSDavid Xu 		AIO_UNLOCK(ki);
194955a122bfSDavid Xu 	}
19501ce91824SDavid Xu 	return (error);
19512244ea07SJohn Dyson }
19522244ea07SJohn Dyson 
19533858a1f4SJohn Baldwin int
19548451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap)
19553858a1f4SJohn Baldwin {
19563858a1f4SJohn Baldwin 
19573858a1f4SJohn Baldwin 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
19583858a1f4SJohn Baldwin }
19593858a1f4SJohn Baldwin 
19602244ea07SJohn Dyson /*
1961bfbbc4aaSJason Evans  * Allow a process to wakeup when any of the I/O requests are completed.
19622244ea07SJohn Dyson  */
19633858a1f4SJohn Baldwin static int
19643858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
19653858a1f4SJohn Baldwin     struct timespec *ts)
1966fd3bf775SJohn Dyson {
1967b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19684a11ca4eSPoul-Henning Kamp 	struct timeval atv;
19692244ea07SJohn Dyson 	struct kaioinfo *ki;
19705652770dSJohn Baldwin 	struct kaiocb *firstjob, *job;
19713858a1f4SJohn Baldwin 	int error, i, timo;
19722244ea07SJohn Dyson 
19732244ea07SJohn Dyson 	timo = 0;
19743858a1f4SJohn Baldwin 	if (ts) {
19753858a1f4SJohn Baldwin 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
19762244ea07SJohn Dyson 			return (EINVAL);
19772244ea07SJohn Dyson 
19783858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
19792244ea07SJohn Dyson 		if (itimerfix(&atv))
19802244ea07SJohn Dyson 			return (EINVAL);
1981227ee8a1SPoul-Henning Kamp 		timo = tvtohz(&atv);
19822244ea07SJohn Dyson 	}
19832244ea07SJohn Dyson 
19842244ea07SJohn Dyson 	ki = p->p_aioinfo;
19852244ea07SJohn Dyson 	if (ki == NULL)
1986ac41f2efSAlfred Perlstein 		return (EAGAIN);
19872244ea07SJohn Dyson 
19883858a1f4SJohn Baldwin 	if (njoblist == 0)
1989ac41f2efSAlfred Perlstein 		return (0);
19902244ea07SJohn Dyson 
1991759ccccaSDavid Xu 	AIO_LOCK(ki);
19921ce91824SDavid Xu 	for (;;) {
19935652770dSJohn Baldwin 		firstjob = NULL;
19941ce91824SDavid Xu 		error = 0;
19955652770dSJohn Baldwin 		TAILQ_FOREACH(job, &ki->kaio_all, allist) {
199684af4da6SJohn Dyson 			for (i = 0; i < njoblist; i++) {
19975652770dSJohn Baldwin 				if (job->ujob == ujoblist[i]) {
19985652770dSJohn Baldwin 					if (firstjob == NULL)
19995652770dSJohn Baldwin 						firstjob = job;
2000f3215338SJohn Baldwin 					if (job->jobflags & KAIOCB_FINISHED)
20011ce91824SDavid Xu 						goto RETURN;
200284af4da6SJohn Dyson 				}
200384af4da6SJohn Dyson 			}
200484af4da6SJohn Dyson 		}
20051ce91824SDavid Xu 		/* All tasks were finished. */
20065652770dSJohn Baldwin 		if (firstjob == NULL)
20071ce91824SDavid Xu 			break;
20082244ea07SJohn Dyson 
2009fd3bf775SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
2010759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
20111ce91824SDavid Xu 		    "aiospn", timo);
20121ce91824SDavid Xu 		if (error == ERESTART)
20131ce91824SDavid Xu 			error = EINTR;
20141ce91824SDavid Xu 		if (error)
20151ce91824SDavid Xu 			break;
20162244ea07SJohn Dyson 	}
20171ce91824SDavid Xu RETURN:
2018759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20193858a1f4SJohn Baldwin 	return (error);
20203858a1f4SJohn Baldwin }
20213858a1f4SJohn Baldwin 
20223858a1f4SJohn Baldwin int
20238451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
20243858a1f4SJohn Baldwin {
20253858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
20263858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
20273858a1f4SJohn Baldwin 	int error;
20283858a1f4SJohn Baldwin 
2029913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
20303858a1f4SJohn Baldwin 		return (EINVAL);
20313858a1f4SJohn Baldwin 
20323858a1f4SJohn Baldwin 	if (uap->timeout) {
20333858a1f4SJohn Baldwin 		/* Get timespec struct. */
20343858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
20353858a1f4SJohn Baldwin 			return (error);
20363858a1f4SJohn Baldwin 		tsp = &ts;
20373858a1f4SJohn Baldwin 	} else
20383858a1f4SJohn Baldwin 		tsp = NULL;
20393858a1f4SJohn Baldwin 
2040913b9329SAlan Somers 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
20413858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
20423858a1f4SJohn Baldwin 	if (error == 0)
20433858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2044913b9329SAlan Somers 	free(ujoblist, M_AIOS);
20451ce91824SDavid Xu 	return (error);
20462244ea07SJohn Dyson }
2047ee877a35SJohn Dyson 
2048ee877a35SJohn Dyson /*
204972bce9ffSAlan Somers  * aio_cancel cancels any non-bio aio operations not currently in progress.
2050ee877a35SJohn Dyson  */
2051ee877a35SJohn Dyson int
20528451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2053fd3bf775SJohn Dyson {
2054b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2055dd85920aSJason Evans 	struct kaioinfo *ki;
20565652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
2057dd85920aSJason Evans 	struct file *fp;
20581ce91824SDavid Xu 	int error;
2059dd85920aSJason Evans 	int cancelled = 0;
2060dd85920aSJason Evans 	int notcancelled = 0;
2061dd85920aSJason Evans 	struct vnode *vp;
2062dd85920aSJason Evans 
20632a522eb9SJohn Baldwin 	/* Lookup file object. */
2064cbd92ce6SMatt Macy 	error = fget(td, uap->fd, &cap_no_rights, &fp);
20652a522eb9SJohn Baldwin 	if (error)
20662a522eb9SJohn Baldwin 		return (error);
2067dd85920aSJason Evans 
20681ce91824SDavid Xu 	ki = p->p_aioinfo;
20691ce91824SDavid Xu 	if (ki == NULL)
20701ce91824SDavid Xu 		goto done;
20711ce91824SDavid Xu 
2072dd85920aSJason Evans 	if (fp->f_type == DTYPE_VNODE) {
20733b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
20747ad2a82dSMateusz Guzik 		if (vn_isdisk(vp)) {
20752a522eb9SJohn Baldwin 			fdrop(fp, td);
2076b40ce416SJulian Elischer 			td->td_retval[0] = AIO_NOTCANCELED;
2077ac41f2efSAlfred Perlstein 			return (0);
2078dd85920aSJason Evans 		}
2079dd85920aSJason Evans 	}
2080dd85920aSJason Evans 
2081759ccccaSDavid Xu 	AIO_LOCK(ki);
20825652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
20835652770dSJohn Baldwin 		if ((uap->fd == job->uaiocb.aio_fildes) &&
2084dd85920aSJason Evans 		    ((uap->aiocbp == NULL) ||
20855652770dSJohn Baldwin 		     (uap->aiocbp == job->ujob))) {
2086f3215338SJohn Baldwin 			if (aio_cancel_job(p, ki, job)) {
20871ce91824SDavid Xu 				cancelled++;
2088dd85920aSJason Evans 			} else {
2089dd85920aSJason Evans 				notcancelled++;
2090dd85920aSJason Evans 			}
20911aa4c324SDavid Xu 			if (uap->aiocbp != NULL)
20921aa4c324SDavid Xu 				break;
2093dd85920aSJason Evans 		}
2094dd85920aSJason Evans 	}
2095759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20961ce91824SDavid Xu 
2097ad49abc0SAlan Cox done:
20982a522eb9SJohn Baldwin 	fdrop(fp, td);
20991aa4c324SDavid Xu 
21001aa4c324SDavid Xu 	if (uap->aiocbp != NULL) {
2101dd85920aSJason Evans 		if (cancelled) {
2102b40ce416SJulian Elischer 			td->td_retval[0] = AIO_CANCELED;
2103ac41f2efSAlfred Perlstein 			return (0);
2104dd85920aSJason Evans 		}
21051aa4c324SDavid Xu 	}
21061aa4c324SDavid Xu 
21071aa4c324SDavid Xu 	if (notcancelled) {
21081aa4c324SDavid Xu 		td->td_retval[0] = AIO_NOTCANCELED;
21091aa4c324SDavid Xu 		return (0);
21101aa4c324SDavid Xu 	}
21111aa4c324SDavid Xu 
21121aa4c324SDavid Xu 	if (cancelled) {
21131aa4c324SDavid Xu 		td->td_retval[0] = AIO_CANCELED;
21141aa4c324SDavid Xu 		return (0);
21151aa4c324SDavid Xu 	}
21161aa4c324SDavid Xu 
2117b40ce416SJulian Elischer 	td->td_retval[0] = AIO_ALLDONE;
2118dd85920aSJason Evans 
2119ac41f2efSAlfred Perlstein 	return (0);
2120ee877a35SJohn Dyson }
2121ee877a35SJohn Dyson 
2122ee877a35SJohn Dyson /*
2123873fbcd7SRobert Watson  * aio_error is implemented in the kernel level for compatibility purposes
2124873fbcd7SRobert Watson  * only.  For a user mode async implementation, it would be best to do it in
2125873fbcd7SRobert Watson  * a userland subroutine.
2126ee877a35SJohn Dyson  */
21273858a1f4SJohn Baldwin static int
21285652770dSJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2129fd3bf775SJohn Dyson {
2130b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
21315652770dSJohn Baldwin 	struct kaiocb *job;
21322244ea07SJohn Dyson 	struct kaioinfo *ki;
21331ce91824SDavid Xu 	int status;
2134ee877a35SJohn Dyson 
21352244ea07SJohn Dyson 	ki = p->p_aioinfo;
21361ce91824SDavid Xu 	if (ki == NULL) {
21371ce91824SDavid Xu 		td->td_retval[0] = EINVAL;
21381ce91824SDavid Xu 		return (0);
21391ce91824SDavid Xu 	}
2140ee877a35SJohn Dyson 
2141759ccccaSDavid Xu 	AIO_LOCK(ki);
21425652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_all, allist) {
21435652770dSJohn Baldwin 		if (job->ujob == ujob) {
2144f3215338SJohn Baldwin 			if (job->jobflags & KAIOCB_FINISHED)
21451ce91824SDavid Xu 				td->td_retval[0] =
21465652770dSJohn Baldwin 					job->uaiocb._aiocb_private.error;
21471ce91824SDavid Xu 			else
2148b40ce416SJulian Elischer 				td->td_retval[0] = EINPROGRESS;
2149759ccccaSDavid Xu 			AIO_UNLOCK(ki);
2150ac41f2efSAlfred Perlstein 			return (0);
21512244ea07SJohn Dyson 		}
21522244ea07SJohn Dyson 	}
2153759ccccaSDavid Xu 	AIO_UNLOCK(ki);
215484af4da6SJohn Dyson 
21552244ea07SJohn Dyson 	/*
2156a9bf5e37SDavid Xu 	 * Hack for failure of aio_aqueue.
21572244ea07SJohn Dyson 	 */
21585652770dSJohn Baldwin 	status = ops->fetch_status(ujob);
21591ce91824SDavid Xu 	if (status == -1) {
21605652770dSJohn Baldwin 		td->td_retval[0] = ops->fetch_error(ujob);
21611ce91824SDavid Xu 		return (0);
21621ce91824SDavid Xu 	}
21631ce91824SDavid Xu 
21641ce91824SDavid Xu 	td->td_retval[0] = EINVAL;
21651ce91824SDavid Xu 	return (0);
2166ee877a35SJohn Dyson }
2167ee877a35SJohn Dyson 
21683858a1f4SJohn Baldwin int
21698451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap)
21703858a1f4SJohn Baldwin {
21713858a1f4SJohn Baldwin 
21723858a1f4SJohn Baldwin 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
21733858a1f4SJohn Baldwin }
21743858a1f4SJohn Baldwin 
2175eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */
2176399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2177ee877a35SJohn Dyson int
2178399e8c17SJohn Baldwin freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
21790972628aSDavid Xu {
21800972628aSDavid Xu 
21813858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
21823858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
21830972628aSDavid Xu }
2184399e8c17SJohn Baldwin #endif
21850972628aSDavid Xu 
21860972628aSDavid Xu int
21878451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap)
2188fd3bf775SJohn Dyson {
218921d56e9cSAlfred Perlstein 
21903858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2191ee877a35SJohn Dyson }
2192ee877a35SJohn Dyson 
2193022ca2fcSAlan Somers int
2194022ca2fcSAlan Somers sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2195022ca2fcSAlan Somers {
2196022ca2fcSAlan Somers 
2197022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2198022ca2fcSAlan Somers }
2199022ca2fcSAlan Somers 
2200eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */
2201399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2202ee877a35SJohn Dyson int
2203399e8c17SJohn Baldwin freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
22040972628aSDavid Xu {
22050972628aSDavid Xu 
22063858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
22073858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
22080972628aSDavid Xu }
2209399e8c17SJohn Baldwin #endif
22100972628aSDavid Xu 
22110972628aSDavid Xu int
22128451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap)
2213fd3bf775SJohn Dyson {
221421d56e9cSAlfred Perlstein 
22153858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
22160972628aSDavid Xu }
22170972628aSDavid Xu 
22186160e12cSGleb Smirnoff int
2219022ca2fcSAlan Somers sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2220022ca2fcSAlan Somers {
2221022ca2fcSAlan Somers 
2222022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2223022ca2fcSAlan Somers }
2224022ca2fcSAlan Somers 
2225022ca2fcSAlan Somers int
22266160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
22276160e12cSGleb Smirnoff {
22286160e12cSGleb Smirnoff 
22296160e12cSGleb Smirnoff 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
22306160e12cSGleb Smirnoff }
22316160e12cSGleb Smirnoff 
22320972628aSDavid Xu static int
22333858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
22343858a1f4SJohn Baldwin     struct aiocb **acb_list, int nent, struct sigevent *sig,
22353858a1f4SJohn Baldwin     struct aiocb_ops *ops)
22360972628aSDavid Xu {
2237b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22385652770dSJohn Baldwin 	struct aiocb *job;
22392244ea07SJohn Dyson 	struct kaioinfo *ki;
22401ce91824SDavid Xu 	struct aioliojob *lj;
224169cd28daSDoug Ambrisko 	struct kevent kev;
22421ce91824SDavid Xu 	int error;
224352c09831SAlan Somers 	int nagain, nerror;
2244ee877a35SJohn Dyson 	int i;
2245ee877a35SJohn Dyson 
22463858a1f4SJohn Baldwin 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2247ac41f2efSAlfred Perlstein 		return (EINVAL);
22482244ea07SJohn Dyson 
2249913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
2250ac41f2efSAlfred Perlstein 		return (EINVAL);
22512244ea07SJohn Dyson 
2252bfbbc4aaSJason Evans 	if (p->p_aioinfo == NULL)
22532244ea07SJohn Dyson 		aio_init_aioinfo(p);
22542244ea07SJohn Dyson 
22552244ea07SJohn Dyson 	ki = p->p_aioinfo;
22562244ea07SJohn Dyson 
2257a163d034SWarner Losh 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
225884af4da6SJohn Dyson 	lj->lioj_flags = 0;
22591ce91824SDavid Xu 	lj->lioj_count = 0;
22601ce91824SDavid Xu 	lj->lioj_finished_count = 0;
22612e5f6152SMark Johnston 	lj->lioj_signal.sigev_notify = SIGEV_NONE;
2262d8b0556cSKonstantin Belousov 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
22634c0fb2cfSDavid Xu 	ksiginfo_init(&lj->lioj_ksi);
226469cd28daSDoug Ambrisko 
226584af4da6SJohn Dyson 	/*
2266bfbbc4aaSJason Evans 	 * Setup signal.
226784af4da6SJohn Dyson 	 */
22683858a1f4SJohn Baldwin 	if (sig && (mode == LIO_NOWAIT)) {
22693858a1f4SJohn Baldwin 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
227069cd28daSDoug Ambrisko 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
227169cd28daSDoug Ambrisko 			/* Assume only new style KEVENT */
227236c4960eSMark Johnston 			memset(&kev, 0, sizeof(kev));
227369cd28daSDoug Ambrisko 			kev.filter = EVFILT_LIO;
227469cd28daSDoug Ambrisko 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
22753858a1f4SJohn Baldwin 			kev.ident = (uintptr_t)uacb_list; /* something unique */
227669cd28daSDoug Ambrisko 			kev.data = (intptr_t)lj;
22771ce91824SDavid Xu 			/* pass user defined sigval data */
22781ce91824SDavid Xu 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
22794db71d27SJohn-Mark Gurney 			error = kqfd_register(
2280792843c3SMark Johnston 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2281792843c3SMark Johnston 			    M_WAITOK);
228269cd28daSDoug Ambrisko 			if (error) {
228369cd28daSDoug Ambrisko 				uma_zfree(aiolio_zone, lj);
228469cd28daSDoug Ambrisko 				return (error);
228569cd28daSDoug Ambrisko 			}
22861ce91824SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
22871ce91824SDavid Xu 			;
228868d71118SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
228968d71118SDavid Xu 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
229068d71118SDavid Xu 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
229169cd28daSDoug Ambrisko 					uma_zfree(aiolio_zone, lj);
229269cd28daSDoug Ambrisko 					return EINVAL;
229368d71118SDavid Xu 				}
229484af4da6SJohn Dyson 				lj->lioj_flags |= LIOJ_SIGNAL;
229568d71118SDavid Xu 		} else {
229668d71118SDavid Xu 			uma_zfree(aiolio_zone, lj);
229768d71118SDavid Xu 			return EINVAL;
22984d752b01SAlan Cox 		}
22991ce91824SDavid Xu 	}
230069cd28daSDoug Ambrisko 
2301759ccccaSDavid Xu 	AIO_LOCK(ki);
23022f3cf918SAlfred Perlstein 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
23032244ea07SJohn Dyson 	/*
23041ce91824SDavid Xu 	 * Add extra aiocb count to avoid the lio to be freed
23051ce91824SDavid Xu 	 * by other threads doing aio_waitcomplete or aio_return,
23061ce91824SDavid Xu 	 * and prevent event from being sent until we have queued
23071ce91824SDavid Xu 	 * all tasks.
23081ce91824SDavid Xu 	 */
23091ce91824SDavid Xu 	lj->lioj_count = 1;
2310759ccccaSDavid Xu 	AIO_UNLOCK(ki);
23111ce91824SDavid Xu 
23121ce91824SDavid Xu 	/*
2313bfbbc4aaSJason Evans 	 * Get pointers to the list of I/O requests.
23142244ea07SJohn Dyson 	 */
231552c09831SAlan Somers 	nagain = 0;
2316fd3bf775SJohn Dyson 	nerror = 0;
23173858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++) {
23185652770dSJohn Baldwin 		job = acb_list[i];
23195652770dSJohn Baldwin 		if (job != NULL) {
23205652770dSJohn Baldwin 			error = aio_aqueue(td, job, lj, LIO_NOP, ops);
232152c09831SAlan Somers 			if (error == EAGAIN)
232252c09831SAlan Somers 				nagain++;
232352c09831SAlan Somers 			else if (error != 0)
2324fd3bf775SJohn Dyson 				nerror++;
2325fd3bf775SJohn Dyson 		}
2326fd3bf775SJohn Dyson 	}
23272244ea07SJohn Dyson 
23281ce91824SDavid Xu 	error = 0;
2329759ccccaSDavid Xu 	AIO_LOCK(ki);
23303858a1f4SJohn Baldwin 	if (mode == LIO_WAIT) {
23311ce91824SDavid Xu 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2332fd3bf775SJohn Dyson 			ki->kaio_flags |= KAIO_WAKEUP;
2333759ccccaSDavid Xu 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
23341ce91824SDavid Xu 			    PRIBIO | PCATCH, "aiospn", 0);
23351ce91824SDavid Xu 			if (error == ERESTART)
23361ce91824SDavid Xu 				error = EINTR;
23371ce91824SDavid Xu 			if (error)
23381ce91824SDavid Xu 				break;
23391ce91824SDavid Xu 		}
23401ce91824SDavid Xu 	} else {
23411ce91824SDavid Xu 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
23421ce91824SDavid Xu 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
23431ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
23441ce91824SDavid Xu 				KNOTE_LOCKED(&lj->klist, 1);
23451ce91824SDavid Xu 			}
234629331656SKonstantin Belousov 			if ((lj->lioj_flags & (LIOJ_SIGNAL |
234729331656SKonstantin Belousov 			    LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
234829331656SKonstantin Belousov 			    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
23491ce91824SDavid Xu 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
23506814c2daSKonstantin Belousov 				aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
23516814c2daSKonstantin Belousov 				    lj->lioj_count != 1);
23521ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
23532244ea07SJohn Dyson 			}
23542244ea07SJohn Dyson 		}
23551ce91824SDavid Xu 	}
23561ce91824SDavid Xu 	lj->lioj_count--;
23571ce91824SDavid Xu 	if (lj->lioj_count == 0) {
23581ce91824SDavid Xu 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
23591ce91824SDavid Xu 		knlist_delete(&lj->klist, curthread, 1);
2360759ccccaSDavid Xu 		PROC_LOCK(p);
23611ce91824SDavid Xu 		sigqueue_take(&lj->lioj_ksi);
23621ce91824SDavid Xu 		PROC_UNLOCK(p);
2363759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23641ce91824SDavid Xu 		uma_zfree(aiolio_zone, lj);
23651ce91824SDavid Xu 	} else
2366759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23672244ea07SJohn Dyson 
23681ce91824SDavid Xu 	if (nerror)
23691ce91824SDavid Xu 		return (EIO);
237052c09831SAlan Somers 	else if (nagain)
237152c09831SAlan Somers 		return (EAGAIN);
237252c09831SAlan Somers 	else
23731ce91824SDavid Xu 		return (error);
2374ee877a35SJohn Dyson }
2375fd3bf775SJohn Dyson 
23763858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
2377399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
23783858a1f4SJohn Baldwin int
2379399e8c17SJohn Baldwin freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
23803858a1f4SJohn Baldwin {
23813858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23823858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23833858a1f4SJohn Baldwin 	struct osigevent osig;
23843858a1f4SJohn Baldwin 	int error, nent;
23853858a1f4SJohn Baldwin 
23863858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23873858a1f4SJohn Baldwin 		return (EINVAL);
23883858a1f4SJohn Baldwin 
23893858a1f4SJohn Baldwin 	nent = uap->nent;
2390913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
23913858a1f4SJohn Baldwin 		return (EINVAL);
23923858a1f4SJohn Baldwin 
23933858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23943858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
23953858a1f4SJohn Baldwin 		if (error)
23963858a1f4SJohn Baldwin 			return (error);
23973858a1f4SJohn Baldwin 		error = convert_old_sigevent(&osig, &sig);
23983858a1f4SJohn Baldwin 		if (error)
23993858a1f4SJohn Baldwin 			return (error);
24003858a1f4SJohn Baldwin 		sigp = &sig;
24013858a1f4SJohn Baldwin 	} else
24023858a1f4SJohn Baldwin 		sigp = NULL;
24033858a1f4SJohn Baldwin 
24043858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24053858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24063858a1f4SJohn Baldwin 	if (error == 0)
24073858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode,
24083858a1f4SJohn Baldwin 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
24093858a1f4SJohn Baldwin 		    &aiocb_ops_osigevent);
24103858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24113858a1f4SJohn Baldwin 	return (error);
24123858a1f4SJohn Baldwin }
2413399e8c17SJohn Baldwin #endif
24143858a1f4SJohn Baldwin 
24153858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
24163858a1f4SJohn Baldwin int
24178451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
24183858a1f4SJohn Baldwin {
24193858a1f4SJohn Baldwin 	struct aiocb **acb_list;
24203858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
24213858a1f4SJohn Baldwin 	int error, nent;
24223858a1f4SJohn Baldwin 
24233858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
24243858a1f4SJohn Baldwin 		return (EINVAL);
24253858a1f4SJohn Baldwin 
24263858a1f4SJohn Baldwin 	nent = uap->nent;
2427913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
24283858a1f4SJohn Baldwin 		return (EINVAL);
24293858a1f4SJohn Baldwin 
24303858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
24313858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig, sizeof(sig));
24323858a1f4SJohn Baldwin 		if (error)
24333858a1f4SJohn Baldwin 			return (error);
24343858a1f4SJohn Baldwin 		sigp = &sig;
24353858a1f4SJohn Baldwin 	} else
24363858a1f4SJohn Baldwin 		sigp = NULL;
24373858a1f4SJohn Baldwin 
24383858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24393858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24403858a1f4SJohn Baldwin 	if (error == 0)
24413858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
24423858a1f4SJohn Baldwin 		    nent, sigp, &aiocb_ops);
24433858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24443858a1f4SJohn Baldwin 	return (error);
24453858a1f4SJohn Baldwin }
24463858a1f4SJohn Baldwin 
2447fd3bf775SJohn Dyson static void
2448022ca2fcSAlan Somers aio_biocleanup(struct bio *bp)
2449fd3bf775SJohn Dyson {
24505652770dSJohn Baldwin 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
245127b8220dSDavid Xu 	struct kaioinfo *ki;
245201206038SAlan Somers 	struct buf *pbuf = (struct buf *)bp->bio_caller2;
24531ce91824SDavid Xu 
2454f743d981SAlexander Motin 	/* Release mapping into kernel space. */
245501206038SAlan Somers 	if (pbuf != NULL) {
245601206038SAlan Somers 		MPASS(pbuf->b_npages <= atop(maxphys) + 1);
245701206038SAlan Somers 		pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
245801206038SAlan Somers 		vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
245901206038SAlan Somers 		uma_zfree(pbuf_zone, pbuf);
2460f743d981SAlexander Motin 		atomic_subtract_int(&num_buf_aio, 1);
2461a9d4fe97SKonstantin Belousov 		ki = job->userproc->p_aioinfo;
2462f3215338SJohn Baldwin 		AIO_LOCK(ki);
2463f3215338SJohn Baldwin 		ki->kaio_buffer_count--;
2464f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
2465cd853791SKonstantin Belousov 	} else {
246601206038SAlan Somers 		MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
246701206038SAlan Somers 		vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
246801206038SAlan Somers 		free(bp->bio_ma, M_TEMP);
24698091e52bSJohn Baldwin 		atomic_subtract_int(&num_unmapped_aio, 1);
2470cd853791SKonstantin Belousov 	}
2471f743d981SAlexander Motin 	g_destroy_bio(bp);
247284af4da6SJohn Dyson }
2473bfbbc4aaSJason Evans 
2474022ca2fcSAlan Somers static void
2475022ca2fcSAlan Somers aio_biowakeup(struct bio *bp)
2476022ca2fcSAlan Somers {
2477022ca2fcSAlan Somers 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2478022ca2fcSAlan Somers 	size_t nbytes;
2479022ca2fcSAlan Somers 	long bcount = bp->bio_bcount;
2480022ca2fcSAlan Somers 	long resid = bp->bio_resid;
2481022ca2fcSAlan Somers 	int error, opcode, nblks;
2482022ca2fcSAlan Somers 	int bio_error = bp->bio_error;
2483022ca2fcSAlan Somers 	uint16_t flags = bp->bio_flags;
2484022ca2fcSAlan Somers 
2485022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
2486022ca2fcSAlan Somers 
2487022ca2fcSAlan Somers 	aio_biocleanup(bp);
2488022ca2fcSAlan Somers 
2489022ca2fcSAlan Somers 	nbytes =bcount - resid;
2490022ca2fcSAlan Somers 	atomic_add_acq_long(&job->nbytes, nbytes);
2491022ca2fcSAlan Somers 	nblks = btodb(nbytes);
2492022ca2fcSAlan Somers 	error = 0;
2493022ca2fcSAlan Somers 	/*
2494022ca2fcSAlan Somers 	 * If multiple bios experienced an error, the job will reflect the
2495022ca2fcSAlan Somers 	 * error of whichever failed bio completed last.
2496022ca2fcSAlan Somers 	 */
2497022ca2fcSAlan Somers 	if (flags & BIO_ERROR)
2498022ca2fcSAlan Somers 		atomic_set_int(&job->error, bio_error);
24992247f489SAlan Somers 	if (opcode & LIO_WRITE)
2500022ca2fcSAlan Somers 		atomic_add_int(&job->outblock, nblks);
2501022ca2fcSAlan Somers 	else
2502022ca2fcSAlan Somers 		atomic_add_int(&job->inblock, nblks);
2503022ca2fcSAlan Somers 	atomic_subtract_int(&job->nbio, 1);
2504022ca2fcSAlan Somers 
2505022ca2fcSAlan Somers 
2506022ca2fcSAlan Somers 	if (atomic_load_int(&job->nbio) == 0) {
2507022ca2fcSAlan Somers 		if (atomic_load_int(&job->error))
2508022ca2fcSAlan Somers 			aio_complete(job, -1, job->error);
2509022ca2fcSAlan Somers 		else
2510022ca2fcSAlan Somers 			aio_complete(job, atomic_load_long(&job->nbytes), 0);
2511022ca2fcSAlan Somers 	}
2512022ca2fcSAlan Somers }
2513022ca2fcSAlan Somers 
2514eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */
25153858a1f4SJohn Baldwin static int
25165652770dSJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
25173858a1f4SJohn Baldwin     struct timespec *ts, struct aiocb_ops *ops)
2518bfbbc4aaSJason Evans {
2519b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2520bfbbc4aaSJason Evans 	struct timeval atv;
2521bfbbc4aaSJason Evans 	struct kaioinfo *ki;
25225652770dSJohn Baldwin 	struct kaiocb *job;
25235652770dSJohn Baldwin 	struct aiocb *ujob;
2524bb430bc7SJohn Baldwin 	long error, status;
2525bb430bc7SJohn Baldwin 	int timo;
2526bfbbc4aaSJason Evans 
25275652770dSJohn Baldwin 	ops->store_aiocb(ujobp, NULL);
2528dd85920aSJason Evans 
252938d68e2dSPawel Jakub Dawidek 	if (ts == NULL) {
2530bfbbc4aaSJason Evans 		timo = 0;
253138d68e2dSPawel Jakub Dawidek 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
253238d68e2dSPawel Jakub Dawidek 		timo = -1;
253338d68e2dSPawel Jakub Dawidek 	} else {
25343858a1f4SJohn Baldwin 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2535bfbbc4aaSJason Evans 			return (EINVAL);
2536bfbbc4aaSJason Evans 
25373858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2538bfbbc4aaSJason Evans 		if (itimerfix(&atv))
2539bfbbc4aaSJason Evans 			return (EINVAL);
2540bfbbc4aaSJason Evans 		timo = tvtohz(&atv);
2541bfbbc4aaSJason Evans 	}
2542bfbbc4aaSJason Evans 
25438213baf0SChristian S.J. Peron 	if (p->p_aioinfo == NULL)
2544323fe565SDavid Xu 		aio_init_aioinfo(p);
25458213baf0SChristian S.J. Peron 	ki = p->p_aioinfo;
2546bfbbc4aaSJason Evans 
25471ce91824SDavid Xu 	error = 0;
25485652770dSJohn Baldwin 	job = NULL;
2549759ccccaSDavid Xu 	AIO_LOCK(ki);
25505652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
255138d68e2dSPawel Jakub Dawidek 		if (timo == -1) {
255238d68e2dSPawel Jakub Dawidek 			error = EWOULDBLOCK;
255338d68e2dSPawel Jakub Dawidek 			break;
255438d68e2dSPawel Jakub Dawidek 		}
25551ce91824SDavid Xu 		ki->kaio_flags |= KAIO_WAKEUP;
2556759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
25571ce91824SDavid Xu 		    "aiowc", timo);
255827b8220dSDavid Xu 		if (timo && error == ERESTART)
25591ce91824SDavid Xu 			error = EINTR;
25601ce91824SDavid Xu 		if (error)
25611ce91824SDavid Xu 			break;
25621ce91824SDavid Xu 	}
25631ce91824SDavid Xu 
25645652770dSJohn Baldwin 	if (job != NULL) {
2565f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
25665652770dSJohn Baldwin 		ujob = job->ujob;
25675652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
25685652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
25691ce91824SDavid Xu 		td->td_retval[0] = status;
2570b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
2571b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
2572b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
2573b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
25745652770dSJohn Baldwin 		aio_free_entry(job);
2575759ccccaSDavid Xu 		AIO_UNLOCK(ki);
25765652770dSJohn Baldwin 		ops->store_aiocb(ujobp, ujob);
25775652770dSJohn Baldwin 		ops->store_error(ujob, error);
25785652770dSJohn Baldwin 		ops->store_status(ujob, status);
25791ce91824SDavid Xu 	} else
2580759ccccaSDavid Xu 		AIO_UNLOCK(ki);
2581bfbbc4aaSJason Evans 
2582ac41f2efSAlfred Perlstein 	return (error);
2583bfbbc4aaSJason Evans }
2584cb679c38SJonathan Lemon 
258599eee864SDavid Xu int
25868451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
25873858a1f4SJohn Baldwin {
25883858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
25893858a1f4SJohn Baldwin 	int error;
25903858a1f4SJohn Baldwin 
25913858a1f4SJohn Baldwin 	if (uap->timeout) {
25923858a1f4SJohn Baldwin 		/* Get timespec struct. */
25933858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts, sizeof(ts));
25943858a1f4SJohn Baldwin 		if (error)
25953858a1f4SJohn Baldwin 			return (error);
25963858a1f4SJohn Baldwin 		tsp = &ts;
25973858a1f4SJohn Baldwin 	} else
25983858a1f4SJohn Baldwin 		tsp = NULL;
25993858a1f4SJohn Baldwin 
26003858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
26013858a1f4SJohn Baldwin }
26023858a1f4SJohn Baldwin 
26033858a1f4SJohn Baldwin static int
26045652770dSJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
26053858a1f4SJohn Baldwin     struct aiocb_ops *ops)
260699eee864SDavid Xu {
2607801ac943SThomas Munro 	int listop;
260899eee864SDavid Xu 
2609801ac943SThomas Munro 	switch (op) {
2610801ac943SThomas Munro 	case O_SYNC:
2611801ac943SThomas Munro 		listop = LIO_SYNC;
2612801ac943SThomas Munro 		break;
2613801ac943SThomas Munro 	case O_DSYNC:
2614801ac943SThomas Munro 		listop = LIO_DSYNC;
2615801ac943SThomas Munro 		break;
2616801ac943SThomas Munro 	default:
261799eee864SDavid Xu 		return (EINVAL);
2618801ac943SThomas Munro 	}
2619801ac943SThomas Munro 
2620801ac943SThomas Munro 	return (aio_aqueue(td, ujob, NULL, listop, ops));
26213858a1f4SJohn Baldwin }
26223858a1f4SJohn Baldwin 
26233858a1f4SJohn Baldwin int
26248451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
26253858a1f4SJohn Baldwin {
26263858a1f4SJohn Baldwin 
26273858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
262899eee864SDavid Xu }
262999eee864SDavid Xu 
2630eb8e6d52SEivind Eklund /* kqueue attach function */
2631cb679c38SJonathan Lemon static int
2632cb679c38SJonathan Lemon filt_aioattach(struct knote *kn)
2633cb679c38SJonathan Lemon {
26342b34e843SKonstantin Belousov 	struct kaiocb *job;
26352b34e843SKonstantin Belousov 
26362b34e843SKonstantin Belousov 	job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2637cb679c38SJonathan Lemon 
2638cb679c38SJonathan Lemon 	/*
26395652770dSJohn Baldwin 	 * The job pointer must be validated before using it, so
2640cb679c38SJonathan Lemon 	 * registration is restricted to the kernel; the user cannot
2641cb679c38SJonathan Lemon 	 * set EV_FLAG1.
2642cb679c38SJonathan Lemon 	 */
2643cb679c38SJonathan Lemon 	if ((kn->kn_flags & EV_FLAG1) == 0)
2644cb679c38SJonathan Lemon 		return (EPERM);
26455652770dSJohn Baldwin 	kn->kn_ptr.p_aio = job;
2646cb679c38SJonathan Lemon 	kn->kn_flags &= ~EV_FLAG1;
2647cb679c38SJonathan Lemon 
26485652770dSJohn Baldwin 	knlist_add(&job->klist, kn, 0);
2649cb679c38SJonathan Lemon 
2650cb679c38SJonathan Lemon 	return (0);
2651cb679c38SJonathan Lemon }
2652cb679c38SJonathan Lemon 
2653eb8e6d52SEivind Eklund /* kqueue detach function */
2654cb679c38SJonathan Lemon static void
2655cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn)
2656cb679c38SJonathan Lemon {
26578e9fc278SDoug Ambrisko 	struct knlist *knl;
2658cb679c38SJonathan Lemon 
26598e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_aio->klist;
26608e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26618e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26628e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26638e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
2664cb679c38SJonathan Lemon }
2665cb679c38SJonathan Lemon 
2666eb8e6d52SEivind Eklund /* kqueue filter function */
2667cb679c38SJonathan Lemon /*ARGSUSED*/
2668cb679c38SJonathan Lemon static int
2669cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint)
2670cb679c38SJonathan Lemon {
26715652770dSJohn Baldwin 	struct kaiocb *job = kn->kn_ptr.p_aio;
2672cb679c38SJonathan Lemon 
26735652770dSJohn Baldwin 	kn->kn_data = job->uaiocb._aiocb_private.error;
2674f3215338SJohn Baldwin 	if (!(job->jobflags & KAIOCB_FINISHED))
2675cb679c38SJonathan Lemon 		return (0);
2676cb679c38SJonathan Lemon 	kn->kn_flags |= EV_EOF;
2677cb679c38SJonathan Lemon 	return (1);
2678cb679c38SJonathan Lemon }
267969cd28daSDoug Ambrisko 
268069cd28daSDoug Ambrisko /* kqueue attach function */
268169cd28daSDoug Ambrisko static int
268269cd28daSDoug Ambrisko filt_lioattach(struct knote *kn)
268369cd28daSDoug Ambrisko {
26842b34e843SKonstantin Belousov 	struct aioliojob *lj;
26852b34e843SKonstantin Belousov 
26862b34e843SKonstantin Belousov 	lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
268769cd28daSDoug Ambrisko 
268869cd28daSDoug Ambrisko 	/*
26891ce91824SDavid Xu 	 * The aioliojob pointer must be validated before using it, so
269069cd28daSDoug Ambrisko 	 * registration is restricted to the kernel; the user cannot
269169cd28daSDoug Ambrisko 	 * set EV_FLAG1.
269269cd28daSDoug Ambrisko 	 */
269369cd28daSDoug Ambrisko 	if ((kn->kn_flags & EV_FLAG1) == 0)
269469cd28daSDoug Ambrisko 		return (EPERM);
2695a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_lio = lj;
269669cd28daSDoug Ambrisko 	kn->kn_flags &= ~EV_FLAG1;
269769cd28daSDoug Ambrisko 
269869cd28daSDoug Ambrisko 	knlist_add(&lj->klist, kn, 0);
269969cd28daSDoug Ambrisko 
270069cd28daSDoug Ambrisko 	return (0);
270169cd28daSDoug Ambrisko }
270269cd28daSDoug Ambrisko 
270369cd28daSDoug Ambrisko /* kqueue detach function */
270469cd28daSDoug Ambrisko static void
270569cd28daSDoug Ambrisko filt_liodetach(struct knote *kn)
270669cd28daSDoug Ambrisko {
27078e9fc278SDoug Ambrisko 	struct knlist *knl;
270869cd28daSDoug Ambrisko 
27098e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_lio->klist;
27108e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
27118e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
27128e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
27138e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
271469cd28daSDoug Ambrisko }
271569cd28daSDoug Ambrisko 
271669cd28daSDoug Ambrisko /* kqueue filter function */
271769cd28daSDoug Ambrisko /*ARGSUSED*/
271869cd28daSDoug Ambrisko static int
271969cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint)
272069cd28daSDoug Ambrisko {
2721a8afa221SJean-Sébastien Pédron 	struct aioliojob * lj = kn->kn_ptr.p_lio;
27221ce91824SDavid Xu 
272369cd28daSDoug Ambrisko 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
272469cd28daSDoug Ambrisko }
27253858a1f4SJohn Baldwin 
2726841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2727399e8c17SJohn Baldwin #include <sys/mount.h>
2728399e8c17SJohn Baldwin #include <sys/socket.h>
2729399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32.h>
2730399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_proto.h>
2731399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2732399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_syscall.h>
2733399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_util.h>
27343858a1f4SJohn Baldwin 
27353858a1f4SJohn Baldwin struct __aiocb_private32 {
27363858a1f4SJohn Baldwin 	int32_t	status;
27373858a1f4SJohn Baldwin 	int32_t	error;
27383858a1f4SJohn Baldwin 	uint32_t kernelinfo;
27393858a1f4SJohn Baldwin };
27403858a1f4SJohn Baldwin 
2741399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27423858a1f4SJohn Baldwin typedef struct oaiocb32 {
27433858a1f4SJohn Baldwin 	int	aio_fildes;		/* File descriptor */
27443858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27453858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27463858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27473858a1f4SJohn Baldwin 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
27483858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27493858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27503858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27513858a1f4SJohn Baldwin } oaiocb32_t;
2752399e8c17SJohn Baldwin #endif
27533858a1f4SJohn Baldwin 
27543858a1f4SJohn Baldwin typedef struct aiocb32 {
27553858a1f4SJohn Baldwin 	int32_t	aio_fildes;		/* File descriptor */
27563858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27573858a1f4SJohn Baldwin 	uint32_t aio_buf;	/* I/O buffer in process space */
27583858a1f4SJohn Baldwin 	uint32_t aio_nbytes;	/* Number of bytes for I/O */
27593858a1f4SJohn Baldwin 	int	__spare__[2];
27603858a1f4SJohn Baldwin 	uint32_t __spare2__;
27613858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27623858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27633858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27643858a1f4SJohn Baldwin 	struct	sigevent32 aio_sigevent;	/* Signal to deliver */
27653858a1f4SJohn Baldwin } aiocb32_t;
27663858a1f4SJohn Baldwin 
2767399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27683858a1f4SJohn Baldwin static int
27693858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
27703858a1f4SJohn Baldwin {
27713858a1f4SJohn Baldwin 
27723858a1f4SJohn Baldwin 	/*
27733858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
27743858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
27753858a1f4SJohn Baldwin 	 */
27763858a1f4SJohn Baldwin 	CP(*osig, *nsig, sigev_notify);
27773858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
27783858a1f4SJohn Baldwin 	case SIGEV_NONE:
27793858a1f4SJohn Baldwin 		break;
27803858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
27813858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
27823858a1f4SJohn Baldwin 		break;
27833858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
27843858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
27853858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
27863858a1f4SJohn Baldwin 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
27873858a1f4SJohn Baldwin 		break;
27883858a1f4SJohn Baldwin 	default:
27893858a1f4SJohn Baldwin 		return (EINVAL);
27903858a1f4SJohn Baldwin 	}
27913858a1f4SJohn Baldwin 	return (0);
27923858a1f4SJohn Baldwin }
27933858a1f4SJohn Baldwin 
27943858a1f4SJohn Baldwin static int
2795022ca2fcSAlan Somers aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2796022ca2fcSAlan Somers     int type __unused)
27973858a1f4SJohn Baldwin {
27983858a1f4SJohn Baldwin 	struct oaiocb32 job32;
2799022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
28003858a1f4SJohn Baldwin 	int error;
28013858a1f4SJohn Baldwin 
2802022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
28033858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
28043858a1f4SJohn Baldwin 	if (error)
28053858a1f4SJohn Baldwin 		return (error);
28063858a1f4SJohn Baldwin 
2807022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2808022ca2fcSAlan Somers 
2809022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2810022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2811022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, aio_buf);
2812022ca2fcSAlan Somers 	CP(job32, *kcb, aio_nbytes);
2813022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
2814022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2815022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2816022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2817022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
28183858a1f4SJohn Baldwin 	return (convert_old_sigevent32(&job32.aio_sigevent,
2819022ca2fcSAlan Somers 	    &kcb->aio_sigevent));
28203858a1f4SJohn Baldwin }
2821399e8c17SJohn Baldwin #endif
28223858a1f4SJohn Baldwin 
28233858a1f4SJohn Baldwin static int
2824022ca2fcSAlan Somers aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
28253858a1f4SJohn Baldwin {
28263858a1f4SJohn Baldwin 	struct aiocb32 job32;
2827022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
2828022ca2fcSAlan Somers 	struct iovec32 *iov32;
28293858a1f4SJohn Baldwin 	int error;
28303858a1f4SJohn Baldwin 
28313858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
28323858a1f4SJohn Baldwin 	if (error)
28333858a1f4SJohn Baldwin 		return (error);
2834022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2835022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2836022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
28372884918cSMark Johnston 	if (type == LIO_NOP)
28382884918cSMark Johnston 		type = kcb->aio_lio_opcode;
28392247f489SAlan Somers 	if (type & LIO_VECTORED) {
2840022ca2fcSAlan Somers 		iov32 = PTRIN(job32.aio_iov);
2841022ca2fcSAlan Somers 		CP(job32, *kcb, aio_iovcnt);
2842022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
2843022ca2fcSAlan Somers 		error = freebsd32_copyinuio(iov32,
2844022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
2845022ca2fcSAlan Somers 		if (error)
2846022ca2fcSAlan Somers 			return (error);
2847022ca2fcSAlan Somers 	} else {
2848022ca2fcSAlan Somers 		PTRIN_CP(job32, *kcb, aio_buf);
2849022ca2fcSAlan Somers 		CP(job32, *kcb, aio_nbytes);
2850022ca2fcSAlan Somers 	}
2851022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2852022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2853022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2854022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2855022ca2fcSAlan Somers 	error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2856022ca2fcSAlan Somers 
2857022ca2fcSAlan Somers 	return (error);
28583858a1f4SJohn Baldwin }
28593858a1f4SJohn Baldwin 
28603858a1f4SJohn Baldwin static long
28613858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob)
28623858a1f4SJohn Baldwin {
28633858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28643858a1f4SJohn Baldwin 
28653858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28663858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.status));
28673858a1f4SJohn Baldwin }
28683858a1f4SJohn Baldwin 
28693858a1f4SJohn Baldwin static long
28703858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob)
28713858a1f4SJohn Baldwin {
28723858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28733858a1f4SJohn Baldwin 
28743858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28753858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.error));
28763858a1f4SJohn Baldwin }
28773858a1f4SJohn Baldwin 
28783858a1f4SJohn Baldwin static int
28793858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status)
28803858a1f4SJohn Baldwin {
28813858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28823858a1f4SJohn Baldwin 
28833858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28843858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.status, status));
28853858a1f4SJohn Baldwin }
28863858a1f4SJohn Baldwin 
28873858a1f4SJohn Baldwin static int
28883858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error)
28893858a1f4SJohn Baldwin {
28903858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28913858a1f4SJohn Baldwin 
28923858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28933858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.error, error));
28943858a1f4SJohn Baldwin }
28953858a1f4SJohn Baldwin 
28963858a1f4SJohn Baldwin static int
28973858a1f4SJohn Baldwin aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
28983858a1f4SJohn Baldwin {
28993858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
29003858a1f4SJohn Baldwin 
29013858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
29023858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
29033858a1f4SJohn Baldwin }
29043858a1f4SJohn Baldwin 
29053858a1f4SJohn Baldwin static int
29063858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
29073858a1f4SJohn Baldwin {
29083858a1f4SJohn Baldwin 
29093858a1f4SJohn Baldwin 	return (suword32(ujobp, (long)ujob));
29103858a1f4SJohn Baldwin }
29113858a1f4SJohn Baldwin 
29123858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = {
2913849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin,
29143858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29153858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29163858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29173858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29183858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29193858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29203858a1f4SJohn Baldwin };
29213858a1f4SJohn Baldwin 
2922399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29233858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = {
2924849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin_old_sigevent,
29253858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29263858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29273858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29283858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29293858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29303858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29313858a1f4SJohn Baldwin };
2932399e8c17SJohn Baldwin #endif
29333858a1f4SJohn Baldwin 
29343858a1f4SJohn Baldwin int
29353858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
29363858a1f4SJohn Baldwin {
29373858a1f4SJohn Baldwin 
29383858a1f4SJohn Baldwin 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29393858a1f4SJohn Baldwin }
29403858a1f4SJohn Baldwin 
29413858a1f4SJohn Baldwin int
29423858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
29433858a1f4SJohn Baldwin {
29443858a1f4SJohn Baldwin 	struct timespec32 ts32;
29453858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
29463858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
29473858a1f4SJohn Baldwin 	uint32_t *ujoblist32;
29483858a1f4SJohn Baldwin 	int error, i;
29493858a1f4SJohn Baldwin 
2950913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
29513858a1f4SJohn Baldwin 		return (EINVAL);
29523858a1f4SJohn Baldwin 
29533858a1f4SJohn Baldwin 	if (uap->timeout) {
29543858a1f4SJohn Baldwin 		/* Get timespec struct. */
29553858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
29563858a1f4SJohn Baldwin 			return (error);
29573858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
29583858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
29593858a1f4SJohn Baldwin 		tsp = &ts;
29603858a1f4SJohn Baldwin 	} else
29613858a1f4SJohn Baldwin 		tsp = NULL;
29623858a1f4SJohn Baldwin 
2963913b9329SAlan Somers 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
29643858a1f4SJohn Baldwin 	ujoblist32 = (uint32_t *)ujoblist;
29653858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
29663858a1f4SJohn Baldwin 	    sizeof(ujoblist32[0]));
29673858a1f4SJohn Baldwin 	if (error == 0) {
2968df485bdbSAlan Somers 		for (i = uap->nent - 1; i >= 0; i--)
29693858a1f4SJohn Baldwin 			ujoblist[i] = PTRIN(ujoblist32[i]);
29703858a1f4SJohn Baldwin 
29713858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
29723858a1f4SJohn Baldwin 	}
2973913b9329SAlan Somers 	free(ujoblist, M_AIOS);
29743858a1f4SJohn Baldwin 	return (error);
29753858a1f4SJohn Baldwin }
29763858a1f4SJohn Baldwin 
29773858a1f4SJohn Baldwin int
29783858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
29793858a1f4SJohn Baldwin {
29803858a1f4SJohn Baldwin 
29813858a1f4SJohn Baldwin 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29823858a1f4SJohn Baldwin }
29833858a1f4SJohn Baldwin 
2984399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29853858a1f4SJohn Baldwin int
2986399e8c17SJohn Baldwin freebsd6_freebsd32_aio_read(struct thread *td,
2987399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_read_args *uap)
29883858a1f4SJohn Baldwin {
29893858a1f4SJohn Baldwin 
29903858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29913858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29923858a1f4SJohn Baldwin }
2993399e8c17SJohn Baldwin #endif
29943858a1f4SJohn Baldwin 
29953858a1f4SJohn Baldwin int
29963858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
29973858a1f4SJohn Baldwin {
29983858a1f4SJohn Baldwin 
29993858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
30003858a1f4SJohn Baldwin 	    &aiocb32_ops));
30013858a1f4SJohn Baldwin }
30023858a1f4SJohn Baldwin 
3003022ca2fcSAlan Somers int
3004022ca2fcSAlan Somers freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
3005022ca2fcSAlan Somers {
3006022ca2fcSAlan Somers 
3007022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
3008022ca2fcSAlan Somers 	    &aiocb32_ops));
3009022ca2fcSAlan Somers }
3010022ca2fcSAlan Somers 
3011399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30123858a1f4SJohn Baldwin int
3013399e8c17SJohn Baldwin freebsd6_freebsd32_aio_write(struct thread *td,
3014399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_write_args *uap)
30153858a1f4SJohn Baldwin {
30163858a1f4SJohn Baldwin 
30173858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30183858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
30193858a1f4SJohn Baldwin }
3020399e8c17SJohn Baldwin #endif
30213858a1f4SJohn Baldwin 
30223858a1f4SJohn Baldwin int
30233858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
30243858a1f4SJohn Baldwin {
30253858a1f4SJohn Baldwin 
30263858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30273858a1f4SJohn Baldwin 	    &aiocb32_ops));
30283858a1f4SJohn Baldwin }
30293858a1f4SJohn Baldwin 
30303858a1f4SJohn Baldwin int
3031022ca2fcSAlan Somers freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3032022ca2fcSAlan Somers {
3033022ca2fcSAlan Somers 
3034022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3035022ca2fcSAlan Somers 	    &aiocb32_ops));
3036022ca2fcSAlan Somers }
3037022ca2fcSAlan Somers 
3038022ca2fcSAlan Somers int
30396160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
30406160e12cSGleb Smirnoff {
30416160e12cSGleb Smirnoff 
30426160e12cSGleb Smirnoff 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
30436160e12cSGleb Smirnoff 	    &aiocb32_ops));
30446160e12cSGleb Smirnoff }
30456160e12cSGleb Smirnoff 
30466160e12cSGleb Smirnoff int
30473858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td,
30483858a1f4SJohn Baldwin     struct freebsd32_aio_waitcomplete_args *uap)
30493858a1f4SJohn Baldwin {
3050e588eeb1SJohn Baldwin 	struct timespec32 ts32;
30513858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
30523858a1f4SJohn Baldwin 	int error;
30533858a1f4SJohn Baldwin 
30543858a1f4SJohn Baldwin 	if (uap->timeout) {
30553858a1f4SJohn Baldwin 		/* Get timespec struct. */
30563858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
30573858a1f4SJohn Baldwin 		if (error)
30583858a1f4SJohn Baldwin 			return (error);
30593858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
30603858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
30613858a1f4SJohn Baldwin 		tsp = &ts;
30623858a1f4SJohn Baldwin 	} else
30633858a1f4SJohn Baldwin 		tsp = NULL;
30643858a1f4SJohn Baldwin 
30653858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
30663858a1f4SJohn Baldwin 	    &aiocb32_ops));
30673858a1f4SJohn Baldwin }
30683858a1f4SJohn Baldwin 
30693858a1f4SJohn Baldwin int
30703858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
30713858a1f4SJohn Baldwin {
30723858a1f4SJohn Baldwin 
30733858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
30743858a1f4SJohn Baldwin 	    &aiocb32_ops));
30753858a1f4SJohn Baldwin }
30763858a1f4SJohn Baldwin 
3077399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30783858a1f4SJohn Baldwin int
3079399e8c17SJohn Baldwin freebsd6_freebsd32_lio_listio(struct thread *td,
3080399e8c17SJohn Baldwin     struct freebsd6_freebsd32_lio_listio_args *uap)
30813858a1f4SJohn Baldwin {
30823858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30833858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30843858a1f4SJohn Baldwin 	struct osigevent32 osig;
30853858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30863858a1f4SJohn Baldwin 	int error, i, nent;
30873858a1f4SJohn Baldwin 
30883858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30893858a1f4SJohn Baldwin 		return (EINVAL);
30903858a1f4SJohn Baldwin 
30913858a1f4SJohn Baldwin 	nent = uap->nent;
3092913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
30933858a1f4SJohn Baldwin 		return (EINVAL);
30943858a1f4SJohn Baldwin 
30953858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30963858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
30973858a1f4SJohn Baldwin 		if (error)
30983858a1f4SJohn Baldwin 			return (error);
30993858a1f4SJohn Baldwin 		error = convert_old_sigevent32(&osig, &sig);
31003858a1f4SJohn Baldwin 		if (error)
31013858a1f4SJohn Baldwin 			return (error);
31023858a1f4SJohn Baldwin 		sigp = &sig;
31033858a1f4SJohn Baldwin 	} else
31043858a1f4SJohn Baldwin 		sigp = NULL;
31053858a1f4SJohn Baldwin 
31063858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31073858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31083858a1f4SJohn Baldwin 	if (error) {
31093858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31103858a1f4SJohn Baldwin 		return (error);
31113858a1f4SJohn Baldwin 	}
31123858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31133858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31143858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31153858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31163858a1f4SJohn Baldwin 
31173858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31183858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31193858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent);
31203858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31213858a1f4SJohn Baldwin 	return (error);
31223858a1f4SJohn Baldwin }
3123399e8c17SJohn Baldwin #endif
31243858a1f4SJohn Baldwin 
31253858a1f4SJohn Baldwin int
31263858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
31273858a1f4SJohn Baldwin {
31283858a1f4SJohn Baldwin 	struct aiocb **acb_list;
31293858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
31303858a1f4SJohn Baldwin 	struct sigevent32 sig32;
31313858a1f4SJohn Baldwin 	uint32_t *acb_list32;
31323858a1f4SJohn Baldwin 	int error, i, nent;
31333858a1f4SJohn Baldwin 
31343858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
31353858a1f4SJohn Baldwin 		return (EINVAL);
31363858a1f4SJohn Baldwin 
31373858a1f4SJohn Baldwin 	nent = uap->nent;
3138913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
31393858a1f4SJohn Baldwin 		return (EINVAL);
31403858a1f4SJohn Baldwin 
31413858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
31423858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig32, sizeof(sig32));
31433858a1f4SJohn Baldwin 		if (error)
31443858a1f4SJohn Baldwin 			return (error);
31453858a1f4SJohn Baldwin 		error = convert_sigevent32(&sig32, &sig);
31463858a1f4SJohn Baldwin 		if (error)
31473858a1f4SJohn Baldwin 			return (error);
31483858a1f4SJohn Baldwin 		sigp = &sig;
31493858a1f4SJohn Baldwin 	} else
31503858a1f4SJohn Baldwin 		sigp = NULL;
31513858a1f4SJohn Baldwin 
31523858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31533858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31543858a1f4SJohn Baldwin 	if (error) {
31553858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31563858a1f4SJohn Baldwin 		return (error);
31573858a1f4SJohn Baldwin 	}
31583858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31593858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31603858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31613858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31623858a1f4SJohn Baldwin 
31633858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31643858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31653858a1f4SJohn Baldwin 	    &aiocb32_ops);
31663858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31673858a1f4SJohn Baldwin 	return (error);
31683858a1f4SJohn Baldwin }
31693858a1f4SJohn Baldwin 
31703858a1f4SJohn Baldwin #endif
3171