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