source: mainline/uspace/lib/c/generic/bd_srv.c@ 39330200

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 39330200 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
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
29/** @addtogroup libc
30 * @{
31 */
32/**
33 * @file
34 * @brief Block device server stub
35 */
36#include <errno.h>
37#include <ipc/bd.h>
38#include <macros.h>
39#include <stdlib.h>
40#include <stddef.h>
41#include <stdint.h>
42
43#include <bd_srv.h>
44
45static void bd_read_blocks_srv(bd_srv_t *srv, ipc_callid_t callid,
46 ipc_call_t *call)
47{
48 aoff64_t ba;
49 size_t cnt;
50 void *buf;
51 size_t size;
52 errno_t rc;
53 ipc_callid_t rcallid;
54
55 ba = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
56 cnt = IPC_GET_ARG3(*call);
57
58 if (!async_data_read_receive(&rcallid, &size)) {
59 async_answer_0(callid, EINVAL);
60 return;
61 }
62
63 buf = malloc(size);
64 if (buf == NULL) {
65 async_answer_0(rcallid, ENOMEM);
66 async_answer_0(callid, ENOMEM);
67 return;
68 }
69
70 if (srv->srvs->ops->read_blocks == NULL) {
71 async_answer_0(rcallid, ENOTSUP);
72 async_answer_0(callid, ENOTSUP);
73 free(buf);
74 return;
75 }
76
77 rc = srv->srvs->ops->read_blocks(srv, ba, cnt, buf, size);
78 if (rc != EOK) {
79 async_answer_0(rcallid, ENOMEM);
80 async_answer_0(callid, ENOMEM);
81 free(buf);
82 return;
83 }
84
85 async_data_read_finalize(rcallid, buf, size);
86
87 free(buf);
88 async_answer_0(callid, EOK);
89}
90
91static void bd_read_toc_srv(bd_srv_t *srv, ipc_callid_t callid,
92 ipc_call_t *call)
93{
94 uint8_t session;
95 void *buf;
96 size_t size;
97 errno_t rc;
98 ipc_callid_t rcallid;
99
100 session = IPC_GET_ARG1(*call);
101
102 if (!async_data_read_receive(&rcallid, &size)) {
103 async_answer_0(callid, EINVAL);
104 return;
105 }
106
107 buf = malloc(size);
108 if (buf == NULL) {
109 async_answer_0(rcallid, ENOMEM);
110 async_answer_0(callid, ENOMEM);
111 return;
112 }
113
114 if (srv->srvs->ops->read_toc == NULL) {
115 async_answer_0(rcallid, ENOTSUP);
116 async_answer_0(callid, ENOTSUP);
117 free(buf);
118 return;
119 }
120
121 rc = srv->srvs->ops->read_toc(srv, session, buf, size);
122 if (rc != EOK) {
123 async_answer_0(rcallid, ENOMEM);
124 async_answer_0(callid, ENOMEM);
125 free(buf);
126 return;
127 }
128
129 async_data_read_finalize(rcallid, buf, size);
130
131 free(buf);
132 async_answer_0(callid, EOK);
133}
134
135static void bd_sync_cache_srv(bd_srv_t *srv, ipc_callid_t callid,
136 ipc_call_t *call)
137{
138 aoff64_t ba;
139 size_t cnt;
140 errno_t rc;
141
142 ba = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
143 cnt = IPC_GET_ARG3(*call);
144
145 if (srv->srvs->ops->sync_cache == NULL) {
146 async_answer_0(callid, ENOTSUP);
147 return;
148 }
149
150 rc = srv->srvs->ops->sync_cache(srv, ba, cnt);
151 async_answer_0(callid, rc);
152}
153
154static void bd_write_blocks_srv(bd_srv_t *srv, ipc_callid_t callid,
155 ipc_call_t *call)
156{
157 aoff64_t ba;
158 size_t cnt;
159 void *data;
160 size_t size;
161 errno_t rc;
162
163 ba = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
164 cnt = IPC_GET_ARG3(*call);
165
166 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
167 if (rc != EOK) {
168 async_answer_0(callid, rc);
169 return;
170 }
171
172 if (srv->srvs->ops->write_blocks == NULL) {
173 async_answer_0(callid, ENOTSUP);
174 return;
175 }
176
177 rc = srv->srvs->ops->write_blocks(srv, ba, cnt, data, size);
178 free(data);
179 async_answer_0(callid, rc);
180}
181
182static void bd_get_block_size_srv(bd_srv_t *srv, ipc_callid_t callid,
183 ipc_call_t *call)
184{
185 errno_t rc;
186 size_t block_size;
187
188 if (srv->srvs->ops->get_block_size == NULL) {
189 async_answer_0(callid, ENOTSUP);
190 return;
191 }
192
193 rc = srv->srvs->ops->get_block_size(srv, &block_size);
194 async_answer_1(callid, rc, block_size);
195}
196
197static void bd_get_num_blocks_srv(bd_srv_t *srv, ipc_callid_t callid,
198 ipc_call_t *call)
199{
200 errno_t rc;
201 aoff64_t num_blocks;
202
203 if (srv->srvs->ops->get_num_blocks == NULL) {
204 async_answer_0(callid, ENOTSUP);
205 return;
206 }
207
208 rc = srv->srvs->ops->get_num_blocks(srv, &num_blocks);
209 async_answer_2(callid, rc, LOWER32(num_blocks), UPPER32(num_blocks));
210}
211
212static bd_srv_t *bd_srv_create(bd_srvs_t *srvs)
213{
214 bd_srv_t *srv;
215
216 srv = calloc(1, sizeof(bd_srv_t));
217 if (srv == NULL)
218 return NULL;
219
220 srv->srvs = srvs;
221 return srv;
222}
223
224void bd_srvs_init(bd_srvs_t *srvs)
225{
226 srvs->ops = NULL;
227 srvs->sarg = NULL;
228}
229
230errno_t bd_conn(ipc_callid_t iid, ipc_call_t *icall, bd_srvs_t *srvs)
231{
232 bd_srv_t *srv;
233 errno_t rc;
234
235 /* Accept the connection */
236 async_answer_0(iid, EOK);
237
238 srv = bd_srv_create(srvs);
239 if (srv == NULL)
240 return ENOMEM;
241
242 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
243 if (sess == NULL)
244 return ENOMEM;
245
246 srv->client_sess = sess;
247
248 rc = srvs->ops->open(srvs, srv);
249 if (rc != EOK)
250 return rc;
251
252 while (true) {
253 ipc_call_t call;
254 ipc_callid_t callid = async_get_call(&call);
255 sysarg_t method = IPC_GET_IMETHOD(call);
256
257 if (!method) {
258 /* The other side has hung up */
259 async_answer_0(callid, EOK);
260 break;
261 }
262
263 switch (method) {
264 case BD_READ_BLOCKS:
265 bd_read_blocks_srv(srv, callid, &call);
266 break;
267 case BD_READ_TOC:
268 bd_read_toc_srv(srv, callid, &call);
269 break;
270 case BD_SYNC_CACHE:
271 bd_sync_cache_srv(srv, callid, &call);
272 break;
273 case BD_WRITE_BLOCKS:
274 bd_write_blocks_srv(srv, callid, &call);
275 break;
276 case BD_GET_BLOCK_SIZE:
277 bd_get_block_size_srv(srv, callid, &call);
278 break;
279 case BD_GET_NUM_BLOCKS:
280 bd_get_num_blocks_srv(srv, callid, &call);
281 break;
282 default:
283 async_answer_0(callid, EINVAL);
284 }
285 }
286
287 rc = srvs->ops->close(srv);
288 free(srv);
289
290 return rc;
291}
292
293/** @}
294 */
Note: See TracBrowser for help on using the repository browser.