19454b2d8SWarner Losh /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 4ee877a35SJohn Dyson * Copyright (c) 1997 John S. Dyson. All rights reserved. 5ee877a35SJohn Dyson * 6ee877a35SJohn Dyson * Redistribution and use in source and binary forms, with or without 7ee877a35SJohn Dyson * modification, are permitted provided that the following conditions 8ee877a35SJohn Dyson * are met: 9ee877a35SJohn Dyson * 1. Redistributions of source code must retain the above copyright 10ee877a35SJohn Dyson * notice, this list of conditions and the following disclaimer. 11ee877a35SJohn Dyson * 2. John S. Dyson's name may not be used to endorse or promote products 12ee877a35SJohn Dyson * derived from this software without specific prior written permission. 13ee877a35SJohn Dyson * 14ee877a35SJohn Dyson * DISCLAIMER: This code isn't warranted to do anything useful. Anything 15ee877a35SJohn Dyson * bad that happens because of using this software isn't the responsibility 16ee877a35SJohn Dyson * of the author. This software is distributed AS-IS. 17ee877a35SJohn Dyson */ 18ee877a35SJohn Dyson 19ee877a35SJohn Dyson /* 208a6472b7SPeter Dufault * This file contains support for the POSIX 1003.1B AIO/LIO facility. 21ee877a35SJohn Dyson */ 22ee877a35SJohn Dyson 23677b542eSDavid E. O'Brien #include <sys/cdefs.h> 24677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 25677b542eSDavid E. O'Brien 26ee877a35SJohn Dyson #include <sys/param.h> 27ee877a35SJohn Dyson #include <sys/systm.h> 28f591779bSSeigo Tanimura #include <sys/malloc.h> 299626b608SPoul-Henning Kamp #include <sys/bio.h> 30a5c9bce7SBruce Evans #include <sys/buf.h> 314a144410SRobert Watson #include <sys/capsicum.h> 3275b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 33ee877a35SJohn Dyson #include <sys/sysproto.h> 34ee877a35SJohn Dyson #include <sys/filedesc.h> 35ee877a35SJohn Dyson #include <sys/kernel.h> 3677409fe1SPoul-Henning Kamp #include <sys/module.h> 37c9a970a7SAlan Cox #include <sys/kthread.h> 38ee877a35SJohn Dyson #include <sys/fcntl.h> 39ee877a35SJohn Dyson #include <sys/file.h> 40104a9b7eSAlexander Kabaev #include <sys/limits.h> 41fdebd4f0SBruce Evans #include <sys/lock.h> 4235e0e5b3SJohn Baldwin #include <sys/mutex.h> 43ee877a35SJohn Dyson #include <sys/unistd.h> 446aeb05d7STom Rhodes #include <sys/posix4.h> 45ee877a35SJohn Dyson #include <sys/proc.h> 462d2f8ae7SBruce Evans #include <sys/resourcevar.h> 47ee877a35SJohn Dyson #include <sys/signalvar.h> 48496ab053SKonstantin Belousov #include <sys/syscallsubr.h> 49bfbbc4aaSJason Evans #include <sys/protosw.h> 5089f6b863SAttilio Rao #include <sys/rwlock.h> 511ce91824SDavid Xu #include <sys/sema.h> 521ce91824SDavid Xu #include <sys/socket.h> 53bfbbc4aaSJason Evans #include <sys/socketvar.h> 5421d56e9cSAlfred Perlstein #include <sys/syscall.h> 5521d56e9cSAlfred Perlstein #include <sys/sysent.h> 56a624e84fSJohn Dyson #include <sys/sysctl.h> 579c20dc99SJohn Baldwin #include <sys/syslog.h> 58ee99e978SBruce Evans #include <sys/sx.h> 591ce91824SDavid Xu #include <sys/taskqueue.h> 60fd3bf775SJohn Dyson #include <sys/vnode.h> 61fd3bf775SJohn Dyson #include <sys/conf.h> 62cb679c38SJonathan Lemon #include <sys/event.h> 6399eee864SDavid Xu #include <sys/mount.h> 64f743d981SAlexander Motin #include <geom/geom.h> 65ee877a35SJohn Dyson 661ce91824SDavid Xu #include <machine/atomic.h> 671ce91824SDavid Xu 68ee877a35SJohn Dyson #include <vm/vm.h> 69f743d981SAlexander Motin #include <vm/vm_page.h> 70ee877a35SJohn Dyson #include <vm/vm_extern.h> 712244ea07SJohn Dyson #include <vm/pmap.h> 722244ea07SJohn Dyson #include <vm/vm_map.h> 7399eee864SDavid Xu #include <vm/vm_object.h> 74c897b813SJeff Roberson #include <vm/uma.h> 75ee877a35SJohn Dyson #include <sys/aio.h> 765aaef07cSJohn Dyson 77eb8e6d52SEivind Eklund /* 78eb8e6d52SEivind Eklund * Counter for allocating reference ids to new jobs. Wrapped to 1 on 7999eee864SDavid Xu * overflow. (XXX will be removed soon.) 80eb8e6d52SEivind Eklund */ 8199eee864SDavid Xu static u_long jobrefid; 822244ea07SJohn Dyson 8399eee864SDavid Xu /* 8499eee864SDavid Xu * Counter for aio_fsync. 8599eee864SDavid Xu */ 8699eee864SDavid Xu static uint64_t jobseqno; 8799eee864SDavid Xu 8884af4da6SJohn Dyson #ifndef MAX_AIO_PER_PROC 892244ea07SJohn Dyson #define MAX_AIO_PER_PROC 32 9084af4da6SJohn Dyson #endif 9184af4da6SJohn Dyson 9284af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE_PER_PROC 93913b9329SAlan Somers #define MAX_AIO_QUEUE_PER_PROC 256 9484af4da6SJohn Dyson #endif 9584af4da6SJohn Dyson 9684af4da6SJohn Dyson #ifndef MAX_AIO_QUEUE 97913b9329SAlan Somers #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */ 9884af4da6SJohn Dyson #endif 9984af4da6SJohn Dyson 10084af4da6SJohn Dyson #ifndef MAX_BUF_AIO 10184af4da6SJohn Dyson #define MAX_BUF_AIO 16 10284af4da6SJohn Dyson #endif 10384af4da6SJohn Dyson 104e603be7aSRobert Watson FEATURE(aio, "Asynchronous I/O"); 105c45796d5SAlan Somers SYSCTL_DECL(_p1003_1b); 106e603be7aSRobert Watson 1073858a1f4SJohn Baldwin static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list"); 108913b9329SAlan Somers static MALLOC_DEFINE(M_AIOS, "aios", "aio_suspend aio control block list"); 1093858a1f4SJohn Baldwin 1107029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1110dd6c035SJohn Baldwin "Async IO management"); 112eb8e6d52SEivind Eklund 113f3215338SJohn Baldwin static int enable_aio_unsafe = 0; 114f3215338SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0, 115f3215338SJohn Baldwin "Permit asynchronous IO on all file types, not just known-safe types"); 116f3215338SJohn Baldwin 1179c20dc99SJohn Baldwin static unsigned int unsafe_warningcnt = 1; 1189c20dc99SJohn Baldwin SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW, 1199c20dc99SJohn Baldwin &unsafe_warningcnt, 0, 1209c20dc99SJohn Baldwin "Warnings that will be triggered upon failed IO requests on unsafe files"); 1219c20dc99SJohn Baldwin 122303b270bSEivind Eklund static int max_aio_procs = MAX_AIO_PROCS; 1230dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0, 12439314b7dSJohn Baldwin "Maximum number of kernel processes to use for handling async IO "); 125a624e84fSJohn Dyson 126eb8e6d52SEivind Eklund static int num_aio_procs = 0; 1270dd6c035SJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0, 12839314b7dSJohn Baldwin "Number of presently active kernel processes for async IO"); 129a624e84fSJohn Dyson 130eb8e6d52SEivind Eklund /* 131eb8e6d52SEivind Eklund * The code will adjust the actual number of AIO processes towards this 132eb8e6d52SEivind Eklund * number when it gets a chance. 133eb8e6d52SEivind Eklund */ 134eb8e6d52SEivind Eklund static int target_aio_procs = TARGET_AIO_PROCS; 135eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs, 1360dd6c035SJohn Baldwin 0, 1370dd6c035SJohn Baldwin "Preferred number of ready kernel processes for async IO"); 138a624e84fSJohn Dyson 139eb8e6d52SEivind Eklund static int max_queue_count = MAX_AIO_QUEUE; 140eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0, 141eb8e6d52SEivind Eklund "Maximum number of aio requests to queue, globally"); 142a624e84fSJohn Dyson 143eb8e6d52SEivind Eklund static int num_queue_count = 0; 144eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0, 145eb8e6d52SEivind Eklund "Number of queued aio requests"); 146a624e84fSJohn Dyson 147eb8e6d52SEivind Eklund static int num_buf_aio = 0; 148eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0, 149eb8e6d52SEivind Eklund "Number of aio requests presently handled by the buf subsystem"); 150fd3bf775SJohn Dyson 1518091e52bSJohn Baldwin static int num_unmapped_aio = 0; 1528091e52bSJohn Baldwin SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio, 1538091e52bSJohn Baldwin 0, 1548091e52bSJohn Baldwin "Number of aio requests presently handled by unmapped I/O buffers"); 1558091e52bSJohn Baldwin 15639314b7dSJohn Baldwin /* Number of async I/O processes in the process of being started */ 157a9bf5e37SDavid Xu /* XXX This should be local to aio_aqueue() */ 158eb8e6d52SEivind Eklund static int num_aio_resv_start = 0; 159fd3bf775SJohn Dyson 160eb8e6d52SEivind Eklund static int aiod_lifetime; 161eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0, 162eb8e6d52SEivind Eklund "Maximum lifetime for idle aiod"); 16384af4da6SJohn Dyson 164eb8e6d52SEivind Eklund static int max_aio_per_proc = MAX_AIO_PER_PROC; 165eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc, 1660dd6c035SJohn Baldwin 0, 16786bbef43SJohn Baldwin "Maximum active aio requests per process"); 168eb8e6d52SEivind Eklund 169eb8e6d52SEivind Eklund static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC; 170eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW, 171eb8e6d52SEivind Eklund &max_aio_queue_per_proc, 0, 17286bbef43SJohn Baldwin "Maximum queued aio requests per process"); 173eb8e6d52SEivind Eklund 174eb8e6d52SEivind Eklund static int max_buf_aio = MAX_BUF_AIO; 175eb8e6d52SEivind Eklund SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0, 17686bbef43SJohn Baldwin "Maximum buf aio requests per process"); 177eb8e6d52SEivind Eklund 178913b9329SAlan Somers /* 179913b9329SAlan Somers * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires 180913b9329SAlan Somers * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with 181913b9329SAlan Somers * vfs.aio.aio_listio_max. 182913b9329SAlan Somers */ 183c45796d5SAlan Somers SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max, 184913b9329SAlan Somers CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc, 185913b9329SAlan Somers 0, "Maximum aio requests for a single lio_listio call"); 186c45796d5SAlan Somers 187399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 1880972628aSDavid Xu typedef struct oaiocb { 1890972628aSDavid Xu int aio_fildes; /* File descriptor */ 1900972628aSDavid Xu off_t aio_offset; /* File offset for I/O */ 1910972628aSDavid Xu volatile void *aio_buf; /* I/O buffer in process space */ 1920972628aSDavid Xu size_t aio_nbytes; /* Number of bytes for I/O */ 1930972628aSDavid Xu struct osigevent aio_sigevent; /* Signal to deliver */ 1940972628aSDavid Xu int aio_lio_opcode; /* LIO opcode */ 1950972628aSDavid Xu int aio_reqprio; /* Request priority -- ignored */ 1960972628aSDavid Xu struct __aiocb_private _aiocb_private; 1970972628aSDavid Xu } oaiocb_t; 198399e8c17SJohn Baldwin #endif 1990972628aSDavid Xu 2001aa4c324SDavid Xu /* 2015652770dSJohn Baldwin * Below is a key of locks used to protect each member of struct kaiocb 2021aa4c324SDavid Xu * aioliojob and kaioinfo and any backends. 2031aa4c324SDavid Xu * 2041aa4c324SDavid Xu * * - need not protected 205759ccccaSDavid Xu * a - locked by kaioinfo lock 2061aa4c324SDavid Xu * b - locked by backend lock, the backend lock can be null in some cases, 2071aa4c324SDavid Xu * for example, BIO belongs to this type, in this case, proc lock is 2081aa4c324SDavid Xu * reused. 2091aa4c324SDavid Xu * c - locked by aio_job_mtx, the lock for the generic file I/O backend. 2101aa4c324SDavid Xu */ 2111aa4c324SDavid Xu 2121aa4c324SDavid Xu /* 213f3215338SJohn Baldwin * If the routine that services an AIO request blocks while running in an 214f3215338SJohn Baldwin * AIO kernel process it can starve other I/O requests. BIO requests 21572bce9ffSAlan Somers * queued via aio_qbio() complete asynchronously and do not use AIO kernel 216f3215338SJohn Baldwin * processes at all. Socket I/O requests use a separate pool of 217f3215338SJohn Baldwin * kprocs and also force non-blocking I/O. Other file I/O requests 218f3215338SJohn Baldwin * use the generic fo_read/fo_write operations which can block. The 219f3215338SJohn Baldwin * fsync and mlock operations can also block while executing. Ideally 220f3215338SJohn Baldwin * none of these requests would block while executing. 221f3215338SJohn Baldwin * 222f3215338SJohn Baldwin * Note that the service routines cannot toggle O_NONBLOCK in the file 223f3215338SJohn Baldwin * structure directly while handling a request due to races with 224f3215338SJohn Baldwin * userland threads. 2251aa4c324SDavid Xu */ 2261aa4c324SDavid Xu 22748dac059SAlan Cox /* jobflags */ 228f3215338SJohn Baldwin #define KAIOCB_QUEUEING 0x01 229f3215338SJohn Baldwin #define KAIOCB_CANCELLED 0x02 230f3215338SJohn Baldwin #define KAIOCB_CANCELLING 0x04 2315652770dSJohn Baldwin #define KAIOCB_CHECKSYNC 0x08 232f3215338SJohn Baldwin #define KAIOCB_CLEARED 0x10 233f3215338SJohn Baldwin #define KAIOCB_FINISHED 0x20 23448dac059SAlan Cox 2352244ea07SJohn Dyson /* 2362244ea07SJohn Dyson * AIO process info 2372244ea07SJohn Dyson */ 23884af4da6SJohn Dyson #define AIOP_FREE 0x1 /* proc on free queue */ 23984af4da6SJohn Dyson 24039314b7dSJohn Baldwin struct aioproc { 24139314b7dSJohn Baldwin int aioprocflags; /* (c) AIO proc flags */ 24239314b7dSJohn Baldwin TAILQ_ENTRY(aioproc) list; /* (c) list of processes */ 24339314b7dSJohn Baldwin struct proc *aioproc; /* (*) the AIO proc */ 2442244ea07SJohn Dyson }; 2452244ea07SJohn Dyson 24684af4da6SJohn Dyson /* 24784af4da6SJohn Dyson * data-structure for lio signal management 24884af4da6SJohn Dyson */ 2491ce91824SDavid Xu struct aioliojob { 2501aa4c324SDavid Xu int lioj_flags; /* (a) listio flags */ 2515c5005ecSKonstantin Belousov int lioj_count; /* (a) count of jobs */ 2525c5005ecSKonstantin Belousov int lioj_finished_count; /* (a) count of finished jobs */ 2531aa4c324SDavid Xu struct sigevent lioj_signal; /* (a) signal on all I/O done */ 2541aa4c324SDavid Xu TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */ 2551aa4c324SDavid Xu struct knlist klist; /* (a) list of knotes */ 2561aa4c324SDavid Xu ksiginfo_t lioj_ksi; /* (a) Realtime signal info */ 25784af4da6SJohn Dyson }; 2581ce91824SDavid Xu 25984af4da6SJohn Dyson #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */ 26084af4da6SJohn Dyson #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */ 26169cd28daSDoug Ambrisko #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */ 26284af4da6SJohn Dyson 26384af4da6SJohn Dyson /* 26484af4da6SJohn Dyson * per process aio data structure 26584af4da6SJohn Dyson */ 2662244ea07SJohn Dyson struct kaioinfo { 267759ccccaSDavid Xu struct mtx kaio_mtx; /* the lock to protect this struct */ 2681aa4c324SDavid Xu int kaio_flags; /* (a) per process kaio flags */ 2691aa4c324SDavid Xu int kaio_active_count; /* (c) number of currently used AIOs */ 2701aa4c324SDavid Xu int kaio_count; /* (a) size of AIO queue */ 27172bce9ffSAlan Somers int kaio_buffer_count; /* (a) number of bio buffers */ 2725652770dSJohn Baldwin TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */ 2735652770dSJohn Baldwin TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */ 2741aa4c324SDavid Xu TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */ 2755652770dSJohn Baldwin TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */ 2765652770dSJohn Baldwin TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */ 277f3215338SJohn Baldwin TAILQ_HEAD(,kaiocb) kaio_syncready; /* (a) second q for aio_fsync */ 27839314b7dSJohn Baldwin struct task kaio_task; /* (*) task to kick aio processes */ 279f3215338SJohn Baldwin struct task kaio_sync_task; /* (*) task to schedule fsync jobs */ 2802244ea07SJohn Dyson }; 2812244ea07SJohn Dyson 282759ccccaSDavid Xu #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx) 283759ccccaSDavid Xu #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx) 284759ccccaSDavid Xu #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f)) 285759ccccaSDavid Xu #define AIO_MTX(ki) (&(ki)->kaio_mtx) 286759ccccaSDavid Xu 28784af4da6SJohn Dyson #define KAIO_RUNDOWN 0x1 /* process is being run down */ 2880dd6c035SJohn Baldwin #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */ 289fd3bf775SJohn Dyson 2903858a1f4SJohn Baldwin /* 2913858a1f4SJohn Baldwin * Operations used to interact with userland aio control blocks. 2923858a1f4SJohn Baldwin * Different ABIs provide their own operations. 2933858a1f4SJohn Baldwin */ 2943858a1f4SJohn Baldwin struct aiocb_ops { 295022ca2fcSAlan Somers int (*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty); 2963858a1f4SJohn Baldwin long (*fetch_status)(struct aiocb *ujob); 2973858a1f4SJohn Baldwin long (*fetch_error)(struct aiocb *ujob); 2983858a1f4SJohn Baldwin int (*store_status)(struct aiocb *ujob, long status); 2993858a1f4SJohn Baldwin int (*store_error)(struct aiocb *ujob, long error); 3003858a1f4SJohn Baldwin int (*store_kernelinfo)(struct aiocb *ujob, long jobref); 3013858a1f4SJohn Baldwin int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob); 3023858a1f4SJohn Baldwin }; 3033858a1f4SJohn Baldwin 30439314b7dSJohn Baldwin static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */ 3051ce91824SDavid Xu static struct sema aio_newproc_sem; 3061ce91824SDavid Xu static struct mtx aio_job_mtx; 3075652770dSJohn Baldwin static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */ 3081ce91824SDavid Xu static struct unrhdr *aiod_unr; 3092244ea07SJohn Dyson 310022ca2fcSAlan Somers static void aio_biocleanup(struct bio *bp); 3116a1162d4SAlexander Leidinger void aio_init_aioinfo(struct proc *p); 312723d37c0SKonstantin Belousov static int aio_onceonly(void); 3135652770dSJohn Baldwin static int aio_free_entry(struct kaiocb *job); 3145652770dSJohn Baldwin static void aio_process_rw(struct kaiocb *job); 3155652770dSJohn Baldwin static void aio_process_sync(struct kaiocb *job); 3165652770dSJohn Baldwin static void aio_process_mlock(struct kaiocb *job); 317f3215338SJohn Baldwin static void aio_schedule_fsync(void *context, int pending); 3181ce91824SDavid Xu static int aio_newproc(int *); 3195652770dSJohn Baldwin int aio_aqueue(struct thread *td, struct aiocb *ujob, 3203858a1f4SJohn Baldwin struct aioliojob *lio, int type, struct aiocb_ops *ops); 321f3215338SJohn Baldwin static int aio_queue_file(struct file *fp, struct kaiocb *job); 32272bce9ffSAlan Somers static void aio_biowakeup(struct bio *bp); 32375b8b3b2SJohn Baldwin static void aio_proc_rundown(void *arg, struct proc *p); 3240dd6c035SJohn Baldwin static void aio_proc_rundown_exec(void *arg, struct proc *p, 3250dd6c035SJohn Baldwin struct image_params *imgp); 32672bce9ffSAlan Somers static int aio_qbio(struct proc *p, struct kaiocb *job); 3271ce91824SDavid Xu static void aio_daemon(void *param); 328f3215338SJohn Baldwin static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job); 329005ce8e4SJohn Baldwin static bool aio_clear_cancel_function_locked(struct kaiocb *job); 330dbbccfe9SDavid Xu static int aio_kick(struct proc *userp); 33199eee864SDavid Xu static void aio_kick_nowait(struct proc *userp); 33299eee864SDavid Xu static void aio_kick_helper(void *context, int pending); 33321d56e9cSAlfred Perlstein static int filt_aioattach(struct knote *kn); 33421d56e9cSAlfred Perlstein static void filt_aiodetach(struct knote *kn); 33521d56e9cSAlfred Perlstein static int filt_aio(struct knote *kn, long hint); 33669cd28daSDoug Ambrisko static int filt_lioattach(struct knote *kn); 33769cd28daSDoug Ambrisko static void filt_liodetach(struct knote *kn); 33869cd28daSDoug Ambrisko static int filt_lio(struct knote *kn, long hint); 3392244ea07SJohn Dyson 340eb8e6d52SEivind Eklund /* 341eb8e6d52SEivind Eklund * Zones for: 342eb8e6d52SEivind Eklund * kaio Per process async io info 34339314b7dSJohn Baldwin * aiop async io process data 344eb8e6d52SEivind Eklund * aiocb async io jobs 345eb8e6d52SEivind Eklund * aiolio list io jobs 346eb8e6d52SEivind Eklund */ 347913b9329SAlan Somers static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiolio_zone; 348fd3bf775SJohn Dyson 349eb8e6d52SEivind Eklund /* kqueue filters for aio */ 350e76d823bSRobert Watson static struct filterops aio_filtops = { 351e76d823bSRobert Watson .f_isfd = 0, 352e76d823bSRobert Watson .f_attach = filt_aioattach, 353e76d823bSRobert Watson .f_detach = filt_aiodetach, 354e76d823bSRobert Watson .f_event = filt_aio, 355e76d823bSRobert Watson }; 356e76d823bSRobert Watson static struct filterops lio_filtops = { 357e76d823bSRobert Watson .f_isfd = 0, 358e76d823bSRobert Watson .f_attach = filt_lioattach, 359e76d823bSRobert Watson .f_detach = filt_liodetach, 360e76d823bSRobert Watson .f_event = filt_lio 361e76d823bSRobert Watson }; 36221d56e9cSAlfred Perlstein 36375b8b3b2SJohn Baldwin static eventhandler_tag exit_tag, exec_tag; 36475b8b3b2SJohn Baldwin 365c85650caSJohn Baldwin TASKQUEUE_DEFINE_THREAD(aiod_kick); 3661ce91824SDavid Xu 367eb8e6d52SEivind Eklund /* 368eb8e6d52SEivind Eklund * Main operations function for use as a kernel module. 369eb8e6d52SEivind Eklund */ 37021d56e9cSAlfred Perlstein static int 37121d56e9cSAlfred Perlstein aio_modload(struct module *module, int cmd, void *arg) 37221d56e9cSAlfred Perlstein { 37321d56e9cSAlfred Perlstein int error = 0; 37421d56e9cSAlfred Perlstein 37521d56e9cSAlfred Perlstein switch (cmd) { 37621d56e9cSAlfred Perlstein case MOD_LOAD: 37721d56e9cSAlfred Perlstein aio_onceonly(); 37821d56e9cSAlfred Perlstein break; 37921d56e9cSAlfred Perlstein case MOD_SHUTDOWN: 38021d56e9cSAlfred Perlstein break; 38121d56e9cSAlfred Perlstein default: 382f3215338SJohn Baldwin error = EOPNOTSUPP; 38321d56e9cSAlfred Perlstein break; 38421d56e9cSAlfred Perlstein } 38521d56e9cSAlfred Perlstein return (error); 38621d56e9cSAlfred Perlstein } 38721d56e9cSAlfred Perlstein 38821d56e9cSAlfred Perlstein static moduledata_t aio_mod = { 38921d56e9cSAlfred Perlstein "aio", 39021d56e9cSAlfred Perlstein &aio_modload, 39121d56e9cSAlfred Perlstein NULL 39221d56e9cSAlfred Perlstein }; 39321d56e9cSAlfred Perlstein 394399e8c17SJohn Baldwin DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY); 39521d56e9cSAlfred Perlstein MODULE_VERSION(aio, 1); 39621d56e9cSAlfred Perlstein 397fd3bf775SJohn Dyson /* 3982244ea07SJohn Dyson * Startup initialization 3992244ea07SJohn Dyson */ 400723d37c0SKonstantin Belousov static int 40121d56e9cSAlfred Perlstein aio_onceonly(void) 402fd3bf775SJohn Dyson { 40321d56e9cSAlfred Perlstein 40475b8b3b2SJohn Baldwin exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL, 40575b8b3b2SJohn Baldwin EVENTHANDLER_PRI_ANY); 4060dd6c035SJohn Baldwin exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, 4070dd6c035SJohn Baldwin NULL, EVENTHANDLER_PRI_ANY); 40821d56e9cSAlfred Perlstein kqueue_add_filteropts(EVFILT_AIO, &aio_filtops); 40969cd28daSDoug Ambrisko kqueue_add_filteropts(EVFILT_LIO, &lio_filtops); 4102244ea07SJohn Dyson TAILQ_INIT(&aio_freeproc); 4111ce91824SDavid Xu sema_init(&aio_newproc_sem, 0, "aio_new_proc"); 4121ce91824SDavid Xu mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF); 4132244ea07SJohn Dyson TAILQ_INIT(&aio_jobs); 4141ce91824SDavid Xu aiod_unr = new_unrhdr(1, INT_MAX, NULL); 415c897b813SJeff Roberson kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL, 416c897b813SJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 41739314b7dSJohn Baldwin aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL, 418c897b813SJeff Roberson NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 4195652770dSJohn Baldwin aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL, 420c897b813SJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 4211ce91824SDavid Xu aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL, 422c897b813SJeff Roberson NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 42384af4da6SJohn Dyson aiod_lifetime = AIOD_LIFETIME_DEFAULT; 424fd3bf775SJohn Dyson jobrefid = 1; 425399e8c17SJohn Baldwin p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO); 42686d52125SAlfred Perlstein p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE); 42786d52125SAlfred Perlstein p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0); 428723d37c0SKonstantin Belousov 429723d37c0SKonstantin Belousov return (0); 4302244ea07SJohn Dyson } 4312244ea07SJohn Dyson 432eb8e6d52SEivind Eklund /* 433bfbbc4aaSJason Evans * Init the per-process aioinfo structure. The aioinfo limits are set 434bfbbc4aaSJason Evans * per-process for user limit (resource) management. 4352244ea07SJohn Dyson */ 4366a1162d4SAlexander Leidinger void 437fd3bf775SJohn Dyson aio_init_aioinfo(struct proc *p) 438fd3bf775SJohn Dyson { 4392244ea07SJohn Dyson struct kaioinfo *ki; 440ac41f2efSAlfred Perlstein 441a163d034SWarner Losh ki = uma_zalloc(kaio_zone, M_WAITOK); 4429889bbacSKonstantin Belousov mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW); 44384af4da6SJohn Dyson ki->kaio_flags = 0; 4442244ea07SJohn Dyson ki->kaio_active_count = 0; 4451ce91824SDavid Xu ki->kaio_count = 0; 446fd3bf775SJohn Dyson ki->kaio_buffer_count = 0; 4471ce91824SDavid Xu TAILQ_INIT(&ki->kaio_all); 4481ce91824SDavid Xu TAILQ_INIT(&ki->kaio_done); 4492244ea07SJohn Dyson TAILQ_INIT(&ki->kaio_jobqueue); 45084af4da6SJohn Dyson TAILQ_INIT(&ki->kaio_liojoblist); 45199eee864SDavid Xu TAILQ_INIT(&ki->kaio_syncqueue); 452f3215338SJohn Baldwin TAILQ_INIT(&ki->kaio_syncready); 45399eee864SDavid Xu TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p); 454f3215338SJohn Baldwin TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki); 4553999ebe3SAlan Cox PROC_LOCK(p); 4563999ebe3SAlan Cox if (p->p_aioinfo == NULL) { 4573999ebe3SAlan Cox p->p_aioinfo = ki; 4583999ebe3SAlan Cox PROC_UNLOCK(p); 4593999ebe3SAlan Cox } else { 4603999ebe3SAlan Cox PROC_UNLOCK(p); 461759ccccaSDavid Xu mtx_destroy(&ki->kaio_mtx); 4623999ebe3SAlan Cox uma_zfree(kaio_zone, ki); 4632244ea07SJohn Dyson } 464bfbbc4aaSJason Evans 46522035f47SOleksandr Tymoshenko while (num_aio_procs < MIN(target_aio_procs, max_aio_procs)) 4661ce91824SDavid Xu aio_newproc(NULL); 4672244ea07SJohn Dyson } 4682244ea07SJohn Dyson 4694c0fb2cfSDavid Xu static int 4706814c2daSKonstantin Belousov aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext) 4714c0fb2cfSDavid Xu { 472cf7d9a8cSDavid Xu struct thread *td; 473cf7d9a8cSDavid Xu int error; 474759ccccaSDavid Xu 475cf7d9a8cSDavid Xu error = sigev_findtd(p, sigev, &td); 476cf7d9a8cSDavid Xu if (error) 477cf7d9a8cSDavid Xu return (error); 4784c0fb2cfSDavid Xu if (!KSI_ONQ(ksi)) { 479cf7d9a8cSDavid Xu ksiginfo_set_sigev(ksi, sigev); 4804c0fb2cfSDavid Xu ksi->ksi_code = SI_ASYNCIO; 4816814c2daSKonstantin Belousov ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0; 482cf7d9a8cSDavid Xu tdsendsignal(p, td, ksi->ksi_signo, ksi); 4834c0fb2cfSDavid Xu } 484759ccccaSDavid Xu PROC_UNLOCK(p); 485cf7d9a8cSDavid Xu return (error); 4864c0fb2cfSDavid Xu } 4874c0fb2cfSDavid Xu 4882244ea07SJohn Dyson /* 489bfbbc4aaSJason Evans * Free a job entry. Wait for completion if it is currently active, but don't 490bfbbc4aaSJason Evans * delay forever. If we delay, we return a flag that says that we have to 491bfbbc4aaSJason Evans * restart the queue scan. 4922244ea07SJohn Dyson */ 49388ed460eSAlan Cox static int 4945652770dSJohn Baldwin aio_free_entry(struct kaiocb *job) 495fd3bf775SJohn Dyson { 4962244ea07SJohn Dyson struct kaioinfo *ki; 4971ce91824SDavid Xu struct aioliojob *lj; 4982244ea07SJohn Dyson struct proc *p; 4992244ea07SJohn Dyson 5005652770dSJohn Baldwin p = job->userproc; 5011ce91824SDavid Xu MPASS(curproc == p); 5022244ea07SJohn Dyson ki = p->p_aioinfo; 5031ce91824SDavid Xu MPASS(ki != NULL); 5041ce91824SDavid Xu 505759ccccaSDavid Xu AIO_LOCK_ASSERT(ki, MA_OWNED); 506f3215338SJohn Baldwin MPASS(job->jobflags & KAIOCB_FINISHED); 507759ccccaSDavid Xu 5081ce91824SDavid Xu atomic_subtract_int(&num_queue_count, 1); 5091ce91824SDavid Xu 5101ce91824SDavid Xu ki->kaio_count--; 5111ce91824SDavid Xu MPASS(ki->kaio_count >= 0); 5121ce91824SDavid Xu 5135652770dSJohn Baldwin TAILQ_REMOVE(&ki->kaio_done, job, plist); 5145652770dSJohn Baldwin TAILQ_REMOVE(&ki->kaio_all, job, allist); 51527b8220dSDavid Xu 5165652770dSJohn Baldwin lj = job->lio; 51784af4da6SJohn Dyson if (lj) { 5181ce91824SDavid Xu lj->lioj_count--; 5191ce91824SDavid Xu lj->lioj_finished_count--; 5201ce91824SDavid Xu 521a9bf5e37SDavid Xu if (lj->lioj_count == 0) { 5221ce91824SDavid Xu TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 5231ce91824SDavid Xu /* lio is going away, we need to destroy any knotes */ 5241ce91824SDavid Xu knlist_delete(&lj->klist, curthread, 1); 525759ccccaSDavid Xu PROC_LOCK(p); 5261ce91824SDavid Xu sigqueue_take(&lj->lioj_ksi); 527759ccccaSDavid Xu PROC_UNLOCK(p); 5281ce91824SDavid Xu uma_zfree(aiolio_zone, lj); 52984af4da6SJohn Dyson } 53084af4da6SJohn Dyson } 5311ce91824SDavid Xu 5325652770dSJohn Baldwin /* job is going away, we need to destroy any knotes */ 5335652770dSJohn Baldwin knlist_delete(&job->klist, curthread, 1); 534759ccccaSDavid Xu PROC_LOCK(p); 5355652770dSJohn Baldwin sigqueue_take(&job->ksi); 536759ccccaSDavid Xu PROC_UNLOCK(p); 5371ce91824SDavid Xu 538759ccccaSDavid Xu AIO_UNLOCK(ki); 5392a522eb9SJohn Baldwin 5402a522eb9SJohn Baldwin /* 5412a522eb9SJohn Baldwin * The thread argument here is used to find the owning process 5422a522eb9SJohn Baldwin * and is also passed to fo_close() which may pass it to various 5432a522eb9SJohn Baldwin * places such as devsw close() routines. Because of that, we 5442a522eb9SJohn Baldwin * need a thread pointer from the process owning the job that is 5452a522eb9SJohn Baldwin * persistent and won't disappear out from under us or move to 5462a522eb9SJohn Baldwin * another process. 5472a522eb9SJohn Baldwin * 5482a522eb9SJohn Baldwin * Currently, all the callers of this function call it to remove 5495652770dSJohn Baldwin * a kaiocb from the current process' job list either via a 5502a522eb9SJohn Baldwin * syscall or due to the current process calling exit() or 5512a522eb9SJohn Baldwin * execve(). Thus, we know that p == curproc. We also know that 5522a522eb9SJohn Baldwin * curthread can't exit since we are curthread. 5532a522eb9SJohn Baldwin * 5542a522eb9SJohn Baldwin * Therefore, we use curthread as the thread to pass to 5552a522eb9SJohn Baldwin * knlist_delete(). This does mean that it is possible for the 5562a522eb9SJohn Baldwin * thread pointer at close time to differ from the thread pointer 5572a522eb9SJohn Baldwin * at open time, but this is already true of file descriptors in 5582a522eb9SJohn Baldwin * a multithreaded process. 559b40ce416SJulian Elischer */ 5605652770dSJohn Baldwin if (job->fd_file) 5615652770dSJohn Baldwin fdrop(job->fd_file, curthread); 5625652770dSJohn Baldwin crfree(job->cred); 563022ca2fcSAlan Somers if (job->uiop != &job->uio) 564022ca2fcSAlan Somers free(job->uiop, M_IOV); 5655652770dSJohn Baldwin uma_zfree(aiocb_zone, job); 566759ccccaSDavid Xu AIO_LOCK(ki); 5671ce91824SDavid Xu 568ac41f2efSAlfred Perlstein return (0); 5692244ea07SJohn Dyson } 5702244ea07SJohn Dyson 571993182e5SAlexander Leidinger static void 5720dd6c035SJohn Baldwin aio_proc_rundown_exec(void *arg, struct proc *p, 5730dd6c035SJohn Baldwin struct image_params *imgp __unused) 574993182e5SAlexander Leidinger { 575993182e5SAlexander Leidinger aio_proc_rundown(arg, p); 576993182e5SAlexander Leidinger } 577993182e5SAlexander Leidinger 578f3215338SJohn Baldwin static int 579f3215338SJohn Baldwin aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job) 580f3215338SJohn Baldwin { 581f3215338SJohn Baldwin aio_cancel_fn_t *func; 582f3215338SJohn Baldwin int cancelled; 583f3215338SJohn Baldwin 584f3215338SJohn Baldwin AIO_LOCK_ASSERT(ki, MA_OWNED); 585f3215338SJohn Baldwin if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED)) 586f3215338SJohn Baldwin return (0); 587f3215338SJohn Baldwin MPASS((job->jobflags & KAIOCB_CANCELLING) == 0); 588f3215338SJohn Baldwin job->jobflags |= KAIOCB_CANCELLED; 589f3215338SJohn Baldwin 590f3215338SJohn Baldwin func = job->cancel_fn; 591f3215338SJohn Baldwin 592f3215338SJohn Baldwin /* 593f3215338SJohn Baldwin * If there is no cancel routine, just leave the job marked as 594f3215338SJohn Baldwin * cancelled. The job should be in active use by a caller who 595f3215338SJohn Baldwin * should complete it normally or when it fails to install a 596f3215338SJohn Baldwin * cancel routine. 597f3215338SJohn Baldwin */ 598f3215338SJohn Baldwin if (func == NULL) 599f3215338SJohn Baldwin return (0); 600f3215338SJohn Baldwin 601f3215338SJohn Baldwin /* 602f3215338SJohn Baldwin * Set the CANCELLING flag so that aio_complete() will defer 603f3215338SJohn Baldwin * completions of this job. This prevents the job from being 604f3215338SJohn Baldwin * freed out from under the cancel callback. After the 605f3215338SJohn Baldwin * callback any deferred completion (whether from the callback 606f3215338SJohn Baldwin * or any other source) will be completed. 607f3215338SJohn Baldwin */ 608f3215338SJohn Baldwin job->jobflags |= KAIOCB_CANCELLING; 609f3215338SJohn Baldwin AIO_UNLOCK(ki); 610f3215338SJohn Baldwin func(job); 611f3215338SJohn Baldwin AIO_LOCK(ki); 612f3215338SJohn Baldwin job->jobflags &= ~KAIOCB_CANCELLING; 613f3215338SJohn Baldwin if (job->jobflags & KAIOCB_FINISHED) { 614f3215338SJohn Baldwin cancelled = job->uaiocb._aiocb_private.error == ECANCELED; 615f3215338SJohn Baldwin TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist); 616f3215338SJohn Baldwin aio_bio_done_notify(p, job); 617f3215338SJohn Baldwin } else { 618f3215338SJohn Baldwin /* 619f3215338SJohn Baldwin * The cancel callback might have scheduled an 620f3215338SJohn Baldwin * operation to cancel this request, but it is 621f3215338SJohn Baldwin * only counted as cancelled if the request is 622f3215338SJohn Baldwin * cancelled when the callback returns. 623f3215338SJohn Baldwin */ 624f3215338SJohn Baldwin cancelled = 0; 625f3215338SJohn Baldwin } 626f3215338SJohn Baldwin return (cancelled); 627f3215338SJohn Baldwin } 628f3215338SJohn Baldwin 6292244ea07SJohn Dyson /* 6302244ea07SJohn Dyson * Rundown the jobs for a given process. 6312244ea07SJohn Dyson */ 63221d56e9cSAlfred Perlstein static void 63375b8b3b2SJohn Baldwin aio_proc_rundown(void *arg, struct proc *p) 634fd3bf775SJohn Dyson { 6352244ea07SJohn Dyson struct kaioinfo *ki; 6361ce91824SDavid Xu struct aioliojob *lj; 6375652770dSJohn Baldwin struct kaiocb *job, *jobn; 6382244ea07SJohn Dyson 6392a522eb9SJohn Baldwin KASSERT(curthread->td_proc == p, 6402a522eb9SJohn Baldwin ("%s: called on non-curproc", __func__)); 6412244ea07SJohn Dyson ki = p->p_aioinfo; 6422244ea07SJohn Dyson if (ki == NULL) 6432244ea07SJohn Dyson return; 6442244ea07SJohn Dyson 645759ccccaSDavid Xu AIO_LOCK(ki); 64627b8220dSDavid Xu ki->kaio_flags |= KAIO_RUNDOWN; 6471ce91824SDavid Xu 6481ce91824SDavid Xu restart: 649a624e84fSJohn Dyson 650bfbbc4aaSJason Evans /* 6511ce91824SDavid Xu * Try to cancel all pending requests. This code simulates 6521ce91824SDavid Xu * aio_cancel on all pending I/O requests. 653bfbbc4aaSJason Evans */ 6545652770dSJohn Baldwin TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) { 655f3215338SJohn Baldwin aio_cancel_job(p, ki, job); 6562244ea07SJohn Dyson } 65784af4da6SJohn Dyson 6581ce91824SDavid Xu /* Wait for all running I/O to be finished */ 659f3215338SJohn Baldwin if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) { 66084af4da6SJohn Dyson ki->kaio_flags |= KAIO_WAKEUP; 661759ccccaSDavid Xu msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz); 6621ce91824SDavid Xu goto restart; 66384af4da6SJohn Dyson } 66484af4da6SJohn Dyson 6651ce91824SDavid Xu /* Free all completed I/O requests. */ 6665652770dSJohn Baldwin while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL) 6675652770dSJohn Baldwin aio_free_entry(job); 66884af4da6SJohn Dyson 6691ce91824SDavid Xu while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) { 670a9bf5e37SDavid Xu if (lj->lioj_count == 0) { 67184af4da6SJohn Dyson TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 6721ce91824SDavid Xu knlist_delete(&lj->klist, curthread, 1); 673759ccccaSDavid Xu PROC_LOCK(p); 6741ce91824SDavid Xu sigqueue_take(&lj->lioj_ksi); 675759ccccaSDavid Xu PROC_UNLOCK(p); 676c897b813SJeff Roberson uma_zfree(aiolio_zone, lj); 677f4f0ecefSJohn Dyson } else { 678a9bf5e37SDavid Xu panic("LIO job not cleaned up: C:%d, FC:%d\n", 679a9bf5e37SDavid Xu lj->lioj_count, lj->lioj_finished_count); 68084af4da6SJohn Dyson } 681f4f0ecefSJohn Dyson } 682759ccccaSDavid Xu AIO_UNLOCK(ki); 683c85650caSJohn Baldwin taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task); 684f3215338SJohn Baldwin taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task); 6855114048bSKonstantin Belousov mtx_destroy(&ki->kaio_mtx); 686c897b813SJeff Roberson uma_zfree(kaio_zone, ki); 687a624e84fSJohn Dyson p->p_aioinfo = NULL; 6882244ea07SJohn Dyson } 6892244ea07SJohn Dyson 6902244ea07SJohn Dyson /* 691bfbbc4aaSJason Evans * Select a job to run (called by an AIO daemon). 6922244ea07SJohn Dyson */ 6935652770dSJohn Baldwin static struct kaiocb * 69439314b7dSJohn Baldwin aio_selectjob(struct aioproc *aiop) 695fd3bf775SJohn Dyson { 6965652770dSJohn Baldwin struct kaiocb *job; 697bfbbc4aaSJason Evans struct kaioinfo *ki; 698bfbbc4aaSJason Evans struct proc *userp; 6992244ea07SJohn Dyson 7001ce91824SDavid Xu mtx_assert(&aio_job_mtx, MA_OWNED); 701f3215338SJohn Baldwin restart: 7025652770dSJohn Baldwin TAILQ_FOREACH(job, &aio_jobs, list) { 7035652770dSJohn Baldwin userp = job->userproc; 7042244ea07SJohn Dyson ki = userp->p_aioinfo; 7052244ea07SJohn Dyson 70686bbef43SJohn Baldwin if (ki->kaio_active_count < max_aio_per_proc) { 7075652770dSJohn Baldwin TAILQ_REMOVE(&aio_jobs, job, list); 708f3215338SJohn Baldwin if (!aio_clear_cancel_function(job)) 709f3215338SJohn Baldwin goto restart; 710f3215338SJohn Baldwin 7111ce91824SDavid Xu /* Account for currently active jobs. */ 7121ce91824SDavid Xu ki->kaio_active_count++; 7131ce91824SDavid Xu break; 7141ce91824SDavid Xu } 7151ce91824SDavid Xu } 7165652770dSJohn Baldwin return (job); 7172244ea07SJohn Dyson } 7182244ea07SJohn Dyson 7192244ea07SJohn Dyson /* 7200dd6c035SJohn Baldwin * Move all data to a permanent storage device. This code 721801ac943SThomas Munro * simulates the fsync and fdatasync syscalls. 72299eee864SDavid Xu */ 72399eee864SDavid Xu static int 724801ac943SThomas Munro aio_fsync_vnode(struct thread *td, struct vnode *vp, int op) 72599eee864SDavid Xu { 72699eee864SDavid Xu struct mount *mp; 727922bee44SKonstantin Belousov vm_object_t obj; 72899eee864SDavid Xu int error; 72999eee864SDavid Xu 730922bee44SKonstantin Belousov for (;;) { 731922bee44SKonstantin Belousov error = vn_start_write(vp, &mp, V_WAIT | PCATCH); 732922bee44SKonstantin Belousov if (error != 0) 733922bee44SKonstantin Belousov break; 734cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 735922bee44SKonstantin Belousov obj = vp->v_object; 736922bee44SKonstantin Belousov if (obj != NULL) { 737922bee44SKonstantin Belousov VM_OBJECT_WLOCK(obj); 738922bee44SKonstantin Belousov vm_object_page_clean(obj, 0, 0, 0); 739922bee44SKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 74099eee864SDavid Xu } 741801ac943SThomas Munro if (op == LIO_DSYNC) 742801ac943SThomas Munro error = VOP_FDATASYNC(vp, td); 743801ac943SThomas Munro else 74499eee864SDavid Xu error = VOP_FSYNC(vp, MNT_WAIT, td); 74599eee864SDavid Xu 746b249ce48SMateusz Guzik VOP_UNLOCK(vp); 74799eee864SDavid Xu vn_finished_write(mp); 748*2933a7caSKonstantin Belousov if (error != ERELOOKUP) 749922bee44SKonstantin Belousov break; 750922bee44SKonstantin Belousov } 75199eee864SDavid Xu return (error); 75299eee864SDavid Xu } 75399eee864SDavid Xu 75499eee864SDavid Xu /* 755f95c13dbSGleb Smirnoff * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that 75672bce9ffSAlan Somers * does the I/O request for the non-bio version of the operations. The normal 75772bce9ffSAlan Somers * vn operations are used, and this code should work in all instances for every 75872bce9ffSAlan Somers * type of file, including pipes, sockets, fifos, and regular files. 7591ce91824SDavid Xu * 7601aa4c324SDavid Xu * XXX I don't think it works well for socket, pipe, and fifo. 7612244ea07SJohn Dyson */ 76288ed460eSAlan Cox static void 7635652770dSJohn Baldwin aio_process_rw(struct kaiocb *job) 764fd3bf775SJohn Dyson { 765f8f750c5SRobert Watson struct ucred *td_savedcred; 766b40ce416SJulian Elischer struct thread *td; 7672244ea07SJohn Dyson struct aiocb *cb; 7682244ea07SJohn Dyson struct file *fp; 769bb430bc7SJohn Baldwin ssize_t cnt; 770b1012d80SJohn Baldwin long msgsnd_st, msgsnd_end; 771b1012d80SJohn Baldwin long msgrcv_st, msgrcv_end; 772b1012d80SJohn Baldwin long oublock_st, oublock_end; 773b1012d80SJohn Baldwin long inblock_st, inblock_end; 774022ca2fcSAlan Somers int error, opcode; 7752244ea07SJohn Dyson 7765652770dSJohn Baldwin KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ || 777022ca2fcSAlan Somers job->uaiocb.aio_lio_opcode == LIO_READV || 778022ca2fcSAlan Somers job->uaiocb.aio_lio_opcode == LIO_WRITE || 779022ca2fcSAlan Somers job->uaiocb.aio_lio_opcode == LIO_WRITEV, 7805652770dSJohn Baldwin ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode)); 781f95c13dbSGleb Smirnoff 782f3215338SJohn Baldwin aio_switch_vmspace(job); 783b40ce416SJulian Elischer td = curthread; 784f8f750c5SRobert Watson td_savedcred = td->td_ucred; 7855652770dSJohn Baldwin td->td_ucred = job->cred; 786022ca2fcSAlan Somers job->uiop->uio_td = td; 7875652770dSJohn Baldwin cb = &job->uaiocb; 7885652770dSJohn Baldwin fp = job->fd_file; 789bfbbc4aaSJason Evans 790022ca2fcSAlan Somers opcode = job->uaiocb.aio_lio_opcode; 791022ca2fcSAlan Somers cnt = job->uiop->uio_resid; 7922244ea07SJohn Dyson 793b1012d80SJohn Baldwin msgrcv_st = td->td_ru.ru_msgrcv; 794b1012d80SJohn Baldwin msgsnd_st = td->td_ru.ru_msgsnd; 7951c4bcd05SJeff Roberson inblock_st = td->td_ru.ru_inblock; 7961c4bcd05SJeff Roberson oublock_st = td->td_ru.ru_oublock; 797b1012d80SJohn Baldwin 798279d7226SMatthew Dillon /* 799a9bf5e37SDavid Xu * aio_aqueue() acquires a reference to the file that is 8009b16adc1SAlan Cox * released in aio_free_entry(). 801279d7226SMatthew Dillon */ 802022ca2fcSAlan Somers if (opcode == LIO_READ || opcode == LIO_READV) { 803022ca2fcSAlan Somers if (job->uiop->uio_resid == 0) 8045114048bSKonstantin Belousov error = 0; 8055114048bSKonstantin Belousov else 806022ca2fcSAlan Somers error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET, 807022ca2fcSAlan Somers td); 8082244ea07SJohn Dyson } else { 8096d53aa62SDavid Xu if (fp->f_type == DTYPE_VNODE) 8106d53aa62SDavid Xu bwillwrite(); 811022ca2fcSAlan Somers error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td); 8122244ea07SJohn Dyson } 813b1012d80SJohn Baldwin msgrcv_end = td->td_ru.ru_msgrcv; 814b1012d80SJohn Baldwin msgsnd_end = td->td_ru.ru_msgsnd; 8151c4bcd05SJeff Roberson inblock_end = td->td_ru.ru_inblock; 8161c4bcd05SJeff Roberson oublock_end = td->td_ru.ru_oublock; 817fd3bf775SJohn Dyson 818b1012d80SJohn Baldwin job->msgrcv = msgrcv_end - msgrcv_st; 819b1012d80SJohn Baldwin job->msgsnd = msgsnd_end - msgsnd_st; 820b1012d80SJohn Baldwin job->inblock = inblock_end - inblock_st; 821b1012d80SJohn Baldwin job->outblock = oublock_end - oublock_st; 8222244ea07SJohn Dyson 823022ca2fcSAlan Somers if (error != 0 && job->uiop->uio_resid != cnt) { 8242244ea07SJohn Dyson if (error == ERESTART || error == EINTR || error == EWOULDBLOCK) 8252244ea07SJohn Dyson error = 0; 8262247f489SAlan Somers if (error == EPIPE && (opcode & LIO_WRITE)) { 8275652770dSJohn Baldwin PROC_LOCK(job->userproc); 8285652770dSJohn Baldwin kern_psignal(job->userproc, SIGPIPE); 8295652770dSJohn Baldwin PROC_UNLOCK(job->userproc); 83019eb87d2SJohn Baldwin } 8312244ea07SJohn Dyson } 8322244ea07SJohn Dyson 833022ca2fcSAlan Somers cnt -= job->uiop->uio_resid; 834f8f750c5SRobert Watson td->td_ucred = td_savedcred; 835f0ec1740SJohn Baldwin if (error) 836f0ec1740SJohn Baldwin aio_complete(job, -1, error); 837f0ec1740SJohn Baldwin else 838f0ec1740SJohn Baldwin aio_complete(job, cnt, 0); 8392244ea07SJohn Dyson } 8402244ea07SJohn Dyson 84169cd28daSDoug Ambrisko static void 8425652770dSJohn Baldwin aio_process_sync(struct kaiocb *job) 843f95c13dbSGleb Smirnoff { 844f95c13dbSGleb Smirnoff struct thread *td = curthread; 845f95c13dbSGleb Smirnoff struct ucred *td_savedcred = td->td_ucred; 8465652770dSJohn Baldwin struct file *fp = job->fd_file; 847f95c13dbSGleb Smirnoff int error = 0; 848f95c13dbSGleb Smirnoff 8492247f489SAlan Somers KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC, 8505652770dSJohn Baldwin ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode)); 851f95c13dbSGleb Smirnoff 8525652770dSJohn Baldwin td->td_ucred = job->cred; 853801ac943SThomas Munro if (fp->f_vnode != NULL) { 854801ac943SThomas Munro error = aio_fsync_vnode(td, fp->f_vnode, 855801ac943SThomas Munro job->uaiocb.aio_lio_opcode); 856801ac943SThomas Munro } 857f95c13dbSGleb Smirnoff td->td_ucred = td_savedcred; 858f0ec1740SJohn Baldwin if (error) 859f0ec1740SJohn Baldwin aio_complete(job, -1, error); 860f0ec1740SJohn Baldwin else 861f0ec1740SJohn Baldwin aio_complete(job, 0, 0); 862f95c13dbSGleb Smirnoff } 863f95c13dbSGleb Smirnoff 864f95c13dbSGleb Smirnoff static void 8655652770dSJohn Baldwin aio_process_mlock(struct kaiocb *job) 8666160e12cSGleb Smirnoff { 8675652770dSJohn Baldwin struct aiocb *cb = &job->uaiocb; 8686160e12cSGleb Smirnoff int error; 8696160e12cSGleb Smirnoff 8705652770dSJohn Baldwin KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK, 8715652770dSJohn Baldwin ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode)); 8726160e12cSGleb Smirnoff 873f3215338SJohn Baldwin aio_switch_vmspace(job); 874496ab053SKonstantin Belousov error = kern_mlock(job->userproc, job->cred, 875496ab053SKonstantin Belousov __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes); 876496ab053SKonstantin Belousov aio_complete(job, error != 0 ? -1 : 0, error); 8776160e12cSGleb Smirnoff } 8786160e12cSGleb Smirnoff 8796160e12cSGleb Smirnoff static void 880f3215338SJohn Baldwin aio_bio_done_notify(struct proc *userp, struct kaiocb *job) 8811ce91824SDavid Xu { 8821ce91824SDavid Xu struct aioliojob *lj; 88369cd28daSDoug Ambrisko struct kaioinfo *ki; 8845652770dSJohn Baldwin struct kaiocb *sjob, *sjobn; 8851ce91824SDavid Xu int lj_done; 886f3215338SJohn Baldwin bool schedule_fsync; 88769cd28daSDoug Ambrisko 88869cd28daSDoug Ambrisko ki = userp->p_aioinfo; 889759ccccaSDavid Xu AIO_LOCK_ASSERT(ki, MA_OWNED); 8905652770dSJohn Baldwin lj = job->lio; 89169cd28daSDoug Ambrisko lj_done = 0; 89269cd28daSDoug Ambrisko if (lj) { 8931ce91824SDavid Xu lj->lioj_finished_count++; 8941ce91824SDavid Xu if (lj->lioj_count == lj->lioj_finished_count) 89569cd28daSDoug Ambrisko lj_done = 1; 89669cd28daSDoug Ambrisko } 8975652770dSJohn Baldwin TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist); 898f3215338SJohn Baldwin MPASS(job->jobflags & KAIOCB_FINISHED); 89927b8220dSDavid Xu 90027b8220dSDavid Xu if (ki->kaio_flags & KAIO_RUNDOWN) 90127b8220dSDavid Xu goto notification_done; 90227b8220dSDavid Xu 9035652770dSJohn Baldwin if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL || 9045652770dSJohn Baldwin job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) 9056814c2daSKonstantin Belousov aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true); 9061ce91824SDavid Xu 9075652770dSJohn Baldwin KNOTE_LOCKED(&job->klist, 1); 9081ce91824SDavid Xu 90969cd28daSDoug Ambrisko if (lj_done) { 9101ce91824SDavid Xu if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 91169cd28daSDoug Ambrisko lj->lioj_flags |= LIOJ_KEVENT_POSTED; 9121ce91824SDavid Xu KNOTE_LOCKED(&lj->klist, 1); 91369cd28daSDoug Ambrisko } 9141ce91824SDavid Xu if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED)) 91529331656SKonstantin Belousov == LIOJ_SIGNAL && 91629331656SKonstantin Belousov (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 9174c0fb2cfSDavid Xu lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) { 9186814c2daSKonstantin Belousov aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi, 9196814c2daSKonstantin Belousov true); 92069cd28daSDoug Ambrisko lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 92169cd28daSDoug Ambrisko } 92269cd28daSDoug Ambrisko } 92327b8220dSDavid Xu 92427b8220dSDavid Xu notification_done: 9255652770dSJohn Baldwin if (job->jobflags & KAIOCB_CHECKSYNC) { 926f3215338SJohn Baldwin schedule_fsync = false; 9275652770dSJohn Baldwin TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) { 928b9a53e16SJohn Baldwin if (job->fd_file != sjob->fd_file || 929b9a53e16SJohn Baldwin job->seqno >= sjob->seqno) 930b9a53e16SJohn Baldwin continue; 931b9a53e16SJohn Baldwin if (--sjob->pending > 0) 932b9a53e16SJohn Baldwin continue; 933b9a53e16SJohn Baldwin TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list); 934005ce8e4SJohn Baldwin if (!aio_clear_cancel_function_locked(sjob)) 935f3215338SJohn Baldwin continue; 936b9a53e16SJohn Baldwin TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list); 937f3215338SJohn Baldwin schedule_fsync = true; 93899eee864SDavid Xu } 939f3215338SJohn Baldwin if (schedule_fsync) 940f3215338SJohn Baldwin taskqueue_enqueue(taskqueue_aiod_kick, 941f3215338SJohn Baldwin &ki->kaio_sync_task); 94299eee864SDavid Xu } 94327b8220dSDavid Xu if (ki->kaio_flags & KAIO_WAKEUP) { 94469cd28daSDoug Ambrisko ki->kaio_flags &= ~KAIO_WAKEUP; 9451ce91824SDavid Xu wakeup(&userp->p_aioinfo); 94669cd28daSDoug Ambrisko } 94769cd28daSDoug Ambrisko } 94869cd28daSDoug Ambrisko 9498a4dc40fSJohn Baldwin static void 950f3215338SJohn Baldwin aio_schedule_fsync(void *context, int pending) 951f3215338SJohn Baldwin { 952f3215338SJohn Baldwin struct kaioinfo *ki; 953f3215338SJohn Baldwin struct kaiocb *job; 954f3215338SJohn Baldwin 955f3215338SJohn Baldwin ki = context; 956f3215338SJohn Baldwin AIO_LOCK(ki); 957f3215338SJohn Baldwin while (!TAILQ_EMPTY(&ki->kaio_syncready)) { 958f3215338SJohn Baldwin job = TAILQ_FIRST(&ki->kaio_syncready); 959f3215338SJohn Baldwin TAILQ_REMOVE(&ki->kaio_syncready, job, list); 960f3215338SJohn Baldwin AIO_UNLOCK(ki); 961f3215338SJohn Baldwin aio_schedule(job, aio_process_sync); 962f3215338SJohn Baldwin AIO_LOCK(ki); 963f3215338SJohn Baldwin } 964f3215338SJohn Baldwin AIO_UNLOCK(ki); 965f3215338SJohn Baldwin } 966f3215338SJohn Baldwin 967f3215338SJohn Baldwin bool 968f3215338SJohn Baldwin aio_cancel_cleared(struct kaiocb *job) 969f3215338SJohn Baldwin { 970f3215338SJohn Baldwin 971f3215338SJohn Baldwin /* 972f3215338SJohn Baldwin * The caller should hold the same queue lock held when 973f3215338SJohn Baldwin * aio_clear_cancel_function() was called and set this flag 974f3215338SJohn Baldwin * ensuring this check sees an up-to-date value. However, 975f3215338SJohn Baldwin * there is no way to assert that. 976f3215338SJohn Baldwin */ 977f3215338SJohn Baldwin return ((job->jobflags & KAIOCB_CLEARED) != 0); 978f3215338SJohn Baldwin } 979f3215338SJohn Baldwin 980005ce8e4SJohn Baldwin static bool 981005ce8e4SJohn Baldwin aio_clear_cancel_function_locked(struct kaiocb *job) 982005ce8e4SJohn Baldwin { 983005ce8e4SJohn Baldwin 984005ce8e4SJohn Baldwin AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED); 985005ce8e4SJohn Baldwin MPASS(job->cancel_fn != NULL); 986005ce8e4SJohn Baldwin if (job->jobflags & KAIOCB_CANCELLING) { 987005ce8e4SJohn Baldwin job->jobflags |= KAIOCB_CLEARED; 988005ce8e4SJohn Baldwin return (false); 989005ce8e4SJohn Baldwin } 990005ce8e4SJohn Baldwin job->cancel_fn = NULL; 991005ce8e4SJohn Baldwin return (true); 992005ce8e4SJohn Baldwin } 993005ce8e4SJohn Baldwin 994f3215338SJohn Baldwin bool 995f3215338SJohn Baldwin aio_clear_cancel_function(struct kaiocb *job) 996f3215338SJohn Baldwin { 997f3215338SJohn Baldwin struct kaioinfo *ki; 998005ce8e4SJohn Baldwin bool ret; 999f3215338SJohn Baldwin 1000f3215338SJohn Baldwin ki = job->userproc->p_aioinfo; 1001f3215338SJohn Baldwin AIO_LOCK(ki); 1002005ce8e4SJohn Baldwin ret = aio_clear_cancel_function_locked(job); 1003f3215338SJohn Baldwin AIO_UNLOCK(ki); 1004005ce8e4SJohn Baldwin return (ret); 1005f3215338SJohn Baldwin } 1006005ce8e4SJohn Baldwin 1007005ce8e4SJohn Baldwin static bool 1008005ce8e4SJohn Baldwin aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func) 1009005ce8e4SJohn Baldwin { 1010005ce8e4SJohn Baldwin 1011005ce8e4SJohn Baldwin AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED); 1012005ce8e4SJohn Baldwin if (job->jobflags & KAIOCB_CANCELLED) 1013005ce8e4SJohn Baldwin return (false); 1014005ce8e4SJohn Baldwin job->cancel_fn = func; 1015f3215338SJohn Baldwin return (true); 1016f3215338SJohn Baldwin } 1017f3215338SJohn Baldwin 1018f3215338SJohn Baldwin bool 1019f3215338SJohn Baldwin aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func) 1020f3215338SJohn Baldwin { 1021f3215338SJohn Baldwin struct kaioinfo *ki; 1022005ce8e4SJohn Baldwin bool ret; 1023f3215338SJohn Baldwin 1024f3215338SJohn Baldwin ki = job->userproc->p_aioinfo; 1025f3215338SJohn Baldwin AIO_LOCK(ki); 1026005ce8e4SJohn Baldwin ret = aio_set_cancel_function_locked(job, func); 1027f3215338SJohn Baldwin AIO_UNLOCK(ki); 1028005ce8e4SJohn Baldwin return (ret); 1029f3215338SJohn Baldwin } 1030f3215338SJohn Baldwin 1031f3215338SJohn Baldwin void 1032f3215338SJohn Baldwin aio_complete(struct kaiocb *job, long status, int error) 1033f3215338SJohn Baldwin { 1034f3215338SJohn Baldwin struct kaioinfo *ki; 1035f3215338SJohn Baldwin struct proc *userp; 1036f3215338SJohn Baldwin 1037f3215338SJohn Baldwin job->uaiocb._aiocb_private.error = error; 1038f3215338SJohn Baldwin job->uaiocb._aiocb_private.status = status; 1039f3215338SJohn Baldwin 1040f3215338SJohn Baldwin userp = job->userproc; 1041f3215338SJohn Baldwin ki = userp->p_aioinfo; 1042f3215338SJohn Baldwin 1043f3215338SJohn Baldwin AIO_LOCK(ki); 1044f3215338SJohn Baldwin KASSERT(!(job->jobflags & KAIOCB_FINISHED), 1045f3215338SJohn Baldwin ("duplicate aio_complete")); 1046f3215338SJohn Baldwin job->jobflags |= KAIOCB_FINISHED; 1047f3215338SJohn Baldwin if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) { 1048f3215338SJohn Baldwin TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist); 1049f3215338SJohn Baldwin aio_bio_done_notify(userp, job); 1050f3215338SJohn Baldwin } 1051f3215338SJohn Baldwin AIO_UNLOCK(ki); 1052f3215338SJohn Baldwin } 1053f3215338SJohn Baldwin 1054f3215338SJohn Baldwin void 1055f3215338SJohn Baldwin aio_cancel(struct kaiocb *job) 1056f3215338SJohn Baldwin { 1057f3215338SJohn Baldwin 1058f3215338SJohn Baldwin aio_complete(job, -1, ECANCELED); 1059f3215338SJohn Baldwin } 1060f3215338SJohn Baldwin 1061f3215338SJohn Baldwin void 10625652770dSJohn Baldwin aio_switch_vmspace(struct kaiocb *job) 10638a4dc40fSJohn Baldwin { 10648a4dc40fSJohn Baldwin 10655652770dSJohn Baldwin vmspace_switch_aio(job->userproc->p_vmspace); 10668a4dc40fSJohn Baldwin } 10678a4dc40fSJohn Baldwin 10682244ea07SJohn Dyson /* 1069f95c13dbSGleb Smirnoff * The AIO daemon, most of the actual work is done in aio_process_*, 107084af4da6SJohn Dyson * but the setup (and address space mgmt) is done in this routine. 10712244ea07SJohn Dyson */ 10722244ea07SJohn Dyson static void 10731ce91824SDavid Xu aio_daemon(void *_id) 10742244ea07SJohn Dyson { 10755652770dSJohn Baldwin struct kaiocb *job; 107639314b7dSJohn Baldwin struct aioproc *aiop; 1077bfbbc4aaSJason Evans struct kaioinfo *ki; 1078f3215338SJohn Baldwin struct proc *p; 10798a4dc40fSJohn Baldwin struct vmspace *myvm; 1080b40ce416SJulian Elischer struct thread *td = curthread; 10811ce91824SDavid Xu int id = (intptr_t)_id; 10822244ea07SJohn Dyson 10832244ea07SJohn Dyson /* 10848a4dc40fSJohn Baldwin * Grab an extra reference on the daemon's vmspace so that it 10858a4dc40fSJohn Baldwin * doesn't get freed by jobs that switch to a different 10868a4dc40fSJohn Baldwin * vmspace. 10872244ea07SJohn Dyson */ 10888a4dc40fSJohn Baldwin p = td->td_proc; 10898a4dc40fSJohn Baldwin myvm = vmspace_acquire_ref(p); 1090fd3bf775SJohn Dyson 10918a4dc40fSJohn Baldwin KASSERT(p->p_textvp == NULL, ("kthread has a textvp")); 1092fd3bf775SJohn Dyson 1093fd3bf775SJohn Dyson /* 1094bfbbc4aaSJason Evans * Allocate and ready the aio control info. There is one aiop structure 1095bfbbc4aaSJason Evans * per daemon. 1096fd3bf775SJohn Dyson */ 1097a163d034SWarner Losh aiop = uma_zalloc(aiop_zone, M_WAITOK); 109839314b7dSJohn Baldwin aiop->aioproc = p; 109939314b7dSJohn Baldwin aiop->aioprocflags = 0; 1100bfbbc4aaSJason Evans 1101fd3bf775SJohn Dyson /* 1102fd3bf775SJohn Dyson * Wakeup parent process. (Parent sleeps to keep from blasting away 1103b40ce416SJulian Elischer * and creating too many daemons.) 1104fd3bf775SJohn Dyson */ 11051ce91824SDavid Xu sema_post(&aio_newproc_sem); 11062244ea07SJohn Dyson 11071ce91824SDavid Xu mtx_lock(&aio_job_mtx); 1108bfbbc4aaSJason Evans for (;;) { 1109fd3bf775SJohn Dyson /* 1110fd3bf775SJohn Dyson * Take daemon off of free queue 1111fd3bf775SJohn Dyson */ 111239314b7dSJohn Baldwin if (aiop->aioprocflags & AIOP_FREE) { 11132244ea07SJohn Dyson TAILQ_REMOVE(&aio_freeproc, aiop, list); 111439314b7dSJohn Baldwin aiop->aioprocflags &= ~AIOP_FREE; 11152244ea07SJohn Dyson } 11162244ea07SJohn Dyson 1117fd3bf775SJohn Dyson /* 1118bfbbc4aaSJason Evans * Check for jobs. 1119fd3bf775SJohn Dyson */ 11205652770dSJohn Baldwin while ((job = aio_selectjob(aiop)) != NULL) { 11211ce91824SDavid Xu mtx_unlock(&aio_job_mtx); 11222244ea07SJohn Dyson 1123f3215338SJohn Baldwin ki = job->userproc->p_aioinfo; 1124f3215338SJohn Baldwin job->handle_fn(job); 112584af4da6SJohn Dyson 11269b84335cSDavid Xu mtx_lock(&aio_job_mtx); 11279b84335cSDavid Xu /* Decrement the active job count. */ 11289b84335cSDavid Xu ki->kaio_active_count--; 11292244ea07SJohn Dyson } 11302244ea07SJohn Dyson 1131fd3bf775SJohn Dyson /* 1132bfbbc4aaSJason Evans * Disconnect from user address space. 1133fd3bf775SJohn Dyson */ 11348a4dc40fSJohn Baldwin if (p->p_vmspace != myvm) { 11351ce91824SDavid Xu mtx_unlock(&aio_job_mtx); 11368a4dc40fSJohn Baldwin vmspace_switch_aio(myvm); 11371ce91824SDavid Xu mtx_lock(&aio_job_mtx); 11381ce91824SDavid Xu /* 11391ce91824SDavid Xu * We have to restart to avoid race, we only sleep if 11408a4dc40fSJohn Baldwin * no job can be selected. 11411ce91824SDavid Xu */ 11421ce91824SDavid Xu continue; 1143fd3bf775SJohn Dyson } 1144fd3bf775SJohn Dyson 11451ce91824SDavid Xu mtx_assert(&aio_job_mtx, MA_OWNED); 11461ce91824SDavid Xu 1147fd3bf775SJohn Dyson TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 114839314b7dSJohn Baldwin aiop->aioprocflags |= AIOP_FREE; 1149fd3bf775SJohn Dyson 1150fd3bf775SJohn Dyson /* 1151bfbbc4aaSJason Evans * If daemon is inactive for a long time, allow it to exit, 1152bfbbc4aaSJason Evans * thereby freeing resources. 1153fd3bf775SJohn Dyson */ 115439314b7dSJohn Baldwin if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy", 11558a4dc40fSJohn Baldwin aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) && 115639314b7dSJohn Baldwin (aiop->aioprocflags & AIOP_FREE) && 11578a4dc40fSJohn Baldwin num_aio_procs > target_aio_procs) 11588a4dc40fSJohn Baldwin break; 11598a4dc40fSJohn Baldwin } 1160fd3bf775SJohn Dyson TAILQ_REMOVE(&aio_freeproc, aiop, list); 116184af4da6SJohn Dyson num_aio_procs--; 11621ce91824SDavid Xu mtx_unlock(&aio_job_mtx); 11631ce91824SDavid Xu uma_zfree(aiop_zone, aiop); 11641ce91824SDavid Xu free_unr(aiod_unr, id); 11658a4dc40fSJohn Baldwin vmspace_free(myvm); 11668a4dc40fSJohn Baldwin 11678a4dc40fSJohn Baldwin KASSERT(p->p_vmspace == myvm, 11688a4dc40fSJohn Baldwin ("AIOD: bad vmspace for exiting daemon")); 1169f7db0c95SMark Johnston KASSERT(refcount_load(&myvm->vm_refcnt) > 1, 1170f7db0c95SMark Johnston ("AIOD: bad vm refcnt for exiting daemon: %d", 1171f7db0c95SMark Johnston refcount_load(&myvm->vm_refcnt))); 11723745c395SJulian Elischer kproc_exit(0); 1173fd3bf775SJohn Dyson } 11742244ea07SJohn Dyson 11752244ea07SJohn Dyson /* 1176bfbbc4aaSJason Evans * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The 1177bfbbc4aaSJason Evans * AIO daemon modifies its environment itself. 11782244ea07SJohn Dyson */ 11792244ea07SJohn Dyson static int 11801ce91824SDavid Xu aio_newproc(int *start) 1181fd3bf775SJohn Dyson { 11822244ea07SJohn Dyson int error; 1183c9a970a7SAlan Cox struct proc *p; 11841ce91824SDavid Xu int id; 11852244ea07SJohn Dyson 11861ce91824SDavid Xu id = alloc_unr(aiod_unr); 11873745c395SJulian Elischer error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p, 11881ce91824SDavid Xu RFNOWAIT, 0, "aiod%d", id); 11891ce91824SDavid Xu if (error == 0) { 1190fd3bf775SJohn Dyson /* 11911ce91824SDavid Xu * Wait until daemon is started. 1192fd3bf775SJohn Dyson */ 11931ce91824SDavid Xu sema_wait(&aio_newproc_sem); 11941ce91824SDavid Xu mtx_lock(&aio_job_mtx); 119584af4da6SJohn Dyson num_aio_procs++; 11961ce91824SDavid Xu if (start != NULL) 11977f34b521SDavid Xu (*start)--; 11981ce91824SDavid Xu mtx_unlock(&aio_job_mtx); 11991ce91824SDavid Xu } else { 12001ce91824SDavid Xu free_unr(aiod_unr, id); 12011ce91824SDavid Xu } 1202ac41f2efSAlfred Perlstein return (error); 12032244ea07SJohn Dyson } 12042244ea07SJohn Dyson 12052244ea07SJohn Dyson /* 120672bce9ffSAlan Somers * Try the high-performance, low-overhead bio method for eligible 120788ed460eSAlan Cox * VCHR devices. This method doesn't use an aio helper thread, and 120888ed460eSAlan Cox * thus has very low overhead. 120988ed460eSAlan Cox * 1210a9bf5e37SDavid Xu * Assumes that the caller, aio_aqueue(), has incremented the file 121188ed460eSAlan Cox * structure's reference count, preventing its deallocation for the 121288ed460eSAlan Cox * duration of this call. 1213fd3bf775SJohn Dyson */ 121488ed460eSAlan Cox static int 121572bce9ffSAlan Somers aio_qbio(struct proc *p, struct kaiocb *job) 1216fd3bf775SJohn Dyson { 1217fd3bf775SJohn Dyson struct aiocb *cb; 1218fd3bf775SJohn Dyson struct file *fp; 1219f743d981SAlexander Motin struct buf *pbuf; 1220fd3bf775SJohn Dyson struct vnode *vp; 1221f3215a60SKonstantin Belousov struct cdevsw *csw; 1222f3215a60SKonstantin Belousov struct cdev *dev; 1223fd3bf775SJohn Dyson struct kaioinfo *ki; 1224022ca2fcSAlan Somers struct bio **bios = NULL; 1225022ca2fcSAlan Somers off_t offset; 1226022ca2fcSAlan Somers int bio_cmd, error, i, iovcnt, opcode, poff, ref; 1227f743d981SAlexander Motin vm_prot_t prot; 1228022ca2fcSAlan Somers bool use_unmapped; 1229fd3bf775SJohn Dyson 12305652770dSJohn Baldwin cb = &job->uaiocb; 12315652770dSJohn Baldwin fp = job->fd_file; 1232022ca2fcSAlan Somers opcode = cb->aio_lio_opcode; 1233fd3bf775SJohn Dyson 1234022ca2fcSAlan Somers if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV || 1235022ca2fcSAlan Somers opcode == LIO_READ || opcode == LIO_READV)) 1236f54c5606SJohn Baldwin return (-1); 12376160e12cSGleb Smirnoff if (fp == NULL || fp->f_type != DTYPE_VNODE) 1238008626c3SPoul-Henning Kamp return (-1); 1239fd3bf775SJohn Dyson 12403b6d9652SPoul-Henning Kamp vp = fp->f_vnode; 1241f743d981SAlexander Motin if (vp->v_type != VCHR) 1242f582ac06SBrian Feldman return (-1); 1243ad8de0f2SDavid Xu if (vp->v_bufobj.bo_bsize == 0) 1244ad8de0f2SDavid Xu return (-1); 1245022ca2fcSAlan Somers 12462247f489SAlan Somers bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ; 1247022ca2fcSAlan Somers iovcnt = job->uiop->uio_iovcnt; 1248022ca2fcSAlan Somers if (iovcnt > max_buf_aio) 1249008626c3SPoul-Henning Kamp return (-1); 1250022ca2fcSAlan Somers for (i = 0; i < iovcnt; i++) { 1251022ca2fcSAlan Somers if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0) 1252022ca2fcSAlan Somers return (-1); 1253022ca2fcSAlan Somers if (job->uiop->uio_iov[i].iov_len > maxphys) { 1254022ca2fcSAlan Somers error = -1; 1255022ca2fcSAlan Somers return (-1); 1256022ca2fcSAlan Somers } 1257022ca2fcSAlan Somers } 1258022ca2fcSAlan Somers offset = cb->aio_offset; 1259fd3bf775SJohn Dyson 1260f3215a60SKonstantin Belousov ref = 0; 1261f3215a60SKonstantin Belousov csw = devvn_refthread(vp, &dev, &ref); 1262f3215a60SKonstantin Belousov if (csw == NULL) 1263f3215a60SKonstantin Belousov return (ENXIO); 1264f743d981SAlexander Motin 1265f743d981SAlexander Motin if ((csw->d_flags & D_DISK) == 0) { 1266f743d981SAlexander Motin error = -1; 1267f743d981SAlexander Motin goto unref; 1268f743d981SAlexander Motin } 1269022ca2fcSAlan Somers if (job->uiop->uio_resid > dev->si_iosize_max) { 1270f3215a60SKonstantin Belousov error = -1; 1271f3215a60SKonstantin Belousov goto unref; 1272f3215a60SKonstantin Belousov } 1273f3215a60SKonstantin Belousov 1274f743d981SAlexander Motin ki = p->p_aioinfo; 1275022ca2fcSAlan Somers job->error = 0; 12764d805eacSJohn Baldwin 1277022ca2fcSAlan Somers use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed; 1278022ca2fcSAlan Somers if (!use_unmapped) { 1279022ca2fcSAlan Somers AIO_LOCK(ki); 1280022ca2fcSAlan Somers if (ki->kaio_buffer_count + iovcnt > max_buf_aio) { 1281022ca2fcSAlan Somers AIO_UNLOCK(ki); 1282f54c5606SJohn Baldwin error = EAGAIN; 1283f743d981SAlexander Motin goto unref; 1284f743d981SAlexander Motin } 1285022ca2fcSAlan Somers ki->kaio_buffer_count += iovcnt; 1286022ca2fcSAlan Somers AIO_UNLOCK(ki); 1287022ca2fcSAlan Somers } 12884d805eacSJohn Baldwin 1289022ca2fcSAlan Somers bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK); 1290022ca2fcSAlan Somers atomic_store_int(&job->nbio, iovcnt); 1291022ca2fcSAlan Somers for (i = 0; i < iovcnt; i++) { 1292022ca2fcSAlan Somers struct vm_page** pages; 1293022ca2fcSAlan Somers struct bio *bp; 1294022ca2fcSAlan Somers void *buf; 1295022ca2fcSAlan Somers size_t nbytes; 1296022ca2fcSAlan Somers int npages; 1297022ca2fcSAlan Somers 1298022ca2fcSAlan Somers buf = job->uiop->uio_iov[i].iov_base; 1299022ca2fcSAlan Somers nbytes = job->uiop->uio_iov[i].iov_len; 1300022ca2fcSAlan Somers 1301022ca2fcSAlan Somers bios[i] = g_alloc_bio(); 1302022ca2fcSAlan Somers bp = bios[i]; 1303022ca2fcSAlan Somers 1304022ca2fcSAlan Somers poff = (vm_offset_t)buf & PAGE_MASK; 1305022ca2fcSAlan Somers if (use_unmapped) { 1306022ca2fcSAlan Somers pbuf = NULL; 1307022ca2fcSAlan Somers pages = malloc(sizeof(vm_page_t) * (atop(round_page( 1308022ca2fcSAlan Somers nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO); 1309022ca2fcSAlan Somers } else { 131001206038SAlan Somers pbuf = uma_zalloc(pbuf_zone, M_WAITOK); 1311f743d981SAlexander Motin BUF_KERNPROC(pbuf); 131201206038SAlan Somers pages = pbuf->b_pages; 13134d805eacSJohn Baldwin } 13141ce91824SDavid Xu 1315022ca2fcSAlan Somers bp->bio_length = nbytes; 1316022ca2fcSAlan Somers bp->bio_bcount = nbytes; 131772bce9ffSAlan Somers bp->bio_done = aio_biowakeup; 1318022ca2fcSAlan Somers bp->bio_offset = offset; 1319022ca2fcSAlan Somers bp->bio_cmd = bio_cmd; 1320f743d981SAlexander Motin bp->bio_dev = dev; 132101206038SAlan Somers bp->bio_caller1 = job; 132201206038SAlan Somers bp->bio_caller2 = pbuf; 1323f743d981SAlexander Motin 1324f743d981SAlexander Motin prot = VM_PROT_READ; 1325022ca2fcSAlan Somers if (opcode == LIO_READ || opcode == LIO_READV) 1326f743d981SAlexander Motin prot |= VM_PROT_WRITE; /* Less backwards than it looks */ 132701206038SAlan Somers npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map, 1328022ca2fcSAlan Somers (vm_offset_t)buf, bp->bio_length, prot, pages, 1329cd853791SKonstantin Belousov atop(maxphys) + 1); 133001206038SAlan Somers if (npages < 0) { 1331022ca2fcSAlan Somers if (pbuf != NULL) 1332022ca2fcSAlan Somers uma_zfree(pbuf_zone, pbuf); 1333022ca2fcSAlan Somers else 1334022ca2fcSAlan Somers free(pages, M_TEMP); 1335f743d981SAlexander Motin error = EFAULT; 1336022ca2fcSAlan Somers g_destroy_bio(bp); 1337022ca2fcSAlan Somers i--; 1338022ca2fcSAlan Somers goto destroy_bios; 1339f743d981SAlexander Motin } 13404d805eacSJohn Baldwin if (pbuf != NULL) { 134101206038SAlan Somers pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages); 1342f743d981SAlexander Motin bp->bio_data = pbuf->b_data + poff; 134301206038SAlan Somers pbuf->b_npages = npages; 1344022ca2fcSAlan Somers atomic_add_int(&num_buf_aio, 1); 1345f743d981SAlexander Motin } else { 134601206038SAlan Somers bp->bio_ma = pages; 134701206038SAlan Somers bp->bio_ma_n = npages; 1348f743d981SAlexander Motin bp->bio_ma_offset = poff; 1349f743d981SAlexander Motin bp->bio_data = unmapped_buf; 1350f743d981SAlexander Motin bp->bio_flags |= BIO_UNMAPPED; 13518091e52bSJohn Baldwin atomic_add_int(&num_unmapped_aio, 1); 1352f743d981SAlexander Motin } 1353f743d981SAlexander Motin 1354022ca2fcSAlan Somers offset += nbytes; 1355022ca2fcSAlan Somers } 1356022ca2fcSAlan Somers 1357bfbbc4aaSJason Evans /* Perform transfer. */ 1358022ca2fcSAlan Somers for (i = 0; i < iovcnt; i++) 1359022ca2fcSAlan Somers csw->d_strategy(bios[i]); 1360022ca2fcSAlan Somers free(bios, M_TEMP); 1361022ca2fcSAlan Somers 1362f3215a60SKonstantin Belousov dev_relthread(dev, ref); 1363ac41f2efSAlfred Perlstein return (0); 1364fd3bf775SJohn Dyson 1365022ca2fcSAlan Somers destroy_bios: 1366022ca2fcSAlan Somers for (; i >= 0; i--) 1367022ca2fcSAlan Somers aio_biocleanup(bios[i]); 1368022ca2fcSAlan Somers free(bios, M_TEMP); 1369f3215a60SKonstantin Belousov unref: 1370f3215a60SKonstantin Belousov dev_relthread(dev, ref); 1371fd3bf775SJohn Dyson return (error); 1372fd3bf775SJohn Dyson } 1373fd3bf775SJohn Dyson 1374399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 13753858a1f4SJohn Baldwin static int 13763858a1f4SJohn Baldwin convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig) 13773858a1f4SJohn Baldwin { 13783858a1f4SJohn Baldwin 13793858a1f4SJohn Baldwin /* 13803858a1f4SJohn Baldwin * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 13813858a1f4SJohn Baldwin * supported by AIO with the old sigevent structure. 13823858a1f4SJohn Baldwin */ 13833858a1f4SJohn Baldwin nsig->sigev_notify = osig->sigev_notify; 13843858a1f4SJohn Baldwin switch (nsig->sigev_notify) { 13853858a1f4SJohn Baldwin case SIGEV_NONE: 13863858a1f4SJohn Baldwin break; 13873858a1f4SJohn Baldwin case SIGEV_SIGNAL: 13883858a1f4SJohn Baldwin nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 13893858a1f4SJohn Baldwin break; 13903858a1f4SJohn Baldwin case SIGEV_KEVENT: 13913858a1f4SJohn Baldwin nsig->sigev_notify_kqueue = 13923858a1f4SJohn Baldwin osig->__sigev_u.__sigev_notify_kqueue; 13933858a1f4SJohn Baldwin nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr; 13943858a1f4SJohn Baldwin break; 13953858a1f4SJohn Baldwin default: 13963858a1f4SJohn Baldwin return (EINVAL); 13973858a1f4SJohn Baldwin } 13983858a1f4SJohn Baldwin return (0); 13993858a1f4SJohn Baldwin } 14003858a1f4SJohn Baldwin 14013858a1f4SJohn Baldwin static int 1402022ca2fcSAlan Somers aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob, 1403022ca2fcSAlan Somers int type __unused) 14043858a1f4SJohn Baldwin { 14053858a1f4SJohn Baldwin struct oaiocb *ojob; 1406022ca2fcSAlan Somers struct aiocb *kcb = &kjob->uaiocb; 14073858a1f4SJohn Baldwin int error; 14083858a1f4SJohn Baldwin 1409022ca2fcSAlan Somers bzero(kcb, sizeof(struct aiocb)); 1410022ca2fcSAlan Somers error = copyin(ujob, kcb, sizeof(struct oaiocb)); 14113858a1f4SJohn Baldwin if (error) 14123858a1f4SJohn Baldwin return (error); 1413022ca2fcSAlan Somers /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */ 1414022ca2fcSAlan Somers ojob = (struct oaiocb *)kcb; 1415022ca2fcSAlan Somers return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent)); 14163858a1f4SJohn Baldwin } 1417399e8c17SJohn Baldwin #endif 14183858a1f4SJohn Baldwin 14193858a1f4SJohn Baldwin static int 1420022ca2fcSAlan Somers aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type) 14213858a1f4SJohn Baldwin { 1422022ca2fcSAlan Somers struct aiocb *kcb = &kjob->uaiocb; 1423022ca2fcSAlan Somers int error; 14243858a1f4SJohn Baldwin 1425022ca2fcSAlan Somers error = copyin(ujob, kcb, sizeof(struct aiocb)); 1426022ca2fcSAlan Somers if (error) 1427022ca2fcSAlan Somers return (error); 1428f30a1ae8SThomas Munro if (type == LIO_NOP) 1429f30a1ae8SThomas Munro type = kcb->aio_lio_opcode; 14302247f489SAlan Somers if (type & LIO_VECTORED) { 1431022ca2fcSAlan Somers /* malloc a uio and copy in the iovec */ 1432022ca2fcSAlan Somers error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov), 1433022ca2fcSAlan Somers kcb->aio_iovcnt, &kjob->uiop); 1434022ca2fcSAlan Somers } 1435022ca2fcSAlan Somers 1436022ca2fcSAlan Somers return (error); 14373858a1f4SJohn Baldwin } 14383858a1f4SJohn Baldwin 14393858a1f4SJohn Baldwin static long 14403858a1f4SJohn Baldwin aiocb_fetch_status(struct aiocb *ujob) 14413858a1f4SJohn Baldwin { 14423858a1f4SJohn Baldwin 14433858a1f4SJohn Baldwin return (fuword(&ujob->_aiocb_private.status)); 14443858a1f4SJohn Baldwin } 14453858a1f4SJohn Baldwin 14463858a1f4SJohn Baldwin static long 14473858a1f4SJohn Baldwin aiocb_fetch_error(struct aiocb *ujob) 14483858a1f4SJohn Baldwin { 14493858a1f4SJohn Baldwin 14503858a1f4SJohn Baldwin return (fuword(&ujob->_aiocb_private.error)); 14513858a1f4SJohn Baldwin } 14523858a1f4SJohn Baldwin 14533858a1f4SJohn Baldwin static int 14543858a1f4SJohn Baldwin aiocb_store_status(struct aiocb *ujob, long status) 14553858a1f4SJohn Baldwin { 14563858a1f4SJohn Baldwin 14573858a1f4SJohn Baldwin return (suword(&ujob->_aiocb_private.status, status)); 14583858a1f4SJohn Baldwin } 14593858a1f4SJohn Baldwin 14603858a1f4SJohn Baldwin static int 14613858a1f4SJohn Baldwin aiocb_store_error(struct aiocb *ujob, long error) 14623858a1f4SJohn Baldwin { 14633858a1f4SJohn Baldwin 14643858a1f4SJohn Baldwin return (suword(&ujob->_aiocb_private.error, error)); 14653858a1f4SJohn Baldwin } 14663858a1f4SJohn Baldwin 14673858a1f4SJohn Baldwin static int 14683858a1f4SJohn Baldwin aiocb_store_kernelinfo(struct aiocb *ujob, long jobref) 14693858a1f4SJohn Baldwin { 14703858a1f4SJohn Baldwin 14713858a1f4SJohn Baldwin return (suword(&ujob->_aiocb_private.kernelinfo, jobref)); 14723858a1f4SJohn Baldwin } 14733858a1f4SJohn Baldwin 14743858a1f4SJohn Baldwin static int 14753858a1f4SJohn Baldwin aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 14763858a1f4SJohn Baldwin { 14773858a1f4SJohn Baldwin 14783858a1f4SJohn Baldwin return (suword(ujobp, (long)ujob)); 14793858a1f4SJohn Baldwin } 14803858a1f4SJohn Baldwin 14813858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops = { 1482849aef49SAndrew Turner .aio_copyin = aiocb_copyin, 14833858a1f4SJohn Baldwin .fetch_status = aiocb_fetch_status, 14843858a1f4SJohn Baldwin .fetch_error = aiocb_fetch_error, 14853858a1f4SJohn Baldwin .store_status = aiocb_store_status, 14863858a1f4SJohn Baldwin .store_error = aiocb_store_error, 14873858a1f4SJohn Baldwin .store_kernelinfo = aiocb_store_kernelinfo, 14883858a1f4SJohn Baldwin .store_aiocb = aiocb_store_aiocb, 14893858a1f4SJohn Baldwin }; 14903858a1f4SJohn Baldwin 1491399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 14923858a1f4SJohn Baldwin static struct aiocb_ops aiocb_ops_osigevent = { 1493849aef49SAndrew Turner .aio_copyin = aiocb_copyin_old_sigevent, 14943858a1f4SJohn Baldwin .fetch_status = aiocb_fetch_status, 14953858a1f4SJohn Baldwin .fetch_error = aiocb_fetch_error, 14963858a1f4SJohn Baldwin .store_status = aiocb_store_status, 14973858a1f4SJohn Baldwin .store_error = aiocb_store_error, 14983858a1f4SJohn Baldwin .store_kernelinfo = aiocb_store_kernelinfo, 14993858a1f4SJohn Baldwin .store_aiocb = aiocb_store_aiocb, 15003858a1f4SJohn Baldwin }; 1501399e8c17SJohn Baldwin #endif 15023858a1f4SJohn Baldwin 1503bfbbc4aaSJason Evans /* 150472bce9ffSAlan Somers * Queue a new AIO request. Choosing either the threaded or direct bio VCHR 1505bfbbc4aaSJason Evans * technique is done in this code. 15062244ea07SJohn Dyson */ 15076a1162d4SAlexander Leidinger int 15085652770dSJohn Baldwin aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj, 15093858a1f4SJohn Baldwin int type, struct aiocb_ops *ops) 1510fd3bf775SJohn Dyson { 1511b40ce416SJulian Elischer struct proc *p = td->td_proc; 1512022ca2fcSAlan Somers struct file *fp = NULL; 1513f3215338SJohn Baldwin struct kaiocb *job; 15142244ea07SJohn Dyson struct kaioinfo *ki; 1515c6fa9f78SAlan Cox struct kevent kev; 15161ce91824SDavid Xu int opcode; 15171ce91824SDavid Xu int error; 15184db71d27SJohn-Mark Gurney int fd, kqfd; 15191ce91824SDavid Xu int jid; 1520fde80935SDavid Xu u_short evflags; 15212244ea07SJohn Dyson 1522a9bf5e37SDavid Xu if (p->p_aioinfo == NULL) 1523a9bf5e37SDavid Xu aio_init_aioinfo(p); 1524a9bf5e37SDavid Xu 15251ce91824SDavid Xu ki = p->p_aioinfo; 15261ce91824SDavid Xu 15275652770dSJohn Baldwin ops->store_status(ujob, -1); 15285652770dSJohn Baldwin ops->store_error(ujob, 0); 15295652770dSJohn Baldwin ops->store_kernelinfo(ujob, -1); 1530a9bf5e37SDavid Xu 1531a9bf5e37SDavid Xu if (num_queue_count >= max_queue_count || 153286bbef43SJohn Baldwin ki->kaio_count >= max_aio_queue_per_proc) { 1533022ca2fcSAlan Somers error = EAGAIN; 1534022ca2fcSAlan Somers goto err1; 1535a9bf5e37SDavid Xu } 1536a9bf5e37SDavid Xu 15375652770dSJohn Baldwin job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO); 15385652770dSJohn Baldwin knlist_init_mtx(&job->klist, AIO_MTX(ki)); 1539fd3bf775SJohn Dyson 1540022ca2fcSAlan Somers error = ops->aio_copyin(ujob, job, type); 1541022ca2fcSAlan Somers if (error) 1542022ca2fcSAlan Somers goto err2; 154368d71118SDavid Xu 1544bb430bc7SJohn Baldwin if (job->uaiocb.aio_nbytes > IOSIZE_MAX) { 1545022ca2fcSAlan Somers error = EINVAL; 1546022ca2fcSAlan Somers goto err2; 1547434ea137SGleb Smirnoff } 1548434ea137SGleb Smirnoff 15495652770dSJohn Baldwin if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT && 15505652770dSJohn Baldwin job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL && 15515652770dSJohn Baldwin job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID && 15525652770dSJohn Baldwin job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) { 1553022ca2fcSAlan Somers error = EINVAL; 1554022ca2fcSAlan Somers goto err2; 155568d71118SDavid Xu } 155668d71118SDavid Xu 15575652770dSJohn Baldwin if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL || 15585652770dSJohn Baldwin job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) && 15595652770dSJohn Baldwin !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) { 1560022ca2fcSAlan Somers error = EINVAL; 1561022ca2fcSAlan Somers goto err2; 15622f3cf918SAlfred Perlstein } 15632244ea07SJohn Dyson 1564ff1a3078SAlan Somers /* Get the opcode. */ 1565ff1a3078SAlan Somers if (type == LIO_NOP) { 1566ff1a3078SAlan Somers switch (job->uaiocb.aio_lio_opcode) { 1567ff1a3078SAlan Somers case LIO_WRITE: 1568f30a1ae8SThomas Munro case LIO_WRITEV: 1569ff1a3078SAlan Somers case LIO_NOP: 1570ff1a3078SAlan Somers case LIO_READ: 1571f30a1ae8SThomas Munro case LIO_READV: 1572ff1a3078SAlan Somers opcode = job->uaiocb.aio_lio_opcode; 1573ff1a3078SAlan Somers break; 1574ff1a3078SAlan Somers default: 1575ff1a3078SAlan Somers error = EINVAL; 1576ff1a3078SAlan Somers goto err2; 1577ff1a3078SAlan Somers } 1578ff1a3078SAlan Somers } else 1579ff1a3078SAlan Somers opcode = job->uaiocb.aio_lio_opcode = type; 1580ff1a3078SAlan Somers 15815652770dSJohn Baldwin ksiginfo_init(&job->ksi); 15824c0fb2cfSDavid Xu 1583bfbbc4aaSJason Evans /* Save userspace address of the job info. */ 15845652770dSJohn Baldwin job->ujob = ujob; 158511783b14SJohn Dyson 1586a9d2f8d8SRobert Watson /* 1587a9d2f8d8SRobert Watson * Validate the opcode and fetch the file object for the specified 1588a9d2f8d8SRobert Watson * file descriptor. 1589a9d2f8d8SRobert Watson * 1590a9d2f8d8SRobert Watson * XXXRW: Moved the opcode validation up here so that we don't 1591a9d2f8d8SRobert Watson * retrieve a file descriptor without knowing what the capabiltity 1592a9d2f8d8SRobert Watson * should be. 1593a9d2f8d8SRobert Watson */ 15945652770dSJohn Baldwin fd = job->uaiocb.aio_fildes; 15952a522eb9SJohn Baldwin switch (opcode) { 15962a522eb9SJohn Baldwin case LIO_WRITE: 1597022ca2fcSAlan Somers case LIO_WRITEV: 1598cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_pwrite_rights, &fp); 15992a522eb9SJohn Baldwin break; 16002a522eb9SJohn Baldwin case LIO_READ: 1601022ca2fcSAlan Somers case LIO_READV: 1602cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_pread_rights, &fp); 1603a9d2f8d8SRobert Watson break; 1604a9d2f8d8SRobert Watson case LIO_SYNC: 1605801ac943SThomas Munro case LIO_DSYNC: 1606cbd92ce6SMatt Macy error = fget(td, fd, &cap_fsync_rights, &fp); 1607a9d2f8d8SRobert Watson break; 16086160e12cSGleb Smirnoff case LIO_MLOCK: 16096160e12cSGleb Smirnoff break; 1610a9d2f8d8SRobert Watson case LIO_NOP: 1611cbd92ce6SMatt Macy error = fget(td, fd, &cap_no_rights, &fp); 16122a522eb9SJohn Baldwin break; 16132a522eb9SJohn Baldwin default: 1614a9d2f8d8SRobert Watson error = EINVAL; 16152a522eb9SJohn Baldwin } 1616022ca2fcSAlan Somers if (error) 1617022ca2fcSAlan Somers goto err3; 161899eee864SDavid Xu 16192247f489SAlan Somers if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) { 162099eee864SDavid Xu error = EINVAL; 1621022ca2fcSAlan Somers goto err3; 162299eee864SDavid Xu } 16232244ea07SJohn Dyson 1624022ca2fcSAlan Somers if ((opcode == LIO_READ || opcode == LIO_READV || 1625022ca2fcSAlan Somers opcode == LIO_WRITE || opcode == LIO_WRITEV) && 1626711dba24SKonstantin Belousov job->uaiocb.aio_offset < 0 && 1627711dba24SKonstantin Belousov (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) { 1628ae124fc4SAlan Cox error = EINVAL; 1629022ca2fcSAlan Somers goto err3; 16302244ea07SJohn Dyson } 16311ce91824SDavid Xu 16328d9ed174SKonstantin Belousov if (fp != NULL && fp->f_ops == &path_fileops) { 16338d9ed174SKonstantin Belousov error = EBADF; 16348d9ed174SKonstantin Belousov goto err3; 16358d9ed174SKonstantin Belousov } 16368d9ed174SKonstantin Belousov 16375652770dSJohn Baldwin job->fd_file = fp; 16381ce91824SDavid Xu 163999eee864SDavid Xu mtx_lock(&aio_job_mtx); 164099eee864SDavid Xu jid = jobrefid++; 16415652770dSJohn Baldwin job->seqno = jobseqno++; 164299eee864SDavid Xu mtx_unlock(&aio_job_mtx); 16435652770dSJohn Baldwin error = ops->store_kernelinfo(ujob, jid); 16441ce91824SDavid Xu if (error) { 16451ce91824SDavid Xu error = EINVAL; 1646022ca2fcSAlan Somers goto err3; 16471ce91824SDavid Xu } 16485652770dSJohn Baldwin job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid; 16492244ea07SJohn Dyson 16502244ea07SJohn Dyson if (opcode == LIO_NOP) { 1651a5c0b1c0SAlan Cox fdrop(fp, td); 1652022ca2fcSAlan Somers MPASS(job->uiop == &job->uio || job->uiop == NULL); 16535652770dSJohn Baldwin uma_zfree(aiocb_zone, job); 1654ac41f2efSAlfred Perlstein return (0); 16552244ea07SJohn Dyson } 16562244ea07SJohn Dyson 16575652770dSJohn Baldwin if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT) 1658cb679c38SJonathan Lemon goto no_kqueue; 16595652770dSJohn Baldwin evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags; 1660fde80935SDavid Xu if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) { 1661fde80935SDavid Xu error = EINVAL; 1662022ca2fcSAlan Somers goto err3; 1663fde80935SDavid Xu } 16645652770dSJohn Baldwin kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue; 166536c4960eSMark Johnston memset(&kev, 0, sizeof(kev)); 16665652770dSJohn Baldwin kev.ident = (uintptr_t)job->ujob; 1667cb679c38SJonathan Lemon kev.filter = EVFILT_AIO; 1668fde80935SDavid Xu kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags; 16695652770dSJohn Baldwin kev.data = (intptr_t)job; 16705652770dSJohn Baldwin kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr; 1671792843c3SMark Johnston error = kqfd_register(kqfd, &kev, td, M_WAITOK); 1672f3215338SJohn Baldwin if (error) 1673022ca2fcSAlan Somers goto err3; 1674f3215338SJohn Baldwin 1675cb679c38SJonathan Lemon no_kqueue: 1676cb679c38SJonathan Lemon 16775652770dSJohn Baldwin ops->store_error(ujob, EINPROGRESS); 16785652770dSJohn Baldwin job->uaiocb._aiocb_private.error = EINPROGRESS; 16795652770dSJohn Baldwin job->userproc = p; 16805652770dSJohn Baldwin job->cred = crhold(td->td_ucred); 1681f3215338SJohn Baldwin job->jobflags = KAIOCB_QUEUEING; 16825652770dSJohn Baldwin job->lio = lj; 16832244ea07SJohn Dyson 16842247f489SAlan Somers if (opcode & LIO_VECTORED) { 1685022ca2fcSAlan Somers /* Use the uio copied in by aio_copyin */ 1686022ca2fcSAlan Somers MPASS(job->uiop != &job->uio && job->uiop != NULL); 16872247f489SAlan Somers } else { 1688022ca2fcSAlan Somers /* Setup the inline uio */ 1689022ca2fcSAlan Somers job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf; 1690022ca2fcSAlan Somers job->iov[0].iov_len = job->uaiocb.aio_nbytes; 1691022ca2fcSAlan Somers job->uio.uio_iov = job->iov; 1692022ca2fcSAlan Somers job->uio.uio_iovcnt = 1; 1693022ca2fcSAlan Somers job->uio.uio_resid = job->uaiocb.aio_nbytes; 1694022ca2fcSAlan Somers job->uio.uio_segflg = UIO_USERSPACE; 1695022ca2fcSAlan Somers job->uiop = &job->uio; 1696022ca2fcSAlan Somers } 16972247f489SAlan Somers switch (opcode & (LIO_READ | LIO_WRITE)) { 1698022ca2fcSAlan Somers case LIO_READ: 1699022ca2fcSAlan Somers job->uiop->uio_rw = UIO_READ; 1700022ca2fcSAlan Somers break; 1701022ca2fcSAlan Somers case LIO_WRITE: 1702022ca2fcSAlan Somers job->uiop->uio_rw = UIO_WRITE; 1703022ca2fcSAlan Somers break; 1704022ca2fcSAlan Somers } 1705022ca2fcSAlan Somers job->uiop->uio_offset = job->uaiocb.aio_offset; 1706022ca2fcSAlan Somers job->uiop->uio_td = td; 1707022ca2fcSAlan Somers 1708f3215338SJohn Baldwin if (opcode == LIO_MLOCK) { 1709f3215338SJohn Baldwin aio_schedule(job, aio_process_mlock); 1710f3215338SJohn Baldwin error = 0; 1711f3215338SJohn Baldwin } else if (fp->f_ops->fo_aio_queue == NULL) 1712f3215338SJohn Baldwin error = aio_queue_file(fp, job); 1713f3215338SJohn Baldwin else 1714f3215338SJohn Baldwin error = fo_aio_queue(fp, job); 1715f3215338SJohn Baldwin if (error) 1716022ca2fcSAlan Somers goto err3; 1717f3215338SJohn Baldwin 1718f3215338SJohn Baldwin AIO_LOCK(ki); 1719f3215338SJohn Baldwin job->jobflags &= ~KAIOCB_QUEUEING; 1720f3215338SJohn Baldwin TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist); 1721f3215338SJohn Baldwin ki->kaio_count++; 1722f3215338SJohn Baldwin if (lj) 1723f3215338SJohn Baldwin lj->lioj_count++; 1724f3215338SJohn Baldwin atomic_add_int(&num_queue_count, 1); 1725f3215338SJohn Baldwin if (job->jobflags & KAIOCB_FINISHED) { 1726f3215338SJohn Baldwin /* 1727f3215338SJohn Baldwin * The queue callback completed the request synchronously. 1728f3215338SJohn Baldwin * The bulk of the completion is deferred in that case 1729f3215338SJohn Baldwin * until this point. 1730f3215338SJohn Baldwin */ 1731f3215338SJohn Baldwin aio_bio_done_notify(p, job); 1732f3215338SJohn Baldwin } else 1733f3215338SJohn Baldwin TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist); 1734f3215338SJohn Baldwin AIO_UNLOCK(ki); 1735f3215338SJohn Baldwin return (0); 1736f3215338SJohn Baldwin 1737022ca2fcSAlan Somers err3: 1738f3215338SJohn Baldwin if (fp) 1739f3215338SJohn Baldwin fdrop(fp, td); 1740022ca2fcSAlan Somers knlist_delete(&job->klist, curthread, 0); 1741022ca2fcSAlan Somers err2: 1742022ca2fcSAlan Somers if (job->uiop != &job->uio) 1743022ca2fcSAlan Somers free(job->uiop, M_IOV); 1744f3215338SJohn Baldwin uma_zfree(aiocb_zone, job); 1745022ca2fcSAlan Somers err1: 1746f3215338SJohn Baldwin ops->store_error(ujob, error); 1747f3215338SJohn Baldwin return (error); 1748f3215338SJohn Baldwin } 1749f3215338SJohn Baldwin 1750f3215338SJohn Baldwin static void 1751f3215338SJohn Baldwin aio_cancel_daemon_job(struct kaiocb *job) 1752f3215338SJohn Baldwin { 1753f3215338SJohn Baldwin 1754f3215338SJohn Baldwin mtx_lock(&aio_job_mtx); 1755f3215338SJohn Baldwin if (!aio_cancel_cleared(job)) 1756f3215338SJohn Baldwin TAILQ_REMOVE(&aio_jobs, job, list); 1757f3215338SJohn Baldwin mtx_unlock(&aio_job_mtx); 1758f3215338SJohn Baldwin aio_cancel(job); 1759f3215338SJohn Baldwin } 1760f3215338SJohn Baldwin 1761f3215338SJohn Baldwin void 1762f3215338SJohn Baldwin aio_schedule(struct kaiocb *job, aio_handle_fn_t *func) 1763f3215338SJohn Baldwin { 1764f3215338SJohn Baldwin 1765f3215338SJohn Baldwin mtx_lock(&aio_job_mtx); 1766f3215338SJohn Baldwin if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) { 1767f3215338SJohn Baldwin mtx_unlock(&aio_job_mtx); 1768f3215338SJohn Baldwin aio_cancel(job); 1769f3215338SJohn Baldwin return; 1770f3215338SJohn Baldwin } 1771f3215338SJohn Baldwin job->handle_fn = func; 1772f3215338SJohn Baldwin TAILQ_INSERT_TAIL(&aio_jobs, job, list); 1773f3215338SJohn Baldwin aio_kick_nowait(job->userproc); 1774f3215338SJohn Baldwin mtx_unlock(&aio_job_mtx); 1775f3215338SJohn Baldwin } 1776f3215338SJohn Baldwin 1777f3215338SJohn Baldwin static void 1778f3215338SJohn Baldwin aio_cancel_sync(struct kaiocb *job) 1779f3215338SJohn Baldwin { 1780f3215338SJohn Baldwin struct kaioinfo *ki; 1781f3215338SJohn Baldwin 1782f3215338SJohn Baldwin ki = job->userproc->p_aioinfo; 1783005ce8e4SJohn Baldwin AIO_LOCK(ki); 1784f3215338SJohn Baldwin if (!aio_cancel_cleared(job)) 1785f3215338SJohn Baldwin TAILQ_REMOVE(&ki->kaio_syncqueue, job, list); 1786005ce8e4SJohn Baldwin AIO_UNLOCK(ki); 1787f3215338SJohn Baldwin aio_cancel(job); 1788f3215338SJohn Baldwin } 1789f3215338SJohn Baldwin 1790f3215338SJohn Baldwin int 1791f3215338SJohn Baldwin aio_queue_file(struct file *fp, struct kaiocb *job) 1792f3215338SJohn Baldwin { 1793f3215338SJohn Baldwin struct kaioinfo *ki; 1794f3215338SJohn Baldwin struct kaiocb *job2; 17959fe297bbSKonstantin Belousov struct vnode *vp; 17969fe297bbSKonstantin Belousov struct mount *mp; 1797f54c5606SJohn Baldwin int error; 17989fe297bbSKonstantin Belousov bool safe; 1799f3215338SJohn Baldwin 1800f3215338SJohn Baldwin ki = job->userproc->p_aioinfo; 180172bce9ffSAlan Somers error = aio_qbio(job->userproc, job); 1802f54c5606SJohn Baldwin if (error >= 0) 1803f54c5606SJohn Baldwin return (error); 18049fe297bbSKonstantin Belousov safe = false; 18059fe297bbSKonstantin Belousov if (fp->f_type == DTYPE_VNODE) { 18069fe297bbSKonstantin Belousov vp = fp->f_vnode; 18079fe297bbSKonstantin Belousov if (vp->v_type == VREG || vp->v_type == VDIR) { 18089fe297bbSKonstantin Belousov mp = fp->f_vnode->v_mount; 18099fe297bbSKonstantin Belousov if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0) 18109fe297bbSKonstantin Belousov safe = true; 18119fe297bbSKonstantin Belousov } 18129fe297bbSKonstantin Belousov } 18139c20dc99SJohn Baldwin if (!(safe || enable_aio_unsafe)) { 18149c20dc99SJohn Baldwin counted_warning(&unsafe_warningcnt, 18159c20dc99SJohn Baldwin "is attempting to use unsafe AIO requests"); 1816f3215338SJohn Baldwin return (EOPNOTSUPP); 18179c20dc99SJohn Baldwin } 181884af4da6SJohn Dyson 18192247f489SAlan Somers if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) { 18207e409184SJohn Baldwin aio_schedule(job, aio_process_rw); 18217e409184SJohn Baldwin error = 0; 18222247f489SAlan Somers } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) { 1823f3215338SJohn Baldwin AIO_LOCK(ki); 18245652770dSJohn Baldwin TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) { 18255652770dSJohn Baldwin if (job2->fd_file == job->fd_file && 18262247f489SAlan Somers ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) && 18275652770dSJohn Baldwin job2->seqno < job->seqno) { 18285652770dSJohn Baldwin job2->jobflags |= KAIOCB_CHECKSYNC; 18295652770dSJohn Baldwin job->pending++; 1830dbbccfe9SDavid Xu } 1831dbbccfe9SDavid Xu } 18325652770dSJohn Baldwin if (job->pending != 0) { 1833005ce8e4SJohn Baldwin if (!aio_set_cancel_function_locked(job, 1834005ce8e4SJohn Baldwin aio_cancel_sync)) { 1835f3215338SJohn Baldwin AIO_UNLOCK(ki); 1836f3215338SJohn Baldwin aio_cancel(job); 1837f3215338SJohn Baldwin return (0); 1838f3215338SJohn Baldwin } 18395652770dSJohn Baldwin TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list); 1840759ccccaSDavid Xu AIO_UNLOCK(ki); 1841f3215338SJohn Baldwin return (0); 1842dbbccfe9SDavid Xu } 1843759ccccaSDavid Xu AIO_UNLOCK(ki); 1844f3215338SJohn Baldwin aio_schedule(job, aio_process_sync); 1845f3215338SJohn Baldwin error = 0; 18462247f489SAlan Somers } else { 1847f3215338SJohn Baldwin error = EINVAL; 1848f3215338SJohn Baldwin } 184999eee864SDavid Xu return (error); 185099eee864SDavid Xu } 185199eee864SDavid Xu 185299eee864SDavid Xu static void 185399eee864SDavid Xu aio_kick_nowait(struct proc *userp) 185499eee864SDavid Xu { 185599eee864SDavid Xu struct kaioinfo *ki = userp->p_aioinfo; 185639314b7dSJohn Baldwin struct aioproc *aiop; 185799eee864SDavid Xu 185899eee864SDavid Xu mtx_assert(&aio_job_mtx, MA_OWNED); 185999eee864SDavid Xu if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 186099eee864SDavid Xu TAILQ_REMOVE(&aio_freeproc, aiop, list); 186139314b7dSJohn Baldwin aiop->aioprocflags &= ~AIOP_FREE; 186239314b7dSJohn Baldwin wakeup(aiop->aioproc); 18630dd6c035SJohn Baldwin } else if (num_aio_resv_start + num_aio_procs < max_aio_procs && 186486bbef43SJohn Baldwin ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) { 1865c85650caSJohn Baldwin taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task); 186699eee864SDavid Xu } 186799eee864SDavid Xu } 186899eee864SDavid Xu 1869dbbccfe9SDavid Xu static int 187099eee864SDavid Xu aio_kick(struct proc *userp) 187199eee864SDavid Xu { 187299eee864SDavid Xu struct kaioinfo *ki = userp->p_aioinfo; 187339314b7dSJohn Baldwin struct aioproc *aiop; 1874dbbccfe9SDavid Xu int error, ret = 0; 187599eee864SDavid Xu 187699eee864SDavid Xu mtx_assert(&aio_job_mtx, MA_OWNED); 187799eee864SDavid Xu retryproc: 1878d254af07SMatthew Dillon if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 18792244ea07SJohn Dyson TAILQ_REMOVE(&aio_freeproc, aiop, list); 188039314b7dSJohn Baldwin aiop->aioprocflags &= ~AIOP_FREE; 188139314b7dSJohn Baldwin wakeup(aiop->aioproc); 18820dd6c035SJohn Baldwin } else if (num_aio_resv_start + num_aio_procs < max_aio_procs && 188386bbef43SJohn Baldwin ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) { 1884fd3bf775SJohn Dyson num_aio_resv_start++; 18851ce91824SDavid Xu mtx_unlock(&aio_job_mtx); 18861ce91824SDavid Xu error = aio_newproc(&num_aio_resv_start); 18871ce91824SDavid Xu mtx_lock(&aio_job_mtx); 18881ce91824SDavid Xu if (error) { 188984af4da6SJohn Dyson num_aio_resv_start--; 18902244ea07SJohn Dyson goto retryproc; 1891fd3bf775SJohn Dyson } 1892dbbccfe9SDavid Xu } else { 1893dbbccfe9SDavid Xu ret = -1; 18941ce91824SDavid Xu } 1895dbbccfe9SDavid Xu return (ret); 189699eee864SDavid Xu } 18971ce91824SDavid Xu 189899eee864SDavid Xu static void 189999eee864SDavid Xu aio_kick_helper(void *context, int pending) 190099eee864SDavid Xu { 190199eee864SDavid Xu struct proc *userp = context; 190299eee864SDavid Xu 190399eee864SDavid Xu mtx_lock(&aio_job_mtx); 1904dbbccfe9SDavid Xu while (--pending >= 0) { 1905dbbccfe9SDavid Xu if (aio_kick(userp)) 1906dbbccfe9SDavid Xu break; 1907dbbccfe9SDavid Xu } 190899eee864SDavid Xu mtx_unlock(&aio_job_mtx); 19092244ea07SJohn Dyson } 19102244ea07SJohn Dyson 1911fd3bf775SJohn Dyson /* 1912bfbbc4aaSJason Evans * Support the aio_return system call, as a side-effect, kernel resources are 1913bfbbc4aaSJason Evans * released. 19142244ea07SJohn Dyson */ 19153858a1f4SJohn Baldwin static int 19165652770dSJohn Baldwin kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops) 1917fd3bf775SJohn Dyson { 1918b40ce416SJulian Elischer struct proc *p = td->td_proc; 19195652770dSJohn Baldwin struct kaiocb *job; 19202244ea07SJohn Dyson struct kaioinfo *ki; 1921bb430bc7SJohn Baldwin long status, error; 19222244ea07SJohn Dyson 1923c0bf5caaSAlan Cox ki = p->p_aioinfo; 1924c0bf5caaSAlan Cox if (ki == NULL) 1925ac41f2efSAlfred Perlstein return (EINVAL); 1926759ccccaSDavid Xu AIO_LOCK(ki); 19275652770dSJohn Baldwin TAILQ_FOREACH(job, &ki->kaio_done, plist) { 19285652770dSJohn Baldwin if (job->ujob == ujob) 1929c0bf5caaSAlan Cox break; 1930c0bf5caaSAlan Cox } 19315652770dSJohn Baldwin if (job != NULL) { 1932f3215338SJohn Baldwin MPASS(job->jobflags & KAIOCB_FINISHED); 19335652770dSJohn Baldwin status = job->uaiocb._aiocb_private.status; 19345652770dSJohn Baldwin error = job->uaiocb._aiocb_private.error; 19351ce91824SDavid Xu td->td_retval[0] = status; 1936b1012d80SJohn Baldwin td->td_ru.ru_oublock += job->outblock; 1937b1012d80SJohn Baldwin td->td_ru.ru_inblock += job->inblock; 1938b1012d80SJohn Baldwin td->td_ru.ru_msgsnd += job->msgsnd; 1939b1012d80SJohn Baldwin td->td_ru.ru_msgrcv += job->msgrcv; 19405652770dSJohn Baldwin aio_free_entry(job); 1941759ccccaSDavid Xu AIO_UNLOCK(ki); 19425652770dSJohn Baldwin ops->store_error(ujob, error); 19435652770dSJohn Baldwin ops->store_status(ujob, status); 194455a122bfSDavid Xu } else { 19451ce91824SDavid Xu error = EINVAL; 1946759ccccaSDavid Xu AIO_UNLOCK(ki); 194755a122bfSDavid Xu } 19481ce91824SDavid Xu return (error); 19492244ea07SJohn Dyson } 19502244ea07SJohn Dyson 19513858a1f4SJohn Baldwin int 19528451d0ddSKip Macy sys_aio_return(struct thread *td, struct aio_return_args *uap) 19533858a1f4SJohn Baldwin { 19543858a1f4SJohn Baldwin 19553858a1f4SJohn Baldwin return (kern_aio_return(td, uap->aiocbp, &aiocb_ops)); 19563858a1f4SJohn Baldwin } 19573858a1f4SJohn Baldwin 19582244ea07SJohn Dyson /* 1959bfbbc4aaSJason Evans * Allow a process to wakeup when any of the I/O requests are completed. 19602244ea07SJohn Dyson */ 19613858a1f4SJohn Baldwin static int 19623858a1f4SJohn Baldwin kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist, 19633858a1f4SJohn Baldwin struct timespec *ts) 1964fd3bf775SJohn Dyson { 1965b40ce416SJulian Elischer struct proc *p = td->td_proc; 19664a11ca4eSPoul-Henning Kamp struct timeval atv; 19672244ea07SJohn Dyson struct kaioinfo *ki; 19685652770dSJohn Baldwin struct kaiocb *firstjob, *job; 19693858a1f4SJohn Baldwin int error, i, timo; 19702244ea07SJohn Dyson 19712244ea07SJohn Dyson timo = 0; 19723858a1f4SJohn Baldwin if (ts) { 19733858a1f4SJohn Baldwin if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 19742244ea07SJohn Dyson return (EINVAL); 19752244ea07SJohn Dyson 19763858a1f4SJohn Baldwin TIMESPEC_TO_TIMEVAL(&atv, ts); 19772244ea07SJohn Dyson if (itimerfix(&atv)) 19782244ea07SJohn Dyson return (EINVAL); 1979227ee8a1SPoul-Henning Kamp timo = tvtohz(&atv); 19802244ea07SJohn Dyson } 19812244ea07SJohn Dyson 19822244ea07SJohn Dyson ki = p->p_aioinfo; 19832244ea07SJohn Dyson if (ki == NULL) 1984ac41f2efSAlfred Perlstein return (EAGAIN); 19852244ea07SJohn Dyson 19863858a1f4SJohn Baldwin if (njoblist == 0) 1987ac41f2efSAlfred Perlstein return (0); 19882244ea07SJohn Dyson 1989759ccccaSDavid Xu AIO_LOCK(ki); 19901ce91824SDavid Xu for (;;) { 19915652770dSJohn Baldwin firstjob = NULL; 19921ce91824SDavid Xu error = 0; 19935652770dSJohn Baldwin TAILQ_FOREACH(job, &ki->kaio_all, allist) { 199484af4da6SJohn Dyson for (i = 0; i < njoblist; i++) { 19955652770dSJohn Baldwin if (job->ujob == ujoblist[i]) { 19965652770dSJohn Baldwin if (firstjob == NULL) 19975652770dSJohn Baldwin firstjob = job; 1998f3215338SJohn Baldwin if (job->jobflags & KAIOCB_FINISHED) 19991ce91824SDavid Xu goto RETURN; 200084af4da6SJohn Dyson } 200184af4da6SJohn Dyson } 200284af4da6SJohn Dyson } 20031ce91824SDavid Xu /* All tasks were finished. */ 20045652770dSJohn Baldwin if (firstjob == NULL) 20051ce91824SDavid Xu break; 20062244ea07SJohn Dyson 2007fd3bf775SJohn Dyson ki->kaio_flags |= KAIO_WAKEUP; 2008759ccccaSDavid Xu error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 20091ce91824SDavid Xu "aiospn", timo); 20101ce91824SDavid Xu if (error == ERESTART) 20111ce91824SDavid Xu error = EINTR; 20121ce91824SDavid Xu if (error) 20131ce91824SDavid Xu break; 20142244ea07SJohn Dyson } 20151ce91824SDavid Xu RETURN: 2016759ccccaSDavid Xu AIO_UNLOCK(ki); 20173858a1f4SJohn Baldwin return (error); 20183858a1f4SJohn Baldwin } 20193858a1f4SJohn Baldwin 20203858a1f4SJohn Baldwin int 20218451d0ddSKip Macy sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap) 20223858a1f4SJohn Baldwin { 20233858a1f4SJohn Baldwin struct timespec ts, *tsp; 20243858a1f4SJohn Baldwin struct aiocb **ujoblist; 20253858a1f4SJohn Baldwin int error; 20263858a1f4SJohn Baldwin 2027913b9329SAlan Somers if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc) 20283858a1f4SJohn Baldwin return (EINVAL); 20293858a1f4SJohn Baldwin 20303858a1f4SJohn Baldwin if (uap->timeout) { 20313858a1f4SJohn Baldwin /* Get timespec struct. */ 20323858a1f4SJohn Baldwin if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) 20333858a1f4SJohn Baldwin return (error); 20343858a1f4SJohn Baldwin tsp = &ts; 20353858a1f4SJohn Baldwin } else 20363858a1f4SJohn Baldwin tsp = NULL; 20373858a1f4SJohn Baldwin 2038913b9329SAlan Somers ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK); 20393858a1f4SJohn Baldwin error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0])); 20403858a1f4SJohn Baldwin if (error == 0) 20413858a1f4SJohn Baldwin error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 2042913b9329SAlan Somers free(ujoblist, M_AIOS); 20431ce91824SDavid Xu return (error); 20442244ea07SJohn Dyson } 2045ee877a35SJohn Dyson 2046ee877a35SJohn Dyson /* 204772bce9ffSAlan Somers * aio_cancel cancels any non-bio aio operations not currently in progress. 2048ee877a35SJohn Dyson */ 2049ee877a35SJohn Dyson int 20508451d0ddSKip Macy sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap) 2051fd3bf775SJohn Dyson { 2052b40ce416SJulian Elischer struct proc *p = td->td_proc; 2053dd85920aSJason Evans struct kaioinfo *ki; 20545652770dSJohn Baldwin struct kaiocb *job, *jobn; 2055dd85920aSJason Evans struct file *fp; 20561ce91824SDavid Xu int error; 2057dd85920aSJason Evans int cancelled = 0; 2058dd85920aSJason Evans int notcancelled = 0; 2059dd85920aSJason Evans struct vnode *vp; 2060dd85920aSJason Evans 20612a522eb9SJohn Baldwin /* Lookup file object. */ 2062cbd92ce6SMatt Macy error = fget(td, uap->fd, &cap_no_rights, &fp); 20632a522eb9SJohn Baldwin if (error) 20642a522eb9SJohn Baldwin return (error); 2065dd85920aSJason Evans 20661ce91824SDavid Xu ki = p->p_aioinfo; 20671ce91824SDavid Xu if (ki == NULL) 20681ce91824SDavid Xu goto done; 20691ce91824SDavid Xu 2070dd85920aSJason Evans if (fp->f_type == DTYPE_VNODE) { 20713b6d9652SPoul-Henning Kamp vp = fp->f_vnode; 20727ad2a82dSMateusz Guzik if (vn_isdisk(vp)) { 20732a522eb9SJohn Baldwin fdrop(fp, td); 2074b40ce416SJulian Elischer td->td_retval[0] = AIO_NOTCANCELED; 2075ac41f2efSAlfred Perlstein return (0); 2076dd85920aSJason Evans } 2077dd85920aSJason Evans } 2078dd85920aSJason Evans 2079759ccccaSDavid Xu AIO_LOCK(ki); 20805652770dSJohn Baldwin TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) { 20815652770dSJohn Baldwin if ((uap->fd == job->uaiocb.aio_fildes) && 2082dd85920aSJason Evans ((uap->aiocbp == NULL) || 20835652770dSJohn Baldwin (uap->aiocbp == job->ujob))) { 2084f3215338SJohn Baldwin if (aio_cancel_job(p, ki, job)) { 20851ce91824SDavid Xu cancelled++; 2086dd85920aSJason Evans } else { 2087dd85920aSJason Evans notcancelled++; 2088dd85920aSJason Evans } 20891aa4c324SDavid Xu if (uap->aiocbp != NULL) 20901aa4c324SDavid Xu break; 2091dd85920aSJason Evans } 2092dd85920aSJason Evans } 2093759ccccaSDavid Xu AIO_UNLOCK(ki); 20941ce91824SDavid Xu 2095ad49abc0SAlan Cox done: 20962a522eb9SJohn Baldwin fdrop(fp, td); 20971aa4c324SDavid Xu 20981aa4c324SDavid Xu if (uap->aiocbp != NULL) { 2099dd85920aSJason Evans if (cancelled) { 2100b40ce416SJulian Elischer td->td_retval[0] = AIO_CANCELED; 2101ac41f2efSAlfred Perlstein return (0); 2102dd85920aSJason Evans } 21031aa4c324SDavid Xu } 21041aa4c324SDavid Xu 21051aa4c324SDavid Xu if (notcancelled) { 21061aa4c324SDavid Xu td->td_retval[0] = AIO_NOTCANCELED; 21071aa4c324SDavid Xu return (0); 21081aa4c324SDavid Xu } 21091aa4c324SDavid Xu 21101aa4c324SDavid Xu if (cancelled) { 21111aa4c324SDavid Xu td->td_retval[0] = AIO_CANCELED; 21121aa4c324SDavid Xu return (0); 21131aa4c324SDavid Xu } 21141aa4c324SDavid Xu 2115b40ce416SJulian Elischer td->td_retval[0] = AIO_ALLDONE; 2116dd85920aSJason Evans 2117ac41f2efSAlfred Perlstein return (0); 2118ee877a35SJohn Dyson } 2119ee877a35SJohn Dyson 2120ee877a35SJohn Dyson /* 2121873fbcd7SRobert Watson * aio_error is implemented in the kernel level for compatibility purposes 2122873fbcd7SRobert Watson * only. For a user mode async implementation, it would be best to do it in 2123873fbcd7SRobert Watson * a userland subroutine. 2124ee877a35SJohn Dyson */ 21253858a1f4SJohn Baldwin static int 21265652770dSJohn Baldwin kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops) 2127fd3bf775SJohn Dyson { 2128b40ce416SJulian Elischer struct proc *p = td->td_proc; 21295652770dSJohn Baldwin struct kaiocb *job; 21302244ea07SJohn Dyson struct kaioinfo *ki; 21311ce91824SDavid Xu int status; 2132ee877a35SJohn Dyson 21332244ea07SJohn Dyson ki = p->p_aioinfo; 21341ce91824SDavid Xu if (ki == NULL) { 21351ce91824SDavid Xu td->td_retval[0] = EINVAL; 21361ce91824SDavid Xu return (0); 21371ce91824SDavid Xu } 2138ee877a35SJohn Dyson 2139759ccccaSDavid Xu AIO_LOCK(ki); 21405652770dSJohn Baldwin TAILQ_FOREACH(job, &ki->kaio_all, allist) { 21415652770dSJohn Baldwin if (job->ujob == ujob) { 2142f3215338SJohn Baldwin if (job->jobflags & KAIOCB_FINISHED) 21431ce91824SDavid Xu td->td_retval[0] = 21445652770dSJohn Baldwin job->uaiocb._aiocb_private.error; 21451ce91824SDavid Xu else 2146b40ce416SJulian Elischer td->td_retval[0] = EINPROGRESS; 2147759ccccaSDavid Xu AIO_UNLOCK(ki); 2148ac41f2efSAlfred Perlstein return (0); 21492244ea07SJohn Dyson } 21502244ea07SJohn Dyson } 2151759ccccaSDavid Xu AIO_UNLOCK(ki); 215284af4da6SJohn Dyson 21532244ea07SJohn Dyson /* 2154a9bf5e37SDavid Xu * Hack for failure of aio_aqueue. 21552244ea07SJohn Dyson */ 21565652770dSJohn Baldwin status = ops->fetch_status(ujob); 21571ce91824SDavid Xu if (status == -1) { 21585652770dSJohn Baldwin td->td_retval[0] = ops->fetch_error(ujob); 21591ce91824SDavid Xu return (0); 21601ce91824SDavid Xu } 21611ce91824SDavid Xu 21621ce91824SDavid Xu td->td_retval[0] = EINVAL; 21631ce91824SDavid Xu return (0); 2164ee877a35SJohn Dyson } 2165ee877a35SJohn Dyson 21663858a1f4SJohn Baldwin int 21678451d0ddSKip Macy sys_aio_error(struct thread *td, struct aio_error_args *uap) 21683858a1f4SJohn Baldwin { 21693858a1f4SJohn Baldwin 21703858a1f4SJohn Baldwin return (kern_aio_error(td, uap->aiocbp, &aiocb_ops)); 21713858a1f4SJohn Baldwin } 21723858a1f4SJohn Baldwin 2173eb8e6d52SEivind Eklund /* syscall - asynchronous read from a file (REALTIME) */ 2174399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 2175ee877a35SJohn Dyson int 2176399e8c17SJohn Baldwin freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap) 21770972628aSDavid Xu { 21780972628aSDavid Xu 21793858a1f4SJohn Baldwin return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 21803858a1f4SJohn Baldwin &aiocb_ops_osigevent)); 21810972628aSDavid Xu } 2182399e8c17SJohn Baldwin #endif 21830972628aSDavid Xu 21840972628aSDavid Xu int 21858451d0ddSKip Macy sys_aio_read(struct thread *td, struct aio_read_args *uap) 2186fd3bf775SJohn Dyson { 218721d56e9cSAlfred Perlstein 21883858a1f4SJohn Baldwin return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops)); 2189ee877a35SJohn Dyson } 2190ee877a35SJohn Dyson 2191022ca2fcSAlan Somers int 2192022ca2fcSAlan Somers sys_aio_readv(struct thread *td, struct aio_readv_args *uap) 2193022ca2fcSAlan Somers { 2194022ca2fcSAlan Somers 2195022ca2fcSAlan Somers return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops)); 2196022ca2fcSAlan Somers } 2197022ca2fcSAlan Somers 2198eb8e6d52SEivind Eklund /* syscall - asynchronous write to a file (REALTIME) */ 2199399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 2200ee877a35SJohn Dyson int 2201399e8c17SJohn Baldwin freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap) 22020972628aSDavid Xu { 22030972628aSDavid Xu 22043858a1f4SJohn Baldwin return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 22053858a1f4SJohn Baldwin &aiocb_ops_osigevent)); 22060972628aSDavid Xu } 2207399e8c17SJohn Baldwin #endif 22080972628aSDavid Xu 22090972628aSDavid Xu int 22108451d0ddSKip Macy sys_aio_write(struct thread *td, struct aio_write_args *uap) 2211fd3bf775SJohn Dyson { 221221d56e9cSAlfred Perlstein 22133858a1f4SJohn Baldwin return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops)); 22140972628aSDavid Xu } 22150972628aSDavid Xu 22166160e12cSGleb Smirnoff int 2217022ca2fcSAlan Somers sys_aio_writev(struct thread *td, struct aio_writev_args *uap) 2218022ca2fcSAlan Somers { 2219022ca2fcSAlan Somers 2220022ca2fcSAlan Somers return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops)); 2221022ca2fcSAlan Somers } 2222022ca2fcSAlan Somers 2223022ca2fcSAlan Somers int 22246160e12cSGleb Smirnoff sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap) 22256160e12cSGleb Smirnoff { 22266160e12cSGleb Smirnoff 22276160e12cSGleb Smirnoff return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops)); 22286160e12cSGleb Smirnoff } 22296160e12cSGleb Smirnoff 22300972628aSDavid Xu static int 22313858a1f4SJohn Baldwin kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list, 22323858a1f4SJohn Baldwin struct aiocb **acb_list, int nent, struct sigevent *sig, 22333858a1f4SJohn Baldwin struct aiocb_ops *ops) 22340972628aSDavid Xu { 2235b40ce416SJulian Elischer struct proc *p = td->td_proc; 22365652770dSJohn Baldwin struct aiocb *job; 22372244ea07SJohn Dyson struct kaioinfo *ki; 22381ce91824SDavid Xu struct aioliojob *lj; 223969cd28daSDoug Ambrisko struct kevent kev; 22401ce91824SDavid Xu int error; 224152c09831SAlan Somers int nagain, nerror; 2242ee877a35SJohn Dyson int i; 2243ee877a35SJohn Dyson 22443858a1f4SJohn Baldwin if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT)) 2245ac41f2efSAlfred Perlstein return (EINVAL); 22462244ea07SJohn Dyson 2247913b9329SAlan Somers if (nent < 0 || nent > max_aio_queue_per_proc) 2248ac41f2efSAlfred Perlstein return (EINVAL); 22492244ea07SJohn Dyson 2250bfbbc4aaSJason Evans if (p->p_aioinfo == NULL) 22512244ea07SJohn Dyson aio_init_aioinfo(p); 22522244ea07SJohn Dyson 22532244ea07SJohn Dyson ki = p->p_aioinfo; 22542244ea07SJohn Dyson 2255a163d034SWarner Losh lj = uma_zalloc(aiolio_zone, M_WAITOK); 225684af4da6SJohn Dyson lj->lioj_flags = 0; 22571ce91824SDavid Xu lj->lioj_count = 0; 22581ce91824SDavid Xu lj->lioj_finished_count = 0; 22592e5f6152SMark Johnston lj->lioj_signal.sigev_notify = SIGEV_NONE; 2260d8b0556cSKonstantin Belousov knlist_init_mtx(&lj->klist, AIO_MTX(ki)); 22614c0fb2cfSDavid Xu ksiginfo_init(&lj->lioj_ksi); 226269cd28daSDoug Ambrisko 226384af4da6SJohn Dyson /* 2264bfbbc4aaSJason Evans * Setup signal. 226584af4da6SJohn Dyson */ 22663858a1f4SJohn Baldwin if (sig && (mode == LIO_NOWAIT)) { 22673858a1f4SJohn Baldwin bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal)); 226869cd28daSDoug Ambrisko if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 226969cd28daSDoug Ambrisko /* Assume only new style KEVENT */ 227036c4960eSMark Johnston memset(&kev, 0, sizeof(kev)); 227169cd28daSDoug Ambrisko kev.filter = EVFILT_LIO; 227269cd28daSDoug Ambrisko kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; 22733858a1f4SJohn Baldwin kev.ident = (uintptr_t)uacb_list; /* something unique */ 227469cd28daSDoug Ambrisko kev.data = (intptr_t)lj; 22751ce91824SDavid Xu /* pass user defined sigval data */ 22761ce91824SDavid Xu kev.udata = lj->lioj_signal.sigev_value.sival_ptr; 22774db71d27SJohn-Mark Gurney error = kqfd_register( 2278792843c3SMark Johnston lj->lioj_signal.sigev_notify_kqueue, &kev, td, 2279792843c3SMark Johnston M_WAITOK); 228069cd28daSDoug Ambrisko if (error) { 228169cd28daSDoug Ambrisko uma_zfree(aiolio_zone, lj); 228269cd28daSDoug Ambrisko return (error); 228369cd28daSDoug Ambrisko } 22841ce91824SDavid Xu } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) { 22851ce91824SDavid Xu ; 228668d71118SDavid Xu } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 228768d71118SDavid Xu lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) { 228868d71118SDavid Xu if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) { 228969cd28daSDoug Ambrisko uma_zfree(aiolio_zone, lj); 229069cd28daSDoug Ambrisko return EINVAL; 229168d71118SDavid Xu } 229284af4da6SJohn Dyson lj->lioj_flags |= LIOJ_SIGNAL; 229368d71118SDavid Xu } else { 229468d71118SDavid Xu uma_zfree(aiolio_zone, lj); 229568d71118SDavid Xu return EINVAL; 22964d752b01SAlan Cox } 22971ce91824SDavid Xu } 229869cd28daSDoug Ambrisko 2299759ccccaSDavid Xu AIO_LOCK(ki); 23002f3cf918SAlfred Perlstein TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); 23012244ea07SJohn Dyson /* 23021ce91824SDavid Xu * Add extra aiocb count to avoid the lio to be freed 23031ce91824SDavid Xu * by other threads doing aio_waitcomplete or aio_return, 23041ce91824SDavid Xu * and prevent event from being sent until we have queued 23051ce91824SDavid Xu * all tasks. 23061ce91824SDavid Xu */ 23071ce91824SDavid Xu lj->lioj_count = 1; 2308759ccccaSDavid Xu AIO_UNLOCK(ki); 23091ce91824SDavid Xu 23101ce91824SDavid Xu /* 2311bfbbc4aaSJason Evans * Get pointers to the list of I/O requests. 23122244ea07SJohn Dyson */ 231352c09831SAlan Somers nagain = 0; 2314fd3bf775SJohn Dyson nerror = 0; 23153858a1f4SJohn Baldwin for (i = 0; i < nent; i++) { 23165652770dSJohn Baldwin job = acb_list[i]; 23175652770dSJohn Baldwin if (job != NULL) { 23185652770dSJohn Baldwin error = aio_aqueue(td, job, lj, LIO_NOP, ops); 231952c09831SAlan Somers if (error == EAGAIN) 232052c09831SAlan Somers nagain++; 232152c09831SAlan Somers else if (error != 0) 2322fd3bf775SJohn Dyson nerror++; 2323fd3bf775SJohn Dyson } 2324fd3bf775SJohn Dyson } 23252244ea07SJohn Dyson 23261ce91824SDavid Xu error = 0; 2327759ccccaSDavid Xu AIO_LOCK(ki); 23283858a1f4SJohn Baldwin if (mode == LIO_WAIT) { 23291ce91824SDavid Xu while (lj->lioj_count - 1 != lj->lioj_finished_count) { 2330fd3bf775SJohn Dyson ki->kaio_flags |= KAIO_WAKEUP; 2331759ccccaSDavid Xu error = msleep(&p->p_aioinfo, AIO_MTX(ki), 23321ce91824SDavid Xu PRIBIO | PCATCH, "aiospn", 0); 23331ce91824SDavid Xu if (error == ERESTART) 23341ce91824SDavid Xu error = EINTR; 23351ce91824SDavid Xu if (error) 23361ce91824SDavid Xu break; 23371ce91824SDavid Xu } 23381ce91824SDavid Xu } else { 23391ce91824SDavid Xu if (lj->lioj_count - 1 == lj->lioj_finished_count) { 23401ce91824SDavid Xu if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 23411ce91824SDavid Xu lj->lioj_flags |= LIOJ_KEVENT_POSTED; 23421ce91824SDavid Xu KNOTE_LOCKED(&lj->klist, 1); 23431ce91824SDavid Xu } 234429331656SKonstantin Belousov if ((lj->lioj_flags & (LIOJ_SIGNAL | 234529331656SKonstantin Belousov LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL && 234629331656SKonstantin Belousov (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 23471ce91824SDavid Xu lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) { 23486814c2daSKonstantin Belousov aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi, 23496814c2daSKonstantin Belousov lj->lioj_count != 1); 23501ce91824SDavid Xu lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 23512244ea07SJohn Dyson } 23522244ea07SJohn Dyson } 23531ce91824SDavid Xu } 23541ce91824SDavid Xu lj->lioj_count--; 23551ce91824SDavid Xu if (lj->lioj_count == 0) { 23561ce91824SDavid Xu TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 23571ce91824SDavid Xu knlist_delete(&lj->klist, curthread, 1); 2358759ccccaSDavid Xu PROC_LOCK(p); 23591ce91824SDavid Xu sigqueue_take(&lj->lioj_ksi); 23601ce91824SDavid Xu PROC_UNLOCK(p); 2361759ccccaSDavid Xu AIO_UNLOCK(ki); 23621ce91824SDavid Xu uma_zfree(aiolio_zone, lj); 23631ce91824SDavid Xu } else 2364759ccccaSDavid Xu AIO_UNLOCK(ki); 23652244ea07SJohn Dyson 23661ce91824SDavid Xu if (nerror) 23671ce91824SDavid Xu return (EIO); 236852c09831SAlan Somers else if (nagain) 236952c09831SAlan Somers return (EAGAIN); 237052c09831SAlan Somers else 23711ce91824SDavid Xu return (error); 2372ee877a35SJohn Dyson } 2373fd3bf775SJohn Dyson 23743858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */ 2375399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 23763858a1f4SJohn Baldwin int 2377399e8c17SJohn Baldwin freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap) 23783858a1f4SJohn Baldwin { 23793858a1f4SJohn Baldwin struct aiocb **acb_list; 23803858a1f4SJohn Baldwin struct sigevent *sigp, sig; 23813858a1f4SJohn Baldwin struct osigevent osig; 23823858a1f4SJohn Baldwin int error, nent; 23833858a1f4SJohn Baldwin 23843858a1f4SJohn Baldwin if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 23853858a1f4SJohn Baldwin return (EINVAL); 23863858a1f4SJohn Baldwin 23873858a1f4SJohn Baldwin nent = uap->nent; 2388913b9329SAlan Somers if (nent < 0 || nent > max_aio_queue_per_proc) 23893858a1f4SJohn Baldwin return (EINVAL); 23903858a1f4SJohn Baldwin 23913858a1f4SJohn Baldwin if (uap->sig && (uap->mode == LIO_NOWAIT)) { 23923858a1f4SJohn Baldwin error = copyin(uap->sig, &osig, sizeof(osig)); 23933858a1f4SJohn Baldwin if (error) 23943858a1f4SJohn Baldwin return (error); 23953858a1f4SJohn Baldwin error = convert_old_sigevent(&osig, &sig); 23963858a1f4SJohn Baldwin if (error) 23973858a1f4SJohn Baldwin return (error); 23983858a1f4SJohn Baldwin sigp = &sig; 23993858a1f4SJohn Baldwin } else 24003858a1f4SJohn Baldwin sigp = NULL; 24013858a1f4SJohn Baldwin 24023858a1f4SJohn Baldwin acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 24033858a1f4SJohn Baldwin error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 24043858a1f4SJohn Baldwin if (error == 0) 24053858a1f4SJohn Baldwin error = kern_lio_listio(td, uap->mode, 24063858a1f4SJohn Baldwin (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 24073858a1f4SJohn Baldwin &aiocb_ops_osigevent); 24083858a1f4SJohn Baldwin free(acb_list, M_LIO); 24093858a1f4SJohn Baldwin return (error); 24103858a1f4SJohn Baldwin } 2411399e8c17SJohn Baldwin #endif 24123858a1f4SJohn Baldwin 24133858a1f4SJohn Baldwin /* syscall - list directed I/O (REALTIME) */ 24143858a1f4SJohn Baldwin int 24158451d0ddSKip Macy sys_lio_listio(struct thread *td, struct lio_listio_args *uap) 24163858a1f4SJohn Baldwin { 24173858a1f4SJohn Baldwin struct aiocb **acb_list; 24183858a1f4SJohn Baldwin struct sigevent *sigp, sig; 24193858a1f4SJohn Baldwin int error, nent; 24203858a1f4SJohn Baldwin 24213858a1f4SJohn Baldwin if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 24223858a1f4SJohn Baldwin return (EINVAL); 24233858a1f4SJohn Baldwin 24243858a1f4SJohn Baldwin nent = uap->nent; 2425913b9329SAlan Somers if (nent < 0 || nent > max_aio_queue_per_proc) 24263858a1f4SJohn Baldwin return (EINVAL); 24273858a1f4SJohn Baldwin 24283858a1f4SJohn Baldwin if (uap->sig && (uap->mode == LIO_NOWAIT)) { 24293858a1f4SJohn Baldwin error = copyin(uap->sig, &sig, sizeof(sig)); 24303858a1f4SJohn Baldwin if (error) 24313858a1f4SJohn Baldwin return (error); 24323858a1f4SJohn Baldwin sigp = &sig; 24333858a1f4SJohn Baldwin } else 24343858a1f4SJohn Baldwin sigp = NULL; 24353858a1f4SJohn Baldwin 24363858a1f4SJohn Baldwin acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 24373858a1f4SJohn Baldwin error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 24383858a1f4SJohn Baldwin if (error == 0) 24393858a1f4SJohn Baldwin error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list, 24403858a1f4SJohn Baldwin nent, sigp, &aiocb_ops); 24413858a1f4SJohn Baldwin free(acb_list, M_LIO); 24423858a1f4SJohn Baldwin return (error); 24433858a1f4SJohn Baldwin } 24443858a1f4SJohn Baldwin 2445fd3bf775SJohn Dyson static void 2446022ca2fcSAlan Somers aio_biocleanup(struct bio *bp) 2447fd3bf775SJohn Dyson { 24485652770dSJohn Baldwin struct kaiocb *job = (struct kaiocb *)bp->bio_caller1; 244927b8220dSDavid Xu struct kaioinfo *ki; 245001206038SAlan Somers struct buf *pbuf = (struct buf *)bp->bio_caller2; 24511ce91824SDavid Xu 2452f743d981SAlexander Motin /* Release mapping into kernel space. */ 245301206038SAlan Somers if (pbuf != NULL) { 245401206038SAlan Somers MPASS(pbuf->b_npages <= atop(maxphys) + 1); 245501206038SAlan Somers pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages); 245601206038SAlan Somers vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages); 245701206038SAlan Somers uma_zfree(pbuf_zone, pbuf); 2458f743d981SAlexander Motin atomic_subtract_int(&num_buf_aio, 1); 2459a9d4fe97SKonstantin Belousov ki = job->userproc->p_aioinfo; 2460f3215338SJohn Baldwin AIO_LOCK(ki); 2461f3215338SJohn Baldwin ki->kaio_buffer_count--; 2462f3215338SJohn Baldwin AIO_UNLOCK(ki); 2463cd853791SKonstantin Belousov } else { 246401206038SAlan Somers MPASS(bp->bio_ma_n <= atop(maxphys) + 1); 246501206038SAlan Somers vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n); 246601206038SAlan Somers free(bp->bio_ma, M_TEMP); 24678091e52bSJohn Baldwin atomic_subtract_int(&num_unmapped_aio, 1); 2468cd853791SKonstantin Belousov } 2469f743d981SAlexander Motin g_destroy_bio(bp); 247084af4da6SJohn Dyson } 2471bfbbc4aaSJason Evans 2472022ca2fcSAlan Somers static void 2473022ca2fcSAlan Somers aio_biowakeup(struct bio *bp) 2474022ca2fcSAlan Somers { 2475022ca2fcSAlan Somers struct kaiocb *job = (struct kaiocb *)bp->bio_caller1; 2476022ca2fcSAlan Somers size_t nbytes; 2477022ca2fcSAlan Somers long bcount = bp->bio_bcount; 2478022ca2fcSAlan Somers long resid = bp->bio_resid; 2479022ca2fcSAlan Somers int error, opcode, nblks; 2480022ca2fcSAlan Somers int bio_error = bp->bio_error; 2481022ca2fcSAlan Somers uint16_t flags = bp->bio_flags; 2482022ca2fcSAlan Somers 2483022ca2fcSAlan Somers opcode = job->uaiocb.aio_lio_opcode; 2484022ca2fcSAlan Somers 2485022ca2fcSAlan Somers aio_biocleanup(bp); 2486022ca2fcSAlan Somers 2487022ca2fcSAlan Somers nbytes =bcount - resid; 2488022ca2fcSAlan Somers atomic_add_acq_long(&job->nbytes, nbytes); 2489022ca2fcSAlan Somers nblks = btodb(nbytes); 2490022ca2fcSAlan Somers error = 0; 2491022ca2fcSAlan Somers /* 2492022ca2fcSAlan Somers * If multiple bios experienced an error, the job will reflect the 2493022ca2fcSAlan Somers * error of whichever failed bio completed last. 2494022ca2fcSAlan Somers */ 2495022ca2fcSAlan Somers if (flags & BIO_ERROR) 2496022ca2fcSAlan Somers atomic_set_int(&job->error, bio_error); 24972247f489SAlan Somers if (opcode & LIO_WRITE) 2498022ca2fcSAlan Somers atomic_add_int(&job->outblock, nblks); 2499022ca2fcSAlan Somers else 2500022ca2fcSAlan Somers atomic_add_int(&job->inblock, nblks); 2501022ca2fcSAlan Somers atomic_subtract_int(&job->nbio, 1); 2502022ca2fcSAlan Somers 2503022ca2fcSAlan Somers 2504022ca2fcSAlan Somers if (atomic_load_int(&job->nbio) == 0) { 2505022ca2fcSAlan Somers if (atomic_load_int(&job->error)) 2506022ca2fcSAlan Somers aio_complete(job, -1, job->error); 2507022ca2fcSAlan Somers else 2508022ca2fcSAlan Somers aio_complete(job, atomic_load_long(&job->nbytes), 0); 2509022ca2fcSAlan Somers } 2510022ca2fcSAlan Somers } 2511022ca2fcSAlan Somers 2512eb8e6d52SEivind Eklund /* syscall - wait for the next completion of an aio request */ 25133858a1f4SJohn Baldwin static int 25145652770dSJohn Baldwin kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp, 25153858a1f4SJohn Baldwin struct timespec *ts, struct aiocb_ops *ops) 2516bfbbc4aaSJason Evans { 2517b40ce416SJulian Elischer struct proc *p = td->td_proc; 2518bfbbc4aaSJason Evans struct timeval atv; 2519bfbbc4aaSJason Evans struct kaioinfo *ki; 25205652770dSJohn Baldwin struct kaiocb *job; 25215652770dSJohn Baldwin struct aiocb *ujob; 2522bb430bc7SJohn Baldwin long error, status; 2523bb430bc7SJohn Baldwin int timo; 2524bfbbc4aaSJason Evans 25255652770dSJohn Baldwin ops->store_aiocb(ujobp, NULL); 2526dd85920aSJason Evans 252738d68e2dSPawel Jakub Dawidek if (ts == NULL) { 2528bfbbc4aaSJason Evans timo = 0; 252938d68e2dSPawel Jakub Dawidek } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) { 253038d68e2dSPawel Jakub Dawidek timo = -1; 253138d68e2dSPawel Jakub Dawidek } else { 25323858a1f4SJohn Baldwin if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000)) 2533bfbbc4aaSJason Evans return (EINVAL); 2534bfbbc4aaSJason Evans 25353858a1f4SJohn Baldwin TIMESPEC_TO_TIMEVAL(&atv, ts); 2536bfbbc4aaSJason Evans if (itimerfix(&atv)) 2537bfbbc4aaSJason Evans return (EINVAL); 2538bfbbc4aaSJason Evans timo = tvtohz(&atv); 2539bfbbc4aaSJason Evans } 2540bfbbc4aaSJason Evans 25418213baf0SChristian S.J. Peron if (p->p_aioinfo == NULL) 2542323fe565SDavid Xu aio_init_aioinfo(p); 25438213baf0SChristian S.J. Peron ki = p->p_aioinfo; 2544bfbbc4aaSJason Evans 25451ce91824SDavid Xu error = 0; 25465652770dSJohn Baldwin job = NULL; 2547759ccccaSDavid Xu AIO_LOCK(ki); 25485652770dSJohn Baldwin while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) { 254938d68e2dSPawel Jakub Dawidek if (timo == -1) { 255038d68e2dSPawel Jakub Dawidek error = EWOULDBLOCK; 255138d68e2dSPawel Jakub Dawidek break; 255238d68e2dSPawel Jakub Dawidek } 25531ce91824SDavid Xu ki->kaio_flags |= KAIO_WAKEUP; 2554759ccccaSDavid Xu error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 25551ce91824SDavid Xu "aiowc", timo); 255627b8220dSDavid Xu if (timo && error == ERESTART) 25571ce91824SDavid Xu error = EINTR; 25581ce91824SDavid Xu if (error) 25591ce91824SDavid Xu break; 25601ce91824SDavid Xu } 25611ce91824SDavid Xu 25625652770dSJohn Baldwin if (job != NULL) { 2563f3215338SJohn Baldwin MPASS(job->jobflags & KAIOCB_FINISHED); 25645652770dSJohn Baldwin ujob = job->ujob; 25655652770dSJohn Baldwin status = job->uaiocb._aiocb_private.status; 25665652770dSJohn Baldwin error = job->uaiocb._aiocb_private.error; 25671ce91824SDavid Xu td->td_retval[0] = status; 2568b1012d80SJohn Baldwin td->td_ru.ru_oublock += job->outblock; 2569b1012d80SJohn Baldwin td->td_ru.ru_inblock += job->inblock; 2570b1012d80SJohn Baldwin td->td_ru.ru_msgsnd += job->msgsnd; 2571b1012d80SJohn Baldwin td->td_ru.ru_msgrcv += job->msgrcv; 25725652770dSJohn Baldwin aio_free_entry(job); 2573759ccccaSDavid Xu AIO_UNLOCK(ki); 25745652770dSJohn Baldwin ops->store_aiocb(ujobp, ujob); 25755652770dSJohn Baldwin ops->store_error(ujob, error); 25765652770dSJohn Baldwin ops->store_status(ujob, status); 25771ce91824SDavid Xu } else 2578759ccccaSDavid Xu AIO_UNLOCK(ki); 2579bfbbc4aaSJason Evans 2580ac41f2efSAlfred Perlstein return (error); 2581bfbbc4aaSJason Evans } 2582cb679c38SJonathan Lemon 258399eee864SDavid Xu int 25848451d0ddSKip Macy sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap) 25853858a1f4SJohn Baldwin { 25863858a1f4SJohn Baldwin struct timespec ts, *tsp; 25873858a1f4SJohn Baldwin int error; 25883858a1f4SJohn Baldwin 25893858a1f4SJohn Baldwin if (uap->timeout) { 25903858a1f4SJohn Baldwin /* Get timespec struct. */ 25913858a1f4SJohn Baldwin error = copyin(uap->timeout, &ts, sizeof(ts)); 25923858a1f4SJohn Baldwin if (error) 25933858a1f4SJohn Baldwin return (error); 25943858a1f4SJohn Baldwin tsp = &ts; 25953858a1f4SJohn Baldwin } else 25963858a1f4SJohn Baldwin tsp = NULL; 25973858a1f4SJohn Baldwin 25983858a1f4SJohn Baldwin return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops)); 25993858a1f4SJohn Baldwin } 26003858a1f4SJohn Baldwin 26013858a1f4SJohn Baldwin static int 26025652770dSJohn Baldwin kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob, 26033858a1f4SJohn Baldwin struct aiocb_ops *ops) 260499eee864SDavid Xu { 2605801ac943SThomas Munro int listop; 260699eee864SDavid Xu 2607801ac943SThomas Munro switch (op) { 2608801ac943SThomas Munro case O_SYNC: 2609801ac943SThomas Munro listop = LIO_SYNC; 2610801ac943SThomas Munro break; 2611801ac943SThomas Munro case O_DSYNC: 2612801ac943SThomas Munro listop = LIO_DSYNC; 2613801ac943SThomas Munro break; 2614801ac943SThomas Munro default: 261599eee864SDavid Xu return (EINVAL); 2616801ac943SThomas Munro } 2617801ac943SThomas Munro 2618801ac943SThomas Munro return (aio_aqueue(td, ujob, NULL, listop, ops)); 26193858a1f4SJohn Baldwin } 26203858a1f4SJohn Baldwin 26213858a1f4SJohn Baldwin int 26228451d0ddSKip Macy sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap) 26233858a1f4SJohn Baldwin { 26243858a1f4SJohn Baldwin 26253858a1f4SJohn Baldwin return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops)); 262699eee864SDavid Xu } 262799eee864SDavid Xu 2628eb8e6d52SEivind Eklund /* kqueue attach function */ 2629cb679c38SJonathan Lemon static int 2630cb679c38SJonathan Lemon filt_aioattach(struct knote *kn) 2631cb679c38SJonathan Lemon { 26322b34e843SKonstantin Belousov struct kaiocb *job; 26332b34e843SKonstantin Belousov 26342b34e843SKonstantin Belousov job = (struct kaiocb *)(uintptr_t)kn->kn_sdata; 2635cb679c38SJonathan Lemon 2636cb679c38SJonathan Lemon /* 26375652770dSJohn Baldwin * The job pointer must be validated before using it, so 2638cb679c38SJonathan Lemon * registration is restricted to the kernel; the user cannot 2639cb679c38SJonathan Lemon * set EV_FLAG1. 2640cb679c38SJonathan Lemon */ 2641cb679c38SJonathan Lemon if ((kn->kn_flags & EV_FLAG1) == 0) 2642cb679c38SJonathan Lemon return (EPERM); 26435652770dSJohn Baldwin kn->kn_ptr.p_aio = job; 2644cb679c38SJonathan Lemon kn->kn_flags &= ~EV_FLAG1; 2645cb679c38SJonathan Lemon 26465652770dSJohn Baldwin knlist_add(&job->klist, kn, 0); 2647cb679c38SJonathan Lemon 2648cb679c38SJonathan Lemon return (0); 2649cb679c38SJonathan Lemon } 2650cb679c38SJonathan Lemon 2651eb8e6d52SEivind Eklund /* kqueue detach function */ 2652cb679c38SJonathan Lemon static void 2653cb679c38SJonathan Lemon filt_aiodetach(struct knote *kn) 2654cb679c38SJonathan Lemon { 26558e9fc278SDoug Ambrisko struct knlist *knl; 2656cb679c38SJonathan Lemon 26578e9fc278SDoug Ambrisko knl = &kn->kn_ptr.p_aio->klist; 26588e9fc278SDoug Ambrisko knl->kl_lock(knl->kl_lockarg); 26598e9fc278SDoug Ambrisko if (!knlist_empty(knl)) 26608e9fc278SDoug Ambrisko knlist_remove(knl, kn, 1); 26618e9fc278SDoug Ambrisko knl->kl_unlock(knl->kl_lockarg); 2662cb679c38SJonathan Lemon } 2663cb679c38SJonathan Lemon 2664eb8e6d52SEivind Eklund /* kqueue filter function */ 2665cb679c38SJonathan Lemon /*ARGSUSED*/ 2666cb679c38SJonathan Lemon static int 2667cb679c38SJonathan Lemon filt_aio(struct knote *kn, long hint) 2668cb679c38SJonathan Lemon { 26695652770dSJohn Baldwin struct kaiocb *job = kn->kn_ptr.p_aio; 2670cb679c38SJonathan Lemon 26715652770dSJohn Baldwin kn->kn_data = job->uaiocb._aiocb_private.error; 2672f3215338SJohn Baldwin if (!(job->jobflags & KAIOCB_FINISHED)) 2673cb679c38SJonathan Lemon return (0); 2674cb679c38SJonathan Lemon kn->kn_flags |= EV_EOF; 2675cb679c38SJonathan Lemon return (1); 2676cb679c38SJonathan Lemon } 267769cd28daSDoug Ambrisko 267869cd28daSDoug Ambrisko /* kqueue attach function */ 267969cd28daSDoug Ambrisko static int 268069cd28daSDoug Ambrisko filt_lioattach(struct knote *kn) 268169cd28daSDoug Ambrisko { 26822b34e843SKonstantin Belousov struct aioliojob *lj; 26832b34e843SKonstantin Belousov 26842b34e843SKonstantin Belousov lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata; 268569cd28daSDoug Ambrisko 268669cd28daSDoug Ambrisko /* 26871ce91824SDavid Xu * The aioliojob pointer must be validated before using it, so 268869cd28daSDoug Ambrisko * registration is restricted to the kernel; the user cannot 268969cd28daSDoug Ambrisko * set EV_FLAG1. 269069cd28daSDoug Ambrisko */ 269169cd28daSDoug Ambrisko if ((kn->kn_flags & EV_FLAG1) == 0) 269269cd28daSDoug Ambrisko return (EPERM); 2693a8afa221SJean-Sébastien Pédron kn->kn_ptr.p_lio = lj; 269469cd28daSDoug Ambrisko kn->kn_flags &= ~EV_FLAG1; 269569cd28daSDoug Ambrisko 269669cd28daSDoug Ambrisko knlist_add(&lj->klist, kn, 0); 269769cd28daSDoug Ambrisko 269869cd28daSDoug Ambrisko return (0); 269969cd28daSDoug Ambrisko } 270069cd28daSDoug Ambrisko 270169cd28daSDoug Ambrisko /* kqueue detach function */ 270269cd28daSDoug Ambrisko static void 270369cd28daSDoug Ambrisko filt_liodetach(struct knote *kn) 270469cd28daSDoug Ambrisko { 27058e9fc278SDoug Ambrisko struct knlist *knl; 270669cd28daSDoug Ambrisko 27078e9fc278SDoug Ambrisko knl = &kn->kn_ptr.p_lio->klist; 27088e9fc278SDoug Ambrisko knl->kl_lock(knl->kl_lockarg); 27098e9fc278SDoug Ambrisko if (!knlist_empty(knl)) 27108e9fc278SDoug Ambrisko knlist_remove(knl, kn, 1); 27118e9fc278SDoug Ambrisko knl->kl_unlock(knl->kl_lockarg); 271269cd28daSDoug Ambrisko } 271369cd28daSDoug Ambrisko 271469cd28daSDoug Ambrisko /* kqueue filter function */ 271569cd28daSDoug Ambrisko /*ARGSUSED*/ 271669cd28daSDoug Ambrisko static int 271769cd28daSDoug Ambrisko filt_lio(struct knote *kn, long hint) 271869cd28daSDoug Ambrisko { 2719a8afa221SJean-Sébastien Pédron struct aioliojob * lj = kn->kn_ptr.p_lio; 27201ce91824SDavid Xu 272169cd28daSDoug Ambrisko return (lj->lioj_flags & LIOJ_KEVENT_POSTED); 272269cd28daSDoug Ambrisko } 27233858a1f4SJohn Baldwin 2724841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2725399e8c17SJohn Baldwin #include <sys/mount.h> 2726399e8c17SJohn Baldwin #include <sys/socket.h> 2727399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32.h> 2728399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_proto.h> 2729399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h> 2730399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_syscall.h> 2731399e8c17SJohn Baldwin #include <compat/freebsd32/freebsd32_util.h> 27323858a1f4SJohn Baldwin 27333858a1f4SJohn Baldwin struct __aiocb_private32 { 27343858a1f4SJohn Baldwin int32_t status; 27353858a1f4SJohn Baldwin int32_t error; 27363858a1f4SJohn Baldwin uint32_t kernelinfo; 27373858a1f4SJohn Baldwin }; 27383858a1f4SJohn Baldwin 2739399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 27403858a1f4SJohn Baldwin typedef struct oaiocb32 { 27413858a1f4SJohn Baldwin int aio_fildes; /* File descriptor */ 27423858a1f4SJohn Baldwin uint64_t aio_offset __packed; /* File offset for I/O */ 27433858a1f4SJohn Baldwin uint32_t aio_buf; /* I/O buffer in process space */ 27443858a1f4SJohn Baldwin uint32_t aio_nbytes; /* Number of bytes for I/O */ 27453858a1f4SJohn Baldwin struct osigevent32 aio_sigevent; /* Signal to deliver */ 27463858a1f4SJohn Baldwin int aio_lio_opcode; /* LIO opcode */ 27473858a1f4SJohn Baldwin int aio_reqprio; /* Request priority -- ignored */ 27483858a1f4SJohn Baldwin struct __aiocb_private32 _aiocb_private; 27493858a1f4SJohn Baldwin } oaiocb32_t; 2750399e8c17SJohn Baldwin #endif 27513858a1f4SJohn Baldwin 27523858a1f4SJohn Baldwin typedef struct aiocb32 { 27533858a1f4SJohn Baldwin int32_t aio_fildes; /* File descriptor */ 27543858a1f4SJohn Baldwin uint64_t aio_offset __packed; /* File offset for I/O */ 27553858a1f4SJohn Baldwin uint32_t aio_buf; /* I/O buffer in process space */ 27563858a1f4SJohn Baldwin uint32_t aio_nbytes; /* Number of bytes for I/O */ 27573858a1f4SJohn Baldwin int __spare__[2]; 27583858a1f4SJohn Baldwin uint32_t __spare2__; 27593858a1f4SJohn Baldwin int aio_lio_opcode; /* LIO opcode */ 27603858a1f4SJohn Baldwin int aio_reqprio; /* Request priority -- ignored */ 27613858a1f4SJohn Baldwin struct __aiocb_private32 _aiocb_private; 27623858a1f4SJohn Baldwin struct sigevent32 aio_sigevent; /* Signal to deliver */ 27633858a1f4SJohn Baldwin } aiocb32_t; 27643858a1f4SJohn Baldwin 2765399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 27663858a1f4SJohn Baldwin static int 27673858a1f4SJohn Baldwin convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig) 27683858a1f4SJohn Baldwin { 27693858a1f4SJohn Baldwin 27703858a1f4SJohn Baldwin /* 27713858a1f4SJohn Baldwin * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 27723858a1f4SJohn Baldwin * supported by AIO with the old sigevent structure. 27733858a1f4SJohn Baldwin */ 27743858a1f4SJohn Baldwin CP(*osig, *nsig, sigev_notify); 27753858a1f4SJohn Baldwin switch (nsig->sigev_notify) { 27763858a1f4SJohn Baldwin case SIGEV_NONE: 27773858a1f4SJohn Baldwin break; 27783858a1f4SJohn Baldwin case SIGEV_SIGNAL: 27793858a1f4SJohn Baldwin nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 27803858a1f4SJohn Baldwin break; 27813858a1f4SJohn Baldwin case SIGEV_KEVENT: 27823858a1f4SJohn Baldwin nsig->sigev_notify_kqueue = 27833858a1f4SJohn Baldwin osig->__sigev_u.__sigev_notify_kqueue; 27843858a1f4SJohn Baldwin PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr); 27853858a1f4SJohn Baldwin break; 27863858a1f4SJohn Baldwin default: 27873858a1f4SJohn Baldwin return (EINVAL); 27883858a1f4SJohn Baldwin } 27893858a1f4SJohn Baldwin return (0); 27903858a1f4SJohn Baldwin } 27913858a1f4SJohn Baldwin 27923858a1f4SJohn Baldwin static int 2793022ca2fcSAlan Somers aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob, 2794022ca2fcSAlan Somers int type __unused) 27953858a1f4SJohn Baldwin { 27963858a1f4SJohn Baldwin struct oaiocb32 job32; 2797022ca2fcSAlan Somers struct aiocb *kcb = &kjob->uaiocb; 27983858a1f4SJohn Baldwin int error; 27993858a1f4SJohn Baldwin 2800022ca2fcSAlan Somers bzero(kcb, sizeof(struct aiocb)); 28013858a1f4SJohn Baldwin error = copyin(ujob, &job32, sizeof(job32)); 28023858a1f4SJohn Baldwin if (error) 28033858a1f4SJohn Baldwin return (error); 28043858a1f4SJohn Baldwin 2805022ca2fcSAlan Somers /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */ 2806022ca2fcSAlan Somers 2807022ca2fcSAlan Somers CP(job32, *kcb, aio_fildes); 2808022ca2fcSAlan Somers CP(job32, *kcb, aio_offset); 2809022ca2fcSAlan Somers PTRIN_CP(job32, *kcb, aio_buf); 2810022ca2fcSAlan Somers CP(job32, *kcb, aio_nbytes); 2811022ca2fcSAlan Somers CP(job32, *kcb, aio_lio_opcode); 2812022ca2fcSAlan Somers CP(job32, *kcb, aio_reqprio); 2813022ca2fcSAlan Somers CP(job32, *kcb, _aiocb_private.status); 2814022ca2fcSAlan Somers CP(job32, *kcb, _aiocb_private.error); 2815022ca2fcSAlan Somers PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo); 28163858a1f4SJohn Baldwin return (convert_old_sigevent32(&job32.aio_sigevent, 2817022ca2fcSAlan Somers &kcb->aio_sigevent)); 28183858a1f4SJohn Baldwin } 2819399e8c17SJohn Baldwin #endif 28203858a1f4SJohn Baldwin 28213858a1f4SJohn Baldwin static int 2822022ca2fcSAlan Somers aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type) 28233858a1f4SJohn Baldwin { 28243858a1f4SJohn Baldwin struct aiocb32 job32; 2825022ca2fcSAlan Somers struct aiocb *kcb = &kjob->uaiocb; 2826022ca2fcSAlan Somers struct iovec32 *iov32; 28273858a1f4SJohn Baldwin int error; 28283858a1f4SJohn Baldwin 28293858a1f4SJohn Baldwin error = copyin(ujob, &job32, sizeof(job32)); 28303858a1f4SJohn Baldwin if (error) 28313858a1f4SJohn Baldwin return (error); 2832022ca2fcSAlan Somers CP(job32, *kcb, aio_fildes); 2833022ca2fcSAlan Somers CP(job32, *kcb, aio_offset); 2834022ca2fcSAlan Somers CP(job32, *kcb, aio_lio_opcode); 28352884918cSMark Johnston if (type == LIO_NOP) 28362884918cSMark Johnston type = kcb->aio_lio_opcode; 28372247f489SAlan Somers if (type & LIO_VECTORED) { 2838022ca2fcSAlan Somers iov32 = PTRIN(job32.aio_iov); 2839022ca2fcSAlan Somers CP(job32, *kcb, aio_iovcnt); 2840022ca2fcSAlan Somers /* malloc a uio and copy in the iovec */ 2841022ca2fcSAlan Somers error = freebsd32_copyinuio(iov32, 2842022ca2fcSAlan Somers kcb->aio_iovcnt, &kjob->uiop); 2843022ca2fcSAlan Somers if (error) 2844022ca2fcSAlan Somers return (error); 2845022ca2fcSAlan Somers } else { 2846022ca2fcSAlan Somers PTRIN_CP(job32, *kcb, aio_buf); 2847022ca2fcSAlan Somers CP(job32, *kcb, aio_nbytes); 2848022ca2fcSAlan Somers } 2849022ca2fcSAlan Somers CP(job32, *kcb, aio_reqprio); 2850022ca2fcSAlan Somers CP(job32, *kcb, _aiocb_private.status); 2851022ca2fcSAlan Somers CP(job32, *kcb, _aiocb_private.error); 2852022ca2fcSAlan Somers PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo); 2853022ca2fcSAlan Somers error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent); 2854022ca2fcSAlan Somers 2855022ca2fcSAlan Somers return (error); 28563858a1f4SJohn Baldwin } 28573858a1f4SJohn Baldwin 28583858a1f4SJohn Baldwin static long 28593858a1f4SJohn Baldwin aiocb32_fetch_status(struct aiocb *ujob) 28603858a1f4SJohn Baldwin { 28613858a1f4SJohn Baldwin struct aiocb32 *ujob32; 28623858a1f4SJohn Baldwin 28633858a1f4SJohn Baldwin ujob32 = (struct aiocb32 *)ujob; 28643858a1f4SJohn Baldwin return (fuword32(&ujob32->_aiocb_private.status)); 28653858a1f4SJohn Baldwin } 28663858a1f4SJohn Baldwin 28673858a1f4SJohn Baldwin static long 28683858a1f4SJohn Baldwin aiocb32_fetch_error(struct aiocb *ujob) 28693858a1f4SJohn Baldwin { 28703858a1f4SJohn Baldwin struct aiocb32 *ujob32; 28713858a1f4SJohn Baldwin 28723858a1f4SJohn Baldwin ujob32 = (struct aiocb32 *)ujob; 28733858a1f4SJohn Baldwin return (fuword32(&ujob32->_aiocb_private.error)); 28743858a1f4SJohn Baldwin } 28753858a1f4SJohn Baldwin 28763858a1f4SJohn Baldwin static int 28773858a1f4SJohn Baldwin aiocb32_store_status(struct aiocb *ujob, long status) 28783858a1f4SJohn Baldwin { 28793858a1f4SJohn Baldwin struct aiocb32 *ujob32; 28803858a1f4SJohn Baldwin 28813858a1f4SJohn Baldwin ujob32 = (struct aiocb32 *)ujob; 28823858a1f4SJohn Baldwin return (suword32(&ujob32->_aiocb_private.status, status)); 28833858a1f4SJohn Baldwin } 28843858a1f4SJohn Baldwin 28853858a1f4SJohn Baldwin static int 28863858a1f4SJohn Baldwin aiocb32_store_error(struct aiocb *ujob, long error) 28873858a1f4SJohn Baldwin { 28883858a1f4SJohn Baldwin struct aiocb32 *ujob32; 28893858a1f4SJohn Baldwin 28903858a1f4SJohn Baldwin ujob32 = (struct aiocb32 *)ujob; 28913858a1f4SJohn Baldwin return (suword32(&ujob32->_aiocb_private.error, error)); 28923858a1f4SJohn Baldwin } 28933858a1f4SJohn Baldwin 28943858a1f4SJohn Baldwin static int 28953858a1f4SJohn Baldwin aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref) 28963858a1f4SJohn Baldwin { 28973858a1f4SJohn Baldwin struct aiocb32 *ujob32; 28983858a1f4SJohn Baldwin 28993858a1f4SJohn Baldwin ujob32 = (struct aiocb32 *)ujob; 29003858a1f4SJohn Baldwin return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref)); 29013858a1f4SJohn Baldwin } 29023858a1f4SJohn Baldwin 29033858a1f4SJohn Baldwin static int 29043858a1f4SJohn Baldwin aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 29053858a1f4SJohn Baldwin { 29063858a1f4SJohn Baldwin 29073858a1f4SJohn Baldwin return (suword32(ujobp, (long)ujob)); 29083858a1f4SJohn Baldwin } 29093858a1f4SJohn Baldwin 29103858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops = { 2911849aef49SAndrew Turner .aio_copyin = aiocb32_copyin, 29123858a1f4SJohn Baldwin .fetch_status = aiocb32_fetch_status, 29133858a1f4SJohn Baldwin .fetch_error = aiocb32_fetch_error, 29143858a1f4SJohn Baldwin .store_status = aiocb32_store_status, 29153858a1f4SJohn Baldwin .store_error = aiocb32_store_error, 29163858a1f4SJohn Baldwin .store_kernelinfo = aiocb32_store_kernelinfo, 29173858a1f4SJohn Baldwin .store_aiocb = aiocb32_store_aiocb, 29183858a1f4SJohn Baldwin }; 29193858a1f4SJohn Baldwin 2920399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 29213858a1f4SJohn Baldwin static struct aiocb_ops aiocb32_ops_osigevent = { 2922849aef49SAndrew Turner .aio_copyin = aiocb32_copyin_old_sigevent, 29233858a1f4SJohn Baldwin .fetch_status = aiocb32_fetch_status, 29243858a1f4SJohn Baldwin .fetch_error = aiocb32_fetch_error, 29253858a1f4SJohn Baldwin .store_status = aiocb32_store_status, 29263858a1f4SJohn Baldwin .store_error = aiocb32_store_error, 29273858a1f4SJohn Baldwin .store_kernelinfo = aiocb32_store_kernelinfo, 29283858a1f4SJohn Baldwin .store_aiocb = aiocb32_store_aiocb, 29293858a1f4SJohn Baldwin }; 2930399e8c17SJohn Baldwin #endif 29313858a1f4SJohn Baldwin 29323858a1f4SJohn Baldwin int 29333858a1f4SJohn Baldwin freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap) 29343858a1f4SJohn Baldwin { 29353858a1f4SJohn Baldwin 29363858a1f4SJohn Baldwin return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 29373858a1f4SJohn Baldwin } 29383858a1f4SJohn Baldwin 29393858a1f4SJohn Baldwin int 29403858a1f4SJohn Baldwin freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap) 29413858a1f4SJohn Baldwin { 29423858a1f4SJohn Baldwin struct timespec32 ts32; 29433858a1f4SJohn Baldwin struct timespec ts, *tsp; 29443858a1f4SJohn Baldwin struct aiocb **ujoblist; 29453858a1f4SJohn Baldwin uint32_t *ujoblist32; 29463858a1f4SJohn Baldwin int error, i; 29473858a1f4SJohn Baldwin 2948913b9329SAlan Somers if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc) 29493858a1f4SJohn Baldwin return (EINVAL); 29503858a1f4SJohn Baldwin 29513858a1f4SJohn Baldwin if (uap->timeout) { 29523858a1f4SJohn Baldwin /* Get timespec struct. */ 29533858a1f4SJohn Baldwin if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0) 29543858a1f4SJohn Baldwin return (error); 29553858a1f4SJohn Baldwin CP(ts32, ts, tv_sec); 29563858a1f4SJohn Baldwin CP(ts32, ts, tv_nsec); 29573858a1f4SJohn Baldwin tsp = &ts; 29583858a1f4SJohn Baldwin } else 29593858a1f4SJohn Baldwin tsp = NULL; 29603858a1f4SJohn Baldwin 2961913b9329SAlan Somers ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK); 29623858a1f4SJohn Baldwin ujoblist32 = (uint32_t *)ujoblist; 29633858a1f4SJohn Baldwin error = copyin(uap->aiocbp, ujoblist32, uap->nent * 29643858a1f4SJohn Baldwin sizeof(ujoblist32[0])); 29653858a1f4SJohn Baldwin if (error == 0) { 2966df485bdbSAlan Somers for (i = uap->nent - 1; i >= 0; i--) 29673858a1f4SJohn Baldwin ujoblist[i] = PTRIN(ujoblist32[i]); 29683858a1f4SJohn Baldwin 29693858a1f4SJohn Baldwin error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 29703858a1f4SJohn Baldwin } 2971913b9329SAlan Somers free(ujoblist, M_AIOS); 29723858a1f4SJohn Baldwin return (error); 29733858a1f4SJohn Baldwin } 29743858a1f4SJohn Baldwin 29753858a1f4SJohn Baldwin int 29763858a1f4SJohn Baldwin freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap) 29773858a1f4SJohn Baldwin { 29783858a1f4SJohn Baldwin 29793858a1f4SJohn Baldwin return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 29803858a1f4SJohn Baldwin } 29813858a1f4SJohn Baldwin 2982399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 29833858a1f4SJohn Baldwin int 2984399e8c17SJohn Baldwin freebsd6_freebsd32_aio_read(struct thread *td, 2985399e8c17SJohn Baldwin struct freebsd6_freebsd32_aio_read_args *uap) 29863858a1f4SJohn Baldwin { 29873858a1f4SJohn Baldwin 29883858a1f4SJohn Baldwin return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 29893858a1f4SJohn Baldwin &aiocb32_ops_osigevent)); 29903858a1f4SJohn Baldwin } 2991399e8c17SJohn Baldwin #endif 29923858a1f4SJohn Baldwin 29933858a1f4SJohn Baldwin int 29943858a1f4SJohn Baldwin freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap) 29953858a1f4SJohn Baldwin { 29963858a1f4SJohn Baldwin 29973858a1f4SJohn Baldwin return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 29983858a1f4SJohn Baldwin &aiocb32_ops)); 29993858a1f4SJohn Baldwin } 30003858a1f4SJohn Baldwin 3001022ca2fcSAlan Somers int 3002022ca2fcSAlan Somers freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap) 3003022ca2fcSAlan Somers { 3004022ca2fcSAlan Somers 3005022ca2fcSAlan Somers return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV, 3006022ca2fcSAlan Somers &aiocb32_ops)); 3007022ca2fcSAlan Somers } 3008022ca2fcSAlan Somers 3009399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 30103858a1f4SJohn Baldwin int 3011399e8c17SJohn Baldwin freebsd6_freebsd32_aio_write(struct thread *td, 3012399e8c17SJohn Baldwin struct freebsd6_freebsd32_aio_write_args *uap) 30133858a1f4SJohn Baldwin { 30143858a1f4SJohn Baldwin 30153858a1f4SJohn Baldwin return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 30163858a1f4SJohn Baldwin &aiocb32_ops_osigevent)); 30173858a1f4SJohn Baldwin } 3018399e8c17SJohn Baldwin #endif 30193858a1f4SJohn Baldwin 30203858a1f4SJohn Baldwin int 30213858a1f4SJohn Baldwin freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap) 30223858a1f4SJohn Baldwin { 30233858a1f4SJohn Baldwin 30243858a1f4SJohn Baldwin return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 30253858a1f4SJohn Baldwin &aiocb32_ops)); 30263858a1f4SJohn Baldwin } 30273858a1f4SJohn Baldwin 30283858a1f4SJohn Baldwin int 3029022ca2fcSAlan Somers freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap) 3030022ca2fcSAlan Somers { 3031022ca2fcSAlan Somers 3032022ca2fcSAlan Somers return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV, 3033022ca2fcSAlan Somers &aiocb32_ops)); 3034022ca2fcSAlan Somers } 3035022ca2fcSAlan Somers 3036022ca2fcSAlan Somers int 30376160e12cSGleb Smirnoff freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap) 30386160e12cSGleb Smirnoff { 30396160e12cSGleb Smirnoff 30406160e12cSGleb Smirnoff return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK, 30416160e12cSGleb Smirnoff &aiocb32_ops)); 30426160e12cSGleb Smirnoff } 30436160e12cSGleb Smirnoff 30446160e12cSGleb Smirnoff int 30453858a1f4SJohn Baldwin freebsd32_aio_waitcomplete(struct thread *td, 30463858a1f4SJohn Baldwin struct freebsd32_aio_waitcomplete_args *uap) 30473858a1f4SJohn Baldwin { 3048e588eeb1SJohn Baldwin struct timespec32 ts32; 30493858a1f4SJohn Baldwin struct timespec ts, *tsp; 30503858a1f4SJohn Baldwin int error; 30513858a1f4SJohn Baldwin 30523858a1f4SJohn Baldwin if (uap->timeout) { 30533858a1f4SJohn Baldwin /* Get timespec struct. */ 30543858a1f4SJohn Baldwin error = copyin(uap->timeout, &ts32, sizeof(ts32)); 30553858a1f4SJohn Baldwin if (error) 30563858a1f4SJohn Baldwin return (error); 30573858a1f4SJohn Baldwin CP(ts32, ts, tv_sec); 30583858a1f4SJohn Baldwin CP(ts32, ts, tv_nsec); 30593858a1f4SJohn Baldwin tsp = &ts; 30603858a1f4SJohn Baldwin } else 30613858a1f4SJohn Baldwin tsp = NULL; 30623858a1f4SJohn Baldwin 30633858a1f4SJohn Baldwin return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp, 30643858a1f4SJohn Baldwin &aiocb32_ops)); 30653858a1f4SJohn Baldwin } 30663858a1f4SJohn Baldwin 30673858a1f4SJohn Baldwin int 30683858a1f4SJohn Baldwin freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap) 30693858a1f4SJohn Baldwin { 30703858a1f4SJohn Baldwin 30713858a1f4SJohn Baldwin return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp, 30723858a1f4SJohn Baldwin &aiocb32_ops)); 30733858a1f4SJohn Baldwin } 30743858a1f4SJohn Baldwin 3075399e8c17SJohn Baldwin #ifdef COMPAT_FREEBSD6 30763858a1f4SJohn Baldwin int 3077399e8c17SJohn Baldwin freebsd6_freebsd32_lio_listio(struct thread *td, 3078399e8c17SJohn Baldwin struct freebsd6_freebsd32_lio_listio_args *uap) 30793858a1f4SJohn Baldwin { 30803858a1f4SJohn Baldwin struct aiocb **acb_list; 30813858a1f4SJohn Baldwin struct sigevent *sigp, sig; 30823858a1f4SJohn Baldwin struct osigevent32 osig; 30833858a1f4SJohn Baldwin uint32_t *acb_list32; 30843858a1f4SJohn Baldwin int error, i, nent; 30853858a1f4SJohn Baldwin 30863858a1f4SJohn Baldwin if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 30873858a1f4SJohn Baldwin return (EINVAL); 30883858a1f4SJohn Baldwin 30893858a1f4SJohn Baldwin nent = uap->nent; 3090913b9329SAlan Somers if (nent < 0 || nent > max_aio_queue_per_proc) 30913858a1f4SJohn Baldwin return (EINVAL); 30923858a1f4SJohn Baldwin 30933858a1f4SJohn Baldwin if (uap->sig && (uap->mode == LIO_NOWAIT)) { 30943858a1f4SJohn Baldwin error = copyin(uap->sig, &osig, sizeof(osig)); 30953858a1f4SJohn Baldwin if (error) 30963858a1f4SJohn Baldwin return (error); 30973858a1f4SJohn Baldwin error = convert_old_sigevent32(&osig, &sig); 30983858a1f4SJohn Baldwin if (error) 30993858a1f4SJohn Baldwin return (error); 31003858a1f4SJohn Baldwin sigp = &sig; 31013858a1f4SJohn Baldwin } else 31023858a1f4SJohn Baldwin sigp = NULL; 31033858a1f4SJohn Baldwin 31043858a1f4SJohn Baldwin acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 31053858a1f4SJohn Baldwin error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 31063858a1f4SJohn Baldwin if (error) { 31073858a1f4SJohn Baldwin free(acb_list32, M_LIO); 31083858a1f4SJohn Baldwin return (error); 31093858a1f4SJohn Baldwin } 31103858a1f4SJohn Baldwin acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 31113858a1f4SJohn Baldwin for (i = 0; i < nent; i++) 31123858a1f4SJohn Baldwin acb_list[i] = PTRIN(acb_list32[i]); 31133858a1f4SJohn Baldwin free(acb_list32, M_LIO); 31143858a1f4SJohn Baldwin 31153858a1f4SJohn Baldwin error = kern_lio_listio(td, uap->mode, 31163858a1f4SJohn Baldwin (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 31173858a1f4SJohn Baldwin &aiocb32_ops_osigevent); 31183858a1f4SJohn Baldwin free(acb_list, M_LIO); 31193858a1f4SJohn Baldwin return (error); 31203858a1f4SJohn Baldwin } 3121399e8c17SJohn Baldwin #endif 31223858a1f4SJohn Baldwin 31233858a1f4SJohn Baldwin int 31243858a1f4SJohn Baldwin freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap) 31253858a1f4SJohn Baldwin { 31263858a1f4SJohn Baldwin struct aiocb **acb_list; 31273858a1f4SJohn Baldwin struct sigevent *sigp, sig; 31283858a1f4SJohn Baldwin struct sigevent32 sig32; 31293858a1f4SJohn Baldwin uint32_t *acb_list32; 31303858a1f4SJohn Baldwin int error, i, nent; 31313858a1f4SJohn Baldwin 31323858a1f4SJohn Baldwin if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 31333858a1f4SJohn Baldwin return (EINVAL); 31343858a1f4SJohn Baldwin 31353858a1f4SJohn Baldwin nent = uap->nent; 3136913b9329SAlan Somers if (nent < 0 || nent > max_aio_queue_per_proc) 31373858a1f4SJohn Baldwin return (EINVAL); 31383858a1f4SJohn Baldwin 31393858a1f4SJohn Baldwin if (uap->sig && (uap->mode == LIO_NOWAIT)) { 31403858a1f4SJohn Baldwin error = copyin(uap->sig, &sig32, sizeof(sig32)); 31413858a1f4SJohn Baldwin if (error) 31423858a1f4SJohn Baldwin return (error); 31433858a1f4SJohn Baldwin error = convert_sigevent32(&sig32, &sig); 31443858a1f4SJohn Baldwin if (error) 31453858a1f4SJohn Baldwin return (error); 31463858a1f4SJohn Baldwin sigp = &sig; 31473858a1f4SJohn Baldwin } else 31483858a1f4SJohn Baldwin sigp = NULL; 31493858a1f4SJohn Baldwin 31503858a1f4SJohn Baldwin acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 31513858a1f4SJohn Baldwin error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 31523858a1f4SJohn Baldwin if (error) { 31533858a1f4SJohn Baldwin free(acb_list32, M_LIO); 31543858a1f4SJohn Baldwin return (error); 31553858a1f4SJohn Baldwin } 31563858a1f4SJohn Baldwin acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 31573858a1f4SJohn Baldwin for (i = 0; i < nent; i++) 31583858a1f4SJohn Baldwin acb_list[i] = PTRIN(acb_list32[i]); 31593858a1f4SJohn Baldwin free(acb_list32, M_LIO); 31603858a1f4SJohn Baldwin 31613858a1f4SJohn Baldwin error = kern_lio_listio(td, uap->mode, 31623858a1f4SJohn Baldwin (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 31633858a1f4SJohn Baldwin &aiocb32_ops); 31643858a1f4SJohn Baldwin free(acb_list, M_LIO); 31653858a1f4SJohn Baldwin return (error); 31663858a1f4SJohn Baldwin } 31673858a1f4SJohn Baldwin 31683858a1f4SJohn Baldwin #endif 3169