1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * $FreeBSD$ 29 * 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/types.h> 34 #include <sys/syscall.h> 35 #include <sys/aio.h> 36 37 #include "namespace.h" 38 #include <errno.h> 39 #include <stddef.h> 40 #include <signal.h> 41 #include "sigev_thread.h" 42 #include "un-namespace.h" 43 44 __weak_reference(__aio_read, aio_read); 45 __weak_reference(__aio_readv, aio_readv); 46 __weak_reference(__aio_write, aio_write); 47 __weak_reference(__aio_writev, aio_writev); 48 __weak_reference(__aio_return, aio_return); 49 __weak_reference(__aio_waitcomplete, aio_waitcomplete); 50 __weak_reference(__aio_fsync, aio_fsync); 51 __weak_reference(__lio_listio, lio_listio); 52 53 typedef void (*aio_func)(union sigval val, struct aiocb *iocb); 54 55 extern int __sys_aio_read(struct aiocb *iocb); 56 extern int __sys_aio_readv(struct aiocb *iocb); 57 extern int __sys_aio_write(struct aiocb *iocb); 58 extern int __sys_aio_writev(struct aiocb *iocb); 59 extern ssize_t __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout); 60 extern ssize_t __sys_aio_return(struct aiocb *iocb); 61 extern int __sys_aio_error(struct aiocb *iocb); 62 extern int __sys_aio_fsync(int op, struct aiocb *iocb); 63 extern int __sys_lio_listio(int mode, struct aiocb * const list[], int nent, 64 struct sigevent *sig); 65 66 static void 67 aio_dispatch(struct sigev_node *sn) 68 { 69 aio_func f = sn->sn_func; 70 71 f(sn->sn_value, (struct aiocb *)sn->sn_id); 72 } 73 74 static int 75 aio_sigev_alloc(sigev_id_t id, struct sigevent *sigevent, 76 struct sigev_node **sn, struct sigevent *saved_ev) 77 { 78 if (__sigev_check_init()) { 79 /* This might be that thread library is not enabled. */ 80 errno = EINVAL; 81 return (-1); 82 } 83 84 *sn = __sigev_alloc(SI_ASYNCIO, sigevent, NULL, 1); 85 if (*sn == NULL) { 86 errno = EAGAIN; 87 return (-1); 88 } 89 90 *saved_ev = *sigevent; 91 (*sn)->sn_id = id; 92 __sigev_get_sigevent(*sn, sigevent, (*sn)->sn_id); 93 (*sn)->sn_dispatch = aio_dispatch; 94 95 __sigev_list_lock(); 96 __sigev_register(*sn); 97 __sigev_list_unlock(); 98 99 return (0); 100 } 101 102 static int 103 aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb)) 104 { 105 struct sigev_node *sn; 106 struct sigevent saved_ev; 107 int ret, err; 108 109 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) { 110 ret = sysfunc(iocb); 111 return (ret); 112 } 113 114 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn, 115 &saved_ev); 116 if (ret) 117 return (ret); 118 ret = sysfunc(iocb); 119 iocb->aio_sigevent = saved_ev; 120 if (ret != 0) { 121 err = errno; 122 __sigev_list_lock(); 123 __sigev_delete_node(sn); 124 __sigev_list_unlock(); 125 errno = err; 126 } 127 return (ret); 128 } 129 130 int 131 __aio_read(struct aiocb *iocb) 132 { 133 134 return aio_io(iocb, &__sys_aio_read); 135 } 136 137 int 138 __aio_readv(struct aiocb *iocb) 139 { 140 141 return aio_io(iocb, &__sys_aio_readv); 142 } 143 144 int 145 __aio_write(struct aiocb *iocb) 146 { 147 148 return aio_io(iocb, &__sys_aio_write); 149 } 150 151 int 152 __aio_writev(struct aiocb *iocb) 153 { 154 155 return aio_io(iocb, &__sys_aio_writev); 156 } 157 158 ssize_t 159 __aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout) 160 { 161 ssize_t ret; 162 int err; 163 164 ret = __sys_aio_waitcomplete(iocbp, timeout); 165 if (*iocbp) { 166 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) { 167 err = errno; 168 __sigev_list_lock(); 169 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp)); 170 __sigev_list_unlock(); 171 errno = err; 172 } 173 } 174 175 return (ret); 176 } 177 178 ssize_t 179 __aio_return(struct aiocb *iocb) 180 { 181 182 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) { 183 if (__sys_aio_error(iocb) == EINPROGRESS) { 184 /* 185 * Fail with EINVAL to match the semantics of 186 * __sys_aio_return() for an in-progress 187 * request. 188 */ 189 errno = EINVAL; 190 return (-1); 191 } 192 __sigev_list_lock(); 193 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb); 194 __sigev_list_unlock(); 195 } 196 197 return __sys_aio_return(iocb); 198 } 199 200 int 201 __aio_fsync(int op, struct aiocb *iocb) 202 { 203 struct sigev_node *sn; 204 struct sigevent saved_ev; 205 int ret, err; 206 207 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) 208 return __sys_aio_fsync(op, iocb); 209 210 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn, 211 &saved_ev); 212 if (ret) 213 return (ret); 214 ret = __sys_aio_fsync(op, iocb); 215 iocb->aio_sigevent = saved_ev; 216 if (ret != 0) { 217 err = errno; 218 __sigev_list_lock(); 219 __sigev_delete_node(sn); 220 __sigev_list_unlock(); 221 errno = err; 222 } 223 return (ret); 224 } 225 226 int 227 __lio_listio(int mode, struct aiocb * const list[], int nent, 228 struct sigevent *sig) 229 { 230 struct sigev_node *sn; 231 struct sigevent saved_ev; 232 int ret, err; 233 234 if (sig == NULL || sig->sigev_notify != SIGEV_THREAD) 235 return (__sys_lio_listio(mode, list, nent, sig)); 236 237 ret = aio_sigev_alloc((sigev_id_t)list, sig, &sn, &saved_ev); 238 if (ret) 239 return (ret); 240 ret = __sys_lio_listio(mode, list, nent, sig); 241 *sig = saved_ev; 242 if (ret != 0) { 243 err = errno; 244 __sigev_list_lock(); 245 __sigev_delete_node(sn); 246 __sigev_list_unlock(); 247 errno = err; 248 } 249 return (ret); 250 } 251