1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30#include <sys/types.h> 31#include <sys/syscall.h> 32#include <sys/aio.h> 33 34#include "namespace.h" 35#include <errno.h> 36#include <stddef.h> 37#include <signal.h> 38#include "sigev_thread.h" 39#include "un-namespace.h" 40 41__weak_reference(__aio_read, aio_read); 42__weak_reference(__aio_readv, aio_readv); 43__weak_reference(__aio_write, aio_write); 44__weak_reference(__aio_writev, aio_writev); 45__weak_reference(__aio_return, aio_return); 46__weak_reference(__aio_waitcomplete, aio_waitcomplete); 47__weak_reference(__aio_fsync, aio_fsync); 48__weak_reference(__lio_listio, lio_listio); 49 50typedef void (*aio_func)(union sigval val, struct aiocb *iocb); 51 52extern int __sys_aio_read(struct aiocb *iocb); 53extern int __sys_aio_readv(struct aiocb *iocb); 54extern int __sys_aio_write(struct aiocb *iocb); 55extern int __sys_aio_writev(struct aiocb *iocb); 56extern ssize_t __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout); 57extern ssize_t __sys_aio_return(struct aiocb *iocb); 58extern int __sys_aio_error(struct aiocb *iocb); 59extern int __sys_aio_fsync(int op, struct aiocb *iocb); 60extern int __sys_lio_listio(int mode, struct aiocb * const list[], int nent, 61 struct sigevent *sig); 62 63static void 64aio_dispatch(struct sigev_node *sn) 65{ 66 aio_func f = sn->sn_func; 67 68 f(sn->sn_value, (struct aiocb *)sn->sn_id); 69} 70 71static int 72aio_sigev_alloc(sigev_id_t id, struct sigevent *sigevent, 73 struct sigev_node **sn, struct sigevent *saved_ev) 74{ 75 if (__sigev_check_init()) { 76 /* This might be that thread library is not enabled. */ 77 errno = EINVAL; 78 return (-1); 79 } 80 81 *sn = __sigev_alloc(SI_ASYNCIO, sigevent, NULL, 1); 82 if (*sn == NULL) { 83 errno = EAGAIN; 84 return (-1); 85 } 86 87 *saved_ev = *sigevent; 88 (*sn)->sn_id = id; 89 __sigev_get_sigevent(*sn, sigevent, (*sn)->sn_id); 90 (*sn)->sn_dispatch = aio_dispatch; 91 92 __sigev_list_lock(); 93 __sigev_register(*sn); 94 __sigev_list_unlock(); 95 96 return (0); 97} 98 99static int 100aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb)) 101{ 102 struct sigev_node *sn; 103 struct sigevent saved_ev; 104 int ret, err; 105 106 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) { 107 ret = sysfunc(iocb); 108 return (ret); 109 } 110 111 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn, 112 &saved_ev); 113 if (ret) 114 return (ret); 115 ret = sysfunc(iocb); 116 iocb->aio_sigevent = saved_ev; 117 if (ret != 0) { 118 err = errno; 119 __sigev_list_lock(); 120 __sigev_delete_node(sn); 121 __sigev_list_unlock(); 122 errno = err; 123 } 124 return (ret); 125} 126 127int 128__aio_read(struct aiocb *iocb) 129{ 130 131 return aio_io(iocb, &__sys_aio_read); 132} 133 134int 135__aio_readv(struct aiocb *iocb) 136{ 137 138 return aio_io(iocb, &__sys_aio_readv); 139} 140 141int 142__aio_write(struct aiocb *iocb) 143{ 144 145 return aio_io(iocb, &__sys_aio_write); 146} 147 148int 149__aio_writev(struct aiocb *iocb) 150{ 151 152 return aio_io(iocb, &__sys_aio_writev); 153} 154 155ssize_t 156__aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout) 157{ 158 ssize_t ret; 159 int err; 160 161 ret = __sys_aio_waitcomplete(iocbp, timeout); 162 if (*iocbp) { 163 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) { 164 err = errno; 165 __sigev_list_lock(); 166 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp)); 167 __sigev_list_unlock(); 168 errno = err; 169 } 170 } 171 172 return (ret); 173} 174 175ssize_t 176__aio_return(struct aiocb *iocb) 177{ 178 179 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) { 180 if (__sys_aio_error(iocb) == EINPROGRESS) { 181 /* 182 * Fail with EINVAL to match the semantics of 183 * __sys_aio_return() for an in-progress 184 * request. 185 */ 186 errno = EINVAL; 187 return (-1); 188 } 189 __sigev_list_lock(); 190 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb); 191 __sigev_list_unlock(); 192 } 193 194 return __sys_aio_return(iocb); 195} 196 197int 198__aio_fsync(int op, struct aiocb *iocb) 199{ 200 struct sigev_node *sn; 201 struct sigevent saved_ev; 202 int ret, err; 203 204 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) 205 return __sys_aio_fsync(op, iocb); 206 207 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn, 208 &saved_ev); 209 if (ret) 210 return (ret); 211 ret = __sys_aio_fsync(op, iocb); 212 iocb->aio_sigevent = saved_ev; 213 if (ret != 0) { 214 err = errno; 215 __sigev_list_lock(); 216 __sigev_delete_node(sn); 217 __sigev_list_unlock(); 218 errno = err; 219 } 220 return (ret); 221} 222 223int 224__lio_listio(int mode, struct aiocb * const list[], int nent, 225 struct sigevent *sig) 226{ 227 struct sigev_node *sn; 228 struct sigevent saved_ev; 229 int ret, err; 230 231 if (sig == NULL || sig->sigev_notify != SIGEV_THREAD) 232 return (__sys_lio_listio(mode, list, nent, sig)); 233 234 ret = aio_sigev_alloc((sigev_id_t)list, sig, &sn, &saved_ev); 235 if (ret) 236 return (ret); 237 ret = __sys_lio_listio(mode, list, nent, sig); 238 *sig = saved_ev; 239 if (ret != 0) { 240 err = errno; 241 __sigev_list_lock(); 242 __sigev_delete_node(sn); 243 __sigev_list_unlock(); 244 errno = err; 245 } 246 return (ret); 247} 248