xref: /freebsd/sys/kern/vfs_aio.c (revision e9c7ec22877f461f530dfe1cb8e17086300c0ee8)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
4ee877a35SJohn Dyson  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
5ee877a35SJohn Dyson  *
6ee877a35SJohn Dyson  * Redistribution and use in source and binary forms, with or without
7ee877a35SJohn Dyson  * modification, are permitted provided that the following conditions
8ee877a35SJohn Dyson  * are met:
9ee877a35SJohn Dyson  * 1. Redistributions of source code must retain the above copyright
10ee877a35SJohn Dyson  *    notice, this list of conditions and the following disclaimer.
11ee877a35SJohn Dyson  * 2. John S. Dyson's name may not be used to endorse or promote products
12ee877a35SJohn Dyson  *    derived from this software without specific prior written permission.
13ee877a35SJohn Dyson  *
14ee877a35SJohn Dyson  * DISCLAIMER:  This code isn't warranted to do anything useful.  Anything
15ee877a35SJohn Dyson  * bad that happens because of using this software isn't the responsibility
16ee877a35SJohn Dyson  * of the author.  This software is distributed AS-IS.
17ee877a35SJohn Dyson  */
18ee877a35SJohn Dyson 
19ee877a35SJohn Dyson /*
208a6472b7SPeter Dufault  * This file contains support for the POSIX 1003.1B AIO/LIO facility.
21ee877a35SJohn Dyson  */
22ee877a35SJohn Dyson 
23677b542eSDavid E. O'Brien #include <sys/cdefs.h>
24677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
25677b542eSDavid E. O'Brien 
26ee877a35SJohn Dyson #include <sys/param.h>
27ee877a35SJohn Dyson #include <sys/systm.h>
28f591779bSSeigo Tanimura #include <sys/malloc.h>
299626b608SPoul-Henning Kamp #include <sys/bio.h>
30a5c9bce7SBruce Evans #include <sys/buf.h>
314a144410SRobert Watson #include <sys/capsicum.h>
3275b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
33ee877a35SJohn Dyson #include <sys/sysproto.h>
34ee877a35SJohn Dyson #include <sys/filedesc.h>
35ee877a35SJohn Dyson #include <sys/kernel.h>
3677409fe1SPoul-Henning Kamp #include <sys/module.h>
37c9a970a7SAlan Cox #include <sys/kthread.h>
38ee877a35SJohn Dyson #include <sys/fcntl.h>
39ee877a35SJohn Dyson #include <sys/file.h>
40104a9b7eSAlexander Kabaev #include <sys/limits.h>
41fdebd4f0SBruce Evans #include <sys/lock.h>
4235e0e5b3SJohn Baldwin #include <sys/mutex.h>
43ee877a35SJohn Dyson #include <sys/unistd.h>
446aeb05d7STom Rhodes #include <sys/posix4.h>
45ee877a35SJohn Dyson #include <sys/proc.h>
462d2f8ae7SBruce Evans #include <sys/resourcevar.h>
47ee877a35SJohn Dyson #include <sys/signalvar.h>
48496ab053SKonstantin Belousov #include <sys/syscallsubr.h>
49bfbbc4aaSJason Evans #include <sys/protosw.h>
5089f6b863SAttilio Rao #include <sys/rwlock.h>
511ce91824SDavid Xu #include <sys/sema.h>
521ce91824SDavid Xu #include <sys/socket.h>
53bfbbc4aaSJason Evans #include <sys/socketvar.h>
5421d56e9cSAlfred Perlstein #include <sys/syscall.h>
5521d56e9cSAlfred Perlstein #include <sys/sysent.h>
56a624e84fSJohn Dyson #include <sys/sysctl.h>
579c20dc99SJohn Baldwin #include <sys/syslog.h>
58ee99e978SBruce Evans #include <sys/sx.h>
591ce91824SDavid Xu #include <sys/taskqueue.h>
60fd3bf775SJohn Dyson #include <sys/vnode.h>
61fd3bf775SJohn Dyson #include <sys/conf.h>
62cb679c38SJonathan Lemon #include <sys/event.h>
6399eee864SDavid Xu #include <sys/mount.h>
64f743d981SAlexander Motin #include <geom/geom.h>
65ee877a35SJohn Dyson 
661ce91824SDavid Xu #include <machine/atomic.h>
671ce91824SDavid Xu 
68ee877a35SJohn Dyson #include <vm/vm.h>
69f743d981SAlexander Motin #include <vm/vm_page.h>
70ee877a35SJohn Dyson #include <vm/vm_extern.h>
712244ea07SJohn Dyson #include <vm/pmap.h>
722244ea07SJohn Dyson #include <vm/vm_map.h>
7399eee864SDavid Xu #include <vm/vm_object.h>
74c897b813SJeff Roberson #include <vm/uma.h>
75ee877a35SJohn Dyson #include <sys/aio.h>
765aaef07cSJohn Dyson 
77eb8e6d52SEivind Eklund /*
78eb8e6d52SEivind Eklund  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
7999eee864SDavid Xu  * overflow. (XXX will be removed soon.)
80eb8e6d52SEivind Eklund  */
8199eee864SDavid Xu static u_long jobrefid;
822244ea07SJohn Dyson 
8399eee864SDavid Xu /*
8499eee864SDavid Xu  * Counter for aio_fsync.
8599eee864SDavid Xu  */
8699eee864SDavid Xu static uint64_t jobseqno;
8799eee864SDavid Xu 
8884af4da6SJohn Dyson #ifndef MAX_AIO_PER_PROC
892244ea07SJohn Dyson #define MAX_AIO_PER_PROC	32
9084af4da6SJohn Dyson #endif
9184af4da6SJohn Dyson 
9284af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE_PER_PROC
93913b9329SAlan Somers #define MAX_AIO_QUEUE_PER_PROC	256
9484af4da6SJohn Dyson #endif
9584af4da6SJohn Dyson 
9684af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE
97913b9329SAlan Somers #define MAX_AIO_QUEUE		1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
9884af4da6SJohn Dyson #endif
9984af4da6SJohn Dyson 
10084af4da6SJohn Dyson #ifndef MAX_BUF_AIO
10184af4da6SJohn Dyson #define MAX_BUF_AIO		16
10284af4da6SJohn Dyson #endif
10384af4da6SJohn Dyson 
104e603be7aSRobert Watson FEATURE(aio, "Asynchronous I/O");
105c45796d5SAlan Somers SYSCTL_DECL(_p1003_1b);
106e603be7aSRobert Watson 
1073858a1f4SJohn Baldwin static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
108913b9329SAlan Somers static MALLOC_DEFINE(M_AIOS, "aios", "aio_suspend aio control block list");
1093858a1f4SJohn Baldwin 
1107029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1110dd6c035SJohn Baldwin     "Async IO management");
112eb8e6d52SEivind Eklund 
113f3215338SJohn Baldwin static int enable_aio_unsafe = 0;
114f3215338SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
115f3215338SJohn Baldwin     "Permit asynchronous IO on all file types, not just known-safe types");
116f3215338SJohn Baldwin 
1179c20dc99SJohn Baldwin static unsigned int unsafe_warningcnt = 1;
1189c20dc99SJohn Baldwin SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
1199c20dc99SJohn Baldwin     &unsafe_warningcnt, 0,
1209c20dc99SJohn Baldwin     "Warnings that will be triggered upon failed IO requests on unsafe files");
1219c20dc99SJohn Baldwin 
122303b270bSEivind Eklund static int max_aio_procs = MAX_AIO_PROCS;
1230dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
12439314b7dSJohn Baldwin     "Maximum number of kernel processes to use for handling async IO ");
125a624e84fSJohn Dyson 
126eb8e6d52SEivind Eklund static int num_aio_procs = 0;
1270dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
12839314b7dSJohn Baldwin     "Number of presently active kernel processes for async IO");
129a624e84fSJohn Dyson 
130eb8e6d52SEivind Eklund /*
131eb8e6d52SEivind Eklund  * The code will adjust the actual number of AIO processes towards this
132eb8e6d52SEivind Eklund  * number when it gets a chance.
133eb8e6d52SEivind Eklund  */
134eb8e6d52SEivind Eklund static int target_aio_procs = TARGET_AIO_PROCS;
135eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
1360dd6c035SJohn Baldwin     0,
1370dd6c035SJohn Baldwin     "Preferred number of ready kernel processes for async IO");
138a624e84fSJohn Dyson 
139eb8e6d52SEivind Eklund static int max_queue_count = MAX_AIO_QUEUE;
140eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
141eb8e6d52SEivind Eklund     "Maximum number of aio requests to queue, globally");
142a624e84fSJohn Dyson 
143eb8e6d52SEivind Eklund static int num_queue_count = 0;
144eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
145eb8e6d52SEivind Eklund     "Number of queued aio requests");
146a624e84fSJohn Dyson 
147eb8e6d52SEivind Eklund static int num_buf_aio = 0;
148eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
149eb8e6d52SEivind Eklund     "Number of aio requests presently handled by the buf subsystem");
150fd3bf775SJohn Dyson 
1518091e52bSJohn Baldwin static int num_unmapped_aio = 0;
1528091e52bSJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
1538091e52bSJohn Baldwin     0,
1548091e52bSJohn Baldwin     "Number of aio requests presently handled by unmapped I/O buffers");
1558091e52bSJohn Baldwin 
15639314b7dSJohn Baldwin /* Number of async I/O processes in the process of being started */
157a9bf5e37SDavid Xu /* XXX This should be local to aio_aqueue() */
158eb8e6d52SEivind Eklund static int num_aio_resv_start = 0;
159fd3bf775SJohn Dyson 
160eb8e6d52SEivind Eklund static int aiod_lifetime;
161eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
162eb8e6d52SEivind Eklund     "Maximum lifetime for idle aiod");
16384af4da6SJohn Dyson 
164eb8e6d52SEivind Eklund static int max_aio_per_proc = MAX_AIO_PER_PROC;
165eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
1660dd6c035SJohn Baldwin     0,
16786bbef43SJohn Baldwin     "Maximum active aio requests per process");
168eb8e6d52SEivind Eklund 
169eb8e6d52SEivind Eklund static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
170eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
171eb8e6d52SEivind Eklund     &max_aio_queue_per_proc, 0,
17286bbef43SJohn Baldwin     "Maximum queued aio requests per process");
173eb8e6d52SEivind Eklund 
174eb8e6d52SEivind Eklund static int max_buf_aio = MAX_BUF_AIO;
175eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
17686bbef43SJohn Baldwin     "Maximum buf aio requests per process");
177eb8e6d52SEivind Eklund 
178913b9329SAlan Somers /*
179913b9329SAlan Somers  * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
180913b9329SAlan Somers  * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
181913b9329SAlan Somers  * vfs.aio.aio_listio_max.
182913b9329SAlan Somers  */
183c45796d5SAlan Somers SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
184913b9329SAlan Somers     CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
185913b9329SAlan Somers     0, "Maximum aio requests for a single lio_listio call");
186c45796d5SAlan Somers 
187399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
1880972628aSDavid Xu typedef struct oaiocb {
1890972628aSDavid Xu 	int	aio_fildes;		/* File descriptor */
1900972628aSDavid Xu 	off_t	aio_offset;		/* File offset for I/O */
1910972628aSDavid Xu 	volatile void *aio_buf;         /* I/O buffer in process space */
1920972628aSDavid Xu 	size_t	aio_nbytes;		/* Number of bytes for I/O */
1930972628aSDavid Xu 	struct	osigevent aio_sigevent;	/* Signal to deliver */
1940972628aSDavid Xu 	int	aio_lio_opcode;		/* LIO opcode */
1950972628aSDavid Xu 	int	aio_reqprio;		/* Request priority -- ignored */
1960972628aSDavid Xu 	struct	__aiocb_private	_aiocb_private;
1970972628aSDavid Xu } oaiocb_t;
198399e8c17SJohn Baldwin #endif
1990972628aSDavid Xu 
2001aa4c324SDavid Xu /*
2015652770dSJohn Baldwin  * Below is a key of locks used to protect each member of struct kaiocb
2021aa4c324SDavid Xu  * aioliojob and kaioinfo and any backends.
2031aa4c324SDavid Xu  *
2041aa4c324SDavid Xu  * * - need not protected
205759ccccaSDavid Xu  * a - locked by kaioinfo lock
2061aa4c324SDavid Xu  * b - locked by backend lock, the backend lock can be null in some cases,
2071aa4c324SDavid Xu  *     for example, BIO belongs to this type, in this case, proc lock is
2081aa4c324SDavid Xu  *     reused.
2091aa4c324SDavid Xu  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
2101aa4c324SDavid Xu  */
2111aa4c324SDavid Xu 
2121aa4c324SDavid Xu /*
213f3215338SJohn Baldwin  * If the routine that services an AIO request blocks while running in an
214f3215338SJohn Baldwin  * AIO kernel process it can starve other I/O requests.  BIO requests
21572bce9ffSAlan Somers  * queued via aio_qbio() complete asynchronously and do not use AIO kernel
216f3215338SJohn Baldwin  * processes at all.  Socket I/O requests use a separate pool of
217f3215338SJohn Baldwin  * kprocs and also force non-blocking I/O.  Other file I/O requests
218f3215338SJohn Baldwin  * use the generic fo_read/fo_write operations which can block.  The
219f3215338SJohn Baldwin  * fsync and mlock operations can also block while executing.  Ideally
220f3215338SJohn Baldwin  * none of these requests would block while executing.
221f3215338SJohn Baldwin  *
222f3215338SJohn Baldwin  * Note that the service routines cannot toggle O_NONBLOCK in the file
223f3215338SJohn Baldwin  * structure directly while handling a request due to races with
224f3215338SJohn Baldwin  * userland threads.
2251aa4c324SDavid Xu  */
2261aa4c324SDavid Xu 
22748dac059SAlan Cox /* jobflags */
228f3215338SJohn Baldwin #define	KAIOCB_QUEUEING		0x01
229f3215338SJohn Baldwin #define	KAIOCB_CANCELLED	0x02
230f3215338SJohn Baldwin #define	KAIOCB_CANCELLING	0x04
2315652770dSJohn Baldwin #define	KAIOCB_CHECKSYNC	0x08
232f3215338SJohn Baldwin #define	KAIOCB_CLEARED		0x10
233f3215338SJohn Baldwin #define	KAIOCB_FINISHED		0x20
23448dac059SAlan Cox 
2352244ea07SJohn Dyson /*
2362244ea07SJohn Dyson  * AIO process info
2372244ea07SJohn Dyson  */
23884af4da6SJohn Dyson #define AIOP_FREE	0x1			/* proc on free queue */
23984af4da6SJohn Dyson 
24039314b7dSJohn Baldwin struct aioproc {
24139314b7dSJohn Baldwin 	int	aioprocflags;			/* (c) AIO proc flags */
24239314b7dSJohn Baldwin 	TAILQ_ENTRY(aioproc) list;		/* (c) list of processes */
24339314b7dSJohn Baldwin 	struct	proc *aioproc;			/* (*) the AIO proc */
2442244ea07SJohn Dyson };
2452244ea07SJohn Dyson 
24684af4da6SJohn Dyson /*
24784af4da6SJohn Dyson  * data-structure for lio signal management
24884af4da6SJohn Dyson  */
2491ce91824SDavid Xu struct aioliojob {
2501aa4c324SDavid Xu 	int	lioj_flags;			/* (a) listio flags */
2515c5005ecSKonstantin Belousov 	int	lioj_count;			/* (a) count of jobs */
2525c5005ecSKonstantin Belousov 	int	lioj_finished_count;		/* (a) count of finished jobs */
2531aa4c324SDavid Xu 	struct	sigevent lioj_signal;		/* (a) signal on all I/O done */
2541aa4c324SDavid Xu 	TAILQ_ENTRY(aioliojob) lioj_list;	/* (a) lio list */
2551aa4c324SDavid Xu 	struct	knlist klist;			/* (a) list of knotes */
2561aa4c324SDavid Xu 	ksiginfo_t lioj_ksi;			/* (a) Realtime signal info */
25784af4da6SJohn Dyson };
2581ce91824SDavid Xu 
25984af4da6SJohn Dyson #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
26084af4da6SJohn Dyson #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
26169cd28daSDoug Ambrisko #define LIOJ_KEVENT_POSTED	0x4	/* kevent triggered */
26284af4da6SJohn Dyson 
26384af4da6SJohn Dyson /*
26484af4da6SJohn Dyson  * per process aio data structure
26584af4da6SJohn Dyson  */
2662244ea07SJohn Dyson struct kaioinfo {
267759ccccaSDavid Xu 	struct	mtx kaio_mtx;		/* the lock to protect this struct */
2681aa4c324SDavid Xu 	int	kaio_flags;		/* (a) per process kaio flags */
2691aa4c324SDavid Xu 	int	kaio_active_count;	/* (c) number of currently used AIOs */
2701aa4c324SDavid Xu 	int	kaio_count;		/* (a) size of AIO queue */
27172bce9ffSAlan Somers 	int	kaio_buffer_count;	/* (a) number of bio buffers */
2725652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_all;	/* (a) all AIOs in a process */
2735652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_done;	/* (a) done queue for process */
2741aa4c324SDavid Xu 	TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
2755652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_jobqueue;	/* (a) job queue for process */
2765652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncqueue;	/* (a) queue for aio_fsync */
277f3215338SJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncready;  /* (a) second q for aio_fsync */
27839314b7dSJohn Baldwin 	struct	task kaio_task;		/* (*) task to kick aio processes */
279f3215338SJohn Baldwin 	struct	task kaio_sync_task;	/* (*) task to schedule fsync jobs */
2802244ea07SJohn Dyson };
2812244ea07SJohn Dyson 
282759ccccaSDavid Xu #define AIO_LOCK(ki)		mtx_lock(&(ki)->kaio_mtx)
283759ccccaSDavid Xu #define AIO_UNLOCK(ki)		mtx_unlock(&(ki)->kaio_mtx)
284759ccccaSDavid Xu #define AIO_LOCK_ASSERT(ki, f)	mtx_assert(&(ki)->kaio_mtx, (f))
285759ccccaSDavid Xu #define AIO_MTX(ki)		(&(ki)->kaio_mtx)
286759ccccaSDavid Xu 
28784af4da6SJohn Dyson #define KAIO_RUNDOWN	0x1	/* process is being run down */
2880dd6c035SJohn Baldwin #define KAIO_WAKEUP	0x2	/* wakeup process when AIO completes */
289fd3bf775SJohn Dyson 
2903858a1f4SJohn Baldwin /*
2913858a1f4SJohn Baldwin  * Operations used to interact with userland aio control blocks.
2923858a1f4SJohn Baldwin  * Different ABIs provide their own operations.
2933858a1f4SJohn Baldwin  */
2943858a1f4SJohn Baldwin struct aiocb_ops {
295022ca2fcSAlan Somers 	int	(*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
2963858a1f4SJohn Baldwin 	long	(*fetch_status)(struct aiocb *ujob);
2973858a1f4SJohn Baldwin 	long	(*fetch_error)(struct aiocb *ujob);
2983858a1f4SJohn Baldwin 	int	(*store_status)(struct aiocb *ujob, long status);
2993858a1f4SJohn Baldwin 	int	(*store_error)(struct aiocb *ujob, long error);
3003858a1f4SJohn Baldwin 	int	(*store_kernelinfo)(struct aiocb *ujob, long jobref);
3013858a1f4SJohn Baldwin 	int	(*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
3023858a1f4SJohn Baldwin };
3033858a1f4SJohn Baldwin 
30439314b7dSJohn Baldwin static TAILQ_HEAD(,aioproc) aio_freeproc;		/* (c) Idle daemons */
3051ce91824SDavid Xu static struct sema aio_newproc_sem;
3061ce91824SDavid Xu static struct mtx aio_job_mtx;
3075652770dSJohn Baldwin static TAILQ_HEAD(,kaiocb) aio_jobs;			/* (c) Async job list */
3081ce91824SDavid Xu static struct unrhdr *aiod_unr;
3092244ea07SJohn Dyson 
310022ca2fcSAlan Somers static void	aio_biocleanup(struct bio *bp);
3116a1162d4SAlexander Leidinger void		aio_init_aioinfo(struct proc *p);
312723d37c0SKonstantin Belousov static int	aio_onceonly(void);
3135652770dSJohn Baldwin static int	aio_free_entry(struct kaiocb *job);
3145652770dSJohn Baldwin static void	aio_process_rw(struct kaiocb *job);
3155652770dSJohn Baldwin static void	aio_process_sync(struct kaiocb *job);
3165652770dSJohn Baldwin static void	aio_process_mlock(struct kaiocb *job);
317f3215338SJohn Baldwin static void	aio_schedule_fsync(void *context, int pending);
3181ce91824SDavid Xu static int	aio_newproc(int *);
3195652770dSJohn Baldwin int		aio_aqueue(struct thread *td, struct aiocb *ujob,
3203858a1f4SJohn Baldwin 		    struct aioliojob *lio, int type, struct aiocb_ops *ops);
321f3215338SJohn Baldwin static int	aio_queue_file(struct file *fp, struct kaiocb *job);
32272bce9ffSAlan Somers static void	aio_biowakeup(struct bio *bp);
32375b8b3b2SJohn Baldwin static void	aio_proc_rundown(void *arg, struct proc *p);
3240dd6c035SJohn Baldwin static void	aio_proc_rundown_exec(void *arg, struct proc *p,
3250dd6c035SJohn Baldwin 		    struct image_params *imgp);
32672bce9ffSAlan Somers static int	aio_qbio(struct proc *p, struct kaiocb *job);
3271ce91824SDavid Xu static void	aio_daemon(void *param);
328f3215338SJohn Baldwin static void	aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
329005ce8e4SJohn Baldwin static bool	aio_clear_cancel_function_locked(struct kaiocb *job);
330dbbccfe9SDavid Xu static int	aio_kick(struct proc *userp);
33199eee864SDavid Xu static void	aio_kick_nowait(struct proc *userp);
33299eee864SDavid Xu static void	aio_kick_helper(void *context, int pending);
33321d56e9cSAlfred Perlstein static int	filt_aioattach(struct knote *kn);
33421d56e9cSAlfred Perlstein static void	filt_aiodetach(struct knote *kn);
33521d56e9cSAlfred Perlstein static int	filt_aio(struct knote *kn, long hint);
33669cd28daSDoug Ambrisko static int	filt_lioattach(struct knote *kn);
33769cd28daSDoug Ambrisko static void	filt_liodetach(struct knote *kn);
33869cd28daSDoug Ambrisko static int	filt_lio(struct knote *kn, long hint);
3392244ea07SJohn Dyson 
340eb8e6d52SEivind Eklund /*
341eb8e6d52SEivind Eklund  * Zones for:
342eb8e6d52SEivind Eklund  * 	kaio	Per process async io info
34339314b7dSJohn Baldwin  *	aiop	async io process data
344eb8e6d52SEivind Eklund  *	aiocb	async io jobs
345eb8e6d52SEivind Eklund  *	aiolio	list io jobs
346eb8e6d52SEivind Eklund  */
347913b9329SAlan Somers static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiolio_zone;
348fd3bf775SJohn Dyson 
349eb8e6d52SEivind Eklund /* kqueue filters for aio */
350e76d823bSRobert Watson static struct filterops aio_filtops = {
351e76d823bSRobert Watson 	.f_isfd = 0,
352e76d823bSRobert Watson 	.f_attach = filt_aioattach,
353e76d823bSRobert Watson 	.f_detach = filt_aiodetach,
354e76d823bSRobert Watson 	.f_event = filt_aio,
355e76d823bSRobert Watson };
356e76d823bSRobert Watson static struct filterops lio_filtops = {
357e76d823bSRobert Watson 	.f_isfd = 0,
358e76d823bSRobert Watson 	.f_attach = filt_lioattach,
359e76d823bSRobert Watson 	.f_detach = filt_liodetach,
360e76d823bSRobert Watson 	.f_event = filt_lio
361e76d823bSRobert Watson };
36221d56e9cSAlfred Perlstein 
36375b8b3b2SJohn Baldwin static eventhandler_tag exit_tag, exec_tag;
36475b8b3b2SJohn Baldwin 
365c85650caSJohn Baldwin TASKQUEUE_DEFINE_THREAD(aiod_kick);
3661ce91824SDavid Xu 
367eb8e6d52SEivind Eklund /*
368eb8e6d52SEivind Eklund  * Main operations function for use as a kernel module.
369eb8e6d52SEivind Eklund  */
37021d56e9cSAlfred Perlstein static int
37121d56e9cSAlfred Perlstein aio_modload(struct module *module, int cmd, void *arg)
37221d56e9cSAlfred Perlstein {
37321d56e9cSAlfred Perlstein 	int error = 0;
37421d56e9cSAlfred Perlstein 
37521d56e9cSAlfred Perlstein 	switch (cmd) {
37621d56e9cSAlfred Perlstein 	case MOD_LOAD:
37721d56e9cSAlfred Perlstein 		aio_onceonly();
37821d56e9cSAlfred Perlstein 		break;
37921d56e9cSAlfred Perlstein 	case MOD_SHUTDOWN:
38021d56e9cSAlfred Perlstein 		break;
38121d56e9cSAlfred Perlstein 	default:
382f3215338SJohn Baldwin 		error = EOPNOTSUPP;
38321d56e9cSAlfred Perlstein 		break;
38421d56e9cSAlfred Perlstein 	}
38521d56e9cSAlfred Perlstein 	return (error);
38621d56e9cSAlfred Perlstein }
38721d56e9cSAlfred Perlstein 
38821d56e9cSAlfred Perlstein static moduledata_t aio_mod = {
38921d56e9cSAlfred Perlstein 	"aio",
39021d56e9cSAlfred Perlstein 	&aio_modload,
39121d56e9cSAlfred Perlstein 	NULL
39221d56e9cSAlfred Perlstein };
39321d56e9cSAlfred Perlstein 
394399e8c17SJohn Baldwin DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
39521d56e9cSAlfred Perlstein MODULE_VERSION(aio, 1);
39621d56e9cSAlfred Perlstein 
397fd3bf775SJohn Dyson /*
3982244ea07SJohn Dyson  * Startup initialization
3992244ea07SJohn Dyson  */
400723d37c0SKonstantin Belousov static int
40121d56e9cSAlfred Perlstein aio_onceonly(void)
402fd3bf775SJohn Dyson {
40321d56e9cSAlfred Perlstein 
40475b8b3b2SJohn Baldwin 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
40575b8b3b2SJohn Baldwin 	    EVENTHANDLER_PRI_ANY);
4060dd6c035SJohn Baldwin 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
4070dd6c035SJohn Baldwin 	    NULL, EVENTHANDLER_PRI_ANY);
40821d56e9cSAlfred Perlstein 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
40969cd28daSDoug Ambrisko 	kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
4102244ea07SJohn Dyson 	TAILQ_INIT(&aio_freeproc);
4111ce91824SDavid Xu 	sema_init(&aio_newproc_sem, 0, "aio_new_proc");
4121ce91824SDavid Xu 	mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
4132244ea07SJohn Dyson 	TAILQ_INIT(&aio_jobs);
4141ce91824SDavid Xu 	aiod_unr = new_unrhdr(1, INT_MAX, NULL);
415c897b813SJeff Roberson 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
416c897b813SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
41739314b7dSJohn Baldwin 	aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
418c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
4195652770dSJohn Baldwin 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
420c897b813SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
4211ce91824SDavid Xu 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
422c897b813SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
42384af4da6SJohn Dyson 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
424fd3bf775SJohn Dyson 	jobrefid = 1;
425399e8c17SJohn Baldwin 	p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
42686d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
42786d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
428723d37c0SKonstantin Belousov 
429723d37c0SKonstantin Belousov 	return (0);
4302244ea07SJohn Dyson }
4312244ea07SJohn Dyson 
432eb8e6d52SEivind Eklund /*
433bfbbc4aaSJason Evans  * Init the per-process aioinfo structure.  The aioinfo limits are set
434bfbbc4aaSJason Evans  * per-process for user limit (resource) management.
4352244ea07SJohn Dyson  */
4366a1162d4SAlexander Leidinger void
437fd3bf775SJohn Dyson aio_init_aioinfo(struct proc *p)
438fd3bf775SJohn Dyson {
4392244ea07SJohn Dyson 	struct kaioinfo *ki;
440ac41f2efSAlfred Perlstein 
441a163d034SWarner Losh 	ki = uma_zalloc(kaio_zone, M_WAITOK);
4429889bbacSKonstantin Belousov 	mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
44384af4da6SJohn Dyson 	ki->kaio_flags = 0;
4442244ea07SJohn Dyson 	ki->kaio_active_count = 0;
4451ce91824SDavid Xu 	ki->kaio_count = 0;
446fd3bf775SJohn Dyson 	ki->kaio_buffer_count = 0;
4471ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_all);
4481ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_done);
4492244ea07SJohn Dyson 	TAILQ_INIT(&ki->kaio_jobqueue);
45084af4da6SJohn Dyson 	TAILQ_INIT(&ki->kaio_liojoblist);
45199eee864SDavid Xu 	TAILQ_INIT(&ki->kaio_syncqueue);
452f3215338SJohn Baldwin 	TAILQ_INIT(&ki->kaio_syncready);
45399eee864SDavid Xu 	TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
454f3215338SJohn Baldwin 	TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
4553999ebe3SAlan Cox 	PROC_LOCK(p);
4563999ebe3SAlan Cox 	if (p->p_aioinfo == NULL) {
4573999ebe3SAlan Cox 		p->p_aioinfo = ki;
4583999ebe3SAlan Cox 		PROC_UNLOCK(p);
4593999ebe3SAlan Cox 	} else {
4603999ebe3SAlan Cox 		PROC_UNLOCK(p);
461759ccccaSDavid Xu 		mtx_destroy(&ki->kaio_mtx);
4623999ebe3SAlan Cox 		uma_zfree(kaio_zone, ki);
4632244ea07SJohn Dyson 	}
464bfbbc4aaSJason Evans 
46522035f47SOleksandr Tymoshenko 	while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
4661ce91824SDavid Xu 		aio_newproc(NULL);
4672244ea07SJohn Dyson }
4682244ea07SJohn Dyson 
4694c0fb2cfSDavid Xu static int
4706814c2daSKonstantin Belousov aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
4714c0fb2cfSDavid Xu {
472cf7d9a8cSDavid Xu 	struct thread *td;
473cf7d9a8cSDavid Xu 	int error;
474759ccccaSDavid Xu 
475cf7d9a8cSDavid Xu 	error = sigev_findtd(p, sigev, &td);
476cf7d9a8cSDavid Xu 	if (error)
477cf7d9a8cSDavid Xu 		return (error);
4784c0fb2cfSDavid Xu 	if (!KSI_ONQ(ksi)) {
479cf7d9a8cSDavid Xu 		ksiginfo_set_sigev(ksi, sigev);
4804c0fb2cfSDavid Xu 		ksi->ksi_code = SI_ASYNCIO;
4816814c2daSKonstantin Belousov 		ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
482cf7d9a8cSDavid Xu 		tdsendsignal(p, td, ksi->ksi_signo, ksi);
4834c0fb2cfSDavid Xu 	}
484759ccccaSDavid Xu 	PROC_UNLOCK(p);
485cf7d9a8cSDavid Xu 	return (error);
4864c0fb2cfSDavid Xu }
4874c0fb2cfSDavid Xu 
4882244ea07SJohn Dyson /*
489bfbbc4aaSJason Evans  * Free a job entry.  Wait for completion if it is currently active, but don't
490bfbbc4aaSJason Evans  * delay forever.  If we delay, we return a flag that says that we have to
491bfbbc4aaSJason Evans  * restart the queue scan.
4922244ea07SJohn Dyson  */
49388ed460eSAlan Cox static int
4945652770dSJohn Baldwin aio_free_entry(struct kaiocb *job)
495fd3bf775SJohn Dyson {
4962244ea07SJohn Dyson 	struct kaioinfo *ki;
4971ce91824SDavid Xu 	struct aioliojob *lj;
4982244ea07SJohn Dyson 	struct proc *p;
4992244ea07SJohn Dyson 
5005652770dSJohn Baldwin 	p = job->userproc;
5011ce91824SDavid Xu 	MPASS(curproc == p);
5022244ea07SJohn Dyson 	ki = p->p_aioinfo;
5031ce91824SDavid Xu 	MPASS(ki != NULL);
5041ce91824SDavid Xu 
505759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
506f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
507759ccccaSDavid Xu 
5081ce91824SDavid Xu 	atomic_subtract_int(&num_queue_count, 1);
5091ce91824SDavid Xu 
5101ce91824SDavid Xu 	ki->kaio_count--;
5111ce91824SDavid Xu 	MPASS(ki->kaio_count >= 0);
5121ce91824SDavid Xu 
5135652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_done, job, plist);
5145652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_all, job, allist);
51527b8220dSDavid Xu 
5165652770dSJohn Baldwin 	lj = job->lio;
51784af4da6SJohn Dyson 	if (lj) {
5181ce91824SDavid Xu 		lj->lioj_count--;
5191ce91824SDavid Xu 		lj->lioj_finished_count--;
5201ce91824SDavid Xu 
521a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
5221ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
5231ce91824SDavid Xu 			/* lio is going away, we need to destroy any knotes */
5241ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
525759ccccaSDavid Xu 			PROC_LOCK(p);
5261ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
527759ccccaSDavid Xu 			PROC_UNLOCK(p);
5281ce91824SDavid Xu 			uma_zfree(aiolio_zone, lj);
52984af4da6SJohn Dyson 		}
53084af4da6SJohn Dyson 	}
5311ce91824SDavid Xu 
5325652770dSJohn Baldwin 	/* job is going away, we need to destroy any knotes */
5335652770dSJohn Baldwin 	knlist_delete(&job->klist, curthread, 1);
534759ccccaSDavid Xu 	PROC_LOCK(p);
5355652770dSJohn Baldwin 	sigqueue_take(&job->ksi);
536759ccccaSDavid Xu 	PROC_UNLOCK(p);
5371ce91824SDavid Xu 
538759ccccaSDavid Xu 	AIO_UNLOCK(ki);
5392a522eb9SJohn Baldwin 
5402a522eb9SJohn Baldwin 	/*
5412a522eb9SJohn Baldwin 	 * The thread argument here is used to find the owning process
5422a522eb9SJohn Baldwin 	 * and is also passed to fo_close() which may pass it to various
5432a522eb9SJohn Baldwin 	 * places such as devsw close() routines.  Because of that, we
5442a522eb9SJohn Baldwin 	 * need a thread pointer from the process owning the job that is
5452a522eb9SJohn Baldwin 	 * persistent and won't disappear out from under us or move to
5462a522eb9SJohn Baldwin 	 * another process.
5472a522eb9SJohn Baldwin 	 *
5482a522eb9SJohn Baldwin 	 * Currently, all the callers of this function call it to remove
5495652770dSJohn Baldwin 	 * a kaiocb from the current process' job list either via a
5502a522eb9SJohn Baldwin 	 * syscall or due to the current process calling exit() or
5512a522eb9SJohn Baldwin 	 * execve().  Thus, we know that p == curproc.  We also know that
5522a522eb9SJohn Baldwin 	 * curthread can't exit since we are curthread.
5532a522eb9SJohn Baldwin 	 *
5542a522eb9SJohn Baldwin 	 * Therefore, we use curthread as the thread to pass to
5552a522eb9SJohn Baldwin 	 * knlist_delete().  This does mean that it is possible for the
5562a522eb9SJohn Baldwin 	 * thread pointer at close time to differ from the thread pointer
5572a522eb9SJohn Baldwin 	 * at open time, but this is already true of file descriptors in
5582a522eb9SJohn Baldwin 	 * a multithreaded process.
559b40ce416SJulian Elischer 	 */
5605652770dSJohn Baldwin 	if (job->fd_file)
5615652770dSJohn Baldwin 		fdrop(job->fd_file, curthread);
5625652770dSJohn Baldwin 	crfree(job->cred);
563022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
564022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
5655652770dSJohn Baldwin 	uma_zfree(aiocb_zone, job);
566759ccccaSDavid Xu 	AIO_LOCK(ki);
5671ce91824SDavid Xu 
568ac41f2efSAlfred Perlstein 	return (0);
5692244ea07SJohn Dyson }
5702244ea07SJohn Dyson 
571993182e5SAlexander Leidinger static void
5720dd6c035SJohn Baldwin aio_proc_rundown_exec(void *arg, struct proc *p,
5730dd6c035SJohn Baldwin     struct image_params *imgp __unused)
574993182e5SAlexander Leidinger {
575993182e5SAlexander Leidinger    	aio_proc_rundown(arg, p);
576993182e5SAlexander Leidinger }
577993182e5SAlexander Leidinger 
578f3215338SJohn Baldwin static int
579f3215338SJohn Baldwin aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
580f3215338SJohn Baldwin {
581f3215338SJohn Baldwin 	aio_cancel_fn_t *func;
582f3215338SJohn Baldwin 	int cancelled;
583f3215338SJohn Baldwin 
584f3215338SJohn Baldwin 	AIO_LOCK_ASSERT(ki, MA_OWNED);
585f3215338SJohn Baldwin 	if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
586f3215338SJohn Baldwin 		return (0);
587f3215338SJohn Baldwin 	MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
588f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLED;
589f3215338SJohn Baldwin 
590f3215338SJohn Baldwin 	func = job->cancel_fn;
591f3215338SJohn Baldwin 
592f3215338SJohn Baldwin 	/*
593f3215338SJohn Baldwin 	 * If there is no cancel routine, just leave the job marked as
594f3215338SJohn Baldwin 	 * cancelled.  The job should be in active use by a caller who
595f3215338SJohn Baldwin 	 * should complete it normally or when it fails to install a
596f3215338SJohn Baldwin 	 * cancel routine.
597f3215338SJohn Baldwin 	 */
598f3215338SJohn Baldwin 	if (func == NULL)
599f3215338SJohn Baldwin 		return (0);
600f3215338SJohn Baldwin 
601f3215338SJohn Baldwin 	/*
602f3215338SJohn Baldwin 	 * Set the CANCELLING flag so that aio_complete() will defer
603f3215338SJohn Baldwin 	 * completions of this job.  This prevents the job from being
604f3215338SJohn Baldwin 	 * freed out from under the cancel callback.  After the
605f3215338SJohn Baldwin 	 * callback any deferred completion (whether from the callback
606f3215338SJohn Baldwin 	 * or any other source) will be completed.
607f3215338SJohn Baldwin 	 */
608f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLING;
609f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
610f3215338SJohn Baldwin 	func(job);
611f3215338SJohn Baldwin 	AIO_LOCK(ki);
612f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_CANCELLING;
613f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
614f3215338SJohn Baldwin 		cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
615f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
616f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
617f3215338SJohn Baldwin 	} else {
618f3215338SJohn Baldwin 		/*
619f3215338SJohn Baldwin 		 * The cancel callback might have scheduled an
620f3215338SJohn Baldwin 		 * operation to cancel this request, but it is
621f3215338SJohn Baldwin 		 * only counted as cancelled if the request is
622f3215338SJohn Baldwin 		 * cancelled when the callback returns.
623f3215338SJohn Baldwin 		 */
624f3215338SJohn Baldwin 		cancelled = 0;
625f3215338SJohn Baldwin 	}
626f3215338SJohn Baldwin 	return (cancelled);
627f3215338SJohn Baldwin }
628f3215338SJohn Baldwin 
6292244ea07SJohn Dyson /*
6302244ea07SJohn Dyson  * Rundown the jobs for a given process.
6312244ea07SJohn Dyson  */
63221d56e9cSAlfred Perlstein static void
63375b8b3b2SJohn Baldwin aio_proc_rundown(void *arg, struct proc *p)
634fd3bf775SJohn Dyson {
6352244ea07SJohn Dyson 	struct kaioinfo *ki;
6361ce91824SDavid Xu 	struct aioliojob *lj;
6375652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
6382244ea07SJohn Dyson 
6392a522eb9SJohn Baldwin 	KASSERT(curthread->td_proc == p,
6402a522eb9SJohn Baldwin 	    ("%s: called on non-curproc", __func__));
6412244ea07SJohn Dyson 	ki = p->p_aioinfo;
6422244ea07SJohn Dyson 	if (ki == NULL)
6432244ea07SJohn Dyson 		return;
6442244ea07SJohn Dyson 
645759ccccaSDavid Xu 	AIO_LOCK(ki);
64627b8220dSDavid Xu 	ki->kaio_flags |= KAIO_RUNDOWN;
6471ce91824SDavid Xu 
6481ce91824SDavid Xu restart:
649a624e84fSJohn Dyson 
650bfbbc4aaSJason Evans 	/*
6511ce91824SDavid Xu 	 * Try to cancel all pending requests. This code simulates
6521ce91824SDavid Xu 	 * aio_cancel on all pending I/O requests.
653bfbbc4aaSJason Evans 	 */
6545652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
655f3215338SJohn Baldwin 		aio_cancel_job(p, ki, job);
6562244ea07SJohn Dyson 	}
65784af4da6SJohn Dyson 
6581ce91824SDavid Xu 	/* Wait for all running I/O to be finished */
659f3215338SJohn Baldwin 	if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
66084af4da6SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
661759ccccaSDavid Xu 		msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
6621ce91824SDavid Xu 		goto restart;
66384af4da6SJohn Dyson 	}
66484af4da6SJohn Dyson 
6651ce91824SDavid Xu 	/* Free all completed I/O requests. */
6665652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
6675652770dSJohn Baldwin 		aio_free_entry(job);
66884af4da6SJohn Dyson 
6691ce91824SDavid Xu 	while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
670a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
67184af4da6SJohn Dyson 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
6721ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
673759ccccaSDavid Xu 			PROC_LOCK(p);
6741ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
675759ccccaSDavid Xu 			PROC_UNLOCK(p);
676c897b813SJeff Roberson 			uma_zfree(aiolio_zone, lj);
677f4f0ecefSJohn Dyson 		} else {
678a9bf5e37SDavid Xu 			panic("LIO job not cleaned up: C:%d, FC:%d\n",
679a9bf5e37SDavid Xu 			    lj->lioj_count, lj->lioj_finished_count);
68084af4da6SJohn Dyson 		}
681f4f0ecefSJohn Dyson 	}
682759ccccaSDavid Xu 	AIO_UNLOCK(ki);
683c85650caSJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
684f3215338SJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
6855114048bSKonstantin Belousov 	mtx_destroy(&ki->kaio_mtx);
686c897b813SJeff Roberson 	uma_zfree(kaio_zone, ki);
687a624e84fSJohn Dyson 	p->p_aioinfo = NULL;
6882244ea07SJohn Dyson }
6892244ea07SJohn Dyson 
6902244ea07SJohn Dyson /*
691bfbbc4aaSJason Evans  * Select a job to run (called by an AIO daemon).
6922244ea07SJohn Dyson  */
6935652770dSJohn Baldwin static struct kaiocb *
69439314b7dSJohn Baldwin aio_selectjob(struct aioproc *aiop)
695fd3bf775SJohn Dyson {
6965652770dSJohn Baldwin 	struct kaiocb *job;
697bfbbc4aaSJason Evans 	struct kaioinfo *ki;
698bfbbc4aaSJason Evans 	struct proc *userp;
6992244ea07SJohn Dyson 
7001ce91824SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
701f3215338SJohn Baldwin restart:
7025652770dSJohn Baldwin 	TAILQ_FOREACH(job, &aio_jobs, list) {
7035652770dSJohn Baldwin 		userp = job->userproc;
7042244ea07SJohn Dyson 		ki = userp->p_aioinfo;
7052244ea07SJohn Dyson 
70686bbef43SJohn Baldwin 		if (ki->kaio_active_count < max_aio_per_proc) {
7075652770dSJohn Baldwin 			TAILQ_REMOVE(&aio_jobs, job, list);
708f3215338SJohn Baldwin 			if (!aio_clear_cancel_function(job))
709f3215338SJohn Baldwin 				goto restart;
710f3215338SJohn Baldwin 
7111ce91824SDavid Xu 			/* Account for currently active jobs. */
7121ce91824SDavid Xu 			ki->kaio_active_count++;
7131ce91824SDavid Xu 			break;
7141ce91824SDavid Xu 		}
7151ce91824SDavid Xu 	}
7165652770dSJohn Baldwin 	return (job);
7172244ea07SJohn Dyson }
7182244ea07SJohn Dyson 
7192244ea07SJohn Dyson /*
7200dd6c035SJohn Baldwin  * Move all data to a permanent storage device.  This code
721801ac943SThomas Munro  * simulates the fsync and fdatasync syscalls.
72299eee864SDavid Xu  */
72399eee864SDavid Xu static int
724801ac943SThomas Munro aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
72599eee864SDavid Xu {
72699eee864SDavid Xu 	struct mount *mp;
727922bee44SKonstantin Belousov 	vm_object_t obj;
72899eee864SDavid Xu 	int error;
72999eee864SDavid Xu 
730922bee44SKonstantin Belousov 	for (;;) {
731922bee44SKonstantin Belousov 		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
732922bee44SKonstantin Belousov 		if (error != 0)
733922bee44SKonstantin Belousov 			break;
734cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
735922bee44SKonstantin Belousov 		obj = vp->v_object;
736922bee44SKonstantin Belousov 		if (obj != NULL) {
737922bee44SKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
738922bee44SKonstantin Belousov 			vm_object_page_clean(obj, 0, 0, 0);
739922bee44SKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
74099eee864SDavid Xu 		}
741801ac943SThomas Munro 		if (op == LIO_DSYNC)
742801ac943SThomas Munro 			error = VOP_FDATASYNC(vp, td);
743801ac943SThomas Munro 		else
74499eee864SDavid Xu 			error = VOP_FSYNC(vp, MNT_WAIT, td);
74599eee864SDavid Xu 
746b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
74799eee864SDavid Xu 		vn_finished_write(mp);
7482933a7caSKonstantin Belousov 		if (error != ERELOOKUP)
749922bee44SKonstantin Belousov 			break;
750922bee44SKonstantin Belousov 	}
75199eee864SDavid Xu 	return (error);
75299eee864SDavid Xu }
75399eee864SDavid Xu 
75499eee864SDavid Xu /*
755f95c13dbSGleb Smirnoff  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
75672bce9ffSAlan Somers  * does the I/O request for the non-bio version of the operations.  The normal
75772bce9ffSAlan Somers  * vn operations are used, and this code should work in all instances for every
75872bce9ffSAlan Somers  * type of file, including pipes, sockets, fifos, and regular files.
7591ce91824SDavid Xu  *
7601aa4c324SDavid Xu  * XXX I don't think it works well for socket, pipe, and fifo.
7612244ea07SJohn Dyson  */
76288ed460eSAlan Cox static void
7635652770dSJohn Baldwin aio_process_rw(struct kaiocb *job)
764fd3bf775SJohn Dyson {
765f8f750c5SRobert Watson 	struct ucred *td_savedcred;
766b40ce416SJulian Elischer 	struct thread *td;
7672244ea07SJohn Dyson 	struct file *fp;
768bb430bc7SJohn Baldwin 	ssize_t cnt;
769b1012d80SJohn Baldwin 	long msgsnd_st, msgsnd_end;
770b1012d80SJohn Baldwin 	long msgrcv_st, msgrcv_end;
771b1012d80SJohn Baldwin 	long oublock_st, oublock_end;
772b1012d80SJohn Baldwin 	long inblock_st, inblock_end;
773022ca2fcSAlan Somers 	int error, opcode;
7742244ea07SJohn Dyson 
7755652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
776022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_READV ||
777022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITE ||
778022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITEV,
7795652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
780f95c13dbSGleb Smirnoff 
781f3215338SJohn Baldwin 	aio_switch_vmspace(job);
782b40ce416SJulian Elischer 	td = curthread;
783f8f750c5SRobert Watson 	td_savedcred = td->td_ucred;
7845652770dSJohn Baldwin 	td->td_ucred = job->cred;
785022ca2fcSAlan Somers 	job->uiop->uio_td = td;
7865652770dSJohn Baldwin 	fp = job->fd_file;
787bfbbc4aaSJason Evans 
788022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
789022ca2fcSAlan Somers 	cnt = job->uiop->uio_resid;
7902244ea07SJohn Dyson 
791b1012d80SJohn Baldwin 	msgrcv_st = td->td_ru.ru_msgrcv;
792b1012d80SJohn Baldwin 	msgsnd_st = td->td_ru.ru_msgsnd;
7931c4bcd05SJeff Roberson 	inblock_st = td->td_ru.ru_inblock;
7941c4bcd05SJeff Roberson 	oublock_st = td->td_ru.ru_oublock;
795b1012d80SJohn Baldwin 
796279d7226SMatthew Dillon 	/*
797a9bf5e37SDavid Xu 	 * aio_aqueue() acquires a reference to the file that is
7989b16adc1SAlan Cox 	 * released in aio_free_entry().
799279d7226SMatthew Dillon 	 */
800022ca2fcSAlan Somers 	if (opcode == LIO_READ || opcode == LIO_READV) {
801022ca2fcSAlan Somers 		if (job->uiop->uio_resid == 0)
8025114048bSKonstantin Belousov 			error = 0;
8035114048bSKonstantin Belousov 		else
804022ca2fcSAlan Somers 			error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
805022ca2fcSAlan Somers 			    td);
8062244ea07SJohn Dyson 	} else {
8076d53aa62SDavid Xu 		if (fp->f_type == DTYPE_VNODE)
8086d53aa62SDavid Xu 			bwillwrite();
809022ca2fcSAlan Somers 		error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
8102244ea07SJohn Dyson 	}
811b1012d80SJohn Baldwin 	msgrcv_end = td->td_ru.ru_msgrcv;
812b1012d80SJohn Baldwin 	msgsnd_end = td->td_ru.ru_msgsnd;
8131c4bcd05SJeff Roberson 	inblock_end = td->td_ru.ru_inblock;
8141c4bcd05SJeff Roberson 	oublock_end = td->td_ru.ru_oublock;
815fd3bf775SJohn Dyson 
816b1012d80SJohn Baldwin 	job->msgrcv = msgrcv_end - msgrcv_st;
817b1012d80SJohn Baldwin 	job->msgsnd = msgsnd_end - msgsnd_st;
818b1012d80SJohn Baldwin 	job->inblock = inblock_end - inblock_st;
819b1012d80SJohn Baldwin 	job->outblock = oublock_end - oublock_st;
8202244ea07SJohn Dyson 
821022ca2fcSAlan Somers 	if (error != 0 && job->uiop->uio_resid != cnt) {
8222244ea07SJohn Dyson 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
8232244ea07SJohn Dyson 			error = 0;
8242247f489SAlan Somers 		if (error == EPIPE && (opcode & LIO_WRITE)) {
8255652770dSJohn Baldwin 			PROC_LOCK(job->userproc);
8265652770dSJohn Baldwin 			kern_psignal(job->userproc, SIGPIPE);
8275652770dSJohn Baldwin 			PROC_UNLOCK(job->userproc);
82819eb87d2SJohn Baldwin 		}
8292244ea07SJohn Dyson 	}
8302244ea07SJohn Dyson 
831022ca2fcSAlan Somers 	cnt -= job->uiop->uio_resid;
832f8f750c5SRobert Watson 	td->td_ucred = td_savedcred;
833f0ec1740SJohn Baldwin 	if (error)
834f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
835f0ec1740SJohn Baldwin 	else
836f0ec1740SJohn Baldwin 		aio_complete(job, cnt, 0);
8372244ea07SJohn Dyson }
8382244ea07SJohn Dyson 
83969cd28daSDoug Ambrisko static void
8405652770dSJohn Baldwin aio_process_sync(struct kaiocb *job)
841f95c13dbSGleb Smirnoff {
842f95c13dbSGleb Smirnoff 	struct thread *td = curthread;
843f95c13dbSGleb Smirnoff 	struct ucred *td_savedcred = td->td_ucred;
8445652770dSJohn Baldwin 	struct file *fp = job->fd_file;
845f95c13dbSGleb Smirnoff 	int error = 0;
846f95c13dbSGleb Smirnoff 
8472247f489SAlan Somers 	KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
8485652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
849f95c13dbSGleb Smirnoff 
8505652770dSJohn Baldwin 	td->td_ucred = job->cred;
851801ac943SThomas Munro 	if (fp->f_vnode != NULL) {
852801ac943SThomas Munro 		error = aio_fsync_vnode(td, fp->f_vnode,
853801ac943SThomas Munro 		    job->uaiocb.aio_lio_opcode);
854801ac943SThomas Munro 	}
855f95c13dbSGleb Smirnoff 	td->td_ucred = td_savedcred;
856f0ec1740SJohn Baldwin 	if (error)
857f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
858f0ec1740SJohn Baldwin 	else
859f0ec1740SJohn Baldwin 		aio_complete(job, 0, 0);
860f95c13dbSGleb Smirnoff }
861f95c13dbSGleb Smirnoff 
862f95c13dbSGleb Smirnoff static void
8635652770dSJohn Baldwin aio_process_mlock(struct kaiocb *job)
8646160e12cSGleb Smirnoff {
8655652770dSJohn Baldwin 	struct aiocb *cb = &job->uaiocb;
8666160e12cSGleb Smirnoff 	int error;
8676160e12cSGleb Smirnoff 
8685652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
8695652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
8706160e12cSGleb Smirnoff 
871f3215338SJohn Baldwin 	aio_switch_vmspace(job);
872496ab053SKonstantin Belousov 	error = kern_mlock(job->userproc, job->cred,
873496ab053SKonstantin Belousov 	    __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
874496ab053SKonstantin Belousov 	aio_complete(job, error != 0 ? -1 : 0, error);
8756160e12cSGleb Smirnoff }
8766160e12cSGleb Smirnoff 
8776160e12cSGleb Smirnoff static void
878f3215338SJohn Baldwin aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
8791ce91824SDavid Xu {
8801ce91824SDavid Xu 	struct aioliojob *lj;
88169cd28daSDoug Ambrisko 	struct kaioinfo *ki;
8825652770dSJohn Baldwin 	struct kaiocb *sjob, *sjobn;
8831ce91824SDavid Xu 	int lj_done;
884f3215338SJohn Baldwin 	bool schedule_fsync;
88569cd28daSDoug Ambrisko 
88669cd28daSDoug Ambrisko 	ki = userp->p_aioinfo;
887759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
8885652770dSJohn Baldwin 	lj = job->lio;
88969cd28daSDoug Ambrisko 	lj_done = 0;
89069cd28daSDoug Ambrisko 	if (lj) {
8911ce91824SDavid Xu 		lj->lioj_finished_count++;
8921ce91824SDavid Xu 		if (lj->lioj_count == lj->lioj_finished_count)
89369cd28daSDoug Ambrisko 			lj_done = 1;
89469cd28daSDoug Ambrisko 	}
8955652770dSJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
896f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
89727b8220dSDavid Xu 
89827b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_RUNDOWN)
89927b8220dSDavid Xu 		goto notification_done;
90027b8220dSDavid Xu 
9015652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
9025652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
9036814c2daSKonstantin Belousov 		aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
9041ce91824SDavid Xu 
9055652770dSJohn Baldwin 	KNOTE_LOCKED(&job->klist, 1);
9061ce91824SDavid Xu 
90769cd28daSDoug Ambrisko 	if (lj_done) {
9081ce91824SDavid Xu 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
90969cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
9101ce91824SDavid Xu 			KNOTE_LOCKED(&lj->klist, 1);
91169cd28daSDoug Ambrisko 		}
9121ce91824SDavid Xu 		if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
91329331656SKonstantin Belousov 		    == LIOJ_SIGNAL &&
91429331656SKonstantin Belousov 		    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
9154c0fb2cfSDavid Xu 		    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
9166814c2daSKonstantin Belousov 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
9176814c2daSKonstantin Belousov 			    true);
91869cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
91969cd28daSDoug Ambrisko 		}
92069cd28daSDoug Ambrisko 	}
92127b8220dSDavid Xu 
92227b8220dSDavid Xu notification_done:
9235652770dSJohn Baldwin 	if (job->jobflags & KAIOCB_CHECKSYNC) {
924f3215338SJohn Baldwin 		schedule_fsync = false;
9255652770dSJohn Baldwin 		TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
926b9a53e16SJohn Baldwin 			if (job->fd_file != sjob->fd_file ||
927b9a53e16SJohn Baldwin 			    job->seqno >= sjob->seqno)
928b9a53e16SJohn Baldwin 				continue;
929b9a53e16SJohn Baldwin 			if (--sjob->pending > 0)
930b9a53e16SJohn Baldwin 				continue;
931b9a53e16SJohn Baldwin 			TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
932005ce8e4SJohn Baldwin 			if (!aio_clear_cancel_function_locked(sjob))
933f3215338SJohn Baldwin 				continue;
934b9a53e16SJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
935f3215338SJohn Baldwin 			schedule_fsync = true;
93699eee864SDavid Xu 		}
937f3215338SJohn Baldwin 		if (schedule_fsync)
938f3215338SJohn Baldwin 			taskqueue_enqueue(taskqueue_aiod_kick,
939f3215338SJohn Baldwin 			    &ki->kaio_sync_task);
94099eee864SDavid Xu 	}
94127b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_WAKEUP) {
94269cd28daSDoug Ambrisko 		ki->kaio_flags &= ~KAIO_WAKEUP;
9431ce91824SDavid Xu 		wakeup(&userp->p_aioinfo);
94469cd28daSDoug Ambrisko 	}
94569cd28daSDoug Ambrisko }
94669cd28daSDoug Ambrisko 
9478a4dc40fSJohn Baldwin static void
948f3215338SJohn Baldwin aio_schedule_fsync(void *context, int pending)
949f3215338SJohn Baldwin {
950f3215338SJohn Baldwin 	struct kaioinfo *ki;
951f3215338SJohn Baldwin 	struct kaiocb *job;
952f3215338SJohn Baldwin 
953f3215338SJohn Baldwin 	ki = context;
954f3215338SJohn Baldwin 	AIO_LOCK(ki);
955f3215338SJohn Baldwin 	while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
956f3215338SJohn Baldwin 		job = TAILQ_FIRST(&ki->kaio_syncready);
957f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncready, job, list);
958f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
959f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
960f3215338SJohn Baldwin 		AIO_LOCK(ki);
961f3215338SJohn Baldwin 	}
962f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
963f3215338SJohn Baldwin }
964f3215338SJohn Baldwin 
965f3215338SJohn Baldwin bool
966f3215338SJohn Baldwin aio_cancel_cleared(struct kaiocb *job)
967f3215338SJohn Baldwin {
968f3215338SJohn Baldwin 
969f3215338SJohn Baldwin 	/*
970f3215338SJohn Baldwin 	 * The caller should hold the same queue lock held when
971f3215338SJohn Baldwin 	 * aio_clear_cancel_function() was called and set this flag
972f3215338SJohn Baldwin 	 * ensuring this check sees an up-to-date value.  However,
973f3215338SJohn Baldwin 	 * there is no way to assert that.
974f3215338SJohn Baldwin 	 */
975f3215338SJohn Baldwin 	return ((job->jobflags & KAIOCB_CLEARED) != 0);
976f3215338SJohn Baldwin }
977f3215338SJohn Baldwin 
978005ce8e4SJohn Baldwin static bool
979005ce8e4SJohn Baldwin aio_clear_cancel_function_locked(struct kaiocb *job)
980005ce8e4SJohn Baldwin {
981005ce8e4SJohn Baldwin 
982005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
983005ce8e4SJohn Baldwin 	MPASS(job->cancel_fn != NULL);
984005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLING) {
985005ce8e4SJohn Baldwin 		job->jobflags |= KAIOCB_CLEARED;
986005ce8e4SJohn Baldwin 		return (false);
987005ce8e4SJohn Baldwin 	}
988005ce8e4SJohn Baldwin 	job->cancel_fn = NULL;
989005ce8e4SJohn Baldwin 	return (true);
990005ce8e4SJohn Baldwin }
991005ce8e4SJohn Baldwin 
992f3215338SJohn Baldwin bool
993f3215338SJohn Baldwin aio_clear_cancel_function(struct kaiocb *job)
994f3215338SJohn Baldwin {
995f3215338SJohn Baldwin 	struct kaioinfo *ki;
996005ce8e4SJohn Baldwin 	bool ret;
997f3215338SJohn Baldwin 
998f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
999f3215338SJohn Baldwin 	AIO_LOCK(ki);
1000005ce8e4SJohn Baldwin 	ret = aio_clear_cancel_function_locked(job);
1001f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1002005ce8e4SJohn Baldwin 	return (ret);
1003f3215338SJohn Baldwin }
1004005ce8e4SJohn Baldwin 
1005005ce8e4SJohn Baldwin static bool
1006005ce8e4SJohn Baldwin aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1007005ce8e4SJohn Baldwin {
1008005ce8e4SJohn Baldwin 
1009005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1010005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLED)
1011005ce8e4SJohn Baldwin 		return (false);
1012005ce8e4SJohn Baldwin 	job->cancel_fn = func;
1013f3215338SJohn Baldwin 	return (true);
1014f3215338SJohn Baldwin }
1015f3215338SJohn Baldwin 
1016f3215338SJohn Baldwin bool
1017f3215338SJohn Baldwin aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1018f3215338SJohn Baldwin {
1019f3215338SJohn Baldwin 	struct kaioinfo *ki;
1020005ce8e4SJohn Baldwin 	bool ret;
1021f3215338SJohn Baldwin 
1022f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1023f3215338SJohn Baldwin 	AIO_LOCK(ki);
1024005ce8e4SJohn Baldwin 	ret = aio_set_cancel_function_locked(job, func);
1025f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1026005ce8e4SJohn Baldwin 	return (ret);
1027f3215338SJohn Baldwin }
1028f3215338SJohn Baldwin 
1029f3215338SJohn Baldwin void
1030f3215338SJohn Baldwin aio_complete(struct kaiocb *job, long status, int error)
1031f3215338SJohn Baldwin {
1032f3215338SJohn Baldwin 	struct kaioinfo *ki;
1033f3215338SJohn Baldwin 	struct proc *userp;
1034f3215338SJohn Baldwin 
1035f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.error = error;
1036f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.status = status;
1037f3215338SJohn Baldwin 
1038f3215338SJohn Baldwin 	userp = job->userproc;
1039f3215338SJohn Baldwin 	ki = userp->p_aioinfo;
1040f3215338SJohn Baldwin 
1041f3215338SJohn Baldwin 	AIO_LOCK(ki);
1042f3215338SJohn Baldwin 	KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1043f3215338SJohn Baldwin 	    ("duplicate aio_complete"));
1044f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_FINISHED;
1045f3215338SJohn Baldwin 	if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1046f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1047f3215338SJohn Baldwin 		aio_bio_done_notify(userp, job);
1048f3215338SJohn Baldwin 	}
1049f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1050f3215338SJohn Baldwin }
1051f3215338SJohn Baldwin 
1052f3215338SJohn Baldwin void
1053f3215338SJohn Baldwin aio_cancel(struct kaiocb *job)
1054f3215338SJohn Baldwin {
1055f3215338SJohn Baldwin 
1056f3215338SJohn Baldwin 	aio_complete(job, -1, ECANCELED);
1057f3215338SJohn Baldwin }
1058f3215338SJohn Baldwin 
1059f3215338SJohn Baldwin void
10605652770dSJohn Baldwin aio_switch_vmspace(struct kaiocb *job)
10618a4dc40fSJohn Baldwin {
10628a4dc40fSJohn Baldwin 
10635652770dSJohn Baldwin 	vmspace_switch_aio(job->userproc->p_vmspace);
10648a4dc40fSJohn Baldwin }
10658a4dc40fSJohn Baldwin 
10662244ea07SJohn Dyson /*
1067f95c13dbSGleb Smirnoff  * The AIO daemon, most of the actual work is done in aio_process_*,
106884af4da6SJohn Dyson  * but the setup (and address space mgmt) is done in this routine.
10692244ea07SJohn Dyson  */
10702244ea07SJohn Dyson static void
10711ce91824SDavid Xu aio_daemon(void *_id)
10722244ea07SJohn Dyson {
10735652770dSJohn Baldwin 	struct kaiocb *job;
107439314b7dSJohn Baldwin 	struct aioproc *aiop;
1075bfbbc4aaSJason Evans 	struct kaioinfo *ki;
1076f3215338SJohn Baldwin 	struct proc *p;
10778a4dc40fSJohn Baldwin 	struct vmspace *myvm;
1078b40ce416SJulian Elischer 	struct thread *td = curthread;
10791ce91824SDavid Xu 	int id = (intptr_t)_id;
10802244ea07SJohn Dyson 
10812244ea07SJohn Dyson 	/*
10828a4dc40fSJohn Baldwin 	 * Grab an extra reference on the daemon's vmspace so that it
10838a4dc40fSJohn Baldwin 	 * doesn't get freed by jobs that switch to a different
10848a4dc40fSJohn Baldwin 	 * vmspace.
10852244ea07SJohn Dyson 	 */
10868a4dc40fSJohn Baldwin 	p = td->td_proc;
10878a4dc40fSJohn Baldwin 	myvm = vmspace_acquire_ref(p);
1088fd3bf775SJohn Dyson 
10898a4dc40fSJohn Baldwin 	KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1090fd3bf775SJohn Dyson 
1091fd3bf775SJohn Dyson 	/*
1092bfbbc4aaSJason Evans 	 * Allocate and ready the aio control info.  There is one aiop structure
1093bfbbc4aaSJason Evans 	 * per daemon.
1094fd3bf775SJohn Dyson 	 */
1095a163d034SWarner Losh 	aiop = uma_zalloc(aiop_zone, M_WAITOK);
109639314b7dSJohn Baldwin 	aiop->aioproc = p;
109739314b7dSJohn Baldwin 	aiop->aioprocflags = 0;
1098bfbbc4aaSJason Evans 
1099fd3bf775SJohn Dyson 	/*
1100fd3bf775SJohn Dyson 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1101b40ce416SJulian Elischer 	 * and creating too many daemons.)
1102fd3bf775SJohn Dyson 	 */
11031ce91824SDavid Xu 	sema_post(&aio_newproc_sem);
11042244ea07SJohn Dyson 
11051ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
1106bfbbc4aaSJason Evans 	for (;;) {
1107fd3bf775SJohn Dyson 		/*
1108fd3bf775SJohn Dyson 		 * Take daemon off of free queue
1109fd3bf775SJohn Dyson 		 */
111039314b7dSJohn Baldwin 		if (aiop->aioprocflags & AIOP_FREE) {
11112244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
111239314b7dSJohn Baldwin 			aiop->aioprocflags &= ~AIOP_FREE;
11132244ea07SJohn Dyson 		}
11142244ea07SJohn Dyson 
1115fd3bf775SJohn Dyson 		/*
1116bfbbc4aaSJason Evans 		 * Check for jobs.
1117fd3bf775SJohn Dyson 		 */
11185652770dSJohn Baldwin 		while ((job = aio_selectjob(aiop)) != NULL) {
11191ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11202244ea07SJohn Dyson 
1121f3215338SJohn Baldwin 			ki = job->userproc->p_aioinfo;
1122f3215338SJohn Baldwin 			job->handle_fn(job);
112384af4da6SJohn Dyson 
11249b84335cSDavid Xu 			mtx_lock(&aio_job_mtx);
11259b84335cSDavid Xu 			/* Decrement the active job count. */
11269b84335cSDavid Xu 			ki->kaio_active_count--;
11272244ea07SJohn Dyson 		}
11282244ea07SJohn Dyson 
1129fd3bf775SJohn Dyson 		/*
1130bfbbc4aaSJason Evans 		 * Disconnect from user address space.
1131fd3bf775SJohn Dyson 		 */
11328a4dc40fSJohn Baldwin 		if (p->p_vmspace != myvm) {
11331ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11348a4dc40fSJohn Baldwin 			vmspace_switch_aio(myvm);
11351ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
11361ce91824SDavid Xu 			/*
11371ce91824SDavid Xu 			 * We have to restart to avoid race, we only sleep if
11388a4dc40fSJohn Baldwin 			 * no job can be selected.
11391ce91824SDavid Xu 			 */
11401ce91824SDavid Xu 			continue;
1141fd3bf775SJohn Dyson 		}
1142fd3bf775SJohn Dyson 
11431ce91824SDavid Xu 		mtx_assert(&aio_job_mtx, MA_OWNED);
11441ce91824SDavid Xu 
1145fd3bf775SJohn Dyson 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
114639314b7dSJohn Baldwin 		aiop->aioprocflags |= AIOP_FREE;
1147fd3bf775SJohn Dyson 
1148fd3bf775SJohn Dyson 		/*
1149bfbbc4aaSJason Evans 		 * If daemon is inactive for a long time, allow it to exit,
1150bfbbc4aaSJason Evans 		 * thereby freeing resources.
1151fd3bf775SJohn Dyson 		 */
115239314b7dSJohn Baldwin 		if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
11538a4dc40fSJohn Baldwin 		    aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
115439314b7dSJohn Baldwin 		    (aiop->aioprocflags & AIOP_FREE) &&
11558a4dc40fSJohn Baldwin 		    num_aio_procs > target_aio_procs)
11568a4dc40fSJohn Baldwin 			break;
11578a4dc40fSJohn Baldwin 	}
1158fd3bf775SJohn Dyson 	TAILQ_REMOVE(&aio_freeproc, aiop, list);
115984af4da6SJohn Dyson 	num_aio_procs--;
11601ce91824SDavid Xu 	mtx_unlock(&aio_job_mtx);
11611ce91824SDavid Xu 	uma_zfree(aiop_zone, aiop);
11621ce91824SDavid Xu 	free_unr(aiod_unr, id);
11638a4dc40fSJohn Baldwin 	vmspace_free(myvm);
11648a4dc40fSJohn Baldwin 
11658a4dc40fSJohn Baldwin 	KASSERT(p->p_vmspace == myvm,
11668a4dc40fSJohn Baldwin 	    ("AIOD: bad vmspace for exiting daemon"));
1167f7db0c95SMark Johnston 	KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1168f7db0c95SMark Johnston 	    ("AIOD: bad vm refcnt for exiting daemon: %d",
1169f7db0c95SMark Johnston 	    refcount_load(&myvm->vm_refcnt)));
11703745c395SJulian Elischer 	kproc_exit(0);
1171fd3bf775SJohn Dyson }
11722244ea07SJohn Dyson 
11732244ea07SJohn Dyson /*
1174bfbbc4aaSJason Evans  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1175bfbbc4aaSJason Evans  * AIO daemon modifies its environment itself.
11762244ea07SJohn Dyson  */
11772244ea07SJohn Dyson static int
11781ce91824SDavid Xu aio_newproc(int *start)
1179fd3bf775SJohn Dyson {
11802244ea07SJohn Dyson 	int error;
1181c9a970a7SAlan Cox 	struct proc *p;
11821ce91824SDavid Xu 	int id;
11832244ea07SJohn Dyson 
11841ce91824SDavid Xu 	id = alloc_unr(aiod_unr);
11853745c395SJulian Elischer 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
11861ce91824SDavid Xu 		RFNOWAIT, 0, "aiod%d", id);
11871ce91824SDavid Xu 	if (error == 0) {
1188fd3bf775SJohn Dyson 		/*
11891ce91824SDavid Xu 		 * Wait until daemon is started.
1190fd3bf775SJohn Dyson 		 */
11911ce91824SDavid Xu 		sema_wait(&aio_newproc_sem);
11921ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
119384af4da6SJohn Dyson 		num_aio_procs++;
11941ce91824SDavid Xu 		if (start != NULL)
11957f34b521SDavid Xu 			(*start)--;
11961ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
11971ce91824SDavid Xu 	} else {
11981ce91824SDavid Xu 		free_unr(aiod_unr, id);
11991ce91824SDavid Xu 	}
1200ac41f2efSAlfred Perlstein 	return (error);
12012244ea07SJohn Dyson }
12022244ea07SJohn Dyson 
12032244ea07SJohn Dyson /*
120472bce9ffSAlan Somers  * Try the high-performance, low-overhead bio method for eligible
120588ed460eSAlan Cox  * VCHR devices.  This method doesn't use an aio helper thread, and
120688ed460eSAlan Cox  * thus has very low overhead.
120788ed460eSAlan Cox  *
1208a9bf5e37SDavid Xu  * Assumes that the caller, aio_aqueue(), has incremented the file
120988ed460eSAlan Cox  * structure's reference count, preventing its deallocation for the
121088ed460eSAlan Cox  * duration of this call.
1211fd3bf775SJohn Dyson  */
121288ed460eSAlan Cox static int
121372bce9ffSAlan Somers aio_qbio(struct proc *p, struct kaiocb *job)
1214fd3bf775SJohn Dyson {
1215fd3bf775SJohn Dyson 	struct aiocb *cb;
1216fd3bf775SJohn Dyson 	struct file *fp;
1217f743d981SAlexander Motin 	struct buf *pbuf;
1218fd3bf775SJohn Dyson 	struct vnode *vp;
1219f3215a60SKonstantin Belousov 	struct cdevsw *csw;
1220f3215a60SKonstantin Belousov 	struct cdev *dev;
1221fd3bf775SJohn Dyson 	struct kaioinfo *ki;
1222022ca2fcSAlan Somers 	struct bio **bios = NULL;
1223022ca2fcSAlan Somers 	off_t offset;
1224022ca2fcSAlan Somers 	int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1225f743d981SAlexander Motin 	vm_prot_t prot;
1226022ca2fcSAlan Somers 	bool use_unmapped;
1227fd3bf775SJohn Dyson 
12285652770dSJohn Baldwin 	cb = &job->uaiocb;
12295652770dSJohn Baldwin 	fp = job->fd_file;
1230022ca2fcSAlan Somers 	opcode = cb->aio_lio_opcode;
1231fd3bf775SJohn Dyson 
1232022ca2fcSAlan Somers 	if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1233022ca2fcSAlan Somers 	    opcode == LIO_READ || opcode == LIO_READV))
1234f54c5606SJohn Baldwin 		return (-1);
12356160e12cSGleb Smirnoff 	if (fp == NULL || fp->f_type != DTYPE_VNODE)
1236008626c3SPoul-Henning Kamp 		return (-1);
1237fd3bf775SJohn Dyson 
12383b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
1239f743d981SAlexander Motin 	if (vp->v_type != VCHR)
1240f582ac06SBrian Feldman 		return (-1);
1241ad8de0f2SDavid Xu 	if (vp->v_bufobj.bo_bsize == 0)
1242ad8de0f2SDavid Xu 		return (-1);
1243022ca2fcSAlan Somers 
12442247f489SAlan Somers 	bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1245022ca2fcSAlan Somers 	iovcnt = job->uiop->uio_iovcnt;
1246022ca2fcSAlan Somers 	if (iovcnt > max_buf_aio)
1247008626c3SPoul-Henning Kamp 		return (-1);
1248022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1249022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1250022ca2fcSAlan Somers 			return (-1);
1251022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len > maxphys) {
1252022ca2fcSAlan Somers 			error = -1;
1253022ca2fcSAlan Somers 			return (-1);
1254022ca2fcSAlan Somers 		}
1255022ca2fcSAlan Somers 	}
1256022ca2fcSAlan Somers 	offset = cb->aio_offset;
1257fd3bf775SJohn Dyson 
1258f3215a60SKonstantin Belousov 	ref = 0;
1259f3215a60SKonstantin Belousov 	csw = devvn_refthread(vp, &dev, &ref);
1260f3215a60SKonstantin Belousov 	if (csw == NULL)
1261f3215a60SKonstantin Belousov 		return (ENXIO);
1262f743d981SAlexander Motin 
1263f743d981SAlexander Motin 	if ((csw->d_flags & D_DISK) == 0) {
1264f743d981SAlexander Motin 		error = -1;
1265f743d981SAlexander Motin 		goto unref;
1266f743d981SAlexander Motin 	}
1267022ca2fcSAlan Somers 	if (job->uiop->uio_resid > dev->si_iosize_max) {
1268f3215a60SKonstantin Belousov 		error = -1;
1269f3215a60SKonstantin Belousov 		goto unref;
1270f3215a60SKonstantin Belousov 	}
1271f3215a60SKonstantin Belousov 
1272f743d981SAlexander Motin 	ki = p->p_aioinfo;
1273022ca2fcSAlan Somers 	job->error = 0;
12744d805eacSJohn Baldwin 
1275022ca2fcSAlan Somers 	use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1276022ca2fcSAlan Somers 	if (!use_unmapped) {
1277022ca2fcSAlan Somers 		AIO_LOCK(ki);
1278022ca2fcSAlan Somers 		if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1279022ca2fcSAlan Somers 			AIO_UNLOCK(ki);
1280f54c5606SJohn Baldwin 			error = EAGAIN;
1281f743d981SAlexander Motin 			goto unref;
1282f743d981SAlexander Motin 		}
1283022ca2fcSAlan Somers 		ki->kaio_buffer_count += iovcnt;
1284022ca2fcSAlan Somers 		AIO_UNLOCK(ki);
1285022ca2fcSAlan Somers 	}
12864d805eacSJohn Baldwin 
1287022ca2fcSAlan Somers 	bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1288022ca2fcSAlan Somers 	atomic_store_int(&job->nbio, iovcnt);
1289022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1290022ca2fcSAlan Somers 		struct vm_page** pages;
1291022ca2fcSAlan Somers 		struct bio *bp;
1292022ca2fcSAlan Somers 		void *buf;
1293022ca2fcSAlan Somers 		size_t nbytes;
1294022ca2fcSAlan Somers 		int npages;
1295022ca2fcSAlan Somers 
1296022ca2fcSAlan Somers 		buf = job->uiop->uio_iov[i].iov_base;
1297022ca2fcSAlan Somers 		nbytes = job->uiop->uio_iov[i].iov_len;
1298022ca2fcSAlan Somers 
1299022ca2fcSAlan Somers 		bios[i] = g_alloc_bio();
1300022ca2fcSAlan Somers 		bp = bios[i];
1301022ca2fcSAlan Somers 
1302022ca2fcSAlan Somers 		poff = (vm_offset_t)buf & PAGE_MASK;
1303022ca2fcSAlan Somers 		if (use_unmapped) {
1304022ca2fcSAlan Somers 			pbuf = NULL;
1305022ca2fcSAlan Somers 			pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1306022ca2fcSAlan Somers 			    nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1307022ca2fcSAlan Somers 		} else {
130801206038SAlan Somers 			pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1309f743d981SAlexander Motin 			BUF_KERNPROC(pbuf);
131001206038SAlan Somers 			pages = pbuf->b_pages;
13114d805eacSJohn Baldwin 		}
13121ce91824SDavid Xu 
1313022ca2fcSAlan Somers 		bp->bio_length = nbytes;
1314022ca2fcSAlan Somers 		bp->bio_bcount = nbytes;
131572bce9ffSAlan Somers 		bp->bio_done = aio_biowakeup;
1316022ca2fcSAlan Somers 		bp->bio_offset = offset;
1317022ca2fcSAlan Somers 		bp->bio_cmd = bio_cmd;
1318f743d981SAlexander Motin 		bp->bio_dev = dev;
131901206038SAlan Somers 		bp->bio_caller1 = job;
132001206038SAlan Somers 		bp->bio_caller2 = pbuf;
1321f743d981SAlexander Motin 
1322f743d981SAlexander Motin 		prot = VM_PROT_READ;
1323022ca2fcSAlan Somers 		if (opcode == LIO_READ || opcode == LIO_READV)
1324f743d981SAlexander Motin 			prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
132501206038SAlan Somers 		npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1326022ca2fcSAlan Somers 		    (vm_offset_t)buf, bp->bio_length, prot, pages,
1327cd853791SKonstantin Belousov 		    atop(maxphys) + 1);
132801206038SAlan Somers 		if (npages < 0) {
1329022ca2fcSAlan Somers 			if (pbuf != NULL)
1330022ca2fcSAlan Somers 				uma_zfree(pbuf_zone, pbuf);
1331022ca2fcSAlan Somers 			else
1332022ca2fcSAlan Somers 				free(pages, M_TEMP);
1333f743d981SAlexander Motin 			error = EFAULT;
1334022ca2fcSAlan Somers 			g_destroy_bio(bp);
1335022ca2fcSAlan Somers 			i--;
1336022ca2fcSAlan Somers 			goto destroy_bios;
1337f743d981SAlexander Motin 		}
13384d805eacSJohn Baldwin 		if (pbuf != NULL) {
133901206038SAlan Somers 			pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1340f743d981SAlexander Motin 			bp->bio_data = pbuf->b_data + poff;
134101206038SAlan Somers 			pbuf->b_npages = npages;
1342022ca2fcSAlan Somers 			atomic_add_int(&num_buf_aio, 1);
1343f743d981SAlexander Motin 		} else {
134401206038SAlan Somers 			bp->bio_ma = pages;
134501206038SAlan Somers 			bp->bio_ma_n = npages;
1346f743d981SAlexander Motin 			bp->bio_ma_offset = poff;
1347f743d981SAlexander Motin 			bp->bio_data = unmapped_buf;
1348f743d981SAlexander Motin 			bp->bio_flags |= BIO_UNMAPPED;
13498091e52bSJohn Baldwin 			atomic_add_int(&num_unmapped_aio, 1);
1350f743d981SAlexander Motin 		}
1351f743d981SAlexander Motin 
1352022ca2fcSAlan Somers 		offset += nbytes;
1353022ca2fcSAlan Somers 	}
1354022ca2fcSAlan Somers 
1355bfbbc4aaSJason Evans 	/* Perform transfer. */
1356022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++)
1357022ca2fcSAlan Somers 		csw->d_strategy(bios[i]);
1358022ca2fcSAlan Somers 	free(bios, M_TEMP);
1359022ca2fcSAlan Somers 
1360f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1361ac41f2efSAlfred Perlstein 	return (0);
1362fd3bf775SJohn Dyson 
1363022ca2fcSAlan Somers destroy_bios:
1364022ca2fcSAlan Somers 	for (; i >= 0; i--)
1365022ca2fcSAlan Somers 		aio_biocleanup(bios[i]);
1366022ca2fcSAlan Somers 	free(bios, M_TEMP);
1367f3215a60SKonstantin Belousov unref:
1368f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1369fd3bf775SJohn Dyson 	return (error);
1370fd3bf775SJohn Dyson }
1371fd3bf775SJohn Dyson 
1372399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
13733858a1f4SJohn Baldwin static int
13743858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
13753858a1f4SJohn Baldwin {
13763858a1f4SJohn Baldwin 
13773858a1f4SJohn Baldwin 	/*
13783858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
13793858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
13803858a1f4SJohn Baldwin 	 */
13813858a1f4SJohn Baldwin 	nsig->sigev_notify = osig->sigev_notify;
13823858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
13833858a1f4SJohn Baldwin 	case SIGEV_NONE:
13843858a1f4SJohn Baldwin 		break;
13853858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
13863858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
13873858a1f4SJohn Baldwin 		break;
13883858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
13893858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
13903858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
13913858a1f4SJohn Baldwin 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
13923858a1f4SJohn Baldwin 		break;
13933858a1f4SJohn Baldwin 	default:
13943858a1f4SJohn Baldwin 		return (EINVAL);
13953858a1f4SJohn Baldwin 	}
13963858a1f4SJohn Baldwin 	return (0);
13973858a1f4SJohn Baldwin }
13983858a1f4SJohn Baldwin 
13993858a1f4SJohn Baldwin static int
1400022ca2fcSAlan Somers aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1401022ca2fcSAlan Somers     int type __unused)
14023858a1f4SJohn Baldwin {
14033858a1f4SJohn Baldwin 	struct oaiocb *ojob;
1404022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
14053858a1f4SJohn Baldwin 	int error;
14063858a1f4SJohn Baldwin 
1407022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
1408022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct oaiocb));
14093858a1f4SJohn Baldwin 	if (error)
14103858a1f4SJohn Baldwin 		return (error);
1411022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1412022ca2fcSAlan Somers 	ojob = (struct oaiocb *)kcb;
1413022ca2fcSAlan Somers 	return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
14143858a1f4SJohn Baldwin }
1415399e8c17SJohn Baldwin #endif
14163858a1f4SJohn Baldwin 
14173858a1f4SJohn Baldwin static int
1418022ca2fcSAlan Somers aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
14193858a1f4SJohn Baldwin {
1420022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
1421022ca2fcSAlan Somers 	int error;
14223858a1f4SJohn Baldwin 
1423022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct aiocb));
1424022ca2fcSAlan Somers 	if (error)
1425022ca2fcSAlan Somers 		return (error);
1426f30a1ae8SThomas Munro 	if (type == LIO_NOP)
1427f30a1ae8SThomas Munro 		type = kcb->aio_lio_opcode;
14282247f489SAlan Somers 	if (type & LIO_VECTORED) {
1429022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
1430022ca2fcSAlan Somers 		error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1431022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
1432022ca2fcSAlan Somers 	}
1433022ca2fcSAlan Somers 
1434022ca2fcSAlan Somers 	return (error);
14353858a1f4SJohn Baldwin }
14363858a1f4SJohn Baldwin 
14373858a1f4SJohn Baldwin static long
14383858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob)
14393858a1f4SJohn Baldwin {
14403858a1f4SJohn Baldwin 
14413858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.status));
14423858a1f4SJohn Baldwin }
14433858a1f4SJohn Baldwin 
14443858a1f4SJohn Baldwin static long
14453858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob)
14463858a1f4SJohn Baldwin {
14473858a1f4SJohn Baldwin 
14483858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.error));
14493858a1f4SJohn Baldwin }
14503858a1f4SJohn Baldwin 
14513858a1f4SJohn Baldwin static int
14523858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status)
14533858a1f4SJohn Baldwin {
14543858a1f4SJohn Baldwin 
14553858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.status, status));
14563858a1f4SJohn Baldwin }
14573858a1f4SJohn Baldwin 
14583858a1f4SJohn Baldwin static int
14593858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error)
14603858a1f4SJohn Baldwin {
14613858a1f4SJohn Baldwin 
14623858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.error, error));
14633858a1f4SJohn Baldwin }
14643858a1f4SJohn Baldwin 
14653858a1f4SJohn Baldwin static int
14663858a1f4SJohn Baldwin aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
14673858a1f4SJohn Baldwin {
14683858a1f4SJohn Baldwin 
14693858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
14703858a1f4SJohn Baldwin }
14713858a1f4SJohn Baldwin 
14723858a1f4SJohn Baldwin static int
14733858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
14743858a1f4SJohn Baldwin {
14753858a1f4SJohn Baldwin 
14763858a1f4SJohn Baldwin 	return (suword(ujobp, (long)ujob));
14773858a1f4SJohn Baldwin }
14783858a1f4SJohn Baldwin 
14793858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = {
1480849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin,
14813858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14823858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14833858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14843858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14853858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14863858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14873858a1f4SJohn Baldwin };
14883858a1f4SJohn Baldwin 
1489399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
14903858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = {
1491849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin_old_sigevent,
14923858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14933858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14943858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14953858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14963858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb_store_kernelinfo,
14973858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14983858a1f4SJohn Baldwin };
1499399e8c17SJohn Baldwin #endif
15003858a1f4SJohn Baldwin 
1501bfbbc4aaSJason Evans /*
150272bce9ffSAlan Somers  * Queue a new AIO request.  Choosing either the threaded or direct bio VCHR
1503bfbbc4aaSJason Evans  * technique is done in this code.
15042244ea07SJohn Dyson  */
15056a1162d4SAlexander Leidinger int
15065652770dSJohn Baldwin aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
15073858a1f4SJohn Baldwin     int type, struct aiocb_ops *ops)
1508fd3bf775SJohn Dyson {
1509b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1510022ca2fcSAlan Somers 	struct file *fp = NULL;
1511f3215338SJohn Baldwin 	struct kaiocb *job;
15122244ea07SJohn Dyson 	struct kaioinfo *ki;
1513c6fa9f78SAlan Cox 	struct kevent kev;
15141ce91824SDavid Xu 	int opcode;
15151ce91824SDavid Xu 	int error;
15164db71d27SJohn-Mark Gurney 	int fd, kqfd;
15171ce91824SDavid Xu 	int jid;
1518fde80935SDavid Xu 	u_short evflags;
15192244ea07SJohn Dyson 
1520a9bf5e37SDavid Xu 	if (p->p_aioinfo == NULL)
1521a9bf5e37SDavid Xu 		aio_init_aioinfo(p);
1522a9bf5e37SDavid Xu 
15231ce91824SDavid Xu 	ki = p->p_aioinfo;
15241ce91824SDavid Xu 
15255652770dSJohn Baldwin 	ops->store_status(ujob, -1);
15265652770dSJohn Baldwin 	ops->store_error(ujob, 0);
15275652770dSJohn Baldwin 	ops->store_kernelinfo(ujob, -1);
1528a9bf5e37SDavid Xu 
1529a9bf5e37SDavid Xu 	if (num_queue_count >= max_queue_count ||
153086bbef43SJohn Baldwin 	    ki->kaio_count >= max_aio_queue_per_proc) {
1531022ca2fcSAlan Somers 		error = EAGAIN;
1532022ca2fcSAlan Somers 		goto err1;
1533a9bf5e37SDavid Xu 	}
1534a9bf5e37SDavid Xu 
15355652770dSJohn Baldwin 	job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
15365652770dSJohn Baldwin 	knlist_init_mtx(&job->klist, AIO_MTX(ki));
1537fd3bf775SJohn Dyson 
1538022ca2fcSAlan Somers 	error = ops->aio_copyin(ujob, job, type);
1539022ca2fcSAlan Somers 	if (error)
1540022ca2fcSAlan Somers 		goto err2;
154168d71118SDavid Xu 
1542bb430bc7SJohn Baldwin 	if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1543022ca2fcSAlan Somers 		error = EINVAL;
1544022ca2fcSAlan Somers 		goto err2;
1545434ea137SGleb Smirnoff 	}
1546434ea137SGleb Smirnoff 
15475652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
15485652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
15495652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
15505652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1551022ca2fcSAlan Somers 		error = EINVAL;
1552022ca2fcSAlan Somers 		goto err2;
155368d71118SDavid Xu 	}
155468d71118SDavid Xu 
15555652770dSJohn Baldwin 	if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
15565652770dSJohn Baldwin 	     job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
15575652770dSJohn Baldwin 		!_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1558022ca2fcSAlan Somers 		error = EINVAL;
1559022ca2fcSAlan Somers 		goto err2;
15602f3cf918SAlfred Perlstein 	}
15612244ea07SJohn Dyson 
1562ff1a3078SAlan Somers 	/* Get the opcode. */
1563ff1a3078SAlan Somers 	if (type == LIO_NOP) {
1564ff1a3078SAlan Somers 		switch (job->uaiocb.aio_lio_opcode) {
1565ff1a3078SAlan Somers 		case LIO_WRITE:
1566f30a1ae8SThomas Munro 		case LIO_WRITEV:
1567ff1a3078SAlan Somers 		case LIO_NOP:
1568ff1a3078SAlan Somers 		case LIO_READ:
1569f30a1ae8SThomas Munro 		case LIO_READV:
1570ff1a3078SAlan Somers 			opcode = job->uaiocb.aio_lio_opcode;
1571ff1a3078SAlan Somers 			break;
1572ff1a3078SAlan Somers 		default:
1573ff1a3078SAlan Somers 			error = EINVAL;
1574ff1a3078SAlan Somers 			goto err2;
1575ff1a3078SAlan Somers 		}
1576ff1a3078SAlan Somers 	} else
1577ff1a3078SAlan Somers 		opcode = job->uaiocb.aio_lio_opcode = type;
1578ff1a3078SAlan Somers 
15795652770dSJohn Baldwin 	ksiginfo_init(&job->ksi);
15804c0fb2cfSDavid Xu 
1581bfbbc4aaSJason Evans 	/* Save userspace address of the job info. */
15825652770dSJohn Baldwin 	job->ujob = ujob;
158311783b14SJohn Dyson 
1584a9d2f8d8SRobert Watson 	/*
1585a9d2f8d8SRobert Watson 	 * Validate the opcode and fetch the file object for the specified
1586a9d2f8d8SRobert Watson 	 * file descriptor.
1587a9d2f8d8SRobert Watson 	 *
1588a9d2f8d8SRobert Watson 	 * XXXRW: Moved the opcode validation up here so that we don't
1589a9d2f8d8SRobert Watson 	 * retrieve a file descriptor without knowing what the capabiltity
1590a9d2f8d8SRobert Watson 	 * should be.
1591a9d2f8d8SRobert Watson 	 */
15925652770dSJohn Baldwin 	fd = job->uaiocb.aio_fildes;
15932a522eb9SJohn Baldwin 	switch (opcode) {
15942a522eb9SJohn Baldwin 	case LIO_WRITE:
1595022ca2fcSAlan Somers 	case LIO_WRITEV:
1596cbd92ce6SMatt Macy 		error = fget_write(td, fd, &cap_pwrite_rights, &fp);
15972a522eb9SJohn Baldwin 		break;
15982a522eb9SJohn Baldwin 	case LIO_READ:
1599022ca2fcSAlan Somers 	case LIO_READV:
1600cbd92ce6SMatt Macy 		error = fget_read(td, fd, &cap_pread_rights, &fp);
1601a9d2f8d8SRobert Watson 		break;
1602a9d2f8d8SRobert Watson 	case LIO_SYNC:
1603801ac943SThomas Munro 	case LIO_DSYNC:
1604cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_fsync_rights, &fp);
1605a9d2f8d8SRobert Watson 		break;
16066160e12cSGleb Smirnoff 	case LIO_MLOCK:
16076160e12cSGleb Smirnoff 		break;
1608a9d2f8d8SRobert Watson 	case LIO_NOP:
1609cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_no_rights, &fp);
16102a522eb9SJohn Baldwin 		break;
16112a522eb9SJohn Baldwin 	default:
1612a9d2f8d8SRobert Watson 		error = EINVAL;
16132a522eb9SJohn Baldwin 	}
1614022ca2fcSAlan Somers 	if (error)
1615022ca2fcSAlan Somers 		goto err3;
161699eee864SDavid Xu 
16172247f489SAlan Somers 	if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
161899eee864SDavid Xu 		error = EINVAL;
1619022ca2fcSAlan Somers 		goto err3;
162099eee864SDavid Xu 	}
16212244ea07SJohn Dyson 
1622022ca2fcSAlan Somers 	if ((opcode == LIO_READ || opcode == LIO_READV ||
1623022ca2fcSAlan Somers 	    opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1624711dba24SKonstantin Belousov 	    job->uaiocb.aio_offset < 0 &&
1625711dba24SKonstantin Belousov 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1626ae124fc4SAlan Cox 		error = EINVAL;
1627022ca2fcSAlan Somers 		goto err3;
16282244ea07SJohn Dyson 	}
16291ce91824SDavid Xu 
16308d9ed174SKonstantin Belousov 	if (fp != NULL && fp->f_ops == &path_fileops) {
16318d9ed174SKonstantin Belousov 		error = EBADF;
16328d9ed174SKonstantin Belousov 		goto err3;
16338d9ed174SKonstantin Belousov 	}
16348d9ed174SKonstantin Belousov 
16355652770dSJohn Baldwin 	job->fd_file = fp;
16361ce91824SDavid Xu 
163799eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
163899eee864SDavid Xu 	jid = jobrefid++;
16395652770dSJohn Baldwin 	job->seqno = jobseqno++;
164099eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
16415652770dSJohn Baldwin 	error = ops->store_kernelinfo(ujob, jid);
16421ce91824SDavid Xu 	if (error) {
16431ce91824SDavid Xu 		error = EINVAL;
1644022ca2fcSAlan Somers 		goto err3;
16451ce91824SDavid Xu 	}
16465652770dSJohn Baldwin 	job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
16472244ea07SJohn Dyson 
16482244ea07SJohn Dyson 	if (opcode == LIO_NOP) {
1649a5c0b1c0SAlan Cox 		fdrop(fp, td);
1650022ca2fcSAlan Somers 		MPASS(job->uiop == &job->uio || job->uiop == NULL);
16515652770dSJohn Baldwin 		uma_zfree(aiocb_zone, job);
1652ac41f2efSAlfred Perlstein 		return (0);
16532244ea07SJohn Dyson 	}
16542244ea07SJohn Dyson 
16555652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1656cb679c38SJonathan Lemon 		goto no_kqueue;
16575652770dSJohn Baldwin 	evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1658fde80935SDavid Xu 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1659fde80935SDavid Xu 		error = EINVAL;
1660022ca2fcSAlan Somers 		goto err3;
1661fde80935SDavid Xu 	}
16625652770dSJohn Baldwin 	kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
166336c4960eSMark Johnston 	memset(&kev, 0, sizeof(kev));
16645652770dSJohn Baldwin 	kev.ident = (uintptr_t)job->ujob;
1665cb679c38SJonathan Lemon 	kev.filter = EVFILT_AIO;
1666fde80935SDavid Xu 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
16675652770dSJohn Baldwin 	kev.data = (intptr_t)job;
16685652770dSJohn Baldwin 	kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1669792843c3SMark Johnston 	error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1670f3215338SJohn Baldwin 	if (error)
1671022ca2fcSAlan Somers 		goto err3;
1672f3215338SJohn Baldwin 
1673cb679c38SJonathan Lemon no_kqueue:
1674cb679c38SJonathan Lemon 
16755652770dSJohn Baldwin 	ops->store_error(ujob, EINPROGRESS);
16765652770dSJohn Baldwin 	job->uaiocb._aiocb_private.error = EINPROGRESS;
16775652770dSJohn Baldwin 	job->userproc = p;
16785652770dSJohn Baldwin 	job->cred = crhold(td->td_ucred);
1679f3215338SJohn Baldwin 	job->jobflags = KAIOCB_QUEUEING;
16805652770dSJohn Baldwin 	job->lio = lj;
16812244ea07SJohn Dyson 
16822247f489SAlan Somers 	if (opcode & LIO_VECTORED) {
1683022ca2fcSAlan Somers 		/* Use the uio copied in by aio_copyin */
1684022ca2fcSAlan Somers 		MPASS(job->uiop != &job->uio && job->uiop != NULL);
16852247f489SAlan Somers 	} else {
1686022ca2fcSAlan Somers 		/* Setup the inline uio */
1687022ca2fcSAlan Somers 		job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1688022ca2fcSAlan Somers 		job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1689022ca2fcSAlan Somers 		job->uio.uio_iov = job->iov;
1690022ca2fcSAlan Somers 		job->uio.uio_iovcnt = 1;
1691022ca2fcSAlan Somers 		job->uio.uio_resid = job->uaiocb.aio_nbytes;
1692022ca2fcSAlan Somers 		job->uio.uio_segflg = UIO_USERSPACE;
1693022ca2fcSAlan Somers 		job->uiop = &job->uio;
1694022ca2fcSAlan Somers 	}
16952247f489SAlan Somers 	switch (opcode & (LIO_READ | LIO_WRITE)) {
1696022ca2fcSAlan Somers 	case LIO_READ:
1697022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_READ;
1698022ca2fcSAlan Somers 		break;
1699022ca2fcSAlan Somers 	case LIO_WRITE:
1700022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_WRITE;
1701022ca2fcSAlan Somers 		break;
1702022ca2fcSAlan Somers 	}
1703022ca2fcSAlan Somers 	job->uiop->uio_offset = job->uaiocb.aio_offset;
1704022ca2fcSAlan Somers 	job->uiop->uio_td = td;
1705022ca2fcSAlan Somers 
1706f3215338SJohn Baldwin 	if (opcode == LIO_MLOCK) {
1707f3215338SJohn Baldwin 		aio_schedule(job, aio_process_mlock);
1708f3215338SJohn Baldwin 		error = 0;
1709f3215338SJohn Baldwin 	} else if (fp->f_ops->fo_aio_queue == NULL)
1710f3215338SJohn Baldwin 		error = aio_queue_file(fp, job);
1711f3215338SJohn Baldwin 	else
1712f3215338SJohn Baldwin 		error = fo_aio_queue(fp, job);
1713f3215338SJohn Baldwin 	if (error)
171445c2c7c4SKonstantin Belousov 		goto err4;
1715f3215338SJohn Baldwin 
1716f3215338SJohn Baldwin 	AIO_LOCK(ki);
1717f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_QUEUEING;
1718f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1719f3215338SJohn Baldwin 	ki->kaio_count++;
1720f3215338SJohn Baldwin 	if (lj)
1721f3215338SJohn Baldwin 		lj->lioj_count++;
1722f3215338SJohn Baldwin 	atomic_add_int(&num_queue_count, 1);
1723f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
1724f3215338SJohn Baldwin 		/*
1725f3215338SJohn Baldwin 		 * The queue callback completed the request synchronously.
1726f3215338SJohn Baldwin 		 * The bulk of the completion is deferred in that case
1727f3215338SJohn Baldwin 		 * until this point.
1728f3215338SJohn Baldwin 		 */
1729f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
1730f3215338SJohn Baldwin 	} else
1731f3215338SJohn Baldwin 		TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1732f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1733f3215338SJohn Baldwin 	return (0);
1734f3215338SJohn Baldwin 
173545c2c7c4SKonstantin Belousov err4:
173645c2c7c4SKonstantin Belousov 	crfree(job->cred);
1737022ca2fcSAlan Somers err3:
1738f3215338SJohn Baldwin 	if (fp)
1739f3215338SJohn Baldwin 		fdrop(fp, td);
1740022ca2fcSAlan Somers 	knlist_delete(&job->klist, curthread, 0);
1741022ca2fcSAlan Somers err2:
1742022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
1743022ca2fcSAlan Somers 		free(job->uiop, M_IOV);
1744f3215338SJohn Baldwin 	uma_zfree(aiocb_zone, job);
1745022ca2fcSAlan Somers err1:
1746f3215338SJohn Baldwin 	ops->store_error(ujob, error);
1747f3215338SJohn Baldwin 	return (error);
1748f3215338SJohn Baldwin }
1749f3215338SJohn Baldwin 
1750f3215338SJohn Baldwin static void
1751f3215338SJohn Baldwin aio_cancel_daemon_job(struct kaiocb *job)
1752f3215338SJohn Baldwin {
1753f3215338SJohn Baldwin 
1754f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1755f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1756f3215338SJohn Baldwin 		TAILQ_REMOVE(&aio_jobs, job, list);
1757f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1758f3215338SJohn Baldwin 	aio_cancel(job);
1759f3215338SJohn Baldwin }
1760f3215338SJohn Baldwin 
1761f3215338SJohn Baldwin void
1762f3215338SJohn Baldwin aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1763f3215338SJohn Baldwin {
1764f3215338SJohn Baldwin 
1765f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1766f3215338SJohn Baldwin 	if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1767f3215338SJohn Baldwin 		mtx_unlock(&aio_job_mtx);
1768f3215338SJohn Baldwin 		aio_cancel(job);
1769f3215338SJohn Baldwin 		return;
1770f3215338SJohn Baldwin 	}
1771f3215338SJohn Baldwin 	job->handle_fn = func;
1772f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1773f3215338SJohn Baldwin 	aio_kick_nowait(job->userproc);
1774f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1775f3215338SJohn Baldwin }
1776f3215338SJohn Baldwin 
1777f3215338SJohn Baldwin static void
1778f3215338SJohn Baldwin aio_cancel_sync(struct kaiocb *job)
1779f3215338SJohn Baldwin {
1780f3215338SJohn Baldwin 	struct kaioinfo *ki;
1781f3215338SJohn Baldwin 
1782f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1783005ce8e4SJohn Baldwin 	AIO_LOCK(ki);
1784f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1785f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1786005ce8e4SJohn Baldwin 	AIO_UNLOCK(ki);
1787f3215338SJohn Baldwin 	aio_cancel(job);
1788f3215338SJohn Baldwin }
1789f3215338SJohn Baldwin 
1790f3215338SJohn Baldwin int
1791f3215338SJohn Baldwin aio_queue_file(struct file *fp, struct kaiocb *job)
1792f3215338SJohn Baldwin {
1793f3215338SJohn Baldwin 	struct kaioinfo *ki;
1794f3215338SJohn Baldwin 	struct kaiocb *job2;
17959fe297bbSKonstantin Belousov 	struct vnode *vp;
17969fe297bbSKonstantin Belousov 	struct mount *mp;
1797f54c5606SJohn Baldwin 	int error;
17989fe297bbSKonstantin Belousov 	bool safe;
1799f3215338SJohn Baldwin 
1800f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
180172bce9ffSAlan Somers 	error = aio_qbio(job->userproc, job);
1802f54c5606SJohn Baldwin 	if (error >= 0)
1803f54c5606SJohn Baldwin 		return (error);
18049fe297bbSKonstantin Belousov 	safe = false;
18059fe297bbSKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE) {
18069fe297bbSKonstantin Belousov 		vp = fp->f_vnode;
18079fe297bbSKonstantin Belousov 		if (vp->v_type == VREG || vp->v_type == VDIR) {
18089fe297bbSKonstantin Belousov 			mp = fp->f_vnode->v_mount;
18099fe297bbSKonstantin Belousov 			if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
18109fe297bbSKonstantin Belousov 				safe = true;
18119fe297bbSKonstantin Belousov 		}
18129fe297bbSKonstantin Belousov 	}
18139c20dc99SJohn Baldwin 	if (!(safe || enable_aio_unsafe)) {
18149c20dc99SJohn Baldwin 		counted_warning(&unsafe_warningcnt,
18159c20dc99SJohn Baldwin 		    "is attempting to use unsafe AIO requests");
1816f3215338SJohn Baldwin 		return (EOPNOTSUPP);
18179c20dc99SJohn Baldwin 	}
181884af4da6SJohn Dyson 
18192247f489SAlan Somers 	if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
18207e409184SJohn Baldwin 		aio_schedule(job, aio_process_rw);
18217e409184SJohn Baldwin 		error = 0;
18222247f489SAlan Somers 	} else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1823f3215338SJohn Baldwin 		AIO_LOCK(ki);
18245652770dSJohn Baldwin 		TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
18255652770dSJohn Baldwin 			if (job2->fd_file == job->fd_file &&
18262247f489SAlan Somers 			    ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
18275652770dSJohn Baldwin 			    job2->seqno < job->seqno) {
18285652770dSJohn Baldwin 				job2->jobflags |= KAIOCB_CHECKSYNC;
18295652770dSJohn Baldwin 				job->pending++;
1830dbbccfe9SDavid Xu 			}
1831dbbccfe9SDavid Xu 		}
18325652770dSJohn Baldwin 		if (job->pending != 0) {
1833005ce8e4SJohn Baldwin 			if (!aio_set_cancel_function_locked(job,
1834005ce8e4SJohn Baldwin 				aio_cancel_sync)) {
1835f3215338SJohn Baldwin 				AIO_UNLOCK(ki);
1836f3215338SJohn Baldwin 				aio_cancel(job);
1837f3215338SJohn Baldwin 				return (0);
1838f3215338SJohn Baldwin 			}
18395652770dSJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1840759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1841f3215338SJohn Baldwin 			return (0);
1842dbbccfe9SDavid Xu 		}
1843759ccccaSDavid Xu 		AIO_UNLOCK(ki);
1844f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
1845f3215338SJohn Baldwin 		error = 0;
18462247f489SAlan Somers 	} else {
1847f3215338SJohn Baldwin 		error = EINVAL;
1848f3215338SJohn Baldwin 	}
184999eee864SDavid Xu 	return (error);
185099eee864SDavid Xu }
185199eee864SDavid Xu 
185299eee864SDavid Xu static void
185399eee864SDavid Xu aio_kick_nowait(struct proc *userp)
185499eee864SDavid Xu {
185599eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
185639314b7dSJohn Baldwin 	struct aioproc *aiop;
185799eee864SDavid Xu 
185899eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
185999eee864SDavid Xu 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
186099eee864SDavid Xu 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
186139314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
186239314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18630dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
186486bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1865c85650caSJohn Baldwin 		taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
186699eee864SDavid Xu 	}
186799eee864SDavid Xu }
186899eee864SDavid Xu 
1869dbbccfe9SDavid Xu static int
187099eee864SDavid Xu aio_kick(struct proc *userp)
187199eee864SDavid Xu {
187299eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
187339314b7dSJohn Baldwin 	struct aioproc *aiop;
1874dbbccfe9SDavid Xu 	int error, ret = 0;
187599eee864SDavid Xu 
187699eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
187799eee864SDavid Xu retryproc:
1878d254af07SMatthew Dillon 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
18792244ea07SJohn Dyson 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
188039314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
188139314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18820dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
188386bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1884fd3bf775SJohn Dyson 		num_aio_resv_start++;
18851ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
18861ce91824SDavid Xu 		error = aio_newproc(&num_aio_resv_start);
18871ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
18881ce91824SDavid Xu 		if (error) {
188984af4da6SJohn Dyson 			num_aio_resv_start--;
18902244ea07SJohn Dyson 			goto retryproc;
1891fd3bf775SJohn Dyson 		}
1892dbbccfe9SDavid Xu 	} else {
1893dbbccfe9SDavid Xu 		ret = -1;
18941ce91824SDavid Xu 	}
1895dbbccfe9SDavid Xu 	return (ret);
189699eee864SDavid Xu }
18971ce91824SDavid Xu 
189899eee864SDavid Xu static void
189999eee864SDavid Xu aio_kick_helper(void *context, int pending)
190099eee864SDavid Xu {
190199eee864SDavid Xu 	struct proc *userp = context;
190299eee864SDavid Xu 
190399eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
1904dbbccfe9SDavid Xu 	while (--pending >= 0) {
1905dbbccfe9SDavid Xu 		if (aio_kick(userp))
1906dbbccfe9SDavid Xu 			break;
1907dbbccfe9SDavid Xu 	}
190899eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
19092244ea07SJohn Dyson }
19102244ea07SJohn Dyson 
1911fd3bf775SJohn Dyson /*
1912bfbbc4aaSJason Evans  * Support the aio_return system call, as a side-effect, kernel resources are
1913bfbbc4aaSJason Evans  * released.
19142244ea07SJohn Dyson  */
19153858a1f4SJohn Baldwin static int
19165652770dSJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1917fd3bf775SJohn Dyson {
1918b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19195652770dSJohn Baldwin 	struct kaiocb *job;
19202244ea07SJohn Dyson 	struct kaioinfo *ki;
1921bb430bc7SJohn Baldwin 	long status, error;
19222244ea07SJohn Dyson 
1923c0bf5caaSAlan Cox 	ki = p->p_aioinfo;
1924c0bf5caaSAlan Cox 	if (ki == NULL)
1925ac41f2efSAlfred Perlstein 		return (EINVAL);
1926759ccccaSDavid Xu 	AIO_LOCK(ki);
19275652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_done, plist) {
19285652770dSJohn Baldwin 		if (job->ujob == ujob)
1929c0bf5caaSAlan Cox 			break;
1930c0bf5caaSAlan Cox 	}
19315652770dSJohn Baldwin 	if (job != NULL) {
1932f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
19335652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
19345652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
19351ce91824SDavid Xu 		td->td_retval[0] = status;
1936b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
1937b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
1938b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
1939b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
19405652770dSJohn Baldwin 		aio_free_entry(job);
1941759ccccaSDavid Xu 		AIO_UNLOCK(ki);
19425652770dSJohn Baldwin 		ops->store_error(ujob, error);
19435652770dSJohn Baldwin 		ops->store_status(ujob, status);
194455a122bfSDavid Xu 	} else {
19451ce91824SDavid Xu 		error = EINVAL;
1946759ccccaSDavid Xu 		AIO_UNLOCK(ki);
194755a122bfSDavid Xu 	}
19481ce91824SDavid Xu 	return (error);
19492244ea07SJohn Dyson }
19502244ea07SJohn Dyson 
19513858a1f4SJohn Baldwin int
19528451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap)
19533858a1f4SJohn Baldwin {
19543858a1f4SJohn Baldwin 
19553858a1f4SJohn Baldwin 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
19563858a1f4SJohn Baldwin }
19573858a1f4SJohn Baldwin 
19582244ea07SJohn Dyson /*
1959bfbbc4aaSJason Evans  * Allow a process to wakeup when any of the I/O requests are completed.
19602244ea07SJohn Dyson  */
19613858a1f4SJohn Baldwin static int
19623858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
19633858a1f4SJohn Baldwin     struct timespec *ts)
1964fd3bf775SJohn Dyson {
1965b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19664a11ca4eSPoul-Henning Kamp 	struct timeval atv;
19672244ea07SJohn Dyson 	struct kaioinfo *ki;
19685652770dSJohn Baldwin 	struct kaiocb *firstjob, *job;
19693858a1f4SJohn Baldwin 	int error, i, timo;
19702244ea07SJohn Dyson 
19712244ea07SJohn Dyson 	timo = 0;
19723858a1f4SJohn Baldwin 	if (ts) {
19733858a1f4SJohn Baldwin 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
19742244ea07SJohn Dyson 			return (EINVAL);
19752244ea07SJohn Dyson 
19763858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
19772244ea07SJohn Dyson 		if (itimerfix(&atv))
19782244ea07SJohn Dyson 			return (EINVAL);
1979227ee8a1SPoul-Henning Kamp 		timo = tvtohz(&atv);
19802244ea07SJohn Dyson 	}
19812244ea07SJohn Dyson 
19822244ea07SJohn Dyson 	ki = p->p_aioinfo;
19832244ea07SJohn Dyson 	if (ki == NULL)
1984ac41f2efSAlfred Perlstein 		return (EAGAIN);
19852244ea07SJohn Dyson 
19863858a1f4SJohn Baldwin 	if (njoblist == 0)
1987ac41f2efSAlfred Perlstein 		return (0);
19882244ea07SJohn Dyson 
1989759ccccaSDavid Xu 	AIO_LOCK(ki);
19901ce91824SDavid Xu 	for (;;) {
19915652770dSJohn Baldwin 		firstjob = NULL;
19921ce91824SDavid Xu 		error = 0;
19935652770dSJohn Baldwin 		TAILQ_FOREACH(job, &ki->kaio_all, allist) {
199484af4da6SJohn Dyson 			for (i = 0; i < njoblist; i++) {
19955652770dSJohn Baldwin 				if (job->ujob == ujoblist[i]) {
19965652770dSJohn Baldwin 					if (firstjob == NULL)
19975652770dSJohn Baldwin 						firstjob = job;
1998f3215338SJohn Baldwin 					if (job->jobflags & KAIOCB_FINISHED)
19991ce91824SDavid Xu 						goto RETURN;
200084af4da6SJohn Dyson 				}
200184af4da6SJohn Dyson 			}
200284af4da6SJohn Dyson 		}
20031ce91824SDavid Xu 		/* All tasks were finished. */
20045652770dSJohn Baldwin 		if (firstjob == NULL)
20051ce91824SDavid Xu 			break;
20062244ea07SJohn Dyson 
2007fd3bf775SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
2008759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
20091ce91824SDavid Xu 		    "aiospn", timo);
20101ce91824SDavid Xu 		if (error == ERESTART)
20111ce91824SDavid Xu 			error = EINTR;
20121ce91824SDavid Xu 		if (error)
20131ce91824SDavid Xu 			break;
20142244ea07SJohn Dyson 	}
20151ce91824SDavid Xu RETURN:
2016759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20173858a1f4SJohn Baldwin 	return (error);
20183858a1f4SJohn Baldwin }
20193858a1f4SJohn Baldwin 
20203858a1f4SJohn Baldwin int
20218451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
20223858a1f4SJohn Baldwin {
20233858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
20243858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
20253858a1f4SJohn Baldwin 	int error;
20263858a1f4SJohn Baldwin 
2027913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
20283858a1f4SJohn Baldwin 		return (EINVAL);
20293858a1f4SJohn Baldwin 
20303858a1f4SJohn Baldwin 	if (uap->timeout) {
20313858a1f4SJohn Baldwin 		/* Get timespec struct. */
20323858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
20333858a1f4SJohn Baldwin 			return (error);
20343858a1f4SJohn Baldwin 		tsp = &ts;
20353858a1f4SJohn Baldwin 	} else
20363858a1f4SJohn Baldwin 		tsp = NULL;
20373858a1f4SJohn Baldwin 
2038913b9329SAlan Somers 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
20393858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
20403858a1f4SJohn Baldwin 	if (error == 0)
20413858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2042913b9329SAlan Somers 	free(ujoblist, M_AIOS);
20431ce91824SDavid Xu 	return (error);
20442244ea07SJohn Dyson }
2045ee877a35SJohn Dyson 
2046ee877a35SJohn Dyson /*
204772bce9ffSAlan Somers  * aio_cancel cancels any non-bio aio operations not currently in progress.
2048ee877a35SJohn Dyson  */
2049ee877a35SJohn Dyson int
20508451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2051fd3bf775SJohn Dyson {
2052b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2053dd85920aSJason Evans 	struct kaioinfo *ki;
20545652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
2055dd85920aSJason Evans 	struct file *fp;
20561ce91824SDavid Xu 	int error;
2057dd85920aSJason Evans 	int cancelled = 0;
2058dd85920aSJason Evans 	int notcancelled = 0;
2059dd85920aSJason Evans 	struct vnode *vp;
2060dd85920aSJason Evans 
20612a522eb9SJohn Baldwin 	/* Lookup file object. */
2062cbd92ce6SMatt Macy 	error = fget(td, uap->fd, &cap_no_rights, &fp);
20632a522eb9SJohn Baldwin 	if (error)
20642a522eb9SJohn Baldwin 		return (error);
2065dd85920aSJason Evans 
20661ce91824SDavid Xu 	ki = p->p_aioinfo;
20671ce91824SDavid Xu 	if (ki == NULL)
20681ce91824SDavid Xu 		goto done;
20691ce91824SDavid Xu 
2070dd85920aSJason Evans 	if (fp->f_type == DTYPE_VNODE) {
20713b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
20727ad2a82dSMateusz Guzik 		if (vn_isdisk(vp)) {
20732a522eb9SJohn Baldwin 			fdrop(fp, td);
2074b40ce416SJulian Elischer 			td->td_retval[0] = AIO_NOTCANCELED;
2075ac41f2efSAlfred Perlstein 			return (0);
2076dd85920aSJason Evans 		}
2077dd85920aSJason Evans 	}
2078dd85920aSJason Evans 
2079759ccccaSDavid Xu 	AIO_LOCK(ki);
20805652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
20815652770dSJohn Baldwin 		if ((uap->fd == job->uaiocb.aio_fildes) &&
2082dd85920aSJason Evans 		    ((uap->aiocbp == NULL) ||
20835652770dSJohn Baldwin 		     (uap->aiocbp == job->ujob))) {
2084f3215338SJohn Baldwin 			if (aio_cancel_job(p, ki, job)) {
20851ce91824SDavid Xu 				cancelled++;
2086dd85920aSJason Evans 			} else {
2087dd85920aSJason Evans 				notcancelled++;
2088dd85920aSJason Evans 			}
20891aa4c324SDavid Xu 			if (uap->aiocbp != NULL)
20901aa4c324SDavid Xu 				break;
2091dd85920aSJason Evans 		}
2092dd85920aSJason Evans 	}
2093759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20941ce91824SDavid Xu 
2095ad49abc0SAlan Cox done:
20962a522eb9SJohn Baldwin 	fdrop(fp, td);
20971aa4c324SDavid Xu 
20981aa4c324SDavid Xu 	if (uap->aiocbp != NULL) {
2099dd85920aSJason Evans 		if (cancelled) {
2100b40ce416SJulian Elischer 			td->td_retval[0] = AIO_CANCELED;
2101ac41f2efSAlfred Perlstein 			return (0);
2102dd85920aSJason Evans 		}
21031aa4c324SDavid Xu 	}
21041aa4c324SDavid Xu 
21051aa4c324SDavid Xu 	if (notcancelled) {
21061aa4c324SDavid Xu 		td->td_retval[0] = AIO_NOTCANCELED;
21071aa4c324SDavid Xu 		return (0);
21081aa4c324SDavid Xu 	}
21091aa4c324SDavid Xu 
21101aa4c324SDavid Xu 	if (cancelled) {
21111aa4c324SDavid Xu 		td->td_retval[0] = AIO_CANCELED;
21121aa4c324SDavid Xu 		return (0);
21131aa4c324SDavid Xu 	}
21141aa4c324SDavid Xu 
2115b40ce416SJulian Elischer 	td->td_retval[0] = AIO_ALLDONE;
2116dd85920aSJason Evans 
2117ac41f2efSAlfred Perlstein 	return (0);
2118ee877a35SJohn Dyson }
2119ee877a35SJohn Dyson 
2120ee877a35SJohn Dyson /*
2121873fbcd7SRobert Watson  * aio_error is implemented in the kernel level for compatibility purposes
2122873fbcd7SRobert Watson  * only.  For a user mode async implementation, it would be best to do it in
2123873fbcd7SRobert Watson  * a userland subroutine.
2124ee877a35SJohn Dyson  */
21253858a1f4SJohn Baldwin static int
21265652770dSJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2127fd3bf775SJohn Dyson {
2128b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
21295652770dSJohn Baldwin 	struct kaiocb *job;
21302244ea07SJohn Dyson 	struct kaioinfo *ki;
21311ce91824SDavid Xu 	int status;
2132ee877a35SJohn Dyson 
21332244ea07SJohn Dyson 	ki = p->p_aioinfo;
21341ce91824SDavid Xu 	if (ki == NULL) {
21351ce91824SDavid Xu 		td->td_retval[0] = EINVAL;
21361ce91824SDavid Xu 		return (0);
21371ce91824SDavid Xu 	}
2138ee877a35SJohn Dyson 
2139759ccccaSDavid Xu 	AIO_LOCK(ki);
21405652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_all, allist) {
21415652770dSJohn Baldwin 		if (job->ujob == ujob) {
2142f3215338SJohn Baldwin 			if (job->jobflags & KAIOCB_FINISHED)
21431ce91824SDavid Xu 				td->td_retval[0] =
21445652770dSJohn Baldwin 					job->uaiocb._aiocb_private.error;
21451ce91824SDavid Xu 			else
2146b40ce416SJulian Elischer 				td->td_retval[0] = EINPROGRESS;
2147759ccccaSDavid Xu 			AIO_UNLOCK(ki);
2148ac41f2efSAlfred Perlstein 			return (0);
21492244ea07SJohn Dyson 		}
21502244ea07SJohn Dyson 	}
2151759ccccaSDavid Xu 	AIO_UNLOCK(ki);
215284af4da6SJohn Dyson 
21532244ea07SJohn Dyson 	/*
2154a9bf5e37SDavid Xu 	 * Hack for failure of aio_aqueue.
21552244ea07SJohn Dyson 	 */
21565652770dSJohn Baldwin 	status = ops->fetch_status(ujob);
21571ce91824SDavid Xu 	if (status == -1) {
21585652770dSJohn Baldwin 		td->td_retval[0] = ops->fetch_error(ujob);
21591ce91824SDavid Xu 		return (0);
21601ce91824SDavid Xu 	}
21611ce91824SDavid Xu 
21621ce91824SDavid Xu 	td->td_retval[0] = EINVAL;
21631ce91824SDavid Xu 	return (0);
2164ee877a35SJohn Dyson }
2165ee877a35SJohn Dyson 
21663858a1f4SJohn Baldwin int
21678451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap)
21683858a1f4SJohn Baldwin {
21693858a1f4SJohn Baldwin 
21703858a1f4SJohn Baldwin 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
21713858a1f4SJohn Baldwin }
21723858a1f4SJohn Baldwin 
2173eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */
2174399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2175ee877a35SJohn Dyson int
2176399e8c17SJohn Baldwin freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
21770972628aSDavid Xu {
21780972628aSDavid Xu 
21793858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
21803858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
21810972628aSDavid Xu }
2182399e8c17SJohn Baldwin #endif
21830972628aSDavid Xu 
21840972628aSDavid Xu int
21858451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap)
2186fd3bf775SJohn Dyson {
218721d56e9cSAlfred Perlstein 
21883858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2189ee877a35SJohn Dyson }
2190ee877a35SJohn Dyson 
2191022ca2fcSAlan Somers int
2192022ca2fcSAlan Somers sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2193022ca2fcSAlan Somers {
2194022ca2fcSAlan Somers 
2195022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2196022ca2fcSAlan Somers }
2197022ca2fcSAlan Somers 
2198eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */
2199399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2200ee877a35SJohn Dyson int
2201399e8c17SJohn Baldwin freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
22020972628aSDavid Xu {
22030972628aSDavid Xu 
22043858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
22053858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
22060972628aSDavid Xu }
2207399e8c17SJohn Baldwin #endif
22080972628aSDavid Xu 
22090972628aSDavid Xu int
22108451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap)
2211fd3bf775SJohn Dyson {
221221d56e9cSAlfred Perlstein 
22133858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
22140972628aSDavid Xu }
22150972628aSDavid Xu 
22166160e12cSGleb Smirnoff int
2217022ca2fcSAlan Somers sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2218022ca2fcSAlan Somers {
2219022ca2fcSAlan Somers 
2220022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2221022ca2fcSAlan Somers }
2222022ca2fcSAlan Somers 
2223022ca2fcSAlan Somers int
22246160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
22256160e12cSGleb Smirnoff {
22266160e12cSGleb Smirnoff 
22276160e12cSGleb Smirnoff 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
22286160e12cSGleb Smirnoff }
22296160e12cSGleb Smirnoff 
22300972628aSDavid Xu static int
22313858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
22323858a1f4SJohn Baldwin     struct aiocb **acb_list, int nent, struct sigevent *sig,
22333858a1f4SJohn Baldwin     struct aiocb_ops *ops)
22340972628aSDavid Xu {
2235b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22365652770dSJohn Baldwin 	struct aiocb *job;
22372244ea07SJohn Dyson 	struct kaioinfo *ki;
22381ce91824SDavid Xu 	struct aioliojob *lj;
223969cd28daSDoug Ambrisko 	struct kevent kev;
22401ce91824SDavid Xu 	int error;
224152c09831SAlan Somers 	int nagain, nerror;
2242ee877a35SJohn Dyson 	int i;
2243ee877a35SJohn Dyson 
22443858a1f4SJohn Baldwin 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2245ac41f2efSAlfred Perlstein 		return (EINVAL);
22462244ea07SJohn Dyson 
2247913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
2248ac41f2efSAlfred Perlstein 		return (EINVAL);
22492244ea07SJohn Dyson 
2250bfbbc4aaSJason Evans 	if (p->p_aioinfo == NULL)
22512244ea07SJohn Dyson 		aio_init_aioinfo(p);
22522244ea07SJohn Dyson 
22532244ea07SJohn Dyson 	ki = p->p_aioinfo;
22542244ea07SJohn Dyson 
2255a163d034SWarner Losh 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
225684af4da6SJohn Dyson 	lj->lioj_flags = 0;
22571ce91824SDavid Xu 	lj->lioj_count = 0;
22581ce91824SDavid Xu 	lj->lioj_finished_count = 0;
22592e5f6152SMark Johnston 	lj->lioj_signal.sigev_notify = SIGEV_NONE;
2260d8b0556cSKonstantin Belousov 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
22614c0fb2cfSDavid Xu 	ksiginfo_init(&lj->lioj_ksi);
226269cd28daSDoug Ambrisko 
226384af4da6SJohn Dyson 	/*
2264bfbbc4aaSJason Evans 	 * Setup signal.
226584af4da6SJohn Dyson 	 */
22663858a1f4SJohn Baldwin 	if (sig && (mode == LIO_NOWAIT)) {
22673858a1f4SJohn Baldwin 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
226869cd28daSDoug Ambrisko 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
226969cd28daSDoug Ambrisko 			/* Assume only new style KEVENT */
227036c4960eSMark Johnston 			memset(&kev, 0, sizeof(kev));
227169cd28daSDoug Ambrisko 			kev.filter = EVFILT_LIO;
227269cd28daSDoug Ambrisko 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
22733858a1f4SJohn Baldwin 			kev.ident = (uintptr_t)uacb_list; /* something unique */
227469cd28daSDoug Ambrisko 			kev.data = (intptr_t)lj;
22751ce91824SDavid Xu 			/* pass user defined sigval data */
22761ce91824SDavid Xu 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
22774db71d27SJohn-Mark Gurney 			error = kqfd_register(
2278792843c3SMark Johnston 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2279792843c3SMark Johnston 			    M_WAITOK);
228069cd28daSDoug Ambrisko 			if (error) {
228169cd28daSDoug Ambrisko 				uma_zfree(aiolio_zone, lj);
228269cd28daSDoug Ambrisko 				return (error);
228369cd28daSDoug Ambrisko 			}
22841ce91824SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
22851ce91824SDavid Xu 			;
228668d71118SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
228768d71118SDavid Xu 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
228868d71118SDavid Xu 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
228969cd28daSDoug Ambrisko 					uma_zfree(aiolio_zone, lj);
229069cd28daSDoug Ambrisko 					return EINVAL;
229168d71118SDavid Xu 				}
229284af4da6SJohn Dyson 				lj->lioj_flags |= LIOJ_SIGNAL;
229368d71118SDavid Xu 		} else {
229468d71118SDavid Xu 			uma_zfree(aiolio_zone, lj);
229568d71118SDavid Xu 			return EINVAL;
22964d752b01SAlan Cox 		}
22971ce91824SDavid Xu 	}
229869cd28daSDoug Ambrisko 
2299759ccccaSDavid Xu 	AIO_LOCK(ki);
23002f3cf918SAlfred Perlstein 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
23012244ea07SJohn Dyson 	/*
23021ce91824SDavid Xu 	 * Add extra aiocb count to avoid the lio to be freed
23031ce91824SDavid Xu 	 * by other threads doing aio_waitcomplete or aio_return,
23041ce91824SDavid Xu 	 * and prevent event from being sent until we have queued
23051ce91824SDavid Xu 	 * all tasks.
23061ce91824SDavid Xu 	 */
23071ce91824SDavid Xu 	lj->lioj_count = 1;
2308759ccccaSDavid Xu 	AIO_UNLOCK(ki);
23091ce91824SDavid Xu 
23101ce91824SDavid Xu 	/*
2311bfbbc4aaSJason Evans 	 * Get pointers to the list of I/O requests.
23122244ea07SJohn Dyson 	 */
231352c09831SAlan Somers 	nagain = 0;
2314fd3bf775SJohn Dyson 	nerror = 0;
23153858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++) {
23165652770dSJohn Baldwin 		job = acb_list[i];
23175652770dSJohn Baldwin 		if (job != NULL) {
23185652770dSJohn Baldwin 			error = aio_aqueue(td, job, lj, LIO_NOP, ops);
231952c09831SAlan Somers 			if (error == EAGAIN)
232052c09831SAlan Somers 				nagain++;
232152c09831SAlan Somers 			else if (error != 0)
2322fd3bf775SJohn Dyson 				nerror++;
2323fd3bf775SJohn Dyson 		}
2324fd3bf775SJohn Dyson 	}
23252244ea07SJohn Dyson 
23261ce91824SDavid Xu 	error = 0;
2327759ccccaSDavid Xu 	AIO_LOCK(ki);
23283858a1f4SJohn Baldwin 	if (mode == LIO_WAIT) {
23291ce91824SDavid Xu 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2330fd3bf775SJohn Dyson 			ki->kaio_flags |= KAIO_WAKEUP;
2331759ccccaSDavid Xu 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
23321ce91824SDavid Xu 			    PRIBIO | PCATCH, "aiospn", 0);
23331ce91824SDavid Xu 			if (error == ERESTART)
23341ce91824SDavid Xu 				error = EINTR;
23351ce91824SDavid Xu 			if (error)
23361ce91824SDavid Xu 				break;
23371ce91824SDavid Xu 		}
23381ce91824SDavid Xu 	} else {
23391ce91824SDavid Xu 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
23401ce91824SDavid Xu 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
23411ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
23421ce91824SDavid Xu 				KNOTE_LOCKED(&lj->klist, 1);
23431ce91824SDavid Xu 			}
234429331656SKonstantin Belousov 			if ((lj->lioj_flags & (LIOJ_SIGNAL |
234529331656SKonstantin Belousov 			    LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
234629331656SKonstantin Belousov 			    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
23471ce91824SDavid Xu 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
23486814c2daSKonstantin Belousov 				aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
23496814c2daSKonstantin Belousov 				    lj->lioj_count != 1);
23501ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
23512244ea07SJohn Dyson 			}
23522244ea07SJohn Dyson 		}
23531ce91824SDavid Xu 	}
23541ce91824SDavid Xu 	lj->lioj_count--;
23551ce91824SDavid Xu 	if (lj->lioj_count == 0) {
23561ce91824SDavid Xu 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
23571ce91824SDavid Xu 		knlist_delete(&lj->klist, curthread, 1);
2358759ccccaSDavid Xu 		PROC_LOCK(p);
23591ce91824SDavid Xu 		sigqueue_take(&lj->lioj_ksi);
23601ce91824SDavid Xu 		PROC_UNLOCK(p);
2361759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23621ce91824SDavid Xu 		uma_zfree(aiolio_zone, lj);
23631ce91824SDavid Xu 	} else
2364759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23652244ea07SJohn Dyson 
23661ce91824SDavid Xu 	if (nerror)
23671ce91824SDavid Xu 		return (EIO);
236852c09831SAlan Somers 	else if (nagain)
236952c09831SAlan Somers 		return (EAGAIN);
237052c09831SAlan Somers 	else
23711ce91824SDavid Xu 		return (error);
2372ee877a35SJohn Dyson }
2373fd3bf775SJohn Dyson 
23743858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
2375399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
23763858a1f4SJohn Baldwin int
2377399e8c17SJohn Baldwin freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
23783858a1f4SJohn Baldwin {
23793858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23803858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23813858a1f4SJohn Baldwin 	struct osigevent osig;
23823858a1f4SJohn Baldwin 	int error, nent;
23833858a1f4SJohn Baldwin 
23843858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23853858a1f4SJohn Baldwin 		return (EINVAL);
23863858a1f4SJohn Baldwin 
23873858a1f4SJohn Baldwin 	nent = uap->nent;
2388913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
23893858a1f4SJohn Baldwin 		return (EINVAL);
23903858a1f4SJohn Baldwin 
23913858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23923858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
23933858a1f4SJohn Baldwin 		if (error)
23943858a1f4SJohn Baldwin 			return (error);
23953858a1f4SJohn Baldwin 		error = convert_old_sigevent(&osig, &sig);
23963858a1f4SJohn Baldwin 		if (error)
23973858a1f4SJohn Baldwin 			return (error);
23983858a1f4SJohn Baldwin 		sigp = &sig;
23993858a1f4SJohn Baldwin 	} else
24003858a1f4SJohn Baldwin 		sigp = NULL;
24013858a1f4SJohn Baldwin 
24023858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24033858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24043858a1f4SJohn Baldwin 	if (error == 0)
24053858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode,
24063858a1f4SJohn Baldwin 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
24073858a1f4SJohn Baldwin 		    &aiocb_ops_osigevent);
24083858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24093858a1f4SJohn Baldwin 	return (error);
24103858a1f4SJohn Baldwin }
2411399e8c17SJohn Baldwin #endif
24123858a1f4SJohn Baldwin 
24133858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
24143858a1f4SJohn Baldwin int
24158451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
24163858a1f4SJohn Baldwin {
24173858a1f4SJohn Baldwin 	struct aiocb **acb_list;
24183858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
24193858a1f4SJohn Baldwin 	int error, nent;
24203858a1f4SJohn Baldwin 
24213858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
24223858a1f4SJohn Baldwin 		return (EINVAL);
24233858a1f4SJohn Baldwin 
24243858a1f4SJohn Baldwin 	nent = uap->nent;
2425913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
24263858a1f4SJohn Baldwin 		return (EINVAL);
24273858a1f4SJohn Baldwin 
24283858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
24293858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig, sizeof(sig));
24303858a1f4SJohn Baldwin 		if (error)
24313858a1f4SJohn Baldwin 			return (error);
24323858a1f4SJohn Baldwin 		sigp = &sig;
24333858a1f4SJohn Baldwin 	} else
24343858a1f4SJohn Baldwin 		sigp = NULL;
24353858a1f4SJohn Baldwin 
24363858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24373858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24383858a1f4SJohn Baldwin 	if (error == 0)
24393858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
24403858a1f4SJohn Baldwin 		    nent, sigp, &aiocb_ops);
24413858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24423858a1f4SJohn Baldwin 	return (error);
24433858a1f4SJohn Baldwin }
24443858a1f4SJohn Baldwin 
2445fd3bf775SJohn Dyson static void
2446022ca2fcSAlan Somers aio_biocleanup(struct bio *bp)
2447fd3bf775SJohn Dyson {
24485652770dSJohn Baldwin 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
244927b8220dSDavid Xu 	struct kaioinfo *ki;
245001206038SAlan Somers 	struct buf *pbuf = (struct buf *)bp->bio_caller2;
24511ce91824SDavid Xu 
2452f743d981SAlexander Motin 	/* Release mapping into kernel space. */
245301206038SAlan Somers 	if (pbuf != NULL) {
245401206038SAlan Somers 		MPASS(pbuf->b_npages <= atop(maxphys) + 1);
245501206038SAlan Somers 		pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
245601206038SAlan Somers 		vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
245701206038SAlan Somers 		uma_zfree(pbuf_zone, pbuf);
2458f743d981SAlexander Motin 		atomic_subtract_int(&num_buf_aio, 1);
2459a9d4fe97SKonstantin Belousov 		ki = job->userproc->p_aioinfo;
2460f3215338SJohn Baldwin 		AIO_LOCK(ki);
2461f3215338SJohn Baldwin 		ki->kaio_buffer_count--;
2462f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
2463cd853791SKonstantin Belousov 	} else {
246401206038SAlan Somers 		MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
246501206038SAlan Somers 		vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
246601206038SAlan Somers 		free(bp->bio_ma, M_TEMP);
24678091e52bSJohn Baldwin 		atomic_subtract_int(&num_unmapped_aio, 1);
2468cd853791SKonstantin Belousov 	}
2469f743d981SAlexander Motin 	g_destroy_bio(bp);
247084af4da6SJohn Dyson }
2471bfbbc4aaSJason Evans 
2472022ca2fcSAlan Somers static void
2473022ca2fcSAlan Somers aio_biowakeup(struct bio *bp)
2474022ca2fcSAlan Somers {
2475022ca2fcSAlan Somers 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2476022ca2fcSAlan Somers 	size_t nbytes;
2477022ca2fcSAlan Somers 	long bcount = bp->bio_bcount;
2478022ca2fcSAlan Somers 	long resid = bp->bio_resid;
2479*e9c7ec22SMateusz Guzik 	int opcode, nblks;
2480022ca2fcSAlan Somers 	int bio_error = bp->bio_error;
2481022ca2fcSAlan Somers 	uint16_t flags = bp->bio_flags;
2482022ca2fcSAlan Somers 
2483022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
2484022ca2fcSAlan Somers 
2485022ca2fcSAlan Somers 	aio_biocleanup(bp);
2486022ca2fcSAlan Somers 
2487022ca2fcSAlan Somers 	nbytes =bcount - resid;
2488022ca2fcSAlan Somers 	atomic_add_acq_long(&job->nbytes, nbytes);
2489022ca2fcSAlan Somers 	nblks = btodb(nbytes);
2490022ca2fcSAlan Somers 	/*
2491022ca2fcSAlan Somers 	 * If multiple bios experienced an error, the job will reflect the
2492022ca2fcSAlan Somers 	 * error of whichever failed bio completed last.
2493022ca2fcSAlan Somers 	 */
2494022ca2fcSAlan Somers 	if (flags & BIO_ERROR)
2495022ca2fcSAlan Somers 		atomic_set_int(&job->error, bio_error);
24962247f489SAlan Somers 	if (opcode & LIO_WRITE)
2497022ca2fcSAlan Somers 		atomic_add_int(&job->outblock, nblks);
2498022ca2fcSAlan Somers 	else
2499022ca2fcSAlan Somers 		atomic_add_int(&job->inblock, nblks);
2500022ca2fcSAlan Somers 	atomic_subtract_int(&job->nbio, 1);
2501022ca2fcSAlan Somers 
2502022ca2fcSAlan Somers 
2503022ca2fcSAlan Somers 	if (atomic_load_int(&job->nbio) == 0) {
2504022ca2fcSAlan Somers 		if (atomic_load_int(&job->error))
2505022ca2fcSAlan Somers 			aio_complete(job, -1, job->error);
2506022ca2fcSAlan Somers 		else
2507022ca2fcSAlan Somers 			aio_complete(job, atomic_load_long(&job->nbytes), 0);
2508022ca2fcSAlan Somers 	}
2509022ca2fcSAlan Somers }
2510022ca2fcSAlan Somers 
2511eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */
25123858a1f4SJohn Baldwin static int
25135652770dSJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
25143858a1f4SJohn Baldwin     struct timespec *ts, struct aiocb_ops *ops)
2515bfbbc4aaSJason Evans {
2516b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2517bfbbc4aaSJason Evans 	struct timeval atv;
2518bfbbc4aaSJason Evans 	struct kaioinfo *ki;
25195652770dSJohn Baldwin 	struct kaiocb *job;
25205652770dSJohn Baldwin 	struct aiocb *ujob;
2521bb430bc7SJohn Baldwin 	long error, status;
2522bb430bc7SJohn Baldwin 	int timo;
2523bfbbc4aaSJason Evans 
25245652770dSJohn Baldwin 	ops->store_aiocb(ujobp, NULL);
2525dd85920aSJason Evans 
252638d68e2dSPawel Jakub Dawidek 	if (ts == NULL) {
2527bfbbc4aaSJason Evans 		timo = 0;
252838d68e2dSPawel Jakub Dawidek 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
252938d68e2dSPawel Jakub Dawidek 		timo = -1;
253038d68e2dSPawel Jakub Dawidek 	} else {
25313858a1f4SJohn Baldwin 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2532bfbbc4aaSJason Evans 			return (EINVAL);
2533bfbbc4aaSJason Evans 
25343858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2535bfbbc4aaSJason Evans 		if (itimerfix(&atv))
2536bfbbc4aaSJason Evans 			return (EINVAL);
2537bfbbc4aaSJason Evans 		timo = tvtohz(&atv);
2538bfbbc4aaSJason Evans 	}
2539bfbbc4aaSJason Evans 
25408213baf0SChristian S.J. Peron 	if (p->p_aioinfo == NULL)
2541323fe565SDavid Xu 		aio_init_aioinfo(p);
25428213baf0SChristian S.J. Peron 	ki = p->p_aioinfo;
2543bfbbc4aaSJason Evans 
25441ce91824SDavid Xu 	error = 0;
25455652770dSJohn Baldwin 	job = NULL;
2546759ccccaSDavid Xu 	AIO_LOCK(ki);
25475652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
254838d68e2dSPawel Jakub Dawidek 		if (timo == -1) {
254938d68e2dSPawel Jakub Dawidek 			error = EWOULDBLOCK;
255038d68e2dSPawel Jakub Dawidek 			break;
255138d68e2dSPawel Jakub Dawidek 		}
25521ce91824SDavid Xu 		ki->kaio_flags |= KAIO_WAKEUP;
2553759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
25541ce91824SDavid Xu 		    "aiowc", timo);
255527b8220dSDavid Xu 		if (timo && error == ERESTART)
25561ce91824SDavid Xu 			error = EINTR;
25571ce91824SDavid Xu 		if (error)
25581ce91824SDavid Xu 			break;
25591ce91824SDavid Xu 	}
25601ce91824SDavid Xu 
25615652770dSJohn Baldwin 	if (job != NULL) {
2562f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
25635652770dSJohn Baldwin 		ujob = job->ujob;
25645652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
25655652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
25661ce91824SDavid Xu 		td->td_retval[0] = status;
2567b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
2568b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
2569b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
2570b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
25715652770dSJohn Baldwin 		aio_free_entry(job);
2572759ccccaSDavid Xu 		AIO_UNLOCK(ki);
25735652770dSJohn Baldwin 		ops->store_aiocb(ujobp, ujob);
25745652770dSJohn Baldwin 		ops->store_error(ujob, error);
25755652770dSJohn Baldwin 		ops->store_status(ujob, status);
25761ce91824SDavid Xu 	} else
2577759ccccaSDavid Xu 		AIO_UNLOCK(ki);
2578bfbbc4aaSJason Evans 
2579ac41f2efSAlfred Perlstein 	return (error);
2580bfbbc4aaSJason Evans }
2581cb679c38SJonathan Lemon 
258299eee864SDavid Xu int
25838451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
25843858a1f4SJohn Baldwin {
25853858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
25863858a1f4SJohn Baldwin 	int error;
25873858a1f4SJohn Baldwin 
25883858a1f4SJohn Baldwin 	if (uap->timeout) {
25893858a1f4SJohn Baldwin 		/* Get timespec struct. */
25903858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts, sizeof(ts));
25913858a1f4SJohn Baldwin 		if (error)
25923858a1f4SJohn Baldwin 			return (error);
25933858a1f4SJohn Baldwin 		tsp = &ts;
25943858a1f4SJohn Baldwin 	} else
25953858a1f4SJohn Baldwin 		tsp = NULL;
25963858a1f4SJohn Baldwin 
25973858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
25983858a1f4SJohn Baldwin }
25993858a1f4SJohn Baldwin 
26003858a1f4SJohn Baldwin static int
26015652770dSJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
26023858a1f4SJohn Baldwin     struct aiocb_ops *ops)
260399eee864SDavid Xu {
2604801ac943SThomas Munro 	int listop;
260599eee864SDavid Xu 
2606801ac943SThomas Munro 	switch (op) {
2607801ac943SThomas Munro 	case O_SYNC:
2608801ac943SThomas Munro 		listop = LIO_SYNC;
2609801ac943SThomas Munro 		break;
2610801ac943SThomas Munro 	case O_DSYNC:
2611801ac943SThomas Munro 		listop = LIO_DSYNC;
2612801ac943SThomas Munro 		break;
2613801ac943SThomas Munro 	default:
261499eee864SDavid Xu 		return (EINVAL);
2615801ac943SThomas Munro 	}
2616801ac943SThomas Munro 
2617801ac943SThomas Munro 	return (aio_aqueue(td, ujob, NULL, listop, ops));
26183858a1f4SJohn Baldwin }
26193858a1f4SJohn Baldwin 
26203858a1f4SJohn Baldwin int
26218451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
26223858a1f4SJohn Baldwin {
26233858a1f4SJohn Baldwin 
26243858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
262599eee864SDavid Xu }
262699eee864SDavid Xu 
2627eb8e6d52SEivind Eklund /* kqueue attach function */
2628cb679c38SJonathan Lemon static int
2629cb679c38SJonathan Lemon filt_aioattach(struct knote *kn)
2630cb679c38SJonathan Lemon {
26312b34e843SKonstantin Belousov 	struct kaiocb *job;
26322b34e843SKonstantin Belousov 
26332b34e843SKonstantin Belousov 	job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2634cb679c38SJonathan Lemon 
2635cb679c38SJonathan Lemon 	/*
26365652770dSJohn Baldwin 	 * The job pointer must be validated before using it, so
2637cb679c38SJonathan Lemon 	 * registration is restricted to the kernel; the user cannot
2638cb679c38SJonathan Lemon 	 * set EV_FLAG1.
2639cb679c38SJonathan Lemon 	 */
2640cb679c38SJonathan Lemon 	if ((kn->kn_flags & EV_FLAG1) == 0)
2641cb679c38SJonathan Lemon 		return (EPERM);
26425652770dSJohn Baldwin 	kn->kn_ptr.p_aio = job;
2643cb679c38SJonathan Lemon 	kn->kn_flags &= ~EV_FLAG1;
2644cb679c38SJonathan Lemon 
26455652770dSJohn Baldwin 	knlist_add(&job->klist, kn, 0);
2646cb679c38SJonathan Lemon 
2647cb679c38SJonathan Lemon 	return (0);
2648cb679c38SJonathan Lemon }
2649cb679c38SJonathan Lemon 
2650eb8e6d52SEivind Eklund /* kqueue detach function */
2651cb679c38SJonathan Lemon static void
2652cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn)
2653cb679c38SJonathan Lemon {
26548e9fc278SDoug Ambrisko 	struct knlist *knl;
2655cb679c38SJonathan Lemon 
26568e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_aio->klist;
26578e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26588e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26598e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26608e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
2661cb679c38SJonathan Lemon }
2662cb679c38SJonathan Lemon 
2663eb8e6d52SEivind Eklund /* kqueue filter function */
2664cb679c38SJonathan Lemon /*ARGSUSED*/
2665cb679c38SJonathan Lemon static int
2666cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint)
2667cb679c38SJonathan Lemon {
26685652770dSJohn Baldwin 	struct kaiocb *job = kn->kn_ptr.p_aio;
2669cb679c38SJonathan Lemon 
26705652770dSJohn Baldwin 	kn->kn_data = job->uaiocb._aiocb_private.error;
2671f3215338SJohn Baldwin 	if (!(job->jobflags & KAIOCB_FINISHED))
2672cb679c38SJonathan Lemon 		return (0);
2673cb679c38SJonathan Lemon 	kn->kn_flags |= EV_EOF;
2674cb679c38SJonathan Lemon 	return (1);
2675cb679c38SJonathan Lemon }
267669cd28daSDoug Ambrisko 
267769cd28daSDoug Ambrisko /* kqueue attach function */
267869cd28daSDoug Ambrisko static int
267969cd28daSDoug Ambrisko filt_lioattach(struct knote *kn)
268069cd28daSDoug Ambrisko {
26812b34e843SKonstantin Belousov 	struct aioliojob *lj;
26822b34e843SKonstantin Belousov 
26832b34e843SKonstantin Belousov 	lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
268469cd28daSDoug Ambrisko 
268569cd28daSDoug Ambrisko 	/*
26861ce91824SDavid Xu 	 * The aioliojob pointer must be validated before using it, so
268769cd28daSDoug Ambrisko 	 * registration is restricted to the kernel; the user cannot
268869cd28daSDoug Ambrisko 	 * set EV_FLAG1.
268969cd28daSDoug Ambrisko 	 */
269069cd28daSDoug Ambrisko 	if ((kn->kn_flags & EV_FLAG1) == 0)
269169cd28daSDoug Ambrisko 		return (EPERM);
2692a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_lio = lj;
269369cd28daSDoug Ambrisko 	kn->kn_flags &= ~EV_FLAG1;
269469cd28daSDoug Ambrisko 
269569cd28daSDoug Ambrisko 	knlist_add(&lj->klist, kn, 0);
269669cd28daSDoug Ambrisko 
269769cd28daSDoug Ambrisko 	return (0);
269869cd28daSDoug Ambrisko }
269969cd28daSDoug Ambrisko 
270069cd28daSDoug Ambrisko /* kqueue detach function */
270169cd28daSDoug Ambrisko static void
270269cd28daSDoug Ambrisko filt_liodetach(struct knote *kn)
270369cd28daSDoug Ambrisko {
27048e9fc278SDoug Ambrisko 	struct knlist *knl;
270569cd28daSDoug Ambrisko 
27068e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_lio->klist;
27078e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
27088e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
27098e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
27108e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
271169cd28daSDoug Ambrisko }
271269cd28daSDoug Ambrisko 
271369cd28daSDoug Ambrisko /* kqueue filter function */
271469cd28daSDoug Ambrisko /*ARGSUSED*/
271569cd28daSDoug Ambrisko static int
271669cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint)
271769cd28daSDoug Ambrisko {
2718a8afa221SJean-Sébastien Pédron 	struct aioliojob * lj = kn->kn_ptr.p_lio;
27191ce91824SDavid Xu 
272069cd28daSDoug Ambrisko 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
272169cd28daSDoug Ambrisko }
27223858a1f4SJohn Baldwin 
2723841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2724399e8c17SJohn Baldwin #include <sys/mount.h>
2725399e8c17SJohn Baldwin #include <sys/socket.h>
2726399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32.h>
2727399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_proto.h>
2728399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2729399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_syscall.h>
2730399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_util.h>
27313858a1f4SJohn Baldwin 
27323858a1f4SJohn Baldwin struct __aiocb_private32 {
27333858a1f4SJohn Baldwin 	int32_t	status;
27343858a1f4SJohn Baldwin 	int32_t	error;
27353858a1f4SJohn Baldwin 	uint32_t kernelinfo;
27363858a1f4SJohn Baldwin };
27373858a1f4SJohn Baldwin 
2738399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27393858a1f4SJohn Baldwin typedef struct oaiocb32 {
27403858a1f4SJohn Baldwin 	int	aio_fildes;		/* File descriptor */
27413858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27423858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27433858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27443858a1f4SJohn Baldwin 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
27453858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27463858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27473858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27483858a1f4SJohn Baldwin } oaiocb32_t;
2749399e8c17SJohn Baldwin #endif
27503858a1f4SJohn Baldwin 
27513858a1f4SJohn Baldwin typedef struct aiocb32 {
27523858a1f4SJohn Baldwin 	int32_t	aio_fildes;		/* File descriptor */
27533858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27543858a1f4SJohn Baldwin 	uint32_t aio_buf;	/* I/O buffer in process space */
27553858a1f4SJohn Baldwin 	uint32_t aio_nbytes;	/* Number of bytes for I/O */
27563858a1f4SJohn Baldwin 	int	__spare__[2];
27573858a1f4SJohn Baldwin 	uint32_t __spare2__;
27583858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27593858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27603858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27613858a1f4SJohn Baldwin 	struct	sigevent32 aio_sigevent;	/* Signal to deliver */
27623858a1f4SJohn Baldwin } aiocb32_t;
27633858a1f4SJohn Baldwin 
2764399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27653858a1f4SJohn Baldwin static int
27663858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
27673858a1f4SJohn Baldwin {
27683858a1f4SJohn Baldwin 
27693858a1f4SJohn Baldwin 	/*
27703858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
27713858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
27723858a1f4SJohn Baldwin 	 */
27733858a1f4SJohn Baldwin 	CP(*osig, *nsig, sigev_notify);
27743858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
27753858a1f4SJohn Baldwin 	case SIGEV_NONE:
27763858a1f4SJohn Baldwin 		break;
27773858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
27783858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
27793858a1f4SJohn Baldwin 		break;
27803858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
27813858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
27823858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
27833858a1f4SJohn Baldwin 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
27843858a1f4SJohn Baldwin 		break;
27853858a1f4SJohn Baldwin 	default:
27863858a1f4SJohn Baldwin 		return (EINVAL);
27873858a1f4SJohn Baldwin 	}
27883858a1f4SJohn Baldwin 	return (0);
27893858a1f4SJohn Baldwin }
27903858a1f4SJohn Baldwin 
27913858a1f4SJohn Baldwin static int
2792022ca2fcSAlan Somers aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2793022ca2fcSAlan Somers     int type __unused)
27943858a1f4SJohn Baldwin {
27953858a1f4SJohn Baldwin 	struct oaiocb32 job32;
2796022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
27973858a1f4SJohn Baldwin 	int error;
27983858a1f4SJohn Baldwin 
2799022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
28003858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
28013858a1f4SJohn Baldwin 	if (error)
28023858a1f4SJohn Baldwin 		return (error);
28033858a1f4SJohn Baldwin 
2804022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2805022ca2fcSAlan Somers 
2806022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2807022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2808022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, aio_buf);
2809022ca2fcSAlan Somers 	CP(job32, *kcb, aio_nbytes);
2810022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
2811022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2812022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2813022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2814022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
28153858a1f4SJohn Baldwin 	return (convert_old_sigevent32(&job32.aio_sigevent,
2816022ca2fcSAlan Somers 	    &kcb->aio_sigevent));
28173858a1f4SJohn Baldwin }
2818399e8c17SJohn Baldwin #endif
28193858a1f4SJohn Baldwin 
28203858a1f4SJohn Baldwin static int
2821022ca2fcSAlan Somers aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
28223858a1f4SJohn Baldwin {
28233858a1f4SJohn Baldwin 	struct aiocb32 job32;
2824022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
2825022ca2fcSAlan Somers 	struct iovec32 *iov32;
28263858a1f4SJohn Baldwin 	int error;
28273858a1f4SJohn Baldwin 
28283858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
28293858a1f4SJohn Baldwin 	if (error)
28303858a1f4SJohn Baldwin 		return (error);
2831022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2832022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2833022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
28342884918cSMark Johnston 	if (type == LIO_NOP)
28352884918cSMark Johnston 		type = kcb->aio_lio_opcode;
28362247f489SAlan Somers 	if (type & LIO_VECTORED) {
2837022ca2fcSAlan Somers 		iov32 = PTRIN(job32.aio_iov);
2838022ca2fcSAlan Somers 		CP(job32, *kcb, aio_iovcnt);
2839022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
2840022ca2fcSAlan Somers 		error = freebsd32_copyinuio(iov32,
2841022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
2842022ca2fcSAlan Somers 		if (error)
2843022ca2fcSAlan Somers 			return (error);
2844022ca2fcSAlan Somers 	} else {
2845022ca2fcSAlan Somers 		PTRIN_CP(job32, *kcb, aio_buf);
2846022ca2fcSAlan Somers 		CP(job32, *kcb, aio_nbytes);
2847022ca2fcSAlan Somers 	}
2848022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2849022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2850022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2851022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2852022ca2fcSAlan Somers 	error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2853022ca2fcSAlan Somers 
2854022ca2fcSAlan Somers 	return (error);
28553858a1f4SJohn Baldwin }
28563858a1f4SJohn Baldwin 
28573858a1f4SJohn Baldwin static long
28583858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob)
28593858a1f4SJohn Baldwin {
28603858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28613858a1f4SJohn Baldwin 
28623858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28633858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.status));
28643858a1f4SJohn Baldwin }
28653858a1f4SJohn Baldwin 
28663858a1f4SJohn Baldwin static long
28673858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob)
28683858a1f4SJohn Baldwin {
28693858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28703858a1f4SJohn Baldwin 
28713858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28723858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.error));
28733858a1f4SJohn Baldwin }
28743858a1f4SJohn Baldwin 
28753858a1f4SJohn Baldwin static int
28763858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status)
28773858a1f4SJohn Baldwin {
28783858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28793858a1f4SJohn Baldwin 
28803858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28813858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.status, status));
28823858a1f4SJohn Baldwin }
28833858a1f4SJohn Baldwin 
28843858a1f4SJohn Baldwin static int
28853858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error)
28863858a1f4SJohn Baldwin {
28873858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28883858a1f4SJohn Baldwin 
28893858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28903858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.error, error));
28913858a1f4SJohn Baldwin }
28923858a1f4SJohn Baldwin 
28933858a1f4SJohn Baldwin static int
28943858a1f4SJohn Baldwin aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
28953858a1f4SJohn Baldwin {
28963858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28973858a1f4SJohn Baldwin 
28983858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28993858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
29003858a1f4SJohn Baldwin }
29013858a1f4SJohn Baldwin 
29023858a1f4SJohn Baldwin static int
29033858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
29043858a1f4SJohn Baldwin {
29053858a1f4SJohn Baldwin 
29063858a1f4SJohn Baldwin 	return (suword32(ujobp, (long)ujob));
29073858a1f4SJohn Baldwin }
29083858a1f4SJohn Baldwin 
29093858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = {
2910849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin,
29113858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29123858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29133858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29143858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29153858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29163858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29173858a1f4SJohn Baldwin };
29183858a1f4SJohn Baldwin 
2919399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29203858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = {
2921849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin_old_sigevent,
29223858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
29233858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
29243858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
29253858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
29263858a1f4SJohn Baldwin 	.store_kernelinfo = aiocb32_store_kernelinfo,
29273858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
29283858a1f4SJohn Baldwin };
2929399e8c17SJohn Baldwin #endif
29303858a1f4SJohn Baldwin 
29313858a1f4SJohn Baldwin int
29323858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
29333858a1f4SJohn Baldwin {
29343858a1f4SJohn Baldwin 
29353858a1f4SJohn Baldwin 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29363858a1f4SJohn Baldwin }
29373858a1f4SJohn Baldwin 
29383858a1f4SJohn Baldwin int
29393858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
29403858a1f4SJohn Baldwin {
29413858a1f4SJohn Baldwin 	struct timespec32 ts32;
29423858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
29433858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
29443858a1f4SJohn Baldwin 	uint32_t *ujoblist32;
29453858a1f4SJohn Baldwin 	int error, i;
29463858a1f4SJohn Baldwin 
2947913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
29483858a1f4SJohn Baldwin 		return (EINVAL);
29493858a1f4SJohn Baldwin 
29503858a1f4SJohn Baldwin 	if (uap->timeout) {
29513858a1f4SJohn Baldwin 		/* Get timespec struct. */
29523858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
29533858a1f4SJohn Baldwin 			return (error);
29543858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
29553858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
29563858a1f4SJohn Baldwin 		tsp = &ts;
29573858a1f4SJohn Baldwin 	} else
29583858a1f4SJohn Baldwin 		tsp = NULL;
29593858a1f4SJohn Baldwin 
2960913b9329SAlan Somers 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
29613858a1f4SJohn Baldwin 	ujoblist32 = (uint32_t *)ujoblist;
29623858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
29633858a1f4SJohn Baldwin 	    sizeof(ujoblist32[0]));
29643858a1f4SJohn Baldwin 	if (error == 0) {
2965df485bdbSAlan Somers 		for (i = uap->nent - 1; i >= 0; i--)
29663858a1f4SJohn Baldwin 			ujoblist[i] = PTRIN(ujoblist32[i]);
29673858a1f4SJohn Baldwin 
29683858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
29693858a1f4SJohn Baldwin 	}
2970913b9329SAlan Somers 	free(ujoblist, M_AIOS);
29713858a1f4SJohn Baldwin 	return (error);
29723858a1f4SJohn Baldwin }
29733858a1f4SJohn Baldwin 
29743858a1f4SJohn Baldwin int
29753858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
29763858a1f4SJohn Baldwin {
29773858a1f4SJohn Baldwin 
29783858a1f4SJohn Baldwin 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29793858a1f4SJohn Baldwin }
29803858a1f4SJohn Baldwin 
2981399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29823858a1f4SJohn Baldwin int
2983399e8c17SJohn Baldwin freebsd6_freebsd32_aio_read(struct thread *td,
2984399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_read_args *uap)
29853858a1f4SJohn Baldwin {
29863858a1f4SJohn Baldwin 
29873858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29883858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29893858a1f4SJohn Baldwin }
2990399e8c17SJohn Baldwin #endif
29913858a1f4SJohn Baldwin 
29923858a1f4SJohn Baldwin int
29933858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
29943858a1f4SJohn Baldwin {
29953858a1f4SJohn Baldwin 
29963858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29973858a1f4SJohn Baldwin 	    &aiocb32_ops));
29983858a1f4SJohn Baldwin }
29993858a1f4SJohn Baldwin 
3000022ca2fcSAlan Somers int
3001022ca2fcSAlan Somers freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
3002022ca2fcSAlan Somers {
3003022ca2fcSAlan Somers 
3004022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
3005022ca2fcSAlan Somers 	    &aiocb32_ops));
3006022ca2fcSAlan Somers }
3007022ca2fcSAlan Somers 
3008399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30093858a1f4SJohn Baldwin int
3010399e8c17SJohn Baldwin freebsd6_freebsd32_aio_write(struct thread *td,
3011399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_write_args *uap)
30123858a1f4SJohn Baldwin {
30133858a1f4SJohn Baldwin 
30143858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30153858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
30163858a1f4SJohn Baldwin }
3017399e8c17SJohn Baldwin #endif
30183858a1f4SJohn Baldwin 
30193858a1f4SJohn Baldwin int
30203858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
30213858a1f4SJohn Baldwin {
30223858a1f4SJohn Baldwin 
30233858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
30243858a1f4SJohn Baldwin 	    &aiocb32_ops));
30253858a1f4SJohn Baldwin }
30263858a1f4SJohn Baldwin 
30273858a1f4SJohn Baldwin int
3028022ca2fcSAlan Somers freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3029022ca2fcSAlan Somers {
3030022ca2fcSAlan Somers 
3031022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3032022ca2fcSAlan Somers 	    &aiocb32_ops));
3033022ca2fcSAlan Somers }
3034022ca2fcSAlan Somers 
3035022ca2fcSAlan Somers int
30366160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
30376160e12cSGleb Smirnoff {
30386160e12cSGleb Smirnoff 
30396160e12cSGleb Smirnoff 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
30406160e12cSGleb Smirnoff 	    &aiocb32_ops));
30416160e12cSGleb Smirnoff }
30426160e12cSGleb Smirnoff 
30436160e12cSGleb Smirnoff int
30443858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td,
30453858a1f4SJohn Baldwin     struct freebsd32_aio_waitcomplete_args *uap)
30463858a1f4SJohn Baldwin {
3047e588eeb1SJohn Baldwin 	struct timespec32 ts32;
30483858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
30493858a1f4SJohn Baldwin 	int error;
30503858a1f4SJohn Baldwin 
30513858a1f4SJohn Baldwin 	if (uap->timeout) {
30523858a1f4SJohn Baldwin 		/* Get timespec struct. */
30533858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
30543858a1f4SJohn Baldwin 		if (error)
30553858a1f4SJohn Baldwin 			return (error);
30563858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
30573858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
30583858a1f4SJohn Baldwin 		tsp = &ts;
30593858a1f4SJohn Baldwin 	} else
30603858a1f4SJohn Baldwin 		tsp = NULL;
30613858a1f4SJohn Baldwin 
30623858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
30633858a1f4SJohn Baldwin 	    &aiocb32_ops));
30643858a1f4SJohn Baldwin }
30653858a1f4SJohn Baldwin 
30663858a1f4SJohn Baldwin int
30673858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
30683858a1f4SJohn Baldwin {
30693858a1f4SJohn Baldwin 
30703858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
30713858a1f4SJohn Baldwin 	    &aiocb32_ops));
30723858a1f4SJohn Baldwin }
30733858a1f4SJohn Baldwin 
3074399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30753858a1f4SJohn Baldwin int
3076399e8c17SJohn Baldwin freebsd6_freebsd32_lio_listio(struct thread *td,
3077399e8c17SJohn Baldwin     struct freebsd6_freebsd32_lio_listio_args *uap)
30783858a1f4SJohn Baldwin {
30793858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30803858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30813858a1f4SJohn Baldwin 	struct osigevent32 osig;
30823858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30833858a1f4SJohn Baldwin 	int error, i, nent;
30843858a1f4SJohn Baldwin 
30853858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30863858a1f4SJohn Baldwin 		return (EINVAL);
30873858a1f4SJohn Baldwin 
30883858a1f4SJohn Baldwin 	nent = uap->nent;
3089913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
30903858a1f4SJohn Baldwin 		return (EINVAL);
30913858a1f4SJohn Baldwin 
30923858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30933858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
30943858a1f4SJohn Baldwin 		if (error)
30953858a1f4SJohn Baldwin 			return (error);
30963858a1f4SJohn Baldwin 		error = convert_old_sigevent32(&osig, &sig);
30973858a1f4SJohn Baldwin 		if (error)
30983858a1f4SJohn Baldwin 			return (error);
30993858a1f4SJohn Baldwin 		sigp = &sig;
31003858a1f4SJohn Baldwin 	} else
31013858a1f4SJohn Baldwin 		sigp = NULL;
31023858a1f4SJohn Baldwin 
31033858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31043858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31053858a1f4SJohn Baldwin 	if (error) {
31063858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31073858a1f4SJohn Baldwin 		return (error);
31083858a1f4SJohn Baldwin 	}
31093858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31103858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31113858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31123858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31133858a1f4SJohn Baldwin 
31143858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31153858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31163858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent);
31173858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31183858a1f4SJohn Baldwin 	return (error);
31193858a1f4SJohn Baldwin }
3120399e8c17SJohn Baldwin #endif
31213858a1f4SJohn Baldwin 
31223858a1f4SJohn Baldwin int
31233858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
31243858a1f4SJohn Baldwin {
31253858a1f4SJohn Baldwin 	struct aiocb **acb_list;
31263858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
31273858a1f4SJohn Baldwin 	struct sigevent32 sig32;
31283858a1f4SJohn Baldwin 	uint32_t *acb_list32;
31293858a1f4SJohn Baldwin 	int error, i, nent;
31303858a1f4SJohn Baldwin 
31313858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
31323858a1f4SJohn Baldwin 		return (EINVAL);
31333858a1f4SJohn Baldwin 
31343858a1f4SJohn Baldwin 	nent = uap->nent;
3135913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
31363858a1f4SJohn Baldwin 		return (EINVAL);
31373858a1f4SJohn Baldwin 
31383858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
31393858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig32, sizeof(sig32));
31403858a1f4SJohn Baldwin 		if (error)
31413858a1f4SJohn Baldwin 			return (error);
31423858a1f4SJohn Baldwin 		error = convert_sigevent32(&sig32, &sig);
31433858a1f4SJohn Baldwin 		if (error)
31443858a1f4SJohn Baldwin 			return (error);
31453858a1f4SJohn Baldwin 		sigp = &sig;
31463858a1f4SJohn Baldwin 	} else
31473858a1f4SJohn Baldwin 		sigp = NULL;
31483858a1f4SJohn Baldwin 
31493858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31503858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31513858a1f4SJohn Baldwin 	if (error) {
31523858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31533858a1f4SJohn Baldwin 		return (error);
31543858a1f4SJohn Baldwin 	}
31553858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31563858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31573858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31583858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31593858a1f4SJohn Baldwin 
31603858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31613858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31623858a1f4SJohn Baldwin 	    &aiocb32_ops);
31633858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31643858a1f4SJohn Baldwin 	return (error);
31653858a1f4SJohn Baldwin }
31663858a1f4SJohn Baldwin 
31673858a1f4SJohn Baldwin #endif
3168