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