xref: /freebsd/sys/kern/vfs_aio.c (revision ef9ffb8594eee294334ced627755bf5b46b48f9f)
19454b2d8SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
23ee877a35SJohn Dyson #include <sys/param.h>
24ee877a35SJohn Dyson #include <sys/systm.h>
25f591779bSSeigo Tanimura #include <sys/malloc.h>
269626b608SPoul-Henning Kamp #include <sys/bio.h>
27a5c9bce7SBruce Evans #include <sys/buf.h>
284a144410SRobert Watson #include <sys/capsicum.h>
2975b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
30ee877a35SJohn Dyson #include <sys/sysproto.h>
31ee877a35SJohn Dyson #include <sys/filedesc.h>
32ee877a35SJohn Dyson #include <sys/kernel.h>
3377409fe1SPoul-Henning Kamp #include <sys/module.h>
34c9a970a7SAlan Cox #include <sys/kthread.h>
35ee877a35SJohn Dyson #include <sys/fcntl.h>
36ee877a35SJohn Dyson #include <sys/file.h>
37104a9b7eSAlexander Kabaev #include <sys/limits.h>
38fdebd4f0SBruce Evans #include <sys/lock.h>
3935e0e5b3SJohn Baldwin #include <sys/mutex.h>
40ee877a35SJohn Dyson #include <sys/unistd.h>
416aeb05d7STom Rhodes #include <sys/posix4.h>
42ee877a35SJohn Dyson #include <sys/proc.h>
432d2f8ae7SBruce Evans #include <sys/resourcevar.h>
44ee877a35SJohn Dyson #include <sys/signalvar.h>
45496ab053SKonstantin Belousov #include <sys/syscallsubr.h>
46bfbbc4aaSJason Evans #include <sys/protosw.h>
4789f6b863SAttilio Rao #include <sys/rwlock.h>
481ce91824SDavid Xu #include <sys/sema.h>
491ce91824SDavid Xu #include <sys/socket.h>
50bfbbc4aaSJason Evans #include <sys/socketvar.h>
5121d56e9cSAlfred Perlstein #include <sys/syscall.h>
52a624e84fSJohn Dyson #include <sys/sysctl.h>
539c20dc99SJohn Baldwin #include <sys/syslog.h>
54ee99e978SBruce Evans #include <sys/sx.h>
551ce91824SDavid Xu #include <sys/taskqueue.h>
56fd3bf775SJohn Dyson #include <sys/vnode.h>
57fd3bf775SJohn Dyson #include <sys/conf.h>
58cb679c38SJonathan Lemon #include <sys/event.h>
5999eee864SDavid Xu #include <sys/mount.h>
60f743d981SAlexander Motin #include <geom/geom.h>
61ee877a35SJohn Dyson 
621ce91824SDavid Xu #include <machine/atomic.h>
631ce91824SDavid Xu 
64ee877a35SJohn Dyson #include <vm/vm.h>
65f743d981SAlexander Motin #include <vm/vm_page.h>
66ee877a35SJohn Dyson #include <vm/vm_extern.h>
672244ea07SJohn Dyson #include <vm/pmap.h>
682244ea07SJohn Dyson #include <vm/vm_map.h>
6999eee864SDavid Xu #include <vm/vm_object.h>
70b068bb09SKonstantin Belousov #include <vm/vnode_pager.h>
71c897b813SJeff Roberson #include <vm/uma.h>
72ee877a35SJohn Dyson #include <sys/aio.h>
735aaef07cSJohn Dyson 
74eb8e6d52SEivind Eklund /*
7599eee864SDavid Xu  * Counter for aio_fsync.
7699eee864SDavid Xu  */
7799eee864SDavid Xu static uint64_t jobseqno;
7899eee864SDavid Xu 
7984af4da6SJohn Dyson #ifndef MAX_AIO_PER_PROC
802244ea07SJohn Dyson #define MAX_AIO_PER_PROC	32
8184af4da6SJohn Dyson #endif
8284af4da6SJohn Dyson 
8384af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE_PER_PROC
84913b9329SAlan Somers #define MAX_AIO_QUEUE_PER_PROC	256
8584af4da6SJohn Dyson #endif
8684af4da6SJohn Dyson 
8784af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE
88913b9329SAlan Somers #define MAX_AIO_QUEUE		1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
8984af4da6SJohn Dyson #endif
9084af4da6SJohn Dyson 
9184af4da6SJohn Dyson #ifndef MAX_BUF_AIO
9284af4da6SJohn Dyson #define MAX_BUF_AIO		16
9384af4da6SJohn Dyson #endif
9484af4da6SJohn Dyson 
95e603be7aSRobert Watson FEATURE(aio, "Asynchronous I/O");
96c45796d5SAlan Somers SYSCTL_DECL(_p1003_1b);
97e603be7aSRobert Watson 
983858a1f4SJohn Baldwin static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
999553bc89SMark Johnston static MALLOC_DEFINE(M_AIO, "aio", "structures for asynchronous I/O");
1003858a1f4SJohn Baldwin 
1017029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1020dd6c035SJohn Baldwin     "Async IO management");
103eb8e6d52SEivind Eklund 
104f3215338SJohn Baldwin static int enable_aio_unsafe = 0;
105f3215338SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
106f3215338SJohn Baldwin     "Permit asynchronous IO on all file types, not just known-safe types");
107f3215338SJohn Baldwin 
1089c20dc99SJohn Baldwin static unsigned int unsafe_warningcnt = 1;
1099c20dc99SJohn Baldwin SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
1109c20dc99SJohn Baldwin     &unsafe_warningcnt, 0,
1119c20dc99SJohn Baldwin     "Warnings that will be triggered upon failed IO requests on unsafe files");
1129c20dc99SJohn Baldwin 
113303b270bSEivind Eklund static int max_aio_procs = MAX_AIO_PROCS;
1140dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
11539314b7dSJohn Baldwin     "Maximum number of kernel processes to use for handling async IO ");
116a624e84fSJohn Dyson 
117eb8e6d52SEivind Eklund static int num_aio_procs = 0;
1180dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
11939314b7dSJohn Baldwin     "Number of presently active kernel processes for async IO");
120a624e84fSJohn Dyson 
121eb8e6d52SEivind Eklund /*
122eb8e6d52SEivind Eklund  * The code will adjust the actual number of AIO processes towards this
123eb8e6d52SEivind Eklund  * number when it gets a chance.
124eb8e6d52SEivind Eklund  */
125eb8e6d52SEivind Eklund static int target_aio_procs = TARGET_AIO_PROCS;
126eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
1270dd6c035SJohn Baldwin     0,
1280dd6c035SJohn Baldwin     "Preferred number of ready kernel processes for async IO");
129a624e84fSJohn Dyson 
130eb8e6d52SEivind Eklund static int max_queue_count = MAX_AIO_QUEUE;
131eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
132eb8e6d52SEivind Eklund     "Maximum number of aio requests to queue, globally");
133a624e84fSJohn Dyson 
134eb8e6d52SEivind Eklund static int num_queue_count = 0;
135eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
136eb8e6d52SEivind Eklund     "Number of queued aio requests");
137a624e84fSJohn Dyson 
138eb8e6d52SEivind Eklund static int num_buf_aio = 0;
139eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
140eb8e6d52SEivind Eklund     "Number of aio requests presently handled by the buf subsystem");
141fd3bf775SJohn Dyson 
1428091e52bSJohn Baldwin static int num_unmapped_aio = 0;
1438091e52bSJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
1448091e52bSJohn Baldwin     0,
1458091e52bSJohn Baldwin     "Number of aio requests presently handled by unmapped I/O buffers");
1468091e52bSJohn Baldwin 
14739314b7dSJohn Baldwin /* Number of async I/O processes in the process of being started */
148a9bf5e37SDavid Xu /* XXX This should be local to aio_aqueue() */
149eb8e6d52SEivind Eklund static int num_aio_resv_start = 0;
150fd3bf775SJohn Dyson 
151eb8e6d52SEivind Eklund static int aiod_lifetime;
152eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
153eb8e6d52SEivind Eklund     "Maximum lifetime for idle aiod");
15484af4da6SJohn Dyson 
155eb8e6d52SEivind Eklund static int max_aio_per_proc = MAX_AIO_PER_PROC;
156eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
1570dd6c035SJohn Baldwin     0,
15886bbef43SJohn Baldwin     "Maximum active aio requests per process");
159eb8e6d52SEivind Eklund 
160eb8e6d52SEivind Eklund static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
161eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
162eb8e6d52SEivind Eklund     &max_aio_queue_per_proc, 0,
16386bbef43SJohn Baldwin     "Maximum queued aio requests per process");
164eb8e6d52SEivind Eklund 
165eb8e6d52SEivind Eklund static int max_buf_aio = MAX_BUF_AIO;
166eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
16786bbef43SJohn Baldwin     "Maximum buf aio requests per process");
168eb8e6d52SEivind Eklund 
169913b9329SAlan Somers /*
170913b9329SAlan Somers  * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
171913b9329SAlan Somers  * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
172913b9329SAlan Somers  * vfs.aio.aio_listio_max.
173913b9329SAlan Somers  */
174c45796d5SAlan Somers SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
175913b9329SAlan Somers     CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
176913b9329SAlan Somers     0, "Maximum aio requests for a single lio_listio call");
177c45796d5SAlan Somers 
178399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
1790972628aSDavid Xu typedef struct oaiocb {
1800972628aSDavid Xu 	int	aio_fildes;		/* File descriptor */
1810972628aSDavid Xu 	off_t	aio_offset;		/* File offset for I/O */
1820972628aSDavid Xu 	volatile void *aio_buf;         /* I/O buffer in process space */
1830972628aSDavid Xu 	size_t	aio_nbytes;		/* Number of bytes for I/O */
1840972628aSDavid Xu 	struct	osigevent aio_sigevent;	/* Signal to deliver */
1850972628aSDavid Xu 	int	aio_lio_opcode;		/* LIO opcode */
1860972628aSDavid Xu 	int	aio_reqprio;		/* Request priority -- ignored */
1870972628aSDavid Xu 	struct	__aiocb_private	_aiocb_private;
1880972628aSDavid Xu } oaiocb_t;
189399e8c17SJohn Baldwin #endif
1900972628aSDavid Xu 
1911aa4c324SDavid Xu /*
1925652770dSJohn Baldwin  * Below is a key of locks used to protect each member of struct kaiocb
1931aa4c324SDavid Xu  * aioliojob and kaioinfo and any backends.
1941aa4c324SDavid Xu  *
1951aa4c324SDavid Xu  * * - need not protected
196759ccccaSDavid Xu  * a - locked by kaioinfo lock
1971aa4c324SDavid Xu  * b - locked by backend lock, the backend lock can be null in some cases,
1981aa4c324SDavid Xu  *     for example, BIO belongs to this type, in this case, proc lock is
1991aa4c324SDavid Xu  *     reused.
2001aa4c324SDavid Xu  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
2011aa4c324SDavid Xu  */
2021aa4c324SDavid Xu 
2031aa4c324SDavid Xu /*
204f3215338SJohn Baldwin  * If the routine that services an AIO request blocks while running in an
205f3215338SJohn Baldwin  * AIO kernel process it can starve other I/O requests.  BIO requests
20672bce9ffSAlan Somers  * queued via aio_qbio() complete asynchronously and do not use AIO kernel
207f3215338SJohn Baldwin  * processes at all.  Socket I/O requests use a separate pool of
208f3215338SJohn Baldwin  * kprocs and also force non-blocking I/O.  Other file I/O requests
209f3215338SJohn Baldwin  * use the generic fo_read/fo_write operations which can block.  The
210f3215338SJohn Baldwin  * fsync and mlock operations can also block while executing.  Ideally
211f3215338SJohn Baldwin  * none of these requests would block while executing.
212f3215338SJohn Baldwin  *
213f3215338SJohn Baldwin  * Note that the service routines cannot toggle O_NONBLOCK in the file
214f3215338SJohn Baldwin  * structure directly while handling a request due to races with
215f3215338SJohn Baldwin  * userland threads.
2161aa4c324SDavid Xu  */
2171aa4c324SDavid Xu 
21848dac059SAlan Cox /* jobflags */
219f3215338SJohn Baldwin #define	KAIOCB_QUEUEING		0x01
220f3215338SJohn Baldwin #define	KAIOCB_CANCELLED	0x02
221f3215338SJohn Baldwin #define	KAIOCB_CANCELLING	0x04
2225652770dSJohn Baldwin #define	KAIOCB_CHECKSYNC	0x08
223f3215338SJohn Baldwin #define	KAIOCB_CLEARED		0x10
224f3215338SJohn Baldwin #define	KAIOCB_FINISHED		0x20
22548dac059SAlan Cox 
226e4b7bbd6SKonstantin Belousov /* ioflags */
227e4b7bbd6SKonstantin Belousov #define	KAIOCB_IO_FOFFSET	0x01
228e4b7bbd6SKonstantin Belousov 
2292244ea07SJohn Dyson /*
2302244ea07SJohn Dyson  * AIO process info
2312244ea07SJohn Dyson  */
23284af4da6SJohn Dyson #define AIOP_FREE	0x1			/* proc on free queue */
23384af4da6SJohn Dyson 
23439314b7dSJohn Baldwin struct aioproc {
23539314b7dSJohn Baldwin 	int	aioprocflags;			/* (c) AIO proc flags */
23639314b7dSJohn Baldwin 	TAILQ_ENTRY(aioproc) list;		/* (c) list of processes */
23739314b7dSJohn Baldwin 	struct	proc *aioproc;			/* (*) the AIO proc */
2382244ea07SJohn Dyson };
2392244ea07SJohn Dyson 
24084af4da6SJohn Dyson /*
24184af4da6SJohn Dyson  * data-structure for lio signal management
24284af4da6SJohn Dyson  */
2431ce91824SDavid Xu struct aioliojob {
2441aa4c324SDavid Xu 	int	lioj_flags;			/* (a) listio flags */
2455c5005ecSKonstantin Belousov 	int	lioj_count;			/* (a) count of jobs */
2465c5005ecSKonstantin Belousov 	int	lioj_finished_count;		/* (a) count of finished jobs */
2471aa4c324SDavid Xu 	struct	sigevent lioj_signal;		/* (a) signal on all I/O done */
2481aa4c324SDavid Xu 	TAILQ_ENTRY(aioliojob) lioj_list;	/* (a) lio list */
2491aa4c324SDavid Xu 	struct	knlist klist;			/* (a) list of knotes */
2501aa4c324SDavid Xu 	ksiginfo_t lioj_ksi;			/* (a) Realtime signal info */
25184af4da6SJohn Dyson };
2521ce91824SDavid Xu 
25384af4da6SJohn Dyson #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
25484af4da6SJohn Dyson #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
25569cd28daSDoug Ambrisko #define LIOJ_KEVENT_POSTED	0x4	/* kevent triggered */
25684af4da6SJohn Dyson 
25784af4da6SJohn Dyson /*
25884af4da6SJohn Dyson  * per process aio data structure
25984af4da6SJohn Dyson  */
2602244ea07SJohn Dyson struct kaioinfo {
261759ccccaSDavid Xu 	struct	mtx kaio_mtx;		/* the lock to protect this struct */
2621aa4c324SDavid Xu 	int	kaio_flags;		/* (a) per process kaio flags */
2631aa4c324SDavid Xu 	int	kaio_active_count;	/* (c) number of currently used AIOs */
2641aa4c324SDavid Xu 	int	kaio_count;		/* (a) size of AIO queue */
26572bce9ffSAlan Somers 	int	kaio_buffer_count;	/* (a) number of bio buffers */
2665652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_all;	/* (a) all AIOs in a process */
2675652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_done;	/* (a) done queue for process */
2681aa4c324SDavid Xu 	TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
2695652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_jobqueue;	/* (a) job queue for process */
2705652770dSJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncqueue;	/* (a) queue for aio_fsync */
271f3215338SJohn Baldwin 	TAILQ_HEAD(,kaiocb) kaio_syncready;  /* (a) second q for aio_fsync */
27239314b7dSJohn Baldwin 	struct	task kaio_task;		/* (*) task to kick aio processes */
273f3215338SJohn Baldwin 	struct	task kaio_sync_task;	/* (*) task to schedule fsync jobs */
2742244ea07SJohn Dyson };
2752244ea07SJohn Dyson 
276759ccccaSDavid Xu #define AIO_LOCK(ki)		mtx_lock(&(ki)->kaio_mtx)
277759ccccaSDavid Xu #define AIO_UNLOCK(ki)		mtx_unlock(&(ki)->kaio_mtx)
278759ccccaSDavid Xu #define AIO_LOCK_ASSERT(ki, f)	mtx_assert(&(ki)->kaio_mtx, (f))
279759ccccaSDavid Xu #define AIO_MTX(ki)		(&(ki)->kaio_mtx)
280759ccccaSDavid Xu 
28184af4da6SJohn Dyson #define KAIO_RUNDOWN	0x1	/* process is being run down */
2820dd6c035SJohn Baldwin #define KAIO_WAKEUP	0x2	/* wakeup process when AIO completes */
283fd3bf775SJohn Dyson 
2843858a1f4SJohn Baldwin /*
2853858a1f4SJohn Baldwin  * Operations used to interact with userland aio control blocks.
2863858a1f4SJohn Baldwin  * Different ABIs provide their own operations.
2873858a1f4SJohn Baldwin  */
2883858a1f4SJohn Baldwin struct aiocb_ops {
289022ca2fcSAlan Somers 	int	(*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
2903858a1f4SJohn Baldwin 	long	(*fetch_status)(struct aiocb *ujob);
2913858a1f4SJohn Baldwin 	long	(*fetch_error)(struct aiocb *ujob);
2923858a1f4SJohn Baldwin 	int	(*store_status)(struct aiocb *ujob, long status);
2933858a1f4SJohn Baldwin 	int	(*store_error)(struct aiocb *ujob, long error);
2943858a1f4SJohn Baldwin 	int	(*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
2953858a1f4SJohn Baldwin };
2963858a1f4SJohn Baldwin 
29739314b7dSJohn Baldwin static TAILQ_HEAD(,aioproc) aio_freeproc;		/* (c) Idle daemons */
2981ce91824SDavid Xu static struct sema aio_newproc_sem;
2991ce91824SDavid Xu static struct mtx aio_job_mtx;
3005652770dSJohn Baldwin static TAILQ_HEAD(,kaiocb) aio_jobs;			/* (c) Async job list */
3011ce91824SDavid Xu static struct unrhdr *aiod_unr;
3022244ea07SJohn Dyson 
303022ca2fcSAlan Somers static void	aio_biocleanup(struct bio *bp);
3046a1162d4SAlexander Leidinger void		aio_init_aioinfo(struct proc *p);
305723d37c0SKonstantin Belousov static int	aio_onceonly(void);
3065652770dSJohn Baldwin static int	aio_free_entry(struct kaiocb *job);
3075652770dSJohn Baldwin static void	aio_process_rw(struct kaiocb *job);
3085652770dSJohn Baldwin static void	aio_process_sync(struct kaiocb *job);
3095652770dSJohn Baldwin static void	aio_process_mlock(struct kaiocb *job);
310f3215338SJohn Baldwin static void	aio_schedule_fsync(void *context, int pending);
3111ce91824SDavid Xu static int	aio_newproc(int *);
3125652770dSJohn Baldwin int		aio_aqueue(struct thread *td, struct aiocb *ujob,
3133858a1f4SJohn Baldwin 		    struct aioliojob *lio, int type, struct aiocb_ops *ops);
314f3215338SJohn Baldwin static int	aio_queue_file(struct file *fp, struct kaiocb *job);
31572bce9ffSAlan Somers static void	aio_biowakeup(struct bio *bp);
31675b8b3b2SJohn Baldwin static void	aio_proc_rundown(void *arg, struct proc *p);
3170dd6c035SJohn Baldwin static void	aio_proc_rundown_exec(void *arg, struct proc *p,
3180dd6c035SJohn Baldwin 		    struct image_params *imgp);
31972bce9ffSAlan Somers static int	aio_qbio(struct proc *p, struct kaiocb *job);
3201ce91824SDavid Xu static void	aio_daemon(void *param);
321f3215338SJohn Baldwin static void	aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
322005ce8e4SJohn Baldwin static bool	aio_clear_cancel_function_locked(struct kaiocb *job);
323dbbccfe9SDavid Xu static int	aio_kick(struct proc *userp);
32499eee864SDavid Xu static void	aio_kick_nowait(struct proc *userp);
32599eee864SDavid Xu static void	aio_kick_helper(void *context, int pending);
32621d56e9cSAlfred Perlstein static int	filt_aioattach(struct knote *kn);
32721d56e9cSAlfred Perlstein static void	filt_aiodetach(struct knote *kn);
32821d56e9cSAlfred Perlstein static int	filt_aio(struct knote *kn, long hint);
32969cd28daSDoug Ambrisko static int	filt_lioattach(struct knote *kn);
33069cd28daSDoug Ambrisko static void	filt_liodetach(struct knote *kn);
33169cd28daSDoug Ambrisko static int	filt_lio(struct knote *kn, long hint);
3322244ea07SJohn Dyson 
333eb8e6d52SEivind Eklund /*
334eb8e6d52SEivind Eklund  * Zones for:
335eb8e6d52SEivind Eklund  * 	kaio	Per process async io info
336eb8e6d52SEivind Eklund  *	aiocb	async io jobs
337eb8e6d52SEivind Eklund  *	aiolio	list io jobs
338eb8e6d52SEivind Eklund  */
3399553bc89SMark Johnston static uma_zone_t kaio_zone, aiocb_zone, aiolio_zone;
340fd3bf775SJohn Dyson 
341eb8e6d52SEivind Eklund /* kqueue filters for aio */
342*ef9ffb85SMark Johnston static const struct filterops aio_filtops = {
343e76d823bSRobert Watson 	.f_isfd = 0,
344e76d823bSRobert Watson 	.f_attach = filt_aioattach,
345e76d823bSRobert Watson 	.f_detach = filt_aiodetach,
346e76d823bSRobert Watson 	.f_event = filt_aio,
347e76d823bSRobert Watson };
348*ef9ffb85SMark Johnston static const struct filterops lio_filtops = {
349e76d823bSRobert Watson 	.f_isfd = 0,
350e76d823bSRobert Watson 	.f_attach = filt_lioattach,
351e76d823bSRobert Watson 	.f_detach = filt_liodetach,
352e76d823bSRobert Watson 	.f_event = filt_lio
353e76d823bSRobert Watson };
35421d56e9cSAlfred Perlstein 
35575b8b3b2SJohn Baldwin static eventhandler_tag exit_tag, exec_tag;
35675b8b3b2SJohn Baldwin 
357c85650caSJohn Baldwin TASKQUEUE_DEFINE_THREAD(aiod_kick);
3581ce91824SDavid Xu 
359eb8e6d52SEivind Eklund /*
360eb8e6d52SEivind Eklund  * Main operations function for use as a kernel module.
361eb8e6d52SEivind Eklund  */
36221d56e9cSAlfred Perlstein static int
aio_modload(struct module * module,int cmd,void * arg)36321d56e9cSAlfred Perlstein aio_modload(struct module *module, int cmd, void *arg)
36421d56e9cSAlfred Perlstein {
36521d56e9cSAlfred Perlstein 	int error = 0;
36621d56e9cSAlfred Perlstein 
36721d56e9cSAlfred Perlstein 	switch (cmd) {
36821d56e9cSAlfred Perlstein 	case MOD_LOAD:
36921d56e9cSAlfred Perlstein 		aio_onceonly();
37021d56e9cSAlfred Perlstein 		break;
37121d56e9cSAlfred Perlstein 	case MOD_SHUTDOWN:
37221d56e9cSAlfred Perlstein 		break;
37321d56e9cSAlfred Perlstein 	default:
374f3215338SJohn Baldwin 		error = EOPNOTSUPP;
37521d56e9cSAlfred Perlstein 		break;
37621d56e9cSAlfred Perlstein 	}
37721d56e9cSAlfred Perlstein 	return (error);
37821d56e9cSAlfred Perlstein }
37921d56e9cSAlfred Perlstein 
38021d56e9cSAlfred Perlstein static moduledata_t aio_mod = {
38121d56e9cSAlfred Perlstein 	"aio",
38221d56e9cSAlfred Perlstein 	&aio_modload,
38321d56e9cSAlfred Perlstein 	NULL
38421d56e9cSAlfred Perlstein };
38521d56e9cSAlfred Perlstein 
386399e8c17SJohn Baldwin DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
38721d56e9cSAlfred Perlstein MODULE_VERSION(aio, 1);
38821d56e9cSAlfred Perlstein 
389fd3bf775SJohn Dyson /*
3902244ea07SJohn Dyson  * Startup initialization
3912244ea07SJohn Dyson  */
392723d37c0SKonstantin Belousov static int
aio_onceonly(void)39321d56e9cSAlfred Perlstein aio_onceonly(void)
394fd3bf775SJohn Dyson {
39521d56e9cSAlfred Perlstein 
39675b8b3b2SJohn Baldwin 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
39775b8b3b2SJohn Baldwin 	    EVENTHANDLER_PRI_ANY);
3980dd6c035SJohn Baldwin 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
3990dd6c035SJohn Baldwin 	    NULL, EVENTHANDLER_PRI_ANY);
40021d56e9cSAlfred Perlstein 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
40169cd28daSDoug Ambrisko 	kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
4022244ea07SJohn Dyson 	TAILQ_INIT(&aio_freeproc);
4031ce91824SDavid Xu 	sema_init(&aio_newproc_sem, 0, "aio_new_proc");
4041ce91824SDavid Xu 	mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
4052244ea07SJohn Dyson 	TAILQ_INIT(&aio_jobs);
4061ce91824SDavid Xu 	aiod_unr = new_unrhdr(1, INT_MAX, NULL);
407c897b813SJeff Roberson 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
4089553bc89SMark Johnston 	    NULL, NULL, UMA_ALIGN_PTR, 0);
4095652770dSJohn Baldwin 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
4109553bc89SMark Johnston 	    NULL, NULL, UMA_ALIGN_PTR, 0);
4111ce91824SDavid Xu 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
4129553bc89SMark Johnston 	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
41384af4da6SJohn Dyson 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
414399e8c17SJohn Baldwin 	p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
41586d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
41686d52125SAlfred Perlstein 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
417723d37c0SKonstantin Belousov 
418723d37c0SKonstantin Belousov 	return (0);
4192244ea07SJohn Dyson }
4202244ea07SJohn Dyson 
421eb8e6d52SEivind Eklund /*
422bfbbc4aaSJason Evans  * Init the per-process aioinfo structure.  The aioinfo limits are set
423bfbbc4aaSJason Evans  * per-process for user limit (resource) management.
4242244ea07SJohn Dyson  */
4256a1162d4SAlexander Leidinger void
aio_init_aioinfo(struct proc * p)426fd3bf775SJohn Dyson aio_init_aioinfo(struct proc *p)
427fd3bf775SJohn Dyson {
4282244ea07SJohn Dyson 	struct kaioinfo *ki;
429ac41f2efSAlfred Perlstein 
430a163d034SWarner Losh 	ki = uma_zalloc(kaio_zone, M_WAITOK);
4319889bbacSKonstantin Belousov 	mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
43284af4da6SJohn Dyson 	ki->kaio_flags = 0;
4332244ea07SJohn Dyson 	ki->kaio_active_count = 0;
4341ce91824SDavid Xu 	ki->kaio_count = 0;
435fd3bf775SJohn Dyson 	ki->kaio_buffer_count = 0;
4361ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_all);
4371ce91824SDavid Xu 	TAILQ_INIT(&ki->kaio_done);
4382244ea07SJohn Dyson 	TAILQ_INIT(&ki->kaio_jobqueue);
43984af4da6SJohn Dyson 	TAILQ_INIT(&ki->kaio_liojoblist);
44099eee864SDavid Xu 	TAILQ_INIT(&ki->kaio_syncqueue);
441f3215338SJohn Baldwin 	TAILQ_INIT(&ki->kaio_syncready);
44299eee864SDavid Xu 	TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
443f3215338SJohn Baldwin 	TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
4443999ebe3SAlan Cox 	PROC_LOCK(p);
4453999ebe3SAlan Cox 	if (p->p_aioinfo == NULL) {
4463999ebe3SAlan Cox 		p->p_aioinfo = ki;
4473999ebe3SAlan Cox 		PROC_UNLOCK(p);
4483999ebe3SAlan Cox 	} else {
4493999ebe3SAlan Cox 		PROC_UNLOCK(p);
450759ccccaSDavid Xu 		mtx_destroy(&ki->kaio_mtx);
4513999ebe3SAlan Cox 		uma_zfree(kaio_zone, ki);
4522244ea07SJohn Dyson 	}
453bfbbc4aaSJason Evans 
45422035f47SOleksandr Tymoshenko 	while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
4551ce91824SDavid Xu 		aio_newproc(NULL);
4562244ea07SJohn Dyson }
4572244ea07SJohn Dyson 
4584c0fb2cfSDavid Xu static int
aio_sendsig(struct proc * p,struct sigevent * sigev,ksiginfo_t * ksi,bool ext)4596814c2daSKonstantin Belousov aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
4604c0fb2cfSDavid Xu {
461cf7d9a8cSDavid Xu 	struct thread *td;
462cf7d9a8cSDavid Xu 	int error;
463759ccccaSDavid Xu 
464cf7d9a8cSDavid Xu 	error = sigev_findtd(p, sigev, &td);
465cf7d9a8cSDavid Xu 	if (error)
466cf7d9a8cSDavid Xu 		return (error);
4674c0fb2cfSDavid Xu 	if (!KSI_ONQ(ksi)) {
468cf7d9a8cSDavid Xu 		ksiginfo_set_sigev(ksi, sigev);
4694c0fb2cfSDavid Xu 		ksi->ksi_code = SI_ASYNCIO;
4706814c2daSKonstantin Belousov 		ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
471cf7d9a8cSDavid Xu 		tdsendsignal(p, td, ksi->ksi_signo, ksi);
4724c0fb2cfSDavid Xu 	}
473759ccccaSDavid Xu 	PROC_UNLOCK(p);
474cf7d9a8cSDavid Xu 	return (error);
4754c0fb2cfSDavid Xu }
4764c0fb2cfSDavid Xu 
4772244ea07SJohn Dyson /*
478bfbbc4aaSJason Evans  * Free a job entry.  Wait for completion if it is currently active, but don't
479bfbbc4aaSJason Evans  * delay forever.  If we delay, we return a flag that says that we have to
480bfbbc4aaSJason Evans  * restart the queue scan.
4812244ea07SJohn Dyson  */
48288ed460eSAlan Cox static int
aio_free_entry(struct kaiocb * job)4835652770dSJohn Baldwin aio_free_entry(struct kaiocb *job)
484fd3bf775SJohn Dyson {
4852244ea07SJohn Dyson 	struct kaioinfo *ki;
4861ce91824SDavid Xu 	struct aioliojob *lj;
4872244ea07SJohn Dyson 	struct proc *p;
4882244ea07SJohn Dyson 
4895652770dSJohn Baldwin 	p = job->userproc;
4901ce91824SDavid Xu 	MPASS(curproc == p);
4912244ea07SJohn Dyson 	ki = p->p_aioinfo;
4921ce91824SDavid Xu 	MPASS(ki != NULL);
4931ce91824SDavid Xu 
494759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
495f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
496759ccccaSDavid Xu 
4971ce91824SDavid Xu 	atomic_subtract_int(&num_queue_count, 1);
4981ce91824SDavid Xu 
4991ce91824SDavid Xu 	ki->kaio_count--;
5001ce91824SDavid Xu 	MPASS(ki->kaio_count >= 0);
5011ce91824SDavid Xu 
5025652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_done, job, plist);
5035652770dSJohn Baldwin 	TAILQ_REMOVE(&ki->kaio_all, job, allist);
50427b8220dSDavid Xu 
5055652770dSJohn Baldwin 	lj = job->lio;
50684af4da6SJohn Dyson 	if (lj) {
5071ce91824SDavid Xu 		lj->lioj_count--;
5081ce91824SDavid Xu 		lj->lioj_finished_count--;
5091ce91824SDavid Xu 
510a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
5111ce91824SDavid Xu 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
5121ce91824SDavid Xu 			/* lio is going away, we need to destroy any knotes */
5131ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
514759ccccaSDavid Xu 			PROC_LOCK(p);
5151ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
516759ccccaSDavid Xu 			PROC_UNLOCK(p);
5171ce91824SDavid Xu 			uma_zfree(aiolio_zone, lj);
51884af4da6SJohn Dyson 		}
51984af4da6SJohn Dyson 	}
5201ce91824SDavid Xu 
5215652770dSJohn Baldwin 	/* job is going away, we need to destroy any knotes */
5225652770dSJohn Baldwin 	knlist_delete(&job->klist, curthread, 1);
523759ccccaSDavid Xu 	PROC_LOCK(p);
5245652770dSJohn Baldwin 	sigqueue_take(&job->ksi);
525759ccccaSDavid Xu 	PROC_UNLOCK(p);
5261ce91824SDavid Xu 
527759ccccaSDavid Xu 	AIO_UNLOCK(ki);
5282a522eb9SJohn Baldwin 
5292a522eb9SJohn Baldwin 	/*
5302a522eb9SJohn Baldwin 	 * The thread argument here is used to find the owning process
5312a522eb9SJohn Baldwin 	 * and is also passed to fo_close() which may pass it to various
5322a522eb9SJohn Baldwin 	 * places such as devsw close() routines.  Because of that, we
5332a522eb9SJohn Baldwin 	 * need a thread pointer from the process owning the job that is
5342a522eb9SJohn Baldwin 	 * persistent and won't disappear out from under us or move to
5352a522eb9SJohn Baldwin 	 * another process.
5362a522eb9SJohn Baldwin 	 *
5372a522eb9SJohn Baldwin 	 * Currently, all the callers of this function call it to remove
5385652770dSJohn Baldwin 	 * a kaiocb from the current process' job list either via a
5392a522eb9SJohn Baldwin 	 * syscall or due to the current process calling exit() or
5402a522eb9SJohn Baldwin 	 * execve().  Thus, we know that p == curproc.  We also know that
5412a522eb9SJohn Baldwin 	 * curthread can't exit since we are curthread.
5422a522eb9SJohn Baldwin 	 *
5432a522eb9SJohn Baldwin 	 * Therefore, we use curthread as the thread to pass to
5442a522eb9SJohn Baldwin 	 * knlist_delete().  This does mean that it is possible for the
5452a522eb9SJohn Baldwin 	 * thread pointer at close time to differ from the thread pointer
5462a522eb9SJohn Baldwin 	 * at open time, but this is already true of file descriptors in
5472a522eb9SJohn Baldwin 	 * a multithreaded process.
548b40ce416SJulian Elischer 	 */
5495652770dSJohn Baldwin 	if (job->fd_file)
5505652770dSJohn Baldwin 		fdrop(job->fd_file, curthread);
5515652770dSJohn Baldwin 	crfree(job->cred);
552022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
55361cc4830SAlfredo Mazzinghi 		freeuio(job->uiop);
5545652770dSJohn Baldwin 	uma_zfree(aiocb_zone, job);
555759ccccaSDavid Xu 	AIO_LOCK(ki);
5561ce91824SDavid Xu 
557ac41f2efSAlfred Perlstein 	return (0);
5582244ea07SJohn Dyson }
5592244ea07SJohn Dyson 
560993182e5SAlexander Leidinger static void
aio_proc_rundown_exec(void * arg,struct proc * p,struct image_params * imgp __unused)5610dd6c035SJohn Baldwin aio_proc_rundown_exec(void *arg, struct proc *p,
5620dd6c035SJohn Baldwin     struct image_params *imgp __unused)
563993182e5SAlexander Leidinger {
564993182e5SAlexander Leidinger    	aio_proc_rundown(arg, p);
565993182e5SAlexander Leidinger }
566993182e5SAlexander Leidinger 
567f3215338SJohn Baldwin static int
aio_cancel_job(struct proc * p,struct kaioinfo * ki,struct kaiocb * job)568f3215338SJohn Baldwin aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
569f3215338SJohn Baldwin {
570f3215338SJohn Baldwin 	aio_cancel_fn_t *func;
571f3215338SJohn Baldwin 	int cancelled;
572f3215338SJohn Baldwin 
573f3215338SJohn Baldwin 	AIO_LOCK_ASSERT(ki, MA_OWNED);
574f3215338SJohn Baldwin 	if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
575f3215338SJohn Baldwin 		return (0);
576f3215338SJohn Baldwin 	MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
577f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLED;
578f3215338SJohn Baldwin 
579f3215338SJohn Baldwin 	func = job->cancel_fn;
580f3215338SJohn Baldwin 
581f3215338SJohn Baldwin 	/*
582f3215338SJohn Baldwin 	 * If there is no cancel routine, just leave the job marked as
583f3215338SJohn Baldwin 	 * cancelled.  The job should be in active use by a caller who
584f3215338SJohn Baldwin 	 * should complete it normally or when it fails to install a
585f3215338SJohn Baldwin 	 * cancel routine.
586f3215338SJohn Baldwin 	 */
587f3215338SJohn Baldwin 	if (func == NULL)
588f3215338SJohn Baldwin 		return (0);
589f3215338SJohn Baldwin 
590f3215338SJohn Baldwin 	/*
591f3215338SJohn Baldwin 	 * Set the CANCELLING flag so that aio_complete() will defer
592f3215338SJohn Baldwin 	 * completions of this job.  This prevents the job from being
593f3215338SJohn Baldwin 	 * freed out from under the cancel callback.  After the
594f3215338SJohn Baldwin 	 * callback any deferred completion (whether from the callback
595f3215338SJohn Baldwin 	 * or any other source) will be completed.
596f3215338SJohn Baldwin 	 */
597f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_CANCELLING;
598f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
599f3215338SJohn Baldwin 	func(job);
600f3215338SJohn Baldwin 	AIO_LOCK(ki);
601f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_CANCELLING;
602f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
603f3215338SJohn Baldwin 		cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
604f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
605f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
606f3215338SJohn Baldwin 	} else {
607f3215338SJohn Baldwin 		/*
608f3215338SJohn Baldwin 		 * The cancel callback might have scheduled an
609f3215338SJohn Baldwin 		 * operation to cancel this request, but it is
610f3215338SJohn Baldwin 		 * only counted as cancelled if the request is
611f3215338SJohn Baldwin 		 * cancelled when the callback returns.
612f3215338SJohn Baldwin 		 */
613f3215338SJohn Baldwin 		cancelled = 0;
614f3215338SJohn Baldwin 	}
615f3215338SJohn Baldwin 	return (cancelled);
616f3215338SJohn Baldwin }
617f3215338SJohn Baldwin 
6182244ea07SJohn Dyson /*
6192244ea07SJohn Dyson  * Rundown the jobs for a given process.
6202244ea07SJohn Dyson  */
62121d56e9cSAlfred Perlstein static void
aio_proc_rundown(void * arg,struct proc * p)62275b8b3b2SJohn Baldwin aio_proc_rundown(void *arg, struct proc *p)
623fd3bf775SJohn Dyson {
6242244ea07SJohn Dyson 	struct kaioinfo *ki;
6251ce91824SDavid Xu 	struct aioliojob *lj;
6265652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
6272244ea07SJohn Dyson 
6282a522eb9SJohn Baldwin 	KASSERT(curthread->td_proc == p,
6292a522eb9SJohn Baldwin 	    ("%s: called on non-curproc", __func__));
6302244ea07SJohn Dyson 	ki = p->p_aioinfo;
6312244ea07SJohn Dyson 	if (ki == NULL)
6322244ea07SJohn Dyson 		return;
6332244ea07SJohn Dyson 
634759ccccaSDavid Xu 	AIO_LOCK(ki);
63527b8220dSDavid Xu 	ki->kaio_flags |= KAIO_RUNDOWN;
6361ce91824SDavid Xu 
6371ce91824SDavid Xu restart:
638a624e84fSJohn Dyson 
639bfbbc4aaSJason Evans 	/*
6401ce91824SDavid Xu 	 * Try to cancel all pending requests. This code simulates
6411ce91824SDavid Xu 	 * aio_cancel on all pending I/O requests.
642bfbbc4aaSJason Evans 	 */
6435652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
644f3215338SJohn Baldwin 		aio_cancel_job(p, ki, job);
6452244ea07SJohn Dyson 	}
64684af4da6SJohn Dyson 
6471ce91824SDavid Xu 	/* Wait for all running I/O to be finished */
648f3215338SJohn Baldwin 	if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
64984af4da6SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
650759ccccaSDavid Xu 		msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
6511ce91824SDavid Xu 		goto restart;
65284af4da6SJohn Dyson 	}
65384af4da6SJohn Dyson 
6541ce91824SDavid Xu 	/* Free all completed I/O requests. */
6555652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
6565652770dSJohn Baldwin 		aio_free_entry(job);
65784af4da6SJohn Dyson 
6581ce91824SDavid Xu 	while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
659a9bf5e37SDavid Xu 		if (lj->lioj_count == 0) {
66084af4da6SJohn Dyson 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
6611ce91824SDavid Xu 			knlist_delete(&lj->klist, curthread, 1);
662759ccccaSDavid Xu 			PROC_LOCK(p);
6631ce91824SDavid Xu 			sigqueue_take(&lj->lioj_ksi);
664759ccccaSDavid Xu 			PROC_UNLOCK(p);
665c897b813SJeff Roberson 			uma_zfree(aiolio_zone, lj);
666f4f0ecefSJohn Dyson 		} else {
667a9bf5e37SDavid Xu 			panic("LIO job not cleaned up: C:%d, FC:%d\n",
668a9bf5e37SDavid Xu 			    lj->lioj_count, lj->lioj_finished_count);
66984af4da6SJohn Dyson 		}
670f4f0ecefSJohn Dyson 	}
671759ccccaSDavid Xu 	AIO_UNLOCK(ki);
672c85650caSJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
673f3215338SJohn Baldwin 	taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
6745114048bSKonstantin Belousov 	mtx_destroy(&ki->kaio_mtx);
675c897b813SJeff Roberson 	uma_zfree(kaio_zone, ki);
676a624e84fSJohn Dyson 	p->p_aioinfo = NULL;
6772244ea07SJohn Dyson }
6782244ea07SJohn Dyson 
6792244ea07SJohn Dyson /*
680bfbbc4aaSJason Evans  * Select a job to run (called by an AIO daemon).
6812244ea07SJohn Dyson  */
6825652770dSJohn Baldwin static struct kaiocb *
aio_selectjob(struct aioproc * aiop)68339314b7dSJohn Baldwin aio_selectjob(struct aioproc *aiop)
684fd3bf775SJohn Dyson {
6855652770dSJohn Baldwin 	struct kaiocb *job;
686bfbbc4aaSJason Evans 	struct kaioinfo *ki;
687bfbbc4aaSJason Evans 	struct proc *userp;
6882244ea07SJohn Dyson 
6891ce91824SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
690f3215338SJohn Baldwin restart:
6915652770dSJohn Baldwin 	TAILQ_FOREACH(job, &aio_jobs, list) {
6925652770dSJohn Baldwin 		userp = job->userproc;
6932244ea07SJohn Dyson 		ki = userp->p_aioinfo;
6942244ea07SJohn Dyson 
69586bbef43SJohn Baldwin 		if (ki->kaio_active_count < max_aio_per_proc) {
6965652770dSJohn Baldwin 			TAILQ_REMOVE(&aio_jobs, job, list);
697f3215338SJohn Baldwin 			if (!aio_clear_cancel_function(job))
698f3215338SJohn Baldwin 				goto restart;
699f3215338SJohn Baldwin 
7001ce91824SDavid Xu 			/* Account for currently active jobs. */
7011ce91824SDavid Xu 			ki->kaio_active_count++;
7021ce91824SDavid Xu 			break;
7031ce91824SDavid Xu 		}
7041ce91824SDavid Xu 	}
7055652770dSJohn Baldwin 	return (job);
7062244ea07SJohn Dyson }
7072244ea07SJohn Dyson 
7082244ea07SJohn Dyson /*
7090dd6c035SJohn Baldwin  * Move all data to a permanent storage device.  This code
710801ac943SThomas Munro  * simulates the fsync and fdatasync syscalls.
71199eee864SDavid Xu  */
71299eee864SDavid Xu static int
aio_fsync_vnode(struct thread * td,struct vnode * vp,int op)713801ac943SThomas Munro aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
71499eee864SDavid Xu {
71599eee864SDavid Xu 	struct mount *mp;
71699eee864SDavid Xu 	int error;
71799eee864SDavid Xu 
718922bee44SKonstantin Belousov 	for (;;) {
719a75d1dddSMateusz Guzik 		error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
720922bee44SKonstantin Belousov 		if (error != 0)
721922bee44SKonstantin Belousov 			break;
722cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
723b068bb09SKonstantin Belousov 		vnode_pager_clean_async(vp);
724801ac943SThomas Munro 		if (op == LIO_DSYNC)
725801ac943SThomas Munro 			error = VOP_FDATASYNC(vp, td);
726801ac943SThomas Munro 		else
72799eee864SDavid Xu 			error = VOP_FSYNC(vp, MNT_WAIT, td);
72899eee864SDavid Xu 
729b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
73099eee864SDavid Xu 		vn_finished_write(mp);
7312933a7caSKonstantin Belousov 		if (error != ERELOOKUP)
732922bee44SKonstantin Belousov 			break;
733922bee44SKonstantin Belousov 	}
73499eee864SDavid Xu 	return (error);
73599eee864SDavid Xu }
73699eee864SDavid Xu 
73799eee864SDavid Xu /*
738f95c13dbSGleb Smirnoff  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
73972bce9ffSAlan Somers  * does the I/O request for the non-bio version of the operations.  The normal
74072bce9ffSAlan Somers  * vn operations are used, and this code should work in all instances for every
74172bce9ffSAlan Somers  * type of file, including pipes, sockets, fifos, and regular files.
7421ce91824SDavid Xu  *
7431aa4c324SDavid Xu  * XXX I don't think it works well for socket, pipe, and fifo.
7442244ea07SJohn Dyson  */
74588ed460eSAlan Cox static void
aio_process_rw(struct kaiocb * job)7465652770dSJohn Baldwin aio_process_rw(struct kaiocb *job)
747fd3bf775SJohn Dyson {
748f8f750c5SRobert Watson 	struct ucred *td_savedcred;
749b40ce416SJulian Elischer 	struct thread *td;
7502244ea07SJohn Dyson 	struct file *fp;
751bb430bc7SJohn Baldwin 	ssize_t cnt;
752b1012d80SJohn Baldwin 	long msgsnd_st, msgsnd_end;
753b1012d80SJohn Baldwin 	long msgrcv_st, msgrcv_end;
754b1012d80SJohn Baldwin 	long oublock_st, oublock_end;
755b1012d80SJohn Baldwin 	long inblock_st, inblock_end;
756022ca2fcSAlan Somers 	int error, opcode;
7572244ea07SJohn Dyson 
7585652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
759022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_READV ||
760022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITE ||
761022ca2fcSAlan Somers 	    job->uaiocb.aio_lio_opcode == LIO_WRITEV,
7625652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
763f95c13dbSGleb Smirnoff 
764f3215338SJohn Baldwin 	aio_switch_vmspace(job);
765b40ce416SJulian Elischer 	td = curthread;
766f8f750c5SRobert Watson 	td_savedcred = td->td_ucred;
7675652770dSJohn Baldwin 	td->td_ucred = job->cred;
768022ca2fcSAlan Somers 	job->uiop->uio_td = td;
7695652770dSJohn Baldwin 	fp = job->fd_file;
770bfbbc4aaSJason Evans 
771022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
772022ca2fcSAlan Somers 	cnt = job->uiop->uio_resid;
7732244ea07SJohn Dyson 
774b1012d80SJohn Baldwin 	msgrcv_st = td->td_ru.ru_msgrcv;
775b1012d80SJohn Baldwin 	msgsnd_st = td->td_ru.ru_msgsnd;
7761c4bcd05SJeff Roberson 	inblock_st = td->td_ru.ru_inblock;
7771c4bcd05SJeff Roberson 	oublock_st = td->td_ru.ru_oublock;
778b1012d80SJohn Baldwin 
779279d7226SMatthew Dillon 	/*
780a9bf5e37SDavid Xu 	 * aio_aqueue() acquires a reference to the file that is
7819b16adc1SAlan Cox 	 * released in aio_free_entry().
782279d7226SMatthew Dillon 	 */
783022ca2fcSAlan Somers 	if (opcode == LIO_READ || opcode == LIO_READV) {
784022ca2fcSAlan Somers 		if (job->uiop->uio_resid == 0)
7855114048bSKonstantin Belousov 			error = 0;
7865114048bSKonstantin Belousov 		else
787e4b7bbd6SKonstantin Belousov 			error = fo_read(fp, job->uiop, fp->f_cred,
788e4b7bbd6SKonstantin Belousov 			    (job->ioflags & KAIOCB_IO_FOFFSET) != 0 ? 0 :
789e4b7bbd6SKonstantin Belousov 			    FOF_OFFSET, td);
7902244ea07SJohn Dyson 	} else {
7916d53aa62SDavid Xu 		if (fp->f_type == DTYPE_VNODE)
7926d53aa62SDavid Xu 			bwillwrite();
793e4b7bbd6SKonstantin Belousov 		error = fo_write(fp, job->uiop, fp->f_cred, (job->ioflags &
794e4b7bbd6SKonstantin Belousov 		    KAIOCB_IO_FOFFSET) != 0 ? 0 : FOF_OFFSET, td);
7952244ea07SJohn Dyson 	}
796b1012d80SJohn Baldwin 	msgrcv_end = td->td_ru.ru_msgrcv;
797b1012d80SJohn Baldwin 	msgsnd_end = td->td_ru.ru_msgsnd;
7981c4bcd05SJeff Roberson 	inblock_end = td->td_ru.ru_inblock;
7991c4bcd05SJeff Roberson 	oublock_end = td->td_ru.ru_oublock;
800fd3bf775SJohn Dyson 
801b1012d80SJohn Baldwin 	job->msgrcv = msgrcv_end - msgrcv_st;
802b1012d80SJohn Baldwin 	job->msgsnd = msgsnd_end - msgsnd_st;
803b1012d80SJohn Baldwin 	job->inblock = inblock_end - inblock_st;
804b1012d80SJohn Baldwin 	job->outblock = oublock_end - oublock_st;
8052244ea07SJohn Dyson 
806022ca2fcSAlan Somers 	if (error != 0 && job->uiop->uio_resid != cnt) {
8072244ea07SJohn Dyson 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
8082244ea07SJohn Dyson 			error = 0;
8092247f489SAlan Somers 		if (error == EPIPE && (opcode & LIO_WRITE)) {
8105652770dSJohn Baldwin 			PROC_LOCK(job->userproc);
8115652770dSJohn Baldwin 			kern_psignal(job->userproc, SIGPIPE);
8125652770dSJohn Baldwin 			PROC_UNLOCK(job->userproc);
81319eb87d2SJohn Baldwin 		}
8142244ea07SJohn Dyson 	}
8152244ea07SJohn Dyson 
816022ca2fcSAlan Somers 	cnt -= job->uiop->uio_resid;
817f8f750c5SRobert Watson 	td->td_ucred = td_savedcred;
818f0ec1740SJohn Baldwin 	if (error)
819f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
820f0ec1740SJohn Baldwin 	else
821f0ec1740SJohn Baldwin 		aio_complete(job, cnt, 0);
8222244ea07SJohn Dyson }
8232244ea07SJohn Dyson 
82469cd28daSDoug Ambrisko static void
aio_process_sync(struct kaiocb * job)8255652770dSJohn Baldwin aio_process_sync(struct kaiocb *job)
826f95c13dbSGleb Smirnoff {
827f95c13dbSGleb Smirnoff 	struct thread *td = curthread;
828f95c13dbSGleb Smirnoff 	struct ucred *td_savedcred = td->td_ucred;
8295652770dSJohn Baldwin 	struct file *fp = job->fd_file;
830f95c13dbSGleb Smirnoff 	int error = 0;
831f95c13dbSGleb Smirnoff 
8322247f489SAlan Somers 	KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
8335652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
834f95c13dbSGleb Smirnoff 
8355652770dSJohn Baldwin 	td->td_ucred = job->cred;
836801ac943SThomas Munro 	if (fp->f_vnode != NULL) {
837801ac943SThomas Munro 		error = aio_fsync_vnode(td, fp->f_vnode,
838801ac943SThomas Munro 		    job->uaiocb.aio_lio_opcode);
839801ac943SThomas Munro 	}
840f95c13dbSGleb Smirnoff 	td->td_ucred = td_savedcred;
841f0ec1740SJohn Baldwin 	if (error)
842f0ec1740SJohn Baldwin 		aio_complete(job, -1, error);
843f0ec1740SJohn Baldwin 	else
844f0ec1740SJohn Baldwin 		aio_complete(job, 0, 0);
845f95c13dbSGleb Smirnoff }
846f95c13dbSGleb Smirnoff 
847f95c13dbSGleb Smirnoff static void
aio_process_mlock(struct kaiocb * job)8485652770dSJohn Baldwin aio_process_mlock(struct kaiocb *job)
8496160e12cSGleb Smirnoff {
8505652770dSJohn Baldwin 	struct aiocb *cb = &job->uaiocb;
8516160e12cSGleb Smirnoff 	int error;
8526160e12cSGleb Smirnoff 
8535652770dSJohn Baldwin 	KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
8545652770dSJohn Baldwin 	    ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
8556160e12cSGleb Smirnoff 
856f3215338SJohn Baldwin 	aio_switch_vmspace(job);
857496ab053SKonstantin Belousov 	error = kern_mlock(job->userproc, job->cred,
858496ab053SKonstantin Belousov 	    __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
859496ab053SKonstantin Belousov 	aio_complete(job, error != 0 ? -1 : 0, error);
8606160e12cSGleb Smirnoff }
8616160e12cSGleb Smirnoff 
8626160e12cSGleb Smirnoff static void
aio_bio_done_notify(struct proc * userp,struct kaiocb * job)863f3215338SJohn Baldwin aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
8641ce91824SDavid Xu {
8651ce91824SDavid Xu 	struct aioliojob *lj;
86669cd28daSDoug Ambrisko 	struct kaioinfo *ki;
8675652770dSJohn Baldwin 	struct kaiocb *sjob, *sjobn;
8681ce91824SDavid Xu 	int lj_done;
869f3215338SJohn Baldwin 	bool schedule_fsync;
87069cd28daSDoug Ambrisko 
87169cd28daSDoug Ambrisko 	ki = userp->p_aioinfo;
872759ccccaSDavid Xu 	AIO_LOCK_ASSERT(ki, MA_OWNED);
8735652770dSJohn Baldwin 	lj = job->lio;
87469cd28daSDoug Ambrisko 	lj_done = 0;
87569cd28daSDoug Ambrisko 	if (lj) {
8761ce91824SDavid Xu 		lj->lioj_finished_count++;
8771ce91824SDavid Xu 		if (lj->lioj_count == lj->lioj_finished_count)
87869cd28daSDoug Ambrisko 			lj_done = 1;
87969cd28daSDoug Ambrisko 	}
8805652770dSJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
881f3215338SJohn Baldwin 	MPASS(job->jobflags & KAIOCB_FINISHED);
88227b8220dSDavid Xu 
88327b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_RUNDOWN)
88427b8220dSDavid Xu 		goto notification_done;
88527b8220dSDavid Xu 
8865652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
8875652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
8886814c2daSKonstantin Belousov 		aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
8891ce91824SDavid Xu 
8905652770dSJohn Baldwin 	KNOTE_LOCKED(&job->klist, 1);
8911ce91824SDavid Xu 
89269cd28daSDoug Ambrisko 	if (lj_done) {
8931ce91824SDavid Xu 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
89469cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
8951ce91824SDavid Xu 			KNOTE_LOCKED(&lj->klist, 1);
89669cd28daSDoug Ambrisko 		}
8971ce91824SDavid Xu 		if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
89829331656SKonstantin Belousov 		    == LIOJ_SIGNAL &&
89929331656SKonstantin Belousov 		    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
9004c0fb2cfSDavid Xu 		    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
9016814c2daSKonstantin Belousov 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
9026814c2daSKonstantin Belousov 			    true);
90369cd28daSDoug Ambrisko 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
90469cd28daSDoug Ambrisko 		}
90569cd28daSDoug Ambrisko 	}
90627b8220dSDavid Xu 
90727b8220dSDavid Xu notification_done:
9085652770dSJohn Baldwin 	if (job->jobflags & KAIOCB_CHECKSYNC) {
909f3215338SJohn Baldwin 		schedule_fsync = false;
9105652770dSJohn Baldwin 		TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
911b9a53e16SJohn Baldwin 			if (job->fd_file != sjob->fd_file ||
912b9a53e16SJohn Baldwin 			    job->seqno >= sjob->seqno)
913b9a53e16SJohn Baldwin 				continue;
914b9a53e16SJohn Baldwin 			if (--sjob->pending > 0)
915b9a53e16SJohn Baldwin 				continue;
916b9a53e16SJohn Baldwin 			TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
917005ce8e4SJohn Baldwin 			if (!aio_clear_cancel_function_locked(sjob))
918f3215338SJohn Baldwin 				continue;
919b9a53e16SJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
920f3215338SJohn Baldwin 			schedule_fsync = true;
92199eee864SDavid Xu 		}
922f3215338SJohn Baldwin 		if (schedule_fsync)
923f3215338SJohn Baldwin 			taskqueue_enqueue(taskqueue_aiod_kick,
924f3215338SJohn Baldwin 			    &ki->kaio_sync_task);
92599eee864SDavid Xu 	}
92627b8220dSDavid Xu 	if (ki->kaio_flags & KAIO_WAKEUP) {
92769cd28daSDoug Ambrisko 		ki->kaio_flags &= ~KAIO_WAKEUP;
9281ce91824SDavid Xu 		wakeup(&userp->p_aioinfo);
92969cd28daSDoug Ambrisko 	}
93069cd28daSDoug Ambrisko }
93169cd28daSDoug Ambrisko 
9328a4dc40fSJohn Baldwin static void
aio_schedule_fsync(void * context,int pending)933f3215338SJohn Baldwin aio_schedule_fsync(void *context, int pending)
934f3215338SJohn Baldwin {
935f3215338SJohn Baldwin 	struct kaioinfo *ki;
936f3215338SJohn Baldwin 	struct kaiocb *job;
937f3215338SJohn Baldwin 
938f3215338SJohn Baldwin 	ki = context;
939f3215338SJohn Baldwin 	AIO_LOCK(ki);
940f3215338SJohn Baldwin 	while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
941f3215338SJohn Baldwin 		job = TAILQ_FIRST(&ki->kaio_syncready);
942f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncready, job, list);
943f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
944f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
945f3215338SJohn Baldwin 		AIO_LOCK(ki);
946f3215338SJohn Baldwin 	}
947f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
948f3215338SJohn Baldwin }
949f3215338SJohn Baldwin 
950f3215338SJohn Baldwin bool
aio_cancel_cleared(struct kaiocb * job)951f3215338SJohn Baldwin aio_cancel_cleared(struct kaiocb *job)
952f3215338SJohn Baldwin {
953f3215338SJohn Baldwin 
954f3215338SJohn Baldwin 	/*
955f3215338SJohn Baldwin 	 * The caller should hold the same queue lock held when
956f3215338SJohn Baldwin 	 * aio_clear_cancel_function() was called and set this flag
957f3215338SJohn Baldwin 	 * ensuring this check sees an up-to-date value.  However,
958f3215338SJohn Baldwin 	 * there is no way to assert that.
959f3215338SJohn Baldwin 	 */
960f3215338SJohn Baldwin 	return ((job->jobflags & KAIOCB_CLEARED) != 0);
961f3215338SJohn Baldwin }
962f3215338SJohn Baldwin 
963005ce8e4SJohn Baldwin static bool
aio_clear_cancel_function_locked(struct kaiocb * job)964005ce8e4SJohn Baldwin aio_clear_cancel_function_locked(struct kaiocb *job)
965005ce8e4SJohn Baldwin {
966005ce8e4SJohn Baldwin 
967005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
968005ce8e4SJohn Baldwin 	MPASS(job->cancel_fn != NULL);
969005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLING) {
970005ce8e4SJohn Baldwin 		job->jobflags |= KAIOCB_CLEARED;
971005ce8e4SJohn Baldwin 		return (false);
972005ce8e4SJohn Baldwin 	}
973005ce8e4SJohn Baldwin 	job->cancel_fn = NULL;
974005ce8e4SJohn Baldwin 	return (true);
975005ce8e4SJohn Baldwin }
976005ce8e4SJohn Baldwin 
977f3215338SJohn Baldwin bool
aio_clear_cancel_function(struct kaiocb * job)978f3215338SJohn Baldwin aio_clear_cancel_function(struct kaiocb *job)
979f3215338SJohn Baldwin {
980f3215338SJohn Baldwin 	struct kaioinfo *ki;
981005ce8e4SJohn Baldwin 	bool ret;
982f3215338SJohn Baldwin 
983f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
984f3215338SJohn Baldwin 	AIO_LOCK(ki);
985005ce8e4SJohn Baldwin 	ret = aio_clear_cancel_function_locked(job);
986f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
987005ce8e4SJohn Baldwin 	return (ret);
988f3215338SJohn Baldwin }
989005ce8e4SJohn Baldwin 
990005ce8e4SJohn Baldwin static bool
aio_set_cancel_function_locked(struct kaiocb * job,aio_cancel_fn_t * func)991005ce8e4SJohn Baldwin aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
992005ce8e4SJohn Baldwin {
993005ce8e4SJohn Baldwin 
994005ce8e4SJohn Baldwin 	AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
995005ce8e4SJohn Baldwin 	if (job->jobflags & KAIOCB_CANCELLED)
996005ce8e4SJohn Baldwin 		return (false);
997005ce8e4SJohn Baldwin 	job->cancel_fn = func;
998f3215338SJohn Baldwin 	return (true);
999f3215338SJohn Baldwin }
1000f3215338SJohn Baldwin 
1001f3215338SJohn Baldwin bool
aio_set_cancel_function(struct kaiocb * job,aio_cancel_fn_t * func)1002f3215338SJohn Baldwin aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1003f3215338SJohn Baldwin {
1004f3215338SJohn Baldwin 	struct kaioinfo *ki;
1005005ce8e4SJohn Baldwin 	bool ret;
1006f3215338SJohn Baldwin 
1007f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1008f3215338SJohn Baldwin 	AIO_LOCK(ki);
1009005ce8e4SJohn Baldwin 	ret = aio_set_cancel_function_locked(job, func);
1010f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1011005ce8e4SJohn Baldwin 	return (ret);
1012f3215338SJohn Baldwin }
1013f3215338SJohn Baldwin 
1014f3215338SJohn Baldwin void
aio_complete(struct kaiocb * job,long status,int error)1015f3215338SJohn Baldwin aio_complete(struct kaiocb *job, long status, int error)
1016f3215338SJohn Baldwin {
1017f3215338SJohn Baldwin 	struct kaioinfo *ki;
1018f3215338SJohn Baldwin 	struct proc *userp;
1019f3215338SJohn Baldwin 
1020f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.error = error;
1021f3215338SJohn Baldwin 	job->uaiocb._aiocb_private.status = status;
1022f3215338SJohn Baldwin 
1023f3215338SJohn Baldwin 	userp = job->userproc;
1024f3215338SJohn Baldwin 	ki = userp->p_aioinfo;
1025f3215338SJohn Baldwin 
1026f3215338SJohn Baldwin 	AIO_LOCK(ki);
1027f3215338SJohn Baldwin 	KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1028f3215338SJohn Baldwin 	    ("duplicate aio_complete"));
1029f3215338SJohn Baldwin 	job->jobflags |= KAIOCB_FINISHED;
1030f3215338SJohn Baldwin 	if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1031f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1032f3215338SJohn Baldwin 		aio_bio_done_notify(userp, job);
1033f3215338SJohn Baldwin 	}
1034f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1035f3215338SJohn Baldwin }
1036f3215338SJohn Baldwin 
1037f3215338SJohn Baldwin void
aio_cancel(struct kaiocb * job)1038f3215338SJohn Baldwin aio_cancel(struct kaiocb *job)
1039f3215338SJohn Baldwin {
1040f3215338SJohn Baldwin 
1041f3215338SJohn Baldwin 	aio_complete(job, -1, ECANCELED);
1042f3215338SJohn Baldwin }
1043f3215338SJohn Baldwin 
1044f3215338SJohn Baldwin void
aio_switch_vmspace(struct kaiocb * job)10455652770dSJohn Baldwin aio_switch_vmspace(struct kaiocb *job)
10468a4dc40fSJohn Baldwin {
10478a4dc40fSJohn Baldwin 
10485652770dSJohn Baldwin 	vmspace_switch_aio(job->userproc->p_vmspace);
10498a4dc40fSJohn Baldwin }
10508a4dc40fSJohn Baldwin 
10512244ea07SJohn Dyson /*
1052f95c13dbSGleb Smirnoff  * The AIO daemon, most of the actual work is done in aio_process_*,
105384af4da6SJohn Dyson  * but the setup (and address space mgmt) is done in this routine.
10542244ea07SJohn Dyson  */
10552244ea07SJohn Dyson static void
aio_daemon(void * _id)10561ce91824SDavid Xu aio_daemon(void *_id)
10572244ea07SJohn Dyson {
10585652770dSJohn Baldwin 	struct kaiocb *job;
105939314b7dSJohn Baldwin 	struct aioproc *aiop;
1060bfbbc4aaSJason Evans 	struct kaioinfo *ki;
1061f3215338SJohn Baldwin 	struct proc *p;
10628a4dc40fSJohn Baldwin 	struct vmspace *myvm;
1063b40ce416SJulian Elischer 	struct thread *td = curthread;
10641ce91824SDavid Xu 	int id = (intptr_t)_id;
10652244ea07SJohn Dyson 
10662244ea07SJohn Dyson 	/*
10678a4dc40fSJohn Baldwin 	 * Grab an extra reference on the daemon's vmspace so that it
10688a4dc40fSJohn Baldwin 	 * doesn't get freed by jobs that switch to a different
10698a4dc40fSJohn Baldwin 	 * vmspace.
10702244ea07SJohn Dyson 	 */
10718a4dc40fSJohn Baldwin 	p = td->td_proc;
10728a4dc40fSJohn Baldwin 	myvm = vmspace_acquire_ref(p);
1073fd3bf775SJohn Dyson 
10748a4dc40fSJohn Baldwin 	KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1075fd3bf775SJohn Dyson 
1076fd3bf775SJohn Dyson 	/*
1077bfbbc4aaSJason Evans 	 * Allocate and ready the aio control info.  There is one aiop structure
1078bfbbc4aaSJason Evans 	 * per daemon.
1079fd3bf775SJohn Dyson 	 */
10809553bc89SMark Johnston 	aiop = malloc(sizeof(*aiop), M_AIO, M_WAITOK);
108139314b7dSJohn Baldwin 	aiop->aioproc = p;
108239314b7dSJohn Baldwin 	aiop->aioprocflags = 0;
1083bfbbc4aaSJason Evans 
1084fd3bf775SJohn Dyson 	/*
1085fd3bf775SJohn Dyson 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1086b40ce416SJulian Elischer 	 * and creating too many daemons.)
1087fd3bf775SJohn Dyson 	 */
10881ce91824SDavid Xu 	sema_post(&aio_newproc_sem);
10892244ea07SJohn Dyson 
10901ce91824SDavid Xu 	mtx_lock(&aio_job_mtx);
1091bfbbc4aaSJason Evans 	for (;;) {
1092fd3bf775SJohn Dyson 		/*
1093fd3bf775SJohn Dyson 		 * Take daemon off of free queue
1094fd3bf775SJohn Dyson 		 */
109539314b7dSJohn Baldwin 		if (aiop->aioprocflags & AIOP_FREE) {
10962244ea07SJohn Dyson 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
109739314b7dSJohn Baldwin 			aiop->aioprocflags &= ~AIOP_FREE;
10982244ea07SJohn Dyson 		}
10992244ea07SJohn Dyson 
1100fd3bf775SJohn Dyson 		/*
1101bfbbc4aaSJason Evans 		 * Check for jobs.
1102fd3bf775SJohn Dyson 		 */
11035652770dSJohn Baldwin 		while ((job = aio_selectjob(aiop)) != NULL) {
11041ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11052244ea07SJohn Dyson 
1106f3215338SJohn Baldwin 			ki = job->userproc->p_aioinfo;
1107f3215338SJohn Baldwin 			job->handle_fn(job);
110884af4da6SJohn Dyson 
11099b84335cSDavid Xu 			mtx_lock(&aio_job_mtx);
11109b84335cSDavid Xu 			/* Decrement the active job count. */
11119b84335cSDavid Xu 			ki->kaio_active_count--;
11122244ea07SJohn Dyson 		}
11132244ea07SJohn Dyson 
1114fd3bf775SJohn Dyson 		/*
1115bfbbc4aaSJason Evans 		 * Disconnect from user address space.
1116fd3bf775SJohn Dyson 		 */
11178a4dc40fSJohn Baldwin 		if (p->p_vmspace != myvm) {
11181ce91824SDavid Xu 			mtx_unlock(&aio_job_mtx);
11198a4dc40fSJohn Baldwin 			vmspace_switch_aio(myvm);
11201ce91824SDavid Xu 			mtx_lock(&aio_job_mtx);
11211ce91824SDavid Xu 			/*
11221ce91824SDavid Xu 			 * We have to restart to avoid race, we only sleep if
11238a4dc40fSJohn Baldwin 			 * no job can be selected.
11241ce91824SDavid Xu 			 */
11251ce91824SDavid Xu 			continue;
1126fd3bf775SJohn Dyson 		}
1127fd3bf775SJohn Dyson 
11281ce91824SDavid Xu 		mtx_assert(&aio_job_mtx, MA_OWNED);
11291ce91824SDavid Xu 
1130fd3bf775SJohn Dyson 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
113139314b7dSJohn Baldwin 		aiop->aioprocflags |= AIOP_FREE;
1132fd3bf775SJohn Dyson 
1133fd3bf775SJohn Dyson 		/*
1134bfbbc4aaSJason Evans 		 * If daemon is inactive for a long time, allow it to exit,
1135bfbbc4aaSJason Evans 		 * thereby freeing resources.
1136fd3bf775SJohn Dyson 		 */
113739314b7dSJohn Baldwin 		if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
11388a4dc40fSJohn Baldwin 		    aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
113939314b7dSJohn Baldwin 		    (aiop->aioprocflags & AIOP_FREE) &&
11408a4dc40fSJohn Baldwin 		    num_aio_procs > target_aio_procs)
11418a4dc40fSJohn Baldwin 			break;
11428a4dc40fSJohn Baldwin 	}
1143fd3bf775SJohn Dyson 	TAILQ_REMOVE(&aio_freeproc, aiop, list);
114484af4da6SJohn Dyson 	num_aio_procs--;
11451ce91824SDavid Xu 	mtx_unlock(&aio_job_mtx);
11469553bc89SMark Johnston 	free(aiop, M_AIO);
11471ce91824SDavid Xu 	free_unr(aiod_unr, id);
11488a4dc40fSJohn Baldwin 	vmspace_free(myvm);
11498a4dc40fSJohn Baldwin 
11508a4dc40fSJohn Baldwin 	KASSERT(p->p_vmspace == myvm,
11518a4dc40fSJohn Baldwin 	    ("AIOD: bad vmspace for exiting daemon"));
1152f7db0c95SMark Johnston 	KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1153f7db0c95SMark Johnston 	    ("AIOD: bad vm refcnt for exiting daemon: %d",
1154f7db0c95SMark Johnston 	    refcount_load(&myvm->vm_refcnt)));
11553745c395SJulian Elischer 	kproc_exit(0);
1156fd3bf775SJohn Dyson }
11572244ea07SJohn Dyson 
11582244ea07SJohn Dyson /*
1159bfbbc4aaSJason Evans  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1160bfbbc4aaSJason Evans  * AIO daemon modifies its environment itself.
11612244ea07SJohn Dyson  */
11622244ea07SJohn Dyson static int
aio_newproc(int * start)11631ce91824SDavid Xu aio_newproc(int *start)
1164fd3bf775SJohn Dyson {
11652244ea07SJohn Dyson 	int error;
1166c9a970a7SAlan Cox 	struct proc *p;
11671ce91824SDavid Xu 	int id;
11682244ea07SJohn Dyson 
11691ce91824SDavid Xu 	id = alloc_unr(aiod_unr);
11703745c395SJulian Elischer 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
11711ce91824SDavid Xu 		RFNOWAIT, 0, "aiod%d", id);
11721ce91824SDavid Xu 	if (error == 0) {
1173fd3bf775SJohn Dyson 		/*
11741ce91824SDavid Xu 		 * Wait until daemon is started.
1175fd3bf775SJohn Dyson 		 */
11761ce91824SDavid Xu 		sema_wait(&aio_newproc_sem);
11771ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
117884af4da6SJohn Dyson 		num_aio_procs++;
11791ce91824SDavid Xu 		if (start != NULL)
11807f34b521SDavid Xu 			(*start)--;
11811ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
11821ce91824SDavid Xu 	} else {
11831ce91824SDavid Xu 		free_unr(aiod_unr, id);
11841ce91824SDavid Xu 	}
1185ac41f2efSAlfred Perlstein 	return (error);
11862244ea07SJohn Dyson }
11872244ea07SJohn Dyson 
11882244ea07SJohn Dyson /*
118972bce9ffSAlan Somers  * Try the high-performance, low-overhead bio method for eligible
119088ed460eSAlan Cox  * VCHR devices.  This method doesn't use an aio helper thread, and
119188ed460eSAlan Cox  * thus has very low overhead.
119288ed460eSAlan Cox  *
1193a9bf5e37SDavid Xu  * Assumes that the caller, aio_aqueue(), has incremented the file
119488ed460eSAlan Cox  * structure's reference count, preventing its deallocation for the
119588ed460eSAlan Cox  * duration of this call.
1196fd3bf775SJohn Dyson  */
119788ed460eSAlan Cox static int
aio_qbio(struct proc * p,struct kaiocb * job)119872bce9ffSAlan Somers aio_qbio(struct proc *p, struct kaiocb *job)
1199fd3bf775SJohn Dyson {
1200fd3bf775SJohn Dyson 	struct aiocb *cb;
1201fd3bf775SJohn Dyson 	struct file *fp;
1202f743d981SAlexander Motin 	struct buf *pbuf;
1203fd3bf775SJohn Dyson 	struct vnode *vp;
1204f3215a60SKonstantin Belousov 	struct cdevsw *csw;
1205f3215a60SKonstantin Belousov 	struct cdev *dev;
1206fd3bf775SJohn Dyson 	struct kaioinfo *ki;
1207022ca2fcSAlan Somers 	struct bio **bios = NULL;
1208022ca2fcSAlan Somers 	off_t offset;
1209022ca2fcSAlan Somers 	int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1210f743d981SAlexander Motin 	vm_prot_t prot;
1211022ca2fcSAlan Somers 	bool use_unmapped;
1212fd3bf775SJohn Dyson 
12135652770dSJohn Baldwin 	cb = &job->uaiocb;
12145652770dSJohn Baldwin 	fp = job->fd_file;
1215022ca2fcSAlan Somers 	opcode = cb->aio_lio_opcode;
1216fd3bf775SJohn Dyson 
1217022ca2fcSAlan Somers 	if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1218022ca2fcSAlan Somers 	    opcode == LIO_READ || opcode == LIO_READV))
1219f54c5606SJohn Baldwin 		return (-1);
12206160e12cSGleb Smirnoff 	if (fp == NULL || fp->f_type != DTYPE_VNODE)
1221008626c3SPoul-Henning Kamp 		return (-1);
1222fd3bf775SJohn Dyson 
12233b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
1224f743d981SAlexander Motin 	if (vp->v_type != VCHR)
1225f582ac06SBrian Feldman 		return (-1);
1226ad8de0f2SDavid Xu 	if (vp->v_bufobj.bo_bsize == 0)
1227ad8de0f2SDavid Xu 		return (-1);
1228022ca2fcSAlan Somers 
12292247f489SAlan Somers 	bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1230022ca2fcSAlan Somers 	iovcnt = job->uiop->uio_iovcnt;
1231022ca2fcSAlan Somers 	if (iovcnt > max_buf_aio)
1232008626c3SPoul-Henning Kamp 		return (-1);
1233022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1234022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1235022ca2fcSAlan Somers 			return (-1);
1236022ca2fcSAlan Somers 		if (job->uiop->uio_iov[i].iov_len > maxphys) {
1237022ca2fcSAlan Somers 			error = -1;
1238022ca2fcSAlan Somers 			return (-1);
1239022ca2fcSAlan Somers 		}
1240022ca2fcSAlan Somers 	}
1241022ca2fcSAlan Somers 	offset = cb->aio_offset;
1242fd3bf775SJohn Dyson 
1243f3215a60SKonstantin Belousov 	ref = 0;
1244f3215a60SKonstantin Belousov 	csw = devvn_refthread(vp, &dev, &ref);
1245f3215a60SKonstantin Belousov 	if (csw == NULL)
1246f3215a60SKonstantin Belousov 		return (ENXIO);
1247f743d981SAlexander Motin 
1248f743d981SAlexander Motin 	if ((csw->d_flags & D_DISK) == 0) {
1249f743d981SAlexander Motin 		error = -1;
1250f743d981SAlexander Motin 		goto unref;
1251f743d981SAlexander Motin 	}
1252022ca2fcSAlan Somers 	if (job->uiop->uio_resid > dev->si_iosize_max) {
1253f3215a60SKonstantin Belousov 		error = -1;
1254f3215a60SKonstantin Belousov 		goto unref;
1255f3215a60SKonstantin Belousov 	}
1256f3215a60SKonstantin Belousov 
1257f743d981SAlexander Motin 	ki = p->p_aioinfo;
1258022ca2fcSAlan Somers 	job->error = 0;
12594d805eacSJohn Baldwin 
1260022ca2fcSAlan Somers 	use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1261022ca2fcSAlan Somers 	if (!use_unmapped) {
1262022ca2fcSAlan Somers 		AIO_LOCK(ki);
1263022ca2fcSAlan Somers 		if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1264022ca2fcSAlan Somers 			AIO_UNLOCK(ki);
1265f54c5606SJohn Baldwin 			error = EAGAIN;
1266f743d981SAlexander Motin 			goto unref;
1267f743d981SAlexander Motin 		}
1268022ca2fcSAlan Somers 		ki->kaio_buffer_count += iovcnt;
1269022ca2fcSAlan Somers 		AIO_UNLOCK(ki);
1270022ca2fcSAlan Somers 	}
12714d805eacSJohn Baldwin 
1272022ca2fcSAlan Somers 	bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
127398844e99SJohn Baldwin 	refcount_init(&job->nbio, iovcnt);
1274022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++) {
1275022ca2fcSAlan Somers 		struct vm_page** pages;
1276022ca2fcSAlan Somers 		struct bio *bp;
1277022ca2fcSAlan Somers 		void *buf;
1278022ca2fcSAlan Somers 		size_t nbytes;
1279022ca2fcSAlan Somers 		int npages;
1280022ca2fcSAlan Somers 
1281022ca2fcSAlan Somers 		buf = job->uiop->uio_iov[i].iov_base;
1282022ca2fcSAlan Somers 		nbytes = job->uiop->uio_iov[i].iov_len;
1283022ca2fcSAlan Somers 
1284022ca2fcSAlan Somers 		bios[i] = g_alloc_bio();
1285022ca2fcSAlan Somers 		bp = bios[i];
1286022ca2fcSAlan Somers 
1287022ca2fcSAlan Somers 		poff = (vm_offset_t)buf & PAGE_MASK;
1288022ca2fcSAlan Somers 		if (use_unmapped) {
1289022ca2fcSAlan Somers 			pbuf = NULL;
1290022ca2fcSAlan Somers 			pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1291022ca2fcSAlan Somers 			    nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1292022ca2fcSAlan Somers 		} else {
129301206038SAlan Somers 			pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1294f743d981SAlexander Motin 			BUF_KERNPROC(pbuf);
129501206038SAlan Somers 			pages = pbuf->b_pages;
12964d805eacSJohn Baldwin 		}
12971ce91824SDavid Xu 
1298022ca2fcSAlan Somers 		bp->bio_length = nbytes;
1299022ca2fcSAlan Somers 		bp->bio_bcount = nbytes;
130072bce9ffSAlan Somers 		bp->bio_done = aio_biowakeup;
1301022ca2fcSAlan Somers 		bp->bio_offset = offset;
1302022ca2fcSAlan Somers 		bp->bio_cmd = bio_cmd;
1303f743d981SAlexander Motin 		bp->bio_dev = dev;
130401206038SAlan Somers 		bp->bio_caller1 = job;
130501206038SAlan Somers 		bp->bio_caller2 = pbuf;
1306f743d981SAlexander Motin 
1307f743d981SAlexander Motin 		prot = VM_PROT_READ;
1308022ca2fcSAlan Somers 		if (opcode == LIO_READ || opcode == LIO_READV)
1309f743d981SAlexander Motin 			prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
131001206038SAlan Somers 		npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1311022ca2fcSAlan Somers 		    (vm_offset_t)buf, bp->bio_length, prot, pages,
1312cd853791SKonstantin Belousov 		    atop(maxphys) + 1);
131301206038SAlan Somers 		if (npages < 0) {
1314022ca2fcSAlan Somers 			if (pbuf != NULL)
1315022ca2fcSAlan Somers 				uma_zfree(pbuf_zone, pbuf);
1316022ca2fcSAlan Somers 			else
1317022ca2fcSAlan Somers 				free(pages, M_TEMP);
1318f743d981SAlexander Motin 			error = EFAULT;
1319022ca2fcSAlan Somers 			g_destroy_bio(bp);
1320022ca2fcSAlan Somers 			i--;
1321022ca2fcSAlan Somers 			goto destroy_bios;
1322f743d981SAlexander Motin 		}
13234d805eacSJohn Baldwin 		if (pbuf != NULL) {
132401206038SAlan Somers 			pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1325f743d981SAlexander Motin 			bp->bio_data = pbuf->b_data + poff;
132601206038SAlan Somers 			pbuf->b_npages = npages;
1327022ca2fcSAlan Somers 			atomic_add_int(&num_buf_aio, 1);
1328f743d981SAlexander Motin 		} else {
132901206038SAlan Somers 			bp->bio_ma = pages;
133001206038SAlan Somers 			bp->bio_ma_n = npages;
1331f743d981SAlexander Motin 			bp->bio_ma_offset = poff;
1332f743d981SAlexander Motin 			bp->bio_data = unmapped_buf;
1333f743d981SAlexander Motin 			bp->bio_flags |= BIO_UNMAPPED;
13348091e52bSJohn Baldwin 			atomic_add_int(&num_unmapped_aio, 1);
1335f743d981SAlexander Motin 		}
1336f743d981SAlexander Motin 
1337022ca2fcSAlan Somers 		offset += nbytes;
1338022ca2fcSAlan Somers 	}
1339022ca2fcSAlan Somers 
1340bfbbc4aaSJason Evans 	/* Perform transfer. */
1341022ca2fcSAlan Somers 	for (i = 0; i < iovcnt; i++)
1342022ca2fcSAlan Somers 		csw->d_strategy(bios[i]);
1343022ca2fcSAlan Somers 	free(bios, M_TEMP);
1344022ca2fcSAlan Somers 
1345f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1346ac41f2efSAlfred Perlstein 	return (0);
1347fd3bf775SJohn Dyson 
1348022ca2fcSAlan Somers destroy_bios:
1349022ca2fcSAlan Somers 	for (; i >= 0; i--)
1350022ca2fcSAlan Somers 		aio_biocleanup(bios[i]);
1351022ca2fcSAlan Somers 	free(bios, M_TEMP);
1352f3215a60SKonstantin Belousov unref:
1353f3215a60SKonstantin Belousov 	dev_relthread(dev, ref);
1354fd3bf775SJohn Dyson 	return (error);
1355fd3bf775SJohn Dyson }
1356fd3bf775SJohn Dyson 
1357399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
13583858a1f4SJohn Baldwin static int
convert_old_sigevent(struct osigevent * osig,struct sigevent * nsig)13593858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
13603858a1f4SJohn Baldwin {
13613858a1f4SJohn Baldwin 
13623858a1f4SJohn Baldwin 	/*
13633858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
13643858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
13653858a1f4SJohn Baldwin 	 */
13663858a1f4SJohn Baldwin 	nsig->sigev_notify = osig->sigev_notify;
13673858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
13683858a1f4SJohn Baldwin 	case SIGEV_NONE:
13693858a1f4SJohn Baldwin 		break;
13703858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
13713858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
13723858a1f4SJohn Baldwin 		break;
13733858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
13743858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
13753858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
13763858a1f4SJohn Baldwin 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
13773858a1f4SJohn Baldwin 		break;
13783858a1f4SJohn Baldwin 	default:
13793858a1f4SJohn Baldwin 		return (EINVAL);
13803858a1f4SJohn Baldwin 	}
13813858a1f4SJohn Baldwin 	return (0);
13823858a1f4SJohn Baldwin }
13833858a1f4SJohn Baldwin 
13843858a1f4SJohn Baldwin static int
aiocb_copyin_old_sigevent(struct aiocb * ujob,struct kaiocb * kjob,int type __unused)1385022ca2fcSAlan Somers aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1386022ca2fcSAlan Somers     int type __unused)
13873858a1f4SJohn Baldwin {
13883858a1f4SJohn Baldwin 	struct oaiocb *ojob;
1389022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
13903858a1f4SJohn Baldwin 	int error;
13913858a1f4SJohn Baldwin 
1392022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
1393022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct oaiocb));
13943858a1f4SJohn Baldwin 	if (error)
13953858a1f4SJohn Baldwin 		return (error);
1396022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1397022ca2fcSAlan Somers 	ojob = (struct oaiocb *)kcb;
1398022ca2fcSAlan Somers 	return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
13993858a1f4SJohn Baldwin }
1400399e8c17SJohn Baldwin #endif
14013858a1f4SJohn Baldwin 
14023858a1f4SJohn Baldwin static int
aiocb_copyin(struct aiocb * ujob,struct kaiocb * kjob,int type)1403022ca2fcSAlan Somers aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
14043858a1f4SJohn Baldwin {
1405022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
1406022ca2fcSAlan Somers 	int error;
14073858a1f4SJohn Baldwin 
1408022ca2fcSAlan Somers 	error = copyin(ujob, kcb, sizeof(struct aiocb));
1409022ca2fcSAlan Somers 	if (error)
1410022ca2fcSAlan Somers 		return (error);
1411f30a1ae8SThomas Munro 	if (type == LIO_NOP)
1412f30a1ae8SThomas Munro 		type = kcb->aio_lio_opcode;
14132247f489SAlan Somers 	if (type & LIO_VECTORED) {
1414022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
1415022ca2fcSAlan Somers 		error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1416022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
1417022ca2fcSAlan Somers 	}
1418022ca2fcSAlan Somers 
1419022ca2fcSAlan Somers 	return (error);
14203858a1f4SJohn Baldwin }
14213858a1f4SJohn Baldwin 
14223858a1f4SJohn Baldwin static long
aiocb_fetch_status(struct aiocb * ujob)14233858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob)
14243858a1f4SJohn Baldwin {
14253858a1f4SJohn Baldwin 
14263858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.status));
14273858a1f4SJohn Baldwin }
14283858a1f4SJohn Baldwin 
14293858a1f4SJohn Baldwin static long
aiocb_fetch_error(struct aiocb * ujob)14303858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob)
14313858a1f4SJohn Baldwin {
14323858a1f4SJohn Baldwin 
14333858a1f4SJohn Baldwin 	return (fuword(&ujob->_aiocb_private.error));
14343858a1f4SJohn Baldwin }
14353858a1f4SJohn Baldwin 
14363858a1f4SJohn Baldwin static int
aiocb_store_status(struct aiocb * ujob,long status)14373858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status)
14383858a1f4SJohn Baldwin {
14393858a1f4SJohn Baldwin 
14403858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.status, status));
14413858a1f4SJohn Baldwin }
14423858a1f4SJohn Baldwin 
14433858a1f4SJohn Baldwin static int
aiocb_store_error(struct aiocb * ujob,long error)14443858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error)
14453858a1f4SJohn Baldwin {
14463858a1f4SJohn Baldwin 
14473858a1f4SJohn Baldwin 	return (suword(&ujob->_aiocb_private.error, error));
14483858a1f4SJohn Baldwin }
14493858a1f4SJohn Baldwin 
14503858a1f4SJohn Baldwin static int
aiocb_store_aiocb(struct aiocb ** ujobp,struct aiocb * ujob)14513858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
14523858a1f4SJohn Baldwin {
14533858a1f4SJohn Baldwin 
14543858a1f4SJohn Baldwin 	return (suword(ujobp, (long)ujob));
14553858a1f4SJohn Baldwin }
14563858a1f4SJohn Baldwin 
14573858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = {
1458849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin,
14593858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14603858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14613858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14623858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14633858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14643858a1f4SJohn Baldwin };
14653858a1f4SJohn Baldwin 
1466399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
14673858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = {
1468849aef49SAndrew Turner 	.aio_copyin = aiocb_copyin_old_sigevent,
14693858a1f4SJohn Baldwin 	.fetch_status = aiocb_fetch_status,
14703858a1f4SJohn Baldwin 	.fetch_error = aiocb_fetch_error,
14713858a1f4SJohn Baldwin 	.store_status = aiocb_store_status,
14723858a1f4SJohn Baldwin 	.store_error = aiocb_store_error,
14733858a1f4SJohn Baldwin 	.store_aiocb = aiocb_store_aiocb,
14743858a1f4SJohn Baldwin };
1475399e8c17SJohn Baldwin #endif
14763858a1f4SJohn Baldwin 
1477bfbbc4aaSJason Evans /*
147872bce9ffSAlan Somers  * Queue a new AIO request.  Choosing either the threaded or direct bio VCHR
1479bfbbc4aaSJason Evans  * technique is done in this code.
14802244ea07SJohn Dyson  */
14816a1162d4SAlexander Leidinger int
aio_aqueue(struct thread * td,struct aiocb * ujob,struct aioliojob * lj,int type,struct aiocb_ops * ops)14825652770dSJohn Baldwin aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
14833858a1f4SJohn Baldwin     int type, struct aiocb_ops *ops)
1484fd3bf775SJohn Dyson {
1485b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1486022ca2fcSAlan Somers 	struct file *fp = NULL;
1487f3215338SJohn Baldwin 	struct kaiocb *job;
14882244ea07SJohn Dyson 	struct kaioinfo *ki;
1489c6fa9f78SAlan Cox 	struct kevent kev;
14901ce91824SDavid Xu 	int opcode;
14911ce91824SDavid Xu 	int error;
14924db71d27SJohn-Mark Gurney 	int fd, kqfd;
1493fde80935SDavid Xu 	u_short evflags;
14942244ea07SJohn Dyson 
1495a9bf5e37SDavid Xu 	if (p->p_aioinfo == NULL)
1496a9bf5e37SDavid Xu 		aio_init_aioinfo(p);
1497a9bf5e37SDavid Xu 
14981ce91824SDavid Xu 	ki = p->p_aioinfo;
14991ce91824SDavid Xu 
15005652770dSJohn Baldwin 	ops->store_status(ujob, -1);
15015652770dSJohn Baldwin 	ops->store_error(ujob, 0);
1502a9bf5e37SDavid Xu 
1503a9bf5e37SDavid Xu 	if (num_queue_count >= max_queue_count ||
150486bbef43SJohn Baldwin 	    ki->kaio_count >= max_aio_queue_per_proc) {
1505022ca2fcSAlan Somers 		error = EAGAIN;
1506022ca2fcSAlan Somers 		goto err1;
1507a9bf5e37SDavid Xu 	}
1508a9bf5e37SDavid Xu 
15095652770dSJohn Baldwin 	job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
15105652770dSJohn Baldwin 	knlist_init_mtx(&job->klist, AIO_MTX(ki));
1511fd3bf775SJohn Dyson 
1512022ca2fcSAlan Somers 	error = ops->aio_copyin(ujob, job, type);
1513022ca2fcSAlan Somers 	if (error)
1514022ca2fcSAlan Somers 		goto err2;
151568d71118SDavid Xu 
1516bb430bc7SJohn Baldwin 	if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1517022ca2fcSAlan Somers 		error = EINVAL;
1518022ca2fcSAlan Somers 		goto err2;
1519434ea137SGleb Smirnoff 	}
1520434ea137SGleb Smirnoff 
15215652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
15225652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
15235652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
15245652770dSJohn Baldwin 	    job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1525022ca2fcSAlan Somers 		error = EINVAL;
1526022ca2fcSAlan Somers 		goto err2;
152768d71118SDavid Xu 	}
152868d71118SDavid Xu 
15295652770dSJohn Baldwin 	if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
15305652770dSJohn Baldwin 	     job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
15315652770dSJohn Baldwin 		!_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1532022ca2fcSAlan Somers 		error = EINVAL;
1533022ca2fcSAlan Somers 		goto err2;
15342f3cf918SAlfred Perlstein 	}
15352244ea07SJohn Dyson 
1536ff1a3078SAlan Somers 	/* Get the opcode. */
1537ff1a3078SAlan Somers 	if (type == LIO_NOP) {
1538e4b7bbd6SKonstantin Belousov 		switch (job->uaiocb.aio_lio_opcode & ~LIO_FOFFSET) {
1539ff1a3078SAlan Somers 		case LIO_WRITE:
1540f30a1ae8SThomas Munro 		case LIO_WRITEV:
1541ff1a3078SAlan Somers 		case LIO_NOP:
1542ff1a3078SAlan Somers 		case LIO_READ:
1543f30a1ae8SThomas Munro 		case LIO_READV:
1544e4b7bbd6SKonstantin Belousov 			opcode = job->uaiocb.aio_lio_opcode & ~LIO_FOFFSET;
1545e4b7bbd6SKonstantin Belousov 			if ((job->uaiocb.aio_lio_opcode & LIO_FOFFSET) != 0)
1546e4b7bbd6SKonstantin Belousov 				job->ioflags |= KAIOCB_IO_FOFFSET;
1547ff1a3078SAlan Somers 			break;
1548ff1a3078SAlan Somers 		default:
1549ff1a3078SAlan Somers 			error = EINVAL;
1550ff1a3078SAlan Somers 			goto err2;
1551ff1a3078SAlan Somers 		}
1552ff1a3078SAlan Somers 	} else
1553ff1a3078SAlan Somers 		opcode = job->uaiocb.aio_lio_opcode = type;
1554ff1a3078SAlan Somers 
15555652770dSJohn Baldwin 	ksiginfo_init(&job->ksi);
15564c0fb2cfSDavid Xu 
1557bfbbc4aaSJason Evans 	/* Save userspace address of the job info. */
15585652770dSJohn Baldwin 	job->ujob = ujob;
155911783b14SJohn Dyson 
1560a9d2f8d8SRobert Watson 	/*
1561a9d2f8d8SRobert Watson 	 * Validate the opcode and fetch the file object for the specified
1562a9d2f8d8SRobert Watson 	 * file descriptor.
1563a9d2f8d8SRobert Watson 	 *
1564a9d2f8d8SRobert Watson 	 * XXXRW: Moved the opcode validation up here so that we don't
1565a9d2f8d8SRobert Watson 	 * retrieve a file descriptor without knowing what the capabiltity
1566a9d2f8d8SRobert Watson 	 * should be.
1567a9d2f8d8SRobert Watson 	 */
15685652770dSJohn Baldwin 	fd = job->uaiocb.aio_fildes;
15692a522eb9SJohn Baldwin 	switch (opcode) {
15702a522eb9SJohn Baldwin 	case LIO_WRITE:
1571022ca2fcSAlan Somers 	case LIO_WRITEV:
1572cbd92ce6SMatt Macy 		error = fget_write(td, fd, &cap_pwrite_rights, &fp);
15732a522eb9SJohn Baldwin 		break;
15742a522eb9SJohn Baldwin 	case LIO_READ:
1575022ca2fcSAlan Somers 	case LIO_READV:
1576cbd92ce6SMatt Macy 		error = fget_read(td, fd, &cap_pread_rights, &fp);
1577a9d2f8d8SRobert Watson 		break;
1578a9d2f8d8SRobert Watson 	case LIO_SYNC:
1579801ac943SThomas Munro 	case LIO_DSYNC:
1580cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_fsync_rights, &fp);
1581a9d2f8d8SRobert Watson 		break;
15826160e12cSGleb Smirnoff 	case LIO_MLOCK:
15836160e12cSGleb Smirnoff 		break;
1584a9d2f8d8SRobert Watson 	case LIO_NOP:
1585cbd92ce6SMatt Macy 		error = fget(td, fd, &cap_no_rights, &fp);
15862a522eb9SJohn Baldwin 		break;
15872a522eb9SJohn Baldwin 	default:
1588a9d2f8d8SRobert Watson 		error = EINVAL;
15892a522eb9SJohn Baldwin 	}
1590022ca2fcSAlan Somers 	if (error)
1591022ca2fcSAlan Somers 		goto err3;
159299eee864SDavid Xu 
15932247f489SAlan Somers 	if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
159499eee864SDavid Xu 		error = EINVAL;
1595022ca2fcSAlan Somers 		goto err3;
159699eee864SDavid Xu 	}
15972244ea07SJohn Dyson 
1598022ca2fcSAlan Somers 	if ((opcode == LIO_READ || opcode == LIO_READV ||
1599022ca2fcSAlan Somers 	    opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1600711dba24SKonstantin Belousov 	    job->uaiocb.aio_offset < 0 &&
1601711dba24SKonstantin Belousov 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1602ae124fc4SAlan Cox 		error = EINVAL;
1603022ca2fcSAlan Somers 		goto err3;
16042244ea07SJohn Dyson 	}
16051ce91824SDavid Xu 
16068d9ed174SKonstantin Belousov 	if (fp != NULL && fp->f_ops == &path_fileops) {
16078d9ed174SKonstantin Belousov 		error = EBADF;
16088d9ed174SKonstantin Belousov 		goto err3;
16098d9ed174SKonstantin Belousov 	}
16108d9ed174SKonstantin Belousov 
16115652770dSJohn Baldwin 	job->fd_file = fp;
16121ce91824SDavid Xu 
161399eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
16145652770dSJohn Baldwin 	job->seqno = jobseqno++;
161599eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
16162244ea07SJohn Dyson 	if (opcode == LIO_NOP) {
1617a5c0b1c0SAlan Cox 		fdrop(fp, td);
1618022ca2fcSAlan Somers 		MPASS(job->uiop == &job->uio || job->uiop == NULL);
16195652770dSJohn Baldwin 		uma_zfree(aiocb_zone, job);
1620ac41f2efSAlfred Perlstein 		return (0);
16212244ea07SJohn Dyson 	}
16222244ea07SJohn Dyson 
16235652770dSJohn Baldwin 	if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1624cb679c38SJonathan Lemon 		goto no_kqueue;
16255652770dSJohn Baldwin 	evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1626fde80935SDavid Xu 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1627fde80935SDavid Xu 		error = EINVAL;
1628022ca2fcSAlan Somers 		goto err3;
1629fde80935SDavid Xu 	}
16305652770dSJohn Baldwin 	kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
163136c4960eSMark Johnston 	memset(&kev, 0, sizeof(kev));
16325652770dSJohn Baldwin 	kev.ident = (uintptr_t)job->ujob;
1633cb679c38SJonathan Lemon 	kev.filter = EVFILT_AIO;
1634fde80935SDavid Xu 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
16355652770dSJohn Baldwin 	kev.data = (intptr_t)job;
16365652770dSJohn Baldwin 	kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1637792843c3SMark Johnston 	error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1638f3215338SJohn Baldwin 	if (error)
1639022ca2fcSAlan Somers 		goto err3;
1640f3215338SJohn Baldwin 
1641cb679c38SJonathan Lemon no_kqueue:
1642cb679c38SJonathan Lemon 
16435652770dSJohn Baldwin 	ops->store_error(ujob, EINPROGRESS);
16445652770dSJohn Baldwin 	job->uaiocb._aiocb_private.error = EINPROGRESS;
16455652770dSJohn Baldwin 	job->userproc = p;
16465652770dSJohn Baldwin 	job->cred = crhold(td->td_ucred);
1647f3215338SJohn Baldwin 	job->jobflags = KAIOCB_QUEUEING;
16485652770dSJohn Baldwin 	job->lio = lj;
16492244ea07SJohn Dyson 
16502247f489SAlan Somers 	if (opcode & LIO_VECTORED) {
1651022ca2fcSAlan Somers 		/* Use the uio copied in by aio_copyin */
1652022ca2fcSAlan Somers 		MPASS(job->uiop != &job->uio && job->uiop != NULL);
16532247f489SAlan Somers 	} else {
1654022ca2fcSAlan Somers 		/* Setup the inline uio */
1655022ca2fcSAlan Somers 		job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1656022ca2fcSAlan Somers 		job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1657022ca2fcSAlan Somers 		job->uio.uio_iov = job->iov;
1658022ca2fcSAlan Somers 		job->uio.uio_iovcnt = 1;
1659022ca2fcSAlan Somers 		job->uio.uio_resid = job->uaiocb.aio_nbytes;
1660022ca2fcSAlan Somers 		job->uio.uio_segflg = UIO_USERSPACE;
1661022ca2fcSAlan Somers 		job->uiop = &job->uio;
1662022ca2fcSAlan Somers 	}
16632247f489SAlan Somers 	switch (opcode & (LIO_READ | LIO_WRITE)) {
1664022ca2fcSAlan Somers 	case LIO_READ:
1665022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_READ;
1666022ca2fcSAlan Somers 		break;
1667022ca2fcSAlan Somers 	case LIO_WRITE:
1668022ca2fcSAlan Somers 		job->uiop->uio_rw = UIO_WRITE;
1669022ca2fcSAlan Somers 		break;
1670022ca2fcSAlan Somers 	}
1671022ca2fcSAlan Somers 	job->uiop->uio_offset = job->uaiocb.aio_offset;
1672022ca2fcSAlan Somers 	job->uiop->uio_td = td;
1673022ca2fcSAlan Somers 
1674f3215338SJohn Baldwin 	if (opcode == LIO_MLOCK) {
1675f3215338SJohn Baldwin 		aio_schedule(job, aio_process_mlock);
1676f3215338SJohn Baldwin 		error = 0;
1677f3215338SJohn Baldwin 	} else if (fp->f_ops->fo_aio_queue == NULL)
1678f3215338SJohn Baldwin 		error = aio_queue_file(fp, job);
1679f3215338SJohn Baldwin 	else
1680f3215338SJohn Baldwin 		error = fo_aio_queue(fp, job);
1681f3215338SJohn Baldwin 	if (error)
168245c2c7c4SKonstantin Belousov 		goto err4;
1683f3215338SJohn Baldwin 
1684f3215338SJohn Baldwin 	AIO_LOCK(ki);
1685f3215338SJohn Baldwin 	job->jobflags &= ~KAIOCB_QUEUEING;
1686f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1687f3215338SJohn Baldwin 	ki->kaio_count++;
1688f3215338SJohn Baldwin 	if (lj)
1689f3215338SJohn Baldwin 		lj->lioj_count++;
1690f3215338SJohn Baldwin 	atomic_add_int(&num_queue_count, 1);
1691f3215338SJohn Baldwin 	if (job->jobflags & KAIOCB_FINISHED) {
1692f3215338SJohn Baldwin 		/*
1693f3215338SJohn Baldwin 		 * The queue callback completed the request synchronously.
1694f3215338SJohn Baldwin 		 * The bulk of the completion is deferred in that case
1695f3215338SJohn Baldwin 		 * until this point.
1696f3215338SJohn Baldwin 		 */
1697f3215338SJohn Baldwin 		aio_bio_done_notify(p, job);
1698f3215338SJohn Baldwin 	} else
1699f3215338SJohn Baldwin 		TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1700f3215338SJohn Baldwin 	AIO_UNLOCK(ki);
1701f3215338SJohn Baldwin 	return (0);
1702f3215338SJohn Baldwin 
170345c2c7c4SKonstantin Belousov err4:
170445c2c7c4SKonstantin Belousov 	crfree(job->cred);
1705022ca2fcSAlan Somers err3:
1706f3215338SJohn Baldwin 	if (fp)
1707f3215338SJohn Baldwin 		fdrop(fp, td);
1708022ca2fcSAlan Somers 	knlist_delete(&job->klist, curthread, 0);
1709022ca2fcSAlan Somers err2:
1710022ca2fcSAlan Somers 	if (job->uiop != &job->uio)
171161cc4830SAlfredo Mazzinghi 		freeuio(job->uiop);
1712f3215338SJohn Baldwin 	uma_zfree(aiocb_zone, job);
1713022ca2fcSAlan Somers err1:
1714f3215338SJohn Baldwin 	ops->store_error(ujob, error);
1715f3215338SJohn Baldwin 	return (error);
1716f3215338SJohn Baldwin }
1717f3215338SJohn Baldwin 
1718f3215338SJohn Baldwin static void
aio_cancel_daemon_job(struct kaiocb * job)1719f3215338SJohn Baldwin aio_cancel_daemon_job(struct kaiocb *job)
1720f3215338SJohn Baldwin {
1721f3215338SJohn Baldwin 
1722f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1723f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1724f3215338SJohn Baldwin 		TAILQ_REMOVE(&aio_jobs, job, list);
1725f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1726f3215338SJohn Baldwin 	aio_cancel(job);
1727f3215338SJohn Baldwin }
1728f3215338SJohn Baldwin 
1729f3215338SJohn Baldwin void
aio_schedule(struct kaiocb * job,aio_handle_fn_t * func)1730f3215338SJohn Baldwin aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1731f3215338SJohn Baldwin {
1732f3215338SJohn Baldwin 
1733f3215338SJohn Baldwin 	mtx_lock(&aio_job_mtx);
1734f3215338SJohn Baldwin 	if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1735f3215338SJohn Baldwin 		mtx_unlock(&aio_job_mtx);
1736f3215338SJohn Baldwin 		aio_cancel(job);
1737f3215338SJohn Baldwin 		return;
1738f3215338SJohn Baldwin 	}
1739f3215338SJohn Baldwin 	job->handle_fn = func;
1740f3215338SJohn Baldwin 	TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1741f3215338SJohn Baldwin 	aio_kick_nowait(job->userproc);
1742f3215338SJohn Baldwin 	mtx_unlock(&aio_job_mtx);
1743f3215338SJohn Baldwin }
1744f3215338SJohn Baldwin 
1745f3215338SJohn Baldwin static void
aio_cancel_sync(struct kaiocb * job)1746f3215338SJohn Baldwin aio_cancel_sync(struct kaiocb *job)
1747f3215338SJohn Baldwin {
1748f3215338SJohn Baldwin 	struct kaioinfo *ki;
1749f3215338SJohn Baldwin 
1750f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
1751005ce8e4SJohn Baldwin 	AIO_LOCK(ki);
1752f3215338SJohn Baldwin 	if (!aio_cancel_cleared(job))
1753f3215338SJohn Baldwin 		TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1754005ce8e4SJohn Baldwin 	AIO_UNLOCK(ki);
1755f3215338SJohn Baldwin 	aio_cancel(job);
1756f3215338SJohn Baldwin }
1757f3215338SJohn Baldwin 
1758f3215338SJohn Baldwin int
aio_queue_file(struct file * fp,struct kaiocb * job)1759f3215338SJohn Baldwin aio_queue_file(struct file *fp, struct kaiocb *job)
1760f3215338SJohn Baldwin {
1761f3215338SJohn Baldwin 	struct kaioinfo *ki;
1762f3215338SJohn Baldwin 	struct kaiocb *job2;
17639fe297bbSKonstantin Belousov 	struct vnode *vp;
17649fe297bbSKonstantin Belousov 	struct mount *mp;
1765f54c5606SJohn Baldwin 	int error;
17669fe297bbSKonstantin Belousov 	bool safe;
1767f3215338SJohn Baldwin 
1768f3215338SJohn Baldwin 	ki = job->userproc->p_aioinfo;
176972bce9ffSAlan Somers 	error = aio_qbio(job->userproc, job);
1770f54c5606SJohn Baldwin 	if (error >= 0)
1771f54c5606SJohn Baldwin 		return (error);
17729fe297bbSKonstantin Belousov 	safe = false;
17739fe297bbSKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE) {
17749fe297bbSKonstantin Belousov 		vp = fp->f_vnode;
17759fe297bbSKonstantin Belousov 		if (vp->v_type == VREG || vp->v_type == VDIR) {
17769fe297bbSKonstantin Belousov 			mp = fp->f_vnode->v_mount;
17779fe297bbSKonstantin Belousov 			if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
17789fe297bbSKonstantin Belousov 				safe = true;
17799fe297bbSKonstantin Belousov 		}
17809fe297bbSKonstantin Belousov 	}
17819c20dc99SJohn Baldwin 	if (!(safe || enable_aio_unsafe)) {
17829c20dc99SJohn Baldwin 		counted_warning(&unsafe_warningcnt,
17839c20dc99SJohn Baldwin 		    "is attempting to use unsafe AIO requests");
1784f3215338SJohn Baldwin 		return (EOPNOTSUPP);
17859c20dc99SJohn Baldwin 	}
178684af4da6SJohn Dyson 
17872247f489SAlan Somers 	if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
17887e409184SJohn Baldwin 		aio_schedule(job, aio_process_rw);
17897e409184SJohn Baldwin 		error = 0;
17902247f489SAlan Somers 	} else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1791f3215338SJohn Baldwin 		AIO_LOCK(ki);
17925652770dSJohn Baldwin 		TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
17935652770dSJohn Baldwin 			if (job2->fd_file == job->fd_file &&
17942247f489SAlan Somers 			    ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
17955652770dSJohn Baldwin 			    job2->seqno < job->seqno) {
17965652770dSJohn Baldwin 				job2->jobflags |= KAIOCB_CHECKSYNC;
17975652770dSJohn Baldwin 				job->pending++;
1798dbbccfe9SDavid Xu 			}
1799dbbccfe9SDavid Xu 		}
18005652770dSJohn Baldwin 		if (job->pending != 0) {
1801005ce8e4SJohn Baldwin 			if (!aio_set_cancel_function_locked(job,
1802005ce8e4SJohn Baldwin 				aio_cancel_sync)) {
1803f3215338SJohn Baldwin 				AIO_UNLOCK(ki);
1804f3215338SJohn Baldwin 				aio_cancel(job);
1805f3215338SJohn Baldwin 				return (0);
1806f3215338SJohn Baldwin 			}
18075652770dSJohn Baldwin 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1808759ccccaSDavid Xu 			AIO_UNLOCK(ki);
1809f3215338SJohn Baldwin 			return (0);
1810dbbccfe9SDavid Xu 		}
1811759ccccaSDavid Xu 		AIO_UNLOCK(ki);
1812f3215338SJohn Baldwin 		aio_schedule(job, aio_process_sync);
1813f3215338SJohn Baldwin 		error = 0;
18142247f489SAlan Somers 	} else {
1815f3215338SJohn Baldwin 		error = EINVAL;
1816f3215338SJohn Baldwin 	}
181799eee864SDavid Xu 	return (error);
181899eee864SDavid Xu }
181999eee864SDavid Xu 
182099eee864SDavid Xu static void
aio_kick_nowait(struct proc * userp)182199eee864SDavid Xu aio_kick_nowait(struct proc *userp)
182299eee864SDavid Xu {
182399eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
182439314b7dSJohn Baldwin 	struct aioproc *aiop;
182599eee864SDavid Xu 
182699eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
182799eee864SDavid Xu 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
182899eee864SDavid Xu 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
182939314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
183039314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18310dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
183286bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1833c85650caSJohn Baldwin 		taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
183499eee864SDavid Xu 	}
183599eee864SDavid Xu }
183699eee864SDavid Xu 
1837dbbccfe9SDavid Xu static int
aio_kick(struct proc * userp)183899eee864SDavid Xu aio_kick(struct proc *userp)
183999eee864SDavid Xu {
184099eee864SDavid Xu 	struct kaioinfo *ki = userp->p_aioinfo;
184139314b7dSJohn Baldwin 	struct aioproc *aiop;
1842dbbccfe9SDavid Xu 	int error, ret = 0;
184399eee864SDavid Xu 
184499eee864SDavid Xu 	mtx_assert(&aio_job_mtx, MA_OWNED);
184599eee864SDavid Xu retryproc:
1846d254af07SMatthew Dillon 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
18472244ea07SJohn Dyson 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
184839314b7dSJohn Baldwin 		aiop->aioprocflags &= ~AIOP_FREE;
184939314b7dSJohn Baldwin 		wakeup(aiop->aioproc);
18500dd6c035SJohn Baldwin 	} else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
185186bbef43SJohn Baldwin 	    ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1852fd3bf775SJohn Dyson 		num_aio_resv_start++;
18531ce91824SDavid Xu 		mtx_unlock(&aio_job_mtx);
18541ce91824SDavid Xu 		error = aio_newproc(&num_aio_resv_start);
18551ce91824SDavid Xu 		mtx_lock(&aio_job_mtx);
18561ce91824SDavid Xu 		if (error) {
185784af4da6SJohn Dyson 			num_aio_resv_start--;
18582244ea07SJohn Dyson 			goto retryproc;
1859fd3bf775SJohn Dyson 		}
1860dbbccfe9SDavid Xu 	} else {
1861dbbccfe9SDavid Xu 		ret = -1;
18621ce91824SDavid Xu 	}
1863dbbccfe9SDavid Xu 	return (ret);
186499eee864SDavid Xu }
18651ce91824SDavid Xu 
186699eee864SDavid Xu static void
aio_kick_helper(void * context,int pending)186799eee864SDavid Xu aio_kick_helper(void *context, int pending)
186899eee864SDavid Xu {
186999eee864SDavid Xu 	struct proc *userp = context;
187099eee864SDavid Xu 
187199eee864SDavid Xu 	mtx_lock(&aio_job_mtx);
1872dbbccfe9SDavid Xu 	while (--pending >= 0) {
1873dbbccfe9SDavid Xu 		if (aio_kick(userp))
1874dbbccfe9SDavid Xu 			break;
1875dbbccfe9SDavid Xu 	}
187699eee864SDavid Xu 	mtx_unlock(&aio_job_mtx);
18772244ea07SJohn Dyson }
18782244ea07SJohn Dyson 
1879fd3bf775SJohn Dyson /*
1880bfbbc4aaSJason Evans  * Support the aio_return system call, as a side-effect, kernel resources are
1881bfbbc4aaSJason Evans  * released.
18822244ea07SJohn Dyson  */
18833858a1f4SJohn Baldwin static int
kern_aio_return(struct thread * td,struct aiocb * ujob,struct aiocb_ops * ops)18845652770dSJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1885fd3bf775SJohn Dyson {
1886b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
18875652770dSJohn Baldwin 	struct kaiocb *job;
18882244ea07SJohn Dyson 	struct kaioinfo *ki;
1889bb430bc7SJohn Baldwin 	long status, error;
18902244ea07SJohn Dyson 
1891c0bf5caaSAlan Cox 	ki = p->p_aioinfo;
1892c0bf5caaSAlan Cox 	if (ki == NULL)
1893ac41f2efSAlfred Perlstein 		return (EINVAL);
1894759ccccaSDavid Xu 	AIO_LOCK(ki);
18955652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_done, plist) {
18965652770dSJohn Baldwin 		if (job->ujob == ujob)
1897c0bf5caaSAlan Cox 			break;
1898c0bf5caaSAlan Cox 	}
18995652770dSJohn Baldwin 	if (job != NULL) {
1900f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
19015652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
19025652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
19031ce91824SDavid Xu 		td->td_retval[0] = status;
1904b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
1905b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
1906b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
1907b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
19085652770dSJohn Baldwin 		aio_free_entry(job);
1909759ccccaSDavid Xu 		AIO_UNLOCK(ki);
19105652770dSJohn Baldwin 		ops->store_error(ujob, error);
19115652770dSJohn Baldwin 		ops->store_status(ujob, status);
191255a122bfSDavid Xu 	} else {
19131ce91824SDavid Xu 		error = EINVAL;
1914759ccccaSDavid Xu 		AIO_UNLOCK(ki);
191555a122bfSDavid Xu 	}
19161ce91824SDavid Xu 	return (error);
19172244ea07SJohn Dyson }
19182244ea07SJohn Dyson 
19193858a1f4SJohn Baldwin int
sys_aio_return(struct thread * td,struct aio_return_args * uap)19208451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap)
19213858a1f4SJohn Baldwin {
19223858a1f4SJohn Baldwin 
19233858a1f4SJohn Baldwin 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
19243858a1f4SJohn Baldwin }
19253858a1f4SJohn Baldwin 
19262244ea07SJohn Dyson /*
1927bfbbc4aaSJason Evans  * Allow a process to wakeup when any of the I/O requests are completed.
19282244ea07SJohn Dyson  */
19293858a1f4SJohn Baldwin static int
kern_aio_suspend(struct thread * td,int njoblist,struct aiocb ** ujoblist,struct timespec * ts)19303858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
19313858a1f4SJohn Baldwin     struct timespec *ts)
1932fd3bf775SJohn Dyson {
1933b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
19344a11ca4eSPoul-Henning Kamp 	struct timeval atv;
19352244ea07SJohn Dyson 	struct kaioinfo *ki;
19365652770dSJohn Baldwin 	struct kaiocb *firstjob, *job;
19373858a1f4SJohn Baldwin 	int error, i, timo;
19382244ea07SJohn Dyson 
19392244ea07SJohn Dyson 	timo = 0;
19403858a1f4SJohn Baldwin 	if (ts) {
19413858a1f4SJohn Baldwin 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
19422244ea07SJohn Dyson 			return (EINVAL);
19432244ea07SJohn Dyson 
19443858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
19452244ea07SJohn Dyson 		if (itimerfix(&atv))
19462244ea07SJohn Dyson 			return (EINVAL);
1947227ee8a1SPoul-Henning Kamp 		timo = tvtohz(&atv);
19482244ea07SJohn Dyson 	}
19492244ea07SJohn Dyson 
19502244ea07SJohn Dyson 	ki = p->p_aioinfo;
19512244ea07SJohn Dyson 	if (ki == NULL)
1952ac41f2efSAlfred Perlstein 		return (EAGAIN);
19532244ea07SJohn Dyson 
19543858a1f4SJohn Baldwin 	if (njoblist == 0)
1955ac41f2efSAlfred Perlstein 		return (0);
19562244ea07SJohn Dyson 
1957759ccccaSDavid Xu 	AIO_LOCK(ki);
19581ce91824SDavid Xu 	for (;;) {
19595652770dSJohn Baldwin 		firstjob = NULL;
19601ce91824SDavid Xu 		error = 0;
19615652770dSJohn Baldwin 		TAILQ_FOREACH(job, &ki->kaio_all, allist) {
196284af4da6SJohn Dyson 			for (i = 0; i < njoblist; i++) {
19635652770dSJohn Baldwin 				if (job->ujob == ujoblist[i]) {
19645652770dSJohn Baldwin 					if (firstjob == NULL)
19655652770dSJohn Baldwin 						firstjob = job;
1966f3215338SJohn Baldwin 					if (job->jobflags & KAIOCB_FINISHED)
19671ce91824SDavid Xu 						goto RETURN;
196884af4da6SJohn Dyson 				}
196984af4da6SJohn Dyson 			}
197084af4da6SJohn Dyson 		}
19711ce91824SDavid Xu 		/* All tasks were finished. */
19725652770dSJohn Baldwin 		if (firstjob == NULL)
19731ce91824SDavid Xu 			break;
19742244ea07SJohn Dyson 
1975fd3bf775SJohn Dyson 		ki->kaio_flags |= KAIO_WAKEUP;
1976759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
19771ce91824SDavid Xu 		    "aiospn", timo);
19781ce91824SDavid Xu 		if (error == ERESTART)
19791ce91824SDavid Xu 			error = EINTR;
19801ce91824SDavid Xu 		if (error)
19811ce91824SDavid Xu 			break;
19822244ea07SJohn Dyson 	}
19831ce91824SDavid Xu RETURN:
1984759ccccaSDavid Xu 	AIO_UNLOCK(ki);
19853858a1f4SJohn Baldwin 	return (error);
19863858a1f4SJohn Baldwin }
19873858a1f4SJohn Baldwin 
19883858a1f4SJohn Baldwin int
sys_aio_suspend(struct thread * td,struct aio_suspend_args * uap)19898451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
19903858a1f4SJohn Baldwin {
19913858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
19923858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
19933858a1f4SJohn Baldwin 	int error;
19943858a1f4SJohn Baldwin 
1995913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
19963858a1f4SJohn Baldwin 		return (EINVAL);
19973858a1f4SJohn Baldwin 
19983858a1f4SJohn Baldwin 	if (uap->timeout) {
19993858a1f4SJohn Baldwin 		/* Get timespec struct. */
20003858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
20013858a1f4SJohn Baldwin 			return (error);
20023858a1f4SJohn Baldwin 		tsp = &ts;
20033858a1f4SJohn Baldwin 	} else
20043858a1f4SJohn Baldwin 		tsp = NULL;
20053858a1f4SJohn Baldwin 
20069553bc89SMark Johnston 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
20073858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
20083858a1f4SJohn Baldwin 	if (error == 0)
20093858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
20109553bc89SMark Johnston 	free(ujoblist, M_AIO);
20111ce91824SDavid Xu 	return (error);
20122244ea07SJohn Dyson }
2013ee877a35SJohn Dyson 
2014ee877a35SJohn Dyson /*
201572bce9ffSAlan Somers  * aio_cancel cancels any non-bio aio operations not currently in progress.
2016ee877a35SJohn Dyson  */
2017ee877a35SJohn Dyson int
sys_aio_cancel(struct thread * td,struct aio_cancel_args * uap)20188451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2019fd3bf775SJohn Dyson {
2020b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2021dd85920aSJason Evans 	struct kaioinfo *ki;
20225652770dSJohn Baldwin 	struct kaiocb *job, *jobn;
2023dd85920aSJason Evans 	struct file *fp;
20241ce91824SDavid Xu 	int error;
2025dd85920aSJason Evans 	int cancelled = 0;
2026dd85920aSJason Evans 	int notcancelled = 0;
2027dd85920aSJason Evans 	struct vnode *vp;
2028dd85920aSJason Evans 
20292a522eb9SJohn Baldwin 	/* Lookup file object. */
2030cbd92ce6SMatt Macy 	error = fget(td, uap->fd, &cap_no_rights, &fp);
20312a522eb9SJohn Baldwin 	if (error)
20322a522eb9SJohn Baldwin 		return (error);
2033dd85920aSJason Evans 
20341ce91824SDavid Xu 	ki = p->p_aioinfo;
20351ce91824SDavid Xu 	if (ki == NULL)
20361ce91824SDavid Xu 		goto done;
20371ce91824SDavid Xu 
2038dd85920aSJason Evans 	if (fp->f_type == DTYPE_VNODE) {
20393b6d9652SPoul-Henning Kamp 		vp = fp->f_vnode;
20407ad2a82dSMateusz Guzik 		if (vn_isdisk(vp)) {
20412a522eb9SJohn Baldwin 			fdrop(fp, td);
2042b40ce416SJulian Elischer 			td->td_retval[0] = AIO_NOTCANCELED;
2043ac41f2efSAlfred Perlstein 			return (0);
2044dd85920aSJason Evans 		}
2045dd85920aSJason Evans 	}
2046dd85920aSJason Evans 
2047759ccccaSDavid Xu 	AIO_LOCK(ki);
20485652770dSJohn Baldwin 	TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
20495652770dSJohn Baldwin 		if ((uap->fd == job->uaiocb.aio_fildes) &&
2050dd85920aSJason Evans 		    ((uap->aiocbp == NULL) ||
20515652770dSJohn Baldwin 		     (uap->aiocbp == job->ujob))) {
2052f3215338SJohn Baldwin 			if (aio_cancel_job(p, ki, job)) {
20531ce91824SDavid Xu 				cancelled++;
2054dd85920aSJason Evans 			} else {
2055dd85920aSJason Evans 				notcancelled++;
2056dd85920aSJason Evans 			}
20571aa4c324SDavid Xu 			if (uap->aiocbp != NULL)
20581aa4c324SDavid Xu 				break;
2059dd85920aSJason Evans 		}
2060dd85920aSJason Evans 	}
2061759ccccaSDavid Xu 	AIO_UNLOCK(ki);
20621ce91824SDavid Xu 
2063ad49abc0SAlan Cox done:
20642a522eb9SJohn Baldwin 	fdrop(fp, td);
20651aa4c324SDavid Xu 
20661aa4c324SDavid Xu 	if (uap->aiocbp != NULL) {
2067dd85920aSJason Evans 		if (cancelled) {
2068b40ce416SJulian Elischer 			td->td_retval[0] = AIO_CANCELED;
2069ac41f2efSAlfred Perlstein 			return (0);
2070dd85920aSJason Evans 		}
20711aa4c324SDavid Xu 	}
20721aa4c324SDavid Xu 
20731aa4c324SDavid Xu 	if (notcancelled) {
20741aa4c324SDavid Xu 		td->td_retval[0] = AIO_NOTCANCELED;
20751aa4c324SDavid Xu 		return (0);
20761aa4c324SDavid Xu 	}
20771aa4c324SDavid Xu 
20781aa4c324SDavid Xu 	if (cancelled) {
20791aa4c324SDavid Xu 		td->td_retval[0] = AIO_CANCELED;
20801aa4c324SDavid Xu 		return (0);
20811aa4c324SDavid Xu 	}
20821aa4c324SDavid Xu 
2083b40ce416SJulian Elischer 	td->td_retval[0] = AIO_ALLDONE;
2084dd85920aSJason Evans 
2085ac41f2efSAlfred Perlstein 	return (0);
2086ee877a35SJohn Dyson }
2087ee877a35SJohn Dyson 
2088ee877a35SJohn Dyson /*
2089873fbcd7SRobert Watson  * aio_error is implemented in the kernel level for compatibility purposes
2090873fbcd7SRobert Watson  * only.  For a user mode async implementation, it would be best to do it in
2091873fbcd7SRobert Watson  * a userland subroutine.
2092ee877a35SJohn Dyson  */
20933858a1f4SJohn Baldwin static int
kern_aio_error(struct thread * td,struct aiocb * ujob,struct aiocb_ops * ops)20945652770dSJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2095fd3bf775SJohn Dyson {
2096b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
20975652770dSJohn Baldwin 	struct kaiocb *job;
20982244ea07SJohn Dyson 	struct kaioinfo *ki;
20991ce91824SDavid Xu 	int status;
2100ee877a35SJohn Dyson 
21012244ea07SJohn Dyson 	ki = p->p_aioinfo;
21021ce91824SDavid Xu 	if (ki == NULL) {
21031ce91824SDavid Xu 		td->td_retval[0] = EINVAL;
21041ce91824SDavid Xu 		return (0);
21051ce91824SDavid Xu 	}
2106ee877a35SJohn Dyson 
2107759ccccaSDavid Xu 	AIO_LOCK(ki);
21085652770dSJohn Baldwin 	TAILQ_FOREACH(job, &ki->kaio_all, allist) {
21095652770dSJohn Baldwin 		if (job->ujob == ujob) {
2110f3215338SJohn Baldwin 			if (job->jobflags & KAIOCB_FINISHED)
21111ce91824SDavid Xu 				td->td_retval[0] =
21125652770dSJohn Baldwin 					job->uaiocb._aiocb_private.error;
21131ce91824SDavid Xu 			else
2114b40ce416SJulian Elischer 				td->td_retval[0] = EINPROGRESS;
2115759ccccaSDavid Xu 			AIO_UNLOCK(ki);
2116ac41f2efSAlfred Perlstein 			return (0);
21172244ea07SJohn Dyson 		}
21182244ea07SJohn Dyson 	}
2119759ccccaSDavid Xu 	AIO_UNLOCK(ki);
212084af4da6SJohn Dyson 
21212244ea07SJohn Dyson 	/*
2122a9bf5e37SDavid Xu 	 * Hack for failure of aio_aqueue.
21232244ea07SJohn Dyson 	 */
21245652770dSJohn Baldwin 	status = ops->fetch_status(ujob);
21251ce91824SDavid Xu 	if (status == -1) {
21265652770dSJohn Baldwin 		td->td_retval[0] = ops->fetch_error(ujob);
21271ce91824SDavid Xu 		return (0);
21281ce91824SDavid Xu 	}
21291ce91824SDavid Xu 
21301ce91824SDavid Xu 	td->td_retval[0] = EINVAL;
21311ce91824SDavid Xu 	return (0);
2132ee877a35SJohn Dyson }
2133ee877a35SJohn Dyson 
21343858a1f4SJohn Baldwin int
sys_aio_error(struct thread * td,struct aio_error_args * uap)21358451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap)
21363858a1f4SJohn Baldwin {
21373858a1f4SJohn Baldwin 
21383858a1f4SJohn Baldwin 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
21393858a1f4SJohn Baldwin }
21403858a1f4SJohn Baldwin 
2141eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */
2142399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2143ee877a35SJohn Dyson int
freebsd6_aio_read(struct thread * td,struct freebsd6_aio_read_args * uap)2144399e8c17SJohn Baldwin freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
21450972628aSDavid Xu {
21460972628aSDavid Xu 
21473858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
21483858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
21490972628aSDavid Xu }
2150399e8c17SJohn Baldwin #endif
21510972628aSDavid Xu 
21520972628aSDavid Xu int
sys_aio_read(struct thread * td,struct aio_read_args * uap)21538451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap)
2154fd3bf775SJohn Dyson {
215521d56e9cSAlfred Perlstein 
21563858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2157ee877a35SJohn Dyson }
2158ee877a35SJohn Dyson 
2159022ca2fcSAlan Somers int
sys_aio_readv(struct thread * td,struct aio_readv_args * uap)2160022ca2fcSAlan Somers sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2161022ca2fcSAlan Somers {
2162022ca2fcSAlan Somers 
2163022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2164022ca2fcSAlan Somers }
2165022ca2fcSAlan Somers 
2166eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */
2167399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
2168ee877a35SJohn Dyson int
freebsd6_aio_write(struct thread * td,struct freebsd6_aio_write_args * uap)2169399e8c17SJohn Baldwin freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
21700972628aSDavid Xu {
21710972628aSDavid Xu 
21723858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
21733858a1f4SJohn Baldwin 	    &aiocb_ops_osigevent));
21740972628aSDavid Xu }
2175399e8c17SJohn Baldwin #endif
21760972628aSDavid Xu 
21770972628aSDavid Xu int
sys_aio_write(struct thread * td,struct aio_write_args * uap)21788451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap)
2179fd3bf775SJohn Dyson {
218021d56e9cSAlfred Perlstein 
21813858a1f4SJohn Baldwin 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
21820972628aSDavid Xu }
21830972628aSDavid Xu 
21846160e12cSGleb Smirnoff int
sys_aio_writev(struct thread * td,struct aio_writev_args * uap)2185022ca2fcSAlan Somers sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2186022ca2fcSAlan Somers {
2187022ca2fcSAlan Somers 
2188022ca2fcSAlan Somers 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2189022ca2fcSAlan Somers }
2190022ca2fcSAlan Somers 
2191022ca2fcSAlan Somers int
sys_aio_mlock(struct thread * td,struct aio_mlock_args * uap)21926160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
21936160e12cSGleb Smirnoff {
21946160e12cSGleb Smirnoff 
21956160e12cSGleb Smirnoff 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
21966160e12cSGleb Smirnoff }
21976160e12cSGleb Smirnoff 
21980972628aSDavid Xu static int
kern_lio_listio(struct thread * td,int mode,struct aiocb * const * uacb_list,struct aiocb ** acb_list,int nent,struct sigevent * sig,struct aiocb_ops * ops)21993858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
22003858a1f4SJohn Baldwin     struct aiocb **acb_list, int nent, struct sigevent *sig,
22013858a1f4SJohn Baldwin     struct aiocb_ops *ops)
22020972628aSDavid Xu {
2203b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22045652770dSJohn Baldwin 	struct aiocb *job;
22052244ea07SJohn Dyson 	struct kaioinfo *ki;
22061ce91824SDavid Xu 	struct aioliojob *lj;
220769cd28daSDoug Ambrisko 	struct kevent kev;
22081ce91824SDavid Xu 	int error;
220952c09831SAlan Somers 	int nagain, nerror;
2210ee877a35SJohn Dyson 	int i;
2211ee877a35SJohn Dyson 
22123858a1f4SJohn Baldwin 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2213ac41f2efSAlfred Perlstein 		return (EINVAL);
22142244ea07SJohn Dyson 
2215913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
2216ac41f2efSAlfred Perlstein 		return (EINVAL);
22172244ea07SJohn Dyson 
2218bfbbc4aaSJason Evans 	if (p->p_aioinfo == NULL)
22192244ea07SJohn Dyson 		aio_init_aioinfo(p);
22202244ea07SJohn Dyson 
22212244ea07SJohn Dyson 	ki = p->p_aioinfo;
22222244ea07SJohn Dyson 
2223a163d034SWarner Losh 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
222484af4da6SJohn Dyson 	lj->lioj_flags = 0;
22251ce91824SDavid Xu 	lj->lioj_count = 0;
22261ce91824SDavid Xu 	lj->lioj_finished_count = 0;
22272e5f6152SMark Johnston 	lj->lioj_signal.sigev_notify = SIGEV_NONE;
2228d8b0556cSKonstantin Belousov 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
22294c0fb2cfSDavid Xu 	ksiginfo_init(&lj->lioj_ksi);
223069cd28daSDoug Ambrisko 
223184af4da6SJohn Dyson 	/*
2232bfbbc4aaSJason Evans 	 * Setup signal.
223384af4da6SJohn Dyson 	 */
22343858a1f4SJohn Baldwin 	if (sig && (mode == LIO_NOWAIT)) {
22353858a1f4SJohn Baldwin 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
223669cd28daSDoug Ambrisko 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
223769cd28daSDoug Ambrisko 			/* Assume only new style KEVENT */
223836c4960eSMark Johnston 			memset(&kev, 0, sizeof(kev));
223969cd28daSDoug Ambrisko 			kev.filter = EVFILT_LIO;
224069cd28daSDoug Ambrisko 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
22413858a1f4SJohn Baldwin 			kev.ident = (uintptr_t)uacb_list; /* something unique */
224269cd28daSDoug Ambrisko 			kev.data = (intptr_t)lj;
22431ce91824SDavid Xu 			/* pass user defined sigval data */
22441ce91824SDavid Xu 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
22454db71d27SJohn-Mark Gurney 			error = kqfd_register(
2246792843c3SMark Johnston 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2247792843c3SMark Johnston 			    M_WAITOK);
224869cd28daSDoug Ambrisko 			if (error) {
224969cd28daSDoug Ambrisko 				uma_zfree(aiolio_zone, lj);
225069cd28daSDoug Ambrisko 				return (error);
225169cd28daSDoug Ambrisko 			}
22521ce91824SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
22531ce91824SDavid Xu 			;
225468d71118SDavid Xu 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
225568d71118SDavid Xu 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
225668d71118SDavid Xu 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
225769cd28daSDoug Ambrisko 					uma_zfree(aiolio_zone, lj);
225869cd28daSDoug Ambrisko 					return EINVAL;
225968d71118SDavid Xu 				}
226084af4da6SJohn Dyson 				lj->lioj_flags |= LIOJ_SIGNAL;
226168d71118SDavid Xu 		} else {
226268d71118SDavid Xu 			uma_zfree(aiolio_zone, lj);
226368d71118SDavid Xu 			return EINVAL;
22644d752b01SAlan Cox 		}
22651ce91824SDavid Xu 	}
226669cd28daSDoug Ambrisko 
2267759ccccaSDavid Xu 	AIO_LOCK(ki);
22682f3cf918SAlfred Perlstein 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
22692244ea07SJohn Dyson 	/*
22701ce91824SDavid Xu 	 * Add extra aiocb count to avoid the lio to be freed
22711ce91824SDavid Xu 	 * by other threads doing aio_waitcomplete or aio_return,
22721ce91824SDavid Xu 	 * and prevent event from being sent until we have queued
22731ce91824SDavid Xu 	 * all tasks.
22741ce91824SDavid Xu 	 */
22751ce91824SDavid Xu 	lj->lioj_count = 1;
2276759ccccaSDavid Xu 	AIO_UNLOCK(ki);
22771ce91824SDavid Xu 
22781ce91824SDavid Xu 	/*
2279bfbbc4aaSJason Evans 	 * Get pointers to the list of I/O requests.
22802244ea07SJohn Dyson 	 */
228152c09831SAlan Somers 	nagain = 0;
2282fd3bf775SJohn Dyson 	nerror = 0;
22833858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++) {
22845652770dSJohn Baldwin 		job = acb_list[i];
22855652770dSJohn Baldwin 		if (job != NULL) {
22865652770dSJohn Baldwin 			error = aio_aqueue(td, job, lj, LIO_NOP, ops);
228752c09831SAlan Somers 			if (error == EAGAIN)
228852c09831SAlan Somers 				nagain++;
228952c09831SAlan Somers 			else if (error != 0)
2290fd3bf775SJohn Dyson 				nerror++;
2291fd3bf775SJohn Dyson 		}
2292fd3bf775SJohn Dyson 	}
22932244ea07SJohn Dyson 
22941ce91824SDavid Xu 	error = 0;
2295759ccccaSDavid Xu 	AIO_LOCK(ki);
22963858a1f4SJohn Baldwin 	if (mode == LIO_WAIT) {
22971ce91824SDavid Xu 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2298fd3bf775SJohn Dyson 			ki->kaio_flags |= KAIO_WAKEUP;
2299759ccccaSDavid Xu 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
23001ce91824SDavid Xu 			    PRIBIO | PCATCH, "aiospn", 0);
23011ce91824SDavid Xu 			if (error == ERESTART)
23021ce91824SDavid Xu 				error = EINTR;
23031ce91824SDavid Xu 			if (error)
23041ce91824SDavid Xu 				break;
23051ce91824SDavid Xu 		}
23061ce91824SDavid Xu 	} else {
23071ce91824SDavid Xu 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
23081ce91824SDavid Xu 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
23091ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
23101ce91824SDavid Xu 				KNOTE_LOCKED(&lj->klist, 1);
23111ce91824SDavid Xu 			}
231229331656SKonstantin Belousov 			if ((lj->lioj_flags & (LIOJ_SIGNAL |
231329331656SKonstantin Belousov 			    LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
231429331656SKonstantin Belousov 			    (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
23151ce91824SDavid Xu 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
23166814c2daSKonstantin Belousov 				aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
23176814c2daSKonstantin Belousov 				    lj->lioj_count != 1);
23181ce91824SDavid Xu 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
23192244ea07SJohn Dyson 			}
23202244ea07SJohn Dyson 		}
23211ce91824SDavid Xu 	}
23221ce91824SDavid Xu 	lj->lioj_count--;
23231ce91824SDavid Xu 	if (lj->lioj_count == 0) {
23241ce91824SDavid Xu 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
23251ce91824SDavid Xu 		knlist_delete(&lj->klist, curthread, 1);
2326759ccccaSDavid Xu 		PROC_LOCK(p);
23271ce91824SDavid Xu 		sigqueue_take(&lj->lioj_ksi);
23281ce91824SDavid Xu 		PROC_UNLOCK(p);
2329759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23301ce91824SDavid Xu 		uma_zfree(aiolio_zone, lj);
23311ce91824SDavid Xu 	} else
2332759ccccaSDavid Xu 		AIO_UNLOCK(ki);
23332244ea07SJohn Dyson 
23341ce91824SDavid Xu 	if (nerror)
23351ce91824SDavid Xu 		return (EIO);
233652c09831SAlan Somers 	else if (nagain)
233752c09831SAlan Somers 		return (EAGAIN);
233852c09831SAlan Somers 	else
23391ce91824SDavid Xu 		return (error);
2340ee877a35SJohn Dyson }
2341fd3bf775SJohn Dyson 
23423858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
2343399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
23443858a1f4SJohn Baldwin int
freebsd6_lio_listio(struct thread * td,struct freebsd6_lio_listio_args * uap)2345399e8c17SJohn Baldwin freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
23463858a1f4SJohn Baldwin {
23473858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23483858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23493858a1f4SJohn Baldwin 	struct osigevent osig;
23503858a1f4SJohn Baldwin 	int error, nent;
23513858a1f4SJohn Baldwin 
23523858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23533858a1f4SJohn Baldwin 		return (EINVAL);
23543858a1f4SJohn Baldwin 
23553858a1f4SJohn Baldwin 	nent = uap->nent;
2356913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
23573858a1f4SJohn Baldwin 		return (EINVAL);
23583858a1f4SJohn Baldwin 
23593858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23603858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
23613858a1f4SJohn Baldwin 		if (error)
23623858a1f4SJohn Baldwin 			return (error);
23633858a1f4SJohn Baldwin 		error = convert_old_sigevent(&osig, &sig);
23643858a1f4SJohn Baldwin 		if (error)
23653858a1f4SJohn Baldwin 			return (error);
23663858a1f4SJohn Baldwin 		sigp = &sig;
23673858a1f4SJohn Baldwin 	} else
23683858a1f4SJohn Baldwin 		sigp = NULL;
23693858a1f4SJohn Baldwin 
23703858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
23713858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
23723858a1f4SJohn Baldwin 	if (error == 0)
23733858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode,
23743858a1f4SJohn Baldwin 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
23753858a1f4SJohn Baldwin 		    &aiocb_ops_osigevent);
23763858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
23773858a1f4SJohn Baldwin 	return (error);
23783858a1f4SJohn Baldwin }
2379399e8c17SJohn Baldwin #endif
23803858a1f4SJohn Baldwin 
23813858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */
23823858a1f4SJohn Baldwin int
sys_lio_listio(struct thread * td,struct lio_listio_args * uap)23838451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
23843858a1f4SJohn Baldwin {
23853858a1f4SJohn Baldwin 	struct aiocb **acb_list;
23863858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
23873858a1f4SJohn Baldwin 	int error, nent;
23883858a1f4SJohn Baldwin 
23893858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
23903858a1f4SJohn Baldwin 		return (EINVAL);
23913858a1f4SJohn Baldwin 
23923858a1f4SJohn Baldwin 	nent = uap->nent;
2393913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
23943858a1f4SJohn Baldwin 		return (EINVAL);
23953858a1f4SJohn Baldwin 
23963858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
23973858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig, sizeof(sig));
23983858a1f4SJohn Baldwin 		if (error)
23993858a1f4SJohn Baldwin 			return (error);
24003858a1f4SJohn Baldwin 		sigp = &sig;
24013858a1f4SJohn Baldwin 	} else
24023858a1f4SJohn Baldwin 		sigp = NULL;
24033858a1f4SJohn Baldwin 
24043858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
24053858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
24063858a1f4SJohn Baldwin 	if (error == 0)
24073858a1f4SJohn Baldwin 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
24083858a1f4SJohn Baldwin 		    nent, sigp, &aiocb_ops);
24093858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
24103858a1f4SJohn Baldwin 	return (error);
24113858a1f4SJohn Baldwin }
24123858a1f4SJohn Baldwin 
2413fd3bf775SJohn Dyson static void
aio_biocleanup(struct bio * bp)2414022ca2fcSAlan Somers aio_biocleanup(struct bio *bp)
2415fd3bf775SJohn Dyson {
24165652770dSJohn Baldwin 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
241727b8220dSDavid Xu 	struct kaioinfo *ki;
241801206038SAlan Somers 	struct buf *pbuf = (struct buf *)bp->bio_caller2;
24191ce91824SDavid Xu 
2420f743d981SAlexander Motin 	/* Release mapping into kernel space. */
242101206038SAlan Somers 	if (pbuf != NULL) {
242201206038SAlan Somers 		MPASS(pbuf->b_npages <= atop(maxphys) + 1);
242301206038SAlan Somers 		pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
242401206038SAlan Somers 		vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
242501206038SAlan Somers 		uma_zfree(pbuf_zone, pbuf);
2426f743d981SAlexander Motin 		atomic_subtract_int(&num_buf_aio, 1);
2427a9d4fe97SKonstantin Belousov 		ki = job->userproc->p_aioinfo;
2428f3215338SJohn Baldwin 		AIO_LOCK(ki);
2429f3215338SJohn Baldwin 		ki->kaio_buffer_count--;
2430f3215338SJohn Baldwin 		AIO_UNLOCK(ki);
2431cd853791SKonstantin Belousov 	} else {
243201206038SAlan Somers 		MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
243301206038SAlan Somers 		vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
243401206038SAlan Somers 		free(bp->bio_ma, M_TEMP);
24358091e52bSJohn Baldwin 		atomic_subtract_int(&num_unmapped_aio, 1);
2436cd853791SKonstantin Belousov 	}
2437f743d981SAlexander Motin 	g_destroy_bio(bp);
243884af4da6SJohn Dyson }
2439bfbbc4aaSJason Evans 
2440022ca2fcSAlan Somers static void
aio_biowakeup(struct bio * bp)2441022ca2fcSAlan Somers aio_biowakeup(struct bio *bp)
2442022ca2fcSAlan Somers {
2443022ca2fcSAlan Somers 	struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2444022ca2fcSAlan Somers 	size_t nbytes;
2445022ca2fcSAlan Somers 	long bcount = bp->bio_bcount;
2446022ca2fcSAlan Somers 	long resid = bp->bio_resid;
2447e9c7ec22SMateusz Guzik 	int opcode, nblks;
2448022ca2fcSAlan Somers 	int bio_error = bp->bio_error;
2449022ca2fcSAlan Somers 	uint16_t flags = bp->bio_flags;
2450022ca2fcSAlan Somers 
2451022ca2fcSAlan Somers 	opcode = job->uaiocb.aio_lio_opcode;
2452022ca2fcSAlan Somers 
2453022ca2fcSAlan Somers 	aio_biocleanup(bp);
2454022ca2fcSAlan Somers 
2455022ca2fcSAlan Somers 	nbytes = bcount - resid;
2456022ca2fcSAlan Somers 	atomic_add_acq_long(&job->nbytes, nbytes);
2457022ca2fcSAlan Somers 	nblks = btodb(nbytes);
2458cca6d616SJohn Baldwin 
2459022ca2fcSAlan Somers 	/*
2460022ca2fcSAlan Somers 	 * If multiple bios experienced an error, the job will reflect the
2461022ca2fcSAlan Somers 	 * error of whichever failed bio completed last.
2462022ca2fcSAlan Somers 	 */
2463022ca2fcSAlan Somers 	if (flags & BIO_ERROR)
246498844e99SJohn Baldwin 		atomic_store_int(&job->error, bio_error);
24652247f489SAlan Somers 	if (opcode & LIO_WRITE)
2466022ca2fcSAlan Somers 		atomic_add_int(&job->outblock, nblks);
2467022ca2fcSAlan Somers 	else
2468022ca2fcSAlan Somers 		atomic_add_int(&job->inblock, nblks);
2469022ca2fcSAlan Somers 
247098844e99SJohn Baldwin 	if (refcount_release(&job->nbio)) {
247198844e99SJohn Baldwin 		bio_error = atomic_load_int(&job->error);
247298844e99SJohn Baldwin 		if (bio_error != 0)
247398844e99SJohn Baldwin 			aio_complete(job, -1, bio_error);
2474022ca2fcSAlan Somers 		else
2475022ca2fcSAlan Somers 			aio_complete(job, atomic_load_long(&job->nbytes), 0);
2476022ca2fcSAlan Somers 	}
2477022ca2fcSAlan Somers }
2478022ca2fcSAlan Somers 
2479eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */
24803858a1f4SJohn Baldwin static int
kern_aio_waitcomplete(struct thread * td,struct aiocb ** ujobp,struct timespec * ts,struct aiocb_ops * ops)24815652770dSJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
24823858a1f4SJohn Baldwin     struct timespec *ts, struct aiocb_ops *ops)
2483bfbbc4aaSJason Evans {
2484b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2485bfbbc4aaSJason Evans 	struct timeval atv;
2486bfbbc4aaSJason Evans 	struct kaioinfo *ki;
24875652770dSJohn Baldwin 	struct kaiocb *job;
24885652770dSJohn Baldwin 	struct aiocb *ujob;
2489bb430bc7SJohn Baldwin 	long error, status;
2490bb430bc7SJohn Baldwin 	int timo;
2491bfbbc4aaSJason Evans 
24925652770dSJohn Baldwin 	ops->store_aiocb(ujobp, NULL);
2493dd85920aSJason Evans 
249438d68e2dSPawel Jakub Dawidek 	if (ts == NULL) {
2495bfbbc4aaSJason Evans 		timo = 0;
249638d68e2dSPawel Jakub Dawidek 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
249738d68e2dSPawel Jakub Dawidek 		timo = -1;
249838d68e2dSPawel Jakub Dawidek 	} else {
24993858a1f4SJohn Baldwin 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2500bfbbc4aaSJason Evans 			return (EINVAL);
2501bfbbc4aaSJason Evans 
25023858a1f4SJohn Baldwin 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2503bfbbc4aaSJason Evans 		if (itimerfix(&atv))
2504bfbbc4aaSJason Evans 			return (EINVAL);
2505bfbbc4aaSJason Evans 		timo = tvtohz(&atv);
2506bfbbc4aaSJason Evans 	}
2507bfbbc4aaSJason Evans 
25088213baf0SChristian S.J. Peron 	if (p->p_aioinfo == NULL)
2509323fe565SDavid Xu 		aio_init_aioinfo(p);
25108213baf0SChristian S.J. Peron 	ki = p->p_aioinfo;
2511bfbbc4aaSJason Evans 
25121ce91824SDavid Xu 	error = 0;
25135652770dSJohn Baldwin 	job = NULL;
2514759ccccaSDavid Xu 	AIO_LOCK(ki);
25155652770dSJohn Baldwin 	while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
251638d68e2dSPawel Jakub Dawidek 		if (timo == -1) {
251738d68e2dSPawel Jakub Dawidek 			error = EWOULDBLOCK;
251838d68e2dSPawel Jakub Dawidek 			break;
251938d68e2dSPawel Jakub Dawidek 		}
25201ce91824SDavid Xu 		ki->kaio_flags |= KAIO_WAKEUP;
2521759ccccaSDavid Xu 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
25221ce91824SDavid Xu 		    "aiowc", timo);
252327b8220dSDavid Xu 		if (timo && error == ERESTART)
25241ce91824SDavid Xu 			error = EINTR;
25251ce91824SDavid Xu 		if (error)
25261ce91824SDavid Xu 			break;
25271ce91824SDavid Xu 	}
25281ce91824SDavid Xu 
25295652770dSJohn Baldwin 	if (job != NULL) {
2530f3215338SJohn Baldwin 		MPASS(job->jobflags & KAIOCB_FINISHED);
25315652770dSJohn Baldwin 		ujob = job->ujob;
25325652770dSJohn Baldwin 		status = job->uaiocb._aiocb_private.status;
25335652770dSJohn Baldwin 		error = job->uaiocb._aiocb_private.error;
25341ce91824SDavid Xu 		td->td_retval[0] = status;
2535b1012d80SJohn Baldwin 		td->td_ru.ru_oublock += job->outblock;
2536b1012d80SJohn Baldwin 		td->td_ru.ru_inblock += job->inblock;
2537b1012d80SJohn Baldwin 		td->td_ru.ru_msgsnd += job->msgsnd;
2538b1012d80SJohn Baldwin 		td->td_ru.ru_msgrcv += job->msgrcv;
25395652770dSJohn Baldwin 		aio_free_entry(job);
2540759ccccaSDavid Xu 		AIO_UNLOCK(ki);
25415652770dSJohn Baldwin 		ops->store_aiocb(ujobp, ujob);
25425652770dSJohn Baldwin 		ops->store_error(ujob, error);
25435652770dSJohn Baldwin 		ops->store_status(ujob, status);
25441ce91824SDavid Xu 	} else
2545759ccccaSDavid Xu 		AIO_UNLOCK(ki);
2546bfbbc4aaSJason Evans 
2547ac41f2efSAlfred Perlstein 	return (error);
2548bfbbc4aaSJason Evans }
2549cb679c38SJonathan Lemon 
255099eee864SDavid Xu int
sys_aio_waitcomplete(struct thread * td,struct aio_waitcomplete_args * uap)25518451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
25523858a1f4SJohn Baldwin {
25533858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
25543858a1f4SJohn Baldwin 	int error;
25553858a1f4SJohn Baldwin 
25563858a1f4SJohn Baldwin 	if (uap->timeout) {
25573858a1f4SJohn Baldwin 		/* Get timespec struct. */
25583858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts, sizeof(ts));
25593858a1f4SJohn Baldwin 		if (error)
25603858a1f4SJohn Baldwin 			return (error);
25613858a1f4SJohn Baldwin 		tsp = &ts;
25623858a1f4SJohn Baldwin 	} else
25633858a1f4SJohn Baldwin 		tsp = NULL;
25643858a1f4SJohn Baldwin 
25653858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
25663858a1f4SJohn Baldwin }
25673858a1f4SJohn Baldwin 
25683858a1f4SJohn Baldwin static int
kern_aio_fsync(struct thread * td,int op,struct aiocb * ujob,struct aiocb_ops * ops)25695652770dSJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
25703858a1f4SJohn Baldwin     struct aiocb_ops *ops)
257199eee864SDavid Xu {
2572801ac943SThomas Munro 	int listop;
257399eee864SDavid Xu 
2574801ac943SThomas Munro 	switch (op) {
2575801ac943SThomas Munro 	case O_SYNC:
2576801ac943SThomas Munro 		listop = LIO_SYNC;
2577801ac943SThomas Munro 		break;
2578801ac943SThomas Munro 	case O_DSYNC:
2579801ac943SThomas Munro 		listop = LIO_DSYNC;
2580801ac943SThomas Munro 		break;
2581801ac943SThomas Munro 	default:
258299eee864SDavid Xu 		return (EINVAL);
2583801ac943SThomas Munro 	}
2584801ac943SThomas Munro 
2585801ac943SThomas Munro 	return (aio_aqueue(td, ujob, NULL, listop, ops));
25863858a1f4SJohn Baldwin }
25873858a1f4SJohn Baldwin 
25883858a1f4SJohn Baldwin int
sys_aio_fsync(struct thread * td,struct aio_fsync_args * uap)25898451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
25903858a1f4SJohn Baldwin {
25913858a1f4SJohn Baldwin 
25923858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
259399eee864SDavid Xu }
259499eee864SDavid Xu 
2595eb8e6d52SEivind Eklund /* kqueue attach function */
2596cb679c38SJonathan Lemon static int
filt_aioattach(struct knote * kn)2597cb679c38SJonathan Lemon filt_aioattach(struct knote *kn)
2598cb679c38SJonathan Lemon {
25992b34e843SKonstantin Belousov 	struct kaiocb *job;
26002b34e843SKonstantin Belousov 
26012b34e843SKonstantin Belousov 	job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2602cb679c38SJonathan Lemon 
2603cb679c38SJonathan Lemon 	/*
26045652770dSJohn Baldwin 	 * The job pointer must be validated before using it, so
2605cb679c38SJonathan Lemon 	 * registration is restricted to the kernel; the user cannot
2606cb679c38SJonathan Lemon 	 * set EV_FLAG1.
2607cb679c38SJonathan Lemon 	 */
2608cb679c38SJonathan Lemon 	if ((kn->kn_flags & EV_FLAG1) == 0)
2609cb679c38SJonathan Lemon 		return (EPERM);
26105652770dSJohn Baldwin 	kn->kn_ptr.p_aio = job;
2611cb679c38SJonathan Lemon 	kn->kn_flags &= ~EV_FLAG1;
2612cb679c38SJonathan Lemon 
26135652770dSJohn Baldwin 	knlist_add(&job->klist, kn, 0);
2614cb679c38SJonathan Lemon 
2615cb679c38SJonathan Lemon 	return (0);
2616cb679c38SJonathan Lemon }
2617cb679c38SJonathan Lemon 
2618eb8e6d52SEivind Eklund /* kqueue detach function */
2619cb679c38SJonathan Lemon static void
filt_aiodetach(struct knote * kn)2620cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn)
2621cb679c38SJonathan Lemon {
26228e9fc278SDoug Ambrisko 	struct knlist *knl;
2623cb679c38SJonathan Lemon 
26248e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_aio->klist;
26258e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26268e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26278e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26288e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
2629cb679c38SJonathan Lemon }
2630cb679c38SJonathan Lemon 
2631eb8e6d52SEivind Eklund /* kqueue filter function */
2632cb679c38SJonathan Lemon /*ARGSUSED*/
2633cb679c38SJonathan Lemon static int
filt_aio(struct knote * kn,long hint)2634cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint)
2635cb679c38SJonathan Lemon {
26365652770dSJohn Baldwin 	struct kaiocb *job = kn->kn_ptr.p_aio;
2637cb679c38SJonathan Lemon 
26385652770dSJohn Baldwin 	kn->kn_data = job->uaiocb._aiocb_private.error;
2639f3215338SJohn Baldwin 	if (!(job->jobflags & KAIOCB_FINISHED))
2640cb679c38SJonathan Lemon 		return (0);
2641cb679c38SJonathan Lemon 	kn->kn_flags |= EV_EOF;
2642cb679c38SJonathan Lemon 	return (1);
2643cb679c38SJonathan Lemon }
264469cd28daSDoug Ambrisko 
264569cd28daSDoug Ambrisko /* kqueue attach function */
264669cd28daSDoug Ambrisko static int
filt_lioattach(struct knote * kn)264769cd28daSDoug Ambrisko filt_lioattach(struct knote *kn)
264869cd28daSDoug Ambrisko {
26492b34e843SKonstantin Belousov 	struct aioliojob *lj;
26502b34e843SKonstantin Belousov 
26512b34e843SKonstantin Belousov 	lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
265269cd28daSDoug Ambrisko 
265369cd28daSDoug Ambrisko 	/*
26541ce91824SDavid Xu 	 * The aioliojob pointer must be validated before using it, so
265569cd28daSDoug Ambrisko 	 * registration is restricted to the kernel; the user cannot
265669cd28daSDoug Ambrisko 	 * set EV_FLAG1.
265769cd28daSDoug Ambrisko 	 */
265869cd28daSDoug Ambrisko 	if ((kn->kn_flags & EV_FLAG1) == 0)
265969cd28daSDoug Ambrisko 		return (EPERM);
2660a8afa221SJean-Sébastien Pédron 	kn->kn_ptr.p_lio = lj;
266169cd28daSDoug Ambrisko 	kn->kn_flags &= ~EV_FLAG1;
266269cd28daSDoug Ambrisko 
266369cd28daSDoug Ambrisko 	knlist_add(&lj->klist, kn, 0);
266469cd28daSDoug Ambrisko 
266569cd28daSDoug Ambrisko 	return (0);
266669cd28daSDoug Ambrisko }
266769cd28daSDoug Ambrisko 
266869cd28daSDoug Ambrisko /* kqueue detach function */
266969cd28daSDoug Ambrisko static void
filt_liodetach(struct knote * kn)267069cd28daSDoug Ambrisko filt_liodetach(struct knote *kn)
267169cd28daSDoug Ambrisko {
26728e9fc278SDoug Ambrisko 	struct knlist *knl;
267369cd28daSDoug Ambrisko 
26748e9fc278SDoug Ambrisko 	knl = &kn->kn_ptr.p_lio->klist;
26758e9fc278SDoug Ambrisko 	knl->kl_lock(knl->kl_lockarg);
26768e9fc278SDoug Ambrisko 	if (!knlist_empty(knl))
26778e9fc278SDoug Ambrisko 		knlist_remove(knl, kn, 1);
26788e9fc278SDoug Ambrisko 	knl->kl_unlock(knl->kl_lockarg);
267969cd28daSDoug Ambrisko }
268069cd28daSDoug Ambrisko 
268169cd28daSDoug Ambrisko /* kqueue filter function */
268269cd28daSDoug Ambrisko /*ARGSUSED*/
268369cd28daSDoug Ambrisko static int
filt_lio(struct knote * kn,long hint)268469cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint)
268569cd28daSDoug Ambrisko {
2686a8afa221SJean-Sébastien Pédron 	struct aioliojob * lj = kn->kn_ptr.p_lio;
26871ce91824SDavid Xu 
268869cd28daSDoug Ambrisko 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
268969cd28daSDoug Ambrisko }
26903858a1f4SJohn Baldwin 
2691841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2692399e8c17SJohn Baldwin #include <sys/mount.h>
2693399e8c17SJohn Baldwin #include <sys/socket.h>
269431d1b816SDmitry Chagin #include <sys/sysent.h>
2695399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32.h>
2696399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_proto.h>
2697399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2698399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_syscall.h>
2699399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_util.h>
27003858a1f4SJohn Baldwin 
27013858a1f4SJohn Baldwin struct __aiocb_private32 {
27023858a1f4SJohn Baldwin 	int32_t	status;
27033858a1f4SJohn Baldwin 	int32_t	error;
27044605a99bSAndrew Gallatin 	uint32_t spare;
27053858a1f4SJohn Baldwin };
27063858a1f4SJohn Baldwin 
2707399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27083858a1f4SJohn Baldwin typedef struct oaiocb32 {
27093858a1f4SJohn Baldwin 	int	aio_fildes;		/* File descriptor */
27103858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27113858a1f4SJohn Baldwin 	uint32_t aio_buf;		/* I/O buffer in process space */
27123858a1f4SJohn Baldwin 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
27133858a1f4SJohn Baldwin 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
27143858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27153858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27163858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27173858a1f4SJohn Baldwin } oaiocb32_t;
2718399e8c17SJohn Baldwin #endif
27193858a1f4SJohn Baldwin 
27203858a1f4SJohn Baldwin typedef struct aiocb32 {
27213858a1f4SJohn Baldwin 	int32_t	aio_fildes;		/* File descriptor */
27223858a1f4SJohn Baldwin 	uint64_t aio_offset __packed;	/* File offset for I/O */
27233858a1f4SJohn Baldwin 	uint32_t aio_buf;	/* I/O buffer in process space */
27243858a1f4SJohn Baldwin 	uint32_t aio_nbytes;	/* Number of bytes for I/O */
27253858a1f4SJohn Baldwin 	int	__spare__[2];
27263858a1f4SJohn Baldwin 	uint32_t __spare2__;
27273858a1f4SJohn Baldwin 	int	aio_lio_opcode;		/* LIO opcode */
27283858a1f4SJohn Baldwin 	int	aio_reqprio;		/* Request priority -- ignored */
27293858a1f4SJohn Baldwin 	struct	__aiocb_private32 _aiocb_private;
27303858a1f4SJohn Baldwin 	struct	sigevent32 aio_sigevent;	/* Signal to deliver */
27313858a1f4SJohn Baldwin } aiocb32_t;
27323858a1f4SJohn Baldwin 
2733399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
27343858a1f4SJohn Baldwin static int
convert_old_sigevent32(struct osigevent32 * osig,struct sigevent * nsig)27353858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
27363858a1f4SJohn Baldwin {
27373858a1f4SJohn Baldwin 
27383858a1f4SJohn Baldwin 	/*
27393858a1f4SJohn Baldwin 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
27403858a1f4SJohn Baldwin 	 * supported by AIO with the old sigevent structure.
27413858a1f4SJohn Baldwin 	 */
27423858a1f4SJohn Baldwin 	CP(*osig, *nsig, sigev_notify);
27433858a1f4SJohn Baldwin 	switch (nsig->sigev_notify) {
27443858a1f4SJohn Baldwin 	case SIGEV_NONE:
27453858a1f4SJohn Baldwin 		break;
27463858a1f4SJohn Baldwin 	case SIGEV_SIGNAL:
27473858a1f4SJohn Baldwin 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
27483858a1f4SJohn Baldwin 		break;
27493858a1f4SJohn Baldwin 	case SIGEV_KEVENT:
27503858a1f4SJohn Baldwin 		nsig->sigev_notify_kqueue =
27513858a1f4SJohn Baldwin 		    osig->__sigev_u.__sigev_notify_kqueue;
27523858a1f4SJohn Baldwin 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
27533858a1f4SJohn Baldwin 		break;
27543858a1f4SJohn Baldwin 	default:
27553858a1f4SJohn Baldwin 		return (EINVAL);
27563858a1f4SJohn Baldwin 	}
27573858a1f4SJohn Baldwin 	return (0);
27583858a1f4SJohn Baldwin }
27593858a1f4SJohn Baldwin 
27603858a1f4SJohn Baldwin static int
aiocb32_copyin_old_sigevent(struct aiocb * ujob,struct kaiocb * kjob,int type __unused)2761022ca2fcSAlan Somers aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2762022ca2fcSAlan Somers     int type __unused)
27633858a1f4SJohn Baldwin {
27643858a1f4SJohn Baldwin 	struct oaiocb32 job32;
2765022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
27663858a1f4SJohn Baldwin 	int error;
27673858a1f4SJohn Baldwin 
2768022ca2fcSAlan Somers 	bzero(kcb, sizeof(struct aiocb));
27693858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
27703858a1f4SJohn Baldwin 	if (error)
27713858a1f4SJohn Baldwin 		return (error);
27723858a1f4SJohn Baldwin 
2773022ca2fcSAlan Somers 	/* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2774022ca2fcSAlan Somers 
2775022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2776022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2777022ca2fcSAlan Somers 	PTRIN_CP(job32, *kcb, aio_buf);
2778022ca2fcSAlan Somers 	CP(job32, *kcb, aio_nbytes);
2779022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
2780022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2781022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2782022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
27833858a1f4SJohn Baldwin 	return (convert_old_sigevent32(&job32.aio_sigevent,
2784022ca2fcSAlan Somers 	    &kcb->aio_sigevent));
27853858a1f4SJohn Baldwin }
2786399e8c17SJohn Baldwin #endif
27873858a1f4SJohn Baldwin 
27883858a1f4SJohn Baldwin static int
aiocb32_copyin(struct aiocb * ujob,struct kaiocb * kjob,int type)2789022ca2fcSAlan Somers aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
27903858a1f4SJohn Baldwin {
27913858a1f4SJohn Baldwin 	struct aiocb32 job32;
2792022ca2fcSAlan Somers 	struct aiocb *kcb = &kjob->uaiocb;
2793022ca2fcSAlan Somers 	struct iovec32 *iov32;
27943858a1f4SJohn Baldwin 	int error;
27953858a1f4SJohn Baldwin 
27963858a1f4SJohn Baldwin 	error = copyin(ujob, &job32, sizeof(job32));
27973858a1f4SJohn Baldwin 	if (error)
27983858a1f4SJohn Baldwin 		return (error);
2799022ca2fcSAlan Somers 	CP(job32, *kcb, aio_fildes);
2800022ca2fcSAlan Somers 	CP(job32, *kcb, aio_offset);
2801022ca2fcSAlan Somers 	CP(job32, *kcb, aio_lio_opcode);
28022884918cSMark Johnston 	if (type == LIO_NOP)
28032884918cSMark Johnston 		type = kcb->aio_lio_opcode;
28042247f489SAlan Somers 	if (type & LIO_VECTORED) {
2805022ca2fcSAlan Somers 		iov32 = PTRIN(job32.aio_iov);
2806022ca2fcSAlan Somers 		CP(job32, *kcb, aio_iovcnt);
2807022ca2fcSAlan Somers 		/* malloc a uio and copy in the iovec */
2808022ca2fcSAlan Somers 		error = freebsd32_copyinuio(iov32,
2809022ca2fcSAlan Somers 		    kcb->aio_iovcnt, &kjob->uiop);
2810022ca2fcSAlan Somers 		if (error)
2811022ca2fcSAlan Somers 			return (error);
2812022ca2fcSAlan Somers 	} else {
2813022ca2fcSAlan Somers 		PTRIN_CP(job32, *kcb, aio_buf);
2814022ca2fcSAlan Somers 		CP(job32, *kcb, aio_nbytes);
2815022ca2fcSAlan Somers 	}
2816022ca2fcSAlan Somers 	CP(job32, *kcb, aio_reqprio);
2817022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.status);
2818022ca2fcSAlan Somers 	CP(job32, *kcb, _aiocb_private.error);
2819022ca2fcSAlan Somers 	error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2820022ca2fcSAlan Somers 
2821022ca2fcSAlan Somers 	return (error);
28223858a1f4SJohn Baldwin }
28233858a1f4SJohn Baldwin 
28243858a1f4SJohn Baldwin static long
aiocb32_fetch_status(struct aiocb * ujob)28253858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob)
28263858a1f4SJohn Baldwin {
28273858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28283858a1f4SJohn Baldwin 
28293858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28303858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.status));
28313858a1f4SJohn Baldwin }
28323858a1f4SJohn Baldwin 
28333858a1f4SJohn Baldwin static long
aiocb32_fetch_error(struct aiocb * ujob)28343858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob)
28353858a1f4SJohn Baldwin {
28363858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28373858a1f4SJohn Baldwin 
28383858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28393858a1f4SJohn Baldwin 	return (fuword32(&ujob32->_aiocb_private.error));
28403858a1f4SJohn Baldwin }
28413858a1f4SJohn Baldwin 
28423858a1f4SJohn Baldwin static int
aiocb32_store_status(struct aiocb * ujob,long status)28433858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status)
28443858a1f4SJohn Baldwin {
28453858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28463858a1f4SJohn Baldwin 
28473858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28483858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.status, status));
28493858a1f4SJohn Baldwin }
28503858a1f4SJohn Baldwin 
28513858a1f4SJohn Baldwin static int
aiocb32_store_error(struct aiocb * ujob,long error)28523858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error)
28533858a1f4SJohn Baldwin {
28543858a1f4SJohn Baldwin 	struct aiocb32 *ujob32;
28553858a1f4SJohn Baldwin 
28563858a1f4SJohn Baldwin 	ujob32 = (struct aiocb32 *)ujob;
28573858a1f4SJohn Baldwin 	return (suword32(&ujob32->_aiocb_private.error, error));
28583858a1f4SJohn Baldwin }
28593858a1f4SJohn Baldwin 
28603858a1f4SJohn Baldwin static int
aiocb32_store_aiocb(struct aiocb ** ujobp,struct aiocb * ujob)28613858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
28623858a1f4SJohn Baldwin {
28633858a1f4SJohn Baldwin 
28643858a1f4SJohn Baldwin 	return (suword32(ujobp, (long)ujob));
28653858a1f4SJohn Baldwin }
28663858a1f4SJohn Baldwin 
28673858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = {
2868849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin,
28693858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
28703858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
28713858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
28723858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
28733858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
28743858a1f4SJohn Baldwin };
28753858a1f4SJohn Baldwin 
2876399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
28773858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = {
2878849aef49SAndrew Turner 	.aio_copyin = aiocb32_copyin_old_sigevent,
28793858a1f4SJohn Baldwin 	.fetch_status = aiocb32_fetch_status,
28803858a1f4SJohn Baldwin 	.fetch_error = aiocb32_fetch_error,
28813858a1f4SJohn Baldwin 	.store_status = aiocb32_store_status,
28823858a1f4SJohn Baldwin 	.store_error = aiocb32_store_error,
28833858a1f4SJohn Baldwin 	.store_aiocb = aiocb32_store_aiocb,
28843858a1f4SJohn Baldwin };
2885399e8c17SJohn Baldwin #endif
28863858a1f4SJohn Baldwin 
28873858a1f4SJohn Baldwin int
freebsd32_aio_return(struct thread * td,struct freebsd32_aio_return_args * uap)28883858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
28893858a1f4SJohn Baldwin {
28903858a1f4SJohn Baldwin 
28913858a1f4SJohn Baldwin 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
28923858a1f4SJohn Baldwin }
28933858a1f4SJohn Baldwin 
28943858a1f4SJohn Baldwin int
freebsd32_aio_suspend(struct thread * td,struct freebsd32_aio_suspend_args * uap)28953858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
28963858a1f4SJohn Baldwin {
28973858a1f4SJohn Baldwin 	struct timespec32 ts32;
28983858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
28993858a1f4SJohn Baldwin 	struct aiocb **ujoblist;
29003858a1f4SJohn Baldwin 	uint32_t *ujoblist32;
29013858a1f4SJohn Baldwin 	int error, i;
29023858a1f4SJohn Baldwin 
2903913b9329SAlan Somers 	if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
29043858a1f4SJohn Baldwin 		return (EINVAL);
29053858a1f4SJohn Baldwin 
29063858a1f4SJohn Baldwin 	if (uap->timeout) {
29073858a1f4SJohn Baldwin 		/* Get timespec struct. */
29083858a1f4SJohn Baldwin 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
29093858a1f4SJohn Baldwin 			return (error);
29103858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
29113858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
29123858a1f4SJohn Baldwin 		tsp = &ts;
29133858a1f4SJohn Baldwin 	} else
29143858a1f4SJohn Baldwin 		tsp = NULL;
29153858a1f4SJohn Baldwin 
29169553bc89SMark Johnston 	ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
29173858a1f4SJohn Baldwin 	ujoblist32 = (uint32_t *)ujoblist;
29183858a1f4SJohn Baldwin 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
29193858a1f4SJohn Baldwin 	    sizeof(ujoblist32[0]));
29203858a1f4SJohn Baldwin 	if (error == 0) {
2921df485bdbSAlan Somers 		for (i = uap->nent - 1; i >= 0; i--)
29223858a1f4SJohn Baldwin 			ujoblist[i] = PTRIN(ujoblist32[i]);
29233858a1f4SJohn Baldwin 
29243858a1f4SJohn Baldwin 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
29253858a1f4SJohn Baldwin 	}
29269553bc89SMark Johnston 	free(ujoblist, M_AIO);
29273858a1f4SJohn Baldwin 	return (error);
29283858a1f4SJohn Baldwin }
29293858a1f4SJohn Baldwin 
29303858a1f4SJohn Baldwin int
freebsd32_aio_error(struct thread * td,struct freebsd32_aio_error_args * uap)29313858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
29323858a1f4SJohn Baldwin {
29333858a1f4SJohn Baldwin 
29343858a1f4SJohn Baldwin 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
29353858a1f4SJohn Baldwin }
29363858a1f4SJohn Baldwin 
2937399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29383858a1f4SJohn Baldwin int
freebsd6_freebsd32_aio_read(struct thread * td,struct freebsd6_freebsd32_aio_read_args * uap)2939399e8c17SJohn Baldwin freebsd6_freebsd32_aio_read(struct thread *td,
2940399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_read_args *uap)
29413858a1f4SJohn Baldwin {
29423858a1f4SJohn Baldwin 
29433858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29443858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29453858a1f4SJohn Baldwin }
2946399e8c17SJohn Baldwin #endif
29473858a1f4SJohn Baldwin 
29483858a1f4SJohn Baldwin int
freebsd32_aio_read(struct thread * td,struct freebsd32_aio_read_args * uap)29493858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
29503858a1f4SJohn Baldwin {
29513858a1f4SJohn Baldwin 
29523858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
29533858a1f4SJohn Baldwin 	    &aiocb32_ops));
29543858a1f4SJohn Baldwin }
29553858a1f4SJohn Baldwin 
2956022ca2fcSAlan Somers int
freebsd32_aio_readv(struct thread * td,struct freebsd32_aio_readv_args * uap)2957022ca2fcSAlan Somers freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
2958022ca2fcSAlan Somers {
2959022ca2fcSAlan Somers 
2960022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
2961022ca2fcSAlan Somers 	    &aiocb32_ops));
2962022ca2fcSAlan Somers }
2963022ca2fcSAlan Somers 
2964399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
29653858a1f4SJohn Baldwin int
freebsd6_freebsd32_aio_write(struct thread * td,struct freebsd6_freebsd32_aio_write_args * uap)2966399e8c17SJohn Baldwin freebsd6_freebsd32_aio_write(struct thread *td,
2967399e8c17SJohn Baldwin     struct freebsd6_freebsd32_aio_write_args *uap)
29683858a1f4SJohn Baldwin {
29693858a1f4SJohn Baldwin 
29703858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
29713858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent));
29723858a1f4SJohn Baldwin }
2973399e8c17SJohn Baldwin #endif
29743858a1f4SJohn Baldwin 
29753858a1f4SJohn Baldwin int
freebsd32_aio_write(struct thread * td,struct freebsd32_aio_write_args * uap)29763858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
29773858a1f4SJohn Baldwin {
29783858a1f4SJohn Baldwin 
29793858a1f4SJohn Baldwin 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
29803858a1f4SJohn Baldwin 	    &aiocb32_ops));
29813858a1f4SJohn Baldwin }
29823858a1f4SJohn Baldwin 
29833858a1f4SJohn Baldwin int
freebsd32_aio_writev(struct thread * td,struct freebsd32_aio_writev_args * uap)2984022ca2fcSAlan Somers freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
2985022ca2fcSAlan Somers {
2986022ca2fcSAlan Somers 
2987022ca2fcSAlan Somers 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
2988022ca2fcSAlan Somers 	    &aiocb32_ops));
2989022ca2fcSAlan Somers }
2990022ca2fcSAlan Somers 
2991022ca2fcSAlan Somers int
freebsd32_aio_mlock(struct thread * td,struct freebsd32_aio_mlock_args * uap)29926160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
29936160e12cSGleb Smirnoff {
29946160e12cSGleb Smirnoff 
29956160e12cSGleb Smirnoff 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
29966160e12cSGleb Smirnoff 	    &aiocb32_ops));
29976160e12cSGleb Smirnoff }
29986160e12cSGleb Smirnoff 
29996160e12cSGleb Smirnoff int
freebsd32_aio_waitcomplete(struct thread * td,struct freebsd32_aio_waitcomplete_args * uap)30003858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td,
30013858a1f4SJohn Baldwin     struct freebsd32_aio_waitcomplete_args *uap)
30023858a1f4SJohn Baldwin {
3003e588eeb1SJohn Baldwin 	struct timespec32 ts32;
30043858a1f4SJohn Baldwin 	struct timespec ts, *tsp;
30053858a1f4SJohn Baldwin 	int error;
30063858a1f4SJohn Baldwin 
30073858a1f4SJohn Baldwin 	if (uap->timeout) {
30083858a1f4SJohn Baldwin 		/* Get timespec struct. */
30093858a1f4SJohn Baldwin 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
30103858a1f4SJohn Baldwin 		if (error)
30113858a1f4SJohn Baldwin 			return (error);
30123858a1f4SJohn Baldwin 		CP(ts32, ts, tv_sec);
30133858a1f4SJohn Baldwin 		CP(ts32, ts, tv_nsec);
30143858a1f4SJohn Baldwin 		tsp = &ts;
30153858a1f4SJohn Baldwin 	} else
30163858a1f4SJohn Baldwin 		tsp = NULL;
30173858a1f4SJohn Baldwin 
30183858a1f4SJohn Baldwin 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
30193858a1f4SJohn Baldwin 	    &aiocb32_ops));
30203858a1f4SJohn Baldwin }
30213858a1f4SJohn Baldwin 
30223858a1f4SJohn Baldwin int
freebsd32_aio_fsync(struct thread * td,struct freebsd32_aio_fsync_args * uap)30233858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
30243858a1f4SJohn Baldwin {
30253858a1f4SJohn Baldwin 
30263858a1f4SJohn Baldwin 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
30273858a1f4SJohn Baldwin 	    &aiocb32_ops));
30283858a1f4SJohn Baldwin }
30293858a1f4SJohn Baldwin 
3030399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6
30313858a1f4SJohn Baldwin int
freebsd6_freebsd32_lio_listio(struct thread * td,struct freebsd6_freebsd32_lio_listio_args * uap)3032399e8c17SJohn Baldwin freebsd6_freebsd32_lio_listio(struct thread *td,
3033399e8c17SJohn Baldwin     struct freebsd6_freebsd32_lio_listio_args *uap)
30343858a1f4SJohn Baldwin {
30353858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30363858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30373858a1f4SJohn Baldwin 	struct osigevent32 osig;
30383858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30393858a1f4SJohn Baldwin 	int error, i, nent;
30403858a1f4SJohn Baldwin 
30413858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30423858a1f4SJohn Baldwin 		return (EINVAL);
30433858a1f4SJohn Baldwin 
30443858a1f4SJohn Baldwin 	nent = uap->nent;
3045913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
30463858a1f4SJohn Baldwin 		return (EINVAL);
30473858a1f4SJohn Baldwin 
30483858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30493858a1f4SJohn Baldwin 		error = copyin(uap->sig, &osig, sizeof(osig));
30503858a1f4SJohn Baldwin 		if (error)
30513858a1f4SJohn Baldwin 			return (error);
30523858a1f4SJohn Baldwin 		error = convert_old_sigevent32(&osig, &sig);
30533858a1f4SJohn Baldwin 		if (error)
30543858a1f4SJohn Baldwin 			return (error);
30553858a1f4SJohn Baldwin 		sigp = &sig;
30563858a1f4SJohn Baldwin 	} else
30573858a1f4SJohn Baldwin 		sigp = NULL;
30583858a1f4SJohn Baldwin 
30593858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
30603858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
30613858a1f4SJohn Baldwin 	if (error) {
30623858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
30633858a1f4SJohn Baldwin 		return (error);
30643858a1f4SJohn Baldwin 	}
30653858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
30663858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
30673858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
30683858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
30693858a1f4SJohn Baldwin 
30703858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
30713858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
30723858a1f4SJohn Baldwin 	    &aiocb32_ops_osigevent);
30733858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
30743858a1f4SJohn Baldwin 	return (error);
30753858a1f4SJohn Baldwin }
3076399e8c17SJohn Baldwin #endif
30773858a1f4SJohn Baldwin 
30783858a1f4SJohn Baldwin int
freebsd32_lio_listio(struct thread * td,struct freebsd32_lio_listio_args * uap)30793858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
30803858a1f4SJohn Baldwin {
30813858a1f4SJohn Baldwin 	struct aiocb **acb_list;
30823858a1f4SJohn Baldwin 	struct sigevent *sigp, sig;
30833858a1f4SJohn Baldwin 	struct sigevent32 sig32;
30843858a1f4SJohn Baldwin 	uint32_t *acb_list32;
30853858a1f4SJohn Baldwin 	int error, i, nent;
30863858a1f4SJohn Baldwin 
30873858a1f4SJohn Baldwin 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
30883858a1f4SJohn Baldwin 		return (EINVAL);
30893858a1f4SJohn Baldwin 
30903858a1f4SJohn Baldwin 	nent = uap->nent;
3091913b9329SAlan Somers 	if (nent < 0 || nent > max_aio_queue_per_proc)
30923858a1f4SJohn Baldwin 		return (EINVAL);
30933858a1f4SJohn Baldwin 
30943858a1f4SJohn Baldwin 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
30953858a1f4SJohn Baldwin 		error = copyin(uap->sig, &sig32, sizeof(sig32));
30963858a1f4SJohn Baldwin 		if (error)
30973858a1f4SJohn Baldwin 			return (error);
30983858a1f4SJohn Baldwin 		error = convert_sigevent32(&sig32, &sig);
30993858a1f4SJohn Baldwin 		if (error)
31003858a1f4SJohn Baldwin 			return (error);
31013858a1f4SJohn Baldwin 		sigp = &sig;
31023858a1f4SJohn Baldwin 	} else
31033858a1f4SJohn Baldwin 		sigp = NULL;
31043858a1f4SJohn Baldwin 
31053858a1f4SJohn Baldwin 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
31063858a1f4SJohn Baldwin 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
31073858a1f4SJohn Baldwin 	if (error) {
31083858a1f4SJohn Baldwin 		free(acb_list32, M_LIO);
31093858a1f4SJohn Baldwin 		return (error);
31103858a1f4SJohn Baldwin 	}
31113858a1f4SJohn Baldwin 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
31123858a1f4SJohn Baldwin 	for (i = 0; i < nent; i++)
31133858a1f4SJohn Baldwin 		acb_list[i] = PTRIN(acb_list32[i]);
31143858a1f4SJohn Baldwin 	free(acb_list32, M_LIO);
31153858a1f4SJohn Baldwin 
31163858a1f4SJohn Baldwin 	error = kern_lio_listio(td, uap->mode,
31173858a1f4SJohn Baldwin 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
31183858a1f4SJohn Baldwin 	    &aiocb32_ops);
31193858a1f4SJohn Baldwin 	free(acb_list, M_LIO);
31203858a1f4SJohn Baldwin 	return (error);
31213858a1f4SJohn Baldwin }
31223858a1f4SJohn Baldwin 
31233858a1f4SJohn Baldwin #endif
3124