xref: /freebsd/sys/kern/vfs_aio.c (revision f30a1ae8d5290a52e898279bafc38556bf16bed8)
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;
72799eee864SDavid Xu 	int error;
72899eee864SDavid Xu 
72999eee864SDavid Xu 	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
73099eee864SDavid Xu 		goto drop;
731cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
73299eee864SDavid Xu 	if (vp->v_object != NULL) {
73389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(vp->v_object);
73499eee864SDavid Xu 		vm_object_page_clean(vp->v_object, 0, 0, 0);
73589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(vp->v_object);
73699eee864SDavid Xu 	}
737801ac943SThomas Munro 	if (op == LIO_DSYNC)
738801ac943SThomas Munro 		error = VOP_FDATASYNC(vp, td);
739801ac943SThomas Munro 	else
74099eee864SDavid Xu 		error = VOP_FSYNC(vp, MNT_WAIT, td);
74199eee864SDavid Xu 
742b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
74399eee864SDavid Xu 	vn_finished_write(mp);
74499eee864SDavid Xu drop:
74599eee864SDavid Xu 	return (error);
74699eee864SDavid Xu }
74799eee864SDavid Xu 
74899eee864SDavid Xu /*
749f95c13dbSGleb Smirnoff  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
75072bce9ffSAlan Somers  * does the I/O request for the non-bio version of the operations.  The normal
75172bce9ffSAlan Somers  * vn operations are used, and this code should work in all instances for every
75272bce9ffSAlan Somers  * type of file, including pipes, sockets, fifos, and regular files.
7531ce91824SDavid Xu  *
7541aa4c324SDavid Xu  * XXX I don't think it works well for socket, pipe, and fifo.
7552244ea07SJohn Dyson  */
75688ed460eSAlan Cox static void
7575652770dSJohn Baldwin aio_process_rw(struct kaiocb *job)
758fd3bf775SJohn Dyson {
759f8f750c5SRobert Watson 	struct ucred *td_savedcred;
760b40ce416SJulian Elischer 	struct thread *td;
7612244ea07SJohn Dyson 	struct aiocb *cb;
7622244ea07SJohn Dyson 	struct file *fp;
763bb430bc7SJohn Baldwin 	ssize_t cnt;
764b1012d80SJohn Baldwin 	long msgsnd_st, msgsnd_end;
765b1012d80SJohn Baldwin 	long msgrcv_st, msgrcv_end;
766b1012d80SJohn Baldwin 	long oublock_st, oublock_end;
767b1012d80SJohn Baldwin 	long inblock_st, inblock_end;
768022ca2fcSAlan Somers 	int error, opcode;
7692244ea07SJohn Dyson 
7705652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
771022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_READV ||
772022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITE ||
773022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITEV,
7745652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
775f95c13dbSGleb Smirnoff 
776f3215338SJohn Baldwin 	aio_switch_vmspace(job);
777b40ce416SJulian Elischer 	td = curthread;
778f8f750c5SRobert Watson 	td_savedcred = td->td_ucred;
7795652770dSJohn Baldwin 	td->td_ucred = job->cred;
780022ca2fcSAlan Somers 	job->uiop->uio_td = td;
7815652770dSJohn Baldwin 	cb = &job->uaiocb;
7825652770dSJohn Baldwin 	fp = job->fd_file;
783bfbbc4aaSJason Evans 
784022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
785022ca2fcSAlan Somers 	cnt = job->uiop->uio_resid;
7862244ea07SJohn Dyson 
787b1012d80SJohn Baldwin 	msgrcv_st = td->td_ru.ru_msgrcv;
788b1012d80SJohn Baldwin 	msgsnd_st = td->td_ru.ru_msgsnd;
7891c4bcd05SJeff Roberson 	inblock_st = td->td_ru.ru_inblock;
7901c4bcd05SJeff Roberson 	oublock_st = td->td_ru.ru_oublock;
791b1012d80SJohn Baldwin 
792279d7226SMatthew Dillon 	/*
793a9bf5e37SDavid Xu 	 * aio_aqueue() acquires a reference to the file that is
7949b16adc1SAlan Cox 	 * released in aio_free_entry().
795279d7226SMatthew Dillon 	 */
796022ca2fcSAlan Somers 	if (opcode == LIO_READ || opcode == LIO_READV) {
797022ca2fcSAlan Somers 		if (job->uiop->uio_resid == 0)
7985114048bSKonstantin Belousov 			error = 0;
7995114048bSKonstantin Belousov 		else
800022ca2fcSAlan Somers 			error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
801022ca2fcSAlan Somers 			    td);
8022244ea07SJohn Dyson 	} else {
8036d53aa62SDavid Xu 		if (fp->f_type == DTYPE_VNODE)
8046d53aa62SDavid Xu 			bwillwrite();
805022ca2fcSAlan Somers 		error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
8062244ea07SJohn Dyson 	}
807b1012d80SJohn Baldwin 	msgrcv_end = td->td_ru.ru_msgrcv;
808b1012d80SJohn Baldwin 	msgsnd_end = td->td_ru.ru_msgsnd;
8091c4bcd05SJeff Roberson 	inblock_end = td->td_ru.ru_inblock;
8101c4bcd05SJeff Roberson 	oublock_end = td->td_ru.ru_oublock;
811fd3bf775SJohn Dyson 
812b1012d80SJohn Baldwin 	job->msgrcv = msgrcv_end - msgrcv_st;
813b1012d80SJohn Baldwin 	job->msgsnd = msgsnd_end - msgsnd_st;
814b1012d80SJohn Baldwin 	job->inblock = inblock_end - inblock_st;
815b1012d80SJohn Baldwin 	job->outblock = oublock_end - oublock_st;
8162244ea07SJohn Dyson 
817022ca2fcSAlan Somers 	if (error != 0 && job->uiop->uio_resid != cnt) {
8182244ea07SJohn Dyson 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
8192244ea07SJohn Dyson 			error = 0;
8202247f489SAlan Somers 		if (error == EPIPE && (opcode & LIO_WRITE)) {
8215652770dSJohn Baldwin 			PROC_LOCK(job->userproc);
8225652770dSJohn Baldwin 			kern_psignal(job->userproc, SIGPIPE);
8235652770dSJohn Baldwin 			PROC_UNLOCK(job->userproc);
82419eb87d2SJohn Baldwin 		}
8252244ea07SJohn Dyson 	}
8262244ea07SJohn Dyson 
827022ca2fcSAlan Somers 	cnt -= job->uiop->uio_resid;
828f8f750c5SRobert Watson 	td->td_ucred = td_savedcred;
829f0ec1740SJohn Baldwin 	if (error)
830f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
831f0ec1740SJohn Baldwin 	else
832f0ec1740SJohn Baldwin 		aio_complete(job, cnt, 0);
8332244ea07SJohn Dyson }
8342244ea07SJohn Dyson 
83569cd28daSDoug Ambrisko static void
8365652770dSJohn Baldwin aio_process_sync(struct kaiocb *job)
837f95c13dbSGleb Smirnoff {
838f95c13dbSGleb Smirnoff 	struct thread *td = curthread;
839f95c13dbSGleb Smirnoff 	struct ucred *td_savedcred = td->td_ucred;
8405652770dSJohn Baldwin 	struct file *fp = job->fd_file;
841f95c13dbSGleb Smirnoff 	int error = 0;
842f95c13dbSGleb Smirnoff 
8432247f489SAlan Somers 	KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
8445652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
845f95c13dbSGleb Smirnoff 
8465652770dSJohn Baldwin 	td->td_ucred = job->cred;
847801ac943SThomas Munro 	if (fp->f_vnode != NULL) {
848801ac943SThomas Munro 		error = aio_fsync_vnode(td, fp->f_vnode,
849801ac943SThomas Munro 		    job->uaiocb.aio_lio_opcode);
850801ac943SThomas Munro 	}
851f95c13dbSGleb Smirnoff 	td->td_ucred = td_savedcred;
852f0ec1740SJohn Baldwin 	if (error)
853f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
854f0ec1740SJohn Baldwin 	else
855f0ec1740SJohn Baldwin 		aio_complete(job, 0, 0);
856f95c13dbSGleb Smirnoff }
857f95c13dbSGleb Smirnoff 
858f95c13dbSGleb Smirnoff static void
8595652770dSJohn Baldwin aio_process_mlock(struct kaiocb *job)
8606160e12cSGleb Smirnoff {
8615652770dSJohn Baldwin 	struct aiocb *cb = &job->uaiocb;
8626160e12cSGleb Smirnoff 	int error;
8636160e12cSGleb Smirnoff 
8645652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
8655652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
8666160e12cSGleb Smirnoff 
867f3215338SJohn Baldwin 	aio_switch_vmspace(job);
868496ab053SKonstantin Belousov 	error = kern_mlock(job->userproc, job->cred,
869496ab053SKonstantin Belousov 	    __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
870496ab053SKonstantin Belousov 	aio_complete(job, error != 0 ? -1 : 0, error);
8716160e12cSGleb Smirnoff }
8726160e12cSGleb Smirnoff 
8736160e12cSGleb Smirnoff static void
874f3215338SJohn Baldwin aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
8751ce91824SDavid Xu {
8761ce91824SDavid Xu 	struct aioliojob *lj;
87769cd28daSDoug Ambrisko 	struct kaioinfo *ki;
8785652770dSJohn Baldwin 	struct kaiocb *sjob, *sjobn;
8791ce91824SDavid Xu 	int lj_done;
880f3215338SJohn Baldwin 	bool schedule_fsync;
88169cd28daSDoug Ambrisko 
88269cd28daSDoug Ambrisko 	ki = userp->p_aioinfo;
883759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
8845652770dSJohn Baldwin 	lj = job->lio;
88569cd28daSDoug Ambrisko 	lj_done = 0;
88669cd28daSDoug Ambrisko 	if (lj) {
8871ce91824SDavid Xu 		lj->lioj_finished_count++;
8881ce91824SDavid Xu 		if (lj->lioj_count == lj->lioj_finished_count)
88969cd28daSDoug Ambrisko 			lj_done = 1;
89069cd28daSDoug Ambrisko 	}
8915652770dSJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
892f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
89327b8220dSDavid Xu 
89427b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_RUNDOWN)
89527b8220dSDavid Xu 		goto notification_done;
89627b8220dSDavid Xu 
8975652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
8985652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
8996814c2daSKonstantin Belousov 		aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
9001ce91824SDavid Xu 
9015652770dSJohn Baldwin 	KNOTE_LOCKED(&job->klist, 1);
9021ce91824SDavid Xu 
90369cd28daSDoug Ambrisko 	if (lj_done) {
9041ce91824SDavid Xu 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
90569cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
9061ce91824SDavid Xu 			KNOTE_LOCKED(&lj->klist, 1);
90769cd28daSDoug Ambrisko 		}
9081ce91824SDavid Xu 		if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
90929331656SKonstantin Belousov 		    == LIOJ_SIGNAL &&
91029331656SKonstantin Belousov 		    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
9114c0fb2cfSDavid Xu 		    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
9126814c2daSKonstantin Belousov 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
9136814c2daSKonstantin Belousov 			    true);
91469cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
91569cd28daSDoug Ambrisko 		}
91669cd28daSDoug Ambrisko 	}
91727b8220dSDavid Xu 
91827b8220dSDavid Xu notification_done:
9195652770dSJohn Baldwin 	if (job->jobflags & KAIOCB_CHECKSYNC) {
920f3215338SJohn Baldwin 		schedule_fsync = false;
9215652770dSJohn Baldwin 		TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
922b9a53e16SJohn Baldwin 			if (job->fd_file != sjob->fd_file ||
923b9a53e16SJohn Baldwin 			    job->seqno >= sjob->seqno)
924b9a53e16SJohn Baldwin 				continue;
925b9a53e16SJohn Baldwin 			if (--sjob->pending > 0)
926b9a53e16SJohn Baldwin 				continue;
927b9a53e16SJohn Baldwin 			TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
928005ce8e4SJohn Baldwin 			if (!aio_clear_cancel_function_locked(sjob))
929f3215338SJohn Baldwin 				continue;
930b9a53e16SJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
931f3215338SJohn Baldwin 			schedule_fsync = true;
93299eee864SDavid Xu 		}
933f3215338SJohn Baldwin 		if (schedule_fsync)
934f3215338SJohn Baldwin 			taskqueue_enqueue(taskqueue_aiod_kick,
935f3215338SJohn Baldwin 			    &ki->kaio_sync_task);
93699eee864SDavid Xu 	}
93727b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_WAKEUP) {
93869cd28daSDoug Ambrisko 		ki->kaio_flags &= ~KAIO_WAKEUP;
9391ce91824SDavid Xu 		wakeup(&userp->p_aioinfo);
94069cd28daSDoug Ambrisko 	}
94169cd28daSDoug Ambrisko }
94269cd28daSDoug Ambrisko 
9438a4dc40fSJohn Baldwin static void
944f3215338SJohn Baldwin aio_schedule_fsync(void *context, int pending)
945f3215338SJohn Baldwin {
946f3215338SJohn Baldwin 	struct kaioinfo *ki;
947f3215338SJohn Baldwin 	struct kaiocb *job;
948f3215338SJohn Baldwin 
949f3215338SJohn Baldwin 	ki = context;
950f3215338SJohn Baldwin 	AIO_LOCK(ki);
951f3215338SJohn Baldwin 	while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
952f3215338SJohn Baldwin 		job = TAILQ_FIRST(&ki->kaio_syncready);
953f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncready, job, list);
954f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
955f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
956f3215338SJohn Baldwin 		AIO_LOCK(ki);
957f3215338SJohn Baldwin 	}
958f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
959f3215338SJohn Baldwin }
960f3215338SJohn Baldwin 
961f3215338SJohn Baldwin bool
962f3215338SJohn Baldwin aio_cancel_cleared(struct kaiocb *job)
963f3215338SJohn Baldwin {
964f3215338SJohn Baldwin 
965f3215338SJohn Baldwin 	/*
966f3215338SJohn Baldwin 	 * The caller should hold the same queue lock held when
967f3215338SJohn Baldwin 	 * aio_clear_cancel_function() was called and set this flag
968f3215338SJohn Baldwin 	 * ensuring this check sees an up-to-date value.  However,
969f3215338SJohn Baldwin 	 * there is no way to assert that.
970f3215338SJohn Baldwin 	 */
971f3215338SJohn Baldwin 	return ((job->jobflags & KAIOCB_CLEARED) != 0);
972f3215338SJohn Baldwin }
973f3215338SJohn Baldwin 
974005ce8e4SJohn Baldwin static bool
975005ce8e4SJohn Baldwin aio_clear_cancel_function_locked(struct kaiocb *job)
976005ce8e4SJohn Baldwin {
977005ce8e4SJohn Baldwin 
978005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
979005ce8e4SJohn Baldwin 	MPASS(job->cancel_fn != NULL);
980005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLING) {
981005ce8e4SJohn Baldwin 		job->jobflags |= KAIOCB_CLEARED;
982005ce8e4SJohn Baldwin 		return (false);
983005ce8e4SJohn Baldwin 	}
984005ce8e4SJohn Baldwin 	job->cancel_fn = NULL;
985005ce8e4SJohn Baldwin 	return (true);
986005ce8e4SJohn Baldwin }
987005ce8e4SJohn Baldwin 
988f3215338SJohn Baldwin bool
989f3215338SJohn Baldwin aio_clear_cancel_function(struct kaiocb *job)
990f3215338SJohn Baldwin {
991f3215338SJohn Baldwin 	struct kaioinfo *ki;
992005ce8e4SJohn Baldwin 	bool ret;
993f3215338SJohn Baldwin 
994f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
995f3215338SJohn Baldwin 	AIO_LOCK(ki);
996005ce8e4SJohn Baldwin 	ret = aio_clear_cancel_function_locked(job);
997f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
998005ce8e4SJohn Baldwin 	return (ret);
999f3215338SJohn Baldwin }
1000005ce8e4SJohn Baldwin 
1001005ce8e4SJohn Baldwin static bool
1002005ce8e4SJohn Baldwin aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1003005ce8e4SJohn Baldwin {
1004005ce8e4SJohn Baldwin 
1005005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1006005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLED)
1007005ce8e4SJohn Baldwin 		return (false);
1008005ce8e4SJohn Baldwin 	job->cancel_fn = func;
1009f3215338SJohn Baldwin 	return (true);
1010f3215338SJohn Baldwin }
1011f3215338SJohn Baldwin 
1012f3215338SJohn Baldwin bool
1013f3215338SJohn Baldwin aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1014f3215338SJohn Baldwin {
1015f3215338SJohn Baldwin 	struct kaioinfo *ki;
1016005ce8e4SJohn Baldwin 	bool ret;
1017f3215338SJohn Baldwin 
1018f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1019f3215338SJohn Baldwin 	AIO_LOCK(ki);
1020005ce8e4SJohn Baldwin 	ret = aio_set_cancel_function_locked(job, func);
1021f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1022005ce8e4SJohn Baldwin 	return (ret);
1023f3215338SJohn Baldwin }
1024f3215338SJohn Baldwin 
1025f3215338SJohn Baldwin void
1026f3215338SJohn Baldwin aio_complete(struct kaiocb *job, long status, int error)
1027f3215338SJohn Baldwin {
1028f3215338SJohn Baldwin 	struct kaioinfo *ki;
1029f3215338SJohn Baldwin 	struct proc *userp;
1030f3215338SJohn Baldwin 
1031f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.error = error;
1032f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.status = status;
1033f3215338SJohn Baldwin 
1034f3215338SJohn Baldwin 	userp = job->userproc;
1035f3215338SJohn Baldwin 	ki = userp->p_aioinfo;
1036f3215338SJohn Baldwin 
1037f3215338SJohn Baldwin 	AIO_LOCK(ki);
1038f3215338SJohn Baldwin 	KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1039f3215338SJohn Baldwin 	    ("duplicate aio_complete"));
1040f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_FINISHED;
1041f3215338SJohn Baldwin 	if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1042f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1043f3215338SJohn Baldwin 		aio_bio_done_notify(userp, job);
1044f3215338SJohn Baldwin 	}
1045f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1046f3215338SJohn Baldwin }
1047f3215338SJohn Baldwin 
1048f3215338SJohn Baldwin void
1049f3215338SJohn Baldwin aio_cancel(struct kaiocb *job)
1050f3215338SJohn Baldwin {
1051f3215338SJohn Baldwin 
1052f3215338SJohn Baldwin 	aio_complete(job, -1, ECANCELED);
1053f3215338SJohn Baldwin }
1054f3215338SJohn Baldwin 
1055f3215338SJohn Baldwin void
10565652770dSJohn Baldwin aio_switch_vmspace(struct kaiocb *job)
10578a4dc40fSJohn Baldwin {
10588a4dc40fSJohn Baldwin 
10595652770dSJohn Baldwin 	vmspace_switch_aio(job->userproc->p_vmspace);
10608a4dc40fSJohn Baldwin }
10618a4dc40fSJohn Baldwin 
10622244ea07SJohn Dyson /*
1063f95c13dbSGleb Smirnoff  * The AIO daemon, most of the actual work is done in aio_process_*,
106484af4da6SJohn Dyson  * but the setup (and address space mgmt) is done in this routine.
10652244ea07SJohn Dyson  */
10662244ea07SJohn Dyson static void
10671ce91824SDavid Xu aio_daemon(void *_id)
10682244ea07SJohn Dyson {
10695652770dSJohn Baldwin 	struct kaiocb *job;
107039314b7dSJohn Baldwin 	struct aioproc *aiop;
1071bfbbc4aaSJason Evans 	struct kaioinfo *ki;
1072f3215338SJohn Baldwin 	struct proc *p;
10738a4dc40fSJohn Baldwin 	struct vmspace *myvm;
1074b40ce416SJulian Elischer 	struct thread *td = curthread;
10751ce91824SDavid Xu 	int id = (intptr_t)_id;
10762244ea07SJohn Dyson 
10772244ea07SJohn Dyson 	/*
10788a4dc40fSJohn Baldwin 	 * Grab an extra reference on the daemon's vmspace so that it
10798a4dc40fSJohn Baldwin 	 * doesn't get freed by jobs that switch to a different
10808a4dc40fSJohn Baldwin 	 * vmspace.
10812244ea07SJohn Dyson 	 */
10828a4dc40fSJohn Baldwin 	p = td->td_proc;
10838a4dc40fSJohn Baldwin 	myvm = vmspace_acquire_ref(p);
1084fd3bf775SJohn Dyson 
10858a4dc40fSJohn Baldwin 	KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1086fd3bf775SJohn Dyson 
1087fd3bf775SJohn Dyson 	/*
1088bfbbc4aaSJason Evans 	 * Allocate and ready the aio control info.  There is one aiop structure
1089bfbbc4aaSJason Evans 	 * per daemon.
1090fd3bf775SJohn Dyson 	 */
1091a163d034SWarner Losh 	aiop = uma_zalloc(aiop_zone, M_WAITOK);
109239314b7dSJohn Baldwin 	aiop->aioproc = p;
109339314b7dSJohn Baldwin 	aiop->aioprocflags = 0;
1094bfbbc4aaSJason Evans 
1095fd3bf775SJohn Dyson 	/*
1096fd3bf775SJohn Dyson 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1097b40ce416SJulian Elischer 	 * and creating too many daemons.)
1098fd3bf775SJohn Dyson 	 */
10991ce91824SDavid Xu 	sema_post(&aio_newproc_sem);
11002244ea07SJohn Dyson 
11011ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
1102bfbbc4aaSJason Evans 	for (;;) {
1103fd3bf775SJohn Dyson 		/*
1104fd3bf775SJohn Dyson 		 * Take daemon off of free queue
1105fd3bf775SJohn Dyson 		 */
110639314b7dSJohn Baldwin 		if (aiop->aioprocflags & AIOP_FREE) {
11072244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
110839314b7dSJohn Baldwin 			aiop->aioprocflags &= ~AIOP_FREE;
11092244ea07SJohn Dyson 		}
11102244ea07SJohn Dyson 
1111fd3bf775SJohn Dyson 		/*
1112bfbbc4aaSJason Evans 		 * Check for jobs.
1113fd3bf775SJohn Dyson 		 */
11145652770dSJohn Baldwin 		while ((job = aio_selectjob(aiop)) != NULL) {
11151ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11162244ea07SJohn Dyson 
1117f3215338SJohn Baldwin 			ki = job->userproc->p_aioinfo;
1118f3215338SJohn Baldwin 			job->handle_fn(job);
111984af4da6SJohn Dyson 
11209b84335cSDavid Xu 			mtx_lock(&aio_job_mtx);
11219b84335cSDavid Xu 			/* Decrement the active job count. */
11229b84335cSDavid Xu 			ki->kaio_active_count--;
11232244ea07SJohn Dyson 		}
11242244ea07SJohn Dyson 
1125fd3bf775SJohn Dyson 		/*
1126bfbbc4aaSJason Evans 		 * Disconnect from user address space.
1127fd3bf775SJohn Dyson 		 */
11288a4dc40fSJohn Baldwin 		if (p->p_vmspace != myvm) {
11291ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11308a4dc40fSJohn Baldwin 			vmspace_switch_aio(myvm);
11311ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
11321ce91824SDavid Xu 			/*
11331ce91824SDavid Xu 			 * We have to restart to avoid race, we only sleep if
11348a4dc40fSJohn Baldwin 			 * no job can be selected.
11351ce91824SDavid Xu 			 */
11361ce91824SDavid Xu 			continue;
1137fd3bf775SJohn Dyson 		}
1138fd3bf775SJohn Dyson 
11391ce91824SDavid Xu 		mtx_assert(&aio_job_mtx, MA_OWNED);
11401ce91824SDavid Xu 
1141fd3bf775SJohn Dyson 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
114239314b7dSJohn Baldwin 		aiop->aioprocflags |= AIOP_FREE;
1143fd3bf775SJohn Dyson 
1144fd3bf775SJohn Dyson 		/*
1145bfbbc4aaSJason Evans 		 * If daemon is inactive for a long time, allow it to exit,
1146bfbbc4aaSJason Evans 		 * thereby freeing resources.
1147fd3bf775SJohn Dyson 		 */
114839314b7dSJohn Baldwin 		if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
11498a4dc40fSJohn Baldwin 		    aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
115039314b7dSJohn Baldwin 		    (aiop->aioprocflags & AIOP_FREE) &&
11518a4dc40fSJohn Baldwin 		    num_aio_procs > target_aio_procs)
11528a4dc40fSJohn Baldwin 			break;
11538a4dc40fSJohn Baldwin 	}
1154fd3bf775SJohn Dyson 	TAILQ_REMOVE(&aio_freeproc, aiop, list);
115584af4da6SJohn Dyson 	num_aio_procs--;
11561ce91824SDavid Xu 	mtx_unlock(&aio_job_mtx);
11571ce91824SDavid Xu 	uma_zfree(aiop_zone, aiop);
11581ce91824SDavid Xu 	free_unr(aiod_unr, id);
11598a4dc40fSJohn Baldwin 	vmspace_free(myvm);
11608a4dc40fSJohn Baldwin 
11618a4dc40fSJohn Baldwin 	KASSERT(p->p_vmspace == myvm,
11628a4dc40fSJohn Baldwin 	    ("AIOD: bad vmspace for exiting daemon"));
1163f7db0c95SMark Johnston 	KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1164f7db0c95SMark Johnston 	    ("AIOD: bad vm refcnt for exiting daemon: %d",
1165f7db0c95SMark Johnston 	    refcount_load(&myvm->vm_refcnt)));
11663745c395SJulian Elischer 	kproc_exit(0);
1167fd3bf775SJohn Dyson }
11682244ea07SJohn Dyson 
11692244ea07SJohn Dyson /*
1170bfbbc4aaSJason Evans  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1171bfbbc4aaSJason Evans  * AIO daemon modifies its environment itself.
11722244ea07SJohn Dyson  */
11732244ea07SJohn Dyson static int
11741ce91824SDavid Xu aio_newproc(int *start)
1175fd3bf775SJohn Dyson {
11762244ea07SJohn Dyson 	int error;
1177c9a970a7SAlan Cox 	struct proc *p;
11781ce91824SDavid Xu 	int id;
11792244ea07SJohn Dyson 
11801ce91824SDavid Xu 	id = alloc_unr(aiod_unr);
11813745c395SJulian Elischer 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
11821ce91824SDavid Xu 		RFNOWAIT, 0, "aiod%d", id);
11831ce91824SDavid Xu 	if (error == 0) {
1184fd3bf775SJohn Dyson 		/*
11851ce91824SDavid Xu 		 * Wait until daemon is started.
1186fd3bf775SJohn Dyson 		 */
11871ce91824SDavid Xu 		sema_wait(&aio_newproc_sem);
11881ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
118984af4da6SJohn Dyson 		num_aio_procs++;
11901ce91824SDavid Xu 		if (start != NULL)
11917f34b521SDavid Xu 			(*start)--;
11921ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
11931ce91824SDavid Xu 	} else {
11941ce91824SDavid Xu 		free_unr(aiod_unr, id);
11951ce91824SDavid Xu 	}
1196ac41f2efSAlfred Perlstein 	return (error);
11972244ea07SJohn Dyson }
11982244ea07SJohn Dyson 
11992244ea07SJohn Dyson /*
120072bce9ffSAlan Somers  * Try the high-performance, low-overhead bio method for eligible
120188ed460eSAlan Cox  * VCHR devices.  This method doesn't use an aio helper thread, and
120288ed460eSAlan Cox  * thus has very low overhead.
120388ed460eSAlan Cox  *
1204a9bf5e37SDavid Xu  * Assumes that the caller, aio_aqueue(), has incremented the file
120588ed460eSAlan Cox  * structure's reference count, preventing its deallocation for the
120688ed460eSAlan Cox  * duration of this call.
1207fd3bf775SJohn Dyson  */
120888ed460eSAlan Cox static int
120972bce9ffSAlan Somers aio_qbio(struct proc *p, struct kaiocb *job)
1210fd3bf775SJohn Dyson {
1211fd3bf775SJohn Dyson 	struct aiocb *cb;
1212fd3bf775SJohn Dyson 	struct file *fp;
1213f743d981SAlexander Motin 	struct buf *pbuf;
1214fd3bf775SJohn Dyson 	struct vnode *vp;
1215f3215a60SKonstantin Belousov 	struct cdevsw *csw;
1216f3215a60SKonstantin Belousov 	struct cdev *dev;
1217fd3bf775SJohn Dyson 	struct kaioinfo *ki;
1218022ca2fcSAlan Somers 	struct bio **bios = NULL;
1219022ca2fcSAlan Somers 	off_t offset;
1220022ca2fcSAlan Somers 	int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1221f743d981SAlexander Motin 	vm_prot_t prot;
1222022ca2fcSAlan Somers 	bool use_unmapped;
1223fd3bf775SJohn Dyson 
12245652770dSJohn Baldwin 	cb = &job->uaiocb;
12255652770dSJohn Baldwin 	fp = job->fd_file;
1226022ca2fcSAlan Somers 	opcode = cb->aio_lio_opcode;
1227fd3bf775SJohn Dyson 
1228022ca2fcSAlan Somers 	if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1229022ca2fcSAlan Somers 	    opcode == LIO_READ || opcode == LIO_READV))
1230f54c5606SJohn Baldwin 		return (-1);
12316160e12cSGleb Smirnoff 	if (fp == NULL || fp->f_type != DTYPE_VNODE)
1232008626c3SPoul-Henning Kamp 		return (-1);
1233fd3bf775SJohn Dyson 
12343b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
1235f743d981SAlexander Motin 	if (vp->v_type != VCHR)
1236f582ac06SBrian Feldman 		return (-1);
1237ad8de0f2SDavid Xu 	if (vp->v_bufobj.bo_bsize == 0)
1238ad8de0f2SDavid Xu 		return (-1);
1239022ca2fcSAlan Somers 
12402247f489SAlan Somers 	bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1241022ca2fcSAlan Somers 	iovcnt = job->uiop->uio_iovcnt;
1242022ca2fcSAlan Somers 	if (iovcnt > max_buf_aio)
1243008626c3SPoul-Henning Kamp 		return (-1);
1244022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1245022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1246022ca2fcSAlan Somers 			return (-1);
1247022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len > maxphys) {
1248022ca2fcSAlan Somers 			error = -1;
1249022ca2fcSAlan Somers 			return (-1);
1250022ca2fcSAlan Somers 		}
1251022ca2fcSAlan Somers 	}
1252022ca2fcSAlan Somers 	offset = cb->aio_offset;
1253fd3bf775SJohn Dyson 
1254f3215a60SKonstantin Belousov 	ref = 0;
1255f3215a60SKonstantin Belousov 	csw = devvn_refthread(vp, &dev, &ref);
1256f3215a60SKonstantin Belousov 	if (csw == NULL)
1257f3215a60SKonstantin Belousov 		return (ENXIO);
1258f743d981SAlexander Motin 
1259f743d981SAlexander Motin 	if ((csw->d_flags & D_DISK) == 0) {
1260f743d981SAlexander Motin 		error = -1;
1261f743d981SAlexander Motin 		goto unref;
1262f743d981SAlexander Motin 	}
1263022ca2fcSAlan Somers 	if (job->uiop->uio_resid > dev->si_iosize_max) {
1264f3215a60SKonstantin Belousov 		error = -1;
1265f3215a60SKonstantin Belousov 		goto unref;
1266f3215a60SKonstantin Belousov 	}
1267f3215a60SKonstantin Belousov 
1268f743d981SAlexander Motin 	ki = p->p_aioinfo;
1269022ca2fcSAlan Somers 	job->error = 0;
12704d805eacSJohn Baldwin 
1271022ca2fcSAlan Somers 	use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1272022ca2fcSAlan Somers 	if (!use_unmapped) {
1273022ca2fcSAlan Somers 		AIO_LOCK(ki);
1274022ca2fcSAlan Somers 		if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1275022ca2fcSAlan Somers 			AIO_UNLOCK(ki);
1276f54c5606SJohn Baldwin 			error = EAGAIN;
1277f743d981SAlexander Motin 			goto unref;
1278f743d981SAlexander Motin 		}
1279022ca2fcSAlan Somers 		ki->kaio_buffer_count += iovcnt;
1280022ca2fcSAlan Somers 		AIO_UNLOCK(ki);
1281022ca2fcSAlan Somers 	}
12824d805eacSJohn Baldwin 
1283022ca2fcSAlan Somers 	bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1284022ca2fcSAlan Somers 	atomic_store_int(&job->nbio, iovcnt);
1285022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1286022ca2fcSAlan Somers 		struct vm_page** pages;
1287022ca2fcSAlan Somers 		struct bio *bp;
1288022ca2fcSAlan Somers 		void *buf;
1289022ca2fcSAlan Somers 		size_t nbytes;
1290022ca2fcSAlan Somers 		int npages;
1291022ca2fcSAlan Somers 
1292022ca2fcSAlan Somers 		buf = job->uiop->uio_iov[i].iov_base;
1293022ca2fcSAlan Somers 		nbytes = job->uiop->uio_iov[i].iov_len;
1294022ca2fcSAlan Somers 
1295022ca2fcSAlan Somers 		bios[i] = g_alloc_bio();
1296022ca2fcSAlan Somers 		bp = bios[i];
1297022ca2fcSAlan Somers 
1298022ca2fcSAlan Somers 		poff = (vm_offset_t)buf & PAGE_MASK;
1299022ca2fcSAlan Somers 		if (use_unmapped) {
1300022ca2fcSAlan Somers 			pbuf = NULL;
1301022ca2fcSAlan Somers 			pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1302022ca2fcSAlan Somers 			    nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1303022ca2fcSAlan Somers 		} else {
130401206038SAlan Somers 			pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1305f743d981SAlexander Motin 			BUF_KERNPROC(pbuf);
130601206038SAlan Somers 			pages = pbuf->b_pages;
13074d805eacSJohn Baldwin 		}
13081ce91824SDavid Xu 
1309022ca2fcSAlan Somers 		bp->bio_length = nbytes;
1310022ca2fcSAlan Somers 		bp->bio_bcount = nbytes;
131172bce9ffSAlan Somers 		bp->bio_done = aio_biowakeup;
1312022ca2fcSAlan Somers 		bp->bio_offset = offset;
1313022ca2fcSAlan Somers 		bp->bio_cmd = bio_cmd;
1314f743d981SAlexander Motin 		bp->bio_dev = dev;
131501206038SAlan Somers 		bp->bio_caller1 = job;
131601206038SAlan Somers 		bp->bio_caller2 = pbuf;
1317f743d981SAlexander Motin 
1318f743d981SAlexander Motin 		prot = VM_PROT_READ;
1319022ca2fcSAlan Somers 		if (opcode == LIO_READ || opcode == LIO_READV)
1320f743d981SAlexander Motin 			prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
132101206038SAlan Somers 		npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1322022ca2fcSAlan Somers 		    (vm_offset_t)buf, bp->bio_length, prot, pages,
1323cd853791SKonstantin Belousov 		    atop(maxphys) + 1);
132401206038SAlan Somers 		if (npages < 0) {
1325022ca2fcSAlan Somers 			if (pbuf != NULL)
1326022ca2fcSAlan Somers 				uma_zfree(pbuf_zone, pbuf);
1327022ca2fcSAlan Somers 			else
1328022ca2fcSAlan Somers 				free(pages, M_TEMP);
1329f743d981SAlexander Motin 			error = EFAULT;
1330022ca2fcSAlan Somers 			g_destroy_bio(bp);
1331022ca2fcSAlan Somers 			i--;
1332022ca2fcSAlan Somers 			goto destroy_bios;
1333f743d981SAlexander Motin 		}
13344d805eacSJohn Baldwin 		if (pbuf != NULL) {
133501206038SAlan Somers 			pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1336f743d981SAlexander Motin 			bp->bio_data = pbuf->b_data + poff;
133701206038SAlan Somers 			pbuf->b_npages = npages;
1338022ca2fcSAlan Somers 			atomic_add_int(&num_buf_aio, 1);
1339f743d981SAlexander Motin 		} else {
134001206038SAlan Somers 			bp->bio_ma = pages;
134101206038SAlan Somers 			bp->bio_ma_n = npages;
1342f743d981SAlexander Motin 			bp->bio_ma_offset = poff;
1343f743d981SAlexander Motin 			bp->bio_data = unmapped_buf;
1344f743d981SAlexander Motin 			bp->bio_flags |= BIO_UNMAPPED;
13458091e52bSJohn Baldwin 			atomic_add_int(&num_unmapped_aio, 1);
1346f743d981SAlexander Motin 		}
1347f743d981SAlexander Motin 
1348022ca2fcSAlan Somers 		offset += nbytes;
1349022ca2fcSAlan Somers 	}
1350022ca2fcSAlan Somers 
1351bfbbc4aaSJason Evans 	/* Perform transfer. */
1352022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++)
1353022ca2fcSAlan Somers 		csw->d_strategy(bios[i]);
1354022ca2fcSAlan Somers 	free(bios, M_TEMP);
1355022ca2fcSAlan Somers 
1356f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1357ac41f2efSAlfred Perlstein 	return (0);
1358fd3bf775SJohn Dyson 
1359022ca2fcSAlan Somers destroy_bios:
1360022ca2fcSAlan Somers 	for (; i >= 0; i--)
1361022ca2fcSAlan Somers 		aio_biocleanup(bios[i]);
1362022ca2fcSAlan Somers 	free(bios, M_TEMP);
1363f3215a60SKonstantin Belousov unref:
1364f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1365fd3bf775SJohn Dyson 	return (error);
1366fd3bf775SJohn Dyson }
1367fd3bf775SJohn Dyson 
1368399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
13693858a1f4SJohn Baldwin static int
13703858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
13713858a1f4SJohn Baldwin {
13723858a1f4SJohn Baldwin 
13733858a1f4SJohn Baldwin 	/*
13743858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
13753858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
13763858a1f4SJohn Baldwin 	 */
13773858a1f4SJohn Baldwin 	nsig->sigev_notify = osig->sigev_notify;
13783858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
13793858a1f4SJohn Baldwin 	case SIGEV_NONE:
13803858a1f4SJohn Baldwin 		break;
13813858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
13823858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
13833858a1f4SJohn Baldwin 		break;
13843858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
13853858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
13863858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
13873858a1f4SJohn Baldwin 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
13883858a1f4SJohn Baldwin 		break;
13893858a1f4SJohn Baldwin 	default:
13903858a1f4SJohn Baldwin 		return (EINVAL);
13913858a1f4SJohn Baldwin 	}
13923858a1f4SJohn Baldwin 	return (0);
13933858a1f4SJohn Baldwin }
13943858a1f4SJohn Baldwin 
13953858a1f4SJohn Baldwin static int
1396022ca2fcSAlan Somers aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1397022ca2fcSAlan Somers     int type __unused)
13983858a1f4SJohn Baldwin {
13993858a1f4SJohn Baldwin 	struct oaiocb *ojob;
1400022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
14013858a1f4SJohn Baldwin 	int error;
14023858a1f4SJohn Baldwin 
1403022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
1404022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct oaiocb));
14053858a1f4SJohn Baldwin 	if (error)
14063858a1f4SJohn Baldwin 		return (error);
1407022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1408022ca2fcSAlan Somers 	ojob = (struct oaiocb *)kcb;
1409022ca2fcSAlan Somers 	return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
14103858a1f4SJohn Baldwin }
1411399e8c17SJohn Baldwin #endif
14123858a1f4SJohn Baldwin 
14133858a1f4SJohn Baldwin static int
1414022ca2fcSAlan Somers aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
14153858a1f4SJohn Baldwin {
1416022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
1417022ca2fcSAlan Somers 	int error;
14183858a1f4SJohn Baldwin 
1419022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct aiocb));
1420022ca2fcSAlan Somers 	if (error)
1421022ca2fcSAlan Somers 		return (error);
1422*f30a1ae8SThomas Munro 	if (type == LIO_NOP)
1423*f30a1ae8SThomas Munro 		type = kcb->aio_lio_opcode;
14242247f489SAlan Somers 	if (type & LIO_VECTORED) {
1425022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
1426022ca2fcSAlan Somers 		error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1427022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
1428022ca2fcSAlan Somers 	}
1429022ca2fcSAlan Somers 
1430022ca2fcSAlan Somers 	return (error);
14313858a1f4SJohn Baldwin }
14323858a1f4SJohn Baldwin 
14333858a1f4SJohn Baldwin static long
14343858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob)
14353858a1f4SJohn Baldwin {
14363858a1f4SJohn Baldwin 
14373858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.status));
14383858a1f4SJohn Baldwin }
14393858a1f4SJohn Baldwin 
14403858a1f4SJohn Baldwin static long
14413858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob)
14423858a1f4SJohn Baldwin {
14433858a1f4SJohn Baldwin 
14443858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.error));
14453858a1f4SJohn Baldwin }
14463858a1f4SJohn Baldwin 
14473858a1f4SJohn Baldwin static int
14483858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status)
14493858a1f4SJohn Baldwin {
14503858a1f4SJohn Baldwin 
14513858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.status, status));
14523858a1f4SJohn Baldwin }
14533858a1f4SJohn Baldwin 
14543858a1f4SJohn Baldwin static int
14553858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error)
14563858a1f4SJohn Baldwin {
14573858a1f4SJohn Baldwin 
14583858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.error, error));
14593858a1f4SJohn Baldwin }
14603858a1f4SJohn Baldwin 
14613858a1f4SJohn Baldwin static int
14623858a1f4SJohn Baldwin aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
14633858a1f4SJohn Baldwin {
14643858a1f4SJohn Baldwin 
14653858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
14663858a1f4SJohn Baldwin }
14673858a1f4SJohn Baldwin 
14683858a1f4SJohn Baldwin static int
14693858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
14703858a1f4SJohn Baldwin {
14713858a1f4SJohn Baldwin 
14723858a1f4SJohn Baldwin 	return (suword(ujobp, (long)ujob));
14733858a1f4SJohn Baldwin }
14743858a1f4SJohn Baldwin 
14753858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = {
1476849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin,
14773858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14783858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14793858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14803858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14813858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14823858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14833858a1f4SJohn Baldwin };
14843858a1f4SJohn Baldwin 
1485399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
14863858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = {
1487849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin_old_sigevent,
14883858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14893858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14903858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14913858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14923858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14933858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14943858a1f4SJohn Baldwin };
1495399e8c17SJohn Baldwin #endif
14963858a1f4SJohn Baldwin 
1497bfbbc4aaSJason Evans /*
149872bce9ffSAlan Somers  * Queue a new AIO request.  Choosing either the threaded or direct bio VCHR
1499bfbbc4aaSJason Evans  * technique is done in this code.
15002244ea07SJohn Dyson  */
15016a1162d4SAlexander Leidinger int
15025652770dSJohn Baldwin aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
15033858a1f4SJohn Baldwin     int type, struct aiocb_ops *ops)
1504fd3bf775SJohn Dyson {
1505b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1506022ca2fcSAlan Somers 	struct file *fp = NULL;
1507f3215338SJohn Baldwin 	struct kaiocb *job;
15082244ea07SJohn Dyson 	struct kaioinfo *ki;
1509c6fa9f78SAlan Cox 	struct kevent kev;
15101ce91824SDavid Xu 	int opcode;
15111ce91824SDavid Xu 	int error;
15124db71d27SJohn-Mark Gurney 	int fd, kqfd;
15131ce91824SDavid Xu 	int jid;
1514fde80935SDavid Xu 	u_short evflags;
15152244ea07SJohn Dyson 
1516a9bf5e37SDavid Xu 	if (p->p_aioinfo == NULL)
1517a9bf5e37SDavid Xu 		aio_init_aioinfo(p);
1518a9bf5e37SDavid Xu 
15191ce91824SDavid Xu 	ki = p->p_aioinfo;
15201ce91824SDavid Xu 
15215652770dSJohn Baldwin 	ops->store_status(ujob, -1);
15225652770dSJohn Baldwin 	ops->store_error(ujob, 0);
15235652770dSJohn Baldwin 	ops->store_kernelinfo(ujob, -1);
1524a9bf5e37SDavid Xu 
1525a9bf5e37SDavid Xu 	if (num_queue_count >= max_queue_count ||
152686bbef43SJohn Baldwin 	    ki->kaio_count >= max_aio_queue_per_proc) {
1527022ca2fcSAlan Somers 		error = EAGAIN;
1528022ca2fcSAlan Somers 		goto err1;
1529a9bf5e37SDavid Xu 	}
1530a9bf5e37SDavid Xu 
15315652770dSJohn Baldwin 	job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
15325652770dSJohn Baldwin 	knlist_init_mtx(&job->klist, AIO_MTX(ki));
1533fd3bf775SJohn Dyson 
1534022ca2fcSAlan Somers 	error = ops->aio_copyin(ujob, job, type);
1535022ca2fcSAlan Somers 	if (error)
1536022ca2fcSAlan Somers 		goto err2;
153768d71118SDavid Xu 
1538bb430bc7SJohn Baldwin 	if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1539022ca2fcSAlan Somers 		error = EINVAL;
1540022ca2fcSAlan Somers 		goto err2;
1541434ea137SGleb Smirnoff 	}
1542434ea137SGleb Smirnoff 
15435652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
15445652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
15455652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
15465652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1547022ca2fcSAlan Somers 		error = EINVAL;
1548022ca2fcSAlan Somers 		goto err2;
154968d71118SDavid Xu 	}
155068d71118SDavid Xu 
15515652770dSJohn Baldwin 	if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
15525652770dSJohn Baldwin 	     job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
15535652770dSJohn Baldwin 		!_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1554022ca2fcSAlan Somers 		error = EINVAL;
1555022ca2fcSAlan Somers 		goto err2;
15562f3cf918SAlfred Perlstein 	}
15572244ea07SJohn Dyson 
1558ff1a3078SAlan Somers 	/* Get the opcode. */
1559ff1a3078SAlan Somers 	if (type == LIO_NOP) {
1560ff1a3078SAlan Somers 		switch (job->uaiocb.aio_lio_opcode) {
1561ff1a3078SAlan Somers 		case LIO_WRITE:
1562*f30a1ae8SThomas Munro 		case LIO_WRITEV:
1563ff1a3078SAlan Somers 		case LIO_NOP:
1564ff1a3078SAlan Somers 		case LIO_READ:
1565*f30a1ae8SThomas Munro 		case LIO_READV:
1566ff1a3078SAlan Somers 			opcode = job->uaiocb.aio_lio_opcode;
1567ff1a3078SAlan Somers 			break;
1568ff1a3078SAlan Somers 		default:
1569ff1a3078SAlan Somers 			error = EINVAL;
1570ff1a3078SAlan Somers 			goto err2;
1571ff1a3078SAlan Somers 		}
1572ff1a3078SAlan Somers 	} else
1573ff1a3078SAlan Somers 		opcode = job->uaiocb.aio_lio_opcode = type;
1574ff1a3078SAlan Somers 
15755652770dSJohn Baldwin 	ksiginfo_init(&job->ksi);
15764c0fb2cfSDavid Xu 
1577bfbbc4aaSJason Evans 	/* Save userspace address of the job info. */
15785652770dSJohn Baldwin 	job->ujob = ujob;
157911783b14SJohn Dyson 
1580a9d2f8d8SRobert Watson 	/*
1581a9d2f8d8SRobert Watson 	 * Validate the opcode and fetch the file object for the specified
1582a9d2f8d8SRobert Watson 	 * file descriptor.
1583a9d2f8d8SRobert Watson 	 *
1584a9d2f8d8SRobert Watson 	 * XXXRW: Moved the opcode validation up here so that we don't
1585a9d2f8d8SRobert Watson 	 * retrieve a file descriptor without knowing what the capabiltity
1586a9d2f8d8SRobert Watson 	 * should be.
1587a9d2f8d8SRobert Watson 	 */
15885652770dSJohn Baldwin 	fd = job->uaiocb.aio_fildes;
15892a522eb9SJohn Baldwin 	switch (opcode) {
15902a522eb9SJohn Baldwin 	case LIO_WRITE:
1591022ca2fcSAlan Somers 	case LIO_WRITEV:
1592cbd92ce6SMatt Macy 		error = fget_write(td, fd, &cap_pwrite_rights, &fp);
15932a522eb9SJohn Baldwin 		break;
15942a522eb9SJohn Baldwin 	case LIO_READ:
1595022ca2fcSAlan Somers 	case LIO_READV:
1596cbd92ce6SMatt Macy 		error = fget_read(td, fd, &cap_pread_rights, &fp);
1597a9d2f8d8SRobert Watson 		break;
1598a9d2f8d8SRobert Watson 	case LIO_SYNC:
1599801ac943SThomas Munro 	case LIO_DSYNC:
1600cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_fsync_rights, &fp);
1601a9d2f8d8SRobert Watson 		break;
16026160e12cSGleb Smirnoff 	case LIO_MLOCK:
16036160e12cSGleb Smirnoff 		break;
1604a9d2f8d8SRobert Watson 	case LIO_NOP:
1605cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_no_rights, &fp);
16062a522eb9SJohn Baldwin 		break;
16072a522eb9SJohn Baldwin 	default:
1608a9d2f8d8SRobert Watson 		error = EINVAL;
16092a522eb9SJohn Baldwin 	}
1610022ca2fcSAlan Somers 	if (error)
1611022ca2fcSAlan Somers 		goto err3;
161299eee864SDavid Xu 
16132247f489SAlan Somers 	if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
161499eee864SDavid Xu 		error = EINVAL;
1615022ca2fcSAlan Somers 		goto err3;
161699eee864SDavid Xu 	}
16172244ea07SJohn Dyson 
1618022ca2fcSAlan Somers 	if ((opcode == LIO_READ || opcode == LIO_READV ||
1619022ca2fcSAlan Somers 	    opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1620711dba24SKonstantin Belousov 	    job->uaiocb.aio_offset < 0 &&
1621711dba24SKonstantin Belousov 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1622ae124fc4SAlan Cox 		error = EINVAL;
1623022ca2fcSAlan Somers 		goto err3;
16242244ea07SJohn Dyson 	}
16251ce91824SDavid Xu 
16268d9ed174SKonstantin Belousov 	if (fp != NULL && fp->f_ops == &path_fileops) {
16278d9ed174SKonstantin Belousov 		error = EBADF;
16288d9ed174SKonstantin Belousov 		goto err3;
16298d9ed174SKonstantin Belousov 	}
16308d9ed174SKonstantin Belousov 
16315652770dSJohn Baldwin 	job->fd_file = fp;
16321ce91824SDavid Xu 
163399eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
163499eee864SDavid Xu 	jid = jobrefid++;
16355652770dSJohn Baldwin 	job->seqno = jobseqno++;
163699eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
16375652770dSJohn Baldwin 	error = ops->store_kernelinfo(ujob, jid);
16381ce91824SDavid Xu 	if (error) {
16391ce91824SDavid Xu 		error = EINVAL;
1640022ca2fcSAlan Somers 		goto err3;
16411ce91824SDavid Xu 	}
16425652770dSJohn Baldwin 	job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
16432244ea07SJohn Dyson 
16442244ea07SJohn Dyson 	if (opcode == LIO_NOP) {
1645a5c0b1c0SAlan Cox 		fdrop(fp, td);
1646022ca2fcSAlan Somers 		MPASS(job->uiop == &job->uio || job->uiop == NULL);
16475652770dSJohn Baldwin 		uma_zfree(aiocb_zone, job);
1648ac41f2efSAlfred Perlstein 		return (0);
16492244ea07SJohn Dyson 	}
16502244ea07SJohn Dyson 
16515652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1652cb679c38SJonathan Lemon 		goto no_kqueue;
16535652770dSJohn Baldwin 	evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1654fde80935SDavid Xu 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1655fde80935SDavid Xu 		error = EINVAL;
1656022ca2fcSAlan Somers 		goto err3;
1657fde80935SDavid Xu 	}
16585652770dSJohn Baldwin 	kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
165936c4960eSMark Johnston 	memset(&kev, 0, sizeof(kev));
16605652770dSJohn Baldwin 	kev.ident = (uintptr_t)job->ujob;
1661cb679c38SJonathan Lemon 	kev.filter = EVFILT_AIO;
1662fde80935SDavid Xu 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
16635652770dSJohn Baldwin 	kev.data = (intptr_t)job;
16645652770dSJohn Baldwin 	kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1665792843c3SMark Johnston 	error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1666f3215338SJohn Baldwin 	if (error)
1667022ca2fcSAlan Somers 		goto err3;
1668f3215338SJohn Baldwin 
1669cb679c38SJonathan Lemon no_kqueue:
1670cb679c38SJonathan Lemon 
16715652770dSJohn Baldwin 	ops->store_error(ujob, EINPROGRESS);
16725652770dSJohn Baldwin 	job->uaiocb._aiocb_private.error = EINPROGRESS;
16735652770dSJohn Baldwin 	job->userproc = p;
16745652770dSJohn Baldwin 	job->cred = crhold(td->td_ucred);
1675f3215338SJohn Baldwin 	job->jobflags = KAIOCB_QUEUEING;
16765652770dSJohn Baldwin 	job->lio = lj;
16772244ea07SJohn Dyson 
16782247f489SAlan Somers 	if (opcode & LIO_VECTORED) {
1679022ca2fcSAlan Somers 		/* Use the uio copied in by aio_copyin */
1680022ca2fcSAlan Somers 		MPASS(job->uiop != &job->uio && job->uiop != NULL);
16812247f489SAlan Somers 	} else {
1682022ca2fcSAlan Somers 		/* Setup the inline uio */
1683022ca2fcSAlan Somers 		job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1684022ca2fcSAlan Somers 		job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1685022ca2fcSAlan Somers 		job->uio.uio_iov = job->iov;
1686022ca2fcSAlan Somers 		job->uio.uio_iovcnt = 1;
1687022ca2fcSAlan Somers 		job->uio.uio_resid = job->uaiocb.aio_nbytes;
1688022ca2fcSAlan Somers 		job->uio.uio_segflg = UIO_USERSPACE;
1689022ca2fcSAlan Somers 		job->uiop = &job->uio;
1690022ca2fcSAlan Somers 	}
16912247f489SAlan Somers 	switch (opcode & (LIO_READ | LIO_WRITE)) {
1692022ca2fcSAlan Somers 	case LIO_READ:
1693022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_READ;
1694022ca2fcSAlan Somers 		break;
1695022ca2fcSAlan Somers 	case LIO_WRITE:
1696022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_WRITE;
1697022ca2fcSAlan Somers 		break;
1698022ca2fcSAlan Somers 	}
1699022ca2fcSAlan Somers 	job->uiop->uio_offset = job->uaiocb.aio_offset;
1700022ca2fcSAlan Somers 	job->uiop->uio_td = td;
1701022ca2fcSAlan Somers 
1702f3215338SJohn Baldwin 	if (opcode == LIO_MLOCK) {
1703f3215338SJohn Baldwin 		aio_schedule(job, aio_process_mlock);
1704f3215338SJohn Baldwin 		error = 0;
1705f3215338SJohn Baldwin 	} else if (fp->f_ops->fo_aio_queue == NULL)
1706f3215338SJohn Baldwin 		error = aio_queue_file(fp, job);
1707f3215338SJohn Baldwin 	else
1708f3215338SJohn Baldwin 		error = fo_aio_queue(fp, job);
1709f3215338SJohn Baldwin 	if (error)
1710022ca2fcSAlan Somers 		goto err3;
1711f3215338SJohn Baldwin 
1712f3215338SJohn Baldwin 	AIO_LOCK(ki);
1713f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_QUEUEING;
1714f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1715f3215338SJohn Baldwin 	ki->kaio_count++;
1716f3215338SJohn Baldwin 	if (lj)
1717f3215338SJohn Baldwin 		lj->lioj_count++;
1718f3215338SJohn Baldwin 	atomic_add_int(&num_queue_count, 1);
1719f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
1720f3215338SJohn Baldwin 		/*
1721f3215338SJohn Baldwin 		 * The queue callback completed the request synchronously.
1722f3215338SJohn Baldwin 		 * The bulk of the completion is deferred in that case
1723f3215338SJohn Baldwin 		 * until this point.
1724f3215338SJohn Baldwin 		 */
1725f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
1726f3215338SJohn Baldwin 	} else
1727f3215338SJohn Baldwin 		TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1728f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1729f3215338SJohn Baldwin 	return (0);
1730f3215338SJohn Baldwin 
1731022ca2fcSAlan Somers err3:
1732f3215338SJohn Baldwin 	if (fp)
1733f3215338SJohn Baldwin 		fdrop(fp, td);
1734022ca2fcSAlan Somers 	knlist_delete(&job->klist, curthread, 0);
1735022ca2fcSAlan Somers err2:
1736022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
1737022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
1738f3215338SJohn Baldwin 	uma_zfree(aiocb_zone, job);
1739022ca2fcSAlan Somers err1:
1740f3215338SJohn Baldwin 	ops->store_error(ujob, error);
1741f3215338SJohn Baldwin 	return (error);
1742f3215338SJohn Baldwin }
1743f3215338SJohn Baldwin 
1744f3215338SJohn Baldwin static void
1745f3215338SJohn Baldwin aio_cancel_daemon_job(struct kaiocb *job)
1746f3215338SJohn Baldwin {
1747f3215338SJohn Baldwin 
1748f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1749f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1750f3215338SJohn Baldwin 		TAILQ_REMOVE(&aio_jobs, job, list);
1751f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1752f3215338SJohn Baldwin 	aio_cancel(job);
1753f3215338SJohn Baldwin }
1754f3215338SJohn Baldwin 
1755f3215338SJohn Baldwin void
1756f3215338SJohn Baldwin aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1757f3215338SJohn Baldwin {
1758f3215338SJohn Baldwin 
1759f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1760f3215338SJohn Baldwin 	if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1761f3215338SJohn Baldwin 		mtx_unlock(&aio_job_mtx);
1762f3215338SJohn Baldwin 		aio_cancel(job);
1763f3215338SJohn Baldwin 		return;
1764f3215338SJohn Baldwin 	}
1765f3215338SJohn Baldwin 	job->handle_fn = func;
1766f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1767f3215338SJohn Baldwin 	aio_kick_nowait(job->userproc);
1768f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1769f3215338SJohn Baldwin }
1770f3215338SJohn Baldwin 
1771f3215338SJohn Baldwin static void
1772f3215338SJohn Baldwin aio_cancel_sync(struct kaiocb *job)
1773f3215338SJohn Baldwin {
1774f3215338SJohn Baldwin 	struct kaioinfo *ki;
1775f3215338SJohn Baldwin 
1776f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1777005ce8e4SJohn Baldwin 	AIO_LOCK(ki);
1778f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1779f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1780005ce8e4SJohn Baldwin 	AIO_UNLOCK(ki);
1781f3215338SJohn Baldwin 	aio_cancel(job);
1782f3215338SJohn Baldwin }
1783f3215338SJohn Baldwin 
1784f3215338SJohn Baldwin int
1785f3215338SJohn Baldwin aio_queue_file(struct file *fp, struct kaiocb *job)
1786f3215338SJohn Baldwin {
1787f3215338SJohn Baldwin 	struct kaioinfo *ki;
1788f3215338SJohn Baldwin 	struct kaiocb *job2;
17899fe297bbSKonstantin Belousov 	struct vnode *vp;
17909fe297bbSKonstantin Belousov 	struct mount *mp;
1791f54c5606SJohn Baldwin 	int error;
17929fe297bbSKonstantin Belousov 	bool safe;
1793f3215338SJohn Baldwin 
1794f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
179572bce9ffSAlan Somers 	error = aio_qbio(job->userproc, job);
1796f54c5606SJohn Baldwin 	if (error >= 0)
1797f54c5606SJohn Baldwin 		return (error);
17989fe297bbSKonstantin Belousov 	safe = false;
17999fe297bbSKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE) {
18009fe297bbSKonstantin Belousov 		vp = fp->f_vnode;
18019fe297bbSKonstantin Belousov 		if (vp->v_type == VREG || vp->v_type == VDIR) {
18029fe297bbSKonstantin Belousov 			mp = fp->f_vnode->v_mount;
18039fe297bbSKonstantin Belousov 			if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
18049fe297bbSKonstantin Belousov 				safe = true;
18059fe297bbSKonstantin Belousov 		}
18069fe297bbSKonstantin Belousov 	}
18079c20dc99SJohn Baldwin 	if (!(safe || enable_aio_unsafe)) {
18089c20dc99SJohn Baldwin 		counted_warning(&unsafe_warningcnt,
18099c20dc99SJohn Baldwin 		    "is attempting to use unsafe AIO requests");
1810f3215338SJohn Baldwin 		return (EOPNOTSUPP);
18119c20dc99SJohn Baldwin 	}
181284af4da6SJohn Dyson 
18132247f489SAlan Somers 	if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
18147e409184SJohn Baldwin 		aio_schedule(job, aio_process_rw);
18157e409184SJohn Baldwin 		error = 0;
18162247f489SAlan Somers 	} else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1817f3215338SJohn Baldwin 		AIO_LOCK(ki);
18185652770dSJohn Baldwin 		TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
18195652770dSJohn Baldwin 			if (job2->fd_file == job->fd_file &&
18202247f489SAlan Somers 			    ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
18215652770dSJohn Baldwin 			    job2->seqno < job->seqno) {
18225652770dSJohn Baldwin 				job2->jobflags |= KAIOCB_CHECKSYNC;
18235652770dSJohn Baldwin 				job->pending++;
1824dbbccfe9SDavid Xu 			}
1825dbbccfe9SDavid Xu 		}
18265652770dSJohn Baldwin 		if (job->pending != 0) {
1827005ce8e4SJohn Baldwin 			if (!aio_set_cancel_function_locked(job,
1828005ce8e4SJohn Baldwin 				aio_cancel_sync)) {
1829f3215338SJohn Baldwin 				AIO_UNLOCK(ki);
1830f3215338SJohn Baldwin 				aio_cancel(job);
1831f3215338SJohn Baldwin 				return (0);
1832f3215338SJohn Baldwin 			}
18335652770dSJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1834759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1835f3215338SJohn Baldwin 			return (0);
1836dbbccfe9SDavid Xu 		}
1837759ccccaSDavid Xu 		AIO_UNLOCK(ki);
1838f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
1839f3215338SJohn Baldwin 		error = 0;
18402247f489SAlan Somers 	} else {
1841f3215338SJohn Baldwin 		error = EINVAL;
1842f3215338SJohn Baldwin 	}
184399eee864SDavid Xu 	return (error);
184499eee864SDavid Xu }
184599eee864SDavid Xu 
184699eee864SDavid Xu static void
184799eee864SDavid Xu aio_kick_nowait(struct proc *userp)
184899eee864SDavid Xu {
184999eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
185039314b7dSJohn Baldwin 	struct aioproc *aiop;
185199eee864SDavid Xu 
185299eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
185399eee864SDavid Xu 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
185499eee864SDavid Xu 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
185539314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
185639314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18570dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
185886bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1859c85650caSJohn Baldwin 		taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
186099eee864SDavid Xu 	}
186199eee864SDavid Xu }
186299eee864SDavid Xu 
1863dbbccfe9SDavid Xu static int
186499eee864SDavid Xu aio_kick(struct proc *userp)
186599eee864SDavid Xu {
186699eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
186739314b7dSJohn Baldwin 	struct aioproc *aiop;
1868dbbccfe9SDavid Xu 	int error, ret = 0;
186999eee864SDavid Xu 
187099eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
187199eee864SDavid Xu retryproc:
1872d254af07SMatthew Dillon 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
18732244ea07SJohn Dyson 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
187439314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
187539314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18760dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
187786bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1878fd3bf775SJohn Dyson 		num_aio_resv_start++;
18791ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
18801ce91824SDavid Xu 		error = aio_newproc(&num_aio_resv_start);
18811ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
18821ce91824SDavid Xu 		if (error) {
188384af4da6SJohn Dyson 			num_aio_resv_start--;
18842244ea07SJohn Dyson 			goto retryproc;
1885fd3bf775SJohn Dyson 		}
1886dbbccfe9SDavid Xu 	} else {
1887dbbccfe9SDavid Xu 		ret = -1;
18881ce91824SDavid Xu 	}
1889dbbccfe9SDavid Xu 	return (ret);
189099eee864SDavid Xu }
18911ce91824SDavid Xu 
189299eee864SDavid Xu static void
189399eee864SDavid Xu aio_kick_helper(void *context, int pending)
189499eee864SDavid Xu {
189599eee864SDavid Xu 	struct proc *userp = context;
189699eee864SDavid Xu 
189799eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
1898dbbccfe9SDavid Xu 	while (--pending >= 0) {
1899dbbccfe9SDavid Xu 		if (aio_kick(userp))
1900dbbccfe9SDavid Xu 			break;
1901dbbccfe9SDavid Xu 	}
190299eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
19032244ea07SJohn Dyson }
19042244ea07SJohn Dyson 
1905fd3bf775SJohn Dyson /*
1906bfbbc4aaSJason Evans  * Support the aio_return system call, as a side-effect, kernel resources are
1907bfbbc4aaSJason Evans  * released.
19082244ea07SJohn Dyson  */
19093858a1f4SJohn Baldwin static int
19105652770dSJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1911fd3bf775SJohn Dyson {
1912b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19135652770dSJohn Baldwin 	struct kaiocb *job;
19142244ea07SJohn Dyson 	struct kaioinfo *ki;
1915bb430bc7SJohn Baldwin 	long status, error;
19162244ea07SJohn Dyson 
1917c0bf5caaSAlan Cox 	ki = p->p_aioinfo;
1918c0bf5caaSAlan Cox 	if (ki == NULL)
1919ac41f2efSAlfred Perlstein 		return (EINVAL);
1920759ccccaSDavid Xu 	AIO_LOCK(ki);
19215652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_done, plist) {
19225652770dSJohn Baldwin 		if (job->ujob == ujob)
1923c0bf5caaSAlan Cox 			break;
1924c0bf5caaSAlan Cox 	}
19255652770dSJohn Baldwin 	if (job != NULL) {
1926f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
19275652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
19285652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
19291ce91824SDavid Xu 		td->td_retval[0] = status;
1930b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
1931b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
1932b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
1933b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
19345652770dSJohn Baldwin 		aio_free_entry(job);
1935759ccccaSDavid Xu 		AIO_UNLOCK(ki);
19365652770dSJohn Baldwin 		ops->store_error(ujob, error);
19375652770dSJohn Baldwin 		ops->store_status(ujob, status);
193855a122bfSDavid Xu 	} else {
19391ce91824SDavid Xu 		error = EINVAL;
1940759ccccaSDavid Xu 		AIO_UNLOCK(ki);
194155a122bfSDavid Xu 	}
19421ce91824SDavid Xu 	return (error);
19432244ea07SJohn Dyson }
19442244ea07SJohn Dyson 
19453858a1f4SJohn Baldwin int
19468451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap)
19473858a1f4SJohn Baldwin {
19483858a1f4SJohn Baldwin 
19493858a1f4SJohn Baldwin 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
19503858a1f4SJohn Baldwin }
19513858a1f4SJohn Baldwin 
19522244ea07SJohn Dyson /*
1953bfbbc4aaSJason Evans  * Allow a process to wakeup when any of the I/O requests are completed.
19542244ea07SJohn Dyson  */
19553858a1f4SJohn Baldwin static int
19563858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
19573858a1f4SJohn Baldwin     struct timespec *ts)
1958fd3bf775SJohn Dyson {
1959b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19604a11ca4eSPoul-Henning Kamp 	struct timeval atv;
19612244ea07SJohn Dyson 	struct kaioinfo *ki;
19625652770dSJohn Baldwin 	struct kaiocb *firstjob, *job;
19633858a1f4SJohn Baldwin 	int error, i, timo;
19642244ea07SJohn Dyson 
19652244ea07SJohn Dyson 	timo = 0;
19663858a1f4SJohn Baldwin 	if (ts) {
19673858a1f4SJohn Baldwin 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
19682244ea07SJohn Dyson 			return (EINVAL);
19692244ea07SJohn Dyson 
19703858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
19712244ea07SJohn Dyson 		if (itimerfix(&atv))
19722244ea07SJohn Dyson 			return (EINVAL);
1973227ee8a1SPoul-Henning Kamp 		timo = tvtohz(&atv);
19742244ea07SJohn Dyson 	}
19752244ea07SJohn Dyson 
19762244ea07SJohn Dyson 	ki = p->p_aioinfo;
19772244ea07SJohn Dyson 	if (ki == NULL)
1978ac41f2efSAlfred Perlstein 		return (EAGAIN);
19792244ea07SJohn Dyson 
19803858a1f4SJohn Baldwin 	if (njoblist == 0)
1981ac41f2efSAlfred Perlstein 		return (0);
19822244ea07SJohn Dyson 
1983759ccccaSDavid Xu 	AIO_LOCK(ki);
19841ce91824SDavid Xu 	for (;;) {
19855652770dSJohn Baldwin 		firstjob = NULL;
19861ce91824SDavid Xu 		error = 0;
19875652770dSJohn Baldwin 		TAILQ_FOREACH(job, &ki->kaio_all, allist) {
198884af4da6SJohn Dyson 			for (i = 0; i < njoblist; i++) {
19895652770dSJohn Baldwin 				if (job->ujob == ujoblist[i]) {
19905652770dSJohn Baldwin 					if (firstjob == NULL)
19915652770dSJohn Baldwin 						firstjob = job;
1992f3215338SJohn Baldwin 					if (job->jobflags & KAIOCB_FINISHED)
19931ce91824SDavid Xu 						goto RETURN;
199484af4da6SJohn Dyson 				}
199584af4da6SJohn Dyson 			}
199684af4da6SJohn Dyson 		}
19971ce91824SDavid Xu 		/* All tasks were finished. */
19985652770dSJohn Baldwin 		if (firstjob == NULL)
19991ce91824SDavid Xu 			break;
20002244ea07SJohn Dyson 
2001fd3bf775SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
2002759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
20031ce91824SDavid Xu 		    "aiospn", timo);
20041ce91824SDavid Xu 		if (error == ERESTART)
20051ce91824SDavid Xu 			error = EINTR;
20061ce91824SDavid Xu 		if (error)
20071ce91824SDavid Xu 			break;
20082244ea07SJohn Dyson 	}
20091ce91824SDavid Xu RETURN:
2010759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20113858a1f4SJohn Baldwin 	return (error);
20123858a1f4SJohn Baldwin }
20133858a1f4SJohn Baldwin 
20143858a1f4SJohn Baldwin int
20158451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
20163858a1f4SJohn Baldwin {
20173858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
20183858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
20193858a1f4SJohn Baldwin 	int error;
20203858a1f4SJohn Baldwin 
2021913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
20223858a1f4SJohn Baldwin 		return (EINVAL);
20233858a1f4SJohn Baldwin 
20243858a1f4SJohn Baldwin 	if (uap->timeout) {
20253858a1f4SJohn Baldwin 		/* Get timespec struct. */
20263858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
20273858a1f4SJohn Baldwin 			return (error);
20283858a1f4SJohn Baldwin 		tsp = &ts;
20293858a1f4SJohn Baldwin 	} else
20303858a1f4SJohn Baldwin 		tsp = NULL;
20313858a1f4SJohn Baldwin 
2032913b9329SAlan Somers 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
20333858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
20343858a1f4SJohn Baldwin 	if (error == 0)
20353858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2036913b9329SAlan Somers 	free(ujoblist, M_AIOS);
20371ce91824SDavid Xu 	return (error);
20382244ea07SJohn Dyson }
2039ee877a35SJohn Dyson 
2040ee877a35SJohn Dyson /*
204172bce9ffSAlan Somers  * aio_cancel cancels any non-bio aio operations not currently in progress.
2042ee877a35SJohn Dyson  */
2043ee877a35SJohn Dyson int
20448451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2045fd3bf775SJohn Dyson {
2046b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2047dd85920aSJason Evans 	struct kaioinfo *ki;
20485652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
2049dd85920aSJason Evans 	struct file *fp;
20501ce91824SDavid Xu 	int error;
2051dd85920aSJason Evans 	int cancelled = 0;
2052dd85920aSJason Evans 	int notcancelled = 0;
2053dd85920aSJason Evans 	struct vnode *vp;
2054dd85920aSJason Evans 
20552a522eb9SJohn Baldwin 	/* Lookup file object. */
2056cbd92ce6SMatt Macy 	error = fget(td, uap->fd, &cap_no_rights, &fp);
20572a522eb9SJohn Baldwin 	if (error)
20582a522eb9SJohn Baldwin 		return (error);
2059dd85920aSJason Evans 
20601ce91824SDavid Xu 	ki = p->p_aioinfo;
20611ce91824SDavid Xu 	if (ki == NULL)
20621ce91824SDavid Xu 		goto done;
20631ce91824SDavid Xu 
2064dd85920aSJason Evans 	if (fp->f_type == DTYPE_VNODE) {
20653b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
20667ad2a82dSMateusz Guzik 		if (vn_isdisk(vp)) {
20672a522eb9SJohn Baldwin 			fdrop(fp, td);
2068b40ce416SJulian Elischer 			td->td_retval[0] = AIO_NOTCANCELED;
2069ac41f2efSAlfred Perlstein 			return (0);
2070dd85920aSJason Evans 		}
2071dd85920aSJason Evans 	}
2072dd85920aSJason Evans 
2073759ccccaSDavid Xu 	AIO_LOCK(ki);
20745652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
20755652770dSJohn Baldwin 		if ((uap->fd == job->uaiocb.aio_fildes) &&
2076dd85920aSJason Evans 		    ((uap->aiocbp == NULL) ||
20775652770dSJohn Baldwin 		     (uap->aiocbp == job->ujob))) {
2078f3215338SJohn Baldwin 			if (aio_cancel_job(p, ki, job)) {
20791ce91824SDavid Xu 				cancelled++;
2080dd85920aSJason Evans 			} else {
2081dd85920aSJason Evans 				notcancelled++;
2082dd85920aSJason Evans 			}
20831aa4c324SDavid Xu 			if (uap->aiocbp != NULL)
20841aa4c324SDavid Xu 				break;
2085dd85920aSJason Evans 		}
2086dd85920aSJason Evans 	}
2087759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20881ce91824SDavid Xu 
2089ad49abc0SAlan Cox done:
20902a522eb9SJohn Baldwin 	fdrop(fp, td);
20911aa4c324SDavid Xu 
20921aa4c324SDavid Xu 	if (uap->aiocbp != NULL) {
2093dd85920aSJason Evans 		if (cancelled) {
2094b40ce416SJulian Elischer 			td->td_retval[0] = AIO_CANCELED;
2095ac41f2efSAlfred Perlstein 			return (0);
2096dd85920aSJason Evans 		}
20971aa4c324SDavid Xu 	}
20981aa4c324SDavid Xu 
20991aa4c324SDavid Xu 	if (notcancelled) {
21001aa4c324SDavid Xu 		td->td_retval[0] = AIO_NOTCANCELED;
21011aa4c324SDavid Xu 		return (0);
21021aa4c324SDavid Xu 	}
21031aa4c324SDavid Xu 
21041aa4c324SDavid Xu 	if (cancelled) {
21051aa4c324SDavid Xu 		td->td_retval[0] = AIO_CANCELED;
21061aa4c324SDavid Xu 		return (0);
21071aa4c324SDavid Xu 	}
21081aa4c324SDavid Xu 
2109b40ce416SJulian Elischer 	td->td_retval[0] = AIO_ALLDONE;
2110dd85920aSJason Evans 
2111ac41f2efSAlfred Perlstein 	return (0);
2112ee877a35SJohn Dyson }
2113ee877a35SJohn Dyson 
2114ee877a35SJohn Dyson /*
2115873fbcd7SRobert Watson  * aio_error is implemented in the kernel level for compatibility purposes
2116873fbcd7SRobert Watson  * only.  For a user mode async implementation, it would be best to do it in
2117873fbcd7SRobert Watson  * a userland subroutine.
2118ee877a35SJohn Dyson  */
21193858a1f4SJohn Baldwin static int
21205652770dSJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2121fd3bf775SJohn Dyson {
2122b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
21235652770dSJohn Baldwin 	struct kaiocb *job;
21242244ea07SJohn Dyson 	struct kaioinfo *ki;
21251ce91824SDavid Xu 	int status;
2126ee877a35SJohn Dyson 
21272244ea07SJohn Dyson 	ki = p->p_aioinfo;
21281ce91824SDavid Xu 	if (ki == NULL) {
21291ce91824SDavid Xu 		td->td_retval[0] = EINVAL;
21301ce91824SDavid Xu 		return (0);
21311ce91824SDavid Xu 	}
2132ee877a35SJohn Dyson 
2133759ccccaSDavid Xu 	AIO_LOCK(ki);
21345652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_all, allist) {
21355652770dSJohn Baldwin 		if (job->ujob == ujob) {
2136f3215338SJohn Baldwin 			if (job->jobflags & KAIOCB_FINISHED)
21371ce91824SDavid Xu 				td->td_retval[0] =
21385652770dSJohn Baldwin 					job->uaiocb._aiocb_private.error;
21391ce91824SDavid Xu 			else
2140b40ce416SJulian Elischer 				td->td_retval[0] = EINPROGRESS;
2141759ccccaSDavid Xu 			AIO_UNLOCK(ki);
2142ac41f2efSAlfred Perlstein 			return (0);
21432244ea07SJohn Dyson 		}
21442244ea07SJohn Dyson 	}
2145759ccccaSDavid Xu 	AIO_UNLOCK(ki);
214684af4da6SJohn Dyson 
21472244ea07SJohn Dyson 	/*
2148a9bf5e37SDavid Xu 	 * Hack for failure of aio_aqueue.
21492244ea07SJohn Dyson 	 */
21505652770dSJohn Baldwin 	status = ops->fetch_status(ujob);
21511ce91824SDavid Xu 	if (status == -1) {
21525652770dSJohn Baldwin 		td->td_retval[0] = ops->fetch_error(ujob);
21531ce91824SDavid Xu 		return (0);
21541ce91824SDavid Xu 	}
21551ce91824SDavid Xu 
21561ce91824SDavid Xu 	td->td_retval[0] = EINVAL;
21571ce91824SDavid Xu 	return (0);
2158ee877a35SJohn Dyson }
2159ee877a35SJohn Dyson 
21603858a1f4SJohn Baldwin int
21618451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap)
21623858a1f4SJohn Baldwin {
21633858a1f4SJohn Baldwin 
21643858a1f4SJohn Baldwin 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
21653858a1f4SJohn Baldwin }
21663858a1f4SJohn Baldwin 
2167eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */
2168399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2169ee877a35SJohn Dyson int
2170399e8c17SJohn Baldwin freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
21710972628aSDavid Xu {
21720972628aSDavid Xu 
21733858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
21743858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
21750972628aSDavid Xu }
2176399e8c17SJohn Baldwin #endif
21770972628aSDavid Xu 
21780972628aSDavid Xu int
21798451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap)
2180fd3bf775SJohn Dyson {
218121d56e9cSAlfred Perlstein 
21823858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2183ee877a35SJohn Dyson }
2184ee877a35SJohn Dyson 
2185022ca2fcSAlan Somers int
2186022ca2fcSAlan Somers sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2187022ca2fcSAlan Somers {
2188022ca2fcSAlan Somers 
2189022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2190022ca2fcSAlan Somers }
2191022ca2fcSAlan Somers 
2192eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */
2193399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2194ee877a35SJohn Dyson int
2195399e8c17SJohn Baldwin freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
21960972628aSDavid Xu {
21970972628aSDavid Xu 
21983858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
21993858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
22000972628aSDavid Xu }
2201399e8c17SJohn Baldwin #endif
22020972628aSDavid Xu 
22030972628aSDavid Xu int
22048451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap)
2205fd3bf775SJohn Dyson {
220621d56e9cSAlfred Perlstein 
22073858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
22080972628aSDavid Xu }
22090972628aSDavid Xu 
22106160e12cSGleb Smirnoff int
2211022ca2fcSAlan Somers sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2212022ca2fcSAlan Somers {
2213022ca2fcSAlan Somers 
2214022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2215022ca2fcSAlan Somers }
2216022ca2fcSAlan Somers 
2217022ca2fcSAlan Somers int
22186160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
22196160e12cSGleb Smirnoff {
22206160e12cSGleb Smirnoff 
22216160e12cSGleb Smirnoff 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
22226160e12cSGleb Smirnoff }
22236160e12cSGleb Smirnoff 
22240972628aSDavid Xu static int
22253858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
22263858a1f4SJohn Baldwin     struct aiocb **acb_list, int nent, struct sigevent *sig,
22273858a1f4SJohn Baldwin     struct aiocb_ops *ops)
22280972628aSDavid Xu {
2229b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22305652770dSJohn Baldwin 	struct aiocb *job;
22312244ea07SJohn Dyson 	struct kaioinfo *ki;
22321ce91824SDavid Xu 	struct aioliojob *lj;
223369cd28daSDoug Ambrisko 	struct kevent kev;
22341ce91824SDavid Xu 	int error;
223552c09831SAlan Somers 	int nagain, nerror;
2236ee877a35SJohn Dyson 	int i;
2237ee877a35SJohn Dyson 
22383858a1f4SJohn Baldwin 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2239ac41f2efSAlfred Perlstein 		return (EINVAL);
22402244ea07SJohn Dyson 
2241913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
2242ac41f2efSAlfred Perlstein 		return (EINVAL);
22432244ea07SJohn Dyson 
2244bfbbc4aaSJason Evans 	if (p->p_aioinfo == NULL)
22452244ea07SJohn Dyson 		aio_init_aioinfo(p);
22462244ea07SJohn Dyson 
22472244ea07SJohn Dyson 	ki = p->p_aioinfo;
22482244ea07SJohn Dyson 
2249a163d034SWarner Losh 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
225084af4da6SJohn Dyson 	lj->lioj_flags = 0;
22511ce91824SDavid Xu 	lj->lioj_count = 0;
22521ce91824SDavid Xu 	lj->lioj_finished_count = 0;
22532e5f6152SMark Johnston 	lj->lioj_signal.sigev_notify = SIGEV_NONE;
2254d8b0556cSKonstantin Belousov 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
22554c0fb2cfSDavid Xu 	ksiginfo_init(&lj->lioj_ksi);
225669cd28daSDoug Ambrisko 
225784af4da6SJohn Dyson 	/*
2258bfbbc4aaSJason Evans 	 * Setup signal.
225984af4da6SJohn Dyson 	 */
22603858a1f4SJohn Baldwin 	if (sig && (mode == LIO_NOWAIT)) {
22613858a1f4SJohn Baldwin 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
226269cd28daSDoug Ambrisko 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
226369cd28daSDoug Ambrisko 			/* Assume only new style KEVENT */
226436c4960eSMark Johnston 			memset(&kev, 0, sizeof(kev));
226569cd28daSDoug Ambrisko 			kev.filter = EVFILT_LIO;
226669cd28daSDoug Ambrisko 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
22673858a1f4SJohn Baldwin 			kev.ident = (uintptr_t)uacb_list; /* something unique */
226869cd28daSDoug Ambrisko 			kev.data = (intptr_t)lj;
22691ce91824SDavid Xu 			/* pass user defined sigval data */
22701ce91824SDavid Xu 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
22714db71d27SJohn-Mark Gurney 			error = kqfd_register(
2272792843c3SMark Johnston 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2273792843c3SMark Johnston 			    M_WAITOK);
227469cd28daSDoug Ambrisko 			if (error) {
227569cd28daSDoug Ambrisko 				uma_zfree(aiolio_zone, lj);
227669cd28daSDoug Ambrisko 				return (error);
227769cd28daSDoug Ambrisko 			}
22781ce91824SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
22791ce91824SDavid Xu 			;
228068d71118SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
228168d71118SDavid Xu 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
228268d71118SDavid Xu 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
228369cd28daSDoug Ambrisko 					uma_zfree(aiolio_zone, lj);
228469cd28daSDoug Ambrisko 					return EINVAL;
228568d71118SDavid Xu 				}
228684af4da6SJohn Dyson 				lj->lioj_flags |= LIOJ_SIGNAL;
228768d71118SDavid Xu 		} else {
228868d71118SDavid Xu 			uma_zfree(aiolio_zone, lj);
228968d71118SDavid Xu 			return EINVAL;
22904d752b01SAlan Cox 		}
22911ce91824SDavid Xu 	}
229269cd28daSDoug Ambrisko 
2293759ccccaSDavid Xu 	AIO_LOCK(ki);
22942f3cf918SAlfred Perlstein 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
22952244ea07SJohn Dyson 	/*
22961ce91824SDavid Xu 	 * Add extra aiocb count to avoid the lio to be freed
22971ce91824SDavid Xu 	 * by other threads doing aio_waitcomplete or aio_return,
22981ce91824SDavid Xu 	 * and prevent event from being sent until we have queued
22991ce91824SDavid Xu 	 * all tasks.
23001ce91824SDavid Xu 	 */
23011ce91824SDavid Xu 	lj->lioj_count = 1;
2302759ccccaSDavid Xu 	AIO_UNLOCK(ki);
23031ce91824SDavid Xu 
23041ce91824SDavid Xu 	/*
2305bfbbc4aaSJason Evans 	 * Get pointers to the list of I/O requests.
23062244ea07SJohn Dyson 	 */
230752c09831SAlan Somers 	nagain = 0;
2308fd3bf775SJohn Dyson 	nerror = 0;
23093858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++) {
23105652770dSJohn Baldwin 		job = acb_list[i];
23115652770dSJohn Baldwin 		if (job != NULL) {
23125652770dSJohn Baldwin 			error = aio_aqueue(td, job, lj, LIO_NOP, ops);
231352c09831SAlan Somers 			if (error == EAGAIN)
231452c09831SAlan Somers 				nagain++;
231552c09831SAlan Somers 			else if (error != 0)
2316fd3bf775SJohn Dyson 				nerror++;
2317fd3bf775SJohn Dyson 		}
2318fd3bf775SJohn Dyson 	}
23192244ea07SJohn Dyson 
23201ce91824SDavid Xu 	error = 0;
2321759ccccaSDavid Xu 	AIO_LOCK(ki);
23223858a1f4SJohn Baldwin 	if (mode == LIO_WAIT) {
23231ce91824SDavid Xu 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2324fd3bf775SJohn Dyson 			ki->kaio_flags |= KAIO_WAKEUP;
2325759ccccaSDavid Xu 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
23261ce91824SDavid Xu 			    PRIBIO | PCATCH, "aiospn", 0);
23271ce91824SDavid Xu 			if (error == ERESTART)
23281ce91824SDavid Xu 				error = EINTR;
23291ce91824SDavid Xu 			if (error)
23301ce91824SDavid Xu 				break;
23311ce91824SDavid Xu 		}
23321ce91824SDavid Xu 	} else {
23331ce91824SDavid Xu 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
23341ce91824SDavid Xu 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
23351ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
23361ce91824SDavid Xu 				KNOTE_LOCKED(&lj->klist, 1);
23371ce91824SDavid Xu 			}
233829331656SKonstantin Belousov 			if ((lj->lioj_flags & (LIOJ_SIGNAL |
233929331656SKonstantin Belousov 			    LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
234029331656SKonstantin Belousov 			    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
23411ce91824SDavid Xu 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
23426814c2daSKonstantin Belousov 				aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
23436814c2daSKonstantin Belousov 				    lj->lioj_count != 1);
23441ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
23452244ea07SJohn Dyson 			}
23462244ea07SJohn Dyson 		}
23471ce91824SDavid Xu 	}
23481ce91824SDavid Xu 	lj->lioj_count--;
23491ce91824SDavid Xu 	if (lj->lioj_count == 0) {
23501ce91824SDavid Xu 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
23511ce91824SDavid Xu 		knlist_delete(&lj->klist, curthread, 1);
2352759ccccaSDavid Xu 		PROC_LOCK(p);
23531ce91824SDavid Xu 		sigqueue_take(&lj->lioj_ksi);
23541ce91824SDavid Xu 		PROC_UNLOCK(p);
2355759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23561ce91824SDavid Xu 		uma_zfree(aiolio_zone, lj);
23571ce91824SDavid Xu 	} else
2358759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23592244ea07SJohn Dyson 
23601ce91824SDavid Xu 	if (nerror)
23611ce91824SDavid Xu 		return (EIO);
236252c09831SAlan Somers 	else if (nagain)
236352c09831SAlan Somers 		return (EAGAIN);
236452c09831SAlan Somers 	else
23651ce91824SDavid Xu 		return (error);
2366ee877a35SJohn Dyson }
2367fd3bf775SJohn Dyson 
23683858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
2369399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
23703858a1f4SJohn Baldwin int
2371399e8c17SJohn Baldwin freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
23723858a1f4SJohn Baldwin {
23733858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23743858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23753858a1f4SJohn Baldwin 	struct osigevent osig;
23763858a1f4SJohn Baldwin 	int error, nent;
23773858a1f4SJohn Baldwin 
23783858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23793858a1f4SJohn Baldwin 		return (EINVAL);
23803858a1f4SJohn Baldwin 
23813858a1f4SJohn Baldwin 	nent = uap->nent;
2382913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
23833858a1f4SJohn Baldwin 		return (EINVAL);
23843858a1f4SJohn Baldwin 
23853858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23863858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
23873858a1f4SJohn Baldwin 		if (error)
23883858a1f4SJohn Baldwin 			return (error);
23893858a1f4SJohn Baldwin 		error = convert_old_sigevent(&osig, &sig);
23903858a1f4SJohn Baldwin 		if (error)
23913858a1f4SJohn Baldwin 			return (error);
23923858a1f4SJohn Baldwin 		sigp = &sig;
23933858a1f4SJohn Baldwin 	} else
23943858a1f4SJohn Baldwin 		sigp = NULL;
23953858a1f4SJohn Baldwin 
23963858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
23973858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
23983858a1f4SJohn Baldwin 	if (error == 0)
23993858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode,
24003858a1f4SJohn Baldwin 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
24013858a1f4SJohn Baldwin 		    &aiocb_ops_osigevent);
24023858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24033858a1f4SJohn Baldwin 	return (error);
24043858a1f4SJohn Baldwin }
2405399e8c17SJohn Baldwin #endif
24063858a1f4SJohn Baldwin 
24073858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
24083858a1f4SJohn Baldwin int
24098451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
24103858a1f4SJohn Baldwin {
24113858a1f4SJohn Baldwin 	struct aiocb **acb_list;
24123858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
24133858a1f4SJohn Baldwin 	int error, nent;
24143858a1f4SJohn Baldwin 
24153858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
24163858a1f4SJohn Baldwin 		return (EINVAL);
24173858a1f4SJohn Baldwin 
24183858a1f4SJohn Baldwin 	nent = uap->nent;
2419913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
24203858a1f4SJohn Baldwin 		return (EINVAL);
24213858a1f4SJohn Baldwin 
24223858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
24233858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig, sizeof(sig));
24243858a1f4SJohn Baldwin 		if (error)
24253858a1f4SJohn Baldwin 			return (error);
24263858a1f4SJohn Baldwin 		sigp = &sig;
24273858a1f4SJohn Baldwin 	} else
24283858a1f4SJohn Baldwin 		sigp = NULL;
24293858a1f4SJohn Baldwin 
24303858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24313858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24323858a1f4SJohn Baldwin 	if (error == 0)
24333858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
24343858a1f4SJohn Baldwin 		    nent, sigp, &aiocb_ops);
24353858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24363858a1f4SJohn Baldwin 	return (error);
24373858a1f4SJohn Baldwin }
24383858a1f4SJohn Baldwin 
2439fd3bf775SJohn Dyson static void
2440022ca2fcSAlan Somers aio_biocleanup(struct bio *bp)
2441fd3bf775SJohn Dyson {
24425652770dSJohn Baldwin 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
244327b8220dSDavid Xu 	struct kaioinfo *ki;
244401206038SAlan Somers 	struct buf *pbuf = (struct buf *)bp->bio_caller2;
24451ce91824SDavid Xu 
2446f743d981SAlexander Motin 	/* Release mapping into kernel space. */
244701206038SAlan Somers 	if (pbuf != NULL) {
244801206038SAlan Somers 		MPASS(pbuf->b_npages <= atop(maxphys) + 1);
244901206038SAlan Somers 		pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
245001206038SAlan Somers 		vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
245101206038SAlan Somers 		uma_zfree(pbuf_zone, pbuf);
2452f743d981SAlexander Motin 		atomic_subtract_int(&num_buf_aio, 1);
2453a9d4fe97SKonstantin Belousov 		ki = job->userproc->p_aioinfo;
2454f3215338SJohn Baldwin 		AIO_LOCK(ki);
2455f3215338SJohn Baldwin 		ki->kaio_buffer_count--;
2456f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
2457cd853791SKonstantin Belousov 	} else {
245801206038SAlan Somers 		MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
245901206038SAlan Somers 		vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
246001206038SAlan Somers 		free(bp->bio_ma, M_TEMP);
24618091e52bSJohn Baldwin 		atomic_subtract_int(&num_unmapped_aio, 1);
2462cd853791SKonstantin Belousov 	}
2463f743d981SAlexander Motin 	g_destroy_bio(bp);
246484af4da6SJohn Dyson }
2465bfbbc4aaSJason Evans 
2466022ca2fcSAlan Somers static void
2467022ca2fcSAlan Somers aio_biowakeup(struct bio *bp)
2468022ca2fcSAlan Somers {
2469022ca2fcSAlan Somers 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2470022ca2fcSAlan Somers 	size_t nbytes;
2471022ca2fcSAlan Somers 	long bcount = bp->bio_bcount;
2472022ca2fcSAlan Somers 	long resid = bp->bio_resid;
2473022ca2fcSAlan Somers 	int error, opcode, nblks;
2474022ca2fcSAlan Somers 	int bio_error = bp->bio_error;
2475022ca2fcSAlan Somers 	uint16_t flags = bp->bio_flags;
2476022ca2fcSAlan Somers 
2477022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
2478022ca2fcSAlan Somers 
2479022ca2fcSAlan Somers 	aio_biocleanup(bp);
2480022ca2fcSAlan Somers 
2481022ca2fcSAlan Somers 	nbytes =bcount - resid;
2482022ca2fcSAlan Somers 	atomic_add_acq_long(&job->nbytes, nbytes);
2483022ca2fcSAlan Somers 	nblks = btodb(nbytes);
2484022ca2fcSAlan Somers 	error = 0;
2485022ca2fcSAlan Somers 	/*
2486022ca2fcSAlan Somers 	 * If multiple bios experienced an error, the job will reflect the
2487022ca2fcSAlan Somers 	 * error of whichever failed bio completed last.
2488022ca2fcSAlan Somers 	 */
2489022ca2fcSAlan Somers 	if (flags & BIO_ERROR)
2490022ca2fcSAlan Somers 		atomic_set_int(&job->error, bio_error);
24912247f489SAlan Somers 	if (opcode & LIO_WRITE)
2492022ca2fcSAlan Somers 		atomic_add_int(&job->outblock, nblks);
2493022ca2fcSAlan Somers 	else
2494022ca2fcSAlan Somers 		atomic_add_int(&job->inblock, nblks);
2495022ca2fcSAlan Somers 	atomic_subtract_int(&job->nbio, 1);
2496022ca2fcSAlan Somers 
2497022ca2fcSAlan Somers 
2498022ca2fcSAlan Somers 	if (atomic_load_int(&job->nbio) == 0) {
2499022ca2fcSAlan Somers 		if (atomic_load_int(&job->error))
2500022ca2fcSAlan Somers 			aio_complete(job, -1, job->error);
2501022ca2fcSAlan Somers 		else
2502022ca2fcSAlan Somers 			aio_complete(job, atomic_load_long(&job->nbytes), 0);
2503022ca2fcSAlan Somers 	}
2504022ca2fcSAlan Somers }
2505022ca2fcSAlan Somers 
2506eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */
25073858a1f4SJohn Baldwin static int
25085652770dSJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
25093858a1f4SJohn Baldwin     struct timespec *ts, struct aiocb_ops *ops)
2510bfbbc4aaSJason Evans {
2511b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2512bfbbc4aaSJason Evans 	struct timeval atv;
2513bfbbc4aaSJason Evans 	struct kaioinfo *ki;
25145652770dSJohn Baldwin 	struct kaiocb *job;
25155652770dSJohn Baldwin 	struct aiocb *ujob;
2516bb430bc7SJohn Baldwin 	long error, status;
2517bb430bc7SJohn Baldwin 	int timo;
2518bfbbc4aaSJason Evans 
25195652770dSJohn Baldwin 	ops->store_aiocb(ujobp, NULL);
2520dd85920aSJason Evans 
252138d68e2dSPawel Jakub Dawidek 	if (ts == NULL) {
2522bfbbc4aaSJason Evans 		timo = 0;
252338d68e2dSPawel Jakub Dawidek 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
252438d68e2dSPawel Jakub Dawidek 		timo = -1;
252538d68e2dSPawel Jakub Dawidek 	} else {
25263858a1f4SJohn Baldwin 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2527bfbbc4aaSJason Evans 			return (EINVAL);
2528bfbbc4aaSJason Evans 
25293858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2530bfbbc4aaSJason Evans 		if (itimerfix(&atv))
2531bfbbc4aaSJason Evans 			return (EINVAL);
2532bfbbc4aaSJason Evans 		timo = tvtohz(&atv);
2533bfbbc4aaSJason Evans 	}
2534bfbbc4aaSJason Evans 
25358213baf0SChristian S.J. Peron 	if (p->p_aioinfo == NULL)
2536323fe565SDavid Xu 		aio_init_aioinfo(p);
25378213baf0SChristian S.J. Peron 	ki = p->p_aioinfo;
2538bfbbc4aaSJason Evans 
25391ce91824SDavid Xu 	error = 0;
25405652770dSJohn Baldwin 	job = NULL;
2541759ccccaSDavid Xu 	AIO_LOCK(ki);
25425652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
254338d68e2dSPawel Jakub Dawidek 		if (timo == -1) {
254438d68e2dSPawel Jakub Dawidek 			error = EWOULDBLOCK;
254538d68e2dSPawel Jakub Dawidek 			break;
254638d68e2dSPawel Jakub Dawidek 		}
25471ce91824SDavid Xu 		ki->kaio_flags |= KAIO_WAKEUP;
2548759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
25491ce91824SDavid Xu 		    "aiowc", timo);
255027b8220dSDavid Xu 		if (timo && error == ERESTART)
25511ce91824SDavid Xu 			error = EINTR;
25521ce91824SDavid Xu 		if (error)
25531ce91824SDavid Xu 			break;
25541ce91824SDavid Xu 	}
25551ce91824SDavid Xu 
25565652770dSJohn Baldwin 	if (job != NULL) {
2557f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
25585652770dSJohn Baldwin 		ujob = job->ujob;
25595652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
25605652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
25611ce91824SDavid Xu 		td->td_retval[0] = status;
2562b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
2563b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
2564b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
2565b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
25665652770dSJohn Baldwin 		aio_free_entry(job);
2567759ccccaSDavid Xu 		AIO_UNLOCK(ki);
25685652770dSJohn Baldwin 		ops->store_aiocb(ujobp, ujob);
25695652770dSJohn Baldwin 		ops->store_error(ujob, error);
25705652770dSJohn Baldwin 		ops->store_status(ujob, status);
25711ce91824SDavid Xu 	} else
2572759ccccaSDavid Xu 		AIO_UNLOCK(ki);
2573bfbbc4aaSJason Evans 
2574ac41f2efSAlfred Perlstein 	return (error);
2575bfbbc4aaSJason Evans }
2576cb679c38SJonathan Lemon 
257799eee864SDavid Xu int
25788451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
25793858a1f4SJohn Baldwin {
25803858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
25813858a1f4SJohn Baldwin 	int error;
25823858a1f4SJohn Baldwin 
25833858a1f4SJohn Baldwin 	if (uap->timeout) {
25843858a1f4SJohn Baldwin 		/* Get timespec struct. */
25853858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts, sizeof(ts));
25863858a1f4SJohn Baldwin 		if (error)
25873858a1f4SJohn Baldwin 			return (error);
25883858a1f4SJohn Baldwin 		tsp = &ts;
25893858a1f4SJohn Baldwin 	} else
25903858a1f4SJohn Baldwin 		tsp = NULL;
25913858a1f4SJohn Baldwin 
25923858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
25933858a1f4SJohn Baldwin }
25943858a1f4SJohn Baldwin 
25953858a1f4SJohn Baldwin static int
25965652770dSJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
25973858a1f4SJohn Baldwin     struct aiocb_ops *ops)
259899eee864SDavid Xu {
2599801ac943SThomas Munro 	int listop;
260099eee864SDavid Xu 
2601801ac943SThomas Munro 	switch (op) {
2602801ac943SThomas Munro 	case O_SYNC:
2603801ac943SThomas Munro 		listop = LIO_SYNC;
2604801ac943SThomas Munro 		break;
2605801ac943SThomas Munro 	case O_DSYNC:
2606801ac943SThomas Munro 		listop = LIO_DSYNC;
2607801ac943SThomas Munro 		break;
2608801ac943SThomas Munro 	default:
260999eee864SDavid Xu 		return (EINVAL);
2610801ac943SThomas Munro 	}
2611801ac943SThomas Munro 
2612801ac943SThomas Munro 	return (aio_aqueue(td, ujob, NULL, listop, ops));
26133858a1f4SJohn Baldwin }
26143858a1f4SJohn Baldwin 
26153858a1f4SJohn Baldwin int
26168451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
26173858a1f4SJohn Baldwin {
26183858a1f4SJohn Baldwin 
26193858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
262099eee864SDavid Xu }
262199eee864SDavid Xu 
2622eb8e6d52SEivind Eklund /* kqueue attach function */
2623cb679c38SJonathan Lemon static int
2624cb679c38SJonathan Lemon filt_aioattach(struct knote *kn)
2625cb679c38SJonathan Lemon {
26262b34e843SKonstantin Belousov 	struct kaiocb *job;
26272b34e843SKonstantin Belousov 
26282b34e843SKonstantin Belousov 	job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2629cb679c38SJonathan Lemon 
2630cb679c38SJonathan Lemon 	/*
26315652770dSJohn Baldwin 	 * The job pointer must be validated before using it, so
2632cb679c38SJonathan Lemon 	 * registration is restricted to the kernel; the user cannot
2633cb679c38SJonathan Lemon 	 * set EV_FLAG1.
2634cb679c38SJonathan Lemon 	 */
2635cb679c38SJonathan Lemon 	if ((kn->kn_flags & EV_FLAG1) == 0)
2636cb679c38SJonathan Lemon 		return (EPERM);
26375652770dSJohn Baldwin 	kn->kn_ptr.p_aio = job;
2638cb679c38SJonathan Lemon 	kn->kn_flags &= ~EV_FLAG1;
2639cb679c38SJonathan Lemon 
26405652770dSJohn Baldwin 	knlist_add(&job->klist, kn, 0);
2641cb679c38SJonathan Lemon 
2642cb679c38SJonathan Lemon 	return (0);
2643cb679c38SJonathan Lemon }
2644cb679c38SJonathan Lemon 
2645eb8e6d52SEivind Eklund /* kqueue detach function */
2646cb679c38SJonathan Lemon static void
2647cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn)
2648cb679c38SJonathan Lemon {
26498e9fc278SDoug Ambrisko 	struct knlist *knl;
2650cb679c38SJonathan Lemon 
26518e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_aio->klist;
26528e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26538e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26548e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26558e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
2656cb679c38SJonathan Lemon }
2657cb679c38SJonathan Lemon 
2658eb8e6d52SEivind Eklund /* kqueue filter function */
2659cb679c38SJonathan Lemon /*ARGSUSED*/
2660cb679c38SJonathan Lemon static int
2661cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint)
2662cb679c38SJonathan Lemon {
26635652770dSJohn Baldwin 	struct kaiocb *job = kn->kn_ptr.p_aio;
2664cb679c38SJonathan Lemon 
26655652770dSJohn Baldwin 	kn->kn_data = job->uaiocb._aiocb_private.error;
2666f3215338SJohn Baldwin 	if (!(job->jobflags & KAIOCB_FINISHED))
2667cb679c38SJonathan Lemon 		return (0);
2668cb679c38SJonathan Lemon 	kn->kn_flags |= EV_EOF;
2669cb679c38SJonathan Lemon 	return (1);
2670cb679c38SJonathan Lemon }
267169cd28daSDoug Ambrisko 
267269cd28daSDoug Ambrisko /* kqueue attach function */
267369cd28daSDoug Ambrisko static int
267469cd28daSDoug Ambrisko filt_lioattach(struct knote *kn)
267569cd28daSDoug Ambrisko {
26762b34e843SKonstantin Belousov 	struct aioliojob *lj;
26772b34e843SKonstantin Belousov 
26782b34e843SKonstantin Belousov 	lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
267969cd28daSDoug Ambrisko 
268069cd28daSDoug Ambrisko 	/*
26811ce91824SDavid Xu 	 * The aioliojob pointer must be validated before using it, so
268269cd28daSDoug Ambrisko 	 * registration is restricted to the kernel; the user cannot
268369cd28daSDoug Ambrisko 	 * set EV_FLAG1.
268469cd28daSDoug Ambrisko 	 */
268569cd28daSDoug Ambrisko 	if ((kn->kn_flags & EV_FLAG1) == 0)
268669cd28daSDoug Ambrisko 		return (EPERM);
2687a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_lio = lj;
268869cd28daSDoug Ambrisko 	kn->kn_flags &= ~EV_FLAG1;
268969cd28daSDoug Ambrisko 
269069cd28daSDoug Ambrisko 	knlist_add(&lj->klist, kn, 0);
269169cd28daSDoug Ambrisko 
269269cd28daSDoug Ambrisko 	return (0);
269369cd28daSDoug Ambrisko }
269469cd28daSDoug Ambrisko 
269569cd28daSDoug Ambrisko /* kqueue detach function */
269669cd28daSDoug Ambrisko static void
269769cd28daSDoug Ambrisko filt_liodetach(struct knote *kn)
269869cd28daSDoug Ambrisko {
26998e9fc278SDoug Ambrisko 	struct knlist *knl;
270069cd28daSDoug Ambrisko 
27018e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_lio->klist;
27028e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
27038e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
27048e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
27058e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
270669cd28daSDoug Ambrisko }
270769cd28daSDoug Ambrisko 
270869cd28daSDoug Ambrisko /* kqueue filter function */
270969cd28daSDoug Ambrisko /*ARGSUSED*/
271069cd28daSDoug Ambrisko static int
271169cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint)
271269cd28daSDoug Ambrisko {
2713a8afa221SJean-Sébastien Pédron 	struct aioliojob * lj = kn->kn_ptr.p_lio;
27141ce91824SDavid Xu 
271569cd28daSDoug Ambrisko 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
271669cd28daSDoug Ambrisko }
27173858a1f4SJohn Baldwin 
2718841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2719399e8c17SJohn Baldwin #include <sys/mount.h>
2720399e8c17SJohn Baldwin #include <sys/socket.h>
2721399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32.h>
2722399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_proto.h>
2723399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2724399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_syscall.h>
2725399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_util.h>
27263858a1f4SJohn Baldwin 
27273858a1f4SJohn Baldwin struct __aiocb_private32 {
27283858a1f4SJohn Baldwin 	int32_t	status;
27293858a1f4SJohn Baldwin 	int32_t	error;
27303858a1f4SJohn Baldwin 	uint32_t kernelinfo;
27313858a1f4SJohn Baldwin };
27323858a1f4SJohn Baldwin 
2733399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27343858a1f4SJohn Baldwin typedef struct oaiocb32 {
27353858a1f4SJohn Baldwin 	int	aio_fildes;		/* File descriptor */
27363858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27373858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27383858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27393858a1f4SJohn Baldwin 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
27403858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27413858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27423858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27433858a1f4SJohn Baldwin } oaiocb32_t;
2744399e8c17SJohn Baldwin #endif
27453858a1f4SJohn Baldwin 
27463858a1f4SJohn Baldwin typedef struct aiocb32 {
27473858a1f4SJohn Baldwin 	int32_t	aio_fildes;		/* File descriptor */
27483858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27493858a1f4SJohn Baldwin 	uint32_t aio_buf;	/* I/O buffer in process space */
27503858a1f4SJohn Baldwin 	uint32_t aio_nbytes;	/* Number of bytes for I/O */
27513858a1f4SJohn Baldwin 	int	__spare__[2];
27523858a1f4SJohn Baldwin 	uint32_t __spare2__;
27533858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27543858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27553858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27563858a1f4SJohn Baldwin 	struct	sigevent32 aio_sigevent;	/* Signal to deliver */
27573858a1f4SJohn Baldwin } aiocb32_t;
27583858a1f4SJohn Baldwin 
2759399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27603858a1f4SJohn Baldwin static int
27613858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
27623858a1f4SJohn Baldwin {
27633858a1f4SJohn Baldwin 
27643858a1f4SJohn Baldwin 	/*
27653858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
27663858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
27673858a1f4SJohn Baldwin 	 */
27683858a1f4SJohn Baldwin 	CP(*osig, *nsig, sigev_notify);
27693858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
27703858a1f4SJohn Baldwin 	case SIGEV_NONE:
27713858a1f4SJohn Baldwin 		break;
27723858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
27733858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
27743858a1f4SJohn Baldwin 		break;
27753858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
27763858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
27773858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
27783858a1f4SJohn Baldwin 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
27793858a1f4SJohn Baldwin 		break;
27803858a1f4SJohn Baldwin 	default:
27813858a1f4SJohn Baldwin 		return (EINVAL);
27823858a1f4SJohn Baldwin 	}
27833858a1f4SJohn Baldwin 	return (0);
27843858a1f4SJohn Baldwin }
27853858a1f4SJohn Baldwin 
27863858a1f4SJohn Baldwin static int
2787022ca2fcSAlan Somers aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2788022ca2fcSAlan Somers     int type __unused)
27893858a1f4SJohn Baldwin {
27903858a1f4SJohn Baldwin 	struct oaiocb32 job32;
2791022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
27923858a1f4SJohn Baldwin 	int error;
27933858a1f4SJohn Baldwin 
2794022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
27953858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
27963858a1f4SJohn Baldwin 	if (error)
27973858a1f4SJohn Baldwin 		return (error);
27983858a1f4SJohn Baldwin 
2799022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2800022ca2fcSAlan Somers 
2801022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2802022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2803022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, aio_buf);
2804022ca2fcSAlan Somers 	CP(job32, *kcb, aio_nbytes);
2805022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
2806022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2807022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2808022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2809022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
28103858a1f4SJohn Baldwin 	return (convert_old_sigevent32(&job32.aio_sigevent,
2811022ca2fcSAlan Somers 	    &kcb->aio_sigevent));
28123858a1f4SJohn Baldwin }
2813399e8c17SJohn Baldwin #endif
28143858a1f4SJohn Baldwin 
28153858a1f4SJohn Baldwin static int
2816022ca2fcSAlan Somers aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
28173858a1f4SJohn Baldwin {
28183858a1f4SJohn Baldwin 	struct aiocb32 job32;
2819022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
2820022ca2fcSAlan Somers 	struct iovec32 *iov32;
28213858a1f4SJohn Baldwin 	int error;
28223858a1f4SJohn Baldwin 
28233858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
28243858a1f4SJohn Baldwin 	if (error)
28253858a1f4SJohn Baldwin 		return (error);
2826022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2827022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2828022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
28292247f489SAlan Somers 	if (type & LIO_VECTORED) {
2830022ca2fcSAlan Somers 		iov32 = PTRIN(job32.aio_iov);
2831022ca2fcSAlan Somers 		CP(job32, *kcb, aio_iovcnt);
2832022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
2833022ca2fcSAlan Somers 		error = freebsd32_copyinuio(iov32,
2834022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
2835022ca2fcSAlan Somers 		if (error)
2836022ca2fcSAlan Somers 			return (error);
2837022ca2fcSAlan Somers 	} else {
2838022ca2fcSAlan Somers 		PTRIN_CP(job32, *kcb, aio_buf);
2839022ca2fcSAlan Somers 		CP(job32, *kcb, aio_nbytes);
2840022ca2fcSAlan Somers 	}
2841022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2842022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2843022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2844022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2845022ca2fcSAlan Somers 	error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2846022ca2fcSAlan Somers 
2847022ca2fcSAlan Somers 	return (error);
28483858a1f4SJohn Baldwin }
28493858a1f4SJohn Baldwin 
28503858a1f4SJohn Baldwin static long
28513858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob)
28523858a1f4SJohn Baldwin {
28533858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28543858a1f4SJohn Baldwin 
28553858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28563858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.status));
28573858a1f4SJohn Baldwin }
28583858a1f4SJohn Baldwin 
28593858a1f4SJohn Baldwin static long
28603858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob)
28613858a1f4SJohn Baldwin {
28623858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28633858a1f4SJohn Baldwin 
28643858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28653858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.error));
28663858a1f4SJohn Baldwin }
28673858a1f4SJohn Baldwin 
28683858a1f4SJohn Baldwin static int
28693858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status)
28703858a1f4SJohn Baldwin {
28713858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28723858a1f4SJohn Baldwin 
28733858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28743858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.status, status));
28753858a1f4SJohn Baldwin }
28763858a1f4SJohn Baldwin 
28773858a1f4SJohn Baldwin static int
28783858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error)
28793858a1f4SJohn Baldwin {
28803858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28813858a1f4SJohn Baldwin 
28823858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28833858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.error, error));
28843858a1f4SJohn Baldwin }
28853858a1f4SJohn Baldwin 
28863858a1f4SJohn Baldwin static int
28873858a1f4SJohn Baldwin aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
28883858a1f4SJohn Baldwin {
28893858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28903858a1f4SJohn Baldwin 
28913858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28923858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
28933858a1f4SJohn Baldwin }
28943858a1f4SJohn Baldwin 
28953858a1f4SJohn Baldwin static int
28963858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
28973858a1f4SJohn Baldwin {
28983858a1f4SJohn Baldwin 
28993858a1f4SJohn Baldwin 	return (suword32(ujobp, (long)ujob));
29003858a1f4SJohn Baldwin }
29013858a1f4SJohn Baldwin 
29023858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = {
2903849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin,
29043858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29053858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29063858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29073858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29083858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29093858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29103858a1f4SJohn Baldwin };
29113858a1f4SJohn Baldwin 
2912399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29133858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = {
2914849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin_old_sigevent,
29153858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29163858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29173858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29183858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29193858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29203858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29213858a1f4SJohn Baldwin };
2922399e8c17SJohn Baldwin #endif
29233858a1f4SJohn Baldwin 
29243858a1f4SJohn Baldwin int
29253858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
29263858a1f4SJohn Baldwin {
29273858a1f4SJohn Baldwin 
29283858a1f4SJohn Baldwin 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29293858a1f4SJohn Baldwin }
29303858a1f4SJohn Baldwin 
29313858a1f4SJohn Baldwin int
29323858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
29333858a1f4SJohn Baldwin {
29343858a1f4SJohn Baldwin 	struct timespec32 ts32;
29353858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
29363858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
29373858a1f4SJohn Baldwin 	uint32_t *ujoblist32;
29383858a1f4SJohn Baldwin 	int error, i;
29393858a1f4SJohn Baldwin 
2940913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
29413858a1f4SJohn Baldwin 		return (EINVAL);
29423858a1f4SJohn Baldwin 
29433858a1f4SJohn Baldwin 	if (uap->timeout) {
29443858a1f4SJohn Baldwin 		/* Get timespec struct. */
29453858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
29463858a1f4SJohn Baldwin 			return (error);
29473858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
29483858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
29493858a1f4SJohn Baldwin 		tsp = &ts;
29503858a1f4SJohn Baldwin 	} else
29513858a1f4SJohn Baldwin 		tsp = NULL;
29523858a1f4SJohn Baldwin 
2953913b9329SAlan Somers 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
29543858a1f4SJohn Baldwin 	ujoblist32 = (uint32_t *)ujoblist;
29553858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
29563858a1f4SJohn Baldwin 	    sizeof(ujoblist32[0]));
29573858a1f4SJohn Baldwin 	if (error == 0) {
2958df485bdbSAlan Somers 		for (i = uap->nent - 1; i >= 0; i--)
29593858a1f4SJohn Baldwin 			ujoblist[i] = PTRIN(ujoblist32[i]);
29603858a1f4SJohn Baldwin 
29613858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
29623858a1f4SJohn Baldwin 	}
2963913b9329SAlan Somers 	free(ujoblist, M_AIOS);
29643858a1f4SJohn Baldwin 	return (error);
29653858a1f4SJohn Baldwin }
29663858a1f4SJohn Baldwin 
29673858a1f4SJohn Baldwin int
29683858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
29693858a1f4SJohn Baldwin {
29703858a1f4SJohn Baldwin 
29713858a1f4SJohn Baldwin 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29723858a1f4SJohn Baldwin }
29733858a1f4SJohn Baldwin 
2974399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29753858a1f4SJohn Baldwin int
2976399e8c17SJohn Baldwin freebsd6_freebsd32_aio_read(struct thread *td,
2977399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_read_args *uap)
29783858a1f4SJohn Baldwin {
29793858a1f4SJohn Baldwin 
29803858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29813858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29823858a1f4SJohn Baldwin }
2983399e8c17SJohn Baldwin #endif
29843858a1f4SJohn Baldwin 
29853858a1f4SJohn Baldwin int
29863858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
29873858a1f4SJohn Baldwin {
29883858a1f4SJohn Baldwin 
29893858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29903858a1f4SJohn Baldwin 	    &aiocb32_ops));
29913858a1f4SJohn Baldwin }
29923858a1f4SJohn Baldwin 
2993022ca2fcSAlan Somers int
2994022ca2fcSAlan Somers freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
2995022ca2fcSAlan Somers {
2996022ca2fcSAlan Somers 
2997022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
2998022ca2fcSAlan Somers 	    &aiocb32_ops));
2999022ca2fcSAlan Somers }
3000022ca2fcSAlan Somers 
3001399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30023858a1f4SJohn Baldwin int
3003399e8c17SJohn Baldwin freebsd6_freebsd32_aio_write(struct thread *td,
3004399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_write_args *uap)
30053858a1f4SJohn Baldwin {
30063858a1f4SJohn Baldwin 
30073858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30083858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
30093858a1f4SJohn Baldwin }
3010399e8c17SJohn Baldwin #endif
30113858a1f4SJohn Baldwin 
30123858a1f4SJohn Baldwin int
30133858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
30143858a1f4SJohn Baldwin {
30153858a1f4SJohn Baldwin 
30163858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30173858a1f4SJohn Baldwin 	    &aiocb32_ops));
30183858a1f4SJohn Baldwin }
30193858a1f4SJohn Baldwin 
30203858a1f4SJohn Baldwin int
3021022ca2fcSAlan Somers freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3022022ca2fcSAlan Somers {
3023022ca2fcSAlan Somers 
3024022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3025022ca2fcSAlan Somers 	    &aiocb32_ops));
3026022ca2fcSAlan Somers }
3027022ca2fcSAlan Somers 
3028022ca2fcSAlan Somers int
30296160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
30306160e12cSGleb Smirnoff {
30316160e12cSGleb Smirnoff 
30326160e12cSGleb Smirnoff 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
30336160e12cSGleb Smirnoff 	    &aiocb32_ops));
30346160e12cSGleb Smirnoff }
30356160e12cSGleb Smirnoff 
30366160e12cSGleb Smirnoff int
30373858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td,
30383858a1f4SJohn Baldwin     struct freebsd32_aio_waitcomplete_args *uap)
30393858a1f4SJohn Baldwin {
3040e588eeb1SJohn Baldwin 	struct timespec32 ts32;
30413858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
30423858a1f4SJohn Baldwin 	int error;
30433858a1f4SJohn Baldwin 
30443858a1f4SJohn Baldwin 	if (uap->timeout) {
30453858a1f4SJohn Baldwin 		/* Get timespec struct. */
30463858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
30473858a1f4SJohn Baldwin 		if (error)
30483858a1f4SJohn Baldwin 			return (error);
30493858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
30503858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
30513858a1f4SJohn Baldwin 		tsp = &ts;
30523858a1f4SJohn Baldwin 	} else
30533858a1f4SJohn Baldwin 		tsp = NULL;
30543858a1f4SJohn Baldwin 
30553858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
30563858a1f4SJohn Baldwin 	    &aiocb32_ops));
30573858a1f4SJohn Baldwin }
30583858a1f4SJohn Baldwin 
30593858a1f4SJohn Baldwin int
30603858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
30613858a1f4SJohn Baldwin {
30623858a1f4SJohn Baldwin 
30633858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
30643858a1f4SJohn Baldwin 	    &aiocb32_ops));
30653858a1f4SJohn Baldwin }
30663858a1f4SJohn Baldwin 
3067399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30683858a1f4SJohn Baldwin int
3069399e8c17SJohn Baldwin freebsd6_freebsd32_lio_listio(struct thread *td,
3070399e8c17SJohn Baldwin     struct freebsd6_freebsd32_lio_listio_args *uap)
30713858a1f4SJohn Baldwin {
30723858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30733858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30743858a1f4SJohn Baldwin 	struct osigevent32 osig;
30753858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30763858a1f4SJohn Baldwin 	int error, i, nent;
30773858a1f4SJohn Baldwin 
30783858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30793858a1f4SJohn Baldwin 		return (EINVAL);
30803858a1f4SJohn Baldwin 
30813858a1f4SJohn Baldwin 	nent = uap->nent;
3082913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
30833858a1f4SJohn Baldwin 		return (EINVAL);
30843858a1f4SJohn Baldwin 
30853858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30863858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
30873858a1f4SJohn Baldwin 		if (error)
30883858a1f4SJohn Baldwin 			return (error);
30893858a1f4SJohn Baldwin 		error = convert_old_sigevent32(&osig, &sig);
30903858a1f4SJohn Baldwin 		if (error)
30913858a1f4SJohn Baldwin 			return (error);
30923858a1f4SJohn Baldwin 		sigp = &sig;
30933858a1f4SJohn Baldwin 	} else
30943858a1f4SJohn Baldwin 		sigp = NULL;
30953858a1f4SJohn Baldwin 
30963858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
30973858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
30983858a1f4SJohn Baldwin 	if (error) {
30993858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31003858a1f4SJohn Baldwin 		return (error);
31013858a1f4SJohn Baldwin 	}
31023858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31033858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31043858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31053858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31063858a1f4SJohn Baldwin 
31073858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31083858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31093858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent);
31103858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31113858a1f4SJohn Baldwin 	return (error);
31123858a1f4SJohn Baldwin }
3113399e8c17SJohn Baldwin #endif
31143858a1f4SJohn Baldwin 
31153858a1f4SJohn Baldwin int
31163858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
31173858a1f4SJohn Baldwin {
31183858a1f4SJohn Baldwin 	struct aiocb **acb_list;
31193858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
31203858a1f4SJohn Baldwin 	struct sigevent32 sig32;
31213858a1f4SJohn Baldwin 	uint32_t *acb_list32;
31223858a1f4SJohn Baldwin 	int error, i, nent;
31233858a1f4SJohn Baldwin 
31243858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
31253858a1f4SJohn Baldwin 		return (EINVAL);
31263858a1f4SJohn Baldwin 
31273858a1f4SJohn Baldwin 	nent = uap->nent;
3128913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
31293858a1f4SJohn Baldwin 		return (EINVAL);
31303858a1f4SJohn Baldwin 
31313858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
31323858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig32, sizeof(sig32));
31333858a1f4SJohn Baldwin 		if (error)
31343858a1f4SJohn Baldwin 			return (error);
31353858a1f4SJohn Baldwin 		error = convert_sigevent32(&sig32, &sig);
31363858a1f4SJohn Baldwin 		if (error)
31373858a1f4SJohn Baldwin 			return (error);
31383858a1f4SJohn Baldwin 		sigp = &sig;
31393858a1f4SJohn Baldwin 	} else
31403858a1f4SJohn Baldwin 		sigp = NULL;
31413858a1f4SJohn Baldwin 
31423858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31433858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31443858a1f4SJohn Baldwin 	if (error) {
31453858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31463858a1f4SJohn Baldwin 		return (error);
31473858a1f4SJohn Baldwin 	}
31483858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31493858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31503858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31513858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31523858a1f4SJohn Baldwin 
31533858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31543858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31553858a1f4SJohn Baldwin 	    &aiocb32_ops);
31563858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31573858a1f4SJohn Baldwin 	return (error);
31583858a1f4SJohn Baldwin }
31593858a1f4SJohn Baldwin 
31603858a1f4SJohn Baldwin #endif
3161