]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/librt/aio.c
Merge ^/head r321239 through r321306.
[FreeBSD/FreeBSD.git] / lib / librt / aio.c
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
48 typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
49
50 extern int __sys_aio_read(struct aiocb *iocb);
51 extern int __sys_aio_write(struct aiocb *iocb);
52 extern ssize_t __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
53 extern ssize_t __sys_aio_return(struct aiocb *iocb);
54 extern int __sys_aio_error(struct aiocb *iocb);
55 extern int __sys_aio_fsync(int op, struct aiocb *iocb);
56
57 static void
58 aio_dispatch(struct sigev_node *sn)
59 {
60         aio_func f = sn->sn_func;
61
62         f(sn->sn_value, (struct aiocb *)sn->sn_id);
63 }
64
65 static int
66 aio_sigev_alloc(struct aiocb *iocb, struct sigev_node **sn,
67         struct sigevent *saved_ev)
68 {
69         if (__sigev_check_init()) {
70                 /* This might be that thread library is not enabled. */
71                 errno = EINVAL;
72                 return (-1);
73         }
74
75         *sn = __sigev_alloc(SI_ASYNCIO, &iocb->aio_sigevent, NULL, 1);
76         if (*sn == NULL) {
77                 errno = EAGAIN;
78                 return (-1);
79         }
80         
81         *saved_ev = iocb->aio_sigevent;
82         (*sn)->sn_id = (sigev_id_t)iocb;
83         __sigev_get_sigevent(*sn, &iocb->aio_sigevent, (*sn)->sn_id);
84         (*sn)->sn_dispatch = aio_dispatch;
85
86         __sigev_list_lock();
87         __sigev_register(*sn);
88         __sigev_list_unlock();
89
90         return (0);
91 }
92
93 static int
94 aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
95 {
96         struct sigev_node *sn;
97         struct sigevent saved_ev;
98         int ret, err;
99
100         if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
101                 ret = sysfunc(iocb);
102                 return (ret);
103         }
104
105         ret = aio_sigev_alloc(iocb, &sn, &saved_ev);
106         if (ret)
107                 return (ret);
108         ret = sysfunc(iocb);
109         iocb->aio_sigevent = saved_ev;
110         if (ret != 0) {
111                 err = errno;
112                 __sigev_list_lock();
113                 __sigev_delete_node(sn);
114                 __sigev_list_unlock();
115                 errno = err;
116         }
117         return (ret);
118 }
119
120 int
121 __aio_read(struct aiocb *iocb)
122 {
123
124         return aio_io(iocb, &__sys_aio_read);
125 }
126
127 int
128 __aio_write(struct aiocb *iocb)
129 {
130
131         return aio_io(iocb, &__sys_aio_write);
132 }
133
134 ssize_t
135 __aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
136 {
137         ssize_t ret;
138         int err;
139
140         ret = __sys_aio_waitcomplete(iocbp, timeout);
141         if (*iocbp) {
142                 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
143                         err = errno;
144                         __sigev_list_lock();
145                         __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
146                         __sigev_list_unlock();
147                         errno = err;
148                 }
149         }
150
151         return (ret);
152 }
153
154 ssize_t
155 __aio_return(struct aiocb *iocb)
156 {
157
158         if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
159                 if (__sys_aio_error(iocb) == EINPROGRESS) {
160                         /*
161                          * Fail with EINVAL to match the semantics of
162                          * __sys_aio_return() for an in-progress
163                          * request.
164                          */
165                         errno = EINVAL;
166                         return (-1);
167                 }
168                 __sigev_list_lock();
169                 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
170                 __sigev_list_unlock();
171         }
172
173         return __sys_aio_return(iocb);
174 }
175
176 int
177 __aio_fsync(int op, struct aiocb *iocb)
178 {
179         struct sigev_node *sn;
180         struct sigevent saved_ev;
181         int ret, err;
182
183         if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD)
184                 return __sys_aio_fsync(op, iocb);
185
186         ret = aio_sigev_alloc(iocb, &sn, &saved_ev);
187         if (ret)
188                 return (ret);
189         ret = __sys_aio_fsync(op, iocb);
190         iocb->aio_sigevent = saved_ev;
191         if (ret != 0) {
192                 err = errno;
193                 __sigev_list_lock();
194                 __sigev_delete_node(sn);
195                 __sigev_list_unlock();
196                 errno = err;
197         }
198         return (ret);
199 }