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: head/lib/librt/aio.c 157078 2006-03-24 03:24:27Z davidxu $
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
53typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
54
55extern int __sys_aio_read(struct aiocb *iocb);
56extern int __sys_aio_write(struct aiocb *iocb);
57extern int __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
58extern int __sys_aio_return(struct aiocb *iocb);
59extern int __sys_aio_error(struct aiocb *iocb);
60extern int __sys_aio_fsync(int op, struct aiocb *iocb);
61
62static void
63aio_dispatch(struct sigev_node *sn)
64{
65 aio_func f = sn->sn_func;
66
67 f(sn->sn_value, (struct aiocb *)sn->sn_id);
68}
69
70static int
71aio_sigev_alloc(struct aiocb *iocb, struct sigev_node **sn,
72 struct sigevent *saved_ev)
73{
74 if (__sigev_check_init()) {
75 /* This might be that thread library is not enabled. */
76 errno = EINVAL;
77 return (-1);
78 }
79
80 *sn = __sigev_alloc(SI_ASYNCIO, &iocb->aio_sigevent, NULL, 1);
81 if (*sn == NULL) {
82 errno = EAGAIN;
83 return (-1);
84 }
85
86 *saved_ev = iocb->aio_sigevent;
87 (*sn)->sn_id = (sigev_id_t)iocb;
88 __sigev_get_sigevent(*sn, &iocb->aio_sigevent, (*sn)->sn_id);
89 (*sn)->sn_dispatch = aio_dispatch;
90
91 __sigev_list_lock();
92 __sigev_register(*sn);
93 __sigev_list_unlock();
94
95 return (0);
96}
97
98static int
99aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
100{
101 struct sigev_node *sn;
102 struct sigevent saved_ev;
103 int ret, err;
104
105 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
106 ret = sysfunc(iocb);
107 return (ret);
108 }
109
110 ret = aio_sigev_alloc(iocb, &sn, &saved_ev);
111 if (ret)
112 return (ret);
113 ret = sysfunc(iocb);
114 iocb->aio_sigevent = saved_ev;
115 if (ret != 0) {
116 err = errno;
117 __sigev_list_lock();
118 __sigev_delete_node(sn);
119 __sigev_list_unlock();
120 errno = err;
121 }
122 return (ret);
123}
124
125int
126__aio_read(struct aiocb *iocb)
127{
128
129 return aio_io(iocb, &__sys_aio_read);
130}
131
132int
133__aio_write(struct aiocb *iocb)
134{
135
136 return aio_io(iocb, &__sys_aio_write);
137}
138
139int
140__aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
141{
142 int err;
143 int ret = __sys_aio_waitcomplete(iocbp, timeout);
144
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
158int
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 return (EINPROGRESS);
165 __sigev_list_lock();
166 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
167 __sigev_list_unlock();
168 }
169
170 return __sys_aio_return(iocb);
171}
172
173int
174__aio_fsync(int op, struct aiocb *iocb)
175{
176 struct sigev_node *sn;
177 struct sigevent saved_ev;
178 int ret, err;
179
180 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD)
181 return __sys_aio_fsync(op, iocb);
182
183 ret = aio_sigev_alloc(iocb, &sn, &saved_ev);
184 if (ret)
185 return (ret);
186 ret = __sys_aio_fsync(op, iocb);
187 iocb->aio_sigevent = saved_ev;
188 if (ret != 0) {
189 err = errno;
190 __sigev_list_lock();
191 __sigev_delete_node(sn);
192 __sigev_list_unlock();
193 errno = err;
194 }
195 return (ret);
196}
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: head/lib/librt/aio.c 157078 2006-03-24 03:24:27Z davidxu $
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
53typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
54
55extern int __sys_aio_read(struct aiocb *iocb);
56extern int __sys_aio_write(struct aiocb *iocb);
57extern int __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
58extern int __sys_aio_return(struct aiocb *iocb);
59extern int __sys_aio_error(struct aiocb *iocb);
60extern int __sys_aio_fsync(int op, struct aiocb *iocb);
61
62static void
63aio_dispatch(struct sigev_node *sn)
64{
65 aio_func f = sn->sn_func;
66
67 f(sn->sn_value, (struct aiocb *)sn->sn_id);
68}
69
70static int
71aio_sigev_alloc(struct aiocb *iocb, struct sigev_node **sn,
72 struct sigevent *saved_ev)
73{
74 if (__sigev_check_init()) {
75 /* This might be that thread library is not enabled. */
76 errno = EINVAL;
77 return (-1);
78 }
79
80 *sn = __sigev_alloc(SI_ASYNCIO, &iocb->aio_sigevent, NULL, 1);
81 if (*sn == NULL) {
82 errno = EAGAIN;
83 return (-1);
84 }
85
86 *saved_ev = iocb->aio_sigevent;
87 (*sn)->sn_id = (sigev_id_t)iocb;
88 __sigev_get_sigevent(*sn, &iocb->aio_sigevent, (*sn)->sn_id);
89 (*sn)->sn_dispatch = aio_dispatch;
90
91 __sigev_list_lock();
92 __sigev_register(*sn);
93 __sigev_list_unlock();
94
95 return (0);
96}
97
98static int
99aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
100{
101 struct sigev_node *sn;
102 struct sigevent saved_ev;
103 int ret, err;
104
105 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
106 ret = sysfunc(iocb);
107 return (ret);
108 }
109
110 ret = aio_sigev_alloc(iocb, &sn, &saved_ev);
111 if (ret)
112 return (ret);
113 ret = sysfunc(iocb);
114 iocb->aio_sigevent = saved_ev;
115 if (ret != 0) {
116 err = errno;
117 __sigev_list_lock();
118 __sigev_delete_node(sn);
119 __sigev_list_unlock();
120 errno = err;
121 }
122 return (ret);
123}
124
125int
126__aio_read(struct aiocb *iocb)
127{
128
129 return aio_io(iocb, &__sys_aio_read);
130}
131
132int
133__aio_write(struct aiocb *iocb)
134{
135
136 return aio_io(iocb, &__sys_aio_write);
137}
138
139int
140__aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
141{
142 int err;
143 int ret = __sys_aio_waitcomplete(iocbp, timeout);
144
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
158int
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 return (EINPROGRESS);
165 __sigev_list_lock();
166 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
167 __sigev_list_unlock();
168 }
169
170 return __sys_aio_return(iocb);
171}
172
173int
174__aio_fsync(int op, struct aiocb *iocb)
175{
176 struct sigev_node *sn;
177 struct sigevent saved_ev;
178 int ret, err;
179
180 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD)
181 return __sys_aio_fsync(op, iocb);
182
183 ret = aio_sigev_alloc(iocb, &sn, &saved_ev);
184 if (ret)
185 return (ret);
186 ret = __sys_aio_fsync(op, iocb);
187 iocb->aio_sigevent = saved_ev;
188 if (ret != 0) {
189 err = errno;
190 __sigev_list_lock();
191 __sigev_delete_node(sn);
192 __sigev_list_unlock();
193 errno = err;
194 }
195 return (ret);
196}